@intlayer/analytics 9.0.0-canary.16
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/README.md +330 -0
- package/dist/cjs/core/AnalyticsClient.cjs +182 -0
- package/dist/cjs/core/AnalyticsClient.cjs.map +1 -0
- package/dist/cjs/core/abTesting.cjs +44 -0
- package/dist/cjs/core/abTesting.cjs.map +1 -0
- package/dist/cjs/core/eventBuffer.cjs +68 -0
- package/dist/cjs/core/eventBuffer.cjs.map +1 -0
- package/dist/cjs/core/globalManager.cjs +63 -0
- package/dist/cjs/core/globalManager.cjs.map +1 -0
- package/dist/cjs/core/index.cjs +26 -0
- package/dist/cjs/core/initAnalyticsClient.cjs +51 -0
- package/dist/cjs/core/initAnalyticsClient.cjs.map +1 -0
- package/dist/cjs/core/sampling.cjs +28 -0
- package/dist/cjs/core/sampling.cjs.map +1 -0
- package/dist/cjs/core/session.cjs +42 -0
- package/dist/cjs/core/session.cjs.map +1 -0
- package/dist/cjs/core/transport.cjs +47 -0
- package/dist/cjs/core/transport.cjs.map +1 -0
- package/dist/cjs/core/types.cjs +0 -0
- package/dist/cjs/events/contentExposure.cjs +39 -0
- package/dist/cjs/events/contentExposure.cjs.map +1 -0
- package/dist/cjs/index.cjs +29 -0
- package/dist/cjs/isEnabled.cjs +29 -0
- package/dist/cjs/isEnabled.cjs.map +1 -0
- package/dist/cjs/utils/hash.cjs +26 -0
- package/dist/cjs/utils/hash.cjs.map +1 -0
- package/dist/cjs/utils/idle.cjs +26 -0
- package/dist/cjs/utils/idle.cjs.map +1 -0
- package/dist/esm/core/AnalyticsClient.mjs +177 -0
- package/dist/esm/core/AnalyticsClient.mjs.map +1 -0
- package/dist/esm/core/abTesting.mjs +43 -0
- package/dist/esm/core/abTesting.mjs.map +1 -0
- package/dist/esm/core/eventBuffer.mjs +66 -0
- package/dist/esm/core/eventBuffer.mjs.map +1 -0
- package/dist/esm/core/globalManager.mjs +58 -0
- package/dist/esm/core/globalManager.mjs.map +1 -0
- package/dist/esm/core/index.mjs +10 -0
- package/dist/esm/core/initAnalyticsClient.mjs +49 -0
- package/dist/esm/core/initAnalyticsClient.mjs.map +1 -0
- package/dist/esm/core/sampling.mjs +27 -0
- package/dist/esm/core/sampling.mjs.map +1 -0
- package/dist/esm/core/session.mjs +40 -0
- package/dist/esm/core/session.mjs.map +1 -0
- package/dist/esm/core/transport.mjs +45 -0
- package/dist/esm/core/transport.mjs.map +1 -0
- package/dist/esm/core/types.mjs +0 -0
- package/dist/esm/events/contentExposure.mjs +36 -0
- package/dist/esm/events/contentExposure.mjs.map +1 -0
- package/dist/esm/index.mjs +11 -0
- package/dist/esm/isEnabled.mjs +28 -0
- package/dist/esm/isEnabled.mjs.map +1 -0
- package/dist/esm/utils/hash.mjs +24 -0
- package/dist/esm/utils/hash.mjs.map +1 -0
- package/dist/esm/utils/idle.mjs +24 -0
- package/dist/esm/utils/idle.mjs.map +1 -0
- package/dist/types/core/AnalyticsClient.d.ts +51 -0
- package/dist/types/core/AnalyticsClient.d.ts.map +1 -0
- package/dist/types/core/abTesting.d.ts +23 -0
- package/dist/types/core/abTesting.d.ts.map +1 -0
- package/dist/types/core/eventBuffer.d.ts +35 -0
- package/dist/types/core/eventBuffer.d.ts.map +1 -0
- package/dist/types/core/globalManager.d.ts +28 -0
- package/dist/types/core/globalManager.d.ts.map +1 -0
- package/dist/types/core/index.d.ts +10 -0
- package/dist/types/core/initAnalyticsClient.d.ts +28 -0
- package/dist/types/core/initAnalyticsClient.d.ts.map +1 -0
- package/dist/types/core/sampling.d.ts +20 -0
- package/dist/types/core/sampling.d.ts.map +1 -0
- package/dist/types/core/session.d.ts +17 -0
- package/dist/types/core/session.d.ts.map +1 -0
- package/dist/types/core/transport.d.ts +31 -0
- package/dist/types/core/transport.d.ts.map +1 -0
- package/dist/types/core/types.d.ts +87 -0
- package/dist/types/core/types.d.ts.map +1 -0
- package/dist/types/events/contentExposure.d.ts +38 -0
- package/dist/types/events/contentExposure.d.ts.map +1 -0
- package/dist/types/index.d.ts +11 -0
- package/dist/types/isEnabled.d.ts +18 -0
- package/dist/types/isEnabled.d.ts.map +1 -0
- package/dist/types/utils/hash.d.ts +16 -0
- package/dist/types/utils/hash.d.ts.map +1 -0
- package/dist/types/utils/idle.d.ts +15 -0
- package/dist/types/utils/idle.d.ts.map +1 -0
- package/package.json +100 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
|
|
3
|
+
//#region src/core/eventBuffer.ts
|
|
4
|
+
/** Field separator for coalescing keys — never present in content keys. */
|
|
5
|
+
const KEY_SEPARATOR = "";
|
|
6
|
+
/**
|
|
7
|
+
* Builds the coalescing key for a content-exposure event. Two exposures of the
|
|
8
|
+
* same node, page, locale, experiment, and variant collapse into one event with
|
|
9
|
+
* an incremented `count`.
|
|
10
|
+
*/
|
|
11
|
+
const exposureKey = (event) => [
|
|
12
|
+
event.url,
|
|
13
|
+
event.dictionaryKey,
|
|
14
|
+
event.keyPath,
|
|
15
|
+
event.locale,
|
|
16
|
+
event.experimentKey ?? "",
|
|
17
|
+
event.variant ?? ""
|
|
18
|
+
].join(KEY_SEPARATOR);
|
|
19
|
+
/**
|
|
20
|
+
* Creates an {@link EventBuffer}.
|
|
21
|
+
*
|
|
22
|
+
* @param options - Buffer options.
|
|
23
|
+
* @param options.max - Maximum number of distinct events to retain.
|
|
24
|
+
* @returns A new buffer instance.
|
|
25
|
+
*/
|
|
26
|
+
const createEventBuffer = ({ max }) => {
|
|
27
|
+
let discreteEvents = [];
|
|
28
|
+
const exposures = /* @__PURE__ */ new Map();
|
|
29
|
+
const size = () => discreteEvents.length + exposures.size;
|
|
30
|
+
/** Evicts oldest exposures first, then oldest discrete events. */
|
|
31
|
+
const enforceCap = () => {
|
|
32
|
+
while (size() > max && exposures.size > 0) {
|
|
33
|
+
const oldestKey = exposures.keys().next().value;
|
|
34
|
+
exposures.delete(oldestKey);
|
|
35
|
+
}
|
|
36
|
+
if (size() > max) discreteEvents = discreteEvents.slice(size() - max);
|
|
37
|
+
};
|
|
38
|
+
const push = (event) => {
|
|
39
|
+
if (event.type === "content_exposure") {
|
|
40
|
+
const key = exposureKey(event);
|
|
41
|
+
const existing = exposures.get(key);
|
|
42
|
+
if (existing) {
|
|
43
|
+
existing.count = (existing.count ?? 1) + (event.count ?? 1);
|
|
44
|
+
existing.t = event.t;
|
|
45
|
+
} else exposures.set(key, {
|
|
46
|
+
...event,
|
|
47
|
+
count: event.count ?? 1
|
|
48
|
+
});
|
|
49
|
+
} else discreteEvents.push(event);
|
|
50
|
+
enforceCap();
|
|
51
|
+
return size();
|
|
52
|
+
};
|
|
53
|
+
const drain = () => {
|
|
54
|
+
const drained = [...discreteEvents, ...exposures.values()];
|
|
55
|
+
discreteEvents = [];
|
|
56
|
+
exposures.clear();
|
|
57
|
+
return drained;
|
|
58
|
+
};
|
|
59
|
+
return {
|
|
60
|
+
push,
|
|
61
|
+
drain,
|
|
62
|
+
size
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
//#endregion
|
|
67
|
+
exports.createEventBuffer = createEventBuffer;
|
|
68
|
+
//# sourceMappingURL=eventBuffer.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"eventBuffer.cjs","names":[],"sources":["../../../src/core/eventBuffer.ts"],"sourcesContent":["import type { AnalyticsEvent, ContentExposureEvent } from './types';\n\n/** Field separator for coalescing keys — never present in content keys. */\nconst KEY_SEPARATOR = '\u0001';\n\n/**\n * Builds the coalescing key for a content-exposure event. Two exposures of the\n * same node, page, locale, experiment, and variant collapse into one event with\n * an incremented `count`.\n */\nconst exposureKey = (event: ContentExposureEvent): string =>\n [\n event.url,\n event.dictionaryKey,\n event.keyPath,\n event.locale,\n event.experimentKey ?? '',\n event.variant ?? '',\n ].join(KEY_SEPARATOR);\n\n/**\n * A bounded, coalescing in-memory buffer for analytics events.\n *\n * - `page_view` and `conversion` events are appended verbatim (they are rare\n * and valuable — they are never evicted while exposures remain).\n * - `content_exposure` events are deduplicated by page/node/locale/variant with\n * a running `count`, keeping the highest-volume signal cheap.\n * - When the total number of stored events exceeds `max`, the **oldest\n * exposures** are evicted first (analytics is lossy by design; exposures are\n * the expendable signal). Discrete events are only dropped once no exposures\n * remain.\n */\nexport type EventBuffer = {\n /** Adds an event, coalescing content exposures. Returns the current size. */\n push: (event: AnalyticsEvent) => number;\n /** Removes and returns every buffered event. */\n drain: () => AnalyticsEvent[];\n /** Current number of distinct buffered events. */\n size: () => number;\n};\n\n/**\n * Creates an {@link EventBuffer}.\n *\n * @param options - Buffer options.\n * @param options.max - Maximum number of distinct events to retain.\n * @returns A new buffer instance.\n */\nexport const createEventBuffer = ({ max }: { max: number }): EventBuffer => {\n // Insertion-ordered list of non-coalesced events (page views, conversions).\n let discreteEvents: AnalyticsEvent[] = [];\n // Coalesced content exposures keyed by page/node/locale/variant.\n const exposures = new Map<string, ContentExposureEvent>();\n\n const size = (): number => discreteEvents.length + exposures.size;\n\n /** Evicts oldest exposures first, then oldest discrete events. */\n const enforceCap = (): void => {\n while (size() > max && exposures.size > 0) {\n // Map iteration follows insertion order — the first key is the oldest.\n const oldestKey = exposures.keys().next().value as string;\n exposures.delete(oldestKey);\n }\n if (size() > max) {\n discreteEvents = discreteEvents.slice(size() - max);\n }\n };\n\n const push = (event: AnalyticsEvent): number => {\n if (event.type === 'content_exposure') {\n const key = exposureKey(event);\n const existing = exposures.get(key);\n\n if (existing) {\n existing.count = (existing.count ?? 1) + (event.count ?? 1);\n existing.t = event.t; // keep the most recent timestamp\n } else {\n exposures.set(key, { ...event, count: event.count ?? 1 });\n }\n } else {\n discreteEvents.push(event);\n }\n\n enforceCap();\n\n return size();\n };\n\n const drain = (): AnalyticsEvent[] => {\n const drained: AnalyticsEvent[] = [\n ...discreteEvents,\n ...exposures.values(),\n ];\n discreteEvents = [];\n exposures.clear();\n return drained;\n };\n\n return { push, drain, size };\n};\n"],"mappings":";;;;AAGA,MAAM,gBAAgB;;;;;;AAOtB,MAAM,eAAe,UACnB;CACE,MAAM;CACN,MAAM;CACN,MAAM;CACN,MAAM;CACN,MAAM,iBAAiB;CACvB,MAAM,WAAW;CAClB,CAAC,KAAK,cAAc;;;;;;;;AA8BvB,MAAa,qBAAqB,EAAE,UAAwC;CAE1E,IAAI,iBAAmC,EAAE;CAEzC,MAAM,4BAAY,IAAI,KAAmC;CAEzD,MAAM,aAAqB,eAAe,SAAS,UAAU;;CAG7D,MAAM,mBAAyB;AAC7B,SAAO,MAAM,GAAG,OAAO,UAAU,OAAO,GAAG;GAEzC,MAAM,YAAY,UAAU,MAAM,CAAC,MAAM,CAAC;AAC1C,aAAU,OAAO,UAAU;;AAE7B,MAAI,MAAM,GAAG,IACX,kBAAiB,eAAe,MAAM,MAAM,GAAG,IAAI;;CAIvD,MAAM,QAAQ,UAAkC;AAC9C,MAAI,MAAM,SAAS,oBAAoB;GACrC,MAAM,MAAM,YAAY,MAAM;GAC9B,MAAM,WAAW,UAAU,IAAI,IAAI;AAEnC,OAAI,UAAU;AACZ,aAAS,SAAS,SAAS,SAAS,MAAM,MAAM,SAAS;AACzD,aAAS,IAAI,MAAM;SAEnB,WAAU,IAAI,KAAK;IAAE,GAAG;IAAO,OAAO,MAAM,SAAS;IAAG,CAAC;QAG3D,gBAAe,KAAK,MAAM;AAG5B,cAAY;AAEZ,SAAO,MAAM;;CAGf,MAAM,cAAgC;EACpC,MAAM,UAA4B,CAChC,GAAG,gBACH,GAAG,UAAU,QAAQ,CACtB;AACD,mBAAiB,EAAE;AACnB,YAAU,OAAO;AACjB,SAAO;;AAGT,QAAO;EAAE;EAAM;EAAO;EAAM"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
|
|
3
|
+
//#region src/core/globalManager.ts
|
|
4
|
+
/**
|
|
5
|
+
* Key on `window` used to share the client singleton across multiple module
|
|
6
|
+
* instances. When bundlers include `@intlayer/analytics` more than once (e.g.
|
|
7
|
+
* a framework package plus the app), each copy has its own module-level
|
|
8
|
+
* variables; storing the client on `window` ensures they all share one buffer
|
|
9
|
+
* and one flush timer.
|
|
10
|
+
*/
|
|
11
|
+
const CLIENT_KEY = "__intlayer_analytics_client__";
|
|
12
|
+
/**
|
|
13
|
+
* Key on `window` holding the provider reference count. It lives next to the
|
|
14
|
+
* client (rather than in module scope) for the same reason the client does:
|
|
15
|
+
* duplicated module instances must share one count, otherwise one copy's
|
|
16
|
+
* `stopAnalyticsClient` could stop a client another copy still uses.
|
|
17
|
+
*/
|
|
18
|
+
const REF_COUNT_KEY = "__intlayer_analytics_ref_count__";
|
|
19
|
+
/**
|
|
20
|
+
* Returns the global analytics client, or `null` in SSR or before init.
|
|
21
|
+
*/
|
|
22
|
+
const getGlobalAnalyticsClient = () => {
|
|
23
|
+
if (typeof window === "undefined") return null;
|
|
24
|
+
return window[CLIENT_KEY] ?? null;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Sets (or clears) the global analytics client singleton.
|
|
28
|
+
*
|
|
29
|
+
* @param client - The client to store globally, or `null` to clear it.
|
|
30
|
+
*/
|
|
31
|
+
const setGlobalAnalyticsClient = (client) => {
|
|
32
|
+
if (typeof window === "undefined") return;
|
|
33
|
+
window[CLIENT_KEY] = client;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Increments the shared provider reference count.
|
|
37
|
+
*
|
|
38
|
+
* @returns The count after incrementing (`0` in SSR, where nothing is stored).
|
|
39
|
+
*/
|
|
40
|
+
const incrementAnalyticsClientRefCount = () => {
|
|
41
|
+
if (typeof window === "undefined") return 0;
|
|
42
|
+
const globals = window;
|
|
43
|
+
globals[REF_COUNT_KEY] = (globals[REF_COUNT_KEY] ?? 0) + 1;
|
|
44
|
+
return globals[REF_COUNT_KEY];
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Decrements the shared provider reference count, clamping at zero.
|
|
48
|
+
*
|
|
49
|
+
* @returns The count after decrementing (`0` in SSR, where nothing is stored).
|
|
50
|
+
*/
|
|
51
|
+
const decrementAnalyticsClientRefCount = () => {
|
|
52
|
+
if (typeof window === "undefined") return 0;
|
|
53
|
+
const globals = window;
|
|
54
|
+
globals[REF_COUNT_KEY] = Math.max(0, (globals[REF_COUNT_KEY] ?? 0) - 1);
|
|
55
|
+
return globals[REF_COUNT_KEY];
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
//#endregion
|
|
59
|
+
exports.decrementAnalyticsClientRefCount = decrementAnalyticsClientRefCount;
|
|
60
|
+
exports.getGlobalAnalyticsClient = getGlobalAnalyticsClient;
|
|
61
|
+
exports.incrementAnalyticsClientRefCount = incrementAnalyticsClientRefCount;
|
|
62
|
+
exports.setGlobalAnalyticsClient = setGlobalAnalyticsClient;
|
|
63
|
+
//# sourceMappingURL=globalManager.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"globalManager.cjs","names":[],"sources":["../../../src/core/globalManager.ts"],"sourcesContent":["import type { AnalyticsClient } from './AnalyticsClient';\n\n/**\n * Key on `window` used to share the client singleton across multiple module\n * instances. When bundlers include `@intlayer/analytics` more than once (e.g.\n * a framework package plus the app), each copy has its own module-level\n * variables; storing the client on `window` ensures they all share one buffer\n * and one flush timer.\n */\nconst CLIENT_KEY = '__intlayer_analytics_client__';\n\n/**\n * Key on `window` holding the provider reference count. It lives next to the\n * client (rather than in module scope) for the same reason the client does:\n * duplicated module instances must share one count, otherwise one copy's\n * `stopAnalyticsClient` could stop a client another copy still uses.\n */\nconst REF_COUNT_KEY = '__intlayer_analytics_ref_count__';\n\ntype WindowWithAnalyticsGlobals = typeof window & {\n [CLIENT_KEY]?: AnalyticsClient | null;\n [REF_COUNT_KEY]?: number;\n};\n\n/**\n * Returns the global analytics client, or `null` in SSR or before init.\n */\nexport const getGlobalAnalyticsClient = (): AnalyticsClient | null => {\n if (typeof window === 'undefined') return null;\n return (window as WindowWithAnalyticsGlobals)[CLIENT_KEY] ?? null;\n};\n\n/**\n * Sets (or clears) the global analytics client singleton.\n *\n * @param client - The client to store globally, or `null` to clear it.\n */\nexport const setGlobalAnalyticsClient = (\n client: AnalyticsClient | null\n): void => {\n if (typeof window === 'undefined') return;\n (window as WindowWithAnalyticsGlobals)[CLIENT_KEY] = client;\n};\n\n/**\n * Increments the shared provider reference count.\n *\n * @returns The count after incrementing (`0` in SSR, where nothing is stored).\n */\nexport const incrementAnalyticsClientRefCount = (): number => {\n if (typeof window === 'undefined') return 0;\n const globals = window as WindowWithAnalyticsGlobals;\n globals[REF_COUNT_KEY] = (globals[REF_COUNT_KEY] ?? 0) + 1;\n return globals[REF_COUNT_KEY];\n};\n\n/**\n * Decrements the shared provider reference count, clamping at zero.\n *\n * @returns The count after decrementing (`0` in SSR, where nothing is stored).\n */\nexport const decrementAnalyticsClientRefCount = (): number => {\n if (typeof window === 'undefined') return 0;\n const globals = window as WindowWithAnalyticsGlobals;\n globals[REF_COUNT_KEY] = Math.max(0, (globals[REF_COUNT_KEY] ?? 0) - 1);\n return globals[REF_COUNT_KEY];\n};\n"],"mappings":";;;;;;;;;;AASA,MAAM,aAAa;;;;;;;AAQnB,MAAM,gBAAgB;;;;AAUtB,MAAa,iCAAyD;AACpE,KAAI,OAAO,WAAW,YAAa,QAAO;AAC1C,QAAQ,OAAsC,eAAe;;;;;;;AAQ/D,MAAa,4BACX,WACS;AACT,KAAI,OAAO,WAAW,YAAa;AACnC,CAAC,OAAsC,cAAc;;;;;;;AAQvD,MAAa,yCAAiD;AAC5D,KAAI,OAAO,WAAW,YAAa,QAAO;CAC1C,MAAM,UAAU;AAChB,SAAQ,kBAAkB,QAAQ,kBAAkB,KAAK;AACzD,QAAO,QAAQ;;;;;;;AAQjB,MAAa,yCAAiD;AAC5D,KAAI,OAAO,WAAW,YAAa,QAAO;CAC1C,MAAM,UAAU;AAChB,SAAQ,iBAAiB,KAAK,IAAI,IAAI,QAAQ,kBAAkB,KAAK,EAAE;AACvE,QAAO,QAAQ"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_core_abTesting = require('./abTesting.cjs');
|
|
3
|
+
const require_core_eventBuffer = require('./eventBuffer.cjs');
|
|
4
|
+
const require_core_sampling = require('./sampling.cjs');
|
|
5
|
+
const require_core_session = require('./session.cjs');
|
|
6
|
+
const require_core_transport = require('./transport.cjs');
|
|
7
|
+
const require_core_AnalyticsClient = require('./AnalyticsClient.cjs');
|
|
8
|
+
const require_core_globalManager = require('./globalManager.cjs');
|
|
9
|
+
const require_core_initAnalyticsClient = require('./initAnalyticsClient.cjs');
|
|
10
|
+
|
|
11
|
+
exports.DEFAULT_FLUSH_INTERVAL = require_core_AnalyticsClient.DEFAULT_FLUSH_INTERVAL;
|
|
12
|
+
exports.DEFAULT_MAX_BUFFER_SIZE = require_core_AnalyticsClient.DEFAULT_MAX_BUFFER_SIZE;
|
|
13
|
+
exports.DEFAULT_SAMPLE_RATE = require_core_AnalyticsClient.DEFAULT_SAMPLE_RATE;
|
|
14
|
+
exports.MAX_EVENTS_PER_SEND = require_core_AnalyticsClient.MAX_EVENTS_PER_SEND;
|
|
15
|
+
exports.assignVariant = require_core_abTesting.assignVariant;
|
|
16
|
+
exports.createAnalyticsClient = require_core_AnalyticsClient.createAnalyticsClient;
|
|
17
|
+
exports.createEventBuffer = require_core_eventBuffer.createEventBuffer;
|
|
18
|
+
exports.decrementAnalyticsClientRefCount = require_core_globalManager.decrementAnalyticsClientRefCount;
|
|
19
|
+
exports.getGlobalAnalyticsClient = require_core_globalManager.getGlobalAnalyticsClient;
|
|
20
|
+
exports.getSessionId = require_core_session.getSessionId;
|
|
21
|
+
exports.incrementAnalyticsClientRefCount = require_core_globalManager.incrementAnalyticsClientRefCount;
|
|
22
|
+
exports.initAnalyticsClient = require_core_initAnalyticsClient.initAnalyticsClient;
|
|
23
|
+
exports.isSampledIn = require_core_sampling.isSampledIn;
|
|
24
|
+
exports.sendEvents = require_core_transport.sendEvents;
|
|
25
|
+
exports.setGlobalAnalyticsClient = require_core_globalManager.setGlobalAnalyticsClient;
|
|
26
|
+
exports.stopAnalyticsClient = require_core_initAnalyticsClient.stopAnalyticsClient;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_core_AnalyticsClient = require('./AnalyticsClient.cjs');
|
|
3
|
+
const require_core_globalManager = require('./globalManager.cjs');
|
|
4
|
+
let _intlayer_config_built = require("@intlayer/config/built");
|
|
5
|
+
|
|
6
|
+
//#region src/core/initAnalyticsClient.ts
|
|
7
|
+
/**
|
|
8
|
+
* Initializes the Intlayer analytics client singleton.
|
|
9
|
+
*
|
|
10
|
+
* Behavior comes from the `analytics` config block (`flushInterval`,
|
|
11
|
+
* `sampleRate`); attribution reuses the existing `editor` config
|
|
12
|
+
* (`editor.backendURL` for the endpoint, `editor.clientId` for the project).
|
|
13
|
+
*
|
|
14
|
+
* Safe to call multiple times: returns the existing client and increments a
|
|
15
|
+
* `window`-shared reference count so nested providers (or duplicated module
|
|
16
|
+
* instances) don't stop the client prematurely.
|
|
17
|
+
*
|
|
18
|
+
* @returns The started analytics client singleton.
|
|
19
|
+
*/
|
|
20
|
+
const initAnalyticsClient = () => {
|
|
21
|
+
require_core_globalManager.incrementAnalyticsClientRefCount();
|
|
22
|
+
const existing = require_core_globalManager.getGlobalAnalyticsClient();
|
|
23
|
+
if (existing) return existing;
|
|
24
|
+
const client = require_core_AnalyticsClient.createAnalyticsClient({
|
|
25
|
+
backendURL: _intlayer_config_built.editor?.backendURL ?? "",
|
|
26
|
+
clientId: _intlayer_config_built.editor?.clientId,
|
|
27
|
+
flushInterval: _intlayer_config_built.analytics?.flushInterval ?? 2e4,
|
|
28
|
+
maxBufferSize: 500,
|
|
29
|
+
sampleRate: _intlayer_config_built.analytics?.sampleRate ?? 1
|
|
30
|
+
});
|
|
31
|
+
require_core_globalManager.setGlobalAnalyticsClient(client);
|
|
32
|
+
client.start();
|
|
33
|
+
return client;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Decrements the shared reference count and stops the global analytics client
|
|
37
|
+
* only when the last provider unmounts (flushing any remaining events).
|
|
38
|
+
*
|
|
39
|
+
* Callers must pair each `stopAnalyticsClient` with a prior
|
|
40
|
+
* `initAnalyticsClient` — never call it when init was skipped or cancelled.
|
|
41
|
+
*/
|
|
42
|
+
const stopAnalyticsClient = () => {
|
|
43
|
+
if (require_core_globalManager.decrementAnalyticsClientRefCount() > 0) return;
|
|
44
|
+
require_core_globalManager.getGlobalAnalyticsClient()?.stop();
|
|
45
|
+
require_core_globalManager.setGlobalAnalyticsClient(null);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
//#endregion
|
|
49
|
+
exports.initAnalyticsClient = initAnalyticsClient;
|
|
50
|
+
exports.stopAnalyticsClient = stopAnalyticsClient;
|
|
51
|
+
//# sourceMappingURL=initAnalyticsClient.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"initAnalyticsClient.cjs","names":["getGlobalAnalyticsClient","createAnalyticsClient","editor","analytics","decrementAnalyticsClientRefCount"],"sources":["../../../src/core/initAnalyticsClient.ts"],"sourcesContent":["import { analytics, editor } from '@intlayer/config/built';\nimport {\n type AnalyticsClient,\n createAnalyticsClient,\n DEFAULT_FLUSH_INTERVAL,\n DEFAULT_MAX_BUFFER_SIZE,\n DEFAULT_SAMPLE_RATE,\n} from './AnalyticsClient';\nimport {\n decrementAnalyticsClientRefCount,\n getGlobalAnalyticsClient,\n incrementAnalyticsClientRefCount,\n setGlobalAnalyticsClient,\n} from './globalManager';\n\n/**\n * Initializes the Intlayer analytics client singleton.\n *\n * Behavior comes from the `analytics` config block (`flushInterval`,\n * `sampleRate`); attribution reuses the existing `editor` config\n * (`editor.backendURL` for the endpoint, `editor.clientId` for the project).\n *\n * Safe to call multiple times: returns the existing client and increments a\n * `window`-shared reference count so nested providers (or duplicated module\n * instances) don't stop the client prematurely.\n *\n * @returns The started analytics client singleton.\n */\nexport const initAnalyticsClient = (): AnalyticsClient => {\n incrementAnalyticsClientRefCount();\n\n const existing = getGlobalAnalyticsClient();\n if (existing) return existing;\n\n const client = createAnalyticsClient({\n backendURL: editor?.backendURL ?? '',\n clientId: editor?.clientId,\n flushInterval: analytics?.flushInterval ?? DEFAULT_FLUSH_INTERVAL,\n maxBufferSize: DEFAULT_MAX_BUFFER_SIZE,\n sampleRate: analytics?.sampleRate ?? DEFAULT_SAMPLE_RATE,\n });\n\n setGlobalAnalyticsClient(client);\n client.start();\n\n return client;\n};\n\n/**\n * Decrements the shared reference count and stops the global analytics client\n * only when the last provider unmounts (flushing any remaining events).\n *\n * Callers must pair each `stopAnalyticsClient` with a prior\n * `initAnalyticsClient` — never call it when init was skipped or cancelled.\n */\nexport const stopAnalyticsClient = (): void => {\n if (decrementAnalyticsClientRefCount() > 0) return;\n\n const client = getGlobalAnalyticsClient();\n client?.stop();\n setGlobalAnalyticsClient(null);\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA4BA,MAAa,4BAA6C;AACxD,8DAAkC;CAElC,MAAM,WAAWA,qDAA0B;AAC3C,KAAI,SAAU,QAAO;CAErB,MAAM,SAASC,mDAAsB;EACnC,YAAYC,+BAAQ,cAAc;EAClC,UAAUA,+BAAQ;EAClB,eAAeC,kCAAW;EAC1B;EACA,YAAYA,kCAAW;EACxB,CAAC;AAEF,qDAAyB,OAAO;AAChC,QAAO,OAAO;AAEd,QAAO;;;;;;;;;AAUT,MAAa,4BAAkC;AAC7C,KAAIC,6DAAkC,GAAG,EAAG;AAG5C,CADeJ,qDACT,EAAE,MAAM;AACd,qDAAyB,KAAK"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_utils_hash = require('../utils/hash.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/core/sampling.ts
|
|
5
|
+
/**
|
|
6
|
+
* Deterministic per-session sampling gate. Using the session id (rather than a
|
|
7
|
+
* fresh random draw per event) means a sampled-in session reports *all* of its
|
|
8
|
+
* content exposures, keeping per-session funnels consistent instead of leaving
|
|
9
|
+
* holes.
|
|
10
|
+
*
|
|
11
|
+
* @param sessionId - The current anonymous session id.
|
|
12
|
+
* @param sampleRate - Fraction of sessions to keep, from 0 (none) to 1 (all).
|
|
13
|
+
* @returns `true` when this session's events should be recorded.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* isSampledIn('abc', 1); // always true
|
|
17
|
+
* isSampledIn('abc', 0); // always false
|
|
18
|
+
* isSampledIn('abc', 0.5); // stable per session
|
|
19
|
+
*/
|
|
20
|
+
const isSampledIn = (sessionId, sampleRate) => {
|
|
21
|
+
if (sampleRate >= 1) return true;
|
|
22
|
+
if (sampleRate <= 0) return false;
|
|
23
|
+
return require_utils_hash.hashString(sessionId) / 4294967295 < sampleRate;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
exports.isSampledIn = isSampledIn;
|
|
28
|
+
//# sourceMappingURL=sampling.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sampling.cjs","names":["hashString"],"sources":["../../../src/core/sampling.ts"],"sourcesContent":["import { hashString } from '../utils/hash';\n\n/**\n * Deterministic per-session sampling gate. Using the session id (rather than a\n * fresh random draw per event) means a sampled-in session reports *all* of its\n * content exposures, keeping per-session funnels consistent instead of leaving\n * holes.\n *\n * @param sessionId - The current anonymous session id.\n * @param sampleRate - Fraction of sessions to keep, from 0 (none) to 1 (all).\n * @returns `true` when this session's events should be recorded.\n *\n * @example\n * isSampledIn('abc', 1); // always true\n * isSampledIn('abc', 0); // always false\n * isSampledIn('abc', 0.5); // stable per session\n */\nexport const isSampledIn = (sessionId: string, sampleRate: number): boolean => {\n if (sampleRate >= 1) return true;\n if (sampleRate <= 0) return false;\n\n const bucket = hashString(sessionId) / 0xffffffff;\n return bucket < sampleRate;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAiBA,MAAa,eAAe,WAAmB,eAAgC;AAC7E,KAAI,cAAc,EAAG,QAAO;AAC5B,KAAI,cAAc,EAAG,QAAO;AAG5B,QADeA,8BAAW,UAAU,GAAG,aACvB"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
|
|
3
|
+
//#region src/core/session.ts
|
|
4
|
+
/**
|
|
5
|
+
* Anonymous, per-tab session identity. Stored in `sessionStorage` so it is
|
|
6
|
+
* cleared when the tab closes and is never shared across sites — no durable
|
|
7
|
+
* cookie, no cross-site tracking, no PII.
|
|
8
|
+
*/
|
|
9
|
+
const SESSION_STORAGE_KEY = "__intlayer_analytics_sid__";
|
|
10
|
+
/**
|
|
11
|
+
* Generates a random, opaque session identifier.
|
|
12
|
+
*
|
|
13
|
+
* @returns A URL-safe random string.
|
|
14
|
+
*/
|
|
15
|
+
const generateSessionId = () => {
|
|
16
|
+
const cryptoObj = typeof globalThis !== "undefined" ? globalThis.crypto : void 0;
|
|
17
|
+
if (cryptoObj?.randomUUID) return cryptoObj.randomUUID();
|
|
18
|
+
return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Returns the current session id, creating and persisting one on first access.
|
|
22
|
+
* Degrades gracefully to an in-memory id when `sessionStorage` is unavailable
|
|
23
|
+
* (private mode, native runtimes).
|
|
24
|
+
*
|
|
25
|
+
* @returns The stable session id for the lifetime of the tab.
|
|
26
|
+
*/
|
|
27
|
+
const getSessionId = () => {
|
|
28
|
+
if (typeof window === "undefined" || !window.sessionStorage) return generateSessionId();
|
|
29
|
+
try {
|
|
30
|
+
const existing = window.sessionStorage.getItem(SESSION_STORAGE_KEY);
|
|
31
|
+
if (existing) return existing;
|
|
32
|
+
const created = generateSessionId();
|
|
33
|
+
window.sessionStorage.setItem(SESSION_STORAGE_KEY, created);
|
|
34
|
+
return created;
|
|
35
|
+
} catch {
|
|
36
|
+
return generateSessionId();
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
//#endregion
|
|
41
|
+
exports.getSessionId = getSessionId;
|
|
42
|
+
//# sourceMappingURL=session.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.cjs","names":[],"sources":["../../../src/core/session.ts"],"sourcesContent":["/**\n * Anonymous, per-tab session identity. Stored in `sessionStorage` so it is\n * cleared when the tab closes and is never shared across sites — no durable\n * cookie, no cross-site tracking, no PII.\n */\n\nconst SESSION_STORAGE_KEY = '__intlayer_analytics_sid__';\n\n/**\n * Generates a random, opaque session identifier.\n *\n * @returns A URL-safe random string.\n */\nconst generateSessionId = (): string => {\n const cryptoObj =\n typeof globalThis !== 'undefined'\n ? (globalThis.crypto as Crypto | undefined)\n : undefined;\n\n if (cryptoObj?.randomUUID) {\n return cryptoObj.randomUUID();\n }\n\n // Fallback for environments without crypto.randomUUID (older RN/Lynx)\n return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;\n};\n\n/**\n * Returns the current session id, creating and persisting one on first access.\n * Degrades gracefully to an in-memory id when `sessionStorage` is unavailable\n * (private mode, native runtimes).\n *\n * @returns The stable session id for the lifetime of the tab.\n */\nexport const getSessionId = (): string => {\n if (typeof window === 'undefined' || !window.sessionStorage) {\n return generateSessionId();\n }\n\n try {\n const existing = window.sessionStorage.getItem(SESSION_STORAGE_KEY);\n if (existing) return existing;\n\n const created = generateSessionId();\n window.sessionStorage.setItem(SESSION_STORAGE_KEY, created);\n return created;\n } catch {\n // sessionStorage can throw (Safari private mode, quota) — never break the app\n return generateSessionId();\n }\n};\n"],"mappings":";;;;;;;;AAMA,MAAM,sBAAsB;;;;;;AAO5B,MAAM,0BAAkC;CACtC,MAAM,YACJ,OAAO,eAAe,cACjB,WAAW,SACZ;AAEN,KAAI,WAAW,WACb,QAAO,UAAU,YAAY;AAI/B,QAAO,GAAG,KAAK,KAAK,CAAC,SAAS,GAAG,CAAC,GAAG,KAAK,QAAQ,CAAC,SAAS,GAAG,CAAC,MAAM,GAAG,GAAG;;;;;;;;;AAU9E,MAAa,qBAA6B;AACxC,KAAI,OAAO,WAAW,eAAe,CAAC,OAAO,eAC3C,QAAO,mBAAmB;AAG5B,KAAI;EACF,MAAM,WAAW,OAAO,eAAe,QAAQ,oBAAoB;AACnE,MAAI,SAAU,QAAO;EAErB,MAAM,UAAU,mBAAmB;AACnC,SAAO,eAAe,QAAQ,qBAAqB,QAAQ;AAC3D,SAAO;SACD;AAEN,SAAO,mBAAmB"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
|
|
3
|
+
//#region src/core/transport.ts
|
|
4
|
+
/**
|
|
5
|
+
* Sends a batch of events to the backend. Every path is non-blocking and
|
|
6
|
+
* no-throw — analytics must never break, slow, or delay the host application.
|
|
7
|
+
*
|
|
8
|
+
* Strategy:
|
|
9
|
+
* 1. `navigator.sendBeacon` for the flush-on-hide path — survives page unload
|
|
10
|
+
* and never blocks navigation. The payload is sent as `text/plain` (a
|
|
11
|
+
* CORS-safelisted content type): a JSON `Blob` would require a CORS
|
|
12
|
+
* preflight that browsers handle inconsistently during unload, silently
|
|
13
|
+
* dropping the beacon. The backend parses `text/plain` bodies as JSON.
|
|
14
|
+
* 2. `fetch(..., { keepalive: true })` for interval flushes — survives short
|
|
15
|
+
* navigations without holding the main thread.
|
|
16
|
+
* 3. Plain `fetch` fallback for runtimes without `keepalive` (older RN/Lynx).
|
|
17
|
+
*
|
|
18
|
+
* @param endpoint - Fully-qualified ingestion URL.
|
|
19
|
+
* @param body - The batched payload.
|
|
20
|
+
* @param options - Transport options.
|
|
21
|
+
* @param options.useBeacon - Prefer `sendBeacon` (used when the page is hiding).
|
|
22
|
+
* @returns `true` when a send was dispatched, `false` otherwise.
|
|
23
|
+
*/
|
|
24
|
+
const sendEvents = (endpoint, body, { useBeacon = false } = {}) => {
|
|
25
|
+
try {
|
|
26
|
+
const payload = JSON.stringify(body);
|
|
27
|
+
if (useBeacon && typeof navigator !== "undefined" && typeof navigator.sendBeacon === "function") return navigator.sendBeacon(endpoint, payload);
|
|
28
|
+
if (typeof fetch === "function") {
|
|
29
|
+
fetch(endpoint, {
|
|
30
|
+
method: "POST",
|
|
31
|
+
headers: { "Content-Type": "application/json" },
|
|
32
|
+
body: payload,
|
|
33
|
+
keepalive: true,
|
|
34
|
+
credentials: "omit",
|
|
35
|
+
mode: "cors"
|
|
36
|
+
}).catch(() => {});
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
40
|
+
} catch {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
//#endregion
|
|
46
|
+
exports.sendEvents = sendEvents;
|
|
47
|
+
//# sourceMappingURL=transport.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transport.cjs","names":[],"sources":["../../../src/core/transport.ts"],"sourcesContent":["import type { AnalyticsIngestBody } from './types';\n\n/**\n * Sends a batch of events to the backend. Every path is non-blocking and\n * no-throw — analytics must never break, slow, or delay the host application.\n *\n * Strategy:\n * 1. `navigator.sendBeacon` for the flush-on-hide path — survives page unload\n * and never blocks navigation. The payload is sent as `text/plain` (a\n * CORS-safelisted content type): a JSON `Blob` would require a CORS\n * preflight that browsers handle inconsistently during unload, silently\n * dropping the beacon. The backend parses `text/plain` bodies as JSON.\n * 2. `fetch(..., { keepalive: true })` for interval flushes — survives short\n * navigations without holding the main thread.\n * 3. Plain `fetch` fallback for runtimes without `keepalive` (older RN/Lynx).\n *\n * @param endpoint - Fully-qualified ingestion URL.\n * @param body - The batched payload.\n * @param options - Transport options.\n * @param options.useBeacon - Prefer `sendBeacon` (used when the page is hiding).\n * @returns `true` when a send was dispatched, `false` otherwise.\n */\nexport const sendEvents = (\n endpoint: string,\n body: AnalyticsIngestBody,\n { useBeacon = false }: { useBeacon?: boolean } = {}\n): boolean => {\n try {\n const payload = JSON.stringify(body);\n\n if (\n useBeacon &&\n typeof navigator !== 'undefined' &&\n typeof navigator.sendBeacon === 'function'\n ) {\n // Note: unlike the fetch path below, sendBeacon always includes\n // credentials; the ingest endpoint ignores them.\n return navigator.sendBeacon(endpoint, payload);\n }\n\n if (typeof fetch === 'function') {\n // keepalive lets the request outlive a same-tab navigation.\n void fetch(endpoint, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: payload,\n keepalive: true,\n // Analytics is best-effort; never send credentials.\n credentials: 'omit',\n mode: 'cors',\n }).catch(() => {\n /* swallow — best-effort delivery */\n });\n return true;\n }\n\n return false;\n } catch {\n // JSON serialization failed — drop the batch silently.\n return false;\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAsBA,MAAa,cACX,UACA,MACA,EAAE,YAAY,UAAmC,EAAE,KACvC;AACZ,KAAI;EACF,MAAM,UAAU,KAAK,UAAU,KAAK;AAEpC,MACE,aACA,OAAO,cAAc,eACrB,OAAO,UAAU,eAAe,WAIhC,QAAO,UAAU,WAAW,UAAU,QAAQ;AAGhD,MAAI,OAAO,UAAU,YAAY;AAE/B,GAAK,MAAM,UAAU;IACnB,QAAQ;IACR,SAAS,EAAE,gBAAgB,oBAAoB;IAC/C,MAAM;IACN,WAAW;IAEX,aAAa;IACb,MAAM;IACP,CAAC,CAAC,YAAY,GAEb;AACF,UAAO;;AAGT,SAAO;SACD;AAEN,SAAO"}
|
|
File without changes
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
|
|
3
|
+
//#region src/events/contentExposure.ts
|
|
4
|
+
/**
|
|
5
|
+
* Serializes a `KeyPath[]` into a compact, stable string used both as the
|
|
6
|
+
* coalescing key in the buffer and as the stored `keyPath` on the wire.
|
|
7
|
+
*
|
|
8
|
+
* Only the `key` of each segment is kept (types are implied by the dictionary
|
|
9
|
+
* shape), producing paths like `"faq.items.2.answer"`.
|
|
10
|
+
*
|
|
11
|
+
* @param keyPath - The interpreter key path locating a node.
|
|
12
|
+
* @returns A dot-joined, stable path string.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* serializeKeyPath([{ type: 'object', key: 'faq' }, { type: 'array', key: 2 }]);
|
|
16
|
+
* // "faq.2"
|
|
17
|
+
*/
|
|
18
|
+
const serializeKeyPath = (keyPath) => keyPath.map((segment) => segment.key === void 0 ? "*" : String(segment.key)).join(".");
|
|
19
|
+
/**
|
|
20
|
+
* Builds a content-exposure payload from interpreter node metadata. Shared by
|
|
21
|
+
* every framework binding's analytics node plugin so the wire shape stays
|
|
22
|
+
* identical across React, Vue, Svelte, Angular, etc.
|
|
23
|
+
*
|
|
24
|
+
* @param params - Node metadata available on `NodeProps`.
|
|
25
|
+
* @returns A trackable content-exposure event (minus buffer-stamped fields).
|
|
26
|
+
*/
|
|
27
|
+
const buildContentExposure = (params) => ({
|
|
28
|
+
dictionaryKey: params.dictionaryKey,
|
|
29
|
+
keyPath: serializeKeyPath(params.keyPath),
|
|
30
|
+
locale: params.locale,
|
|
31
|
+
nodeType: params.nodeType,
|
|
32
|
+
experimentKey: params.experimentKey,
|
|
33
|
+
variant: params.variant
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
//#endregion
|
|
37
|
+
exports.buildContentExposure = buildContentExposure;
|
|
38
|
+
exports.serializeKeyPath = serializeKeyPath;
|
|
39
|
+
//# sourceMappingURL=contentExposure.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contentExposure.cjs","names":[],"sources":["../../../src/events/contentExposure.ts"],"sourcesContent":["import type { KeyPath } from '@intlayer/types/keyPath';\nimport type { ContentExposureEvent, TrackableEvent } from '../core/types';\n\n/**\n * Serializes a `KeyPath[]` into a compact, stable string used both as the\n * coalescing key in the buffer and as the stored `keyPath` on the wire.\n *\n * Only the `key` of each segment is kept (types are implied by the dictionary\n * shape), producing paths like `\"faq.items.2.answer\"`.\n *\n * @param keyPath - The interpreter key path locating a node.\n * @returns A dot-joined, stable path string.\n *\n * @example\n * serializeKeyPath([{ type: 'object', key: 'faq' }, { type: 'array', key: 2 }]);\n * // \"faq.2\"\n */\nexport const serializeKeyPath = (keyPath: KeyPath[]): string =>\n keyPath\n .map((segment) => (segment.key === undefined ? '*' : String(segment.key)))\n .join('.');\n\n/**\n * Builds a content-exposure payload from interpreter node metadata. Shared by\n * every framework binding's analytics node plugin so the wire shape stays\n * identical across React, Vue, Svelte, Angular, etc.\n *\n * @param params - Node metadata available on `NodeProps`.\n * @returns A trackable content-exposure event (minus buffer-stamped fields).\n */\nexport const buildContentExposure = (params: {\n dictionaryKey: string;\n keyPath: KeyPath[];\n locale?: string;\n nodeType?: string;\n experimentKey?: string;\n variant?: string;\n}): Omit<TrackableEvent<ContentExposureEvent>, 'type'> => ({\n dictionaryKey: params.dictionaryKey,\n keyPath: serializeKeyPath(params.keyPath),\n // Left undefined when unknown so the client stamps its ambient locale.\n locale: params.locale,\n nodeType: params.nodeType,\n experimentKey: params.experimentKey,\n variant: params.variant,\n});\n"],"mappings":";;;;;;;;;;;;;;;;;AAiBA,MAAa,oBAAoB,YAC/B,QACG,KAAK,YAAa,QAAQ,QAAQ,SAAY,MAAM,OAAO,QAAQ,IAAI,CAAE,CACzE,KAAK,IAAI;;;;;;;;;AAUd,MAAa,wBAAwB,YAOsB;CACzD,eAAe,OAAO;CACtB,SAAS,iBAAiB,OAAO,QAAQ;CAEzC,QAAQ,OAAO;CACf,UAAU,OAAO;CACjB,eAAe,OAAO;CACtB,SAAS,OAAO;CACjB"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_core_abTesting = require('./core/abTesting.cjs');
|
|
3
|
+
const require_core_eventBuffer = require('./core/eventBuffer.cjs');
|
|
4
|
+
const require_core_sampling = require('./core/sampling.cjs');
|
|
5
|
+
const require_core_session = require('./core/session.cjs');
|
|
6
|
+
const require_core_transport = require('./core/transport.cjs');
|
|
7
|
+
const require_core_AnalyticsClient = require('./core/AnalyticsClient.cjs');
|
|
8
|
+
const require_core_globalManager = require('./core/globalManager.cjs');
|
|
9
|
+
const require_core_initAnalyticsClient = require('./core/initAnalyticsClient.cjs');
|
|
10
|
+
const require_events_contentExposure = require('./events/contentExposure.cjs');
|
|
11
|
+
|
|
12
|
+
exports.DEFAULT_FLUSH_INTERVAL = require_core_AnalyticsClient.DEFAULT_FLUSH_INTERVAL;
|
|
13
|
+
exports.DEFAULT_MAX_BUFFER_SIZE = require_core_AnalyticsClient.DEFAULT_MAX_BUFFER_SIZE;
|
|
14
|
+
exports.DEFAULT_SAMPLE_RATE = require_core_AnalyticsClient.DEFAULT_SAMPLE_RATE;
|
|
15
|
+
exports.MAX_EVENTS_PER_SEND = require_core_AnalyticsClient.MAX_EVENTS_PER_SEND;
|
|
16
|
+
exports.assignVariant = require_core_abTesting.assignVariant;
|
|
17
|
+
exports.buildContentExposure = require_events_contentExposure.buildContentExposure;
|
|
18
|
+
exports.createAnalyticsClient = require_core_AnalyticsClient.createAnalyticsClient;
|
|
19
|
+
exports.createEventBuffer = require_core_eventBuffer.createEventBuffer;
|
|
20
|
+
exports.decrementAnalyticsClientRefCount = require_core_globalManager.decrementAnalyticsClientRefCount;
|
|
21
|
+
exports.getGlobalAnalyticsClient = require_core_globalManager.getGlobalAnalyticsClient;
|
|
22
|
+
exports.getSessionId = require_core_session.getSessionId;
|
|
23
|
+
exports.incrementAnalyticsClientRefCount = require_core_globalManager.incrementAnalyticsClientRefCount;
|
|
24
|
+
exports.initAnalyticsClient = require_core_initAnalyticsClient.initAnalyticsClient;
|
|
25
|
+
exports.isSampledIn = require_core_sampling.isSampledIn;
|
|
26
|
+
exports.sendEvents = require_core_transport.sendEvents;
|
|
27
|
+
exports.serializeKeyPath = require_events_contentExposure.serializeKeyPath;
|
|
28
|
+
exports.setGlobalAnalyticsClient = require_core_globalManager.setGlobalAnalyticsClient;
|
|
29
|
+
exports.stopAnalyticsClient = require_core_initAnalyticsClient.stopAnalyticsClient;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
let _intlayer_config_built = require("@intlayer/config/built");
|
|
3
|
+
|
|
4
|
+
//#region src/isEnabled.ts
|
|
5
|
+
/**
|
|
6
|
+
* Compile-time kill switch. Bundlers replace `process.env.INTLAYER_ANALYTICS_ENABLED`
|
|
7
|
+
* (set to `'false'` by `@intlayer/config` unless `analytics.enabled` is `true`
|
|
8
|
+
* AND an `editor.clientId` is configured) so the whole analytics integration is
|
|
9
|
+
* dead-code-eliminated for apps that don't opt in.
|
|
10
|
+
*/
|
|
11
|
+
const TREE_SHAKE_ANALYTICS = process.env.INTLAYER_ANALYTICS_ENABLED === "false";
|
|
12
|
+
/**
|
|
13
|
+
* Runtime guard mirroring `@intlayer/editor`'s `isEnabled`, but inverted with
|
|
14
|
+
* respect to the iframe: analytics runs for real end users on the **top**
|
|
15
|
+
* window and stays off inside the editor/CMS preview iframe so editor sessions
|
|
16
|
+
* are never counted as traffic.
|
|
17
|
+
*
|
|
18
|
+
* Enabled when:
|
|
19
|
+
* - the bundler did not tree-shake analytics away, and
|
|
20
|
+
* - the user opted in (`analytics.enabled === true`), and
|
|
21
|
+
* - a project key is configured (`editor.clientId`), and
|
|
22
|
+
* - we are on the client, and
|
|
23
|
+
* - we are the top window (not embedded in the editor).
|
|
24
|
+
*/
|
|
25
|
+
const isEnabled = !TREE_SHAKE_ANALYTICS && _intlayer_config_built.analytics?.enabled === true && Boolean(_intlayer_config_built.editor?.clientId) && typeof window !== "undefined" && window.self === window.top;
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
exports.isEnabled = isEnabled;
|
|
29
|
+
//# sourceMappingURL=isEnabled.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isEnabled.cjs","names":["analytics","editor"],"sources":["../../src/isEnabled.ts"],"sourcesContent":["import { analytics, editor } from '@intlayer/config/built';\n\n/**\n * Compile-time kill switch. Bundlers replace `process.env.INTLAYER_ANALYTICS_ENABLED`\n * (set to `'false'` by `@intlayer/config` unless `analytics.enabled` is `true`\n * AND an `editor.clientId` is configured) so the whole analytics integration is\n * dead-code-eliminated for apps that don't opt in.\n */\nconst TREE_SHAKE_ANALYTICS = process.env.INTLAYER_ANALYTICS_ENABLED === 'false';\n\n/**\n * Runtime guard mirroring `@intlayer/editor`'s `isEnabled`, but inverted with\n * respect to the iframe: analytics runs for real end users on the **top**\n * window and stays off inside the editor/CMS preview iframe so editor sessions\n * are never counted as traffic.\n *\n * Enabled when:\n * - the bundler did not tree-shake analytics away, and\n * - the user opted in (`analytics.enabled === true`), and\n * - a project key is configured (`editor.clientId`), and\n * - we are on the client, and\n * - we are the top window (not embedded in the editor).\n */\nexport const isEnabled =\n !TREE_SHAKE_ANALYTICS &&\n analytics?.enabled === true &&\n Boolean(editor?.clientId) &&\n typeof window !== 'undefined' &&\n window.self === window.top;\n"],"mappings":";;;;;;;;;;AAQA,MAAM,uBAAuB,QAAQ,IAAI,+BAA+B;;;;;;;;;;;;;;AAexE,MAAa,YACX,CAAC,wBACDA,kCAAW,YAAY,QACvB,QAAQC,+BAAQ,SAAS,IACzB,OAAO,WAAW,eAClB,OAAO,SAAS,OAAO"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
|
|
3
|
+
//#region src/utils/hash.ts
|
|
4
|
+
/**
|
|
5
|
+
* FNV-1a 32-bit string hash. Small, fast, dependency-free, and stable across
|
|
6
|
+
* runtimes — good enough for deterministic A/B bucketing and session seeding
|
|
7
|
+
* (not for cryptographic use).
|
|
8
|
+
*
|
|
9
|
+
* @param input - The string to hash.
|
|
10
|
+
* @returns An unsigned 32-bit integer hash.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* hashString('session-1:homepage-hero'); // 3039336720
|
|
14
|
+
*/
|
|
15
|
+
const hashString = (input) => {
|
|
16
|
+
let hash = 2166136261;
|
|
17
|
+
for (let index = 0; index < input.length; index++) {
|
|
18
|
+
hash ^= input.charCodeAt(index);
|
|
19
|
+
hash = Math.imul(hash, 16777619);
|
|
20
|
+
}
|
|
21
|
+
return hash >>> 0;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
exports.hashString = hashString;
|
|
26
|
+
//# sourceMappingURL=hash.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash.cjs","names":[],"sources":["../../../src/utils/hash.ts"],"sourcesContent":["/**\n * FNV-1a 32-bit string hash. Small, fast, dependency-free, and stable across\n * runtimes — good enough for deterministic A/B bucketing and session seeding\n * (not for cryptographic use).\n *\n * @param input - The string to hash.\n * @returns An unsigned 32-bit integer hash.\n *\n * @example\n * hashString('session-1:homepage-hero'); // 3039336720\n */\nexport const hashString = (input: string): number => {\n let hash = 0x811c9dc5; // FNV offset basis\n\n for (let index = 0; index < input.length; index++) {\n hash ^= input.charCodeAt(index);\n // 32-bit FNV prime multiplication via shifts to stay within Number safe range\n hash = Math.imul(hash, 0x01000193);\n }\n\n // Coerce to unsigned 32-bit\n return hash >>> 0;\n};\n"],"mappings":";;;;;;;;;;;;;;AAWA,MAAa,cAAc,UAA0B;CACnD,IAAI,OAAO;AAEX,MAAK,IAAI,QAAQ,GAAG,QAAQ,MAAM,QAAQ,SAAS;AACjD,UAAQ,MAAM,WAAW,MAAM;AAE/B,SAAO,KAAK,KAAK,MAAM,SAAW;;AAIpC,QAAO,SAAS"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
|
|
3
|
+
//#region src/utils/idle.ts
|
|
4
|
+
/**
|
|
5
|
+
* Runs a callback when the browser is idle, without blocking first render.
|
|
6
|
+
* Falls back to a short `setTimeout` where `requestIdleCallback` is unavailable
|
|
7
|
+
* (Safari, React Native, Lynx).
|
|
8
|
+
*
|
|
9
|
+
* @param callback - Work to run once the main thread is free.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* onIdle(() => attachVisibilityListeners());
|
|
13
|
+
*/
|
|
14
|
+
const onIdle = (callback) => {
|
|
15
|
+
if (typeof window === "undefined") return;
|
|
16
|
+
const requestIdle = window.requestIdleCallback;
|
|
17
|
+
if (typeof requestIdle === "function") {
|
|
18
|
+
requestIdle(() => callback());
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
setTimeout(callback, 1);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
exports.onIdle = onIdle;
|
|
26
|
+
//# sourceMappingURL=idle.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"idle.cjs","names":[],"sources":["../../../src/utils/idle.ts"],"sourcesContent":["/**\n * Runs a callback when the browser is idle, without blocking first render.\n * Falls back to a short `setTimeout` where `requestIdleCallback` is unavailable\n * (Safari, React Native, Lynx).\n *\n * @param callback - Work to run once the main thread is free.\n *\n * @example\n * onIdle(() => attachVisibilityListeners());\n */\nexport const onIdle = (callback: () => void): void => {\n if (typeof window === 'undefined') return;\n\n const requestIdle = (\n window as Window & {\n requestIdleCallback?: (cb: () => void) => number;\n }\n ).requestIdleCallback;\n\n if (typeof requestIdle === 'function') {\n requestIdle(() => callback());\n return;\n }\n\n setTimeout(callback, 1);\n};\n"],"mappings":";;;;;;;;;;;;;AAUA,MAAa,UAAU,aAA+B;AACpD,KAAI,OAAO,WAAW,YAAa;CAEnC,MAAM,cACJ,OAGA;AAEF,KAAI,OAAO,gBAAgB,YAAY;AACrC,oBAAkB,UAAU,CAAC;AAC7B;;AAGF,YAAW,UAAU,EAAE"}
|