@workleap/mixpanel 4.0.8 → 5.0.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,27 @@
1
1
  # @workleap/mixpanel
2
2
 
3
+ ## 5.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - [#150](https://github.com/workleap/wl-telemetry/pull/150) [`133e534`](https://github.com/workleap/wl-telemetry/commit/133e53429453d821086574789e29437001a3c0d5) Thanks [@patricklafrance](https://github.com/patricklafrance)! - Remove deprecated global variables and functions. Updated `initializeMixpanel` signature with breaking changes. View [migration documentation](https://workleap.github.io/wl-telemetry/updating/mixpanel/migrate-to-v4.0/).
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [[`133e534`](https://github.com/workleap/wl-telemetry/commit/133e53429453d821086574789e29437001a3c0d5), [`133e534`](https://github.com/workleap/wl-telemetry/commit/133e53429453d821086574789e29437001a3c0d5)]:
12
+ - @workleap-telemetry/core@2.0.0
13
+ - @workleap/logrocket@3.0.0
14
+
15
+ ## 4.0.9
16
+
17
+ ### Patch Changes
18
+
19
+ - [#148](https://github.com/workleap/wl-telemetry/pull/148) [`1c81d0a`](https://github.com/workleap/wl-telemetry/commit/1c81d0aa6e8ea5793b34a76a491c837748c7612b) Thanks [@patricklafrance](https://github.com/patricklafrance)! - Bumped dependencies.
20
+
21
+ - Updated dependencies [[`1c81d0a`](https://github.com/workleap/wl-telemetry/commit/1c81d0aa6e8ea5793b34a76a491c837748c7612b)]:
22
+ - @workleap/logrocket@2.1.9
23
+ - @workleap-telemetry/core@1.0.8
24
+
3
25
  ## 4.0.8
4
26
 
5
27
  ### Patch Changes
@@ -1,6 +1,5 @@
1
1
  import type { CreateMixpanelTrackingFunctionOptions, MixpanelTrackingFunction } from "@workleap-telemetry/core";
2
2
  import type { Logger } from "@workleap/logging";
3
- export type MixpanelGlobalEventProperties = Map<string, unknown>;
4
3
  /**
5
4
  * @see {@link https://workleap.github.io/wl-telemetry}
6
5
  */
@@ -8,7 +7,7 @@ export interface MixpanelClient {
8
7
  /**
9
8
  * @see {@link https://workleap.github.io/wl-telemetry}
10
9
  */
11
- createTrackingFunction: (options: CreateMixpanelTrackingFunctionOptions) => MixpanelTrackingFunction;
10
+ createTrackingFunction: (options?: CreateMixpanelTrackingFunctionOptions) => MixpanelTrackingFunction;
12
11
  /**
13
12
  * @see {@link https://workleap.github.io/wl-telemetry}
14
13
  */
@@ -18,9 +17,18 @@ export interface MixpanelClient {
18
17
  */
19
18
  setGlobalEventProperties: (values: Record<string, unknown>) => void;
20
19
  }
20
+ /**
21
+ * @see {@link https://workleap.github.io/wl-telemetry}
22
+ */
23
+ export interface MixpanelClientOptions {
24
+ /**
25
+ * @see {@link https://workleap.github.io/wl-telemetry}
26
+ */
27
+ productId?: string;
28
+ }
21
29
  export declare class MixpanelClientImpl implements MixpanelClient {
22
30
  #private;
23
- constructor(productId: string, endpoint: string, globalEventProperties: MixpanelGlobalEventProperties, logger: Logger);
31
+ constructor(endpoint: string, logger: Logger, options?: MixpanelClientOptions);
24
32
  createTrackingFunction(options?: CreateMixpanelTrackingFunctionOptions): MixpanelTrackingFunction;
25
33
  setGlobalEventProperty(key: string, value: unknown): void;
26
34
  setGlobalEventProperties(values: Record<string, unknown>): void;
@@ -5,20 +5,20 @@ import { getBaseProperties } from "./properties.js";
5
5
  ;// CONCATENATED MODULE: ./src/js/MixpanelClient.ts
6
6
 
7
7
  class MixpanelClientImpl {
8
- #productId;
8
+ #globalEventProperties = new Map();
9
9
  #endpoint;
10
- #globalEventProperties;
11
10
  #logger;
12
- constructor(productId, endpoint, globalEventProperties, logger){
13
- this.#productId = productId;
11
+ #productId;
12
+ constructor(endpoint, logger, options = {}){
13
+ const { productId } = options;
14
14
  this.#endpoint = endpoint;
15
- this.#globalEventProperties = globalEventProperties;
16
15
  this.#logger = logger;
16
+ this.#productId = productId;
17
17
  }
18
18
  // IMPORTANT: If you update this method, make sure to update the MixpanelPartialClient
19
19
  // interface as well in @workleap-telemetry/core.
20
20
  createTrackingFunction(options = {}) {
21
- const { targetProductId } = options;
21
+ const { productId, targetProductId, additionalProperties = {} } = options;
22
22
  const trackFunction = async (eventName, properties, _options = {})=>{
23
23
  try {
24
24
  const { keepAlive = false } = _options;
@@ -26,6 +26,7 @@ class MixpanelClientImpl {
26
26
  const allProperties = {
27
27
  ...baseProperties,
28
28
  ...Object.fromEntries(this.#globalEventProperties),
29
+ ...additionalProperties,
29
30
  ...properties
30
31
  };
31
32
  await fetch(this.#endpoint, {
@@ -37,7 +38,7 @@ class MixpanelClientImpl {
37
38
  },
38
39
  body: JSON.stringify({
39
40
  eventName,
40
- productIdentifier: this.#productId,
41
+ productIdentifier: productId ?? this.#productId,
41
42
  // Not sure why, but it seems important to send "null" if not target product identifier
42
43
  // are provided.
43
44
  targetProductIdentifier: targetProductId ?? null,
@@ -1 +1 @@
1
- {"version":3,"file":"js/MixpanelClient.js","sources":["../../src/js/MixpanelClient.ts"],"sourcesContent":["import type { CreateMixpanelTrackingFunctionOptions, MixpanelTrackingFunction } from \"@workleap-telemetry/core\";\nimport type { Logger } from \"@workleap/logging\";\nimport { getBaseProperties } from \"./properties.ts\";\n\nexport type MixpanelGlobalEventProperties = Map<string, unknown>;\n\n/**\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\nexport interface MixpanelClient {\n /**\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\n createTrackingFunction: (options: CreateMixpanelTrackingFunctionOptions) => MixpanelTrackingFunction;\n\n /**\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\n setGlobalEventProperty: (key: string, value: unknown) => void;\n\n /**\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\n setGlobalEventProperties: (values: Record<string, unknown>) => void;\n}\n\nexport class MixpanelClientImpl implements MixpanelClient {\n readonly #productId: string;\n readonly #endpoint: string;\n readonly #globalEventProperties: MixpanelGlobalEventProperties;\n readonly #logger: Logger;\n\n constructor(productId: string, endpoint: string, globalEventProperties: MixpanelGlobalEventProperties, logger: Logger) {\n this.#productId = productId;\n this.#endpoint = endpoint;\n this.#globalEventProperties = globalEventProperties;\n this.#logger = logger;\n }\n\n // IMPORTANT: If you update this method, make sure to update the MixpanelPartialClient\n // interface as well in @workleap-telemetry/core.\n createTrackingFunction(options: CreateMixpanelTrackingFunctionOptions = {}) {\n const {\n targetProductId\n } = options;\n\n const trackFunction: MixpanelTrackingFunction = async (eventName, properties, _options = {}) => {\n try {\n const {\n keepAlive = false\n } = _options;\n\n const baseProperties = getBaseProperties();\n\n const allProperties = {\n ...baseProperties,\n ...Object.fromEntries(this.#globalEventProperties),\n ...properties\n };\n\n await fetch(this.#endpoint, {\n method: \"POST\",\n credentials: \"include\",\n keepalive: keepAlive,\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify({\n eventName,\n productIdentifier: this.#productId,\n // Not sure why, but it seems important to send \"null\" if not target product identifier\n // are provided.\n targetProductIdentifier: targetProductId ?? null,\n properties: allProperties\n })\n });\n } catch (error: unknown) {\n this.#logger\n .withText(`[mixpanel] An error occurred while sending a tracking event to \"${this.#endpoint}\":`)\n .withError(error as Error)\n .error();\n }\n };\n\n return trackFunction;\n }\n\n setGlobalEventProperty(key: string, value: unknown) {\n this.#globalEventProperties.set(key, value);\n }\n\n setGlobalEventProperties(values: Record<string, unknown>) {\n Object.keys(values).forEach(x => {\n this.#globalEventProperties.set(x, values[x]);\n });\n }\n}\n"],"names":["getBaseProperties","MixpanelClientImpl","productId","endpoint","globalEventProperties","logger","options","targetProductId","trackFunction","eventName","properties","_options","keepAlive","baseProperties","allProperties","Object","fetch","JSON","error","key","value","values","x"],"mappings":";;;;;AAEoD;AAwB7C,MAAMC;IACA,UAAU,CAAS;IACnB,SAAS,CAAS;IAClB,sBAAsB,CAAgC;IACtD,OAAO,CAAS;IAEzB,YAAYC,SAAiB,EAAEC,QAAgB,EAAEC,qBAAoD,EAAEC,MAAc,CAAE;QACnH,IAAI,CAAC,UAAU,GAAGH;QAClB,IAAI,CAAC,SAAS,GAAGC;QACjB,IAAI,CAAC,sBAAsB,GAAGC;QAC9B,IAAI,CAAC,OAAO,GAAGC;IACnB;IAEA,sFAAsF;IACtF,iDAAiD;IACjD,uBAAuBC,UAAiD,CAAC,CAAC,EAAE;QACxE,MAAM,EACFC,eAAe,EAClB,GAAGD;QAEJ,MAAME,gBAA0C,OAAOC,WAAWC,YAAYC,WAAW,CAAC,CAAC;YACvF,IAAI;gBACA,MAAM,EACFC,YAAY,KAAK,EACpB,GAAGD;gBAEJ,MAAME,iBAAiBb,iBAAiBA;gBAExC,MAAMc,gBAAgB;oBAClB,GAAGD,cAAc;oBACjB,GAAGE,OAAO,WAAW,CAAC,IAAI,CAAC,sBAAsB,CAAC;oBAClD,GAAGL,UAAU;gBACjB;gBAEA,MAAMM,MAAM,IAAI,CAAC,SAAS,EAAE;oBACxB,QAAQ;oBACR,aAAa;oBACb,WAAWJ;oBACX,SAAS;wBAAE,gBAAgB;oBAAmB;oBAC9C,MAAMK,KAAK,SAAS,CAAC;wBACjBR;wBACA,mBAAmB,IAAI,CAAC,UAAU;wBAClC,uFAAuF;wBACvF,gBAAgB;wBAChB,yBAAyBF,mBAAmB;wBAC5C,YAAYO;oBAChB;gBACJ;YACJ,EAAE,OAAOI,OAAgB;gBACrB,IAAI,CAAC,OAAO,CACP,QAAQ,CAAC,CAAC,gEAAgE,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,EAC9F,SAAS,CAACA,OACV,KAAK;YACd;QACJ;QAEA,OAAOV;IACX;IAEA,uBAAuBW,GAAW,EAAEC,KAAc,EAAE;QAChD,IAAI,CAAC,sBAAsB,CAAC,GAAG,CAACD,KAAKC;IACzC;IAEA,yBAAyBC,MAA+B,EAAE;QACtDN,OAAO,IAAI,CAACM,QAAQ,OAAO,CAACC,CAAAA;YACxB,IAAI,CAAC,sBAAsB,CAAC,GAAG,CAACA,GAAGD,MAAM,CAACC,EAAE;QAChD;IACJ;AACJ"}
1
+ {"version":3,"file":"js/MixpanelClient.js","sources":["../../src/js/MixpanelClient.ts"],"sourcesContent":["import type { CreateMixpanelTrackingFunctionOptions, MixpanelTrackingFunction } from \"@workleap-telemetry/core\";\nimport type { Logger } from \"@workleap/logging\";\nimport { getBaseProperties } from \"./properties.ts\";\n\n/**\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\nexport interface MixpanelClient {\n /**\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\n createTrackingFunction: (options?: CreateMixpanelTrackingFunctionOptions) => MixpanelTrackingFunction;\n /**\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\n setGlobalEventProperty: (key: string, value: unknown) => void;\n /**\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\n setGlobalEventProperties: (values: Record<string, unknown>) => void;\n}\n\n/**\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\nexport interface MixpanelClientOptions {\n /**\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\n productId?: string;\n}\n\nexport class MixpanelClientImpl implements MixpanelClient {\n readonly #globalEventProperties = new Map<string, unknown>();\n readonly #endpoint: string;\n readonly #logger: Logger;\n readonly #productId?: string;\n\n constructor(endpoint: string, logger: Logger, options: MixpanelClientOptions = {}) {\n const {\n productId\n } = options;\n\n this.#endpoint = endpoint;\n this.#logger = logger;\n this.#productId = productId;\n }\n\n // IMPORTANT: If you update this method, make sure to update the MixpanelPartialClient\n // interface as well in @workleap-telemetry/core.\n createTrackingFunction(options: CreateMixpanelTrackingFunctionOptions = {}) {\n const {\n productId,\n targetProductId,\n additionalProperties = {}\n } = options;\n\n const trackFunction: MixpanelTrackingFunction = async (eventName, properties, _options = {}) => {\n try {\n const {\n keepAlive = false\n } = _options;\n\n const baseProperties = getBaseProperties();\n\n const allProperties = {\n ...baseProperties,\n ...Object.fromEntries(this.#globalEventProperties),\n ...additionalProperties,\n ...properties\n };\n\n await fetch(this.#endpoint, {\n method: \"POST\",\n credentials: \"include\",\n keepalive: keepAlive,\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify({\n eventName,\n productIdentifier: productId ?? this.#productId,\n // Not sure why, but it seems important to send \"null\" if not target product identifier\n // are provided.\n targetProductIdentifier: targetProductId ?? null,\n properties: allProperties\n })\n });\n } catch (error: unknown) {\n this.#logger\n .withText(`[mixpanel] An error occurred while sending a tracking event to \"${this.#endpoint}\":`)\n .withError(error as Error)\n .error();\n }\n };\n\n return trackFunction;\n }\n\n setGlobalEventProperty(key: string, value: unknown) {\n this.#globalEventProperties.set(key, value);\n }\n\n setGlobalEventProperties(values: Record<string, unknown>) {\n Object.keys(values).forEach(x => {\n this.#globalEventProperties.set(x, values[x]);\n });\n }\n}\n"],"names":["getBaseProperties","MixpanelClientImpl","Map","endpoint","logger","options","productId","targetProductId","additionalProperties","trackFunction","eventName","properties","_options","keepAlive","baseProperties","allProperties","Object","fetch","JSON","error","key","value","values","x"],"mappings":";;;;;AAEoD;AA8B7C,MAAMC;IACA,sBAAsB,GAAG,IAAIC,MAAuB;IACpD,SAAS,CAAS;IAClB,OAAO,CAAS;IAChB,UAAU,CAAU;IAE7B,YAAYC,QAAgB,EAAEC,MAAc,EAAEC,UAAiC,CAAC,CAAC,CAAE;QAC/E,MAAM,EACFC,SAAS,EACZ,GAAGD;QAEJ,IAAI,CAAC,SAAS,GAAGF;QACjB,IAAI,CAAC,OAAO,GAAGC;QACf,IAAI,CAAC,UAAU,GAAGE;IACtB;IAEA,sFAAsF;IACtF,iDAAiD;IACjD,uBAAuBD,UAAiD,CAAC,CAAC,EAAE;QACxE,MAAM,EACFC,SAAS,EACTC,eAAe,EACfC,uBAAuB,CAAC,CAAC,EAC5B,GAAGH;QAEJ,MAAMI,gBAA0C,OAAOC,WAAWC,YAAYC,WAAW,CAAC,CAAC;YACvF,IAAI;gBACA,MAAM,EACFC,YAAY,KAAK,EACpB,GAAGD;gBAEJ,MAAME,iBAAiBd,iBAAiBA;gBAExC,MAAMe,gBAAgB;oBAClB,GAAGD,cAAc;oBACjB,GAAGE,OAAO,WAAW,CAAC,IAAI,CAAC,sBAAsB,CAAC;oBAClD,GAAGR,oBAAoB;oBACvB,GAAGG,UAAU;gBACjB;gBAEA,MAAMM,MAAM,IAAI,CAAC,SAAS,EAAE;oBACxB,QAAQ;oBACR,aAAa;oBACb,WAAWJ;oBACX,SAAS;wBAAE,gBAAgB;oBAAmB;oBAC9C,MAAMK,KAAK,SAAS,CAAC;wBACjBR;wBACA,mBAAmBJ,aAAa,IAAI,CAAC,UAAU;wBAC/C,uFAAuF;wBACvF,gBAAgB;wBAChB,yBAAyBC,mBAAmB;wBAC5C,YAAYQ;oBAChB;gBACJ;YACJ,EAAE,OAAOI,OAAgB;gBACrB,IAAI,CAAC,OAAO,CACP,QAAQ,CAAC,CAAC,gEAAgE,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,EAC9F,SAAS,CAACA,OACV,KAAK;YACd;QACJ;QAEA,OAAOV;IACX;IAEA,uBAAuBW,GAAW,EAAEC,KAAc,EAAE;QAChD,IAAI,CAAC,sBAAsB,CAAC,GAAG,CAACD,KAAKC;IACzC;IAEA,yBAAyBC,MAA+B,EAAE;QACtDN,OAAO,IAAI,CAACM,QAAQ,OAAO,CAACC,CAAAA;YACxB,IAAI,CAAC,sBAAsB,CAAC,GAAG,CAACA,GAAGD,MAAM,CAACC,EAAE;QAChD;IACJ;AACJ"}
@@ -1,7 +1,6 @@
1
1
  export { createTelemetryContext, type CreateTelemetryContextOptions, type TelemetryContext } from "@workleap-telemetry/core";
2
2
  export type { CreateMixpanelTrackingFunctionOptions, MixpanelEventProperties, MixpanelTrackingFunction, MixpanelTrackingFunctionOptions } from "@workleap-telemetry/core";
3
- export { createTrackingFunction } from "./createTrackingFunction.ts";
4
3
  export type { MixpanelEnvironment } from "./env.ts";
5
4
  export { initializeMixpanel, type InitializeMixpanelOptions } from "./initializeMixpanel.ts";
6
- export type { MixpanelClient, MixpanelGlobalEventProperties } from "./MixpanelClient.ts";
5
+ export type { MixpanelClient } from "./MixpanelClient.ts";
7
6
  export { NoopMixpanelClient } from "./NoopMixpanelClient.ts";
package/dist/js/index.js CHANGED
@@ -1,12 +1,9 @@
1
1
  import { createTelemetryContext } from "@workleap-telemetry/core";
2
- import { createTrackingFunction } from "./createTrackingFunction.js";
3
2
  import { initializeMixpanel } from "./initializeMixpanel.js";
4
3
  import { NoopMixpanelClient } from "./NoopMixpanelClient.js";
5
4
 
6
5
  ;// CONCATENATED MODULE: external "@workleap-telemetry/core"
7
6
 
8
- ;// CONCATENATED MODULE: external "./createTrackingFunction.js"
9
-
10
7
  ;// CONCATENATED MODULE: external "./initializeMixpanel.js"
11
8
 
12
9
  ;// CONCATENATED MODULE: external "./NoopMixpanelClient.js"
@@ -16,7 +13,6 @@ import { NoopMixpanelClient } from "./NoopMixpanelClient.js";
16
13
 
17
14
 
18
15
 
19
-
20
- export { NoopMixpanelClient, createTelemetryContext, createTrackingFunction, initializeMixpanel };
16
+ export { NoopMixpanelClient, createTelemetryContext, initializeMixpanel };
21
17
 
22
18
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"js/index.js","sources":["../../src/js/index.ts"],"sourcesContent":["export { createTelemetryContext, type CreateTelemetryContextOptions, type TelemetryContext } from \"@workleap-telemetry/core\";\nexport type { CreateMixpanelTrackingFunctionOptions, MixpanelEventProperties, MixpanelTrackingFunction, MixpanelTrackingFunctionOptions } from \"@workleap-telemetry/core\";\nexport { createTrackingFunction } from \"./createTrackingFunction.ts\";\nexport type { MixpanelEnvironment } from \"./env.ts\";\nexport { initializeMixpanel, type InitializeMixpanelOptions } from \"./initializeMixpanel.ts\";\nexport type { MixpanelClient, MixpanelGlobalEventProperties } from \"./MixpanelClient.ts\";\nexport { NoopMixpanelClient } from \"./NoopMixpanelClient.ts\";\n\n"],"names":["createTelemetryContext","createTrackingFunction","initializeMixpanel","NoopMixpanelClient"],"mappings":";;;;;;;;;;;;;;AAA6H;AAExD;AAEwB;AAEhC"}
1
+ {"version":3,"file":"js/index.js","sources":["../../src/js/index.ts"],"sourcesContent":["export { createTelemetryContext, type CreateTelemetryContextOptions, type TelemetryContext } from \"@workleap-telemetry/core\";\nexport type { CreateMixpanelTrackingFunctionOptions, MixpanelEventProperties, MixpanelTrackingFunction, MixpanelTrackingFunctionOptions } from \"@workleap-telemetry/core\";\nexport type { MixpanelEnvironment } from \"./env.ts\";\nexport { initializeMixpanel, type InitializeMixpanelOptions } from \"./initializeMixpanel.ts\";\nexport type { MixpanelClient } from \"./MixpanelClient.ts\";\nexport { NoopMixpanelClient } from \"./NoopMixpanelClient.ts\";\n\n"],"names":["createTelemetryContext","initializeMixpanel","NoopMixpanelClient"],"mappings":";;;;;;;;;;;AAA6H;AAGhC;AAEhC"}
@@ -1,13 +1,17 @@
1
1
  import { type LogRocketInstrumentationPartialClient, type TelemetryContext } from "@workleap-telemetry/core";
2
2
  import { type RootLogger } from "@workleap/logging";
3
3
  import { type MixpanelEnvironment } from "./env.ts";
4
- import { type MixpanelClient, type MixpanelGlobalEventProperties } from "./MixpanelClient.ts";
5
- export declare const IsInitializedVariableName = "__WLP_MIXPANEL_IS_INITIALIZED__";
4
+ import { type MixpanelClient } from "./MixpanelClient.ts";
6
5
  export declare function __resetRegistrationGuard(): void;
7
6
  /**
8
7
  * @see {@link https://workleap.github.io/wl-telemetry}
9
8
  */
10
9
  export interface InitializeMixpanelOptions {
10
+ /**
11
+ * An optional product id that will be attached to every event.
12
+ * @see {@link https://workleap.github.io/wl-telemetry}
13
+ */
14
+ productId?: string;
11
15
  /**
12
16
  * An optional tracking endpoint.
13
17
  * @default "tracking/track"
@@ -37,16 +41,13 @@ export interface InitializeMixpanelOptions {
37
41
  loggers?: RootLogger[];
38
42
  }
39
43
  export declare class MixpanelInitializer {
40
- #private;
41
- constructor(globalEventProperties: MixpanelGlobalEventProperties);
42
- initialize(productId: string, envOrTrackingApiBaseUrl: MixpanelEnvironment | (string & {}), options?: InitializeMixpanelOptions): MixpanelClient;
44
+ initialize(envOrTrackingApiBaseUrl: MixpanelEnvironment | (string & {}), options?: Omit<InitializeMixpanelOptions, "globalEventProperties">): MixpanelClient;
43
45
  }
44
46
  /**
45
47
  * Initialize Mixpanel telemetry.
46
- * @param productId The Mixpanel product identifier.
47
48
  * @param envOrTrackingApiBaseUrl The environment to get the navigation url from or a base URL.
48
49
  * @param options Mixpanel instrumentation options.
49
50
  * @returns {MixpanelClient} A Mixpanel client instance.
50
51
  * @see {@link https://workleap.github.io/wl-telemetry}
51
52
  */
52
- export declare function initializeMixpanel(productId: string, envOrTrackingApiBaseUrl: MixpanelEnvironment | (string & {}), options?: InitializeMixpanelOptions): MixpanelClient;
53
+ export declare function initializeMixpanel(envOrTrackingApiBaseUrl: MixpanelEnvironment | (string & {}), options?: InitializeMixpanelOptions): MixpanelClient;
@@ -1,6 +1,5 @@
1
1
  import { HasExecutedGuard } from "@workleap-telemetry/core";
2
2
  import { createCompositeLogger } from "@workleap/logging";
3
- import { setMixpanelContext } from "./context.js";
4
3
  import { getTrackingEndpoint } from "./env.js";
5
4
  import { MixpanelClientImpl } from "./MixpanelClient.js";
6
5
  import { OtherProperties, getTelemetryProperties } from "./properties.js";
@@ -9,8 +8,6 @@ import { OtherProperties, getTelemetryProperties } from "./properties.js";
9
8
 
10
9
  ;// CONCATENATED MODULE: external "@workleap/logging"
11
10
 
12
- ;// CONCATENATED MODULE: external "./context.js"
13
-
14
11
  ;// CONCATENATED MODULE: external "./env.js"
15
12
 
16
13
  ;// CONCATENATED MODULE: external "./MixpanelClient.js"
@@ -23,26 +20,6 @@ import { OtherProperties, getTelemetryProperties } from "./properties.js";
23
20
 
24
21
 
25
22
 
26
-
27
- // DEPRECATED: Grace period ends on January 1th 2026.
28
- // Don't forget to remove the tests as well.
29
- const IsInitializedVariableName = "__WLP_MIXPANEL_IS_INITIALIZED__";
30
- // DEPRECATED: Grace period ends on January 1th 2026.
31
- function registerDeprecatedContextAndGlobalVariables(productId, endpoint, globalEventProperties, logger) {
32
- setMixpanelContext({
33
- productId,
34
- endpoint,
35
- globalEventProperties,
36
- logger
37
- });
38
- // Indicates to the host applications that Mixpanel has been initialized.
39
- // It's useful in cases where an "add-on", like the platform widgets needs
40
- // to know whether or not the host application is using Mixpanel.
41
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
42
- // @ts-ignore
43
- globalThis[IsInitializedVariableName] = true;
44
- }
45
- ///////////////////////////
46
23
  let registrationGuardInstance;
47
24
  // It's important to use a lazy singleton instead of a singleton to avoid
48
25
  // any future probleme related to HMR, tree-shaking, SSR, etc..
@@ -59,18 +36,18 @@ function __resetRegistrationGuard() {
59
36
  }
60
37
  }
61
38
  class MixpanelInitializer {
62
- #globalEventProperties;
63
- constructor(globalEventProperties){
64
- this.#globalEventProperties = globalEventProperties;
65
- }
66
- initialize(productId, envOrTrackingApiBaseUrl, options = {}) {
67
- const { trackingEndpoint, telemetryContext, logRocketInstrumentationClient, verbose = false, loggers = [] } = options;
39
+ initialize(envOrTrackingApiBaseUrl, options = {}) {
40
+ const { productId, trackingEndpoint, telemetryContext, logRocketInstrumentationClient, verbose = false, loggers = [] } = options;
68
41
  const logger = createCompositeLogger(verbose, loggers);
69
42
  const endpoint = getTrackingEndpoint(envOrTrackingApiBaseUrl, trackingEndpoint);
43
+ logger.information("[mixpanel] Mixpanel is initialized.");
44
+ const client = new MixpanelClientImpl(endpoint, logger, {
45
+ productId
46
+ });
70
47
  // Set the telemetry correlation ids on every Mixpanel event.
71
48
  if (telemetryContext) {
72
49
  for (const [key, value] of Object.entries(getTelemetryProperties(telemetryContext))){
73
- this.#globalEventProperties.set(key, value);
50
+ client.setGlobalEventProperty(key, value);
74
51
  }
75
52
  }
76
53
  // If a LogRocket client is already available, register a listener to get the LogRocket session url
@@ -78,28 +55,24 @@ class MixpanelInitializer {
78
55
  if (logRocketInstrumentationClient) {
79
56
  logRocketInstrumentationClient.registerGetSessionUrlListener((sessionUrl)=>{
80
57
  logger.withText("[mixpanel] Received LogRocket session replay URL:").withText(sessionUrl).debug();
81
- this.#globalEventProperties.set(OtherProperties.LogRocketSessionUrl, sessionUrl);
58
+ client.setGlobalEventProperty(OtherProperties.LogRocketSessionUrl, sessionUrl);
82
59
  });
83
60
  }
84
- registerDeprecatedContextAndGlobalVariables(productId, endpoint, this.#globalEventProperties, logger);
85
- logger.information("[mixpanel] Mixpanel is initialized.");
86
- const client = new MixpanelClientImpl(productId, endpoint, this.#globalEventProperties, logger);
87
61
  // The cast is to normalize the inferred return type to the "LogRocketInstrumentationClient" type.
88
62
  return client;
89
63
  }
90
64
  }
91
65
  /**
92
66
  * Initialize Mixpanel telemetry.
93
- * @param productId The Mixpanel product identifier.
94
67
  * @param envOrTrackingApiBaseUrl The environment to get the navigation url from or a base URL.
95
68
  * @param options Mixpanel instrumentation options.
96
69
  * @returns {MixpanelClient} A Mixpanel client instance.
97
70
  * @see {@link https://workleap.github.io/wl-telemetry}
98
- */ function initializeMixpanel(productId, envOrTrackingApiBaseUrl, options) {
71
+ */ function initializeMixpanel(envOrTrackingApiBaseUrl, options) {
99
72
  getRegistrationGuard().throw("[mixpanel] Mixpanel has already been initialized. Did you call the \"initializeMixpanel\" function twice?");
100
- return new MixpanelInitializer(new Map()).initialize(productId, envOrTrackingApiBaseUrl, options);
73
+ return new MixpanelInitializer().initialize(envOrTrackingApiBaseUrl, options);
101
74
  }
102
75
 
103
- export { IsInitializedVariableName, MixpanelInitializer, __resetRegistrationGuard, initializeMixpanel };
76
+ export { MixpanelInitializer, __resetRegistrationGuard, initializeMixpanel };
104
77
 
105
78
  //# sourceMappingURL=initializeMixpanel.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"js/initializeMixpanel.js","sources":["../../src/js/initializeMixpanel.ts"],"sourcesContent":["import { HasExecutedGuard, type LogRocketInstrumentationPartialClient, type TelemetryContext } from \"@workleap-telemetry/core\";\nimport { createCompositeLogger, type Logger, type RootLogger } from \"@workleap/logging\";\nimport { setMixpanelContext } from \"./context.ts\";\nimport { getTrackingEndpoint, type MixpanelEnvironment } from \"./env.ts\";\nimport { type MixpanelClient, MixpanelClientImpl, type MixpanelGlobalEventProperties } from \"./MixpanelClient.ts\";\nimport { getTelemetryProperties, OtherProperties } from \"./properties.ts\";\n\n// DEPRECATED: Grace period ends on January 1th 2026.\n// Don't forget to remove the tests as well.\nexport const IsInitializedVariableName = \"__WLP_MIXPANEL_IS_INITIALIZED__\";\n\n// DEPRECATED: Grace period ends on January 1th 2026.\nfunction registerDeprecatedContextAndGlobalVariables(productId: string, endpoint: string, globalEventProperties: Map<string, unknown>, logger: Logger) {\n setMixpanelContext({\n productId,\n endpoint,\n globalEventProperties,\n logger\n });\n\n // Indicates to the host applications that Mixpanel has been initialized.\n // It's useful in cases where an \"add-on\", like the platform widgets needs\n // to know whether or not the host application is using Mixpanel.\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n globalThis[IsInitializedVariableName] = true;\n}\n\n///////////////////////////\n\nlet registrationGuardInstance: HasExecutedGuard | undefined;\n\n// It's important to use a lazy singleton instead of a singleton to avoid\n// any future probleme related to HMR, tree-shaking, SSR, etc..\nfunction getRegistrationGuard() {\n if (!registrationGuardInstance) {\n registrationGuardInstance = new HasExecutedGuard();\n }\n\n return registrationGuardInstance;\n}\n\n// This function should only be used by tests.\nexport function __resetRegistrationGuard() {\n if (registrationGuardInstance) {\n registrationGuardInstance.reset();\n }\n}\n\n///////////////////////////\n\n/**\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\nexport interface InitializeMixpanelOptions {\n /**\n * An optional tracking endpoint.\n * @default \"tracking/track\"\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\n trackingEndpoint?: string;\n /**\n * Context including telemetry correlation ids.\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\n telemetryContext?: TelemetryContext;\n /**\n * An optional LogRocket client to automatically add the LogRocket\n * session URL to Mixpanel events once it's available.\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\n logRocketInstrumentationClient?: LogRocketInstrumentationPartialClient;\n /**\n * Whether or not debug information should be logged to the console.\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\n verbose?: boolean;\n /**\n * An array of RootLogger instances.\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\n loggers?: RootLogger[];\n}\n\nexport class MixpanelInitializer {\n readonly #globalEventProperties: MixpanelGlobalEventProperties;\n\n constructor(globalEventProperties: MixpanelGlobalEventProperties) {\n this.#globalEventProperties = globalEventProperties;\n }\n\n initialize(\n productId: string,\n envOrTrackingApiBaseUrl: MixpanelEnvironment | (string & {}),\n options: InitializeMixpanelOptions = {}\n ) {\n const {\n trackingEndpoint,\n telemetryContext,\n logRocketInstrumentationClient,\n verbose = false,\n loggers = []\n } = options;\n\n const logger = createCompositeLogger(verbose, loggers);\n const endpoint = getTrackingEndpoint(envOrTrackingApiBaseUrl, trackingEndpoint);\n\n // Set the telemetry correlation ids on every Mixpanel event.\n if (telemetryContext) {\n for (const [key, value] of Object.entries(getTelemetryProperties(telemetryContext))) {\n this.#globalEventProperties.set(key, value);\n }\n }\n\n // If a LogRocket client is already available, register a listener to get the LogRocket session url\n // once it's available to set it on every Mixpanel event.\n if (logRocketInstrumentationClient) {\n logRocketInstrumentationClient.registerGetSessionUrlListener((sessionUrl: string) => {\n logger\n .withText(\"[mixpanel] Received LogRocket session replay URL:\")\n .withText(sessionUrl)\n .debug();\n\n this.#globalEventProperties.set(OtherProperties.LogRocketSessionUrl, sessionUrl);\n });\n }\n\n registerDeprecatedContextAndGlobalVariables(productId, endpoint, this.#globalEventProperties, logger);\n\n logger.information(\"[mixpanel] Mixpanel is initialized.\");\n\n const client = new MixpanelClientImpl(productId, endpoint, this.#globalEventProperties, logger);\n\n // The cast is to normalize the inferred return type to the \"LogRocketInstrumentationClient\" type.\n return client as MixpanelClient;\n }\n}\n\n/**\n * Initialize Mixpanel telemetry.\n * @param productId The Mixpanel product identifier.\n * @param envOrTrackingApiBaseUrl The environment to get the navigation url from or a base URL.\n * @param options Mixpanel instrumentation options.\n * @returns {MixpanelClient} A Mixpanel client instance.\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\nexport function initializeMixpanel(\n productId: string,\n envOrTrackingApiBaseUrl: MixpanelEnvironment | (string & {}),\n options?: InitializeMixpanelOptions\n) {\n getRegistrationGuard().throw(\"[mixpanel] Mixpanel has already been initialized. Did you call the \\\"initializeMixpanel\\\" function twice?\");\n\n return new MixpanelInitializer(\n new Map<string, unknown>()\n ).initialize(\n productId,\n envOrTrackingApiBaseUrl,\n options\n );\n}\n"],"names":["HasExecutedGuard","createCompositeLogger","setMixpanelContext","getTrackingEndpoint","MixpanelClientImpl","getTelemetryProperties","OtherProperties","IsInitializedVariableName","registerDeprecatedContextAndGlobalVariables","productId","endpoint","globalEventProperties","logger","globalThis","registrationGuardInstance","getRegistrationGuard","__resetRegistrationGuard","MixpanelInitializer","envOrTrackingApiBaseUrl","options","trackingEndpoint","telemetryContext","logRocketInstrumentationClient","verbose","loggers","key","value","Object","sessionUrl","client","initializeMixpanel","Map"],"mappings":";;;;;;;;;;;;;;;;;;;;AAA+H;AACvC;AACtC;AACuB;AACyC;AACxC;AAE1E,qDAAqD;AACrD,4CAA4C;AACrC,MAAMO,4BAA4B,kCAAkC;AAE3E,qDAAqD;AACrD,SAASC,4CAA4CC,SAAiB,EAAEC,QAAgB,EAAEC,qBAA2C,EAAEC,MAAc;IACjJV,kBAAkBA,CAAC;QACfO;QACAC;QACAC;QACAC;IACJ;IAEA,yEAAyE;IACzE,0EAA0E;IAC1E,iEAAiE;IACjE,6DAA6D;IAC7D,aAAa;IACbC,UAAU,CAACN,0BAA0B,GAAG;AAC5C;AAEA,2BAA2B;AAE3B,IAAIO;AAEJ,yEAAyE;AACzE,+DAA+D;AAC/D,SAASC;IACL,IAAI,CAACD,2BAA2B;QAC5BA,4BAA4B,IAAId,gBAAgBA;IACpD;IAEA,OAAOc;AACX;AAEA,8CAA8C;AACvC,SAASE;IACZ,IAAIF,2BAA2B;QAC3BA,0BAA0B,KAAK;IACnC;AACJ;AAqCO,MAAMG;IACA,sBAAsB,CAAgC;IAE/D,YAAYN,qBAAoD,CAAE;QAC9D,IAAI,CAAC,sBAAsB,GAAGA;IAClC;IAEA,WACIF,SAAiB,EACjBS,uBAA4D,EAC5DC,UAAqC,CAAC,CAAC,EACzC;QACE,MAAM,EACFC,gBAAgB,EAChBC,gBAAgB,EAChBC,8BAA8B,EAC9BC,UAAU,KAAK,EACfC,UAAU,EAAE,EACf,GAAGL;QAEJ,MAAMP,SAASX,qBAAqBA,CAACsB,SAASC;QAC9C,MAAMd,WAAWP,mBAAmBA,CAACe,yBAAyBE;QAE9D,6DAA6D;QAC7D,IAAIC,kBAAkB;YAClB,KAAK,MAAM,CAACI,KAAKC,MAAM,IAAIC,OAAO,OAAO,CAACtB,sBAAsBA,CAACgB,mBAAoB;gBACjF,IAAI,CAAC,sBAAsB,CAAC,GAAG,CAACI,KAAKC;YACzC;QACJ;QAEA,mGAAmG;QACnG,yDAAyD;QACzD,IAAIJ,gCAAgC;YAChCA,+BAA+B,6BAA6B,CAAC,CAACM;gBAC1DhB,OACK,QAAQ,CAAC,qDACT,QAAQ,CAACgB,YACT,KAAK;gBAEV,IAAI,CAAC,sBAAsB,CAAC,GAAG,CAACtB,mCAAmC,EAAEsB;YACzE;QACJ;QAEApB,4CAA4CC,WAAWC,UAAU,IAAI,CAAC,sBAAsB,EAAEE;QAE9FA,OAAO,WAAW,CAAC;QAEnB,MAAMiB,SAAS,IAAIzB,kBAAkBA,CAACK,WAAWC,UAAU,IAAI,CAAC,sBAAsB,EAAEE;QAExF,kGAAkG;QAClG,OAAOiB;IACX;AACJ;AAEA;;;;;;;CAOC,GACM,SAASC,mBACZrB,SAAiB,EACjBS,uBAA4D,EAC5DC,OAAmC;IAEnCJ,uBAAuB,KAAK,CAAC;IAE7B,OAAO,IAAIE,oBACP,IAAIc,OACN,UAAU,CACRtB,WACAS,yBACAC;AAER"}
1
+ {"version":3,"file":"js/initializeMixpanel.js","sources":["../../src/js/initializeMixpanel.ts"],"sourcesContent":["import { HasExecutedGuard, type LogRocketInstrumentationPartialClient, type TelemetryContext } from \"@workleap-telemetry/core\";\nimport { createCompositeLogger, type RootLogger } from \"@workleap/logging\";\nimport { getTrackingEndpoint, type MixpanelEnvironment } from \"./env.ts\";\nimport { MixpanelClientImpl, type MixpanelClient } from \"./MixpanelClient.ts\";\nimport { getTelemetryProperties, OtherProperties } from \"./properties.ts\";\n\nlet registrationGuardInstance: HasExecutedGuard | undefined;\n\n// It's important to use a lazy singleton instead of a singleton to avoid\n// any future probleme related to HMR, tree-shaking, SSR, etc..\nfunction getRegistrationGuard() {\n if (!registrationGuardInstance) {\n registrationGuardInstance = new HasExecutedGuard();\n }\n\n return registrationGuardInstance;\n}\n\n// This function should only be used by tests.\nexport function __resetRegistrationGuard() {\n if (registrationGuardInstance) {\n registrationGuardInstance.reset();\n }\n}\n\n///////////////////////////\n\n/**\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\nexport interface InitializeMixpanelOptions {\n /**\n * An optional product id that will be attached to every event.\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\n productId?: string;\n /**\n * An optional tracking endpoint.\n * @default \"tracking/track\"\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\n trackingEndpoint?: string;\n /**\n * Context including telemetry correlation ids.\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\n telemetryContext?: TelemetryContext;\n /**\n * An optional LogRocket client to automatically add the LogRocket\n * session URL to Mixpanel events once it's available.\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\n logRocketInstrumentationClient?: LogRocketInstrumentationPartialClient;\n /**\n * Whether or not debug information should be logged to the console.\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\n verbose?: boolean;\n /**\n * An array of RootLogger instances.\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\n loggers?: RootLogger[];\n}\n\nexport class MixpanelInitializer {\n initialize(envOrTrackingApiBaseUrl: MixpanelEnvironment | (string & {}), options: Omit<InitializeMixpanelOptions, \"globalEventProperties\"> = {}) {\n const {\n productId,\n trackingEndpoint,\n telemetryContext,\n logRocketInstrumentationClient,\n verbose = false,\n loggers = []\n } = options;\n\n const logger = createCompositeLogger(verbose, loggers);\n const endpoint = getTrackingEndpoint(envOrTrackingApiBaseUrl, trackingEndpoint);\n\n logger.information(\"[mixpanel] Mixpanel is initialized.\");\n\n const client = new MixpanelClientImpl(endpoint, logger, {\n productId\n });\n\n // Set the telemetry correlation ids on every Mixpanel event.\n if (telemetryContext) {\n for (const [key, value] of Object.entries(getTelemetryProperties(telemetryContext))) {\n client.setGlobalEventProperty(key, value);\n }\n }\n\n // If a LogRocket client is already available, register a listener to get the LogRocket session url\n // once it's available to set it on every Mixpanel event.\n if (logRocketInstrumentationClient) {\n logRocketInstrumentationClient.registerGetSessionUrlListener((sessionUrl: string) => {\n logger\n .withText(\"[mixpanel] Received LogRocket session replay URL:\")\n .withText(sessionUrl)\n .debug();\n\n client.setGlobalEventProperty(OtherProperties.LogRocketSessionUrl, sessionUrl);\n });\n }\n\n // The cast is to normalize the inferred return type to the \"LogRocketInstrumentationClient\" type.\n return client as MixpanelClient;\n }\n}\n\n/**\n * Initialize Mixpanel telemetry.\n * @param envOrTrackingApiBaseUrl The environment to get the navigation url from or a base URL.\n * @param options Mixpanel instrumentation options.\n * @returns {MixpanelClient} A Mixpanel client instance.\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\nexport function initializeMixpanel(\n envOrTrackingApiBaseUrl: MixpanelEnvironment | (string & {}),\n options?: InitializeMixpanelOptions\n) {\n getRegistrationGuard().throw(\"[mixpanel] Mixpanel has already been initialized. Did you call the \\\"initializeMixpanel\\\" function twice?\");\n\n return new MixpanelInitializer().initialize(\n envOrTrackingApiBaseUrl,\n options\n );\n}\n"],"names":["HasExecutedGuard","createCompositeLogger","getTrackingEndpoint","MixpanelClientImpl","getTelemetryProperties","OtherProperties","registrationGuardInstance","getRegistrationGuard","__resetRegistrationGuard","MixpanelInitializer","envOrTrackingApiBaseUrl","options","productId","trackingEndpoint","telemetryContext","logRocketInstrumentationClient","verbose","loggers","logger","endpoint","client","key","value","Object","sessionUrl","initializeMixpanel"],"mappings":";;;;;;;;;;;;;;;;;AAA+H;AACpD;AACF;AACK;AACJ;AAE1E,IAAIM;AAEJ,yEAAyE;AACzE,+DAA+D;AAC/D,SAASC;IACL,IAAI,CAACD,2BAA2B;QAC5BA,4BAA4B,IAAIN,gBAAgBA;IACpD;IAEA,OAAOM;AACX;AAEA,8CAA8C;AACvC,SAASE;IACZ,IAAIF,2BAA2B;QAC3BA,0BAA0B,KAAK;IACnC;AACJ;AA0CO,MAAMG;IACT,WAAWC,uBAA4D,EAAEC,UAAoE,CAAC,CAAC,EAAE;QAC7I,MAAM,EACFC,SAAS,EACTC,gBAAgB,EAChBC,gBAAgB,EAChBC,8BAA8B,EAC9BC,UAAU,KAAK,EACfC,UAAU,EAAE,EACf,GAAGN;QAEJ,MAAMO,SAASjB,qBAAqBA,CAACe,SAASC;QAC9C,MAAME,WAAWjB,mBAAmBA,CAACQ,yBAAyBG;QAE9DK,OAAO,WAAW,CAAC;QAEnB,MAAME,SAAS,IAAIjB,kBAAkBA,CAACgB,UAAUD,QAAQ;YACpDN;QACJ;QAEA,6DAA6D;QAC7D,IAAIE,kBAAkB;YAClB,KAAK,MAAM,CAACO,KAAKC,MAAM,IAAIC,OAAO,OAAO,CAACnB,sBAAsBA,CAACU,mBAAoB;gBACjFM,OAAO,sBAAsB,CAACC,KAAKC;YACvC;QACJ;QAEA,mGAAmG;QACnG,yDAAyD;QACzD,IAAIP,gCAAgC;YAChCA,+BAA+B,6BAA6B,CAAC,CAACS;gBAC1DN,OACK,QAAQ,CAAC,qDACT,QAAQ,CAACM,YACT,KAAK;gBAEVJ,OAAO,sBAAsB,CAACf,mCAAmC,EAAEmB;YACvE;QACJ;QAEA,kGAAkG;QAClG,OAAOJ;IACX;AACJ;AAEA;;;;;;CAMC,GACM,SAASK,mBACZf,uBAA4D,EAC5DC,OAAmC;IAEnCJ,uBAAuB,KAAK,CAAC;IAE7B,OAAO,IAAIE,sBAAsB,UAAU,CACvCC,yBACAC;AAER"}
@@ -0,0 +1,18 @@
1
+ import { type PropsWithChildren } from "react";
2
+ /**
3
+ * @see {@link https://workleap.github.io/wl-telemetry}
4
+ */
5
+ export interface MixpanelPropertiesProviderProps extends PropsWithChildren {
6
+ value: Record<string, unknown>;
7
+ }
8
+ /**
9
+ * Define Mixpanel properties that will be passed down to nested "useTrackingFunction" or "useMixpanelTrackingFunction" hook.
10
+ * @see {@link https://workleap.github.io/wl-telemetry}
11
+ */
12
+ export declare function MixpanelPropertiesProvider(props: MixpanelPropertiesProviderProps): import("react/jsx-runtime").JSX.Element;
13
+ /**
14
+ * Retrieve the Mixpanel properties from all parent providers.
15
+ * @returns The context value or an empty object literal if no provider isdefined.
16
+ * @see {@link https://workleap.github.io/wl-telemetry}
17
+ */
18
+ export declare const useMixpanelProviderProperties: () => Record<string, unknown>;
@@ -0,0 +1,40 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { createContext, useContext, useMemo } from "react";
3
+
4
+ ;// CONCATENATED MODULE: external "react/jsx-runtime"
5
+
6
+ ;// CONCATENATED MODULE: external "react"
7
+
8
+ ;// CONCATENATED MODULE: ./src/react/MixpanelPropertiesProvider.tsx
9
+
10
+
11
+ const MixpanelPropertiesContext = /*#__PURE__*/ createContext({});
12
+ /**
13
+ * Define Mixpanel properties that will be passed down to nested "useTrackingFunction" or "useMixpanelTrackingFunction" hook.
14
+ * @see {@link https://workleap.github.io/wl-telemetry}
15
+ */ function MixpanelPropertiesProvider(props) {
16
+ const { value, children } = props;
17
+ const parentContext = useContext(MixpanelPropertiesContext);
18
+ const contextValue = useMemo(()=>({
19
+ ...parentContext,
20
+ ...value
21
+ }), [
22
+ parentContext,
23
+ value
24
+ ]);
25
+ return /*#__PURE__*/ jsx(MixpanelPropertiesContext.Provider, {
26
+ value: contextValue,
27
+ children: children
28
+ });
29
+ }
30
+ /**
31
+ * Retrieve the Mixpanel properties from all parent providers.
32
+ * @returns The context value or an empty object literal if no provider isdefined.
33
+ * @see {@link https://workleap.github.io/wl-telemetry}
34
+ */ const useMixpanelProviderProperties = ()=>{
35
+ return useContext(MixpanelPropertiesContext);
36
+ };
37
+
38
+ export { MixpanelPropertiesProvider, useMixpanelProviderProperties };
39
+
40
+ //# sourceMappingURL=MixpanelPropertiesProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react/MixpanelPropertiesProvider.js","sources":["../../src/react/MixpanelPropertiesProvider.tsx"],"sourcesContent":["import { createContext, type PropsWithChildren, useContext, useMemo } from \"react\";\n\nconst MixpanelPropertiesContext = createContext<Record<string, unknown>>({});\n\n/**\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\nexport interface MixpanelPropertiesProviderProps extends PropsWithChildren {\n value: Record<string, unknown>;\n}\n\n/**\n * Define Mixpanel properties that will be passed down to nested \"useTrackingFunction\" or \"useMixpanelTrackingFunction\" hook.\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\nexport function MixpanelPropertiesProvider(props: MixpanelPropertiesProviderProps) {\n const {\n value,\n children\n } = props;\n\n const parentContext = useContext(MixpanelPropertiesContext);\n\n const contextValue = useMemo(() => ({\n ...parentContext,\n ...value\n }), [parentContext, value]);\n\n return (\n <MixpanelPropertiesContext.Provider value={contextValue}>\n {children}\n </MixpanelPropertiesContext.Provider>\n );\n}\n\n/**\n * Retrieve the Mixpanel properties from all parent providers.\n * @returns The context value or an empty object literal if no provider isdefined.\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\nexport const useMixpanelProviderProperties = () => {\n return useContext(MixpanelPropertiesContext);\n};\n"],"names":["createContext","useContext","useMemo","MixpanelPropertiesContext","MixpanelPropertiesProvider","props","value","children","parentContext","contextValue","useMixpanelProviderProperties"],"mappings":";;;;;;;;;AAAmF;AAEnF,MAAMG,0CAA4BH,aAAaA,CAA0B,CAAC;AAS1E;;;CAGC,GACM,SAASI,2BAA2BC,KAAsC;IAC7E,MAAM,EACFC,KAAK,EACLC,QAAQ,EACX,GAAGF;IAEJ,MAAMG,gBAAgBP,UAAUA,CAACE;IAEjC,MAAMM,eAAeP,OAAOA,CAAC,IAAO;YAChC,GAAGM,aAAa;YAChB,GAAGF,KAAK;QACZ,IAAI;QAACE;QAAeF;KAAM;IAE1B,qBACI,IAACH,0BAA0B,QAAQ;QAAC,OAAOM;kBACtCF;;AAGb;AAEA;;;;CAIC,GACM,MAAMG,gCAAgC;IACzC,OAAOT,UAAUA,CAACE;AACtB,EAAE"}
@@ -1,3 +1,4 @@
1
1
  export * from "../js/index.ts";
2
2
  export { MixpanelProvider, useMixpanelClient, type UseMixpanelClientOptions } from "./MixpanelContext.tsx";
3
+ export { MixpanelPropertiesProvider, useMixpanelProviderProperties, type MixpanelPropertiesProviderProps } from "./MixpanelPropertiesProvider.tsx";
3
4
  export { useTrackingFunction, type UseTrackingFunctionOptions } from "./useTrackingFunction.ts";
@@ -1,9 +1,12 @@
1
1
  import { MixpanelProvider, useMixpanelClient } from "./MixpanelContext.js";
2
+ import { MixpanelPropertiesProvider, useMixpanelProviderProperties } from "./MixpanelPropertiesProvider.js";
2
3
  import { useTrackingFunction } from "./useTrackingFunction.js";
3
4
  export * from "../js/index.js";
4
5
 
5
6
  ;// CONCATENATED MODULE: external "./MixpanelContext.js"
6
7
 
8
+ ;// CONCATENATED MODULE: external "./MixpanelPropertiesProvider.js"
9
+
7
10
  ;// CONCATENATED MODULE: external "./useTrackingFunction.js"
8
11
 
9
12
  ;// CONCATENATED MODULE: ./src/react/index.ts
@@ -11,6 +14,7 @@ export * from "../js/index.js";
11
14
 
12
15
 
13
16
 
14
- export { MixpanelProvider, useMixpanelClient, useTrackingFunction };
17
+
18
+ export { MixpanelPropertiesProvider, MixpanelProvider, useMixpanelClient, useMixpanelProviderProperties, useTrackingFunction };
15
19
 
16
20
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"react/index.js","sources":["../../src/react/index.ts"],"sourcesContent":["export * from \"../js/index.ts\";\nexport { MixpanelProvider, useMixpanelClient, type UseMixpanelClientOptions } from \"./MixpanelContext.tsx\";\nexport { useTrackingFunction, type UseTrackingFunctionOptions } from \"./useTrackingFunction.ts\";\n\n"],"names":["MixpanelProvider","useMixpanelClient","useTrackingFunction"],"mappings":";;;;;;;;;AAA+B;AAC4E;AACX"}
1
+ {"version":3,"file":"react/index.js","sources":["../../src/react/index.ts"],"sourcesContent":["export * from \"../js/index.ts\";\nexport { MixpanelProvider, useMixpanelClient, type UseMixpanelClientOptions } from \"./MixpanelContext.tsx\";\nexport { MixpanelPropertiesProvider, useMixpanelProviderProperties, type MixpanelPropertiesProviderProps } from \"./MixpanelPropertiesProvider.tsx\";\nexport { useTrackingFunction, type UseTrackingFunctionOptions } from \"./useTrackingFunction.ts\";\n\n"],"names":["MixpanelProvider","useMixpanelClient","MixpanelPropertiesProvider","useMixpanelProviderProperties","useTrackingFunction"],"mappings":";;;;;;;;;;;;AAA+B;AAC4E;AACwC;AACnD"}
@@ -1,13 +1,13 @@
1
- import { useContext, useMemo } from "react";
2
- import { createTrackingFunction } from "../js/createTrackingFunction.js";
3
- import { MixpanelContext } from "./MixpanelContext.js";
1
+ import { useMemo } from "react";
2
+ import { useMixpanelClient } from "./MixpanelContext.js";
3
+ import { useMixpanelProviderProperties } from "./MixpanelPropertiesProvider.js";
4
4
 
5
5
  ;// CONCATENATED MODULE: external "react"
6
6
 
7
- ;// CONCATENATED MODULE: external "../js/createTrackingFunction.js"
8
-
9
7
  ;// CONCATENATED MODULE: external "./MixpanelContext.js"
10
8
 
9
+ ;// CONCATENATED MODULE: external "./MixpanelPropertiesProvider.js"
10
+
11
11
  ;// CONCATENATED MODULE: ./src/react/useTrackingFunction.ts
12
12
 
13
13
 
@@ -16,19 +16,21 @@ import { MixpanelContext } from "./MixpanelContext.js";
16
16
  * Create a Mixpanel tracking function.
17
17
  * @see {@link https://workleap.github.io/wl-telemetry}
18
18
  */ function useTrackingFunction(options = {}) {
19
- const { targetProductId } = options;
20
- const client = useContext(MixpanelContext);
19
+ const { productId, targetProductId } = options;
20
+ const client = useMixpanelClient();
21
+ const additionalProperties = useMixpanelProviderProperties();
21
22
  return useMemo(()=>{
22
23
  const trackOptions = {
23
- targetProductId
24
+ productId,
25
+ targetProductId,
26
+ additionalProperties
24
27
  };
25
- // DEPRECATED: Grace period ends on January 1th 2026.
26
- // After the grace period, only create the tracking function from the client, remove the fallback
27
- // and also use "useMixpanelClient" to retrieve the client rather than "useContext".
28
- return client ? client.createTrackingFunction(trackOptions) : createTrackingFunction(trackOptions);
28
+ return client.createTrackingFunction(trackOptions);
29
29
  }, [
30
30
  client,
31
- targetProductId
31
+ productId,
32
+ targetProductId,
33
+ additionalProperties
32
34
  ]);
33
35
  }
34
36
 
@@ -1 +1 @@
1
- {"version":3,"file":"react/useTrackingFunction.js","sources":["../../src/react/useTrackingFunction.ts"],"sourcesContent":["import type { CreateMixpanelTrackingFunctionOptions } from \"@workleap-telemetry/core\";\nimport { useContext, useMemo } from \"react\";\nimport { createTrackingFunction } from \"../js/createTrackingFunction.ts\";\nimport { MixpanelContext } from \"./MixpanelContext.tsx\";\n\n/**\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\nexport type UseTrackingFunctionOptions = CreateMixpanelTrackingFunctionOptions;\n\n/**\n * Create a Mixpanel tracking function.\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\nexport function useTrackingFunction(options: UseTrackingFunctionOptions = {}) {\n const {\n targetProductId\n } = options;\n\n const client = useContext(MixpanelContext);\n\n return useMemo(() => {\n const trackOptions = {\n targetProductId\n } satisfies CreateMixpanelTrackingFunctionOptions;\n\n // DEPRECATED: Grace period ends on January 1th 2026.\n // After the grace period, only create the tracking function from the client, remove the fallback\n // and also use \"useMixpanelClient\" to retrieve the client rather than \"useContext\".\n return client\n ? client.createTrackingFunction(trackOptions)\n : createTrackingFunction(trackOptions);\n }, [client, targetProductId]);\n}\n"],"names":["useContext","useMemo","createTrackingFunction","MixpanelContext","useTrackingFunction","options","targetProductId","client","trackOptions"],"mappings":";;;;;;;;;;;AAC4C;AAC6B;AACjB;AAOxD;;;CAGC,GACM,SAASI,oBAAoBC,UAAsC,CAAC,CAAC;IACxE,MAAM,EACFC,eAAe,EAClB,GAAGD;IAEJ,MAAME,SAASP,UAAUA,CAACG,eAAeA;IAEzC,OAAOF,OAAOA,CAAC;QACX,MAAMO,eAAe;YACjBF;QACJ;QAEA,qDAAqD;QACrD,iGAAiG;QACjG,oFAAoF;QACpF,OAAOC,SACDA,OAAO,sBAAsB,CAACC,gBAC9BN,sBAAsBA,CAACM;IACjC,GAAG;QAACD;QAAQD;KAAgB;AAChC"}
1
+ {"version":3,"file":"react/useTrackingFunction.js","sources":["../../src/react/useTrackingFunction.ts"],"sourcesContent":["import type { CreateMixpanelTrackingFunctionOptions } from \"@workleap-telemetry/core\";\nimport { useMemo } from \"react\";\nimport { useMixpanelClient } from \"./MixpanelContext.tsx\";\nimport { useMixpanelProviderProperties } from \"./MixpanelPropertiesProvider.tsx\";\n\n/**\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\nexport type UseTrackingFunctionOptions = CreateMixpanelTrackingFunctionOptions;\n\n/**\n * Create a Mixpanel tracking function.\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\nexport function useTrackingFunction(options: UseTrackingFunctionOptions = {}) {\n const {\n productId,\n targetProductId\n } = options;\n\n const client = useMixpanelClient();\n const additionalProperties = useMixpanelProviderProperties();\n\n return useMemo(() => {\n const trackOptions = {\n productId,\n targetProductId,\n additionalProperties\n } satisfies CreateMixpanelTrackingFunctionOptions;\n\n return client.createTrackingFunction(trackOptions);\n }, [client, productId, targetProductId, additionalProperties]);\n}\n"],"names":["useMemo","useMixpanelClient","useMixpanelProviderProperties","useTrackingFunction","options","productId","targetProductId","client","additionalProperties","trackOptions"],"mappings":";;;;;;;;;;;AACgC;AAC0B;AACuB;AAOjF;;;CAGC,GACM,SAASG,oBAAoBC,UAAsC,CAAC,CAAC;IACxE,MAAM,EACFC,SAAS,EACTC,eAAe,EAClB,GAAGF;IAEJ,MAAMG,SAASN,iBAAiBA;IAChC,MAAMO,uBAAuBN,6BAA6BA;IAE1D,OAAOF,OAAOA,CAAC;QACX,MAAMS,eAAe;YACjBJ;YACAC;YACAE;QACJ;QAEA,OAAOD,OAAO,sBAAsB,CAACE;IACzC,GAAG;QAACF;QAAQF;QAAWC;QAAiBE;KAAqB;AACjE"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@workleap/mixpanel",
3
3
  "author": "Workleap",
4
- "version": "4.0.8",
4
+ "version": "5.0.0",
5
5
  "description": "A package containing utilities for Mixpanel telemetry.",
6
6
  "license": "Apache-2.0",
7
7
  "repository": {
@@ -34,7 +34,7 @@
34
34
  "peerDependencies": {
35
35
  "react": "^18.0.0 || ^19.0.0",
36
36
  "react-dom": "^18.0.0 || ^19.0.0",
37
- "@workleap/logrocket": "2.1.8"
37
+ "@workleap/logrocket": "3.0.0"
38
38
  },
39
39
  "peerDependenciesMeta": {
40
40
  "@workleap/logrocket": {
@@ -48,29 +48,30 @@
48
48
  }
49
49
  },
50
50
  "dependencies": {
51
- "@workleap/logging": "^1.3.3",
52
- "@workleap-telemetry/core": "1.0.7"
51
+ "@workleap/logging": "^1.3.6",
52
+ "@workleap-telemetry/core": "2.0.0"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@eslint/js": "9.39.2",
56
- "@rsbuild/core": "1.7.2",
57
- "@rslib/core": "0.19.2",
58
- "@types/react": "19.2.8",
59
- "@typescript-eslint/parser": "8.53.0",
60
- "@workleap/eslint-configs": "1.1.9",
61
- "@workleap/rslib-configs": "1.1.5",
56
+ "@rsbuild/core": "1.7.3",
57
+ "@rslib/core": "0.19.5",
58
+ "@types/react": "19.2.13",
59
+ "@typescript-eslint/parser": "8.55.0",
60
+ "@typescript/native-preview": "7.0.0-dev.20260209.1",
61
+ "@workleap/eslint-configs": "1.1.12",
62
+ "@workleap/rslib-configs": "1.1.7",
62
63
  "@workleap/typescript-configs": "3.0.7",
63
64
  "eslint": "9.39.2",
64
- "happy-dom": "20.1.0",
65
+ "happy-dom": "20.6.0",
65
66
  "typescript": "5.9.3",
66
- "typescript-eslint": "8.53.0",
67
- "vitest": "4.0.17"
67
+ "typescript-eslint": "8.55.0",
68
+ "vitest": "4.0.18"
68
69
  },
69
70
  "sideEffects": false,
70
71
  "scripts": {
71
72
  "build": "rslib build --config ./rslib.build.ts",
72
73
  "eslint": "eslint . --max-warnings=0 --cache --cache-location node_modules/.cache/eslint",
73
- "typecheck": "tsc",
74
+ "typecheck": "tsgo",
74
75
  "test": "vitest --config vitest.config.ts --no-watch"
75
76
  }
76
77
  }
@@ -2,8 +2,6 @@ import type { CreateMixpanelTrackingFunctionOptions, MixpanelTrackingFunction }
2
2
  import type { Logger } from "@workleap/logging";
3
3
  import { getBaseProperties } from "./properties.ts";
4
4
 
5
- export type MixpanelGlobalEventProperties = Map<string, unknown>;
6
-
7
5
  /**
8
6
  * @see {@link https://workleap.github.io/wl-telemetry}
9
7
  */
@@ -11,37 +9,50 @@ export interface MixpanelClient {
11
9
  /**
12
10
  * @see {@link https://workleap.github.io/wl-telemetry}
13
11
  */
14
- createTrackingFunction: (options: CreateMixpanelTrackingFunctionOptions) => MixpanelTrackingFunction;
15
-
12
+ createTrackingFunction: (options?: CreateMixpanelTrackingFunctionOptions) => MixpanelTrackingFunction;
16
13
  /**
17
14
  * @see {@link https://workleap.github.io/wl-telemetry}
18
15
  */
19
16
  setGlobalEventProperty: (key: string, value: unknown) => void;
20
-
21
17
  /**
22
18
  * @see {@link https://workleap.github.io/wl-telemetry}
23
19
  */
24
20
  setGlobalEventProperties: (values: Record<string, unknown>) => void;
25
21
  }
26
22
 
23
+ /**
24
+ * @see {@link https://workleap.github.io/wl-telemetry}
25
+ */
26
+ export interface MixpanelClientOptions {
27
+ /**
28
+ * @see {@link https://workleap.github.io/wl-telemetry}
29
+ */
30
+ productId?: string;
31
+ }
32
+
27
33
  export class MixpanelClientImpl implements MixpanelClient {
28
- readonly #productId: string;
34
+ readonly #globalEventProperties = new Map<string, unknown>();
29
35
  readonly #endpoint: string;
30
- readonly #globalEventProperties: MixpanelGlobalEventProperties;
31
36
  readonly #logger: Logger;
37
+ readonly #productId?: string;
38
+
39
+ constructor(endpoint: string, logger: Logger, options: MixpanelClientOptions = {}) {
40
+ const {
41
+ productId
42
+ } = options;
32
43
 
33
- constructor(productId: string, endpoint: string, globalEventProperties: MixpanelGlobalEventProperties, logger: Logger) {
34
- this.#productId = productId;
35
44
  this.#endpoint = endpoint;
36
- this.#globalEventProperties = globalEventProperties;
37
45
  this.#logger = logger;
46
+ this.#productId = productId;
38
47
  }
39
48
 
40
49
  // IMPORTANT: If you update this method, make sure to update the MixpanelPartialClient
41
50
  // interface as well in @workleap-telemetry/core.
42
51
  createTrackingFunction(options: CreateMixpanelTrackingFunctionOptions = {}) {
43
52
  const {
44
- targetProductId
53
+ productId,
54
+ targetProductId,
55
+ additionalProperties = {}
45
56
  } = options;
46
57
 
47
58
  const trackFunction: MixpanelTrackingFunction = async (eventName, properties, _options = {}) => {
@@ -55,6 +66,7 @@ export class MixpanelClientImpl implements MixpanelClient {
55
66
  const allProperties = {
56
67
  ...baseProperties,
57
68
  ...Object.fromEntries(this.#globalEventProperties),
69
+ ...additionalProperties,
58
70
  ...properties
59
71
  };
60
72
 
@@ -65,7 +77,7 @@ export class MixpanelClientImpl implements MixpanelClient {
65
77
  headers: { "Content-Type": "application/json" },
66
78
  body: JSON.stringify({
67
79
  eventName,
68
- productIdentifier: this.#productId,
80
+ productIdentifier: productId ?? this.#productId,
69
81
  // Not sure why, but it seems important to send "null" if not target product identifier
70
82
  // are provided.
71
83
  targetProductIdentifier: targetProductId ?? null,
package/src/js/index.ts CHANGED
@@ -1,8 +1,7 @@
1
1
  export { createTelemetryContext, type CreateTelemetryContextOptions, type TelemetryContext } from "@workleap-telemetry/core";
2
2
  export type { CreateMixpanelTrackingFunctionOptions, MixpanelEventProperties, MixpanelTrackingFunction, MixpanelTrackingFunctionOptions } from "@workleap-telemetry/core";
3
- export { createTrackingFunction } from "./createTrackingFunction.ts";
4
3
  export type { MixpanelEnvironment } from "./env.ts";
5
4
  export { initializeMixpanel, type InitializeMixpanelOptions } from "./initializeMixpanel.ts";
6
- export type { MixpanelClient, MixpanelGlobalEventProperties } from "./MixpanelClient.ts";
5
+ export type { MixpanelClient } from "./MixpanelClient.ts";
7
6
  export { NoopMixpanelClient } from "./NoopMixpanelClient.ts";
8
7
 
@@ -1,33 +1,9 @@
1
1
  import { HasExecutedGuard, type LogRocketInstrumentationPartialClient, type TelemetryContext } from "@workleap-telemetry/core";
2
- import { createCompositeLogger, type Logger, type RootLogger } from "@workleap/logging";
3
- import { setMixpanelContext } from "./context.ts";
2
+ import { createCompositeLogger, type RootLogger } from "@workleap/logging";
4
3
  import { getTrackingEndpoint, type MixpanelEnvironment } from "./env.ts";
5
- import { type MixpanelClient, MixpanelClientImpl, type MixpanelGlobalEventProperties } from "./MixpanelClient.ts";
4
+ import { MixpanelClientImpl, type MixpanelClient } from "./MixpanelClient.ts";
6
5
  import { getTelemetryProperties, OtherProperties } from "./properties.ts";
7
6
 
8
- // DEPRECATED: Grace period ends on January 1th 2026.
9
- // Don't forget to remove the tests as well.
10
- export const IsInitializedVariableName = "__WLP_MIXPANEL_IS_INITIALIZED__";
11
-
12
- // DEPRECATED: Grace period ends on January 1th 2026.
13
- function registerDeprecatedContextAndGlobalVariables(productId: string, endpoint: string, globalEventProperties: Map<string, unknown>, logger: Logger) {
14
- setMixpanelContext({
15
- productId,
16
- endpoint,
17
- globalEventProperties,
18
- logger
19
- });
20
-
21
- // Indicates to the host applications that Mixpanel has been initialized.
22
- // It's useful in cases where an "add-on", like the platform widgets needs
23
- // to know whether or not the host application is using Mixpanel.
24
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
25
- // @ts-ignore
26
- globalThis[IsInitializedVariableName] = true;
27
- }
28
-
29
- ///////////////////////////
30
-
31
7
  let registrationGuardInstance: HasExecutedGuard | undefined;
32
8
 
33
9
  // It's important to use a lazy singleton instead of a singleton to avoid
@@ -53,6 +29,11 @@ export function __resetRegistrationGuard() {
53
29
  * @see {@link https://workleap.github.io/wl-telemetry}
54
30
  */
55
31
  export interface InitializeMixpanelOptions {
32
+ /**
33
+ * An optional product id that will be attached to every event.
34
+ * @see {@link https://workleap.github.io/wl-telemetry}
35
+ */
36
+ productId?: string;
56
37
  /**
57
38
  * An optional tracking endpoint.
58
39
  * @default "tracking/track"
@@ -83,18 +64,9 @@ export interface InitializeMixpanelOptions {
83
64
  }
84
65
 
85
66
  export class MixpanelInitializer {
86
- readonly #globalEventProperties: MixpanelGlobalEventProperties;
87
-
88
- constructor(globalEventProperties: MixpanelGlobalEventProperties) {
89
- this.#globalEventProperties = globalEventProperties;
90
- }
91
-
92
- initialize(
93
- productId: string,
94
- envOrTrackingApiBaseUrl: MixpanelEnvironment | (string & {}),
95
- options: InitializeMixpanelOptions = {}
96
- ) {
67
+ initialize(envOrTrackingApiBaseUrl: MixpanelEnvironment | (string & {}), options: Omit<InitializeMixpanelOptions, "globalEventProperties"> = {}) {
97
68
  const {
69
+ productId,
98
70
  trackingEndpoint,
99
71
  telemetryContext,
100
72
  logRocketInstrumentationClient,
@@ -105,10 +77,16 @@ export class MixpanelInitializer {
105
77
  const logger = createCompositeLogger(verbose, loggers);
106
78
  const endpoint = getTrackingEndpoint(envOrTrackingApiBaseUrl, trackingEndpoint);
107
79
 
80
+ logger.information("[mixpanel] Mixpanel is initialized.");
81
+
82
+ const client = new MixpanelClientImpl(endpoint, logger, {
83
+ productId
84
+ });
85
+
108
86
  // Set the telemetry correlation ids on every Mixpanel event.
109
87
  if (telemetryContext) {
110
88
  for (const [key, value] of Object.entries(getTelemetryProperties(telemetryContext))) {
111
- this.#globalEventProperties.set(key, value);
89
+ client.setGlobalEventProperty(key, value);
112
90
  }
113
91
  }
114
92
 
@@ -121,16 +99,10 @@ export class MixpanelInitializer {
121
99
  .withText(sessionUrl)
122
100
  .debug();
123
101
 
124
- this.#globalEventProperties.set(OtherProperties.LogRocketSessionUrl, sessionUrl);
102
+ client.setGlobalEventProperty(OtherProperties.LogRocketSessionUrl, sessionUrl);
125
103
  });
126
104
  }
127
105
 
128
- registerDeprecatedContextAndGlobalVariables(productId, endpoint, this.#globalEventProperties, logger);
129
-
130
- logger.information("[mixpanel] Mixpanel is initialized.");
131
-
132
- const client = new MixpanelClientImpl(productId, endpoint, this.#globalEventProperties, logger);
133
-
134
106
  // The cast is to normalize the inferred return type to the "LogRocketInstrumentationClient" type.
135
107
  return client as MixpanelClient;
136
108
  }
@@ -138,23 +110,18 @@ export class MixpanelInitializer {
138
110
 
139
111
  /**
140
112
  * Initialize Mixpanel telemetry.
141
- * @param productId The Mixpanel product identifier.
142
113
  * @param envOrTrackingApiBaseUrl The environment to get the navigation url from or a base URL.
143
114
  * @param options Mixpanel instrumentation options.
144
115
  * @returns {MixpanelClient} A Mixpanel client instance.
145
116
  * @see {@link https://workleap.github.io/wl-telemetry}
146
117
  */
147
118
  export function initializeMixpanel(
148
- productId: string,
149
119
  envOrTrackingApiBaseUrl: MixpanelEnvironment | (string & {}),
150
120
  options?: InitializeMixpanelOptions
151
121
  ) {
152
122
  getRegistrationGuard().throw("[mixpanel] Mixpanel has already been initialized. Did you call the \"initializeMixpanel\" function twice?");
153
123
 
154
- return new MixpanelInitializer(
155
- new Map<string, unknown>()
156
- ).initialize(
157
- productId,
124
+ return new MixpanelInitializer().initialize(
158
125
  envOrTrackingApiBaseUrl,
159
126
  options
160
127
  );
@@ -0,0 +1,43 @@
1
+ import { createContext, type PropsWithChildren, useContext, useMemo } from "react";
2
+
3
+ const MixpanelPropertiesContext = createContext<Record<string, unknown>>({});
4
+
5
+ /**
6
+ * @see {@link https://workleap.github.io/wl-telemetry}
7
+ */
8
+ export interface MixpanelPropertiesProviderProps extends PropsWithChildren {
9
+ value: Record<string, unknown>;
10
+ }
11
+
12
+ /**
13
+ * Define Mixpanel properties that will be passed down to nested "useTrackingFunction" or "useMixpanelTrackingFunction" hook.
14
+ * @see {@link https://workleap.github.io/wl-telemetry}
15
+ */
16
+ export function MixpanelPropertiesProvider(props: MixpanelPropertiesProviderProps) {
17
+ const {
18
+ value,
19
+ children
20
+ } = props;
21
+
22
+ const parentContext = useContext(MixpanelPropertiesContext);
23
+
24
+ const contextValue = useMemo(() => ({
25
+ ...parentContext,
26
+ ...value
27
+ }), [parentContext, value]);
28
+
29
+ return (
30
+ <MixpanelPropertiesContext.Provider value={contextValue}>
31
+ {children}
32
+ </MixpanelPropertiesContext.Provider>
33
+ );
34
+ }
35
+
36
+ /**
37
+ * Retrieve the Mixpanel properties from all parent providers.
38
+ * @returns The context value or an empty object literal if no provider isdefined.
39
+ * @see {@link https://workleap.github.io/wl-telemetry}
40
+ */
41
+ export const useMixpanelProviderProperties = () => {
42
+ return useContext(MixpanelPropertiesContext);
43
+ };
@@ -1,4 +1,5 @@
1
1
  export * from "../js/index.ts";
2
2
  export { MixpanelProvider, useMixpanelClient, type UseMixpanelClientOptions } from "./MixpanelContext.tsx";
3
+ export { MixpanelPropertiesProvider, useMixpanelProviderProperties, type MixpanelPropertiesProviderProps } from "./MixpanelPropertiesProvider.tsx";
3
4
  export { useTrackingFunction, type UseTrackingFunctionOptions } from "./useTrackingFunction.ts";
4
5
 
@@ -1,7 +1,7 @@
1
1
  import type { CreateMixpanelTrackingFunctionOptions } from "@workleap-telemetry/core";
2
- import { useContext, useMemo } from "react";
3
- import { createTrackingFunction } from "../js/createTrackingFunction.ts";
4
- import { MixpanelContext } from "./MixpanelContext.tsx";
2
+ import { useMemo } from "react";
3
+ import { useMixpanelClient } from "./MixpanelContext.tsx";
4
+ import { useMixpanelProviderProperties } from "./MixpanelPropertiesProvider.tsx";
5
5
 
6
6
  /**
7
7
  * @see {@link https://workleap.github.io/wl-telemetry}
@@ -14,21 +14,20 @@ export type UseTrackingFunctionOptions = CreateMixpanelTrackingFunctionOptions;
14
14
  */
15
15
  export function useTrackingFunction(options: UseTrackingFunctionOptions = {}) {
16
16
  const {
17
+ productId,
17
18
  targetProductId
18
19
  } = options;
19
20
 
20
- const client = useContext(MixpanelContext);
21
+ const client = useMixpanelClient();
22
+ const additionalProperties = useMixpanelProviderProperties();
21
23
 
22
24
  return useMemo(() => {
23
25
  const trackOptions = {
24
- targetProductId
26
+ productId,
27
+ targetProductId,
28
+ additionalProperties
25
29
  } satisfies CreateMixpanelTrackingFunctionOptions;
26
30
 
27
- // DEPRECATED: Grace period ends on January 1th 2026.
28
- // After the grace period, only create the tracking function from the client, remove the fallback
29
- // and also use "useMixpanelClient" to retrieve the client rather than "useContext".
30
- return client
31
- ? client.createTrackingFunction(trackOptions)
32
- : createTrackingFunction(trackOptions);
33
- }, [client, targetProductId]);
31
+ return client.createTrackingFunction(trackOptions);
32
+ }, [client, productId, targetProductId, additionalProperties]);
34
33
  }
@@ -1,11 +0,0 @@
1
- import type { Logger } from "@workleap/logging";
2
- import type { MixpanelGlobalEventProperties } from "./MixpanelClient.ts";
3
- export declare const MixpanelContextVariableName = "__WLP_MIXPANEL_CONTEXT__";
4
- export interface MixpanelContext {
5
- productId: string;
6
- endpoint: string;
7
- globalEventProperties: MixpanelGlobalEventProperties;
8
- logger: Logger;
9
- }
10
- export declare function setMixpanelContext(context: MixpanelContext): void;
11
- export declare function getMixpanelContext(): MixpanelContext;
@@ -1,28 +0,0 @@
1
-
2
- ;// CONCATENATED MODULE: ./src/js/context.ts
3
- // DEPRECATED: Grace period ends on January 1th 2026.
4
- // Must keep this code until the end of the grace period because host applications using version 2.* of
5
- // this package would get runtime errors from the Platform Widgets CDN otherwise.
6
- const MixpanelContextVariableName = "__WLP_MIXPANEL_CONTEXT__";
7
- function setMixpanelContext(context) {
8
- // Will be used by the "track" function.
9
- // It's important to share the context with a global DOM object rather than
10
- // a singleton because projects like the platform widgets will use a distinct instance
11
- // of this package than the host application.
12
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
13
- // @ts-ignore
14
- globalThis[MixpanelContextVariableName] = context;
15
- }
16
- function getMixpanelContext() {
17
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
18
- // @ts-ignore
19
- const context = globalThis[MixpanelContextVariableName];
20
- if (!context) {
21
- throw new Error("[mixpanel] The Mixpanel context is not available. Did you initialize Mixpanel with the \"initializeMixpanel\" function?");
22
- }
23
- return context;
24
- }
25
-
26
- export { MixpanelContextVariableName, getMixpanelContext, setMixpanelContext };
27
-
28
- //# sourceMappingURL=context.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"js/context.js","sources":["../../src/js/context.ts"],"sourcesContent":["// DEPRECATED: Grace period ends on January 1th 2026.\n// Must keep this code until the end of the grace period because host applications using version 2.* of\n// this package would get runtime errors from the Platform Widgets CDN otherwise.\n\nimport type { Logger } from \"@workleap/logging\";\nimport type { MixpanelGlobalEventProperties } from \"./MixpanelClient.ts\";\n\nexport const MixpanelContextVariableName = \"__WLP_MIXPANEL_CONTEXT__\";\n\nexport interface MixpanelContext {\n productId: string;\n endpoint: string;\n globalEventProperties: MixpanelGlobalEventProperties;\n logger: Logger;\n}\n\nexport function setMixpanelContext(context: MixpanelContext) {\n // Will be used by the \"track\" function.\n // It's important to share the context with a global DOM object rather than\n // a singleton because projects like the platform widgets will use a distinct instance\n // of this package than the host application.\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n globalThis[MixpanelContextVariableName] = context;\n}\n\nexport function getMixpanelContext() {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n const context = globalThis[MixpanelContextVariableName] as MixpanelContext;\n\n if (!context) {\n throw new Error(\"[mixpanel] The Mixpanel context is not available. Did you initialize Mixpanel with the \\\"initializeMixpanel\\\" function?\");\n }\n\n return context;\n}\n\n"],"names":["MixpanelContextVariableName","setMixpanelContext","context","globalThis","getMixpanelContext","Error"],"mappings":";;AAAA,qDAAqD;AACrD,uGAAuG;AACvG,iFAAiF;AAK1E,MAAMA,8BAA8B,2BAA2B;AAS/D,SAASC,mBAAmBC,OAAwB;IACvD,wCAAwC;IACxC,2EAA2E;IAC3E,sFAAsF;IACtF,6CAA6C;IAC7C,6DAA6D;IAC7D,aAAa;IACbC,UAAU,CAACH,4BAA4B,GAAGE;AAC9C;AAEO,SAASE;IACZ,6DAA6D;IAC7D,aAAa;IACb,MAAMF,UAAUC,UAAU,CAACH,4BAA4B;IAEvD,IAAI,CAACE,SAAS;QACV,MAAM,IAAIG,MAAM;IACpB;IAEA,OAAOH;AACX"}
@@ -1,5 +0,0 @@
1
- import type { CreateMixpanelTrackingFunctionOptions, MixpanelTrackingFunction } from "@workleap-telemetry/core";
2
- /**
3
- * @see {@link https://workleap.github.io/wl-telemetry}
4
- */
5
- export declare function createTrackingFunction(options?: CreateMixpanelTrackingFunctionOptions): MixpanelTrackingFunction;
@@ -1,53 +0,0 @@
1
- import { getMixpanelContext } from "./context.js";
2
- import { getBaseProperties } from "./properties.js";
3
-
4
- ;// CONCATENATED MODULE: external "./context.js"
5
-
6
- ;// CONCATENATED MODULE: external "./properties.js"
7
-
8
- ;// CONCATENATED MODULE: ./src/js/createTrackingFunction.ts
9
- // DEPRECATED: Grace period ends on January 1th 2026.
10
- // Must keep this code until the end of the grace period because host applications using version 2.* of
11
- // this package would get runtime errors from the Platform Widgets CDN otherwise.
12
-
13
-
14
- /**
15
- * @see {@link https://workleap.github.io/wl-telemetry}
16
- */ function createTrackingFunction(options = {}) {
17
- const { targetProductId } = options;
18
- const { productId, endpoint, globalEventProperties, logger } = getMixpanelContext();
19
- const trackFunction = async (eventName, properties, _options = {})=>{
20
- try {
21
- const { keepAlive = false } = _options;
22
- const baseProperties = getBaseProperties();
23
- const allProperties = {
24
- ...baseProperties,
25
- ...Object.fromEntries(globalEventProperties),
26
- ...properties
27
- };
28
- await fetch(endpoint, {
29
- method: "POST",
30
- credentials: "include",
31
- keepalive: keepAlive,
32
- headers: {
33
- "Content-Type": "application/json"
34
- },
35
- body: JSON.stringify({
36
- eventName,
37
- productIdentifier: productId,
38
- // Not sure why, but it seems important to send "null" if not target product identifier
39
- // are provided.
40
- targetProductIdentifier: targetProductId ?? null,
41
- properties: allProperties
42
- })
43
- });
44
- } catch (error) {
45
- logger.withText(`[mixpanel] An error occurred while sending a tracking event to "${endpoint}":`).withError(error).error();
46
- }
47
- };
48
- return trackFunction;
49
- }
50
-
51
- export { createTrackingFunction };
52
-
53
- //# sourceMappingURL=createTrackingFunction.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"js/createTrackingFunction.js","sources":["../../src/js/createTrackingFunction.ts"],"sourcesContent":["// DEPRECATED: Grace period ends on January 1th 2026.\n// Must keep this code until the end of the grace period because host applications using version 2.* of\n// this package would get runtime errors from the Platform Widgets CDN otherwise.\n\nimport type { CreateMixpanelTrackingFunctionOptions, MixpanelTrackingFunction } from \"@workleap-telemetry/core\";\nimport { getMixpanelContext } from \"./context.ts\";\nimport { getBaseProperties } from \"./properties.ts\";\n\n/**\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\nexport function createTrackingFunction(options: CreateMixpanelTrackingFunctionOptions = {}) {\n const {\n targetProductId\n } = options;\n\n const {\n productId,\n endpoint,\n globalEventProperties,\n logger\n } = getMixpanelContext();\n\n const trackFunction: MixpanelTrackingFunction = async (eventName, properties, _options = {}) => {\n try {\n const {\n keepAlive = false\n } = _options;\n\n const baseProperties = getBaseProperties();\n\n const allProperties = {\n ...baseProperties,\n ...Object.fromEntries(globalEventProperties),\n ...properties\n };\n\n await fetch(endpoint, {\n method: \"POST\",\n credentials: \"include\",\n keepalive: keepAlive,\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify({\n eventName,\n productIdentifier: productId,\n // Not sure why, but it seems important to send \"null\" if not target product identifier\n // are provided.\n targetProductIdentifier: targetProductId ?? null,\n properties: allProperties\n })\n });\n } catch (error: unknown) {\n logger\n .withText(`[mixpanel] An error occurred while sending a tracking event to \"${endpoint}\":`)\n .withError(error as Error)\n .error();\n }\n };\n\n return trackFunction;\n}\n"],"names":["getMixpanelContext","getBaseProperties","createTrackingFunction","options","targetProductId","productId","endpoint","globalEventProperties","logger","trackFunction","eventName","properties","_options","keepAlive","baseProperties","allProperties","Object","fetch","JSON","error"],"mappings":";;;;;;;;AAAA,qDAAqD;AACrD,uGAAuG;AACvG,iFAAiF;AAG/B;AACE;AAEpD;;CAEC,GACM,SAASE,uBAAuBC,UAAiD,CAAC,CAAC;IACtF,MAAM,EACFC,eAAe,EAClB,GAAGD;IAEJ,MAAM,EACFE,SAAS,EACTC,QAAQ,EACRC,qBAAqB,EACrBC,MAAM,EACT,GAAGR,kBAAkBA;IAEtB,MAAMS,gBAA0C,OAAOC,WAAWC,YAAYC,WAAW,CAAC,CAAC;QACvF,IAAI;YACA,MAAM,EACFC,YAAY,KAAK,EACpB,GAAGD;YAEJ,MAAME,iBAAiBb,iBAAiBA;YAExC,MAAMc,gBAAgB;gBAClB,GAAGD,cAAc;gBACjB,GAAGE,OAAO,WAAW,CAACT,sBAAsB;gBAC5C,GAAGI,UAAU;YACjB;YAEA,MAAMM,MAAMX,UAAU;gBAClB,QAAQ;gBACR,aAAa;gBACb,WAAWO;gBACX,SAAS;oBAAE,gBAAgB;gBAAmB;gBAC9C,MAAMK,KAAK,SAAS,CAAC;oBACjBR;oBACA,mBAAmBL;oBACnB,uFAAuF;oBACvF,gBAAgB;oBAChB,yBAAyBD,mBAAmB;oBAC5C,YAAYW;gBAChB;YACJ;QACJ,EAAE,OAAOI,OAAgB;YACrBX,OACK,QAAQ,CAAC,CAAC,gEAAgE,EAAEF,SAAS,EAAE,CAAC,EACxF,SAAS,CAACa,OACV,KAAK;QACd;IACJ;IAEA,OAAOV;AACX"}
package/src/js/context.ts DELETED
@@ -1,38 +0,0 @@
1
- // DEPRECATED: Grace period ends on January 1th 2026.
2
- // Must keep this code until the end of the grace period because host applications using version 2.* of
3
- // this package would get runtime errors from the Platform Widgets CDN otherwise.
4
-
5
- import type { Logger } from "@workleap/logging";
6
- import type { MixpanelGlobalEventProperties } from "./MixpanelClient.ts";
7
-
8
- export const MixpanelContextVariableName = "__WLP_MIXPANEL_CONTEXT__";
9
-
10
- export interface MixpanelContext {
11
- productId: string;
12
- endpoint: string;
13
- globalEventProperties: MixpanelGlobalEventProperties;
14
- logger: Logger;
15
- }
16
-
17
- export function setMixpanelContext(context: MixpanelContext) {
18
- // Will be used by the "track" function.
19
- // It's important to share the context with a global DOM object rather than
20
- // a singleton because projects like the platform widgets will use a distinct instance
21
- // of this package than the host application.
22
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
23
- // @ts-ignore
24
- globalThis[MixpanelContextVariableName] = context;
25
- }
26
-
27
- export function getMixpanelContext() {
28
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
29
- // @ts-ignore
30
- const context = globalThis[MixpanelContextVariableName] as MixpanelContext;
31
-
32
- if (!context) {
33
- throw new Error("[mixpanel] The Mixpanel context is not available. Did you initialize Mixpanel with the \"initializeMixpanel\" function?");
34
- }
35
-
36
- return context;
37
- }
38
-
@@ -1,61 +0,0 @@
1
- // DEPRECATED: Grace period ends on January 1th 2026.
2
- // Must keep this code until the end of the grace period because host applications using version 2.* of
3
- // this package would get runtime errors from the Platform Widgets CDN otherwise.
4
-
5
- import type { CreateMixpanelTrackingFunctionOptions, MixpanelTrackingFunction } from "@workleap-telemetry/core";
6
- import { getMixpanelContext } from "./context.ts";
7
- import { getBaseProperties } from "./properties.ts";
8
-
9
- /**
10
- * @see {@link https://workleap.github.io/wl-telemetry}
11
- */
12
- export function createTrackingFunction(options: CreateMixpanelTrackingFunctionOptions = {}) {
13
- const {
14
- targetProductId
15
- } = options;
16
-
17
- const {
18
- productId,
19
- endpoint,
20
- globalEventProperties,
21
- logger
22
- } = getMixpanelContext();
23
-
24
- const trackFunction: MixpanelTrackingFunction = async (eventName, properties, _options = {}) => {
25
- try {
26
- const {
27
- keepAlive = false
28
- } = _options;
29
-
30
- const baseProperties = getBaseProperties();
31
-
32
- const allProperties = {
33
- ...baseProperties,
34
- ...Object.fromEntries(globalEventProperties),
35
- ...properties
36
- };
37
-
38
- await fetch(endpoint, {
39
- method: "POST",
40
- credentials: "include",
41
- keepalive: keepAlive,
42
- headers: { "Content-Type": "application/json" },
43
- body: JSON.stringify({
44
- eventName,
45
- productIdentifier: productId,
46
- // Not sure why, but it seems important to send "null" if not target product identifier
47
- // are provided.
48
- targetProductIdentifier: targetProductId ?? null,
49
- properties: allProperties
50
- })
51
- });
52
- } catch (error: unknown) {
53
- logger
54
- .withText(`[mixpanel] An error occurred while sending a tracking event to "${endpoint}":`)
55
- .withError(error as Error)
56
- .error();
57
- }
58
- };
59
-
60
- return trackFunction;
61
- }