|
| 1 | +import { VIRO_VERSION } from "../Utilities/ViroVersion"; |
| 2 | +import { Platform } from "react-native"; |
| 3 | + |
| 4 | +export class ViroTelemetry { |
| 5 | + private static _isDisabled = false; |
| 6 | + private static _isDebugging = false; |
| 7 | + // TODO: use custom domain |
| 8 | + // private static _telemetryUrl = "https://telemetry.nativevision.xyz"; |
| 9 | + private static _telemetryUrl = "https://native-vision-telemetry.onrender.com"; |
| 10 | + private static _timeout = 8000; |
| 11 | + |
| 12 | + /** |
| 13 | + * Allow a user to start debugging the telemetry to see what is sent. |
| 14 | + */ |
| 15 | + public static setDebugging() { |
| 16 | + this._isDebugging = true; |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Allow a user to opt out of telemetry. |
| 21 | + */ |
| 22 | + public static optOutTelemetry() { |
| 23 | + this._isDisabled = true; |
| 24 | + } |
| 25 | + |
| 26 | + public static recordTelemetry(eventName: string, payload: any = {}) { |
| 27 | + // Skip recording telemetry if the feature is disabled |
| 28 | + if (this._isDisabled) return; |
| 29 | + // Do not send the telemetry data if debugging. Users may use this feature |
| 30 | + // to preview what data would be sent. |
| 31 | + if (this._isDebugging) { |
| 32 | + console.log( |
| 33 | + `[telemetry] ` + JSON.stringify({ eventName, payload }, null, 2) |
| 34 | + ); |
| 35 | + return; |
| 36 | + } |
| 37 | + const controller = new AbortController(); |
| 38 | + const timeoutId = setTimeout(() => controller.abort(), this._timeout); |
| 39 | + |
| 40 | + payload = { ...payload, ...this.getAnonymousMeta() }; |
| 41 | + |
| 42 | + fetch(`${this._telemetryUrl}/api/v1/record`, { |
| 43 | + method: "PUT", |
| 44 | + body: JSON.stringify({ eventName, payload }), |
| 45 | + headers: { "content-type": "application/json" }, |
| 46 | + signal: controller.signal, |
| 47 | + }) |
| 48 | + .catch((e) => console.error(e)) |
| 49 | + .finally(() => clearTimeout(timeoutId)); |
| 50 | + } |
| 51 | + |
| 52 | + private static getAnonymousMeta() { |
| 53 | + let isExpo = false; |
| 54 | + try { |
| 55 | + const myModule = require("expo"); |
| 56 | + isExpo = true; |
| 57 | + } catch (err) { |
| 58 | + // send error to log file |
| 59 | + } |
| 60 | + |
| 61 | + try { |
| 62 | + const traits = { |
| 63 | + // expo |
| 64 | + isExpo: |
| 65 | + // @ts-ignore |
| 66 | + Boolean(window?.expo) || false, |
| 67 | + sdkVersion: |
| 68 | + // @ts-ignore |
| 69 | + window?.expo?.modules?.ExponentConstants?.sdkVersion || undefined, |
| 70 | + androidPackage: |
| 71 | + // @ts-ignore |
| 72 | + window?.expo?.modules?.ExponentConstants?.android?.package || |
| 73 | + undefined, |
| 74 | + iosBundleIdentifier: |
| 75 | + // @ts-ignore |
| 76 | + window?.expo?.modules?.ExponentConstants?.ios?.bundleIdentifier || |
| 77 | + undefined, |
| 78 | + expoDebugMode: |
| 79 | + // @ts-ignore |
| 80 | + window?.expo?.modules?.ExponentConstants?.debugMode || undefined, |
| 81 | + isDevice: |
| 82 | + // @ts-ignore |
| 83 | + window?.expo?.modules?.ExponentConstants?.isDevice || undefined, |
| 84 | + // library version |
| 85 | + viroVersion: VIRO_VERSION, |
| 86 | + platform: Platform.OS, |
| 87 | + deviceOsVersion: Platform.Version, |
| 88 | + reactNativeVersion: |
| 89 | + Platform.constants.reactNativeVersion.major + |
| 90 | + "." + |
| 91 | + Platform.constants.reactNativeVersion.minor + |
| 92 | + "." + |
| 93 | + Platform.constants.reactNativeVersion.patch, |
| 94 | + }; |
| 95 | + |
| 96 | + return traits; |
| 97 | + } catch (e) { |
| 98 | + console.error(e); |
| 99 | + } |
| 100 | + |
| 101 | + return {}; |
| 102 | + } |
| 103 | +} |
0 commit comments