@workleap/mixpanel 2.1.8 → 3.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 +18 -0
- package/dist/js/MixpanelClient.d.ts +22 -0
- package/dist/js/MixpanelClient.js +71 -0
- package/dist/js/MixpanelClient.js.map +1 -0
- package/dist/js/context.d.ts +2 -2
- package/dist/js/context.js +3 -0
- package/dist/js/context.js.map +1 -1
- package/dist/js/createTrackingFunction.d.ts +4 -4
- package/dist/js/createTrackingFunction.js +5 -2
- package/dist/js/createTrackingFunction.js.map +1 -1
- package/dist/js/env.d.ts +2 -2
- package/dist/js/env.js.map +1 -1
- package/dist/js/index.d.ts +5 -3
- package/dist/js/index.js +7 -3
- package/dist/js/index.js.map +1 -1
- package/dist/js/initializeMixpanel.d.ts +28 -7
- package/dist/js/initializeMixpanel.js +67 -56
- package/dist/js/initializeMixpanel.js.map +1 -1
- package/dist/js/properties.d.ts +1 -6
- package/dist/js/properties.js +1 -19
- package/dist/js/properties.js.map +1 -1
- package/dist/react/MixpanelContext.d.ts +18 -0
- package/dist/react/MixpanelContext.js +34 -0
- package/dist/react/MixpanelContext.js.map +1 -0
- package/dist/react/index.d.ts +2 -0
- package/dist/react/index.js +7 -1
- package/dist/react/index.js.map +1 -1
- package/dist/react/useTrackingFunction.d.ts +10 -3
- package/dist/react/useTrackingFunction.js +17 -4
- package/dist/react/useTrackingFunction.js.map +1 -1
- package/package.json +15 -16
- package/src/js/MixpanelClient.ts +85 -0
- package/src/js/context.ts +6 -2
- package/src/js/createTrackingFunction.ts +11 -7
- package/src/js/env.ts +5 -6
- package/src/js/index.ts +5 -3
- package/src/js/initializeMixpanel.ts +123 -73
- package/src/js/properties.ts +1 -28
- package/src/react/MixpanelContext.tsx +41 -0
- package/src/react/index.ts +2 -0
- package/src/react/useTrackingFunction.ts +24 -5
- package/dist/js/HasExecutedGuard.d.ts +0 -6
- package/dist/js/HasExecutedGuard.js +0 -21
- package/dist/js/HasExecutedGuard.js.map +0 -1
- package/dist/js/internal.d.ts +0 -2
- package/dist/js/internal.js +0 -14
- package/dist/js/internal.js.map +0 -1
- package/src/js/HasExecutedGuard.ts +0 -19
- package/src/js/internal.ts +0 -3
package/dist/js/properties.js
CHANGED
|
@@ -45,25 +45,7 @@ function getTelemetryProperties(context) {
|
|
|
45
45
|
const OtherProperties = {
|
|
46
46
|
LogRocketSessionUrl: "LogRocket Session Url"
|
|
47
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
|
-
}
|
|
66
48
|
|
|
67
|
-
export { BaseProperties, OtherProperties, TelemetryProperties,
|
|
49
|
+
export { BaseProperties, OtherProperties, TelemetryProperties, getBaseProperties, getTelemetryProperties };
|
|
68
50
|
|
|
69
51
|
//# sourceMappingURL=properties.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"js/properties.js","sources":["webpack://@workleap/mixpanel/./src/js/properties.ts"],"sourcesContent":["\nimport type { TelemetryContext } from \"@workleap/
|
|
1
|
+
{"version":3,"file":"js/properties.js","sources":["webpack://@workleap/mixpanel/./src/js/properties.ts"],"sourcesContent":["\nimport type { TelemetryContext } from \"@workleap-telemetry/core\";\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"],"names":["browserName","browserVersion","deviceType","osName","referringDomain","BaseProperties","getBaseProperties","userAgent","navigator","device","document","window","TelemetryProperties","getTelemetryProperties","context","OtherProperties"],"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"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type PropsWithChildren } from "react";
|
|
2
|
+
import type { MixpanelClient } from "../js/MixpanelClient.ts";
|
|
3
|
+
export declare const MixpanelContext: import("react").Context<MixpanelClient | undefined>;
|
|
4
|
+
/**
|
|
5
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
6
|
+
*/
|
|
7
|
+
export interface MixpanelProviderProps extends PropsWithChildren {
|
|
8
|
+
client: MixpanelClient;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
12
|
+
*/
|
|
13
|
+
export declare function MixpanelProvider(props: MixpanelProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
14
|
+
/**
|
|
15
|
+
* Retrieve the Mixpanel client.
|
|
16
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
17
|
+
*/
|
|
18
|
+
export declare function useMixpanelClient(): MixpanelClient;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useContext } from "react";
|
|
3
|
+
|
|
4
|
+
;// CONCATENATED MODULE: external "react/jsx-runtime"
|
|
5
|
+
|
|
6
|
+
;// CONCATENATED MODULE: external "react"
|
|
7
|
+
|
|
8
|
+
;// CONCATENATED MODULE: ./src/react/MixpanelContext.tsx
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
const MixpanelContext = /*#__PURE__*/ createContext(undefined);
|
|
12
|
+
/**
|
|
13
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
14
|
+
*/ function MixpanelProvider(props) {
|
|
15
|
+
const { client, children } = props;
|
|
16
|
+
return /*#__PURE__*/ jsx(MixpanelContext.Provider, {
|
|
17
|
+
value: client,
|
|
18
|
+
children: children
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Retrieve the Mixpanel client.
|
|
23
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
24
|
+
*/ function useMixpanelClient() {
|
|
25
|
+
const client = useContext(MixpanelContext);
|
|
26
|
+
if (!client) {
|
|
27
|
+
throw new Error("[mixpanel] The useMixpanelClient function is called before a MixpanelClient instance has been provided.");
|
|
28
|
+
}
|
|
29
|
+
return client;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { MixpanelContext, MixpanelProvider, useMixpanelClient };
|
|
33
|
+
|
|
34
|
+
//# sourceMappingURL=MixpanelContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react/MixpanelContext.js","sources":["webpack://@workleap/mixpanel/./src/react/MixpanelContext.tsx"],"sourcesContent":["import { createContext, type PropsWithChildren, useContext } from \"react\";\nimport type { MixpanelClient } from \"../js/MixpanelClient.ts\";\n\nexport const MixpanelContext = createContext<MixpanelClient | undefined>(undefined);\n\n/**\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\nexport interface MixpanelProviderProps extends PropsWithChildren {\n client: MixpanelClient;\n}\n\n/**\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\nexport function MixpanelProvider(props: MixpanelProviderProps) {\n const {\n client,\n children\n } = props;\n\n return (\n <MixpanelContext.Provider value={client}>\n {children}\n </MixpanelContext.Provider>\n );\n}\n\n/**\n * Retrieve the Mixpanel client.\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\nexport function useMixpanelClient() {\n const client = useContext(MixpanelContext);\n\n if (!client) {\n throw new Error(\"[mixpanel] The useMixpanelClient function is called before a MixpanelClient instance has been provided.\");\n }\n\n return client;\n}\n"],"names":["createContext","useContext","MixpanelContext","undefined","MixpanelProvider","props","client","children","useMixpanelClient","Error"],"mappings":";;;;;;;;;AAA0E;AAGnE,MAAME,gCAAkBF,aAAaA,CAA6BG,WAAW;AASpF;;CAEC,GACM,SAASC,iBAAiBC,KAA4B;IACzD,MAAM,EACFC,MAAM,EACNC,QAAQ,EACX,GAAGF;IAEJ,qBACI,IAACH,gBAAgB,QAAQ;QAAC,OAAOI;kBAC5BC;;AAGb;AAEA;;;CAGC,GACM,SAASC;IACZ,MAAMF,SAASL,UAAUA,CAACC;IAE1B,IAAI,CAACI,QAAQ;QACT,MAAM,IAAIG,MAAM;IACpB;IAEA,OAAOH;AACX"}
|
package/dist/react/index.d.ts
CHANGED
package/dist/react/index.js
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
|
+
import { MixpanelProvider, useMixpanelClient } from "./MixpanelContext.js";
|
|
1
2
|
import { useTrackingFunction } from "./useTrackingFunction.js";
|
|
3
|
+
export * from "../js/index.js";
|
|
4
|
+
|
|
5
|
+
;// CONCATENATED MODULE: external "./MixpanelContext.js"
|
|
2
6
|
|
|
3
7
|
;// CONCATENATED MODULE: external "./useTrackingFunction.js"
|
|
4
8
|
|
|
5
9
|
;// CONCATENATED MODULE: ./src/react/index.ts
|
|
6
10
|
|
|
7
11
|
|
|
8
|
-
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
export { MixpanelProvider, useMixpanelClient, useTrackingFunction };
|
|
9
15
|
|
|
10
16
|
//# sourceMappingURL=index.js.map
|
package/dist/react/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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":"
|
|
1
|
+
{"version":3,"file":"react/index.js","sources":["webpack://@workleap/mixpanel/./src/react/index.ts"],"sourcesContent":["export * from \"../js/index.ts\";\nexport { MixpanelProvider, useMixpanelClient } from \"./MixpanelContext.tsx\";\nexport { useTrackingFunction, type UseTrackingFunctionOptions } from \"./useTrackingFunction.ts\";\n\n"],"names":["MixpanelProvider","useMixpanelClient","useTrackingFunction"],"mappings":";;;;;;;;;AAA+B;AAC6C;AACoB"}
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { type CreateMixpanelTrackingFunctionOptions } from "../js/createTrackingFunction.ts";
|
|
2
|
+
/**
|
|
3
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
4
|
+
*/
|
|
5
|
+
export type UseTrackingFunctionOptions = CreateMixpanelTrackingFunctionOptions;
|
|
6
|
+
/**
|
|
7
|
+
* Create a Mixpanel tracking function.
|
|
8
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
9
|
+
*/
|
|
10
|
+
export declare function useTrackingFunction(options?: UseTrackingFunctionOptions): import("../js/createTrackingFunction.ts").MixpanelTrackingFunction;
|
|
@@ -1,20 +1,33 @@
|
|
|
1
|
-
import { useMemo } from "react";
|
|
1
|
+
import { useContext, useMemo } from "react";
|
|
2
2
|
import { createTrackingFunction } from "../js/createTrackingFunction.js";
|
|
3
|
+
import { MixpanelContext } from "./MixpanelContext.js";
|
|
3
4
|
|
|
4
5
|
;// CONCATENATED MODULE: external "react"
|
|
5
6
|
|
|
6
7
|
;// CONCATENATED MODULE: external "../js/createTrackingFunction.js"
|
|
7
8
|
|
|
9
|
+
;// CONCATENATED MODULE: external "./MixpanelContext.js"
|
|
10
|
+
|
|
8
11
|
;// CONCATENATED MODULE: ./src/react/useTrackingFunction.ts
|
|
9
12
|
|
|
10
13
|
|
|
11
|
-
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Create a Mixpanel tracking function.
|
|
17
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
18
|
+
*/ function useTrackingFunction(options = {}) {
|
|
12
19
|
const { targetProductId } = options;
|
|
20
|
+
const client = useContext(MixpanelContext);
|
|
13
21
|
return useMemo(()=>{
|
|
14
|
-
|
|
22
|
+
const trackOptions = {
|
|
15
23
|
targetProductId
|
|
16
|
-
}
|
|
24
|
+
};
|
|
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);
|
|
17
29
|
}, [
|
|
30
|
+
client,
|
|
18
31
|
targetProductId
|
|
19
32
|
]);
|
|
20
33
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react/useTrackingFunction.js","sources":["webpack://@workleap/mixpanel/./src/react/useTrackingFunction.ts"],"sourcesContent":["import { useMemo } from \"react\";\nimport { createTrackingFunction, type
|
|
1
|
+
{"version":3,"file":"react/useTrackingFunction.js","sources":["webpack://@workleap/mixpanel/./src/react/useTrackingFunction.ts"],"sourcesContent":["import { useContext, useMemo } from \"react\";\nimport { createTrackingFunction, type CreateMixpanelTrackingFunctionOptions } 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":";;;;;;;;;;;AAA4C;AACyE;AAC7D;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"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@workleap/mixpanel",
|
|
3
3
|
"author": "Workleap",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "3.0.0",
|
|
5
5
|
"description": "A package containing utilities for Mixpanel telemetry.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
7
7
|
"repository": {
|
|
@@ -24,11 +24,6 @@
|
|
|
24
24
|
"types": "./dist/react/index.d.ts",
|
|
25
25
|
"import": "./dist/react/index.js",
|
|
26
26
|
"default": "./dist/react/index.js"
|
|
27
|
-
},
|
|
28
|
-
"./internal": {
|
|
29
|
-
"types": "./dist/internal.d.ts",
|
|
30
|
-
"import": "./dist/internal.js",
|
|
31
|
-
"default": "./dist/internal.js"
|
|
32
27
|
}
|
|
33
28
|
},
|
|
34
29
|
"files": [
|
|
@@ -39,9 +34,13 @@
|
|
|
39
34
|
],
|
|
40
35
|
"peerDependencies": {
|
|
41
36
|
"react": "^18.0.0 || ^19.0.0",
|
|
42
|
-
"react-dom": "^18.0.0 || ^19.0.0"
|
|
37
|
+
"react-dom": "^18.0.0 || ^19.0.0",
|
|
38
|
+
"@workleap/logrocket": "2.0.0"
|
|
43
39
|
},
|
|
44
40
|
"peerDependenciesMeta": {
|
|
41
|
+
"@workleap/logrocket": {
|
|
42
|
+
"optional": true
|
|
43
|
+
},
|
|
45
44
|
"react": {
|
|
46
45
|
"optional": true
|
|
47
46
|
},
|
|
@@ -50,19 +49,19 @@
|
|
|
50
49
|
}
|
|
51
50
|
},
|
|
52
51
|
"dependencies": {
|
|
53
|
-
"@workleap/logging": "^1.
|
|
54
|
-
"@workleap/
|
|
52
|
+
"@workleap/logging": "^1.3.0",
|
|
53
|
+
"@workleap-telemetry/core": "1.0.0"
|
|
55
54
|
},
|
|
56
55
|
"devDependencies": {
|
|
57
|
-
"@rsbuild/core": "1.
|
|
58
|
-
"@rslib/core": "0.
|
|
59
|
-
"@types/react": "19.
|
|
60
|
-
"@workleap/eslint-plugin": "3.
|
|
61
|
-
"@workleap/rslib-configs": "1.0
|
|
56
|
+
"@rsbuild/core": "1.5.13",
|
|
57
|
+
"@rslib/core": "0.15.0",
|
|
58
|
+
"@types/react": "19.2.0",
|
|
59
|
+
"@workleap/eslint-plugin": "3.5.0",
|
|
60
|
+
"@workleap/rslib-configs": "1.1.0",
|
|
62
61
|
"@workleap/typescript-configs": "3.0.4",
|
|
63
62
|
"eslint": "8.57.1",
|
|
64
|
-
"happy-dom": "
|
|
65
|
-
"typescript": "5.
|
|
63
|
+
"happy-dom": "19.0.2",
|
|
64
|
+
"typescript": "5.9.3",
|
|
66
65
|
"vitest": "3.2.4"
|
|
67
66
|
},
|
|
68
67
|
"sideEffects": false,
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { Logger } from "@workleap/logging";
|
|
2
|
+
import type { CreateMixpanelTrackingFunctionOptions, MixpanelTrackingFunction } from "./createTrackingFunction.ts";
|
|
3
|
+
import { getBaseProperties } from "./properties.ts";
|
|
4
|
+
|
|
5
|
+
export type MixpanelGlobalEventProperties = Map<string, unknown>;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
9
|
+
*/
|
|
10
|
+
export class MixpanelClient {
|
|
11
|
+
readonly #productId: string;
|
|
12
|
+
readonly #endpoint: string;
|
|
13
|
+
readonly #globalEventProperties: MixpanelGlobalEventProperties;
|
|
14
|
+
readonly #logger: Logger;
|
|
15
|
+
|
|
16
|
+
constructor(productId: string, endpoint: string, globalEventProperties: MixpanelGlobalEventProperties, logger: Logger) {
|
|
17
|
+
this.#productId = productId;
|
|
18
|
+
this.#endpoint = endpoint;
|
|
19
|
+
this.#globalEventProperties = globalEventProperties;
|
|
20
|
+
this.#logger = logger;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
25
|
+
*/
|
|
26
|
+
createTrackingFunction(options: CreateMixpanelTrackingFunctionOptions = {}) {
|
|
27
|
+
const {
|
|
28
|
+
targetProductId
|
|
29
|
+
} = options;
|
|
30
|
+
|
|
31
|
+
const trackFunction: MixpanelTrackingFunction = async (eventName, properties, _options = {}) => {
|
|
32
|
+
try {
|
|
33
|
+
const {
|
|
34
|
+
keepAlive = false
|
|
35
|
+
} = _options;
|
|
36
|
+
|
|
37
|
+
const baseProperties = getBaseProperties();
|
|
38
|
+
|
|
39
|
+
const allProperties = {
|
|
40
|
+
...baseProperties,
|
|
41
|
+
...Object.fromEntries(this.#globalEventProperties),
|
|
42
|
+
...properties
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
await fetch(this.#endpoint, {
|
|
46
|
+
method: "POST",
|
|
47
|
+
credentials: "include",
|
|
48
|
+
keepalive: keepAlive,
|
|
49
|
+
headers: { "Content-Type": "application/json" },
|
|
50
|
+
body: JSON.stringify({
|
|
51
|
+
eventName,
|
|
52
|
+
productIdentifier: this.#productId,
|
|
53
|
+
// Not sure why, but it seems important to send "null" if not target product identifier
|
|
54
|
+
// are provided.
|
|
55
|
+
targetProductIdentifier : targetProductId ?? null,
|
|
56
|
+
properties: allProperties
|
|
57
|
+
})
|
|
58
|
+
});
|
|
59
|
+
} catch (error: unknown) {
|
|
60
|
+
this.#logger
|
|
61
|
+
.withText(`[mixpanel] An error occurred while sending a tracking event to "${this.#endpoint}":`)
|
|
62
|
+
.withError(error as Error)
|
|
63
|
+
.error();
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return trackFunction;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
72
|
+
*/
|
|
73
|
+
setGlobalEventProperty(key: string, value: unknown) {
|
|
74
|
+
this.#globalEventProperties.set(key, value);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
79
|
+
*/
|
|
80
|
+
setGlobalEventProperties(values: Record<string, unknown>) {
|
|
81
|
+
Object.keys(values).forEach(x => {
|
|
82
|
+
this.#globalEventProperties.set(x, values[x]);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
package/src/js/context.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
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
|
+
|
|
1
5
|
import type { Logger } from "@workleap/logging";
|
|
2
|
-
import type {
|
|
6
|
+
import type { MixpanelGlobalEventProperties } from "./MixpanelClient.ts";
|
|
3
7
|
|
|
4
8
|
export const MixpanelContextVariableName = "__WLP_MIXPANEL_CONTEXT__";
|
|
5
9
|
|
|
6
10
|
export interface MixpanelContext {
|
|
7
11
|
productId: string;
|
|
8
12
|
endpoint: string;
|
|
9
|
-
|
|
13
|
+
globalEventProperties: MixpanelGlobalEventProperties;
|
|
10
14
|
logger: Logger;
|
|
11
15
|
}
|
|
12
16
|
|
|
@@ -1,10 +1,14 @@
|
|
|
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
|
+
|
|
1
5
|
import { getMixpanelContext } from "./context.ts";
|
|
2
6
|
import { getBaseProperties, type MixpanelEventProperties } from "./properties.ts";
|
|
3
7
|
|
|
4
8
|
/**
|
|
5
9
|
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
6
10
|
*/
|
|
7
|
-
export interface
|
|
11
|
+
export interface CreateMixpanelTrackingFunctionOptions {
|
|
8
12
|
/**
|
|
9
13
|
* The product identifier of the target product.
|
|
10
14
|
*/
|
|
@@ -14,7 +18,7 @@ export interface CreateTrackingFunctionOptions {
|
|
|
14
18
|
/**
|
|
15
19
|
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
16
20
|
*/
|
|
17
|
-
export interface
|
|
21
|
+
export interface MixpanelTrackingFunctionOptions {
|
|
18
22
|
/**
|
|
19
23
|
* Whether to keep the connection alive for the tracking request.
|
|
20
24
|
* It is mostly used for tracking links where the user might navigate away before the request is completed.
|
|
@@ -35,12 +39,12 @@ export interface TrackingFunctionOptions {
|
|
|
35
39
|
* @param options Options for tracking the event.
|
|
36
40
|
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
37
41
|
*/
|
|
38
|
-
export type
|
|
42
|
+
export type MixpanelTrackingFunction = (eventName: string, properties: MixpanelEventProperties, options?: MixpanelTrackingFunctionOptions) => Promise<void>;
|
|
39
43
|
|
|
40
44
|
/**
|
|
41
45
|
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
42
46
|
*/
|
|
43
|
-
export function createTrackingFunction(options:
|
|
47
|
+
export function createTrackingFunction(options: CreateMixpanelTrackingFunctionOptions = {}) {
|
|
44
48
|
const {
|
|
45
49
|
targetProductId
|
|
46
50
|
} = options;
|
|
@@ -48,11 +52,11 @@ export function createTrackingFunction(options: CreateTrackingFunctionOptions =
|
|
|
48
52
|
const {
|
|
49
53
|
productId,
|
|
50
54
|
endpoint,
|
|
51
|
-
|
|
55
|
+
globalEventProperties,
|
|
52
56
|
logger
|
|
53
57
|
} = getMixpanelContext();
|
|
54
58
|
|
|
55
|
-
const trackFunction:
|
|
59
|
+
const trackFunction: MixpanelTrackingFunction = async (eventName, properties, _options = {}) => {
|
|
56
60
|
try {
|
|
57
61
|
const {
|
|
58
62
|
keepAlive = false
|
|
@@ -62,7 +66,7 @@ export function createTrackingFunction(options: CreateTrackingFunctionOptions =
|
|
|
62
66
|
|
|
63
67
|
const allProperties = {
|
|
64
68
|
...baseProperties,
|
|
65
|
-
...Object.fromEntries(
|
|
69
|
+
...Object.fromEntries(globalEventProperties),
|
|
66
70
|
...properties
|
|
67
71
|
};
|
|
68
72
|
|
package/src/js/env.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
|
|
2
1
|
const EnvironmentList = ["development", "staging", "production", "local", "msw"] as const;
|
|
3
2
|
|
|
4
|
-
export type
|
|
3
|
+
export type MixpanelEnvironment = typeof EnvironmentList[number];
|
|
5
4
|
|
|
6
|
-
const NavigationApiBaseUrl: Record<
|
|
5
|
+
const NavigationApiBaseUrl: Record<MixpanelEnvironment, string> = {
|
|
7
6
|
"production": "https://api.platform.workleap.com/shell/navigation/",
|
|
8
7
|
"staging": "https://api.platform.workleap-stg.com/shell/navigation/",
|
|
9
8
|
"development": "https://api.platform.workleap-dev.com/shell/navigation/",
|
|
@@ -16,11 +15,11 @@ function resolveApiUrl(path: string, baseUrl: string | undefined) : string {
|
|
|
16
15
|
return `${baseUrl}${baseUrl!.endsWith("/") ? "" : "/"}${path.startsWith("/") ? path.substring(1) : path}`;
|
|
17
16
|
}
|
|
18
17
|
|
|
19
|
-
export function getTrackingEndpoint(envOrTrackingApiBaseUrl:
|
|
18
|
+
export function getTrackingEndpoint(envOrTrackingApiBaseUrl: MixpanelEnvironment | (string & {}), trackingEndpoint = "tracking/track"): string {
|
|
20
19
|
let baseUrl: string;
|
|
21
20
|
|
|
22
|
-
if (EnvironmentList.includes(envOrTrackingApiBaseUrl as
|
|
23
|
-
baseUrl = NavigationApiBaseUrl[envOrTrackingApiBaseUrl as
|
|
21
|
+
if (EnvironmentList.includes(envOrTrackingApiBaseUrl as MixpanelEnvironment)) {
|
|
22
|
+
baseUrl = NavigationApiBaseUrl[envOrTrackingApiBaseUrl as MixpanelEnvironment];
|
|
24
23
|
} else {
|
|
25
24
|
baseUrl = envOrTrackingApiBaseUrl;
|
|
26
25
|
}
|
package/src/js/index.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export type
|
|
1
|
+
export { createTelemetryContext, type CreateTelemetryContextOptions, type TelemetryContext } from "@workleap-telemetry/core";
|
|
2
|
+
export { createTrackingFunction, type CreateMixpanelTrackingFunctionOptions, type MixpanelTrackingFunction, type MixpanelTrackingFunctionOptions } from "./createTrackingFunction.ts";
|
|
3
|
+
export type { MixpanelEnvironment } from "./env.ts";
|
|
3
4
|
export { initializeMixpanel, type InitializeMixpanelOptions } from "./initializeMixpanel.ts";
|
|
4
|
-
export {
|
|
5
|
+
export { MixpanelClient, type MixpanelGlobalEventProperties } from "./MixpanelClient.ts";
|
|
6
|
+
export { type MixpanelEventProperties } from "./properties.ts";
|
|
5
7
|
|