@slicemachine/manager 0.17.9-beta.7 → 0.17.9-beta.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/managers/telemetry/TelemetryManager.cjs +5 -5
- package/dist/managers/telemetry/TelemetryManager.cjs.map +1 -1
- package/dist/managers/telemetry/TelemetryManager.js +5 -5
- package/dist/managers/telemetry/TelemetryManager.js.map +1 -1
- package/package.json +2 -2
- package/src/managers/telemetry/TelemetryManager.ts +8 -6
|
@@ -43,7 +43,7 @@ class TelemetryManager extends BaseManager.BaseManager {
|
|
|
43
43
|
return analytics;
|
|
44
44
|
};
|
|
45
45
|
if (isTelemetryEnabled) {
|
|
46
|
-
|
|
46
|
+
this.initExperiment();
|
|
47
47
|
}
|
|
48
48
|
this._anonymousID = crypto.randomUUID();
|
|
49
49
|
this._context = { app: { name: args.appName, version: args.appVersion } };
|
|
@@ -162,13 +162,13 @@ class TelemetryManager extends BaseManager.BaseManager {
|
|
|
162
162
|
async checkIsTelemetryEnabled() {
|
|
163
163
|
let root;
|
|
164
164
|
try {
|
|
165
|
-
root = await this.project.getRoot();
|
|
165
|
+
root = await this.project.getRoot().catch(() => this.project.suggestRoot());
|
|
166
|
+
return prismicrc.readPrismicrc(root).telemetry !== false;
|
|
166
167
|
} catch {
|
|
167
|
-
|
|
168
|
+
return true;
|
|
168
169
|
}
|
|
169
|
-
return prismicrc.readPrismicrc(root).telemetry !== false;
|
|
170
170
|
}
|
|
171
|
-
|
|
171
|
+
initExperiment() {
|
|
172
172
|
try {
|
|
173
173
|
this._experiment = index.__exports.Experiment.initializeRemote(API_TOKENS.API_TOKENS.AmplitudeKey);
|
|
174
174
|
} catch (error) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TelemetryManager.cjs","sources":["../../../../src/managers/telemetry/TelemetryManager.ts"],"sourcesContent":["import {\n\tExperiment,\n\tRemoteEvaluationClient,\n\tVariant,\n} from \"@amplitude/experiment-node-server\";\nimport { randomUUID } from \"node:crypto\";\nimport { Analytics, GroupParams, TrackParams } from \"@segment/analytics-node\";\n\nimport { readPrismicrc } from \"../../lib/prismicrc\";\n\nimport { API_TOKENS } from \"../../constants/API_TOKENS\";\n\nimport { BaseManager } from \"../BaseManager\";\n\nimport {\n\tHumanSegmentEventType,\n\tHumanSegmentEventTypes,\n\tSegmentEvents,\n} from \"./types\";\nimport { Environment } from \"../prismicRepository/types\";\n\ntype TelemetryManagerInitTelemetryArgs = {\n\tappName: string;\n\tappVersion: string;\n};\n\ntype TelemetryManagerTrackArgs = SegmentEvents & {\n\t_includeEnvironmentKind?: boolean;\n};\n\ntype TelemetryManagerIdentifyArgs = {\n\tuserID: string;\n\tintercomHash: string;\n};\n\ntype TelemetryManagerGroupArgs = {\n\tmanualLibsCount: number;\n\tdownloadedLibsCount: number;\n\tnpmLibsCount: number;\n\tdownloadedLibs: string[];\n};\n\ntype TelemetryManagerContext = {\n\tapp: {\n\t\tname: string;\n\t\tversion: string;\n\t};\n};\n\nfunction assertTelemetryInitialized(\n\tsegmentClient: (() => Analytics) | undefined,\n): asserts segmentClient is NonNullable<typeof segmentClient> {\n\tif (segmentClient === undefined) {\n\t\tthrow new Error(\n\t\t\t\"Telemetry has not been initialized. Run `SliceMachineManager.telemetry.prototype.initTelemetry()` before re-calling this method.\",\n\t\t);\n\t}\n}\n\nexport class TelemetryManager extends BaseManager {\n\tprivate _segmentClient: (() => Analytics) | undefined = undefined;\n\tprivate _anonymousID: string | undefined = undefined;\n\tprivate _userID: string | undefined = undefined;\n\tprivate _context: TelemetryManagerContext | undefined = undefined;\n\tprivate _experiment: RemoteEvaluationClient | undefined = undefined;\n\n\tasync initTelemetry(args: TelemetryManagerInitTelemetryArgs): Promise<void> {\n\t\tconst isTelemetryEnabled = await this.checkIsTelemetryEnabled();\n\n\t\tthis._segmentClient = () => {\n\t\t\tconst analytics = new Analytics({\n\t\t\t\twriteKey: API_TOKENS.SegmentKey,\n\t\t\t\t// Since it's a local app, we do not benefit from event batching the way a server would normally do, all tracking event will be awaited.\n\t\t\t\tmaxEventsInBatch: 1,\n\t\t\t\t// TODO: Verify that this actually does not send data to Segment when false.\n\t\t\t\tdisable: !isTelemetryEnabled,\n\t\t\t});\n\n\t\t\tanalytics.on(\"error\", (error) => {\n\t\t\t\t// noop - We don't care if the tracking event\n\t\t\t\t// failed. Some users or networks intentionally\n\t\t\t\t// block Segment, so we can't block the app if\n\t\t\t\t// a tracking event is unsuccessful.\n\t\t\t\tif (import.meta.env.DEV) {\n\t\t\t\t\tconsole.error(`An error occurred with Segment`, error);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\treturn analytics;\n\t\t};\n\n\t\tif (isTelemetryEnabled) {\n\t\t\tawait this.initExperiment();\n\t\t}\n\n\t\tthis._anonymousID = randomUUID();\n\t\tthis._context = { app: { name: args.appName, version: args.appVersion } };\n\t}\n\n\t// TODO: Should `userId` be automatically populated by the logged in\n\t// user? We already have their info via UserRepository.\n\tasync track(args: TelemetryManagerTrackArgs): Promise<void> {\n\t\tconst { event, repository, ...properties } = args;\n\t\tlet repositoryName = repository;\n\n\t\tif (repositoryName === undefined) {\n\t\t\ttry {\n\t\t\t\trepositoryName = await this.project.getRepositoryName();\n\t\t\t} catch (error) {\n\t\t\t\t// noop, happen only when the user is not in a project\n\t\t\t}\n\t\t}\n\n\t\tlet environmentKind: Environment[\"kind\"] | \"_unknown\" | undefined =\n\t\t\tundefined;\n\t\tif (args._includeEnvironmentKind) {\n\t\t\tif (this.project.checkSupportsEnvironments()) {\n\t\t\t\ttry {\n\t\t\t\t\tconst { activeEnvironment } =\n\t\t\t\t\t\tawait this.project.fetchActiveEnvironment();\n\t\t\t\t\tenvironmentKind = activeEnvironment.kind;\n\t\t\t\t} catch {\n\t\t\t\t\tenvironmentKind = \"_unknown\";\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// Assume only the production environment can be\n\t\t\t\t// used if the project's adapter does not\n\t\t\t\t// support environments.\n\t\t\t\tenvironmentKind = \"prod\";\n\t\t\t}\n\t\t}\n\n\t\tconst payload: {\n\t\t\tevent: HumanSegmentEventTypes;\n\t\t\tuserId?: string;\n\t\t\tanonymousId?: string;\n\t\t\tproperties?: Record<string, unknown>;\n\t\t\tcontext?: Partial<TelemetryManagerContext> & {\n\t\t\t\tgroupId?: {\n\t\t\t\t\tRepository?: string;\n\t\t\t\t};\n\t\t\t};\n\t\t} = {\n\t\t\tevent: HumanSegmentEventType[event],\n\t\t\tproperties: {\n\t\t\t\tnodeVersion: process.versions.node,\n\t\t\t\tenvironmentKind,\n\t\t\t\t...properties,\n\t\t\t},\n\t\t\tcontext: { ...this._context },\n\t\t};\n\n\t\tif (this._userID) {\n\t\t\tpayload.userId = this._userID;\n\t\t} else {\n\t\t\tpayload.anonymousId = this._anonymousID;\n\t\t}\n\n\t\tif (repositoryName) {\n\t\t\tpayload.context ||= {};\n\t\t\tpayload.context.groupId ||= {};\n\t\t\tpayload.context.groupId.Repository = repositoryName;\n\t\t}\n\n\t\treturn new Promise((resolve) => {\n\t\t\tassertTelemetryInitialized(this._segmentClient);\n\n\t\t\t// TODO: Make sure client fails gracefully when no internet connection\n\t\t\tthis._segmentClient().track(\n\t\t\t\tpayload as TrackParams,\n\t\t\t\t(maybeError?: unknown) => {\n\t\t\t\t\tif (maybeError && import.meta.env.DEV) {\n\t\t\t\t\t\t// TODO: Not sure how we want to deal with that\n\t\t\t\t\t\tconsole.warn(\n\t\t\t\t\t\t\t`An error occurred during Segment tracking`,\n\t\t\t\t\t\t\tmaybeError,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tresolve();\n\t\t\t\t},\n\t\t\t);\n\t\t});\n\t}\n\n\t// TODO: Should `userID` and `intercomHash` be automatically populated\n\t// by the logged in user? We already have their info via\n\t// UserRepository.\n\tidentify(args: TelemetryManagerIdentifyArgs): Promise<void> {\n\t\tconst payload = {\n\t\t\tuserId: args.userID,\n\t\t\tanonymousId: this._anonymousID,\n\t\t\tintegrations: {\n\t\t\t\tIntercom: {\n\t\t\t\t\tuser_hash: args.intercomHash,\n\t\t\t\t},\n\t\t\t},\n\t\t\tcontext: { ...this._context },\n\t\t};\n\n\t\tthis._userID = args.userID;\n\n\t\treturn new Promise((resolve) => {\n\t\t\tassertTelemetryInitialized(this._segmentClient);\n\n\t\t\t// TODO: Make sure client fails gracefully when no internet connection\n\t\t\tthis._segmentClient().identify(payload, (maybeError?: unknown) => {\n\t\t\t\tif (maybeError && import.meta.env.DEV) {\n\t\t\t\t\t// TODO: Not sure how we want to deal with that\n\t\t\t\t\tconsole.warn(`An error occurred during Segment identify`, maybeError);\n\t\t\t\t}\n\n\t\t\t\tresolve();\n\t\t\t});\n\t\t});\n\t}\n\n\tasync group(args: TelemetryManagerGroupArgs): Promise<void> {\n\t\tlet repositoryName;\n\n\t\ttry {\n\t\t\trepositoryName = await this.project.getRepositoryName();\n\t\t} catch (error) {\n\t\t\t// noop, happen only when the user is not in a project\n\t\t}\n\n\t\tconst payload: {\n\t\t\tgroupId?: string;\n\t\t\tuserId?: string;\n\t\t\tanonymousId?: string;\n\t\t\ttraits?: Record<string, unknown>;\n\t\t\tcontext?: Partial<TelemetryManagerContext> & {\n\t\t\t\tgroupId?: {\n\t\t\t\t\tRepository?: string;\n\t\t\t\t};\n\t\t\t};\n\t\t} = {\n\t\t\ttraits: args,\n\t\t\tcontext: { ...this._context },\n\t\t};\n\n\t\tif (this._userID) {\n\t\t\tpayload.userId = this._userID;\n\t\t} else {\n\t\t\tpayload.anonymousId = this._anonymousID;\n\t\t}\n\n\t\tif (repositoryName) {\n\t\t\tpayload.groupId = repositoryName;\n\t\t\tpayload.context ||= {};\n\t\t\tpayload.context.groupId ||= {};\n\t\t\tpayload.context.groupId.Repository = repositoryName;\n\t\t}\n\n\t\treturn new Promise((resolve) => {\n\t\t\tassertTelemetryInitialized(this._segmentClient);\n\n\t\t\tthis._segmentClient().group(\n\t\t\t\tpayload as GroupParams,\n\t\t\t\t(maybeError?: unknown) => {\n\t\t\t\t\tif (maybeError && import.meta.env.DEV) {\n\t\t\t\t\t\t// TODO: Not sure how we want to deal with that\n\t\t\t\t\t\tconsole.warn(`An error occurred during Segment group`, maybeError);\n\t\t\t\t\t}\n\n\t\t\t\t\tresolve();\n\t\t\t\t},\n\t\t\t);\n\t\t});\n\t}\n\n\tasync checkIsTelemetryEnabled(): Promise<boolean> {\n\t\tlet root: string;\n\t\ttry {\n\t\t\troot = await this.project.getRoot();\n\t\t} catch {\n\t\t\troot = await this.project.suggestRoot();\n\t\t}\n\n\t\treturn readPrismicrc(root).telemetry !== false;\n\t}\n\n\tprivate async initExperiment(): Promise<void> {\n\t\ttry {\n\t\t\tthis._experiment = Experiment.initializeRemote(API_TOKENS.AmplitudeKey);\n\t\t} catch (error) {\n\t\t\tif (import.meta.env.DEV) {\n\t\t\t\tconsole.error(\"Error initializing experiment\", error);\n\t\t\t}\n\t\t}\n\t}\n\n\tasync getExperimentVariant(variantKey: string): Promise<Variant | undefined> {\n\t\tif (this._experiment) {\n\t\t\ttry {\n\t\t\t\tconst repositoryName = await this.project.getRepositoryName();\n\t\t\t\tconst variants = await this._experiment.fetchV2({\n\t\t\t\t\tuser_id: this._userID,\n\t\t\t\t\tuser_properties: {\n\t\t\t\t\t\tRepository: repositoryName,\n\t\t\t\t\t},\n\t\t\t\t});\n\n\t\t\t\treturn variants[variantKey];\n\t\t\t} catch (error) {\n\t\t\t\tif (import.meta.env.DEV) {\n\t\t\t\t\tconsole.error(\"Error fetching experiment variant\", error);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn undefined;\n\t}\n}\n"],"names":["BaseManager","Analytics","API_TOKENS","randomUUID","HumanSegmentEventType","readPrismicrc","Experiment"],"mappings":";;;;;;;;;;;;;;;;AAiDA,SAAS,2BACR,eAA4C;AAE5C,MAAI,kBAAkB,QAAW;AAC1B,UAAA,IAAI,MACT,kIAAkI;AAAA,EAEnI;AACF;AAEM,MAAO,yBAAyBA,YAA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AQ,YAAR,QAAQ,UAAY;AACZ,oBAAA,SAAQ,YAAR,GAAQ,UAAY;AACpB,cAAA,QAAQ,QAAQ,aAAa;AAAA,IACrC;AAEM,WAAA,IAAI,QAAQ,CAAC,YAAW;AAC9B,iCAA2B,KAAK,cAAc;AAE9C,WAAK,eAAc,EAAG,MACrB,SACA,CAAC,eAAwB;AACpB,YAAA,cAAc,OAAqB;AAE9B,kBAAA,KAAK,0CAA0C,UAAU;AAAA,QACjE;;OAGD;AAAA,IAAA,CAEF;AAAA,EACF;AAAA,EAEA,MAAM,0BAAuB;AACxB,QAAA;AACA,QAAA;AACI,aAAA,MAAM,KAAK,QAAQ;YACzB;AACM,aAAA,MAAM,KAAK,QAAQ;IAC1B;AAEM,WAAAC,wBAAc,IAAI,EAAE,cAAc;AAAA,EAC1C;AAAA,EAEQ,MAAM,iBAAc;AACvB,QAAA;AACH,WAAK,cAAcC,MAAA,UAAA,WAAW,iBAAiBJ,WAAA,WAAW,YAAY;AAAA,aAC9D;IAIR;AAAA,EACF;AAAA,EAEA,MAAM,qBAAqB,YAAkB;AAC5C,QAAI,KAAK,aAAa;AACjB,UAAA;AACH,cAAM,iBAAiB,MAAM,KAAK,QAAQ,kBAAiB;AAC3D,cAAM,WAAW,MAAM,KAAK,YAAY,QAAQ;AAAA,UAC/C,SAAS,KAAK;AAAA,UACd,iBAAiB;AAAA,YAChB,YAAY;AAAA,UACZ;AAAA,QAAA,CACD;AAED,eAAO,SAAS,UAAU;AAAA,eAClB;MAIR;AAAA,IACD;AAEM,WAAA;AAAA,EACR;AACA;;"}
|
|
1
|
+
{"version":3,"file":"TelemetryManager.cjs","sources":["../../../../src/managers/telemetry/TelemetryManager.ts"],"sourcesContent":["import {\n\tExperiment,\n\tRemoteEvaluationClient,\n\tVariant,\n} from \"@amplitude/experiment-node-server\";\nimport { randomUUID } from \"node:crypto\";\nimport { Analytics, GroupParams, TrackParams } from \"@segment/analytics-node\";\n\nimport { readPrismicrc } from \"../../lib/prismicrc\";\n\nimport { API_TOKENS } from \"../../constants/API_TOKENS\";\n\nimport { BaseManager } from \"../BaseManager\";\n\nimport {\n\tHumanSegmentEventType,\n\tHumanSegmentEventTypes,\n\tSegmentEvents,\n} from \"./types\";\nimport { Environment } from \"../prismicRepository/types\";\n\ntype TelemetryManagerInitTelemetryArgs = {\n\tappName: string;\n\tappVersion: string;\n};\n\ntype TelemetryManagerTrackArgs = SegmentEvents & {\n\t_includeEnvironmentKind?: boolean;\n};\n\ntype TelemetryManagerIdentifyArgs = {\n\tuserID: string;\n\tintercomHash: string;\n};\n\ntype TelemetryManagerGroupArgs = {\n\tmanualLibsCount: number;\n\tdownloadedLibsCount: number;\n\tnpmLibsCount: number;\n\tdownloadedLibs: string[];\n};\n\ntype TelemetryManagerContext = {\n\tapp: {\n\t\tname: string;\n\t\tversion: string;\n\t};\n};\n\nfunction assertTelemetryInitialized(\n\tsegmentClient: (() => Analytics) | undefined,\n): asserts segmentClient is NonNullable<typeof segmentClient> {\n\tif (segmentClient === undefined) {\n\t\tthrow new Error(\n\t\t\t\"Telemetry has not been initialized. Run `SliceMachineManager.telemetry.prototype.initTelemetry()` before re-calling this method.\",\n\t\t);\n\t}\n}\n\nexport class TelemetryManager extends BaseManager {\n\tprivate _segmentClient: (() => Analytics) | undefined = undefined;\n\tprivate _anonymousID: string | undefined = undefined;\n\tprivate _userID: string | undefined = undefined;\n\tprivate _context: TelemetryManagerContext | undefined = undefined;\n\tprivate _experiment: RemoteEvaluationClient | undefined = undefined;\n\n\tasync initTelemetry(args: TelemetryManagerInitTelemetryArgs): Promise<void> {\n\t\tconst isTelemetryEnabled = await this.checkIsTelemetryEnabled();\n\n\t\tthis._segmentClient = () => {\n\t\t\tconst analytics = new Analytics({\n\t\t\t\twriteKey: API_TOKENS.SegmentKey,\n\t\t\t\t// Since it's a local app, we do not benefit from event batching the way a server would normally do, all tracking event will be awaited.\n\t\t\t\tmaxEventsInBatch: 1,\n\t\t\t\t// TODO: Verify that this actually does not send data to Segment when false.\n\t\t\t\tdisable: !isTelemetryEnabled,\n\t\t\t});\n\n\t\t\tanalytics.on(\"error\", (error) => {\n\t\t\t\t// noop - We don't care if the tracking event\n\t\t\t\t// failed. Some users or networks intentionally\n\t\t\t\t// block Segment, so we can't block the app if\n\t\t\t\t// a tracking event is unsuccessful.\n\t\t\t\tif (import.meta.env.DEV) {\n\t\t\t\t\tconsole.error(`An error occurred with Segment`, error);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\treturn analytics;\n\t\t};\n\n\t\tif (isTelemetryEnabled) {\n\t\t\tthis.initExperiment();\n\t\t}\n\n\t\tthis._anonymousID = randomUUID();\n\t\tthis._context = { app: { name: args.appName, version: args.appVersion } };\n\t}\n\n\t// TODO: Should `userId` be automatically populated by the logged in\n\t// user? We already have their info via UserRepository.\n\tasync track(args: TelemetryManagerTrackArgs): Promise<void> {\n\t\tconst { event, repository, ...properties } = args;\n\t\tlet repositoryName = repository;\n\n\t\tif (repositoryName === undefined) {\n\t\t\ttry {\n\t\t\t\trepositoryName = await this.project.getRepositoryName();\n\t\t\t} catch (error) {\n\t\t\t\t// noop, happen only when the user is not in a project\n\t\t\t}\n\t\t}\n\n\t\tlet environmentKind: Environment[\"kind\"] | \"_unknown\" | undefined =\n\t\t\tundefined;\n\t\tif (args._includeEnvironmentKind) {\n\t\t\tif (this.project.checkSupportsEnvironments()) {\n\t\t\t\ttry {\n\t\t\t\t\tconst { activeEnvironment } =\n\t\t\t\t\t\tawait this.project.fetchActiveEnvironment();\n\t\t\t\t\tenvironmentKind = activeEnvironment.kind;\n\t\t\t\t} catch {\n\t\t\t\t\tenvironmentKind = \"_unknown\";\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// Assume only the production environment can be\n\t\t\t\t// used if the project's adapter does not\n\t\t\t\t// support environments.\n\t\t\t\tenvironmentKind = \"prod\";\n\t\t\t}\n\t\t}\n\n\t\tconst payload: {\n\t\t\tevent: HumanSegmentEventTypes;\n\t\t\tuserId?: string;\n\t\t\tanonymousId?: string;\n\t\t\tproperties?: Record<string, unknown>;\n\t\t\tcontext?: Partial<TelemetryManagerContext> & {\n\t\t\t\tgroupId?: {\n\t\t\t\t\tRepository?: string;\n\t\t\t\t};\n\t\t\t};\n\t\t} = {\n\t\t\tevent: HumanSegmentEventType[event],\n\t\t\tproperties: {\n\t\t\t\tnodeVersion: process.versions.node,\n\t\t\t\tenvironmentKind,\n\t\t\t\t...properties,\n\t\t\t},\n\t\t\tcontext: { ...this._context },\n\t\t};\n\n\t\tif (this._userID) {\n\t\t\tpayload.userId = this._userID;\n\t\t} else {\n\t\t\tpayload.anonymousId = this._anonymousID;\n\t\t}\n\n\t\tif (repositoryName) {\n\t\t\tpayload.context ||= {};\n\t\t\tpayload.context.groupId ||= {};\n\t\t\tpayload.context.groupId.Repository = repositoryName;\n\t\t}\n\n\t\treturn new Promise((resolve) => {\n\t\t\tassertTelemetryInitialized(this._segmentClient);\n\n\t\t\t// TODO: Make sure client fails gracefully when no internet connection\n\t\t\tthis._segmentClient().track(\n\t\t\t\tpayload as TrackParams,\n\t\t\t\t(maybeError?: unknown) => {\n\t\t\t\t\tif (maybeError && import.meta.env.DEV) {\n\t\t\t\t\t\t// TODO: Not sure how we want to deal with that\n\t\t\t\t\t\tconsole.warn(\n\t\t\t\t\t\t\t`An error occurred during Segment tracking`,\n\t\t\t\t\t\t\tmaybeError,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tresolve();\n\t\t\t\t},\n\t\t\t);\n\t\t});\n\t}\n\n\t// TODO: Should `userID` and `intercomHash` be automatically populated\n\t// by the logged in user? We already have their info via\n\t// UserRepository.\n\tidentify(args: TelemetryManagerIdentifyArgs): Promise<void> {\n\t\tconst payload = {\n\t\t\tuserId: args.userID,\n\t\t\tanonymousId: this._anonymousID,\n\t\t\tintegrations: {\n\t\t\t\tIntercom: {\n\t\t\t\t\tuser_hash: args.intercomHash,\n\t\t\t\t},\n\t\t\t},\n\t\t\tcontext: { ...this._context },\n\t\t};\n\n\t\tthis._userID = args.userID;\n\n\t\treturn new Promise((resolve) => {\n\t\t\tassertTelemetryInitialized(this._segmentClient);\n\n\t\t\t// TODO: Make sure client fails gracefully when no internet connection\n\t\t\tthis._segmentClient().identify(payload, (maybeError?: unknown) => {\n\t\t\t\tif (maybeError && import.meta.env.DEV) {\n\t\t\t\t\t// TODO: Not sure how we want to deal with that\n\t\t\t\t\tconsole.warn(`An error occurred during Segment identify`, maybeError);\n\t\t\t\t}\n\n\t\t\t\tresolve();\n\t\t\t});\n\t\t});\n\t}\n\n\tasync group(args: TelemetryManagerGroupArgs): Promise<void> {\n\t\tlet repositoryName;\n\n\t\ttry {\n\t\t\trepositoryName = await this.project.getRepositoryName();\n\t\t} catch (error) {\n\t\t\t// noop, happen only when the user is not in a project\n\t\t}\n\n\t\tconst payload: {\n\t\t\tgroupId?: string;\n\t\t\tuserId?: string;\n\t\t\tanonymousId?: string;\n\t\t\ttraits?: Record<string, unknown>;\n\t\t\tcontext?: Partial<TelemetryManagerContext> & {\n\t\t\t\tgroupId?: {\n\t\t\t\t\tRepository?: string;\n\t\t\t\t};\n\t\t\t};\n\t\t} = {\n\t\t\ttraits: args,\n\t\t\tcontext: { ...this._context },\n\t\t};\n\n\t\tif (this._userID) {\n\t\t\tpayload.userId = this._userID;\n\t\t} else {\n\t\t\tpayload.anonymousId = this._anonymousID;\n\t\t}\n\n\t\tif (repositoryName) {\n\t\t\tpayload.groupId = repositoryName;\n\t\t\tpayload.context ||= {};\n\t\t\tpayload.context.groupId ||= {};\n\t\t\tpayload.context.groupId.Repository = repositoryName;\n\t\t}\n\n\t\treturn new Promise((resolve) => {\n\t\t\tassertTelemetryInitialized(this._segmentClient);\n\n\t\t\tthis._segmentClient().group(\n\t\t\t\tpayload as GroupParams,\n\t\t\t\t(maybeError?: unknown) => {\n\t\t\t\t\tif (maybeError && import.meta.env.DEV) {\n\t\t\t\t\t\t// TODO: Not sure how we want to deal with that\n\t\t\t\t\t\tconsole.warn(`An error occurred during Segment group`, maybeError);\n\t\t\t\t\t}\n\n\t\t\t\t\tresolve();\n\t\t\t\t},\n\t\t\t);\n\t\t});\n\t}\n\n\tasync checkIsTelemetryEnabled(): Promise<boolean> {\n\t\tlet root: string;\n\t\ttry {\n\t\t\troot = await this.project\n\t\t\t\t.getRoot()\n\t\t\t\t.catch(() => this.project.suggestRoot());\n\n\t\t\treturn readPrismicrc(root).telemetry !== false;\n\t\t} catch {\n\t\t\treturn true;\n\t\t}\n\t}\n\n\tprivate initExperiment(): void {\n\t\ttry {\n\t\t\tthis._experiment = Experiment.initializeRemote(API_TOKENS.AmplitudeKey);\n\t\t} catch (error) {\n\t\t\tif (import.meta.env.DEV) {\n\t\t\t\tconsole.error(\"Error initializing experiment\", error);\n\t\t\t}\n\t\t}\n\t}\n\n\tasync getExperimentVariant(variantKey: string): Promise<Variant | undefined> {\n\t\tif (this._experiment) {\n\t\t\ttry {\n\t\t\t\tconst repositoryName = await this.project.getRepositoryName();\n\t\t\t\tconst variants = await this._experiment.fetchV2({\n\t\t\t\t\tuser_id: this._userID,\n\t\t\t\t\tuser_properties: {\n\t\t\t\t\t\tRepository: repositoryName,\n\t\t\t\t\t},\n\t\t\t\t});\n\n\t\t\t\treturn variants[variantKey];\n\t\t\t} catch (error) {\n\t\t\t\tif (import.meta.env.DEV) {\n\t\t\t\t\tconsole.error(\"Error fetching experiment variant\", error);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn undefined;\n\t}\n}\n"],"names":["BaseManager","Analytics","API_TOKENS","randomUUID","HumanSegmentEventType","readPrismicrc","Experiment"],"mappings":";;;;;;;;;;;;;;;;AAiDA,SAAS,2BACR,eAA4C;AAE5C,MAAI,kBAAkB,QAAW;AAC1B,UAAA,IAAI,MACT,kIAAkI;AAAA,EAEnI;AACF;AAEM,MAAO,yBAAyBA,YAAAA,YAAW;AAAA,EAA3C;AAAA;AACG;AACA;AACA;AACA;AACA;AAAA;AAAA,EAER,MAAM,cAAc,M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QAAQ,QAAQ,aAAa;AAAA,IACrC;AAEM,WAAA,IAAI,QAAQ,CAAC,YAAW;AAC9B,iCAA2B,KAAK,cAAc;AAE9C,WAAK,eAAc,EAAG,MACrB,SACA,CAAC,eAAwB;AACpB,YAAA,cAAc,OAAqB;AAE9B,kBAAA,KAAK,0CAA0C,UAAU;AAAA,QACjE;;OAGD;AAAA,IAAA,CAEF;AAAA,EACF;AAAA,EAEA,MAAM,0BAAuB;AACxB,QAAA;AACA,QAAA;AACI,aAAA,MAAM,KAAK,QAChB,QAAS,EACT,MAAM,MAAM,KAAK,QAAQ,YAAA,CAAa;AAEjC,aAAAC,wBAAc,IAAI,EAAE,cAAc;AAAA,IAAA,QACxC;AACM,aAAA;AAAA,IACP;AAAA,EACF;AAAA,EAEQ,iBAAc;AACjB,QAAA;AACH,WAAK,cAAcC,MAAA,UAAA,WAAW,iBAAiBJ,WAAA,WAAW,YAAY;AAAA,aAC9D;IAIR;AAAA,EACF;AAAA,EAEA,MAAM,qBAAqB,YAAkB;AAC5C,QAAI,KAAK,aAAa;AACjB,UAAA;AACH,cAAM,iBAAiB,MAAM,KAAK,QAAQ,kBAAiB;AAC3D,cAAM,WAAW,MAAM,KAAK,YAAY,QAAQ;AAAA,UAC/C,SAAS,KAAK;AAAA,UACd,iBAAiB;AAAA,YAChB,YAAY;AAAA,UACZ;AAAA,QAAA,CACD;AAED,eAAO,SAAS,UAAU;AAAA,eAClB;MAIR;AAAA,IACD;AAEM,WAAA;AAAA,EACR;AACA;;"}
|
|
@@ -41,7 +41,7 @@ class TelemetryManager extends BaseManager {
|
|
|
41
41
|
return analytics;
|
|
42
42
|
};
|
|
43
43
|
if (isTelemetryEnabled) {
|
|
44
|
-
|
|
44
|
+
this.initExperiment();
|
|
45
45
|
}
|
|
46
46
|
this._anonymousID = randomUUID();
|
|
47
47
|
this._context = { app: { name: args.appName, version: args.appVersion } };
|
|
@@ -160,13 +160,13 @@ class TelemetryManager extends BaseManager {
|
|
|
160
160
|
async checkIsTelemetryEnabled() {
|
|
161
161
|
let root;
|
|
162
162
|
try {
|
|
163
|
-
root = await this.project.getRoot();
|
|
163
|
+
root = await this.project.getRoot().catch(() => this.project.suggestRoot());
|
|
164
|
+
return readPrismicrc(root).telemetry !== false;
|
|
164
165
|
} catch {
|
|
165
|
-
|
|
166
|
+
return true;
|
|
166
167
|
}
|
|
167
|
-
return readPrismicrc(root).telemetry !== false;
|
|
168
168
|
}
|
|
169
|
-
|
|
169
|
+
initExperiment() {
|
|
170
170
|
try {
|
|
171
171
|
this._experiment = src.Experiment.initializeRemote(API_TOKENS.AmplitudeKey);
|
|
172
172
|
} catch (error) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TelemetryManager.js","sources":["../../../../src/managers/telemetry/TelemetryManager.ts"],"sourcesContent":["import {\n\tExperiment,\n\tRemoteEvaluationClient,\n\tVariant,\n} from \"@amplitude/experiment-node-server\";\nimport { randomUUID } from \"node:crypto\";\nimport { Analytics, GroupParams, TrackParams } from \"@segment/analytics-node\";\n\nimport { readPrismicrc } from \"../../lib/prismicrc\";\n\nimport { API_TOKENS } from \"../../constants/API_TOKENS\";\n\nimport { BaseManager } from \"../BaseManager\";\n\nimport {\n\tHumanSegmentEventType,\n\tHumanSegmentEventTypes,\n\tSegmentEvents,\n} from \"./types\";\nimport { Environment } from \"../prismicRepository/types\";\n\ntype TelemetryManagerInitTelemetryArgs = {\n\tappName: string;\n\tappVersion: string;\n};\n\ntype TelemetryManagerTrackArgs = SegmentEvents & {\n\t_includeEnvironmentKind?: boolean;\n};\n\ntype TelemetryManagerIdentifyArgs = {\n\tuserID: string;\n\tintercomHash: string;\n};\n\ntype TelemetryManagerGroupArgs = {\n\tmanualLibsCount: number;\n\tdownloadedLibsCount: number;\n\tnpmLibsCount: number;\n\tdownloadedLibs: string[];\n};\n\ntype TelemetryManagerContext = {\n\tapp: {\n\t\tname: string;\n\t\tversion: string;\n\t};\n};\n\nfunction assertTelemetryInitialized(\n\tsegmentClient: (() => Analytics) | undefined,\n): asserts segmentClient is NonNullable<typeof segmentClient> {\n\tif (segmentClient === undefined) {\n\t\tthrow new Error(\n\t\t\t\"Telemetry has not been initialized. Run `SliceMachineManager.telemetry.prototype.initTelemetry()` before re-calling this method.\",\n\t\t);\n\t}\n}\n\nexport class TelemetryManager extends BaseManager {\n\tprivate _segmentClient: (() => Analytics) | undefined = undefined;\n\tprivate _anonymousID: string | undefined = undefined;\n\tprivate _userID: string | undefined = undefined;\n\tprivate _context: TelemetryManagerContext | undefined = undefined;\n\tprivate _experiment: RemoteEvaluationClient | undefined = undefined;\n\n\tasync initTelemetry(args: TelemetryManagerInitTelemetryArgs): Promise<void> {\n\t\tconst isTelemetryEnabled = await this.checkIsTelemetryEnabled();\n\n\t\tthis._segmentClient = () => {\n\t\t\tconst analytics = new Analytics({\n\t\t\t\twriteKey: API_TOKENS.SegmentKey,\n\t\t\t\t// Since it's a local app, we do not benefit from event batching the way a server would normally do, all tracking event will be awaited.\n\t\t\t\tmaxEventsInBatch: 1,\n\t\t\t\t// TODO: Verify that this actually does not send data to Segment when false.\n\t\t\t\tdisable: !isTelemetryEnabled,\n\t\t\t});\n\n\t\t\tanalytics.on(\"error\", (error) => {\n\t\t\t\t// noop - We don't care if the tracking event\n\t\t\t\t// failed. Some users or networks intentionally\n\t\t\t\t// block Segment, so we can't block the app if\n\t\t\t\t// a tracking event is unsuccessful.\n\t\t\t\tif (import.meta.env.DEV) {\n\t\t\t\t\tconsole.error(`An error occurred with Segment`, error);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\treturn analytics;\n\t\t};\n\n\t\tif (isTelemetryEnabled) {\n\t\t\tawait this.initExperiment();\n\t\t}\n\n\t\tthis._anonymousID = randomUUID();\n\t\tthis._context = { app: { name: args.appName, version: args.appVersion } };\n\t}\n\n\t// TODO: Should `userId` be automatically populated by the logged in\n\t// user? We already have their info via UserRepository.\n\tasync track(args: TelemetryManagerTrackArgs): Promise<void> {\n\t\tconst { event, repository, ...properties } = args;\n\t\tlet repositoryName = repository;\n\n\t\tif (repositoryName === undefined) {\n\t\t\ttry {\n\t\t\t\trepositoryName = await this.project.getRepositoryName();\n\t\t\t} catch (error) {\n\t\t\t\t// noop, happen only when the user is not in a project\n\t\t\t}\n\t\t}\n\n\t\tlet environmentKind: Environment[\"kind\"] | \"_unknown\" | undefined =\n\t\t\tundefined;\n\t\tif (args._includeEnvironmentKind) {\n\t\t\tif (this.project.checkSupportsEnvironments()) {\n\t\t\t\ttry {\n\t\t\t\t\tconst { activeEnvironment } =\n\t\t\t\t\t\tawait this.project.fetchActiveEnvironment();\n\t\t\t\t\tenvironmentKind = activeEnvironment.kind;\n\t\t\t\t} catch {\n\t\t\t\t\tenvironmentKind = \"_unknown\";\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// Assume only the production environment can be\n\t\t\t\t// used if the project's adapter does not\n\t\t\t\t// support environments.\n\t\t\t\tenvironmentKind = \"prod\";\n\t\t\t}\n\t\t}\n\n\t\tconst payload: {\n\t\t\tevent: HumanSegmentEventTypes;\n\t\t\tuserId?: string;\n\t\t\tanonymousId?: string;\n\t\t\tproperties?: Record<string, unknown>;\n\t\t\tcontext?: Partial<TelemetryManagerContext> & {\n\t\t\t\tgroupId?: {\n\t\t\t\t\tRepository?: string;\n\t\t\t\t};\n\t\t\t};\n\t\t} = {\n\t\t\tevent: HumanSegmentEventType[event],\n\t\t\tproperties: {\n\t\t\t\tnodeVersion: process.versions.node,\n\t\t\t\tenvironmentKind,\n\t\t\t\t...properties,\n\t\t\t},\n\t\t\tcontext: { ...this._context },\n\t\t};\n\n\t\tif (this._userID) {\n\t\t\tpayload.userId = this._userID;\n\t\t} else {\n\t\t\tpayload.anonymousId = this._anonymousID;\n\t\t}\n\n\t\tif (repositoryName) {\n\t\t\tpayload.context ||= {};\n\t\t\tpayload.context.groupId ||= {};\n\t\t\tpayload.context.groupId.Repository = repositoryName;\n\t\t}\n\n\t\treturn new Promise((resolve) => {\n\t\t\tassertTelemetryInitialized(this._segmentClient);\n\n\t\t\t// TODO: Make sure client fails gracefully when no internet connection\n\t\t\tthis._segmentClient().track(\n\t\t\t\tpayload as TrackParams,\n\t\t\t\t(maybeError?: unknown) => {\n\t\t\t\t\tif (maybeError && import.meta.env.DEV) {\n\t\t\t\t\t\t// TODO: Not sure how we want to deal with that\n\t\t\t\t\t\tconsole.warn(\n\t\t\t\t\t\t\t`An error occurred during Segment tracking`,\n\t\t\t\t\t\t\tmaybeError,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tresolve();\n\t\t\t\t},\n\t\t\t);\n\t\t});\n\t}\n\n\t// TODO: Should `userID` and `intercomHash` be automatically populated\n\t// by the logged in user? We already have their info via\n\t// UserRepository.\n\tidentify(args: TelemetryManagerIdentifyArgs): Promise<void> {\n\t\tconst payload = {\n\t\t\tuserId: args.userID,\n\t\t\tanonymousId: this._anonymousID,\n\t\t\tintegrations: {\n\t\t\t\tIntercom: {\n\t\t\t\t\tuser_hash: args.intercomHash,\n\t\t\t\t},\n\t\t\t},\n\t\t\tcontext: { ...this._context },\n\t\t};\n\n\t\tthis._userID = args.userID;\n\n\t\treturn new Promise((resolve) => {\n\t\t\tassertTelemetryInitialized(this._segmentClient);\n\n\t\t\t// TODO: Make sure client fails gracefully when no internet connection\n\t\t\tthis._segmentClient().identify(payload, (maybeError?: unknown) => {\n\t\t\t\tif (maybeError && import.meta.env.DEV) {\n\t\t\t\t\t// TODO: Not sure how we want to deal with that\n\t\t\t\t\tconsole.warn(`An error occurred during Segment identify`, maybeError);\n\t\t\t\t}\n\n\t\t\t\tresolve();\n\t\t\t});\n\t\t});\n\t}\n\n\tasync group(args: TelemetryManagerGroupArgs): Promise<void> {\n\t\tlet repositoryName;\n\n\t\ttry {\n\t\t\trepositoryName = await this.project.getRepositoryName();\n\t\t} catch (error) {\n\t\t\t// noop, happen only when the user is not in a project\n\t\t}\n\n\t\tconst payload: {\n\t\t\tgroupId?: string;\n\t\t\tuserId?: string;\n\t\t\tanonymousId?: string;\n\t\t\ttraits?: Record<string, unknown>;\n\t\t\tcontext?: Partial<TelemetryManagerContext> & {\n\t\t\t\tgroupId?: {\n\t\t\t\t\tRepository?: string;\n\t\t\t\t};\n\t\t\t};\n\t\t} = {\n\t\t\ttraits: args,\n\t\t\tcontext: { ...this._context },\n\t\t};\n\n\t\tif (this._userID) {\n\t\t\tpayload.userId = this._userID;\n\t\t} else {\n\t\t\tpayload.anonymousId = this._anonymousID;\n\t\t}\n\n\t\tif (repositoryName) {\n\t\t\tpayload.groupId = repositoryName;\n\t\t\tpayload.context ||= {};\n\t\t\tpayload.context.groupId ||= {};\n\t\t\tpayload.context.groupId.Repository = repositoryName;\n\t\t}\n\n\t\treturn new Promise((resolve) => {\n\t\t\tassertTelemetryInitialized(this._segmentClient);\n\n\t\t\tthis._segmentClient().group(\n\t\t\t\tpayload as GroupParams,\n\t\t\t\t(maybeError?: unknown) => {\n\t\t\t\t\tif (maybeError && import.meta.env.DEV) {\n\t\t\t\t\t\t// TODO: Not sure how we want to deal with that\n\t\t\t\t\t\tconsole.warn(`An error occurred during Segment group`, maybeError);\n\t\t\t\t\t}\n\n\t\t\t\t\tresolve();\n\t\t\t\t},\n\t\t\t);\n\t\t});\n\t}\n\n\tasync checkIsTelemetryEnabled(): Promise<boolean> {\n\t\tlet root: string;\n\t\ttry {\n\t\t\troot = await this.project.getRoot();\n\t\t} catch {\n\t\t\troot = await this.project.suggestRoot();\n\t\t}\n\n\t\treturn readPrismicrc(root).telemetry !== false;\n\t}\n\n\tprivate async initExperiment(): Promise<void> {\n\t\ttry {\n\t\t\tthis._experiment = Experiment.initializeRemote(API_TOKENS.AmplitudeKey);\n\t\t} catch (error) {\n\t\t\tif (import.meta.env.DEV) {\n\t\t\t\tconsole.error(\"Error initializing experiment\", error);\n\t\t\t}\n\t\t}\n\t}\n\n\tasync getExperimentVariant(variantKey: string): Promise<Variant | undefined> {\n\t\tif (this._experiment) {\n\t\t\ttry {\n\t\t\t\tconst repositoryName = await this.project.getRepositoryName();\n\t\t\t\tconst variants = await this._experiment.fetchV2({\n\t\t\t\t\tuser_id: this._userID,\n\t\t\t\t\tuser_properties: {\n\t\t\t\t\t\tRepository: repositoryName,\n\t\t\t\t\t},\n\t\t\t\t});\n\n\t\t\t\treturn variants[variantKey];\n\t\t\t} catch (error) {\n\t\t\t\tif (import.meta.env.DEV) {\n\t\t\t\t\tconsole.error(\"Error fetching experiment variant\", error);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn undefined;\n\t}\n}\n"],"names":["Experiment"],"mappings":";;;;;;;;;;;;;;AAiDA,SAAS,2BACR,eAA4C;AAE5C,MAAI,kBAAkB,QAAW;AAC1B,UAAA,IAAI,MACT,kIAAkI;AAAA,EAEnI;AACF;AAEM,MAAO,yBAAyB,YAAW;AAAA,EAA3C;AAAA;AACG;AACA;AACA;AACA;AACA;AAAA;AAAA,EAER,MAAM,cAAc,MAAuC;AACpD,UAAA,qBAAqB,MAAM,KAAK;AAEtC,SAAK,iBAAiB,MAAK;AACpB,YAAA,YAAY,IAAI,UAAU;AAAA,QAC/B,UAAU,WAAW;AAAA;AAAA,QAErB,kBAAkB;AAAA;AAAA,QAElB,SAAS,CAAC;AAAA,MAAA,CACV;AAES,gBAAA,GAAG,SAAS,CAAC,UAAS;AAAA,MAO9B,CACD;AAEM,aAAA;AAAA,IAAA;AAGR,QAAI,oBAAoB;AACvB,YAAM,KAAK;IACX;AAED,SAAK,eAAe;AACf,SAAA,WAAW,EAAE,KAAK,EAAE,MAAM,KAAK,SAAS,SAAS,KAAK;EAC5D;AAAA;AAAA;AAAA,EAIA,MAAM,MAAM,MAA+B;;AAC1C,UAAM,EAAE,OAAO,YAAY,GAAG,eAAe;AAC7C,QAAI,iBAAiB;AAErB,QAAI,mBAAmB,QAAW;AAC7B,UAAA;AACc,yBAAA,MAAM,KAAK,QAAQ;eAC5B;MAER;AAAA,IACD;AAED,QAAI,kBACH;AACD,QAAI,KAAK,yBA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AAM,WAAW,MAAM,KAAK,YAAY,QAAQ;AAAA,UAC/C,SAAS,KAAK;AAAA,UACd,iBAAiB;AAAA,YAChB,YAAY;AAAA,UACZ;AAAA,QAAA,CACD;AAED,eAAO,SAAS,UAAU;AAAA,eAClB;MAIR;AAAA,IACD;AAEM,WAAA;AAAA,EACR;AACA;"}
|
|
1
|
+
{"version":3,"file":"TelemetryManager.js","sources":["../../../../src/managers/telemetry/TelemetryManager.ts"],"sourcesContent":["import {\n\tExperiment,\n\tRemoteEvaluationClient,\n\tVariant,\n} from \"@amplitude/experiment-node-server\";\nimport { randomUUID } from \"node:crypto\";\nimport { Analytics, GroupParams, TrackParams } from \"@segment/analytics-node\";\n\nimport { readPrismicrc } from \"../../lib/prismicrc\";\n\nimport { API_TOKENS } from \"../../constants/API_TOKENS\";\n\nimport { BaseManager } from \"../BaseManager\";\n\nimport {\n\tHumanSegmentEventType,\n\tHumanSegmentEventTypes,\n\tSegmentEvents,\n} from \"./types\";\nimport { Environment } from \"../prismicRepository/types\";\n\ntype TelemetryManagerInitTelemetryArgs = {\n\tappName: string;\n\tappVersion: string;\n};\n\ntype TelemetryManagerTrackArgs = SegmentEvents & {\n\t_includeEnvironmentKind?: boolean;\n};\n\ntype TelemetryManagerIdentifyArgs = {\n\tuserID: string;\n\tintercomHash: string;\n};\n\ntype TelemetryManagerGroupArgs = {\n\tmanualLibsCount: number;\n\tdownloadedLibsCount: number;\n\tnpmLibsCount: number;\n\tdownloadedLibs: string[];\n};\n\ntype TelemetryManagerContext = {\n\tapp: {\n\t\tname: string;\n\t\tversion: string;\n\t};\n};\n\nfunction assertTelemetryInitialized(\n\tsegmentClient: (() => Analytics) | undefined,\n): asserts segmentClient is NonNullable<typeof segmentClient> {\n\tif (segmentClient === undefined) {\n\t\tthrow new Error(\n\t\t\t\"Telemetry has not been initialized. Run `SliceMachineManager.telemetry.prototype.initTelemetry()` before re-calling this method.\",\n\t\t);\n\t}\n}\n\nexport class TelemetryManager extends BaseManager {\n\tprivate _segmentClient: (() => Analytics) | undefined = undefined;\n\tprivate _anonymousID: string | undefined = undefined;\n\tprivate _userID: string | undefined = undefined;\n\tprivate _context: TelemetryManagerContext | undefined = undefined;\n\tprivate _experiment: RemoteEvaluationClient | undefined = undefined;\n\n\tasync initTelemetry(args: TelemetryManagerInitTelemetryArgs): Promise<void> {\n\t\tconst isTelemetryEnabled = await this.checkIsTelemetryEnabled();\n\n\t\tthis._segmentClient = () => {\n\t\t\tconst analytics = new Analytics({\n\t\t\t\twriteKey: API_TOKENS.SegmentKey,\n\t\t\t\t// Since it's a local app, we do not benefit from event batching the way a server would normally do, all tracking event will be awaited.\n\t\t\t\tmaxEventsInBatch: 1,\n\t\t\t\t// TODO: Verify that this actually does not send data to Segment when false.\n\t\t\t\tdisable: !isTelemetryEnabled,\n\t\t\t});\n\n\t\t\tanalytics.on(\"error\", (error) => {\n\t\t\t\t// noop - We don't care if the tracking event\n\t\t\t\t// failed. Some users or networks intentionally\n\t\t\t\t// block Segment, so we can't block the app if\n\t\t\t\t// a tracking event is unsuccessful.\n\t\t\t\tif (import.meta.env.DEV) {\n\t\t\t\t\tconsole.error(`An error occurred with Segment`, error);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\treturn analytics;\n\t\t};\n\n\t\tif (isTelemetryEnabled) {\n\t\t\tthis.initExperiment();\n\t\t}\n\n\t\tthis._anonymousID = randomUUID();\n\t\tthis._context = { app: { name: args.appName, version: args.appVersion } };\n\t}\n\n\t// TODO: Should `userId` be automatically populated by the logged in\n\t// user? We already have their info via UserRepository.\n\tasync track(args: TelemetryManagerTrackArgs): Promise<void> {\n\t\tconst { event, repository, ...properties } = args;\n\t\tlet repositoryName = repository;\n\n\t\tif (repositoryName === undefined) {\n\t\t\ttry {\n\t\t\t\trepositoryName = await this.project.getRepositoryName();\n\t\t\t} catch (error) {\n\t\t\t\t// noop, happen only when the user is not in a project\n\t\t\t}\n\t\t}\n\n\t\tlet environmentKind: Environment[\"kind\"] | \"_unknown\" | undefined =\n\t\t\tundefined;\n\t\tif (args._includeEnvironmentKind) {\n\t\t\tif (this.project.checkSupportsEnvironments()) {\n\t\t\t\ttry {\n\t\t\t\t\tconst { activeEnvironment } =\n\t\t\t\t\t\tawait this.project.fetchActiveEnvironment();\n\t\t\t\t\tenvironmentKind = activeEnvironment.kind;\n\t\t\t\t} catch {\n\t\t\t\t\tenvironmentKind = \"_unknown\";\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// Assume only the production environment can be\n\t\t\t\t// used if the project's adapter does not\n\t\t\t\t// support environments.\n\t\t\t\tenvironmentKind = \"prod\";\n\t\t\t}\n\t\t}\n\n\t\tconst payload: {\n\t\t\tevent: HumanSegmentEventTypes;\n\t\t\tuserId?: string;\n\t\t\tanonymousId?: string;\n\t\t\tproperties?: Record<string, unknown>;\n\t\t\tcontext?: Partial<TelemetryManagerContext> & {\n\t\t\t\tgroupId?: {\n\t\t\t\t\tRepository?: string;\n\t\t\t\t};\n\t\t\t};\n\t\t} = {\n\t\t\tevent: HumanSegmentEventType[event],\n\t\t\tproperties: {\n\t\t\t\tnodeVersion: process.versions.node,\n\t\t\t\tenvironmentKind,\n\t\t\t\t...properties,\n\t\t\t},\n\t\t\tcontext: { ...this._context },\n\t\t};\n\n\t\tif (this._userID) {\n\t\t\tpayload.userId = this._userID;\n\t\t} else {\n\t\t\tpayload.anonymousId = this._anonymousID;\n\t\t}\n\n\t\tif (repositoryName) {\n\t\t\tpayload.context ||= {};\n\t\t\tpayload.context.groupId ||= {};\n\t\t\tpayload.context.groupId.Repository = repositoryName;\n\t\t}\n\n\t\treturn new Promise((resolve) => {\n\t\t\tassertTelemetryInitialized(this._segmentClient);\n\n\t\t\t// TODO: Make sure client fails gracefully when no internet connection\n\t\t\tthis._segmentClient().track(\n\t\t\t\tpayload as TrackParams,\n\t\t\t\t(maybeError?: unknown) => {\n\t\t\t\t\tif (maybeError && import.meta.env.DEV) {\n\t\t\t\t\t\t// TODO: Not sure how we want to deal with that\n\t\t\t\t\t\tconsole.warn(\n\t\t\t\t\t\t\t`An error occurred during Segment tracking`,\n\t\t\t\t\t\t\tmaybeError,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tresolve();\n\t\t\t\t},\n\t\t\t);\n\t\t});\n\t}\n\n\t// TODO: Should `userID` and `intercomHash` be automatically populated\n\t// by the logged in user? We already have their info via\n\t// UserRepository.\n\tidentify(args: TelemetryManagerIdentifyArgs): Promise<void> {\n\t\tconst payload = {\n\t\t\tuserId: args.userID,\n\t\t\tanonymousId: this._anonymousID,\n\t\t\tintegrations: {\n\t\t\t\tIntercom: {\n\t\t\t\t\tuser_hash: args.intercomHash,\n\t\t\t\t},\n\t\t\t},\n\t\t\tcontext: { ...this._context },\n\t\t};\n\n\t\tthis._userID = args.userID;\n\n\t\treturn new Promise((resolve) => {\n\t\t\tassertTelemetryInitialized(this._segmentClient);\n\n\t\t\t// TODO: Make sure client fails gracefully when no internet connection\n\t\t\tthis._segmentClient().identify(payload, (maybeError?: unknown) => {\n\t\t\t\tif (maybeError && import.meta.env.DEV) {\n\t\t\t\t\t// TODO: Not sure how we want to deal with that\n\t\t\t\t\tconsole.warn(`An error occurred during Segment identify`, maybeError);\n\t\t\t\t}\n\n\t\t\t\tresolve();\n\t\t\t});\n\t\t});\n\t}\n\n\tasync group(args: TelemetryManagerGroupArgs): Promise<void> {\n\t\tlet repositoryName;\n\n\t\ttry {\n\t\t\trepositoryName = await this.project.getRepositoryName();\n\t\t} catch (error) {\n\t\t\t// noop, happen only when the user is not in a project\n\t\t}\n\n\t\tconst payload: {\n\t\t\tgroupId?: string;\n\t\t\tuserId?: string;\n\t\t\tanonymousId?: string;\n\t\t\ttraits?: Record<string, unknown>;\n\t\t\tcontext?: Partial<TelemetryManagerContext> & {\n\t\t\t\tgroupId?: {\n\t\t\t\t\tRepository?: string;\n\t\t\t\t};\n\t\t\t};\n\t\t} = {\n\t\t\ttraits: args,\n\t\t\tcontext: { ...this._context },\n\t\t};\n\n\t\tif (this._userID) {\n\t\t\tpayload.userId = this._userID;\n\t\t} else {\n\t\t\tpayload.anonymousId = this._anonymousID;\n\t\t}\n\n\t\tif (repositoryName) {\n\t\t\tpayload.groupId = repositoryName;\n\t\t\tpayload.context ||= {};\n\t\t\tpayload.context.groupId ||= {};\n\t\t\tpayload.context.groupId.Repository = repositoryName;\n\t\t}\n\n\t\treturn new Promise((resolve) => {\n\t\t\tassertTelemetryInitialized(this._segmentClient);\n\n\t\t\tthis._segmentClient().group(\n\t\t\t\tpayload as GroupParams,\n\t\t\t\t(maybeError?: unknown) => {\n\t\t\t\t\tif (maybeError && import.meta.env.DEV) {\n\t\t\t\t\t\t// TODO: Not sure how we want to deal with that\n\t\t\t\t\t\tconsole.warn(`An error occurred during Segment group`, maybeError);\n\t\t\t\t\t}\n\n\t\t\t\t\tresolve();\n\t\t\t\t},\n\t\t\t);\n\t\t});\n\t}\n\n\tasync checkIsTelemetryEnabled(): Promise<boolean> {\n\t\tlet root: string;\n\t\ttry {\n\t\t\troot = await this.project\n\t\t\t\t.getRoot()\n\t\t\t\t.catch(() => this.project.suggestRoot());\n\n\t\t\treturn readPrismicrc(root).telemetry !== false;\n\t\t} catch {\n\t\t\treturn true;\n\t\t}\n\t}\n\n\tprivate initExperiment(): void {\n\t\ttry {\n\t\t\tthis._experiment = Experiment.initializeRemote(API_TOKENS.AmplitudeKey);\n\t\t} catch (error) {\n\t\t\tif (import.meta.env.DEV) {\n\t\t\t\tconsole.error(\"Error initializing experiment\", error);\n\t\t\t}\n\t\t}\n\t}\n\n\tasync getExperimentVariant(variantKey: string): Promise<Variant | undefined> {\n\t\tif (this._experiment) {\n\t\t\ttry {\n\t\t\t\tconst repositoryName = await this.project.getRepositoryName();\n\t\t\t\tconst variants = await this._experiment.fetchV2({\n\t\t\t\t\tuser_id: this._userID,\n\t\t\t\t\tuser_properties: {\n\t\t\t\t\t\tRepository: repositoryName,\n\t\t\t\t\t},\n\t\t\t\t});\n\n\t\t\t\treturn variants[variantKey];\n\t\t\t} catch (error) {\n\t\t\t\tif (import.meta.env.DEV) {\n\t\t\t\t\tconsole.error(\"Error fetching experiment variant\", error);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn undefined;\n\t}\n}\n"],"names":["Experiment"],"mappings":";;;;;;;;;;;;;;AAiDA,SAAS,2BACR,eAA4C;AAE5C,MAAI,kBAAkB,QAAW;AAC1B,UAAA,IAAI,MACT,kIAAkI;AAAA,EAEnI;AACF;AAEM,MAAO,yBAAyB,YAAW;AAAA,EAA3C;AAAA;AACG;AACA;AACA;AACA;AACA;AAAA;AAAA,EAER,MAAM,cAAc,MAAuC;AACpD,UAAA,qBAAqB,MAAM,KAAK;AAEtC,SAAK,iBAAiB,MAAK;AACpB,YAAA,YAAY,IAAI,UAAU;AAAA,QAC/B,UAAU,WAAW;AAAA;AAAA,QAErB,kBAAkB;AAAA;AAAA,QAElB,SAAS,CAAC;AAAA,MAAA,CACV;AAES,gBAAA,GAAG,SAAS,CAAC,UAAS;AAAA,MAO9B,CACD;AAEM,aAAA;AAAA,IAAA;AAGR,QAAI,oBAAoB;AACvB,WAAK,eAAc;AAAA,IACnB;AAED,SAAK,eAAe;AACf,SAAA,WAAW,EAAE,KAAK,EAAE,MAAM,KAAK,SAAS,SAAS,KAAK;EAC5D;AAAA;AAAA;AAAA,EAIA,MAAM,MAAM,MAA+B;;AAC1C,UAAM,EAAE,OAAO,YAAY,GAAG,eAAe;AAC7C,QAAI,iBAAiB;AAErB,QAAI,mBAAmB,QAAW;AAC7B,UAAA;AACc,yBAAA,MAAM,KAAK,QAAQ;eAC5B;MAER;AAAA,IACD;AAED,QAAI,kBACH;AACD,QAAI,KAAK,yBAAyB;AAC7B,UAAA,KAAK,QAAQ,6BAA6B;AACzC,YAAA;AACH,gBAAM,EAAE,kBAAiB,IACxB,MAAM,KAAK,QAAQ,uBAAsB;AAC1C,4BAAkB,kBAAkB;AAAA,QAAA,QACnC;AACiB,4BAAA;AAAA,QAClB;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,UAAU;AAAA,eAClB;MAIR;AAAA,IACD;AAEM,WAAA;AAAA,EACR;AACA;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@slicemachine/manager",
|
|
3
|
-
"version": "0.17.9-beta.
|
|
3
|
+
"version": "0.17.9-beta.9",
|
|
4
4
|
"description": "Manage all aspects of a Slice Machine project.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
"@prismicio/mocks": "2.0.0",
|
|
70
70
|
"@prismicio/types-internal": "^2.2.0",
|
|
71
71
|
"@segment/analytics-node": "1.1.3",
|
|
72
|
-
"@slicemachine/plugin-kit": "0.4.35-beta.
|
|
72
|
+
"@slicemachine/plugin-kit": "0.4.35-beta.9",
|
|
73
73
|
"@wooorm/starry-night": "^1.6.0",
|
|
74
74
|
"cookie": "^0.5.0",
|
|
75
75
|
"cors": "^2.8.5",
|
|
@@ -90,7 +90,7 @@ export class TelemetryManager extends BaseManager {
|
|
|
90
90
|
};
|
|
91
91
|
|
|
92
92
|
if (isTelemetryEnabled) {
|
|
93
|
-
|
|
93
|
+
this.initExperiment();
|
|
94
94
|
}
|
|
95
95
|
|
|
96
96
|
this._anonymousID = randomUUID();
|
|
@@ -272,15 +272,17 @@ export class TelemetryManager extends BaseManager {
|
|
|
272
272
|
async checkIsTelemetryEnabled(): Promise<boolean> {
|
|
273
273
|
let root: string;
|
|
274
274
|
try {
|
|
275
|
-
root = await this.project
|
|
275
|
+
root = await this.project
|
|
276
|
+
.getRoot()
|
|
277
|
+
.catch(() => this.project.suggestRoot());
|
|
278
|
+
|
|
279
|
+
return readPrismicrc(root).telemetry !== false;
|
|
276
280
|
} catch {
|
|
277
|
-
|
|
281
|
+
return true;
|
|
278
282
|
}
|
|
279
|
-
|
|
280
|
-
return readPrismicrc(root).telemetry !== false;
|
|
281
283
|
}
|
|
282
284
|
|
|
283
|
-
private
|
|
285
|
+
private initExperiment(): void {
|
|
284
286
|
try {
|
|
285
287
|
this._experiment = Experiment.initializeRemote(API_TOKENS.AmplitudeKey);
|
|
286
288
|
} catch (error) {
|