@workleap/mixpanel 2.0.2 → 2.1.1
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 +20 -0
- package/dist/js/context.d.ts +3 -2
- package/dist/js/context.js +4 -4
- package/dist/js/context.js.map +1 -1
- package/dist/js/createTrackingFunction.d.ts +6 -3
- package/dist/js/createTrackingFunction.js +5 -4
- package/dist/js/createTrackingFunction.js.map +1 -1
- package/dist/js/initializeMixpanel.d.ts +11 -4
- package/dist/js/initializeMixpanel.js +12 -11
- package/dist/js/initializeMixpanel.js.map +1 -1
- package/package.json +6 -3
- package/src/js/context.ts +5 -4
- package/src/js/createTrackingFunction.ts +12 -6
- package/src/js/initializeMixpanel.ts +20 -12
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# @workleap/mixpanel
|
|
2
2
|
|
|
3
|
+
## 2.1.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#54](https://github.com/workleap/wl-telemetry/pull/54) [`79ce028`](https://github.com/workleap/wl-telemetry/commit/79ce028f2418cc43ed18017e82ce9599e97d0e40) Thanks [@patricklafrance](https://github.com/patricklafrance)! - Added support for line change segments to the LogRocket logger.
|
|
8
|
+
|
|
9
|
+
- Updated dependencies [[`79ce028`](https://github.com/workleap/wl-telemetry/commit/79ce028f2418cc43ed18017e82ce9599e97d0e40)]:
|
|
10
|
+
- @workleap/telemetry@1.1.1
|
|
11
|
+
|
|
12
|
+
## 2.1.0
|
|
13
|
+
|
|
14
|
+
### Minor Changes
|
|
15
|
+
|
|
16
|
+
- [#52](https://github.com/workleap/wl-telemetry/pull/52) [`4affd67`](https://github.com/workleap/wl-telemetry/commit/4affd670d7c5c0495eb41a700e6fe9af1f9f4e0f) Thanks [@patricklafrance](https://github.com/patricklafrance)! - Added a `loggers` option and removed the peerDependency to `@workleap/telemetry`.
|
|
17
|
+
|
|
18
|
+
### Patch Changes
|
|
19
|
+
|
|
20
|
+
- Updated dependencies [[`4affd67`](https://github.com/workleap/wl-telemetry/commit/4affd670d7c5c0495eb41a700e6fe9af1f9f4e0f)]:
|
|
21
|
+
- @workleap/telemetry@1.1.0
|
|
22
|
+
|
|
3
23
|
## 2.0.2
|
|
4
24
|
|
|
5
25
|
### Patch Changes
|
package/dist/js/context.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import type { Logger } from "@workleap/logging";
|
|
1
2
|
import type { SuperProperties } from "./properties.ts";
|
|
2
|
-
export declare const
|
|
3
|
+
export declare const MixpanelContextVariableName = "__WLP_MIXPANEL_CONTEXT__";
|
|
3
4
|
export interface MixpanelContext {
|
|
4
5
|
productId: string;
|
|
5
6
|
endpoint: string;
|
|
6
7
|
superProperties: SuperProperties;
|
|
7
|
-
|
|
8
|
+
logger: Logger;
|
|
8
9
|
}
|
|
9
10
|
export declare function setMixpanelContext(context: MixpanelContext): void;
|
|
10
11
|
export declare function getMixpanelContext(): MixpanelContext;
|
package/dist/js/context.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
|
|
2
2
|
;// CONCATENATED MODULE: ./src/js/context.ts
|
|
3
|
-
const
|
|
3
|
+
const MixpanelContextVariableName = "__WLP_MIXPANEL_CONTEXT__";
|
|
4
4
|
function setMixpanelContext(context) {
|
|
5
5
|
// Will be used by the "track" function.
|
|
6
6
|
// It's important to share the context with a global DOM object rather than
|
|
@@ -8,18 +8,18 @@ function setMixpanelContext(context) {
|
|
|
8
8
|
// of this package than the host application.
|
|
9
9
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
10
10
|
// @ts-ignore
|
|
11
|
-
globalThis[
|
|
11
|
+
globalThis[MixpanelContextVariableName] = context;
|
|
12
12
|
}
|
|
13
13
|
function getMixpanelContext() {
|
|
14
14
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
15
15
|
// @ts-ignore
|
|
16
|
-
const context = globalThis[
|
|
16
|
+
const context = globalThis[MixpanelContextVariableName];
|
|
17
17
|
if (!context) {
|
|
18
18
|
throw new Error("[mixpanel] The Mixpanel context is not available. Did you initialize Mixpanel with the \"initializeMixpanel\" function?");
|
|
19
19
|
}
|
|
20
20
|
return context;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
export {
|
|
23
|
+
export { MixpanelContextVariableName, getMixpanelContext, setMixpanelContext };
|
|
24
24
|
|
|
25
25
|
//# sourceMappingURL=context.js.map
|
package/dist/js/context.js.map
CHANGED
|
@@ -1 +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
|
|
1
|
+
{"version":3,"file":"js/context.js","sources":["webpack://@workleap/mixpanel/./src/js/context.ts"],"sourcesContent":["import type { Logger } from \"@workleap/logging\";\nimport type { SuperProperties } from \"./properties.ts\";\n\nexport const MixpanelContextVariableName = \"__WLP_MIXPANEL_CONTEXT__\";\n\nexport interface MixpanelContext {\n productId: string;\n endpoint: string;\n superProperties: SuperProperties;\n logger: Logger;\n}\n\nexport function setMixpanelContext(context: MixpanelContext) {\n // Will be used by the \"track\" function.\n // It's important to share the context with a global DOM object rather than\n // a singleton because projects like the platform widgets will use a distinct instance\n // of this package than the host application.\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n globalThis[MixpanelContextVariableName] = context;\n}\n\nexport function getMixpanelContext() {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n const context = globalThis[MixpanelContextVariableName] as MixpanelContext;\n\n if (!context) {\n throw new Error(\"[mixpanel] The Mixpanel context is not available. Did you initialize Mixpanel with the \\\"initializeMixpanel\\\" function?\");\n }\n\n return context;\n}\n\n\n"],"names":["MixpanelContextVariableName","setMixpanelContext","context","globalThis","getMixpanelContext","Error"],"mappings":";;AAGO,MAAMA,8BAA8B,2BAA2B;AAS/D,SAASC,mBAAmBC,OAAwB;IACvD,wCAAwC;IACxC,2EAA2E;IAC3E,sFAAsF;IACtF,6CAA6C;IAC7C,6DAA6D;IAC7D,aAAa;IACbC,UAAU,CAACH,4BAA4B,GAAGE;AAC9C;AAEO,SAASE;IACZ,6DAA6D;IAC7D,aAAa;IACb,MAAMF,UAAUC,UAAU,CAACH,4BAA4B;IAEvD,IAAI,CAACE,SAAS;QACV,MAAM,IAAIG,MAAM;IACpB;IAEA,OAAOH;AACX"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type MixpanelEventProperties } from "./properties.ts";
|
|
2
2
|
/**
|
|
3
|
-
* @see https://workleap.github.io/wl-telemetry
|
|
3
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
4
4
|
*/
|
|
5
5
|
export interface CreateTrackingFunctionOptions {
|
|
6
6
|
/**
|
|
@@ -9,7 +9,7 @@ export interface CreateTrackingFunctionOptions {
|
|
|
9
9
|
targetProductId?: string;
|
|
10
10
|
}
|
|
11
11
|
/**
|
|
12
|
-
* @see https://workleap.github.io/wl-telemetry
|
|
12
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
13
13
|
*/
|
|
14
14
|
export interface TrackingFunctionOptions {
|
|
15
15
|
/**
|
|
@@ -29,7 +29,10 @@ export interface TrackingFunctionOptions {
|
|
|
29
29
|
* @param eventName The name of the event to track.
|
|
30
30
|
* @param properties The properties to send with the event.
|
|
31
31
|
* @param options Options for tracking the event.
|
|
32
|
-
* @see https://workleap.github.io/wl-telemetry
|
|
32
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
33
33
|
*/
|
|
34
34
|
export type TrackingFunction = (eventName: string, properties: MixpanelEventProperties, options?: TrackingFunctionOptions) => Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
37
|
+
*/
|
|
35
38
|
export declare function createTrackingFunction(options?: CreateTrackingFunctionOptions): TrackingFunction;
|
|
@@ -8,9 +8,11 @@ import { getBaseProperties } from "./properties.js";
|
|
|
8
8
|
;// CONCATENATED MODULE: ./src/js/createTrackingFunction.ts
|
|
9
9
|
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
/**
|
|
12
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
13
|
+
*/ function createTrackingFunction(options = {}) {
|
|
12
14
|
const { targetProductId } = options;
|
|
13
|
-
const { productId, endpoint, superProperties } = getMixpanelContext();
|
|
15
|
+
const { productId, endpoint, superProperties, logger } = getMixpanelContext();
|
|
14
16
|
const trackFunction = async (eventName, properties, _options = {})=>{
|
|
15
17
|
try {
|
|
16
18
|
const { keepAlive = false } = _options;
|
|
@@ -36,9 +38,8 @@ function createTrackingFunction(options = {}) {
|
|
|
36
38
|
properties: allProperties
|
|
37
39
|
})
|
|
38
40
|
});
|
|
39
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
40
41
|
} catch (error) {
|
|
41
|
-
|
|
42
|
+
logger.withText(`[mixpanel] An error occurred while sending a tracking event to "${endpoint}":`).withError(error).error();
|
|
42
43
|
}
|
|
43
44
|
};
|
|
44
45
|
return trackFunction;
|
|
@@ -1 +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
|
|
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 {@link 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 {@link 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 {@link https://workleap.github.io/wl-telemetry}\n */\nexport type TrackingFunction = (eventName: string, properties: MixpanelEventProperties, options?: TrackingFunctionOptions) => Promise<void>;\n\n/**\n * @see {@link https://workleap.github.io/wl-telemetry}\n */\nexport function createTrackingFunction(options: CreateTrackingFunctionOptions = {}) {\n const {\n targetProductId\n } = options;\n\n const {\n productId,\n endpoint,\n superProperties,\n logger\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 } catch (error: unknown) {\n logger\n .withText(`[mixpanel] An error occurred while sending a tracking event to \"${endpoint}\":`)\n .withError(error as Error)\n .error();\n }\n };\n\n return trackFunction;\n}\n"],"names":["getMixpanelContext","getBaseProperties","createTrackingFunction","options","targetProductId","productId","endpoint","superProperties","logger","trackFunction","eventName","properties","_options","keepAlive","baseProperties","allProperties","Object","fetch","JSON","error"],"mappings":";;;;;;;;AAAkD;AACgC;AAsClF;;CAEC,GACM,SAASE,uBAAuBC,UAAyC,CAAC,CAAC;IAC9E,MAAM,EACFC,eAAe,EAClB,GAAGD;IAEJ,MAAM,EACFE,SAAS,EACTC,QAAQ,EACRC,eAAe,EACfC,MAAM,EACT,GAAGR,kBAAkBA;IAEtB,MAAMS,gBAAkC,OAAOC,WAAWC,YAAYC,WAAW,CAAC,CAAC;QAC/E,IAAI;YACA,MAAM,EACFC,YAAY,KAAK,EACpB,GAAGD;YAEJ,MAAME,iBAAiBb,iBAAiBA;YAExC,MAAMc,gBAAgB;gBAClB,GAAGD,cAAc;gBACjB,GAAGE,OAAO,WAAW,CAACT,gBAAgB;gBACtC,GAAGI,UAAU;YACjB;YAEA,MAAMM,MAAMX,UAAU;gBAClB,QAAQ;gBACR,aAAa;gBACb,WAAWO;gBACX,SAAS;oBAAE,gBAAgB;gBAAmB;gBAC9C,MAAMK,KAAK,SAAS,CAAC;oBACjBR;oBACA,mBAAmBL;oBACnB,uFAAuF;oBACvF,gBAAgB;oBAChB,yBAA0BD,mBAAmB;oBAC7C,YAAYW;gBAChB;YACJ;QACJ,EAAE,OAAOI,OAAgB;YACrBX,OACK,QAAQ,CAAC,CAAC,gEAAgE,EAAEF,SAAS,EAAE,CAAC,EACxF,SAAS,CAACa,OACV,KAAK;QACd;IACJ;IAEA,OAAOV;AACX"}
|
|
@@ -1,15 +1,22 @@
|
|
|
1
|
+
import { type RootLogger } from "@workleap/logging";
|
|
1
2
|
import { type Environment } from "./env.ts";
|
|
2
3
|
/**
|
|
3
|
-
* @see https://workleap.github.io/wl-telemetry
|
|
4
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
4
5
|
*/
|
|
5
6
|
export interface InitializeMixpanelOptions {
|
|
6
7
|
/**
|
|
7
|
-
*
|
|
8
|
-
* If not provided, the default endpoint for the environment will be used.
|
|
8
|
+
* An optional tracking endpoint.
|
|
9
9
|
* @default "tracking/track"
|
|
10
10
|
*/
|
|
11
11
|
trackingEndpoint?: string;
|
|
12
|
+
/**
|
|
13
|
+
* Whether or not debug information should be logged to the console.
|
|
14
|
+
*/
|
|
12
15
|
verbose?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* An array of RootLogger instances.
|
|
18
|
+
*/
|
|
19
|
+
loggers?: RootLogger[];
|
|
13
20
|
}
|
|
14
21
|
export declare function __resetInitializationGuard(): void;
|
|
15
22
|
/**
|
|
@@ -18,6 +25,6 @@ export declare function __resetInitializationGuard(): void;
|
|
|
18
25
|
* @param envOrTrackingApiBaseUrl The environment to get the navigation url from or a base URL.
|
|
19
26
|
* @param options Options for creating the tracking function.
|
|
20
27
|
* @returns A function that sends tracking events to the tracking API.
|
|
21
|
-
* @see https://workleap.github.io/wl-telemetry
|
|
28
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
22
29
|
*/
|
|
23
30
|
export declare function initializeMixpanel(productId: string, envOrTrackingApiBaseUrl: Environment | (string & {}), options?: InitializeMixpanelOptions): void;
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createCompositeLogger } from "@workleap/logging";
|
|
2
|
+
import { createBootstrappingStore, createTelemetryContext } from "@workleap/telemetry";
|
|
2
3
|
import { setMixpanelContext } from "./context.js";
|
|
3
4
|
import { getTrackingEndpoint } from "./env.js";
|
|
4
5
|
import { HasExecutedGuard } from "./HasExecutedGuard.js";
|
|
5
6
|
import { OtherProperties, getSuperProperties, getTelemetryProperties, setSuperProperties, setSuperProperty } from "./properties.js";
|
|
6
7
|
|
|
8
|
+
;// CONCATENATED MODULE: external "@workleap/logging"
|
|
9
|
+
|
|
7
10
|
;// CONCATENATED MODULE: external "@workleap/telemetry"
|
|
8
11
|
|
|
9
12
|
;// CONCATENATED MODULE: external "./context.js"
|
|
@@ -20,6 +23,7 @@ import { OtherProperties, getSuperProperties, getTelemetryProperties, setSuperPr
|
|
|
20
23
|
|
|
21
24
|
|
|
22
25
|
|
|
26
|
+
|
|
23
27
|
function registerLogRocketSessionUrlListener(verbose = false) {
|
|
24
28
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
25
29
|
// @ts-ignore
|
|
@@ -48,16 +52,15 @@ function __resetInitializationGuard() {
|
|
|
48
52
|
* @param envOrTrackingApiBaseUrl The environment to get the navigation url from or a base URL.
|
|
49
53
|
* @param options Options for creating the tracking function.
|
|
50
54
|
* @returns A function that sends tracking events to the tracking API.
|
|
51
|
-
* @see https://workleap.github.io/wl-telemetry
|
|
55
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
52
56
|
*/ function initializeMixpanel(productId, envOrTrackingApiBaseUrl, options = {}) {
|
|
53
|
-
const { verbose = false,
|
|
57
|
+
const { trackingEndpoint, verbose = false, loggers = [] } = options;
|
|
54
58
|
initializationGuard.throw("[mixpanel] Mixpanel has already been initialized. Did you call the \"initializeMixpanel\" function twice?");
|
|
59
|
+
const logger = createCompositeLogger(verbose, loggers);
|
|
55
60
|
const endpoint = getTrackingEndpoint(envOrTrackingApiBaseUrl, trackingEndpoint);
|
|
56
|
-
const telemetryContext =
|
|
57
|
-
verbose
|
|
58
|
-
});
|
|
61
|
+
const telemetryContext = createTelemetryContext(logger);
|
|
59
62
|
setSuperProperties(getTelemetryProperties(telemetryContext));
|
|
60
|
-
const bootstrappingStore =
|
|
63
|
+
const bootstrappingStore = createBootstrappingStore(logger);
|
|
61
64
|
// If LogRocket is already available, register the listener. Otherwise, subscribe to the bootstrapping store
|
|
62
65
|
// and register the listener once a notification is received that LogRocket is registered.
|
|
63
66
|
if (bootstrappingStore.state.isLogRocketReady) {
|
|
@@ -74,11 +77,9 @@ function __resetInitializationGuard() {
|
|
|
74
77
|
productId,
|
|
75
78
|
endpoint,
|
|
76
79
|
superProperties: getSuperProperties(),
|
|
77
|
-
|
|
80
|
+
logger
|
|
78
81
|
});
|
|
79
|
-
|
|
80
|
-
console.log("[mixpanel] Mixpanel is initialized.");
|
|
81
|
-
}
|
|
82
|
+
logger.debug("[mixpanel] Mixpanel is initialized.");
|
|
82
83
|
}
|
|
83
84
|
|
|
84
85
|
export { __resetInitializationGuard, initializeMixpanel };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"js/initializeMixpanel.js","sources":["webpack://@workleap/mixpanel/./src/js/initializeMixpanel.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"js/initializeMixpanel.js","sources":["webpack://@workleap/mixpanel/./src/js/initializeMixpanel.ts"],"sourcesContent":["import { createCompositeLogger, type RootLogger } from \"@workleap/logging\";\nimport { createBootstrappingStore, createTelemetryContext } 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 {@link https://workleap.github.io/wl-telemetry}\n */\nexport interface InitializeMixpanelOptions {\n /**\n * An optional tracking endpoint.\n * @default \"tracking/track\"\n */\n trackingEndpoint?: string;\n /**\n * Whether or not debug information should be logged to the console.\n */\n verbose?: boolean;\n /**\n * An array of RootLogger instances.\n */\n loggers?: RootLogger[];\n}\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 {@link https://workleap.github.io/wl-telemetry}\n */\nexport function initializeMixpanel(productId: string, envOrTrackingApiBaseUrl: Environment | (string & {}), options: InitializeMixpanelOptions = {}) {\n const {\n trackingEndpoint,\n verbose = false,\n loggers = []\n } = options;\n\n initializationGuard.throw(\"[mixpanel] Mixpanel has already been initialized. Did you call the \\\"initializeMixpanel\\\" function twice?\");\n\n const logger = createCompositeLogger(verbose, loggers);\n const endpoint = getTrackingEndpoint(envOrTrackingApiBaseUrl, trackingEndpoint);\n const telemetryContext = createTelemetryContext(logger);\n\n setSuperProperties(getTelemetryProperties(telemetryContext));\n\n const bootstrappingStore = createBootstrappingStore(logger);\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 logger\n });\n\n logger.debug(\"[mixpanel] Mixpanel is initialized.\");\n}\n"],"names":["createCompositeLogger","createBootstrappingStore","createTelemetryContext","setMixpanelContext","getTrackingEndpoint","HasExecutedGuard","getSuperProperties","getTelemetryProperties","OtherProperties","setSuperProperties","setSuperProperty","registerLogRocketSessionUrlListener","verbose","globalThis","sessionUrl","console","initializationGuard","__resetInitializationGuard","initializeMixpanel","productId","envOrTrackingApiBaseUrl","options","trackingEndpoint","loggers","logger","endpoint","telemetryContext","bootstrappingStore","action","store","unsubscribe"],"mappings":";;;;;;;;;;;;;;;;;;;;AAA2E;AACY;AACrC;AACe;AACR;AAC2E;AAqBpI,SAASW,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,EACFC,gBAAgB,EAChBV,UAAU,KAAK,EACfW,UAAU,EAAE,EACf,GAAGF;IAEJL,oBAAoB,KAAK,CAAC;IAE1B,MAAMQ,SAASxB,qBAAqBA,CAACY,SAASW;IAC9C,MAAME,WAAWrB,mBAAmBA,CAACgB,yBAAyBE;IAC9D,MAAMI,mBAAmBxB,sBAAsBA,CAACsB;IAEhDf,kBAAkBA,CAACF,sBAAsBA,CAACmB;IAE1C,MAAMC,qBAAqB1B,wBAAwBA,CAACuB;IAEpD,4GAA4G;IAC5G,0FAA0F;IAC1F,IAAIG,mBAAmB,KAAK,CAAC,gBAAgB,EAAE;QAC3ChB,oCAAoCC;IACxC,OAAO;QACHe,mBAAmB,SAAS,CAAC,CAACC,QAAQC,OAAOC;YACzC,IAAID,MAAM,KAAK,CAAC,gBAAgB,EAAE;gBAC9BC;gBACAnB,oCAAoCC;YACxC;QACJ;IACJ;IAEAT,kBAAkBA,CAAC;QACfgB;QACAM;QACA,iBAAiBnB,kBAAkBA;QACnCkB;IACJ;IAEAA,OAAO,KAAK,CAAC;AACjB"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@workleap/mixpanel",
|
|
3
3
|
"author": "Workleap",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.1.1",
|
|
5
5
|
"description": "A package containing utilities for Mixpanel telemetry.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
7
7
|
"repository": {
|
|
@@ -34,8 +34,7 @@
|
|
|
34
34
|
],
|
|
35
35
|
"peerDependencies": {
|
|
36
36
|
"react": "^18.0.0 || ^19.0.0",
|
|
37
|
-
"react-dom": "^18.0.0 || ^19.0.0"
|
|
38
|
-
"@workleap/telemetry": "1.0.0"
|
|
37
|
+
"react-dom": "^18.0.0 || ^19.0.0"
|
|
39
38
|
},
|
|
40
39
|
"peerDependenciesMeta": {
|
|
41
40
|
"react": {
|
|
@@ -45,6 +44,10 @@
|
|
|
45
44
|
"optional": true
|
|
46
45
|
}
|
|
47
46
|
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@workleap/logging": "^1.2.0",
|
|
49
|
+
"@workleap/telemetry": "1.1.1"
|
|
50
|
+
},
|
|
48
51
|
"devDependencies": {
|
|
49
52
|
"@rsbuild/core": "1.4.6",
|
|
50
53
|
"@rslib/core": "0.10.5",
|
package/src/js/context.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
+
import type { Logger } from "@workleap/logging";
|
|
1
2
|
import type { SuperProperties } from "./properties.ts";
|
|
2
3
|
|
|
3
|
-
export const
|
|
4
|
+
export const MixpanelContextVariableName = "__WLP_MIXPANEL_CONTEXT__";
|
|
4
5
|
|
|
5
6
|
export interface MixpanelContext {
|
|
6
7
|
productId: string;
|
|
7
8
|
endpoint: string;
|
|
8
9
|
superProperties: SuperProperties;
|
|
9
|
-
|
|
10
|
+
logger: Logger;
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
export function setMixpanelContext(context: MixpanelContext) {
|
|
@@ -16,13 +17,13 @@ export function setMixpanelContext(context: MixpanelContext) {
|
|
|
16
17
|
// of this package than the host application.
|
|
17
18
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
18
19
|
// @ts-ignore
|
|
19
|
-
globalThis[
|
|
20
|
+
globalThis[MixpanelContextVariableName] = context;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
export function getMixpanelContext() {
|
|
23
24
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
24
25
|
// @ts-ignore
|
|
25
|
-
const context = globalThis[
|
|
26
|
+
const context = globalThis[MixpanelContextVariableName] as MixpanelContext;
|
|
26
27
|
|
|
27
28
|
if (!context) {
|
|
28
29
|
throw new Error("[mixpanel] The Mixpanel context is not available. Did you initialize Mixpanel with the \"initializeMixpanel\" function?");
|
|
@@ -2,7 +2,7 @@ import { getMixpanelContext } from "./context.ts";
|
|
|
2
2
|
import { getBaseProperties, type MixpanelEventProperties } from "./properties.ts";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* @see https://workleap.github.io/wl-telemetry
|
|
5
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
6
6
|
*/
|
|
7
7
|
export interface CreateTrackingFunctionOptions {
|
|
8
8
|
/**
|
|
@@ -12,7 +12,7 @@ export interface CreateTrackingFunctionOptions {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
* @see https://workleap.github.io/wl-telemetry
|
|
15
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
16
16
|
*/
|
|
17
17
|
export interface TrackingFunctionOptions {
|
|
18
18
|
/**
|
|
@@ -33,10 +33,13 @@ export interface TrackingFunctionOptions {
|
|
|
33
33
|
* @param eventName The name of the event to track.
|
|
34
34
|
* @param properties The properties to send with the event.
|
|
35
35
|
* @param options Options for tracking the event.
|
|
36
|
-
* @see https://workleap.github.io/wl-telemetry
|
|
36
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
37
37
|
*/
|
|
38
38
|
export type TrackingFunction = (eventName: string, properties: MixpanelEventProperties, options?: TrackingFunctionOptions) => Promise<void>;
|
|
39
39
|
|
|
40
|
+
/**
|
|
41
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
42
|
+
*/
|
|
40
43
|
export function createTrackingFunction(options: CreateTrackingFunctionOptions = {}) {
|
|
41
44
|
const {
|
|
42
45
|
targetProductId
|
|
@@ -45,7 +48,8 @@ export function createTrackingFunction(options: CreateTrackingFunctionOptions =
|
|
|
45
48
|
const {
|
|
46
49
|
productId,
|
|
47
50
|
endpoint,
|
|
48
|
-
superProperties
|
|
51
|
+
superProperties,
|
|
52
|
+
logger
|
|
49
53
|
} = getMixpanelContext();
|
|
50
54
|
|
|
51
55
|
const trackFunction: TrackingFunction = async (eventName, properties, _options = {}) => {
|
|
@@ -76,9 +80,11 @@ export function createTrackingFunction(options: CreateTrackingFunctionOptions =
|
|
|
76
80
|
properties: allProperties
|
|
77
81
|
})
|
|
78
82
|
});
|
|
79
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
80
83
|
} catch (error: unknown) {
|
|
81
|
-
|
|
84
|
+
logger
|
|
85
|
+
.withText(`[mixpanel] An error occurred while sending a tracking event to "${endpoint}":`)
|
|
86
|
+
.withError(error as Error)
|
|
87
|
+
.error();
|
|
82
88
|
}
|
|
83
89
|
};
|
|
84
90
|
|
|
@@ -1,21 +1,29 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createCompositeLogger, type RootLogger } from "@workleap/logging";
|
|
2
|
+
import { createBootstrappingStore, createTelemetryContext } from "@workleap/telemetry";
|
|
2
3
|
import { setMixpanelContext } from "./context.ts";
|
|
3
4
|
import { getTrackingEndpoint, type Environment } from "./env.ts";
|
|
4
5
|
import { HasExecutedGuard } from "./HasExecutedGuard.ts";
|
|
5
6
|
import { getSuperProperties, getTelemetryProperties, OtherProperties, setSuperProperties, setSuperProperty } from "./properties.ts";
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
|
-
* @see https://workleap.github.io/wl-telemetry
|
|
9
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
9
10
|
*/
|
|
10
11
|
export interface InitializeMixpanelOptions {
|
|
11
12
|
/**
|
|
12
|
-
*
|
|
13
|
-
* If not provided, the default endpoint for the environment will be used.
|
|
13
|
+
* An optional tracking endpoint.
|
|
14
14
|
* @default "tracking/track"
|
|
15
15
|
*/
|
|
16
16
|
trackingEndpoint?: string;
|
|
17
|
+
/**
|
|
18
|
+
* Whether or not debug information should be logged to the console.
|
|
19
|
+
*/
|
|
17
20
|
verbose?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* An array of RootLogger instances.
|
|
23
|
+
*/
|
|
24
|
+
loggers?: RootLogger[];
|
|
18
25
|
}
|
|
26
|
+
|
|
19
27
|
function registerLogRocketSessionUrlListener(verbose = false) {
|
|
20
28
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
21
29
|
// @ts-ignore
|
|
@@ -48,22 +56,24 @@ export function __resetInitializationGuard() {
|
|
|
48
56
|
* @param envOrTrackingApiBaseUrl The environment to get the navigation url from or a base URL.
|
|
49
57
|
* @param options Options for creating the tracking function.
|
|
50
58
|
* @returns A function that sends tracking events to the tracking API.
|
|
51
|
-
* @see https://workleap.github.io/wl-telemetry
|
|
59
|
+
* @see {@link https://workleap.github.io/wl-telemetry}
|
|
52
60
|
*/
|
|
53
61
|
export function initializeMixpanel(productId: string, envOrTrackingApiBaseUrl: Environment | (string & {}), options: InitializeMixpanelOptions = {}) {
|
|
54
62
|
const {
|
|
63
|
+
trackingEndpoint,
|
|
55
64
|
verbose = false,
|
|
56
|
-
|
|
65
|
+
loggers = []
|
|
57
66
|
} = options;
|
|
58
67
|
|
|
59
68
|
initializationGuard.throw("[mixpanel] Mixpanel has already been initialized. Did you call the \"initializeMixpanel\" function twice?");
|
|
60
69
|
|
|
70
|
+
const logger = createCompositeLogger(verbose, loggers);
|
|
61
71
|
const endpoint = getTrackingEndpoint(envOrTrackingApiBaseUrl, trackingEndpoint);
|
|
62
|
-
const telemetryContext =
|
|
72
|
+
const telemetryContext = createTelemetryContext(logger);
|
|
63
73
|
|
|
64
74
|
setSuperProperties(getTelemetryProperties(telemetryContext));
|
|
65
75
|
|
|
66
|
-
const bootstrappingStore =
|
|
76
|
+
const bootstrappingStore = createBootstrappingStore(logger);
|
|
67
77
|
|
|
68
78
|
// If LogRocket is already available, register the listener. Otherwise, subscribe to the bootstrapping store
|
|
69
79
|
// and register the listener once a notification is received that LogRocket is registered.
|
|
@@ -82,10 +92,8 @@ export function initializeMixpanel(productId: string, envOrTrackingApiBaseUrl: E
|
|
|
82
92
|
productId,
|
|
83
93
|
endpoint,
|
|
84
94
|
superProperties: getSuperProperties(),
|
|
85
|
-
|
|
95
|
+
logger
|
|
86
96
|
});
|
|
87
97
|
|
|
88
|
-
|
|
89
|
-
console.log("[mixpanel] Mixpanel is initialized.");
|
|
90
|
-
}
|
|
98
|
+
logger.debug("[mixpanel] Mixpanel is initialized.");
|
|
91
99
|
}
|