From 75e8e87b67d8caafbd02999d6361e80c32dd45ee Mon Sep 17 00:00:00 2001 From: Ritchie Martori Date: Mon, 16 Sep 2024 19:30:57 -0700 Subject: [PATCH] add version tracking --- app/packages/analytics/src/analytics.test.ts | 14 ++++++++++++++ app/packages/analytics/src/usingAnalytics.ts | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/app/packages/analytics/src/analytics.test.ts b/app/packages/analytics/src/analytics.test.ts index c602afa073..72f5544720 100644 --- a/app/packages/analytics/src/analytics.test.ts +++ b/app/packages/analytics/src/analytics.test.ts @@ -195,4 +195,18 @@ describe("Analytics", () => { const redacted4 = analytics.redact(undefined); expect(redacted4).toEqual(undefined); }); + + it("should allow setting version", () => { + analytics.load({ + writeKey: "test", + userId: "user", + userGroup: "group", + debug: false, + version: "1.0.0", + }); + analytics.track("custom_event"); + expect(mockSegment.track).toHaveBeenCalledWith("custom_event", undefined, { + version: "1.0.0", + }); + }); }); diff --git a/app/packages/analytics/src/usingAnalytics.ts b/app/packages/analytics/src/usingAnalytics.ts index 8d3902d6e7..5c410323e1 100644 --- a/app/packages/analytics/src/usingAnalytics.ts +++ b/app/packages/analytics/src/usingAnalytics.ts @@ -8,6 +8,7 @@ export type AnalyticsInfo = { debug: boolean; disableUrlTracking?: boolean; redact?: string[]; + version?: string; }; export type AnalyticsConfig = { @@ -33,6 +34,7 @@ export class Analytics { private _debounceInterval = 1000; // Default debounce interval in milliseconds (5 seconds) private _disableUrlTracking = false; private _redactedProperties: string[] = []; + private _version?: string; constructor(config?: AnalyticsConfig) { if (config?.debounceInterval) { @@ -75,6 +77,9 @@ export class Analytics { this._segment = AnalyticsBrowser.load({ writeKey: info.writeKey, }); + if (info.version) { + this._version = info.version; + } if (info.userId) { this.identify(info.userId); } @@ -116,6 +121,9 @@ export class Analytics { if (this._disableUrlTracking) { opts = { context: { page: { url: undefined } } }; } + if (this._version) { + opts = { ...opts, version: this._version }; + } this._segment.track(name, properties, opts); }