@workleap/mixpanel 2.0.0 → 2.0.2

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.
Files changed (42) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/js/context.d.ts +10 -0
  3. package/dist/js/context.js +25 -0
  4. package/dist/js/context.js.map +1 -0
  5. package/dist/js/createTrackingFunction.d.ts +35 -0
  6. package/dist/js/createTrackingFunction.js +49 -0
  7. package/dist/js/createTrackingFunction.js.map +1 -0
  8. package/dist/js/env.d.ts +1 -1
  9. package/dist/js/env.js +2 -2
  10. package/dist/js/env.js.map +1 -1
  11. package/dist/js/index.d.ts +3 -3
  12. package/dist/js/index.js +7 -3
  13. package/dist/js/index.js.map +1 -1
  14. package/dist/js/initializeMixpanel.d.ts +6 -29
  15. package/dist/js/initializeMixpanel.js +20 -46
  16. package/dist/js/initializeMixpanel.js.map +1 -1
  17. package/dist/js/properties.d.ts +18 -6
  18. package/dist/js/properties.js +32 -6
  19. package/dist/js/properties.js.map +1 -1
  20. package/dist/react/index.d.ts +1 -1
  21. package/dist/react/index.js +3 -3
  22. package/dist/react/index.js.map +1 -1
  23. package/dist/react/useTrackingFunction.d.ts +3 -0
  24. package/dist/react/useTrackingFunction.js +24 -0
  25. package/dist/react/useTrackingFunction.js.map +1 -0
  26. package/package.json +1 -1
  27. package/src/js/context.ts +34 -0
  28. package/src/js/createTrackingFunction.ts +86 -0
  29. package/src/js/env.ts +2 -2
  30. package/src/js/index.ts +3 -3
  31. package/src/js/initializeMixpanel.ts +25 -81
  32. package/src/js/properties.ts +46 -8
  33. package/src/react/index.ts +1 -1
  34. package/src/react/useTrackingFunction.ts +14 -0
  35. package/dist/js/getMixpanelTrackingFunction.d.ts +0 -14
  36. package/dist/js/getMixpanelTrackingFunction.js +0 -21
  37. package/dist/js/getMixpanelTrackingFunction.js.map +0 -1
  38. package/dist/react/useMixpanelTracking.d.ts +0 -11
  39. package/dist/react/useMixpanelTracking.js +0 -17
  40. package/dist/react/useMixpanelTracking.js.map +0 -1
  41. package/src/js/getMixpanelTrackingFunction.ts +0 -31
  42. package/src/react/useMixpanelTracking.ts +0 -15
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @workleap/mixpanel
2
2
 
3
+ ## 2.0.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#50](https://github.com/workleap/wl-telemetry/pull/50) [`e53857a`](https://github.com/workleap/wl-telemetry/commit/e53857a6c1ba13894f643076f3675dc2b61a9cc5) Thanks [@alexasselin008](https://github.com/alexasselin008)! - Add the trackingEndpoint options to the initializeMixpanel function to allow specifying a custom endpoint for tracking events.
8
+
9
+ ## 2.0.1
10
+
11
+ ### Patch Changes
12
+
13
+ - [#36](https://github.com/workleap/wl-telemetry/pull/36) [`85f432b`](https://github.com/workleap/wl-telemetry/commit/85f432bb1c45433d24a765da7249fef4abb949d4) Thanks [@patricklafrance](https://github.com/patricklafrance)! - Minor fixes to the API.
14
+
3
15
  ## 2.0.0
4
16
 
5
17
  ### Major Changes
@@ -0,0 +1,10 @@
1
+ import type { SuperProperties } from "./properties.ts";
2
+ export declare const ContextVariableName = "__WLP_MIXPANEL_CONTEXT__";
3
+ export interface MixpanelContext {
4
+ productId: string;
5
+ endpoint: string;
6
+ superProperties: SuperProperties;
7
+ verbose: boolean;
8
+ }
9
+ export declare function setMixpanelContext(context: MixpanelContext): void;
10
+ export declare function getMixpanelContext(): MixpanelContext;
@@ -0,0 +1,25 @@
1
+
2
+ ;// CONCATENATED MODULE: ./src/js/context.ts
3
+ const ContextVariableName = "__WLP_MIXPANEL_CONTEXT__";
4
+ function setMixpanelContext(context) {
5
+ // Will be used by the "track" function.
6
+ // It's important to share the context with a global DOM object rather than
7
+ // a singleton because projects like the platform widgets will use a distinct instance
8
+ // of this package than the host application.
9
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
10
+ // @ts-ignore
11
+ globalThis[ContextVariableName] = context;
12
+ }
13
+ function getMixpanelContext() {
14
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
15
+ // @ts-ignore
16
+ const context = globalThis[ContextVariableName];
17
+ if (!context) {
18
+ throw new Error("[mixpanel] The Mixpanel context is not available. Did you initialize Mixpanel with the \"initializeMixpanel\" function?");
19
+ }
20
+ return context;
21
+ }
22
+
23
+ export { ContextVariableName, getMixpanelContext, setMixpanelContext };
24
+
25
+ //# sourceMappingURL=context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"js/context.js","sources":["webpack://@workleap/mixpanel/./src/js/context.ts"],"sourcesContent":["import type { SuperProperties } from \"./properties.ts\";\n\nexport const ContextVariableName = \"__WLP_MIXPANEL_CONTEXT__\";\n\nexport interface MixpanelContext {\n productId: string;\n endpoint: string;\n superProperties: SuperProperties;\n verbose: boolean;\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[ContextVariableName] = context;\n}\n\nexport function getMixpanelContext() {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n const context = globalThis[ContextVariableName] 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\n"],"names":["ContextVariableName","setMixpanelContext","context","globalThis","getMixpanelContext","Error"],"mappings":";;AAEO,MAAMA,sBAAsB,2BAA2B;AASvD,SAASC,mBAAmBC,OAAwB;IACvD,wCAAwC;IACxC,2EAA2E;IAC3E,sFAAsF;IACtF,6CAA6C;IAC7C,6DAA6D;IAC7D,aAAa;IACbC,UAAU,CAACH,oBAAoB,GAAGE;AACtC;AAEO,SAASE;IACZ,6DAA6D;IAC7D,aAAa;IACb,MAAMF,UAAUC,UAAU,CAACH,oBAAoB;IAE/C,IAAI,CAACE,SAAS;QACV,MAAM,IAAIG,MAAM;IACpB;IAEA,OAAOH;AACX"}
@@ -0,0 +1,35 @@
1
+ import { type MixpanelEventProperties } from "./properties.ts";
2
+ /**
3
+ * @see https://workleap.github.io/wl-telemetry
4
+ */
5
+ export interface CreateTrackingFunctionOptions {
6
+ /**
7
+ * The product identifier of the target product.
8
+ */
9
+ targetProductId?: string;
10
+ }
11
+ /**
12
+ * @see https://workleap.github.io/wl-telemetry
13
+ */
14
+ export interface TrackingFunctionOptions {
15
+ /**
16
+ * Whether to keep the connection alive for the tracking request.
17
+ * It is mostly used for tracking links where the user might navigate away before the request is completed.
18
+ *
19
+ * Caution! The body size for keepalive requests is limited to 64 kibibytes.
20
+ *
21
+ * https://developer.mozilla.org/en-US/docs/Web/API/RequestInit#keepalive
22
+ *
23
+ * @default false
24
+ */
25
+ keepAlive?: boolean;
26
+ }
27
+ /**
28
+ * A function that sends tracking events to the tracking API.
29
+ * @param eventName The name of the event to track.
30
+ * @param properties The properties to send with the event.
31
+ * @param options Options for tracking the event.
32
+ * @see https://workleap.github.io/wl-telemetry
33
+ */
34
+ export type TrackingFunction = (eventName: string, properties: MixpanelEventProperties, options?: TrackingFunctionOptions) => Promise<void>;
35
+ export declare function createTrackingFunction(options?: CreateTrackingFunctionOptions): TrackingFunction;
@@ -0,0 +1,49 @@
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
+
10
+
11
+ function createTrackingFunction(options = {}) {
12
+ const { targetProductId } = options;
13
+ const { productId, endpoint, superProperties } = getMixpanelContext();
14
+ const trackFunction = async (eventName, properties, _options = {})=>{
15
+ try {
16
+ const { keepAlive = false } = _options;
17
+ const baseProperties = getBaseProperties();
18
+ const allProperties = {
19
+ ...baseProperties,
20
+ ...Object.fromEntries(superProperties),
21
+ ...properties
22
+ };
23
+ await fetch(endpoint, {
24
+ method: "POST",
25
+ credentials: "include",
26
+ keepalive: keepAlive,
27
+ headers: {
28
+ "Content-Type": "application/json"
29
+ },
30
+ body: JSON.stringify({
31
+ eventName,
32
+ productIdentifier: productId,
33
+ // Not sure why, but it seems important to send "null" if not target product identifier
34
+ // are provided.
35
+ targetProductIdentifier: targetProductId ?? null,
36
+ properties: allProperties
37
+ })
38
+ });
39
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
40
+ } catch (error) {
41
+ // Do nothing...
42
+ }
43
+ };
44
+ return trackFunction;
45
+ }
46
+
47
+ export { createTrackingFunction };
48
+
49
+ //# sourceMappingURL=createTrackingFunction.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"js/createTrackingFunction.js","sources":["webpack://@workleap/mixpanel/./src/js/createTrackingFunction.ts"],"sourcesContent":["import { getMixpanelContext } from \"./context.ts\";\nimport { getBaseProperties, type MixpanelEventProperties } from \"./properties.ts\";\n\n/**\n * @see https://workleap.github.io/wl-telemetry\n */\nexport interface CreateTrackingFunctionOptions {\n /**\n * The product identifier of the target product.\n */\n targetProductId?: string;\n}\n\n/**\n * @see https://workleap.github.io/wl-telemetry\n */\nexport interface TrackingFunctionOptions {\n /**\n * Whether to keep the connection alive for the tracking request.\n * It is mostly used for tracking links where the user might navigate away before the request is completed.\n *\n * Caution! The body size for keepalive requests is limited to 64 kibibytes.\n *\n * https://developer.mozilla.org/en-US/docs/Web/API/RequestInit#keepalive\n *\n * @default false\n */\n keepAlive?: boolean;\n}\n\n/**\n * A function that sends tracking events to the tracking API.\n * @param eventName The name of the event to track.\n * @param properties The properties to send with the event.\n * @param options Options for tracking the event.\n * @see https://workleap.github.io/wl-telemetry\n */\nexport type TrackingFunction = (eventName: string, properties: MixpanelEventProperties, options?: TrackingFunctionOptions) => Promise<void>;\n\nexport function createTrackingFunction(options: CreateTrackingFunctionOptions = {}) {\n const {\n targetProductId\n } = options;\n\n const {\n productId,\n endpoint,\n superProperties\n } = getMixpanelContext();\n\n const trackFunction: TrackingFunction = 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(superProperties),\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 // eslint-disable-next-line @typescript-eslint/no-unused-vars\n } catch (error: unknown) {\n // Do nothing...\n }\n };\n\n return trackFunction;\n}\n"],"names":["getMixpanelContext","getBaseProperties","createTrackingFunction","options","targetProductId","productId","endpoint","superProperties","trackFunction","eventName","properties","_options","keepAlive","baseProperties","allProperties","Object","fetch","JSON","error"],"mappings":";;;;;;;;AAAkD;AACgC;AAsC3E,SAASE,uBAAuBC,UAAyC,CAAC,CAAC;IAC9E,MAAM,EACFC,eAAe,EAClB,GAAGD;IAEJ,MAAM,EACFE,SAAS,EACTC,QAAQ,EACRC,eAAe,EAClB,GAAGP,kBAAkBA;IAEtB,MAAMQ,gBAAkC,OAAOC,WAAWC,YAAYC,WAAW,CAAC,CAAC;QAC/E,IAAI;YACA,MAAM,EACFC,YAAY,KAAK,EACpB,GAAGD;YAEJ,MAAME,iBAAiBZ,iBAAiBA;YAExC,MAAMa,gBAAgB;gBAClB,GAAGD,cAAc;gBACjB,GAAGE,OAAO,WAAW,CAACR,gBAAgB;gBACtC,GAAGG,UAAU;YACjB;YAEA,MAAMM,MAAMV,UAAU;gBAClB,QAAQ;gBACR,aAAa;gBACb,WAAWM;gBACX,SAAS;oBAAE,gBAAgB;gBAAmB;gBAC9C,MAAMK,KAAK,SAAS,CAAC;oBACjBR;oBACA,mBAAmBJ;oBACnB,uFAAuF;oBACvF,gBAAgB;oBAChB,yBAA0BD,mBAAmB;oBAC7C,YAAYU;gBAChB;YACJ;QACJ,6DAA6D;QAC7D,EAAE,OAAOI,OAAgB;QACrB,gBAAgB;QACpB;IACJ;IAEA,OAAOV;AACX"}
package/dist/js/env.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  declare const EnvironmentList: readonly ["development", "staging", "production", "local", "msw"];
2
2
  export type Environment = typeof EnvironmentList[number];
3
- export declare function getTrackingEndpoint(envOrTrackingApiBaseUrl: Environment | (string & {})): string;
3
+ export declare function getTrackingEndpoint(envOrTrackingApiBaseUrl: Environment | (string & {}), trackingEndpoint?: string): string;
4
4
  export {};
package/dist/js/env.js CHANGED
@@ -18,14 +18,14 @@ const NavigationApiBaseUrl = {
18
18
  function resolveApiUrl(path, baseUrl) {
19
19
  return `${baseUrl}${baseUrl.endsWith("/") ? "" : "/"}${path.startsWith("/") ? path.substring(1) : path}`;
20
20
  }
21
- function getTrackingEndpoint(envOrTrackingApiBaseUrl) {
21
+ function getTrackingEndpoint(envOrTrackingApiBaseUrl, trackingEndpoint = "tracking/track") {
22
22
  let baseUrl;
23
23
  if (EnvironmentList.includes(envOrTrackingApiBaseUrl)) {
24
24
  baseUrl = NavigationApiBaseUrl[envOrTrackingApiBaseUrl];
25
25
  } else {
26
26
  baseUrl = envOrTrackingApiBaseUrl;
27
27
  }
28
- return resolveApiUrl("tracking/track", baseUrl);
28
+ return resolveApiUrl(trackingEndpoint, baseUrl);
29
29
  }
30
30
 
31
31
  export { getTrackingEndpoint };
@@ -1 +1 @@
1
- {"version":3,"file":"js/env.js","sources":["webpack://@workleap/mixpanel/./src/js/env.ts"],"sourcesContent":["\nconst EnvironmentList = [\"development\", \"staging\", \"production\", \"local\", \"msw\"] as const;\n\nexport type Environment = typeof EnvironmentList[number];\n\nconst NavigationApiBaseUrl: Record<Environment, string> = {\n \"production\": \"https://api.platform.workleap.com/shell/navigation/\",\n \"staging\": \"https://api.platform.workleap-stg.com/shell/navigation/\",\n \"development\": \"https://api.platform.workleap-dev.com/shell/navigation/\",\n \"local\": \"https://api.platform.workleap-dev.com/shell/navigation/\",\n \"msw\": \"/api/shell/navigation/\"\n};\n\n// Cannot use URL() because it doesn't support relative base url: https://github.com/whatwg/url/issues/531.\nfunction resolveApiUrl(path: string, baseUrl: string | undefined) : string {\n return `${baseUrl}${baseUrl!.endsWith(\"/\") ? \"\" : \"/\"}${path.startsWith(\"/\") ? path.substring(1) : path}`;\n}\n\nexport function getTrackingEndpoint(envOrTrackingApiBaseUrl: Environment | (string & {})) {\n let baseUrl: string;\n\n if (EnvironmentList.includes(envOrTrackingApiBaseUrl as Environment)) {\n baseUrl = NavigationApiBaseUrl[envOrTrackingApiBaseUrl as Environment];\n } else {\n baseUrl = envOrTrackingApiBaseUrl;\n }\n\n return resolveApiUrl(\"tracking/track\", baseUrl);\n}\n"],"names":["EnvironmentList","NavigationApiBaseUrl","resolveApiUrl","path","baseUrl","getTrackingEndpoint","envOrTrackingApiBaseUrl"],"mappings":";;AACA,MAAMA,kBAAkB;IAAC;IAAe;IAAW;IAAc;IAAS;CAAM;AAIhF,MAAMC,uBAAoD;IACtD,cAAc;IACd,WAAW;IACX,eAAe;IACf,SAAS;IACT,OAAO;AACX;AAEA,2GAA2G;AAC3G,SAASC,cAAcC,IAAY,EAAEC,OAA2B;IAC5D,OAAO,GAAGA,UAAUA,QAAS,QAAQ,CAAC,OAAO,KAAK,MAAMD,KAAK,UAAU,CAAC,OAAOA,KAAK,SAAS,CAAC,KAAKA,MAAM;AAC7G;AAEO,SAASE,oBAAoBC,uBAAoD;IACpF,IAAIF;IAEJ,IAAIJ,gBAAgB,QAAQ,CAACM,0BAAyC;QAClEF,UAAUH,oBAAoB,CAACK,wBAAuC;IAC1E,OAAO;QACHF,UAAUE;IACd;IAEA,OAAOJ,cAAc,kBAAkBE;AAC3C"}
1
+ {"version":3,"file":"js/env.js","sources":["webpack://@workleap/mixpanel/./src/js/env.ts"],"sourcesContent":["\nconst EnvironmentList = [\"development\", \"staging\", \"production\", \"local\", \"msw\"] as const;\n\nexport type Environment = typeof EnvironmentList[number];\n\nconst NavigationApiBaseUrl: Record<Environment, string> = {\n \"production\": \"https://api.platform.workleap.com/shell/navigation/\",\n \"staging\": \"https://api.platform.workleap-stg.com/shell/navigation/\",\n \"development\": \"https://api.platform.workleap-dev.com/shell/navigation/\",\n \"local\": \"https://api.platform.workleap-dev.com/shell/navigation/\",\n \"msw\": \"/api/shell/navigation/\"\n};\n\n// Cannot use URL() because it doesn't support relative base url: https://github.com/whatwg/url/issues/531.\nfunction resolveApiUrl(path: string, baseUrl: string | undefined) : string {\n return `${baseUrl}${baseUrl!.endsWith(\"/\") ? \"\" : \"/\"}${path.startsWith(\"/\") ? path.substring(1) : path}`;\n}\n\nexport function getTrackingEndpoint(envOrTrackingApiBaseUrl: Environment | (string & {}), trackingEndpoint = \"tracking/track\"): string {\n let baseUrl: string;\n\n if (EnvironmentList.includes(envOrTrackingApiBaseUrl as Environment)) {\n baseUrl = NavigationApiBaseUrl[envOrTrackingApiBaseUrl as Environment];\n } else {\n baseUrl = envOrTrackingApiBaseUrl;\n }\n\n return resolveApiUrl(trackingEndpoint, baseUrl);\n}\n"],"names":["EnvironmentList","NavigationApiBaseUrl","resolveApiUrl","path","baseUrl","getTrackingEndpoint","envOrTrackingApiBaseUrl","trackingEndpoint"],"mappings":";;AACA,MAAMA,kBAAkB;IAAC;IAAe;IAAW;IAAc;IAAS;CAAM;AAIhF,MAAMC,uBAAoD;IACtD,cAAc;IACd,WAAW;IACX,eAAe;IACf,SAAS;IACT,OAAO;AACX;AAEA,2GAA2G;AAC3G,SAASC,cAAcC,IAAY,EAAEC,OAA2B;IAC5D,OAAO,GAAGA,UAAUA,QAAS,QAAQ,CAAC,OAAO,KAAK,MAAMD,KAAK,UAAU,CAAC,OAAOA,KAAK,SAAS,CAAC,KAAKA,MAAM;AAC7G;AAEO,SAASE,oBAAoBC,uBAAoD,EAAEC,mBAAmB,gBAAgB;IACzH,IAAIH;IAEJ,IAAIJ,gBAAgB,QAAQ,CAACM,0BAAyC;QAClEF,UAAUH,oBAAoB,CAACK,wBAAuC;IAC1E,OAAO;QACHF,UAAUE;IACd;IAEA,OAAOJ,cAAcK,kBAAkBH;AAC3C"}
@@ -1,4 +1,4 @@
1
+ export { createTrackingFunction, type CreateTrackingFunctionOptions, type TrackingFunction, type TrackingFunctionOptions } from "./createTrackingFunction.ts";
1
2
  export type { Environment } from "./env.ts";
2
- export { getMixpanelTrackingFunction } from "./getMixpanelTrackingFunction.ts";
3
- export { initializeMixpanel, type InitializeMixpanelOptions, type MixpanelTrackingFunction, type MixpanelTrackingOptions } from "./initializeMixpanel.ts";
4
- export type { MixpanelTrackEventProperties } from "./properties.ts";
3
+ export { initializeMixpanel, type InitializeMixpanelOptions } from "./initializeMixpanel.ts";
4
+ export { setSuperProperties, setSuperProperty, type MixpanelEventProperties } from "./properties.ts";
package/dist/js/index.js CHANGED
@@ -1,14 +1,18 @@
1
- import { getMixpanelTrackingFunction } from "./getMixpanelTrackingFunction.js";
1
+ import { createTrackingFunction } from "./createTrackingFunction.js";
2
2
  import { initializeMixpanel } from "./initializeMixpanel.js";
3
+ import { setSuperProperties, setSuperProperty } from "./properties.js";
3
4
 
4
- ;// CONCATENATED MODULE: external "./getMixpanelTrackingFunction.js"
5
+ ;// CONCATENATED MODULE: external "./createTrackingFunction.js"
5
6
 
6
7
  ;// CONCATENATED MODULE: external "./initializeMixpanel.js"
7
8
 
9
+ ;// CONCATENATED MODULE: external "./properties.js"
10
+
8
11
  ;// CONCATENATED MODULE: ./src/js/index.ts
9
12
 
10
13
 
11
14
 
12
- export { getMixpanelTrackingFunction, initializeMixpanel };
15
+
16
+ export { createTrackingFunction, initializeMixpanel, setSuperProperties, setSuperProperty };
13
17
 
14
18
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"js/index.js","sources":["webpack://@workleap/mixpanel/./src/js/index.ts"],"sourcesContent":["export type { Environment } from \"./env.ts\";\nexport { getMixpanelTrackingFunction } from \"./getMixpanelTrackingFunction.ts\";\nexport { initializeMixpanel, type InitializeMixpanelOptions, type MixpanelTrackingFunction, type MixpanelTrackingOptions } from \"./initializeMixpanel.ts\";\nexport type { MixpanelTrackEventProperties } from \"./properties.ts\";\n\n"],"names":["getMixpanelTrackingFunction","initializeMixpanel"],"mappings":";;;;;;;;AAC+E;AAC2E"}
1
+ {"version":3,"file":"js/index.js","sources":["webpack://@workleap/mixpanel/./src/js/index.ts"],"sourcesContent":["export { createTrackingFunction, type CreateTrackingFunctionOptions, type TrackingFunction, type TrackingFunctionOptions } from \"./createTrackingFunction.ts\";\nexport type { Environment } from \"./env.ts\";\nexport { initializeMixpanel, type InitializeMixpanelOptions } from \"./initializeMixpanel.ts\";\nexport { setSuperProperties, setSuperProperty, type MixpanelEventProperties } from \"./properties.ts\";\n\n"],"names":["createTrackingFunction","initializeMixpanel","setSuperProperties","setSuperProperty"],"mappings":";;;;;;;;;;;AAA8J;AAEjE;AACQ"}
@@ -1,37 +1,14 @@
1
1
  import { type Environment } from "./env.ts";
2
- import { type MixpanelTrackEventProperties } from "./properties.ts";
3
2
  /**
4
3
  * @see https://workleap.github.io/wl-telemetry
5
4
  */
6
- export interface MixpanelTrackingOptions {
5
+ export interface InitializeMixpanelOptions {
7
6
  /**
8
- * The product identifier of the target product.
7
+ * The endpoint to use for tracking events.
8
+ * If not provided, the default endpoint for the environment will be used.
9
+ * @default "tracking/track"
9
10
  */
10
- targetProductId?: string;
11
- /**
12
- * Whether to keep the connection alive for the tracking request.
13
- * It is mostly used for tracking links where the user might navigate away before the request is completed.
14
- *
15
- * Caution! The body size for keepalive requests is limited to 64 kibibytes.
16
- *
17
- * https://developer.mozilla.org/en-US/docs/Web/API/RequestInit#keepalive
18
- *
19
- * @default false
20
- */
21
- keepAlive?: boolean;
22
- }
23
- /**
24
- * A function that sends tracking events to the tracking API.
25
- * @param eventName The name of the event to track.
26
- * @param properties The properties to send with the event.
27
- * @param options Options for tracking the event.
28
- * @see https://workleap.github.io/wl-telemetry
29
- */
30
- export type MixpanelTrackingFunction = (eventName: string, properties: MixpanelTrackEventProperties, options?: MixpanelTrackingOptions) => Promise<void>;
31
- /**
32
- * @see https://workleap.github.io/wl-telemetry
33
- */
34
- export interface InitializeMixpanelOptions {
11
+ trackingEndpoint?: string;
35
12
  verbose?: boolean;
36
13
  }
37
14
  export declare function __resetInitializationGuard(): void;
@@ -43,4 +20,4 @@ export declare function __resetInitializationGuard(): void;
43
20
  * @returns A function that sends tracking events to the tracking API.
44
21
  * @see https://workleap.github.io/wl-telemetry
45
22
  */
46
- export declare function initializeMixpanel(productId: string, envOrTrackingApiBaseUrl: Environment | (string & {}), options?: InitializeMixpanelOptions): MixpanelTrackingFunction;
23
+ export declare function initializeMixpanel(productId: string, envOrTrackingApiBaseUrl: Environment | (string & {}), options?: InitializeMixpanelOptions): void;
@@ -1,14 +1,14 @@
1
1
  import { getBootstrappingStore, getTelemetryContext } from "@workleap/telemetry";
2
+ import { setMixpanelContext } from "./context.js";
2
3
  import { getTrackingEndpoint } from "./env.js";
3
- import { TrackingFunctionName } from "./getMixpanelTrackingFunction.js";
4
4
  import { HasExecutedGuard } from "./HasExecutedGuard.js";
5
- import { TelemetryProperties, getBaseProperties } from "./properties.js";
5
+ import { OtherProperties, getSuperProperties, getTelemetryProperties, setSuperProperties, setSuperProperty } from "./properties.js";
6
6
 
7
7
  ;// CONCATENATED MODULE: external "@workleap/telemetry"
8
8
 
9
- ;// CONCATENATED MODULE: external "./env.js"
9
+ ;// CONCATENATED MODULE: external "./context.js"
10
10
 
11
- ;// CONCATENATED MODULE: external "./getMixpanelTrackingFunction.js"
11
+ ;// CONCATENATED MODULE: external "./env.js"
12
12
 
13
13
  ;// CONCATENATED MODULE: external "./HasExecutedGuard.js"
14
14
 
@@ -20,7 +20,7 @@ import { TelemetryProperties, getBaseProperties } from "./properties.js";
20
20
 
21
21
 
22
22
 
23
- function registerLogRocketSessionUrlListener(superProperties, verbose = false) {
23
+ function registerLogRocketSessionUrlListener(verbose = false) {
24
24
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
25
25
  // @ts-ignore
26
26
  if (globalThis.__WLP_LOGROCKET_INSTRUMENTATION_REGISTER_GET_SESSION_URL_LISTENER__) {
@@ -31,7 +31,7 @@ function registerLogRocketSessionUrlListener(superProperties, verbose = false) {
31
31
  if (verbose) {
32
32
  console.log("[mixpanel] Received LogRocket session replay URL:", sessionUrl);
33
33
  }
34
- superProperties.set(TelemetryProperties.LogRocketSessionUrl, sessionUrl);
34
+ setSuperProperty(OtherProperties.LogRocketSessionUrl, sessionUrl);
35
35
  });
36
36
  } else if (verbose) {
37
37
  console.log("[mixpanel] Cannot integrate with LogRocket because \"globalThis.__WLP_LOGROCKET_INSTRUMENTATION_REGISTER_GET_SESSION_URL_LISTENER__\" is not available.");
@@ -50,61 +50,35 @@ function __resetInitializationGuard() {
50
50
  * @returns A function that sends tracking events to the tracking API.
51
51
  * @see https://workleap.github.io/wl-telemetry
52
52
  */ function initializeMixpanel(productId, envOrTrackingApiBaseUrl, options = {}) {
53
- const { verbose } = options;
53
+ const { verbose = false, trackingEndpoint } = options;
54
54
  initializationGuard.throw("[mixpanel] Mixpanel has already been initialized. Did you call the \"initializeMixpanel\" function twice?");
55
- // Equivalent to: https://docs.mixpanel.com/docs/tracking-methods/sdks/javascript#setting-super-properties.
56
- const superProperties = new Map();
57
- const fullUrl = getTrackingEndpoint(envOrTrackingApiBaseUrl);
55
+ const endpoint = getTrackingEndpoint(envOrTrackingApiBaseUrl, trackingEndpoint);
58
56
  const telemetryContext = getTelemetryContext({
59
57
  verbose
60
58
  });
61
- superProperties.set(TelemetryProperties.TelemetryId, telemetryContext.telemetryId);
62
- superProperties.set(TelemetryProperties.DeviceId, telemetryContext.deviceId);
59
+ setSuperProperties(getTelemetryProperties(telemetryContext));
63
60
  const bootstrappingStore = getBootstrappingStore();
64
61
  // If LogRocket is already available, register the listener. Otherwise, subscribe to the bootstrapping store
65
62
  // and register the listener once a notification is received that LogRocket is registered.
66
63
  if (bootstrappingStore.state.isLogRocketReady) {
67
- registerLogRocketSessionUrlListener(superProperties, verbose);
64
+ registerLogRocketSessionUrlListener(verbose);
68
65
  } else {
69
66
  bootstrappingStore.subscribe((action, store, unsubscribe)=>{
70
67
  if (store.state.isLogRocketReady) {
71
68
  unsubscribe();
72
- registerLogRocketSessionUrlListener(superProperties, verbose);
69
+ registerLogRocketSessionUrlListener(verbose);
73
70
  }
74
71
  });
75
72
  }
76
- const trackFunction = async (eventName, properties, _options = {})=>{
77
- try {
78
- const { targetProductId = null, keepAlive = false } = _options;
79
- const baseProperties = getBaseProperties();
80
- const allProperties = {
81
- ...baseProperties,
82
- ...Object.fromEntries(superProperties),
83
- ...properties
84
- };
85
- await fetch(fullUrl, {
86
- method: "POST",
87
- credentials: "include",
88
- keepalive: keepAlive,
89
- headers: {
90
- "Content-Type": "application/json"
91
- },
92
- body: JSON.stringify({
93
- eventName,
94
- productIdentifier: productId,
95
- targetProductIdentifier: targetProductId,
96
- properties: allProperties
97
- })
98
- });
99
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
100
- } catch (error) {
101
- // Do nothing...
102
- }
103
- };
104
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
105
- // @ts-ignore
106
- globalThis[TrackingFunctionName] = trackFunction;
107
- return trackFunction;
73
+ setMixpanelContext({
74
+ productId,
75
+ endpoint,
76
+ superProperties: getSuperProperties(),
77
+ verbose
78
+ });
79
+ if (verbose) {
80
+ console.log("[mixpanel] Mixpanel is initialized.");
81
+ }
108
82
  }
109
83
 
110
84
  export { __resetInitializationGuard, initializeMixpanel };
@@ -1 +1 @@
1
- {"version":3,"file":"js/initializeMixpanel.js","sources":["webpack://@workleap/mixpanel/./src/js/initializeMixpanel.ts"],"sourcesContent":["import { getBootstrappingStore, getTelemetryContext } from \"@workleap/telemetry\";\nimport { getTrackingEndpoint, type Environment } from \"./env.ts\";\nimport { TrackingFunctionName } from \"./getMixpanelTrackingFunction.ts\";\nimport { HasExecutedGuard } from \"./HasExecutedGuard.ts\";\nimport { getBaseProperties, TelemetryProperties, type MixpanelTrackEventProperties } from \"./properties.ts\";\n\n/**\n * @see https://workleap.github.io/wl-telemetry\n */\nexport interface MixpanelTrackingOptions {\n /**\n * The product identifier of the target product.\n */\n targetProductId?: string;\n /**\n * Whether to keep the connection alive for the tracking request.\n * It is mostly used for tracking links where the user might navigate away before the request is completed.\n *\n * Caution! The body size for keepalive requests is limited to 64 kibibytes.\n *\n * https://developer.mozilla.org/en-US/docs/Web/API/RequestInit#keepalive\n *\n * @default false\n */\n keepAlive?: boolean;\n}\n\n/**\n * A function that sends tracking events to the tracking API.\n * @param eventName The name of the event to track.\n * @param properties The properties to send with the event.\n * @param options Options for tracking the event.\n * @see https://workleap.github.io/wl-telemetry\n */\nexport type MixpanelTrackingFunction = (eventName: string, properties: MixpanelTrackEventProperties, options?: MixpanelTrackingOptions) => Promise<void>;\n\n/**\n * @see https://workleap.github.io/wl-telemetry\n */\nexport interface InitializeMixpanelOptions {\n verbose?: boolean;\n}\n\nfunction registerLogRocketSessionUrlListener(superProperties: Map<string, string>, verbose = false) {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n if (globalThis.__WLP_LOGROCKET_INSTRUMENTATION_REGISTER_GET_SESSION_URL_LISTENER__) {\n // Automatically add the LogRocket session URL to all Honeycomb traces as an attribute.\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n globalThis.__WLP_LOGROCKET_INSTRUMENTATION_REGISTER_GET_SESSION_URL_LISTENER__((sessionUrl: string) => {\n if (verbose) {\n console.log(\"[mixpanel] Received LogRocket session replay URL:\", sessionUrl);\n }\n\n superProperties.set(TelemetryProperties.LogRocketSessionUrl, sessionUrl);\n });\n } else if (verbose) {\n console.log(\"[mixpanel] Cannot integrate with LogRocket because \\\"globalThis.__WLP_LOGROCKET_INSTRUMENTATION_REGISTER_GET_SESSION_URL_LISTENER__\\\" is not available.\");\n }\n}\n\nconst initializationGuard = new HasExecutedGuard();\n\n// This function should only be used by tests.\nexport function __resetInitializationGuard() {\n initializationGuard.reset();\n}\n\n/**\n * Creates a function that sends tracking events to the tracking API.\n * @param productId Your product identifier.\n * @param envOrTrackingApiBaseUrl The environment to get the navigation url from or a base URL.\n * @param options Options for creating the tracking function.\n * @returns A function that sends tracking events to the tracking API.\n * @see https://workleap.github.io/wl-telemetry\n */\nexport function initializeMixpanel(productId: string, envOrTrackingApiBaseUrl: Environment | (string & {}), options: InitializeMixpanelOptions = {}) : MixpanelTrackingFunction {\n const {\n verbose\n } = options;\n\n initializationGuard.throw(\"[mixpanel] Mixpanel has already been initialized. Did you call the \\\"initializeMixpanel\\\" function twice?\");\n\n // Equivalent to: https://docs.mixpanel.com/docs/tracking-methods/sdks/javascript#setting-super-properties.\n const superProperties = new Map<string, string>();\n\n const fullUrl = getTrackingEndpoint(envOrTrackingApiBaseUrl);\n const telemetryContext = getTelemetryContext({ verbose });\n\n superProperties.set(TelemetryProperties.TelemetryId, telemetryContext.telemetryId);\n superProperties.set(TelemetryProperties.DeviceId, telemetryContext.deviceId);\n\n const bootstrappingStore = getBootstrappingStore();\n\n // If LogRocket is already available, register the listener. Otherwise, subscribe to the bootstrapping store\n // and register the listener once a notification is received that LogRocket is registered.\n if (bootstrappingStore.state.isLogRocketReady) {\n registerLogRocketSessionUrlListener(superProperties, verbose);\n } else {\n bootstrappingStore.subscribe((action, store, unsubscribe) => {\n if (store.state.isLogRocketReady) {\n unsubscribe();\n registerLogRocketSessionUrlListener(superProperties, verbose);\n }\n });\n }\n\n const trackFunction: MixpanelTrackingFunction = async (eventName, properties, _options = {}) => {\n try {\n const {\n targetProductId = null,\n keepAlive = false\n } = _options;\n\n const baseProperties = getBaseProperties();\n\n const allProperties = {\n ...baseProperties,\n ...Object.fromEntries(superProperties),\n ...properties\n };\n\n await fetch(fullUrl, {\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 targetProductIdentifier : targetProductId,\n properties: allProperties\n })\n });\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n } catch (error: unknown) {\n // Do nothing...\n }\n };\n\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n globalThis[TrackingFunctionName] = trackFunction;\n\n return trackFunction;\n}\n"],"names":["getBootstrappingStore","getTelemetryContext","getTrackingEndpoint","TrackingFunctionName","HasExecutedGuard","getBaseProperties","TelemetryProperties","registerLogRocketSessionUrlListener","superProperties","verbose","globalThis","sessionUrl","console","initializationGuard","__resetInitializationGuard","initializeMixpanel","productId","envOrTrackingApiBaseUrl","options","Map","fullUrl","telemetryContext","bootstrappingStore","action","store","unsubscribe","trackFunction","eventName","properties","_options","targetProductId","keepAlive","baseProperties","allProperties","Object","fetch","JSON","error"],"mappings":";;;;;;;;;;;;;;;;;AAAiF;AAChB;AACO;AACf;AACmD;AAuC5G,SAASO,oCAAoCC,eAAoC,EAAEC,UAAU,KAAK;IAC9F,6DAA6D;IAC7D,aAAa;IACb,IAAIC,WAAW,mEAAmE,EAAE;QAChF,uFAAuF;QACvF,6DAA6D;QAC7D,aAAa;QACbA,WAAW,mEAAmE,CAAC,CAACC;YAC5E,IAAIF,SAAS;gBACTG,QAAQ,GAAG,CAAC,qDAAqDD;YACrE;YAEAH,gBAAgB,GAAG,CAACF,uCAAuC,EAAEK;QACjE;IACJ,OAAO,IAAIF,SAAS;QAChBG,QAAQ,GAAG,CAAC;IAChB;AACJ;AAEA,MAAMC,sBAAsB,IAAIT,gBAAgBA;AAEhD,8CAA8C;AACvC,SAASU;IACZD,oBAAoB,KAAK;AAC7B;AAEA;;;;;;;CAOC,GACM,SAASE,mBAAmBC,SAAiB,EAAEC,uBAAoD,EAAEC,UAAqC,CAAC,CAAC;IAC/I,MAAM,EACFT,OAAO,EACV,GAAGS;IAEJL,oBAAoB,KAAK,CAAC;IAE1B,2GAA2G;IAC3G,MAAML,kBAAkB,IAAIW;IAE5B,MAAMC,UAAUlB,mBAAmBA,CAACe;IACpC,MAAMI,mBAAmBpB,mBAAmBA,CAAC;QAAEQ;IAAQ;IAEvDD,gBAAgB,GAAG,CAACF,+BAA+B,EAAEe,iBAAiB,WAAW;IACjFb,gBAAgB,GAAG,CAACF,4BAA4B,EAAEe,iBAAiB,QAAQ;IAE3E,MAAMC,qBAAqBtB,qBAAqBA;IAEhD,4GAA4G;IAC5G,0FAA0F;IAC1F,IAAIsB,mBAAmB,KAAK,CAAC,gBAAgB,EAAE;QAC3Cf,oCAAoCC,iBAAiBC;IACzD,OAAO;QACHa,mBAAmB,SAAS,CAAC,CAACC,QAAQC,OAAOC;YACzC,IAAID,MAAM,KAAK,CAAC,gBAAgB,EAAE;gBAC9BC;gBACAlB,oCAAoCC,iBAAiBC;YACzD;QACJ;IACJ;IAEA,MAAMiB,gBAA0C,OAAOC,WAAWC,YAAYC,WAAW,CAAC,CAAC;QACvF,IAAI;YACA,MAAM,EACFC,kBAAkB,IAAI,EACtBC,YAAY,KAAK,EACpB,GAAGF;YAEJ,MAAMG,iBAAiB3B,iBAAiBA;YAExC,MAAM4B,gBAAgB;gBAClB,GAAGD,cAAc;gBACjB,GAAGE,OAAO,WAAW,CAAC1B,gBAAgB;gBACtC,GAAGoB,UAAU;YACjB;YAEA,MAAMO,MAAMf,SAAS;gBACjB,QAAQ;gBACR,aAAa;gBACb,WAAWW;gBACX,SAAS;oBAAE,gBAAgB;gBAAmB;gBAC9C,MAAMK,KAAK,SAAS,CAAC;oBACjBT;oBACA,mBAAmBX;oBACnB,yBAA0Bc;oBAC1B,YAAYG;gBAChB;YACJ;QACJ,6DAA6D;QAC7D,EAAE,OAAOI,OAAgB;QACrB,gBAAgB;QACpB;IACJ;IAEA,6DAA6D;IAC7D,aAAa;IACb3B,UAAU,CAACP,oBAAoBA,CAAC,GAAGuB;IAEnC,OAAOA;AACX"}
1
+ {"version":3,"file":"js/initializeMixpanel.js","sources":["webpack://@workleap/mixpanel/./src/js/initializeMixpanel.ts"],"sourcesContent":["import { getBootstrappingStore, getTelemetryContext } from \"@workleap/telemetry\";\nimport { setMixpanelContext } from \"./context.ts\";\nimport { getTrackingEndpoint, type Environment } from \"./env.ts\";\nimport { HasExecutedGuard } from \"./HasExecutedGuard.ts\";\nimport { getSuperProperties, getTelemetryProperties, OtherProperties, setSuperProperties, setSuperProperty } from \"./properties.ts\";\n\n/**\n * @see https://workleap.github.io/wl-telemetry\n */\nexport interface InitializeMixpanelOptions {\n /**\n * The endpoint to use for tracking events.\n * If not provided, the default endpoint for the environment will be used.\n * @default \"tracking/track\"\n */\n trackingEndpoint?: string;\n verbose?: boolean;\n}\nfunction registerLogRocketSessionUrlListener(verbose = false) {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n if (globalThis.__WLP_LOGROCKET_INSTRUMENTATION_REGISTER_GET_SESSION_URL_LISTENER__) {\n // Automatically add the LogRocket session URL to all Honeycomb traces as an attribute.\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n globalThis.__WLP_LOGROCKET_INSTRUMENTATION_REGISTER_GET_SESSION_URL_LISTENER__((sessionUrl: string) => {\n if (verbose) {\n console.log(\"[mixpanel] Received LogRocket session replay URL:\", sessionUrl);\n }\n\n setSuperProperty(OtherProperties.LogRocketSessionUrl, sessionUrl);\n });\n } else if (verbose) {\n console.log(\"[mixpanel] Cannot integrate with LogRocket because \\\"globalThis.__WLP_LOGROCKET_INSTRUMENTATION_REGISTER_GET_SESSION_URL_LISTENER__\\\" is not available.\");\n }\n}\n\nconst initializationGuard = new HasExecutedGuard();\n\n// This function should only be used by tests.\nexport function __resetInitializationGuard() {\n initializationGuard.reset();\n}\n\n/**\n * Creates a function that sends tracking events to the tracking API.\n * @param productId Your product identifier.\n * @param envOrTrackingApiBaseUrl The environment to get the navigation url from or a base URL.\n * @param options Options for creating the tracking function.\n * @returns A function that sends tracking events to the tracking API.\n * @see https://workleap.github.io/wl-telemetry\n */\nexport function initializeMixpanel(productId: string, envOrTrackingApiBaseUrl: Environment | (string & {}), options: InitializeMixpanelOptions = {}) {\n const {\n verbose = false,\n trackingEndpoint\n } = options;\n\n initializationGuard.throw(\"[mixpanel] Mixpanel has already been initialized. Did you call the \\\"initializeMixpanel\\\" function twice?\");\n\n const endpoint = getTrackingEndpoint(envOrTrackingApiBaseUrl, trackingEndpoint);\n const telemetryContext = getTelemetryContext({ verbose });\n\n setSuperProperties(getTelemetryProperties(telemetryContext));\n\n const bootstrappingStore = getBootstrappingStore();\n\n // If LogRocket is already available, register the listener. Otherwise, subscribe to the bootstrapping store\n // and register the listener once a notification is received that LogRocket is registered.\n if (bootstrappingStore.state.isLogRocketReady) {\n registerLogRocketSessionUrlListener(verbose);\n } else {\n bootstrappingStore.subscribe((action, store, unsubscribe) => {\n if (store.state.isLogRocketReady) {\n unsubscribe();\n registerLogRocketSessionUrlListener(verbose);\n }\n });\n }\n\n setMixpanelContext({\n productId,\n endpoint,\n superProperties: getSuperProperties(),\n verbose\n });\n\n if (verbose) {\n console.log(\"[mixpanel] Mixpanel is initialized.\");\n }\n}\n"],"names":["getBootstrappingStore","getTelemetryContext","setMixpanelContext","getTrackingEndpoint","HasExecutedGuard","getSuperProperties","getTelemetryProperties","OtherProperties","setSuperProperties","setSuperProperty","registerLogRocketSessionUrlListener","verbose","globalThis","sessionUrl","console","initializationGuard","__resetInitializationGuard","initializeMixpanel","productId","envOrTrackingApiBaseUrl","options","trackingEndpoint","endpoint","telemetryContext","bootstrappingStore","action","store","unsubscribe"],"mappings":";;;;;;;;;;;;;;;;;AAAiF;AAC/B;AACe;AACR;AAC2E;AAcpI,SAASU,oCAAoCC,UAAU,KAAK;IACxD,6DAA6D;IAC7D,aAAa;IACb,IAAIC,WAAW,mEAAmE,EAAE;QAChF,uFAAuF;QACvF,6DAA6D;QAC7D,aAAa;QACbA,WAAW,mEAAmE,CAAC,CAACC;YAC5E,IAAIF,SAAS;gBACTG,QAAQ,GAAG,CAAC,qDAAqDD;YACrE;YAEAJ,gBAAgBA,CAACF,mCAAmC,EAAEM;QAC1D;IACJ,OAAO,IAAIF,SAAS;QAChBG,QAAQ,GAAG,CAAC;IAChB;AACJ;AAEA,MAAMC,sBAAsB,IAAIX,gBAAgBA;AAEhD,8CAA8C;AACvC,SAASY;IACZD,oBAAoB,KAAK;AAC7B;AAEA;;;;;;;CAOC,GACM,SAASE,mBAAmBC,SAAiB,EAAEC,uBAAoD,EAAEC,UAAqC,CAAC,CAAC;IAC/I,MAAM,EACFT,UAAU,KAAK,EACfU,gBAAgB,EACnB,GAAGD;IAEJL,oBAAoB,KAAK,CAAC;IAE1B,MAAMO,WAAWnB,mBAAmBA,CAACgB,yBAAyBE;IAC9D,MAAME,mBAAmBtB,mBAAmBA,CAAC;QAAEU;IAAQ;IAEvDH,kBAAkBA,CAACF,sBAAsBA,CAACiB;IAE1C,MAAMC,qBAAqBxB,qBAAqBA;IAEhD,4GAA4G;IAC5G,0FAA0F;IAC1F,IAAIwB,mBAAmB,KAAK,CAAC,gBAAgB,EAAE;QAC3Cd,oCAAoCC;IACxC,OAAO;QACHa,mBAAmB,SAAS,CAAC,CAACC,QAAQC,OAAOC;YACzC,IAAID,MAAM,KAAK,CAAC,gBAAgB,EAAE;gBAC9BC;gBACAjB,oCAAoCC;YACxC;QACJ;IACJ;IAEAT,kBAAkBA,CAAC;QACfgB;QACAI;QACA,iBAAiBjB,kBAAkBA;QACnCM;IACJ;IAEA,IAAIA,SAAS;QACTG,QAAQ,GAAG,CAAC;IAChB;AACJ"}
@@ -1,4 +1,5 @@
1
- export type MixpanelTrackEventProperties = Record<string, unknown>;
1
+ import type { TelemetryContext } from "@workleap/telemetry";
2
+ export type MixpanelEventProperties = Record<string, unknown>;
2
3
  export declare const BaseProperties: {
3
4
  readonly Browser: "$browser";
4
5
  readonly BrowserVersion: "$browser_version";
@@ -11,11 +12,6 @@ export declare const BaseProperties: {
11
12
  readonly ScreenHeight: "$screen_height";
12
13
  readonly ScreenWidth: "$screen_width";
13
14
  };
14
- export declare const TelemetryProperties: {
15
- readonly DeviceId: "Device Id";
16
- readonly LogRocketSessionUrl: "LogRocket Session Url";
17
- readonly TelemetryId: "Telemetry Id";
18
- };
19
15
  export declare function getBaseProperties(): {
20
16
  $os: string;
21
17
  $browser: string;
@@ -28,3 +24,19 @@ export declare function getBaseProperties(): {
28
24
  $screen_width: number;
29
25
  "Is Mobile": boolean;
30
26
  };
27
+ export declare const TelemetryProperties: {
28
+ readonly DeviceId: "Device Id";
29
+ readonly TelemetryId: "Telemetry Id";
30
+ };
31
+ export declare function getTelemetryProperties(context: TelemetryContext): {
32
+ "Telemetry Id": string;
33
+ "Device Id": string;
34
+ };
35
+ export declare const OtherProperties: {
36
+ LogRocketSessionUrl: string;
37
+ };
38
+ export type SuperProperties = Map<string, unknown>;
39
+ export declare function setSuperProperty(key: string, value: unknown): void;
40
+ export declare function setSuperProperties(values: Record<string, unknown>): void;
41
+ export declare function getSuperProperties(): SuperProperties;
42
+ export declare function __clearSuperProperties(): void;
@@ -16,11 +16,6 @@ const BaseProperties = {
16
16
  ScreenHeight: "$screen_height",
17
17
  ScreenWidth: "$screen_width"
18
18
  };
19
- const TelemetryProperties = {
20
- DeviceId: "Device Id",
21
- LogRocketSessionUrl: "LogRocket Session Url",
22
- TelemetryId: "Telemetry Id"
23
- };
24
19
  function getBaseProperties() {
25
20
  const userAgent = navigator.userAgent;
26
21
  const device = deviceType(userAgent);
@@ -37,7 +32,38 @@ function getBaseProperties() {
37
32
  [BaseProperties.IsMobile]: device.length > 0
38
33
  };
39
34
  }
35
+ const TelemetryProperties = {
36
+ DeviceId: "Device Id",
37
+ TelemetryId: "Telemetry Id"
38
+ };
39
+ function getTelemetryProperties(context) {
40
+ return {
41
+ [TelemetryProperties.TelemetryId]: context.telemetryId,
42
+ [TelemetryProperties.DeviceId]: context.deviceId
43
+ };
44
+ }
45
+ const OtherProperties = {
46
+ LogRocketSessionUrl: "LogRocket Session Url"
47
+ };
48
+ // Equivalent to: https://docs.mixpanel.com/docs/tracking-methods/sdks/javascript#setting-super-properties.
49
+ const superProperties = new Map();
50
+ function setSuperProperty(key, value) {
51
+ superProperties.set(key, value);
52
+ }
53
+ function setSuperProperties(values) {
54
+ Object.keys(values).forEach((x)=>{
55
+ superProperties.set(x, values[x]);
56
+ });
57
+ }
58
+ // Must always return the actual reference to the super properties, not a copy
59
+ // because it will breaks the global context.
60
+ function getSuperProperties() {
61
+ return superProperties;
62
+ }
63
+ function __clearSuperProperties() {
64
+ superProperties.clear();
65
+ }
40
66
 
41
- export { BaseProperties, TelemetryProperties, getBaseProperties };
67
+ export { BaseProperties, OtherProperties, TelemetryProperties, __clearSuperProperties, getBaseProperties, getSuperProperties, getTelemetryProperties, setSuperProperties, setSuperProperty };
42
68
 
43
69
  //# sourceMappingURL=properties.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"js/properties.js","sources":["webpack://@workleap/mixpanel/./src/js/properties.ts"],"sourcesContent":["\nimport { browserName, browserVersion, deviceType, osName, referringDomain } from \"./utils.ts\";\n\nexport type MixpanelTrackEventProperties = Record<string, unknown>;\n\nexport const BaseProperties = {\n Browser: \"$browser\",\n BrowserVersion: \"$browser_version\",\n CurrentUrl: \"$current_url\",\n Device: \"$device\",\n IsMobile: \"Is Mobile\",\n Os: \"$os\",\n Referrer: \"$referrer\",\n ReferringDomain: \"$referring_domain\",\n ScreenHeight: \"$screen_height\",\n ScreenWidth: \"$screen_width\"\n} as const;\n\nexport const TelemetryProperties = {\n DeviceId: \"Device Id\",\n LogRocketSessionUrl: \"LogRocket Session Url\",\n TelemetryId: \"Telemetry Id\"\n} as const;\n\nexport function getBaseProperties() {\n const userAgent = navigator.userAgent;\n const device = deviceType(userAgent);\n\n return {\n [BaseProperties.Os]: osName(userAgent),\n [BaseProperties.Browser]: browserName(userAgent, navigator.vendor),\n [BaseProperties.Referrer]: document.referrer,\n [BaseProperties.ReferringDomain]: referringDomain(document.referrer),\n [BaseProperties.Device]: device,\n [BaseProperties.CurrentUrl]: window.location.href,\n [BaseProperties.BrowserVersion]: browserVersion(userAgent, navigator.vendor),\n [BaseProperties.ScreenHeight]: window.screen.height,\n [BaseProperties.ScreenWidth]: window.screen.width,\n [BaseProperties.IsMobile]: device.length > 0\n } satisfies MixpanelTrackEventProperties;\n}\n"],"names":["browserName","browserVersion","deviceType","osName","referringDomain","BaseProperties","TelemetryProperties","getBaseProperties","userAgent","navigator","device","document","window"],"mappings":";;;;;AAC8F;AAIvF,MAAMK,iBAAiB;IAC1B,SAAS;IACT,gBAAgB;IAChB,YAAY;IACZ,QAAQ;IACR,UAAU;IACV,IAAI;IACJ,UAAU;IACV,iBAAiB;IACjB,cAAc;IACd,aAAa;AACjB,EAAW;AAEJ,MAAMC,sBAAsB;IAC/B,UAAU;IACV,qBAAqB;IACrB,aAAa;AACjB,EAAW;AAEJ,SAASC;IACZ,MAAMC,YAAYC,UAAU,SAAS;IACrC,MAAMC,SAASR,UAAUA,CAACM;IAE1B,OAAO;QACH,CAACH,eAAe,EAAE,CAAC,EAAEF,MAAMA,CAACK;QAC5B,CAACH,eAAe,OAAO,CAAC,EAAEL,WAAWA,CAACQ,WAAWC,UAAU,MAAM;QACjE,CAACJ,eAAe,QAAQ,CAAC,EAAEM,SAAS,QAAQ;QAC5C,CAACN,eAAe,eAAe,CAAC,EAAED,eAAeA,CAACO,SAAS,QAAQ;QACnE,CAACN,eAAe,MAAM,CAAC,EAAEK;QACzB,CAACL,eAAe,UAAU,CAAC,EAAEO,OAAO,QAAQ,CAAC,IAAI;QACjD,CAACP,eAAe,cAAc,CAAC,EAAEJ,cAAcA,CAACO,WAAWC,UAAU,MAAM;QAC3E,CAACJ,eAAe,YAAY,CAAC,EAAEO,OAAO,MAAM,CAAC,MAAM;QACnD,CAACP,eAAe,WAAW,CAAC,EAAEO,OAAO,MAAM,CAAC,KAAK;QACjD,CAACP,eAAe,QAAQ,CAAC,EAAEK,OAAO,MAAM,GAAG;IAC/C;AACJ"}
1
+ {"version":3,"file":"js/properties.js","sources":["webpack://@workleap/mixpanel/./src/js/properties.ts"],"sourcesContent":["\nimport type { TelemetryContext } from \"@workleap/telemetry\";\nimport { browserName, browserVersion, deviceType, osName, referringDomain } from \"./utils.ts\";\n\nexport type MixpanelEventProperties = Record<string, unknown>;\n\nexport const BaseProperties = {\n Browser: \"$browser\",\n BrowserVersion: \"$browser_version\",\n CurrentUrl: \"$current_url\",\n Device: \"$device\",\n IsMobile: \"Is Mobile\",\n Os: \"$os\",\n Referrer: \"$referrer\",\n ReferringDomain: \"$referring_domain\",\n ScreenHeight: \"$screen_height\",\n ScreenWidth: \"$screen_width\"\n} as const;\n\nexport function getBaseProperties() {\n const userAgent = navigator.userAgent;\n const device = deviceType(userAgent);\n\n return {\n [BaseProperties.Os]: osName(userAgent),\n [BaseProperties.Browser]: browserName(userAgent, navigator.vendor),\n [BaseProperties.Referrer]: document.referrer,\n [BaseProperties.ReferringDomain]: referringDomain(document.referrer),\n [BaseProperties.Device]: device,\n [BaseProperties.CurrentUrl]: window.location.href,\n [BaseProperties.BrowserVersion]: browserVersion(userAgent, navigator.vendor),\n [BaseProperties.ScreenHeight]: window.screen.height,\n [BaseProperties.ScreenWidth]: window.screen.width,\n [BaseProperties.IsMobile]: device.length > 0\n } satisfies MixpanelEventProperties;\n}\n\nexport const TelemetryProperties = {\n DeviceId: \"Device Id\",\n TelemetryId: \"Telemetry Id\"\n} as const;\n\nexport function getTelemetryProperties(context: TelemetryContext) {\n return {\n [TelemetryProperties.TelemetryId]: context.telemetryId,\n [TelemetryProperties.DeviceId]: context.deviceId\n } satisfies MixpanelEventProperties;\n}\n\nexport const OtherProperties = {\n LogRocketSessionUrl: \"LogRocket Session Url\"\n};\n\n//////////////////////\n\nexport type SuperProperties = Map<string, unknown>;\n\n// Equivalent to: https://docs.mixpanel.com/docs/tracking-methods/sdks/javascript#setting-super-properties.\nconst superProperties: SuperProperties = new Map<string, unknown>();\n\nexport function setSuperProperty(key: string, value: unknown) {\n superProperties.set(key, value);\n}\n\nexport function setSuperProperties(values: Record<string, unknown>) {\n Object.keys(values).forEach(x => {\n superProperties.set(x, values[x]);\n });\n}\n\n// Must always return the actual reference to the super properties, not a copy\n// because it will breaks the global context.\nexport function getSuperProperties() {\n return superProperties;\n}\n\nexport function __clearSuperProperties() {\n superProperties.clear();\n}\n"],"names":["browserName","browserVersion","deviceType","osName","referringDomain","BaseProperties","getBaseProperties","userAgent","navigator","device","document","window","TelemetryProperties","getTelemetryProperties","context","OtherProperties","superProperties","Map","setSuperProperty","key","value","setSuperProperties","values","Object","x","getSuperProperties","__clearSuperProperties"],"mappings":";;;;;AAE8F;AAIvF,MAAMK,iBAAiB;IAC1B,SAAS;IACT,gBAAgB;IAChB,YAAY;IACZ,QAAQ;IACR,UAAU;IACV,IAAI;IACJ,UAAU;IACV,iBAAiB;IACjB,cAAc;IACd,aAAa;AACjB,EAAW;AAEJ,SAASC;IACZ,MAAMC,YAAYC,UAAU,SAAS;IACrC,MAAMC,SAASP,UAAUA,CAACK;IAE1B,OAAO;QACH,CAACF,eAAe,EAAE,CAAC,EAAEF,MAAMA,CAACI;QAC5B,CAACF,eAAe,OAAO,CAAC,EAAEL,WAAWA,CAACO,WAAWC,UAAU,MAAM;QACjE,CAACH,eAAe,QAAQ,CAAC,EAAEK,SAAS,QAAQ;QAC5C,CAACL,eAAe,eAAe,CAAC,EAAED,eAAeA,CAACM,SAAS,QAAQ;QACnE,CAACL,eAAe,MAAM,CAAC,EAAEI;QACzB,CAACJ,eAAe,UAAU,CAAC,EAAEM,OAAO,QAAQ,CAAC,IAAI;QACjD,CAACN,eAAe,cAAc,CAAC,EAAEJ,cAAcA,CAACM,WAAWC,UAAU,MAAM;QAC3E,CAACH,eAAe,YAAY,CAAC,EAAEM,OAAO,MAAM,CAAC,MAAM;QACnD,CAACN,eAAe,WAAW,CAAC,EAAEM,OAAO,MAAM,CAAC,KAAK;QACjD,CAACN,eAAe,QAAQ,CAAC,EAAEI,OAAO,MAAM,GAAG;IAC/C;AACJ;AAEO,MAAMG,sBAAsB;IAC/B,UAAU;IACV,aAAa;AACjB,EAAW;AAEJ,SAASC,uBAAuBC,OAAyB;IAC5D,OAAO;QACH,CAACF,oBAAoB,WAAW,CAAC,EAAEE,QAAQ,WAAW;QACtD,CAACF,oBAAoB,QAAQ,CAAC,EAAEE,QAAQ,QAAQ;IACpD;AACJ;AAEO,MAAMC,kBAAkB;IAC3B,qBAAqB;AACzB,EAAE;AAMF,2GAA2G;AAC3G,MAAMC,kBAAmC,IAAIC;AAEtC,SAASC,iBAAiBC,GAAW,EAAEC,KAAc;IACxDJ,gBAAgB,GAAG,CAACG,KAAKC;AAC7B;AAEO,SAASC,mBAAmBC,MAA+B;IAC9DC,OAAO,IAAI,CAACD,QAAQ,OAAO,CAACE,CAAAA;QACxBR,gBAAgB,GAAG,CAACQ,GAAGF,MAAM,CAACE,EAAE;IACpC;AACJ;AAEA,8EAA8E;AAC9E,6CAA6C;AACtC,SAASC;IACZ,OAAOT;AACX;AAEO,SAASU;IACZV,gBAAgB,KAAK;AACzB"}
@@ -1 +1 @@
1
- export { useMixpanelTracking } from "./useMixpanelTracking.ts";
1
+ export { useTrackingFunction, type UseTrackingFunctionOptions } from "./useTrackingFunction.ts";
@@ -1,10 +1,10 @@
1
- import { useMixpanelTracking } from "./useMixpanelTracking.js";
1
+ import { useTrackingFunction } from "./useTrackingFunction.js";
2
2
 
3
- ;// CONCATENATED MODULE: external "./useMixpanelTracking.js"
3
+ ;// CONCATENATED MODULE: external "./useTrackingFunction.js"
4
4
 
5
5
  ;// CONCATENATED MODULE: ./src/react/index.ts
6
6
 
7
7
 
8
- export { useMixpanelTracking };
8
+ export { useTrackingFunction };
9
9
 
10
10
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"react/index.js","sources":["webpack://@workleap/mixpanel/./src/react/index.ts"],"sourcesContent":["export { useMixpanelTracking } from \"./useMixpanelTracking.ts\";\n\n"],"names":["useMixpanelTracking"],"mappings":";;;;;AAA+D"}
1
+ {"version":3,"file":"react/index.js","sources":["webpack://@workleap/mixpanel/./src/react/index.ts"],"sourcesContent":["export { useTrackingFunction, type UseTrackingFunctionOptions } from \"./useTrackingFunction.ts\";\n\n"],"names":["useTrackingFunction"],"mappings":";;;;;AAAgG"}
@@ -0,0 +1,3 @@
1
+ import { type CreateTrackingFunctionOptions } from "../js/createTrackingFunction.ts";
2
+ export type UseTrackingFunctionOptions = CreateTrackingFunctionOptions;
3
+ export declare function useTrackingFunction(options?: UseTrackingFunctionOptions): import("../js/createTrackingFunction.ts").TrackingFunction;
@@ -0,0 +1,24 @@
1
+ import { useMemo } from "react";
2
+ import { createTrackingFunction } from "../js/createTrackingFunction.js";
3
+
4
+ ;// CONCATENATED MODULE: external "react"
5
+
6
+ ;// CONCATENATED MODULE: external "../js/createTrackingFunction.js"
7
+
8
+ ;// CONCATENATED MODULE: ./src/react/useTrackingFunction.ts
9
+
10
+
11
+ function useTrackingFunction(options = {}) {
12
+ const { targetProductId } = options;
13
+ return useMemo(()=>{
14
+ return createTrackingFunction({
15
+ targetProductId
16
+ });
17
+ }, [
18
+ targetProductId
19
+ ]);
20
+ }
21
+
22
+ export { useTrackingFunction };
23
+
24
+ //# sourceMappingURL=useTrackingFunction.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react/useTrackingFunction.js","sources":["webpack://@workleap/mixpanel/./src/react/useTrackingFunction.ts"],"sourcesContent":["import { useMemo } from \"react\";\nimport { createTrackingFunction, type CreateTrackingFunctionOptions } from \"../js/createTrackingFunction.ts\";\n\nexport type UseTrackingFunctionOptions = CreateTrackingFunctionOptions;\n\nexport function useTrackingFunction(options: UseTrackingFunctionOptions = {}) {\n const {\n targetProductId\n } = options;\n\n return useMemo(() => {\n return createTrackingFunction({ targetProductId });\n }, [targetProductId]);\n}\n"],"names":["useMemo","createTrackingFunction","useTrackingFunction","options","targetProductId"],"mappings":";;;;;;;;AAAgC;AAC6E;AAItG,SAASE,oBAAoBC,UAAsC,CAAC,CAAC;IACxE,MAAM,EACFC,eAAe,EAClB,GAAGD;IAEJ,OAAOH,OAAOA,CAAC;QACX,OAAOC,sBAAsBA,CAAC;YAAEG;QAAgB;IACpD,GAAG;QAACA;KAAgB;AACxB"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@workleap/mixpanel",
3
3
  "author": "Workleap",
4
- "version": "2.0.0",
4
+ "version": "2.0.2",
5
5
  "description": "A package containing utilities for Mixpanel telemetry.",
6
6
  "license": "Apache-2.0",
7
7
  "repository": {
@@ -0,0 +1,34 @@
1
+ import type { SuperProperties } from "./properties.ts";
2
+
3
+ export const ContextVariableName = "__WLP_MIXPANEL_CONTEXT__";
4
+
5
+ export interface MixpanelContext {
6
+ productId: string;
7
+ endpoint: string;
8
+ superProperties: SuperProperties;
9
+ verbose: boolean;
10
+ }
11
+
12
+ export function setMixpanelContext(context: MixpanelContext) {
13
+ // Will be used by the "track" function.
14
+ // It's important to share the context with a global DOM object rather than
15
+ // a singleton because projects like the platform widgets will use a distinct instance
16
+ // of this package than the host application.
17
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
18
+ // @ts-ignore
19
+ globalThis[ContextVariableName] = context;
20
+ }
21
+
22
+ export function getMixpanelContext() {
23
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
24
+ // @ts-ignore
25
+ const context = globalThis[ContextVariableName] as MixpanelContext;
26
+
27
+ if (!context) {
28
+ throw new Error("[mixpanel] The Mixpanel context is not available. Did you initialize Mixpanel with the \"initializeMixpanel\" function?");
29
+ }
30
+
31
+ return context;
32
+ }
33
+
34
+
@@ -0,0 +1,86 @@
1
+ import { getMixpanelContext } from "./context.ts";
2
+ import { getBaseProperties, type MixpanelEventProperties } from "./properties.ts";
3
+
4
+ /**
5
+ * @see https://workleap.github.io/wl-telemetry
6
+ */
7
+ export interface CreateTrackingFunctionOptions {
8
+ /**
9
+ * The product identifier of the target product.
10
+ */
11
+ targetProductId?: string;
12
+ }
13
+
14
+ /**
15
+ * @see https://workleap.github.io/wl-telemetry
16
+ */
17
+ export interface TrackingFunctionOptions {
18
+ /**
19
+ * Whether to keep the connection alive for the tracking request.
20
+ * It is mostly used for tracking links where the user might navigate away before the request is completed.
21
+ *
22
+ * Caution! The body size for keepalive requests is limited to 64 kibibytes.
23
+ *
24
+ * https://developer.mozilla.org/en-US/docs/Web/API/RequestInit#keepalive
25
+ *
26
+ * @default false
27
+ */
28
+ keepAlive?: boolean;
29
+ }
30
+
31
+ /**
32
+ * A function that sends tracking events to the tracking API.
33
+ * @param eventName The name of the event to track.
34
+ * @param properties The properties to send with the event.
35
+ * @param options Options for tracking the event.
36
+ * @see https://workleap.github.io/wl-telemetry
37
+ */
38
+ export type TrackingFunction = (eventName: string, properties: MixpanelEventProperties, options?: TrackingFunctionOptions) => Promise<void>;
39
+
40
+ export function createTrackingFunction(options: CreateTrackingFunctionOptions = {}) {
41
+ const {
42
+ targetProductId
43
+ } = options;
44
+
45
+ const {
46
+ productId,
47
+ endpoint,
48
+ superProperties
49
+ } = getMixpanelContext();
50
+
51
+ const trackFunction: TrackingFunction = async (eventName, properties, _options = {}) => {
52
+ try {
53
+ const {
54
+ keepAlive = false
55
+ } = _options;
56
+
57
+ const baseProperties = getBaseProperties();
58
+
59
+ const allProperties = {
60
+ ...baseProperties,
61
+ ...Object.fromEntries(superProperties),
62
+ ...properties
63
+ };
64
+
65
+ await fetch(endpoint, {
66
+ method: "POST",
67
+ credentials: "include",
68
+ keepalive: keepAlive,
69
+ headers: { "Content-Type": "application/json" },
70
+ body: JSON.stringify({
71
+ eventName,
72
+ productIdentifier: productId,
73
+ // Not sure why, but it seems important to send "null" if not target product identifier
74
+ // are provided.
75
+ targetProductIdentifier : targetProductId ?? null,
76
+ properties: allProperties
77
+ })
78
+ });
79
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
80
+ } catch (error: unknown) {
81
+ // Do nothing...
82
+ }
83
+ };
84
+
85
+ return trackFunction;
86
+ }
package/src/js/env.ts CHANGED
@@ -16,7 +16,7 @@ function resolveApiUrl(path: string, baseUrl: string | undefined) : string {
16
16
  return `${baseUrl}${baseUrl!.endsWith("/") ? "" : "/"}${path.startsWith("/") ? path.substring(1) : path}`;
17
17
  }
18
18
 
19
- export function getTrackingEndpoint(envOrTrackingApiBaseUrl: Environment | (string & {})) {
19
+ export function getTrackingEndpoint(envOrTrackingApiBaseUrl: Environment | (string & {}), trackingEndpoint = "tracking/track"): string {
20
20
  let baseUrl: string;
21
21
 
22
22
  if (EnvironmentList.includes(envOrTrackingApiBaseUrl as Environment)) {
@@ -25,5 +25,5 @@ export function getTrackingEndpoint(envOrTrackingApiBaseUrl: Environment | (stri
25
25
  baseUrl = envOrTrackingApiBaseUrl;
26
26
  }
27
27
 
28
- return resolveApiUrl("tracking/track", baseUrl);
28
+ return resolveApiUrl(trackingEndpoint, baseUrl);
29
29
  }
package/src/js/index.ts CHANGED
@@ -1,5 +1,5 @@
1
+ export { createTrackingFunction, type CreateTrackingFunctionOptions, type TrackingFunction, type TrackingFunctionOptions } from "./createTrackingFunction.ts";
1
2
  export type { Environment } from "./env.ts";
2
- export { getMixpanelTrackingFunction } from "./getMixpanelTrackingFunction.ts";
3
- export { initializeMixpanel, type InitializeMixpanelOptions, type MixpanelTrackingFunction, type MixpanelTrackingOptions } from "./initializeMixpanel.ts";
4
- export type { MixpanelTrackEventProperties } from "./properties.ts";
3
+ export { initializeMixpanel, type InitializeMixpanelOptions } from "./initializeMixpanel.ts";
4
+ export { setSuperProperties, setSuperProperty, type MixpanelEventProperties } from "./properties.ts";
5
5
 
@@ -1,47 +1,22 @@
1
1
  import { getBootstrappingStore, getTelemetryContext } from "@workleap/telemetry";
2
+ import { setMixpanelContext } from "./context.ts";
2
3
  import { getTrackingEndpoint, type Environment } from "./env.ts";
3
- import { TrackingFunctionName } from "./getMixpanelTrackingFunction.ts";
4
4
  import { HasExecutedGuard } from "./HasExecutedGuard.ts";
5
- import { getBaseProperties, TelemetryProperties, type MixpanelTrackEventProperties } from "./properties.ts";
5
+ import { getSuperProperties, getTelemetryProperties, OtherProperties, setSuperProperties, setSuperProperty } from "./properties.ts";
6
6
 
7
7
  /**
8
8
  * @see https://workleap.github.io/wl-telemetry
9
9
  */
10
- export interface MixpanelTrackingOptions {
10
+ export interface InitializeMixpanelOptions {
11
11
  /**
12
- * The product identifier of the target product.
12
+ * The endpoint to use for tracking events.
13
+ * If not provided, the default endpoint for the environment will be used.
14
+ * @default "tracking/track"
13
15
  */
14
- targetProductId?: string;
15
- /**
16
- * Whether to keep the connection alive for the tracking request.
17
- * It is mostly used for tracking links where the user might navigate away before the request is completed.
18
- *
19
- * Caution! The body size for keepalive requests is limited to 64 kibibytes.
20
- *
21
- * https://developer.mozilla.org/en-US/docs/Web/API/RequestInit#keepalive
22
- *
23
- * @default false
24
- */
25
- keepAlive?: boolean;
26
- }
27
-
28
- /**
29
- * A function that sends tracking events to the tracking API.
30
- * @param eventName The name of the event to track.
31
- * @param properties The properties to send with the event.
32
- * @param options Options for tracking the event.
33
- * @see https://workleap.github.io/wl-telemetry
34
- */
35
- export type MixpanelTrackingFunction = (eventName: string, properties: MixpanelTrackEventProperties, options?: MixpanelTrackingOptions) => Promise<void>;
36
-
37
- /**
38
- * @see https://workleap.github.io/wl-telemetry
39
- */
40
- export interface InitializeMixpanelOptions {
16
+ trackingEndpoint?: string;
41
17
  verbose?: boolean;
42
18
  }
43
-
44
- function registerLogRocketSessionUrlListener(superProperties: Map<string, string>, verbose = false) {
19
+ function registerLogRocketSessionUrlListener(verbose = false) {
45
20
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
46
21
  // @ts-ignore
47
22
  if (globalThis.__WLP_LOGROCKET_INSTRUMENTATION_REGISTER_GET_SESSION_URL_LISTENER__) {
@@ -53,7 +28,7 @@ function registerLogRocketSessionUrlListener(superProperties: Map<string, string
53
28
  console.log("[mixpanel] Received LogRocket session replay URL:", sessionUrl);
54
29
  }
55
30
 
56
- superProperties.set(TelemetryProperties.LogRocketSessionUrl, sessionUrl);
31
+ setSuperProperty(OtherProperties.LogRocketSessionUrl, sessionUrl);
57
32
  });
58
33
  } else if (verbose) {
59
34
  console.log("[mixpanel] Cannot integrate with LogRocket because \"globalThis.__WLP_LOGROCKET_INSTRUMENTATION_REGISTER_GET_SESSION_URL_LISTENER__\" is not available.");
@@ -75,73 +50,42 @@ export function __resetInitializationGuard() {
75
50
  * @returns A function that sends tracking events to the tracking API.
76
51
  * @see https://workleap.github.io/wl-telemetry
77
52
  */
78
- export function initializeMixpanel(productId: string, envOrTrackingApiBaseUrl: Environment | (string & {}), options: InitializeMixpanelOptions = {}) : MixpanelTrackingFunction {
53
+ export function initializeMixpanel(productId: string, envOrTrackingApiBaseUrl: Environment | (string & {}), options: InitializeMixpanelOptions = {}) {
79
54
  const {
80
- verbose
55
+ verbose = false,
56
+ trackingEndpoint
81
57
  } = options;
82
58
 
83
59
  initializationGuard.throw("[mixpanel] Mixpanel has already been initialized. Did you call the \"initializeMixpanel\" function twice?");
84
60
 
85
- // Equivalent to: https://docs.mixpanel.com/docs/tracking-methods/sdks/javascript#setting-super-properties.
86
- const superProperties = new Map<string, string>();
87
-
88
- const fullUrl = getTrackingEndpoint(envOrTrackingApiBaseUrl);
61
+ const endpoint = getTrackingEndpoint(envOrTrackingApiBaseUrl, trackingEndpoint);
89
62
  const telemetryContext = getTelemetryContext({ verbose });
90
63
 
91
- superProperties.set(TelemetryProperties.TelemetryId, telemetryContext.telemetryId);
92
- superProperties.set(TelemetryProperties.DeviceId, telemetryContext.deviceId);
64
+ setSuperProperties(getTelemetryProperties(telemetryContext));
93
65
 
94
66
  const bootstrappingStore = getBootstrappingStore();
95
67
 
96
68
  // If LogRocket is already available, register the listener. Otherwise, subscribe to the bootstrapping store
97
69
  // and register the listener once a notification is received that LogRocket is registered.
98
70
  if (bootstrappingStore.state.isLogRocketReady) {
99
- registerLogRocketSessionUrlListener(superProperties, verbose);
71
+ registerLogRocketSessionUrlListener(verbose);
100
72
  } else {
101
73
  bootstrappingStore.subscribe((action, store, unsubscribe) => {
102
74
  if (store.state.isLogRocketReady) {
103
75
  unsubscribe();
104
- registerLogRocketSessionUrlListener(superProperties, verbose);
76
+ registerLogRocketSessionUrlListener(verbose);
105
77
  }
106
78
  });
107
79
  }
108
80
 
109
- const trackFunction: MixpanelTrackingFunction = async (eventName, properties, _options = {}) => {
110
- try {
111
- const {
112
- targetProductId = null,
113
- keepAlive = false
114
- } = _options;
115
-
116
- const baseProperties = getBaseProperties();
117
-
118
- const allProperties = {
119
- ...baseProperties,
120
- ...Object.fromEntries(superProperties),
121
- ...properties
122
- };
123
-
124
- await fetch(fullUrl, {
125
- method: "POST",
126
- credentials: "include",
127
- keepalive: keepAlive,
128
- headers: { "Content-Type": "application/json" },
129
- body: JSON.stringify({
130
- eventName,
131
- productIdentifier: productId,
132
- targetProductIdentifier : targetProductId,
133
- properties: allProperties
134
- })
135
- });
136
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
137
- } catch (error: unknown) {
138
- // Do nothing...
139
- }
140
- };
141
-
142
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
143
- // @ts-ignore
144
- globalThis[TrackingFunctionName] = trackFunction;
81
+ setMixpanelContext({
82
+ productId,
83
+ endpoint,
84
+ superProperties: getSuperProperties(),
85
+ verbose
86
+ });
145
87
 
146
- return trackFunction;
88
+ if (verbose) {
89
+ console.log("[mixpanel] Mixpanel is initialized.");
90
+ }
147
91
  }
@@ -1,7 +1,8 @@
1
1
 
2
+ import type { TelemetryContext } from "@workleap/telemetry";
2
3
  import { browserName, browserVersion, deviceType, osName, referringDomain } from "./utils.ts";
3
4
 
4
- export type MixpanelTrackEventProperties = Record<string, unknown>;
5
+ export type MixpanelEventProperties = Record<string, unknown>;
5
6
 
6
7
  export const BaseProperties = {
7
8
  Browser: "$browser",
@@ -16,12 +17,6 @@ export const BaseProperties = {
16
17
  ScreenWidth: "$screen_width"
17
18
  } as const;
18
19
 
19
- export const TelemetryProperties = {
20
- DeviceId: "Device Id",
21
- LogRocketSessionUrl: "LogRocket Session Url",
22
- TelemetryId: "Telemetry Id"
23
- } as const;
24
-
25
20
  export function getBaseProperties() {
26
21
  const userAgent = navigator.userAgent;
27
22
  const device = deviceType(userAgent);
@@ -37,5 +32,48 @@ export function getBaseProperties() {
37
32
  [BaseProperties.ScreenHeight]: window.screen.height,
38
33
  [BaseProperties.ScreenWidth]: window.screen.width,
39
34
  [BaseProperties.IsMobile]: device.length > 0
40
- } satisfies MixpanelTrackEventProperties;
35
+ } satisfies MixpanelEventProperties;
36
+ }
37
+
38
+ export const TelemetryProperties = {
39
+ DeviceId: "Device Id",
40
+ TelemetryId: "Telemetry Id"
41
+ } as const;
42
+
43
+ export function getTelemetryProperties(context: TelemetryContext) {
44
+ return {
45
+ [TelemetryProperties.TelemetryId]: context.telemetryId,
46
+ [TelemetryProperties.DeviceId]: context.deviceId
47
+ } satisfies MixpanelEventProperties;
48
+ }
49
+
50
+ export const OtherProperties = {
51
+ LogRocketSessionUrl: "LogRocket Session Url"
52
+ };
53
+
54
+ //////////////////////
55
+
56
+ export type SuperProperties = Map<string, unknown>;
57
+
58
+ // Equivalent to: https://docs.mixpanel.com/docs/tracking-methods/sdks/javascript#setting-super-properties.
59
+ const superProperties: SuperProperties = new Map<string, unknown>();
60
+
61
+ export function setSuperProperty(key: string, value: unknown) {
62
+ superProperties.set(key, value);
63
+ }
64
+
65
+ export function setSuperProperties(values: Record<string, unknown>) {
66
+ Object.keys(values).forEach(x => {
67
+ superProperties.set(x, values[x]);
68
+ });
69
+ }
70
+
71
+ // Must always return the actual reference to the super properties, not a copy
72
+ // because it will breaks the global context.
73
+ export function getSuperProperties() {
74
+ return superProperties;
75
+ }
76
+
77
+ export function __clearSuperProperties() {
78
+ superProperties.clear();
41
79
  }
@@ -1,2 +1,2 @@
1
- export { useMixpanelTracking } from "./useMixpanelTracking.ts";
1
+ export { useTrackingFunction, type UseTrackingFunctionOptions } from "./useTrackingFunction.ts";
2
2
 
@@ -0,0 +1,14 @@
1
+ import { useMemo } from "react";
2
+ import { createTrackingFunction, type CreateTrackingFunctionOptions } from "../js/createTrackingFunction.ts";
3
+
4
+ export type UseTrackingFunctionOptions = CreateTrackingFunctionOptions;
5
+
6
+ export function useTrackingFunction(options: UseTrackingFunctionOptions = {}) {
7
+ const {
8
+ targetProductId
9
+ } = options;
10
+
11
+ return useMemo(() => {
12
+ return createTrackingFunction({ targetProductId });
13
+ }, [targetProductId]);
14
+ }
@@ -1,14 +0,0 @@
1
- import type { MixpanelTrackingFunction } from "./initializeMixpanel.ts";
2
- export declare const TrackingFunctionName = "__WLP_MIXPANEL_TRACKING_FUNCTION__";
3
- /**
4
- * @see https://workleap.github.io/wl-telemetry
5
- */
6
- export interface GetMixpanelTrackingFunctionOptions {
7
- throwOnUndefined?: boolean;
8
- }
9
- /**
10
- * Retrieve the track function.
11
- * @returns A function that sends tracking events to the tracking API.
12
- * @see https://workleap.github.io/wl-telemetry
13
- */
14
- export declare function getMixpanelTrackingFunction(options?: GetMixpanelTrackingFunctionOptions): MixpanelTrackingFunction;
@@ -1,21 +0,0 @@
1
-
2
- ;// CONCATENATED MODULE: ./src/js/getMixpanelTrackingFunction.ts
3
- const TrackingFunctionName = "__WLP_MIXPANEL_TRACKING_FUNCTION__";
4
- /**
5
- * Retrieve the track function.
6
- * @returns A function that sends tracking events to the tracking API.
7
- * @see https://workleap.github.io/wl-telemetry
8
- */ function getMixpanelTrackingFunction(options = {}) {
9
- const { throwOnUndefined = true } = options;
10
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
11
- // @ts-ignore
12
- const trackFunction = globalThis[TrackingFunctionName];
13
- if (!trackFunction && throwOnUndefined) {
14
- throw new Error("[mixpanel] The tracking function is undefined. Did you call the \"initializeMixpanel\" function?");
15
- }
16
- return trackFunction;
17
- }
18
-
19
- export { TrackingFunctionName, getMixpanelTrackingFunction };
20
-
21
- //# sourceMappingURL=getMixpanelTrackingFunction.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"js/getMixpanelTrackingFunction.js","sources":["webpack://@workleap/mixpanel/./src/js/getMixpanelTrackingFunction.ts"],"sourcesContent":["import type { MixpanelTrackingFunction } from \"./initializeMixpanel.ts\";\n\nexport const TrackingFunctionName = \"__WLP_MIXPANEL_TRACKING_FUNCTION__\";\n\n/**\n * @see https://workleap.github.io/wl-telemetry\n */\nexport interface GetMixpanelTrackingFunctionOptions {\n throwOnUndefined?: boolean;\n}\n\n/**\n * Retrieve the track function.\n * @returns A function that sends tracking events to the tracking API.\n * @see https://workleap.github.io/wl-telemetry\n */\nexport function getMixpanelTrackingFunction(options: GetMixpanelTrackingFunctionOptions = {}) {\n const {\n throwOnUndefined = true\n } = options;\n\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n const trackFunction = globalThis[TrackingFunctionName];\n\n if (!trackFunction && throwOnUndefined) {\n throw new Error(\"[mixpanel] The tracking function is undefined. Did you call the \\\"initializeMixpanel\\\" function?\");\n }\n\n return trackFunction as MixpanelTrackingFunction;\n}\n"],"names":["TrackingFunctionName","getMixpanelTrackingFunction","options","throwOnUndefined","trackFunction","globalThis","Error"],"mappings":";;AAEO,MAAMA,uBAAuB,qCAAqC;AASzE;;;;CAIC,GACM,SAASC,4BAA4BC,UAA8C,CAAC,CAAC;IACxF,MAAM,EACFC,mBAAmB,IAAI,EAC1B,GAAGD;IAEJ,6DAA6D;IAC7D,aAAa;IACb,MAAME,gBAAgBC,UAAU,CAACL,qBAAqB;IAEtD,IAAI,CAACI,iBAAiBD,kBAAkB;QACpC,MAAM,IAAIG,MAAM;IACpB;IAEA,OAAOF;AACX"}
@@ -1,11 +0,0 @@
1
- import { type GetMixpanelTrackingFunctionOptions } from "../js/getMixpanelTrackingFunction.ts";
2
- /**
3
- * @see https://workleap.github.io/wl-telemetry
4
- */
5
- export type UseMixpanelTrackingOptions = GetMixpanelTrackingFunctionOptions;
6
- /**
7
- * Retrieve the track function.
8
- * @returns A function that sends tracking events to the tracking API.
9
- * @see https://workleap.github.io/wl-telemetry
10
- */
11
- export declare function useMixpanelTracking(options?: UseMixpanelTrackingOptions): import("../js/initializeMixpanel.ts").MixpanelTrackingFunction;
@@ -1,17 +0,0 @@
1
- import { getMixpanelTrackingFunction } from "../js/getMixpanelTrackingFunction.js";
2
-
3
- ;// CONCATENATED MODULE: external "../js/getMixpanelTrackingFunction.js"
4
-
5
- ;// CONCATENATED MODULE: ./src/react/useMixpanelTracking.ts
6
-
7
- /**
8
- * Retrieve the track function.
9
- * @returns A function that sends tracking events to the tracking API.
10
- * @see https://workleap.github.io/wl-telemetry
11
- */ function useMixpanelTracking(options) {
12
- return getMixpanelTrackingFunction(options);
13
- }
14
-
15
- export { useMixpanelTracking };
16
-
17
- //# sourceMappingURL=useMixpanelTracking.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"react/useMixpanelTracking.js","sources":["webpack://@workleap/mixpanel/./src/react/useMixpanelTracking.ts"],"sourcesContent":["import { getMixpanelTrackingFunction, type GetMixpanelTrackingFunctionOptions } from \"../js/getMixpanelTrackingFunction.ts\";\n\n/**\n * @see https://workleap.github.io/wl-telemetry\n */\nexport type UseMixpanelTrackingOptions = GetMixpanelTrackingFunctionOptions;\n\n/**\n * Retrieve the track function.\n * @returns A function that sends tracking events to the tracking API.\n * @see https://workleap.github.io/wl-telemetry\n */\nexport function useMixpanelTracking(options?: UseMixpanelTrackingOptions) {\n return getMixpanelTrackingFunction(options);\n}\n"],"names":["getMixpanelTrackingFunction","useMixpanelTracking","options"],"mappings":";;;;;AAA4H;AAO5H;;;;CAIC,GACM,SAASC,oBAAoBC,OAAoC;IACpE,OAAOF,2BAA2BA,CAACE;AACvC"}
@@ -1,31 +0,0 @@
1
- import type { MixpanelTrackingFunction } from "./initializeMixpanel.ts";
2
-
3
- export const TrackingFunctionName = "__WLP_MIXPANEL_TRACKING_FUNCTION__";
4
-
5
- /**
6
- * @see https://workleap.github.io/wl-telemetry
7
- */
8
- export interface GetMixpanelTrackingFunctionOptions {
9
- throwOnUndefined?: boolean;
10
- }
11
-
12
- /**
13
- * Retrieve the track function.
14
- * @returns A function that sends tracking events to the tracking API.
15
- * @see https://workleap.github.io/wl-telemetry
16
- */
17
- export function getMixpanelTrackingFunction(options: GetMixpanelTrackingFunctionOptions = {}) {
18
- const {
19
- throwOnUndefined = true
20
- } = options;
21
-
22
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
23
- // @ts-ignore
24
- const trackFunction = globalThis[TrackingFunctionName];
25
-
26
- if (!trackFunction && throwOnUndefined) {
27
- throw new Error("[mixpanel] The tracking function is undefined. Did you call the \"initializeMixpanel\" function?");
28
- }
29
-
30
- return trackFunction as MixpanelTrackingFunction;
31
- }
@@ -1,15 +0,0 @@
1
- import { getMixpanelTrackingFunction, type GetMixpanelTrackingFunctionOptions } from "../js/getMixpanelTrackingFunction.ts";
2
-
3
- /**
4
- * @see https://workleap.github.io/wl-telemetry
5
- */
6
- export type UseMixpanelTrackingOptions = GetMixpanelTrackingFunctionOptions;
7
-
8
- /**
9
- * Retrieve the track function.
10
- * @returns A function that sends tracking events to the tracking API.
11
- * @see https://workleap.github.io/wl-telemetry
12
- */
13
- export function useMixpanelTracking(options?: UseMixpanelTrackingOptions) {
14
- return getMixpanelTrackingFunction(options);
15
- }