@amplitude/analytics-core 2.35.1 → 2.36.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.
@@ -0,0 +1,21 @@
1
+ type ConsoleLogLevel = keyof Console;
2
+ type Callback = (logLevel: ConsoleLogLevel, args: any[]) => void;
3
+ /**
4
+ * Observe a console log method (log, warn, error, etc.)
5
+ * @param level - The console log level to observe
6
+ * @param callback - The callback function to call when the console log level is observed
7
+ */
8
+ declare function addListener(level: ConsoleLogLevel, callback: Callback): Error | void;
9
+ /**
10
+ * Disconnect a callback function from a console log method
11
+ * @param callback - The callback function to disconnect
12
+ */
13
+ declare function removeListener(callback: Callback): void;
14
+ declare function _restoreConsole(): void;
15
+ declare const consoleObserver: {
16
+ addListener: typeof addListener;
17
+ removeListener: typeof removeListener;
18
+ _restoreConsole: typeof _restoreConsole;
19
+ };
20
+ export { consoleObserver, ConsoleLogLevel };
21
+ //# sourceMappingURL=console.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"console.d.ts","sourceRoot":"","sources":["../../../src/observers/console.ts"],"names":[],"mappings":"AAEA,KAAK,eAAe,GAAG,MAAM,OAAO,CAAC;AACrC,KAAK,QAAQ,GAAG,CAAC,QAAQ,EAAE,eAAe,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;AA4DjE;;;;GAIG;AACH,iBAAS,WAAW,CAAC,KAAK,EAAE,eAAe,EAAE,QAAQ,EAAE,QAAQ,GAAG,KAAK,GAAG,IAAI,CAc7E;AAED;;;GAGG;AACH,iBAAS,cAAc,CAAC,QAAQ,EAAE,QAAQ,QAUzC;AAID,iBAAS,eAAe,SASvB;AAED,QAAA,MAAM,eAAe;;;;CAIpB,CAAC;AAEF,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,CAAC"}
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.consoleObserver = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var global_scope_1 = require("../global-scope");
6
+ var globalScope = (0, global_scope_1.getGlobalScope)();
7
+ /* istanbul ignore next */
8
+ var originalConsole = globalScope === null || globalScope === void 0 ? void 0 : globalScope.console;
9
+ var handlers = {};
10
+ // keeps reference to original console methods
11
+ var originalFn = {};
12
+ var inConsoleOverride = false;
13
+ function overrideConsole(logLevel) {
14
+ /* istanbul ignore if */
15
+ if (!originalConsole) {
16
+ return false;
17
+ }
18
+ // should not override if original console property is not a function
19
+ /* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access */
20
+ if (typeof originalConsole[logLevel] !== 'function') {
21
+ return false;
22
+ }
23
+ // if console is already overridden, return true
24
+ if (originalFn[logLevel]) {
25
+ return true;
26
+ }
27
+ // override console method
28
+ var handler = function () {
29
+ var args = [];
30
+ for (var _i = 0; _i < arguments.length; _i++) {
31
+ args[_i] = arguments[_i];
32
+ }
33
+ try {
34
+ if (handlers[logLevel] && !inConsoleOverride) {
35
+ // add a re-entrancy guard to prevent infinite recursion
36
+ inConsoleOverride = true;
37
+ var callbacks = handlers[logLevel];
38
+ if (callbacks) {
39
+ callbacks.forEach(function (callback) {
40
+ try {
41
+ callback(logLevel, args);
42
+ }
43
+ catch (_a) {
44
+ // do nothing
45
+ }
46
+ });
47
+ }
48
+ }
49
+ }
50
+ catch (_a) {
51
+ // do nothing
52
+ }
53
+ inConsoleOverride = false;
54
+ return originalFn[logLevel].apply(originalConsole, args);
55
+ };
56
+ /* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access */
57
+ originalFn[logLevel] = originalConsole[logLevel];
58
+ /* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access */
59
+ originalConsole[logLevel] = handler;
60
+ return true;
61
+ }
62
+ /**
63
+ * Observe a console log method (log, warn, error, etc.)
64
+ * @param level - The console log level to observe
65
+ * @param callback - The callback function to call when the console log level is observed
66
+ */
67
+ function addListener(level, callback) {
68
+ var res = overrideConsole(level);
69
+ /* istanbul ignore if */
70
+ if (!res) {
71
+ return new Error('Console override failed');
72
+ }
73
+ if (handlers[level]) {
74
+ // using ! is safe because we know the key exists based on condition above
75
+ handlers[level].push(callback);
76
+ }
77
+ else {
78
+ handlers[level] = [callback];
79
+ }
80
+ }
81
+ /**
82
+ * Disconnect a callback function from a console log method
83
+ * @param callback - The callback function to disconnect
84
+ */
85
+ function removeListener(callback) {
86
+ var e_1, _a;
87
+ try {
88
+ for (var _b = tslib_1.__values(Object.values(handlers)), _c = _b.next(); !_c.done; _c = _b.next()) {
89
+ var callbacks = _c.value;
90
+ // iterate backwards to avoid index shifting
91
+ for (var i = callbacks.length - 1; i >= 0; i--) {
92
+ if (callbacks[i] === callback) {
93
+ callbacks.splice(i, 1);
94
+ break;
95
+ }
96
+ }
97
+ }
98
+ }
99
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
100
+ finally {
101
+ try {
102
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
103
+ }
104
+ finally { if (e_1) throw e_1.error; }
105
+ }
106
+ }
107
+ // this should only be used for testing
108
+ // restoring console can break console overrides
109
+ function _restoreConsole() {
110
+ var e_2, _a;
111
+ try {
112
+ for (var _b = tslib_1.__values(Object.entries(originalFn)), _c = _b.next(); !_c.done; _c = _b.next()) {
113
+ var _d = tslib_1.__read(_c.value, 2), key = _d[0], originalHandler = _d[1];
114
+ if (originalHandler) {
115
+ /* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access */
116
+ originalConsole[key] = originalHandler;
117
+ }
118
+ }
119
+ }
120
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
121
+ finally {
122
+ try {
123
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
124
+ }
125
+ finally { if (e_2) throw e_2.error; }
126
+ }
127
+ originalFn = {};
128
+ handlers = {};
129
+ }
130
+ var consoleObserver = {
131
+ addListener: addListener,
132
+ removeListener: removeListener,
133
+ _restoreConsole: _restoreConsole,
134
+ };
135
+ exports.consoleObserver = consoleObserver;
136
+ //# sourceMappingURL=console.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"console.js","sourceRoot":"","sources":["../../../src/observers/console.ts"],"names":[],"mappings":";;;;AAAA,gDAAiD;AAKjD,IAAM,WAAW,GAAG,IAAA,6BAAc,GAAE,CAAC;AACrC,0BAA0B;AAC1B,IAAM,eAAe,GAAQ,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,OAAO,CAAC;AAElD,IAAI,QAAQ,GAAmD,EAAE,CAAC;AAElE,8CAA8C;AAC9C,IAAI,UAAU,GAA4D,EAAE,CAAC;AAE7E,IAAI,iBAAiB,GAAG,KAAK,CAAC;AAE9B,SAAS,eAAe,CAAC,QAAyB;IAChD,wBAAwB;IACxB,IAAI,CAAC,eAAe,EAAE;QACpB,OAAO,KAAK,CAAC;KACd;IAED,qEAAqE;IACrE,yEAAyE;IACzE,IAAI,OAAO,eAAe,CAAC,QAAQ,CAAC,KAAK,UAAU,EAAE;QACnD,OAAO,KAAK,CAAC;KACd;IAED,gDAAgD;IAChD,IAAI,UAAU,CAAC,QAAQ,CAAC,EAAE;QACxB,OAAO,IAAI,CAAC;KACb;IAED,0BAA0B;IAC1B,IAAM,OAAO,GAAG;QAAU,cAAc;aAAd,UAAc,EAAd,qBAAc,EAAd,IAAc;YAAd,yBAAc;;QACtC,IAAI;YACF,IAAI,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,iBAAiB,EAAE;gBAC5C,wDAAwD;gBACxD,iBAAiB,GAAG,IAAI,CAAC;gBACzB,IAAM,SAAS,GAAG,QAAQ,CAAC,QAAQ,CAAC,CAAC;gBACrC,IAAI,SAAS,EAAE;oBACb,SAAS,CAAC,OAAO,CAAC,UAAC,QAAQ;wBACzB,IAAI;4BACF,QAAQ,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;yBAC1B;wBAAC,WAAM;4BACN,aAAa;yBACd;oBACH,CAAC,CAAC,CAAC;iBACJ;aACF;SACF;QAAC,WAAM;YACN,aAAa;SACd;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gD;AAChD,SAAS,eAAe;;;QACtB,KAAqC,IAAA,KAAA,iBAAA,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,CAAA,gBAAA,4BAAE;YAAtD,IAAA,KAAA,2BAAsB,EAArB,GAAG,QAAA,EAAE,eAAe,QAAA;YAC9B,IAAI,eAAe,EAAE;gBACnB,yEAAyE;gBACzE,eAAe,CAAC,GAAG,CAAC,GAAG,eAAe,CAAC;aACxC;SACF;;;;;;;;;IACD,UAAU,GAAG,EAAE,CAAC;IAChB,QAAQ,GAAG,EAAE,CAAC;AAChB,CAAC;AAED,IAAM,eAAe,GAAG;IACtB,WAAW,aAAA;IACX,cAAc,gBAAA;IACd,eAAe,iBAAA;CAChB,CAAC;AAEO,0CAAe","sourcesContent":["import { getGlobalScope } from '../global-scope';\n\ntype ConsoleLogLevel = keyof Console;\ntype Callback = (logLevel: ConsoleLogLevel, args: any[]) => void;\n\nconst globalScope = getGlobalScope();\n/* istanbul ignore next */\nconst originalConsole: any = globalScope?.console;\n\nlet handlers: { [key in ConsoleLogLevel]?: Array<Callback> } = {};\n\n// keeps reference to original console methods\nlet originalFn: { [key in ConsoleLogLevel]?: (...args: any[]) => void } = {};\n\nlet inConsoleOverride = false;\n\nfunction overrideConsole(logLevel: ConsoleLogLevel): boolean {\n /* istanbul ignore if */\n if (!originalConsole) {\n return false;\n }\n\n // should not override if original console property is not a function\n /* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access */\n if (typeof originalConsole[logLevel] !== 'function') {\n return false;\n }\n\n // if console is already overridden, return true\n if (originalFn[logLevel]) {\n return true;\n }\n\n // override console method\n const handler = function (...args: any[]) {\n try {\n if (handlers[logLevel] && !inConsoleOverride) {\n // add a re-entrancy guard to prevent infinite recursion\n inConsoleOverride = true;\n const callbacks = handlers[logLevel];\n if (callbacks) {\n callbacks.forEach((callback) => {\n try {\n callback(logLevel, args);\n } catch {\n // do nothing\n }\n });\n }\n }\n } catch {\n // do nothing\n }\n inConsoleOverride = false;\n return originalFn[logLevel]!.apply(originalConsole, args);\n };\n /* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access */\n originalFn[logLevel] = originalConsole[logLevel] as (...args: any[]) => void;\n /* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access */\n originalConsole[logLevel] = handler;\n return true;\n}\n\n/**\n * Observe a console log method (log, warn, error, etc.)\n * @param level - The console log level to observe\n * @param callback - The callback function to call when the console log level is observed\n */\nfunction addListener(level: ConsoleLogLevel, callback: Callback): Error | void {\n const res = overrideConsole(level);\n\n /* istanbul ignore if */\n if (!res) {\n return new Error('Console override failed');\n }\n\n if (handlers[level]) {\n // using ! is safe because we know the key exists based on condition above\n handlers[level]!.push(callback);\n } else {\n handlers[level] = [callback];\n }\n}\n\n/**\n * Disconnect a callback function from a console log method\n * @param callback - The callback function to disconnect\n */\nfunction removeListener(callback: Callback) {\n for (const callbacks of Object.values(handlers)) {\n // iterate backwards to avoid index shifting\n for (let i = callbacks.length - 1; i >= 0; i--) {\n if (callbacks[i] === callback) {\n callbacks.splice(i, 1);\n break;\n }\n }\n }\n}\n\n// this should only be used for testing\n// restoring console can break console overrides\nfunction _restoreConsole() {\n for (const [key, originalHandler] of Object.entries(originalFn)) {\n if (originalHandler) {\n /* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access */\n originalConsole[key] = originalHandler;\n }\n }\n originalFn = {};\n handlers = {};\n}\n\nconst consoleObserver = {\n addListener,\n removeListener,\n _restoreConsole,\n};\n\nexport { consoleObserver, ConsoleLogLevel };\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"browser-storage.d.ts","sourceRoot":"","sources":["../../../src/storage/browser-storage.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,IAAI,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAE/D,qBAAa,cAAc,CAAC,CAAC,CAAE,YAAW,gBAAgB,CAAC,CAAC,CAAC;IAC/C,OAAO,CAAC,OAAO,CAAC;gBAAR,OAAO,CAAC,qBAAS;IAE/B,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IAqB7B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAcxC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAIhD,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAQzC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQlC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAO7B"}
1
+ {"version":3,"file":"browser-storage.d.ts","sourceRoot":"","sources":["../../../src/storage/browser-storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAE/D,qBAAa,cAAc,CAAC,CAAC,CAAE,YAAW,gBAAgB,CAAC,CAAC,CAAC;IAC/C,OAAO,CAAC,OAAO,CAAC;gBAAR,OAAO,CAAC,qBAAS;IAE/B,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IAqB7B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAcxC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAIhD,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAQzC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQlC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAO7B"}
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BrowserStorage = void 0;
4
4
  var tslib_1 = require("tslib");
5
- var uuid_1 = require("../utils/uuid");
6
5
  var BrowserStorage = /** @class */ (function () {
7
6
  function BrowserStorage(storage) {
8
7
  this.storage = storage;
@@ -19,7 +18,7 @@ var BrowserStorage = /** @class */ (function () {
19
18
  }
20
19
  random = String(Date.now());
21
20
  testStorage = new BrowserStorage(this.storage);
22
- testKey = "AMP_TEST_".concat((0, uuid_1.UUID)().substring(0, 8));
21
+ testKey = 'AMP_TEST';
23
22
  _b.label = 1;
24
23
  case 1:
25
24
  _b.trys.push([1, 4, 5, 7]);
@@ -1 +1 @@
1
- {"version":3,"file":"browser-storage.js","sourceRoot":"","sources":["../../../src/storage/browser-storage.ts"],"names":[],"mappings":";;;;AAAA,sCAAqC;AAGrC;IACE,wBAAoB,OAAiB;QAAjB,YAAO,GAAP,OAAO,CAAU;IAAG,CAAC;IAEnC,kCAAS,GAAf;;;;;;wBACE,wBAAwB;wBACxB,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE;4BACjB,sBAAO,KAAK,EAAC;yBACd;wBAEK,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;wBAC5B,WAAW,GAAG,IAAI,cAAc,CAAS,IAAI,CAAC,OAAO,CAAC,CAAC;wBACvD,OAAO,GAAG,mBAAY,IAAA,WAAI,GAAE,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAE,CAAC;;;;wBAEnD,qBAAM,WAAW,CAAC,GAAG,CAAC,OAAO,EAAE,MAAM,CAAC,EAAA;;wBAAtC,SAAsC,CAAC;wBACzB,qBAAM,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,EAAA;;wBAAtC,KAAK,GAAG,SAA8B;wBAC5C,sBAAO,KAAK,KAAK,MAAM,EAAC;;;wBAExB,0BAA0B;wBAC1B,sBAAO,KAAK,EAAC;4BAEb,qBAAM,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,EAAA;;wBAAjC,SAAiC,CAAC;;;;;;KAErC;IAEK,4BAAG,GAAT,UAAU,GAAW;;;;;;;wBAEH,qBAAM,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,EAAA;;wBAA9B,KAAK,GAAG,SAAsB;wBACpC,IAAI,CAAC,KAAK,EAAE;4BACV,sBAAO,SAAS,EAAC;yBAClB;wBACD,+DAA+D;wBAC/D,sBAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,EAAC;;;wBAEzB,OAAO,CAAC,KAAK,CAAC,qDAAqD,CAAC,CAAC;wBACrE,sBAAO,SAAS,EAAC;;;;;KAEpB;IAEK,+BAAM,GAAZ,UAAa,GAAW;;;;gBACtB,sBAAO,CAAA,MAAA,IAAI,CAAC,OAAO,0CAAE,OAAO,CAAC,GAAG,CAAC,KAAI,SAAS,EAAC;;;KAChD;IAEK,4BAAG,GAAT,UAAU,GAAW,EAAE,KAAQ;;;;gBAC7B,IAAI;oBACF,MAAA,IAAI,CAAC,OAAO,0CAAE,OAAO,CAAC,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;iBACnD;gBAAC,WAAM;oBACN,EAAE;iBACH;;;;KACF;IAEK,+BAAM,GAAZ,UAAa,GAAW;;;;gBACtB,IAAI;oBACF,MAAA,IAAI,CAAC,OAAO,0CAAE,UAAU,CAAC,GAAG,CAAC,CAAC;iBAC/B;gBAAC,WAAM;oBACN,EAAE;iBACH;;;;KACF;IAEK,8BAAK,GAAX;;;;gBACE,IAAI;oBACF,MAAA,IAAI,CAAC,OAAO,0CAAE,KAAK,EAAE,CAAC;iBACvB;gBAAC,WAAM;oBACN,EAAE;iBACH;;;;KACF;IACH,qBAAC;AAAD,CAAC,AAjED,IAiEC;AAjEY,wCAAc","sourcesContent":["import { UUID } from '../utils/uuid';\nimport { Storage as AmplitudeStorage } from '../types/storage';\n\nexport class BrowserStorage<T> implements AmplitudeStorage<T> {\n constructor(private storage?: Storage) {}\n\n async isEnabled(): Promise<boolean> {\n /* istanbul ignore if */\n if (!this.storage) {\n return false;\n }\n\n const random = String(Date.now());\n const testStorage = new BrowserStorage<string>(this.storage);\n const testKey = `AMP_TEST_${UUID().substring(0, 8)}`;\n try {\n await testStorage.set(testKey, random);\n const value = await testStorage.get(testKey);\n return value === random;\n } catch {\n /* istanbul ignore next */\n return false;\n } finally {\n await testStorage.remove(testKey);\n }\n }\n\n async get(key: string): Promise<T | undefined> {\n try {\n const value = await this.getRaw(key);\n if (!value) {\n return undefined;\n }\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return JSON.parse(value);\n } catch {\n console.error(`[Amplitude] Error: Could not get value from storage`);\n return undefined;\n }\n }\n\n async getRaw(key: string): Promise<string | undefined> {\n return this.storage?.getItem(key) || undefined;\n }\n\n async set(key: string, value: T): Promise<void> {\n try {\n this.storage?.setItem(key, JSON.stringify(value));\n } catch {\n //\n }\n }\n\n async remove(key: string): Promise<void> {\n try {\n this.storage?.removeItem(key);\n } catch {\n //\n }\n }\n\n async reset(): Promise<void> {\n try {\n this.storage?.clear();\n } catch {\n //\n }\n }\n}\n"]}
1
+ {"version":3,"file":"browser-storage.js","sourceRoot":"","sources":["../../../src/storage/browser-storage.ts"],"names":[],"mappings":";;;;AAEA;IACE,wBAAoB,OAAiB;QAAjB,YAAO,GAAP,OAAO,CAAU;IAAG,CAAC;IAEnC,kCAAS,GAAf;;;;;;wBACE,wBAAwB;wBACxB,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE;4BACjB,sBAAO,KAAK,EAAC;yBACd;wBAEK,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;wBAC5B,WAAW,GAAG,IAAI,cAAc,CAAS,IAAI,CAAC,OAAO,CAAC,CAAC;wBACvD,OAAO,GAAG,UAAU,CAAC;;;;wBAEzB,qBAAM,WAAW,CAAC,GAAG,CAAC,OAAO,EAAE,MAAM,CAAC,EAAA;;wBAAtC,SAAsC,CAAC;wBACzB,qBAAM,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,EAAA;;wBAAtC,KAAK,GAAG,SAA8B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EY,wCAAc","sourcesContent":["import { Storage as AmplitudeStorage } from '../types/storage';\n\nexport class BrowserStorage<T> implements AmplitudeStorage<T> {\n constructor(private storage?: Storage) {}\n\n async isEnabled(): Promise<boolean> {\n /* istanbul ignore if */\n if (!this.storage) {\n return false;\n }\n\n const random = String(Date.now());\n const testStorage = new BrowserStorage<string>(this.storage);\n const testKey = 'AMP_TEST';\n try {\n await testStorage.set(testKey, random);\n const value = await testStorage.get(testKey);\n return value === random;\n } catch {\n /* istanbul ignore next */\n return false;\n } finally {\n await testStorage.remove(testKey);\n }\n }\n\n async get(key: string): Promise<T | undefined> {\n try {\n const value = await this.getRaw(key);\n if (!value) {\n return undefined;\n }\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return JSON.parse(value);\n } catch {\n console.error(`[Amplitude] Error: Could not get value from storage`);\n return undefined;\n }\n }\n\n async getRaw(key: string): Promise<string | undefined> {\n return this.storage?.getItem(key) || undefined;\n }\n\n async set(key: string, value: T): Promise<void> {\n try {\n this.storage?.setItem(key, JSON.stringify(value));\n } catch {\n //\n }\n }\n\n async remove(key: string): Promise<void> {\n try {\n this.storage?.removeItem(key);\n } catch {\n //\n }\n }\n\n async reset(): Promise<void> {\n try {\n this.storage?.clear();\n } catch {\n //\n }\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"cookie.d.ts","sourceRoot":"","sources":["../../../src/storage/cookie.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAqBtF,qBAAa,aAAa,CAAC,CAAC,CAAE,YAAW,OAAO,CAAC,CAAC,CAAC;IACjD,OAAO,EAAE,oBAAoB,CAAC;IAC9B,MAAM,EAAE,mBAAmB,CAAC;IAC5B,OAAO,CAAC,MAAM,CAAC,SAAS,CAAqB;gBAEjC,OAAO,CAAC,EAAE,oBAAoB,EAAE,MAAM,GAAE,mBAAwB;IAKtE,SAAS,IAAI,OAAO,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M,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAkChD,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIlC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAG7B;AAoBD;;;GAGG;AACH,eAAO,MAAM,iBAAiB,UAAW,MAAM,KAAG,MAAM,GAAG,SAE1D,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,aAAa,YAAa,MAAM,GAAG,SAAS,WAAW,MAAM,GAAG,SAAS,KAAG,OAOxF,CAAC"}
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isDomainEqual = exports.decodeCookieValue = exports.CookieStorage = void 0;
4
4
  var tslib_1 = require("tslib");
5
5
  var global_scope_1 = require("../global-scope");
6
+ var uuid_1 = require("../utils/uuid");
6
7
  var CookieStorage = /** @class */ (function () {
7
8
  function CookieStorage(options, config) {
8
9
  if (config === void 0) { config = {}; }
@@ -11,7 +12,7 @@ var CookieStorage = /** @class */ (function () {
11
12
  }
12
13
  CookieStorage.prototype.isEnabled = function () {
13
14
  return tslib_1.__awaiter(this, void 0, void 0, function () {
14
- var testStorage, testKey, value, _a;
15
+ var testCookieOptions, testStorage, testKey, value, _a;
15
16
  return tslib_1.__generator(this, function (_b) {
16
17
  switch (_b.label) {
17
18
  case 0:
@@ -20,8 +21,9 @@ var CookieStorage = /** @class */ (function () {
20
21
  return [2 /*return*/, false];
21
22
  }
22
23
  CookieStorage.testValue = String(Date.now());
23
- testStorage = new CookieStorage(this.options);
24
- testKey = 'AMP_TEST';
24
+ testCookieOptions = tslib_1.__assign(tslib_1.__assign({}, this.options), { expirationDays: 0.003 });
25
+ testStorage = new CookieStorage(testCookieOptions);
26
+ testKey = "AMP_TEST_".concat((0, uuid_1.UUID)().substring(0, 8));
25
27
  _b.label = 1;
26
28
  case 1:
27
29
  _b.trys.push([1, 4, 5, 7]);
@@ -1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,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;IAC7E,IAAM,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;IAC7E,OAAO,WAAW,CAAC,WAAW,EAAE,KAAK,WAAW,CAAC,WAAW,EAAE,CAAC;AACjE,CAAC,CAAC;AAPW,QAAA,aAAa,iBAOxB","sourcesContent":["import { Storage, CookieStorageOptions, CookieStorageConfig } from '../types/storage';\nimport { getGlobalScope } from '../global-scope';\n\n// CookieStore is a Web API not included in standard TypeScript lib types\n// https://developer.mozilla.org/en-US/docs/Web/API/CookieStore\ninterface CookieStoreSetOptions {\n name: string;\n value: string;\n expires?: number;\n domain?: string;\n sameSite?: 'strict' | 'lax' | 'none';\n}\n\ninterface CookieStore {\n getAll(key: string): Promise<CookieStoreSetOptions[] | undefined>;\n}\n\ntype GlobalScopeWithCookieStore = {\n cookieStore?: CookieStore;\n} & typeof global;\n\nexport class CookieStorage<T> implements Storage<T> {\n options: CookieStorageOptions;\n config: CookieStorageConfig;\n private static testValue: undefined | string;\n\n constructor(options?: CookieStorageOptions, config: CookieStorageConfig = {}) {\n this.options = { ...options };\n this.config = config;\n }\n\n async isEnabled(): Promise<boolean> {\n /* istanbul ignore if */\n if (!getGlobalScope()) {\n return false;\n }\n\n CookieStorage.testValue = String(Date.now());\n const testStorage = new CookieStorage<string>(this.options);\n const testKey = 'AMP_TEST';\n try {\n await testStorage.set(testKey, CookieStorage.testValue);\n const value = await testStorage.get(testKey);\n return value === CookieStorage.testValue;\n } catch {\n /* istanbul ignore next */\n return false;\n } finally {\n await testStorage.remove(testKey);\n }\n }\n\n async get(key: string): Promise<T | undefined> {\n const value = await this.getRaw(key);\n if (!value) {\n return undefined;\n }\n try {\n const decodedValue = decodeCookieValue(value);\n if (decodedValue === undefined) {\n console.error(`Amplitude Logger [Error]: Failed to decode cookie value for key: ${key}, value: ${value}`);\n return undefined;\n }\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return JSON.parse(decodedValue);\n } catch {\n console.error(`Amplitude Logger [Error]: Failed to parse cookie value for key: ${key}, value: ${value}`);\n return undefined;\n }\n }\n\n async getRaw(key: string): Promise<string | undefined> {\n const globalScope = getGlobalScope();\n\n // use CookieStore if available and enabled\n const globalScopeWithCookiesStore = globalScope as GlobalScopeWithCookieStore;\n try {\n const cookieStore = globalScopeWithCookiesStore?.cookieStore;\n if (cookieStore) {\n const cookies = await cookieStore.getAll(key);\n if (cookies) {\n /* istanbul ignore if */\n if (cookies.length > 1) {\n this.config.diagnosticsClient?.recordEvent('cookies.duplicate', {\n cookies: cookies.map((cookie) => cookie.domain),\n });\n this.config.diagnosticsClient?.increment('cookies.duplicate.occurrence.cookieStore');\n }\n\n for (const cookie of cookies) {\n if (isDomainEqual(cookie.domain, this.options.domain)) {\n return cookie.value;\n }\n }\n }\n }\n } catch (ignoreError) {\n /* istanbul ignore next */\n // if cookieStore had a surprise failure, fallback to document.cookie\n }\n\n const cookies = (globalScope?.document?.cookie.split('; ') ?? []).filter((c) => c.indexOf(key + '=') === 0);\n let match: string | undefined = undefined;\n\n // if matcher function is provided, use it to de-duplicate when there's more than one cookie\n /* istanbul ignore if */\n const duplicateResolverFn = this.config.duplicateResolverFn;\n if (typeof duplicateResolverFn === 'function' && cookies.length > 1) {\n match = cookies.find((c) => {\n try {\n const res = duplicateResolverFn(c.substring(key.length + 1));\n if (!res) {\n this.config.diagnosticsClient?.increment('cookies.duplicate.occurrence.document.cookie');\n }\n return res;\n } catch (ignoreError) {\n /* istanbul ignore next */\n return false;\n }\n });\n }\n\n // if match was not found, just get the first one that matches the key\n if (!match) {\n match = cookies[0];\n }\n if (!match) {\n return undefined;\n }\n return match.substring(key.length + 1);\n }\n\n async set(key: string, value: T | null): Promise<void> {\n try {\n const expirationDays = this.options.expirationDays ?? 0;\n const expires = value !== null ? expirationDays : -1;\n let expireDate: Date | undefined = undefined;\n if (expires) {\n const date = new Date();\n date.setTime(date.getTime() + expires * 24 * 60 * 60 * 1000);\n expireDate = date;\n }\n let str = `${key}=${btoa(encodeURIComponent(JSON.stringify(value)))}`;\n if (expireDate) {\n str += `; expires=${expireDate.toUTCString()}`;\n }\n str += '; path=/';\n if (this.options.domain) {\n str += `; domain=${this.options.domain}`;\n }\n if (this.options.secure) {\n str += '; Secure';\n }\n if (this.options.sameSite) {\n str += `; SameSite=${this.options.sameSite}`;\n }\n const globalScope = getGlobalScope();\n if (globalScope) {\n globalScope.document.cookie = str;\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n console.error(`Amplitude Logger [Error]: Failed to set cookie for key: ${key}. Error: ${errorMessage}`);\n }\n }\n\n async remove(key: string): Promise<void> {\n await this.set(key, null);\n }\n\n async reset(): Promise<void> {\n return;\n }\n}\n\nconst decodeCookiesAsDefault = (value: string): string | undefined => {\n try {\n return decodeURIComponent(atob(value));\n } catch {\n return undefined;\n }\n};\n\nconst decodeCookiesWithDoubleUrlEncoding = (value: string): string | undefined => {\n // Modern Ruby (v7+) automatically encodes cookies with URL encoding by\n // https://api.rubyonrails.org/classes/ActionDispatch/Cookies.html\n try {\n return decodeURIComponent(atob(decodeURIComponent(value)));\n } catch {\n return undefined;\n }\n};\n\n/**\n * Decodes a cookie value that was encoded with btoa(encodeURIComponent(...)).\n * Handles both standard encoding and double URL encoding (used by Ruby Rails v7+).\n */\nexport const decodeCookieValue = (value: string): string | undefined => {\n return decodeCookiesAsDefault(value) ?? decodeCookiesWithDoubleUrlEncoding(value);\n};\n\n/**\n * Compares two domain strings for equality, ignoring leading dots.\n * This is useful for comparing cookie domains since \".example.com\" and \"example.com\"\n * are effectively equivalent for cookie scoping.\n */\nexport const isDomainEqual = (domain1: string | undefined, domain2: string | undefined): boolean => {\n if (!domain1 || !domain2) {\n return false;\n }\n const normalized1 = domain1.startsWith('.') ? domain1.substring(1) : domain1;\n const normalized2 = domain2.startsWith('.') ? domain2.substring(1) : domain2;\n return normalized1.toLowerCase() === normalized2.toLowerCase();\n};\n"]}
1
+ {"version":3,"file":"cookie.js","sourceRoot":"","sources":["../../../src/storage/cookie.ts"],"names":[],"mappings":";;;;AACA,gDAAiD;AACjD,sCAAqC;AAoBrC;IAKE,uBAAY,OAA8B,EAAE,MAAgC;QAAhC,uBAAA,EAAA,WAAgC;QAC1E,IAAI,CAAC,OAAO,wBAAQ,OAAO,CAAE,CAAC;QAC9B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAEK,iCAAS,GAAf;;;;;;wBACE,wBAAwB;wBACxB,IAAI,CAAC,IAAA,6BAAc,GAAE,EAAE;4BACrB,sBAAO,KAAK,EAAC;yBACd;wBAED,aAAa,CAAC,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;wBACvC,iBAAiB,yCAClB,IAAI,CAAC,OAAO,KACf,cAAc,EAAE,KAAK,GACtB,CAAC;wBACI,WAAW,GAAG,IAAI,aAAa,CAAS,iBAAiB,CAAC,CAAC;wBAC3D,OAAO,GAAG,mBAAY,IAAA,WAAI,GAAE,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAE,CAAC;;;;wBAEnD,qBAAM,WAAW,CAAC,GAAG,CAAC,OAAO,EAAE,aAAa,CAAC,SAAS,CAAC,EAAA;;wBAAvD,SAAuD,CAAC;wBAC1C,qBAAM,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,EAAA;;wBAAtC,KAAK,GAAG,SAA8B;wBAC5C,sBAAO,KAAK,KAAK,aAAa,CAAC,SAAS,EAAC;;;wBAEzC,0BAA0B;wBAC1B,sBAAO,KAAK,EAAC;4BAEb,qBAAM,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,EAAA;;wBAAjC,SAAiC,CAAC;;;;;;KAErC;IAEK,2BAAG,GAAT,UAAU,GAAW;;;;;4BACL,qBAAM,IAA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EF;;;;GAIG;AACI,IAAM,aAAa,GAAG,UAAC,OAA2B,EAAE,OAA2B;IACpF,IAAI,CAAC,OAAO,IAAI,CAAC,OAAO,EAAE;QACxB,OAAO,KAAK,CAAC;KACd;IACD,IAAM,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;IAC7E,IAAM,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;IAC7E,OAAO,WAAW,CAAC,WAAW,EAAE,KAAK,WAAW,CAAC,WAAW,EAAE,CAAC;AACjE,CAAC,CAAC;AAPW,QAAA,aAAa,iBAOxB","sourcesContent":["import { Storage, CookieStorageOptions, CookieStorageConfig } from '../types/storage';\nimport { getGlobalScope } from '../global-scope';\nimport { UUID } from '../utils/uuid';\n\n// CookieStore is a Web API not included in standard TypeScript lib types\n// https://developer.mozilla.org/en-US/docs/Web/API/CookieStore\ninterface CookieStoreSetOptions {\n name: string;\n value: string;\n expires?: number;\n domain?: string;\n sameSite?: 'strict' | 'lax' | 'none';\n}\n\ninterface CookieStore {\n getAll(key: string): Promise<CookieStoreSetOptions[] | undefined>;\n}\n\ntype GlobalScopeWithCookieStore = {\n cookieStore?: CookieStore;\n} & typeof global;\n\nexport class CookieStorage<T> implements Storage<T> {\n options: CookieStorageOptions;\n config: CookieStorageConfig;\n private static testValue: undefined | string;\n\n constructor(options?: CookieStorageOptions, config: CookieStorageConfig = {}) {\n this.options = { ...options };\n this.config = config;\n }\n\n async isEnabled(): Promise<boolean> {\n /* istanbul ignore if */\n if (!getGlobalScope()) {\n return false;\n }\n\n CookieStorage.testValue = String(Date.now());\n const testCookieOptions = {\n ...this.options,\n expirationDays: 0.003, // expire in ~5 minutes\n };\n const testStorage = new CookieStorage<string>(testCookieOptions);\n const testKey = `AMP_TEST_${UUID().substring(0, 8)}`;\n try {\n await testStorage.set(testKey, CookieStorage.testValue);\n const value = await testStorage.get(testKey);\n return value === CookieStorage.testValue;\n } catch {\n /* istanbul ignore next */\n return false;\n } finally {\n await testStorage.remove(testKey);\n }\n }\n\n async get(key: string): Promise<T | undefined> {\n const value = await this.getRaw(key);\n if (!value) {\n return undefined;\n }\n try {\n const decodedValue = decodeCookieValue(value);\n if (decodedValue === undefined) {\n console.error(`Amplitude Logger [Error]: Failed to decode cookie value for key: ${key}, value: ${value}`);\n return undefined;\n }\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return JSON.parse(decodedValue);\n } catch {\n console.error(`Amplitude Logger [Error]: Failed to parse cookie value for key: ${key}, value: ${value}`);\n return undefined;\n }\n }\n\n async getRaw(key: string): Promise<string | undefined> {\n const globalScope = getGlobalScope();\n\n // use CookieStore if available and enabled\n const globalScopeWithCookiesStore = globalScope as GlobalScopeWithCookieStore;\n try {\n const cookieStore = globalScopeWithCookiesStore?.cookieStore;\n if (cookieStore) {\n const cookies = await cookieStore.getAll(key);\n if (cookies) {\n /* istanbul ignore if */\n if (cookies.length > 1) {\n this.config.diagnosticsClient?.recordEvent('cookies.duplicate', {\n cookies: cookies.map((cookie) => cookie.domain),\n });\n this.config.diagnosticsClient?.increment('cookies.duplicate.occurrence.cookieStore');\n }\n\n for (const cookie of cookies) {\n if (isDomainEqual(cookie.domain, this.options.domain)) {\n return cookie.value;\n }\n }\n }\n }\n } catch (ignoreError) {\n /* istanbul ignore next */\n // if cookieStore had a surprise failure, fallback to document.cookie\n }\n\n const cookies = (globalScope?.document?.cookie.split('; ') ?? []).filter((c) => c.indexOf(key + '=') === 0);\n let match: string | undefined = undefined;\n\n // if matcher function is provided, use it to de-duplicate when there's more than one cookie\n /* istanbul ignore if */\n const duplicateResolverFn = this.config.duplicateResolverFn;\n if (typeof duplicateResolverFn === 'function' && cookies.length > 1) {\n match = cookies.find((c) => {\n try {\n const res = duplicateResolverFn(c.substring(key.length + 1));\n if (!res) {\n this.config.diagnosticsClient?.increment('cookies.duplicate.occurrence.document.cookie');\n }\n return res;\n } catch (ignoreError) {\n /* istanbul ignore next */\n return false;\n }\n });\n }\n\n // if match was not found, just get the first one that matches the key\n if (!match) {\n match = cookies[0];\n }\n if (!match) {\n return undefined;\n }\n return match.substring(key.length + 1);\n }\n\n async set(key: string, value: T | null): Promise<void> {\n try {\n const expirationDays = this.options.expirationDays ?? 0;\n const expires = value !== null ? expirationDays : -1;\n let expireDate: Date | undefined = undefined;\n if (expires) {\n const date = new Date();\n date.setTime(date.getTime() + expires * 24 * 60 * 60 * 1000);\n expireDate = date;\n }\n let str = `${key}=${btoa(encodeURIComponent(JSON.stringify(value)))}`;\n if (expireDate) {\n str += `; expires=${expireDate.toUTCString()}`;\n }\n str += '; path=/';\n if (this.options.domain) {\n str += `; domain=${this.options.domain}`;\n }\n if (this.options.secure) {\n str += '; Secure';\n }\n if (this.options.sameSite) {\n str += `; SameSite=${this.options.sameSite}`;\n }\n const globalScope = getGlobalScope();\n if (globalScope) {\n globalScope.document.cookie = str;\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n console.error(`Amplitude Logger [Error]: Failed to set cookie for key: ${key}. Error: ${errorMessage}`);\n }\n }\n\n async remove(key: string): Promise<void> {\n await this.set(key, null);\n }\n\n async reset(): Promise<void> {\n return;\n }\n}\n\nconst decodeCookiesAsDefault = (value: string): string | undefined => {\n try {\n return decodeURIComponent(atob(value));\n } catch {\n return undefined;\n }\n};\n\nconst decodeCookiesWithDoubleUrlEncoding = (value: string): string | undefined => {\n // Modern Ruby (v7+) automatically encodes cookies with URL encoding by\n // https://api.rubyonrails.org/classes/ActionDispatch/Cookies.html\n try {\n return decodeURIComponent(atob(decodeURIComponent(value)));\n } catch {\n return undefined;\n }\n};\n\n/**\n * Decodes a cookie value that was encoded with btoa(encodeURIComponent(...)).\n * Handles both standard encoding and double URL encoding (used by Ruby Rails v7+).\n */\nexport const decodeCookieValue = (value: string): string | undefined => {\n return decodeCookiesAsDefault(value) ?? decodeCookiesWithDoubleUrlEncoding(value);\n};\n\n/**\n * Compares two domain strings for equality, ignoring leading dots.\n * This is useful for comparing cookie domains since \".example.com\" and \"example.com\"\n * are effectively equivalent for cookie scoping.\n */\nexport const isDomainEqual = (domain1: string | undefined, domain2: string | undefined): boolean => {\n if (!domain1 || !domain2) {\n return false;\n }\n const normalized1 = domain1.startsWith('.') ? domain1.substring(1) : domain1;\n const normalized2 = domain2.startsWith('.') ? domain2.substring(1) : domain2;\n return normalized1.toLowerCase() === normalized2.toLowerCase();\n};\n"]}
@@ -52,13 +52,17 @@ export interface FrustrationInteractionsOptions {
52
52
  */
53
53
  dataAttributePrefix?: string;
54
54
  /**
55
- * Configuration for dead clicks tracking
55
+ * Configuration for dead clicks tracking.
56
+ * Set to `false` to disable dead click tracking.
57
+ * Set to `true` or an options object to enable with default or custom settings.
56
58
  */
57
- deadClicks?: DeadClickOptions;
59
+ deadClicks?: boolean | DeadClickOptions;
58
60
  /**
59
- * Configuration for rage clicks tracking
61
+ * Configuration for rage clicks tracking.
62
+ * Set to `false` to disable rage click tracking.
63
+ * Set to `true` or an options object to enable with default or custom settings.
60
64
  */
61
- rageClicks?: RageClickOptions;
65
+ rageClicks?: boolean | RageClickOptions;
62
66
  /**
63
67
  * RegExp pattern list to allow custom patterns for text masking
64
68
  */
@@ -1 +1 @@
1
- {"version":3,"file":"frustration-interactions.d.ts","sourceRoot":"","sources":["../../../src/types/frustration-interactions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAEpD;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,oBAAoB,CAAC,EAAE,MAAM,EAAE,CAAC;CACjC;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,oBAAoB,CAAC,EAAE,MAAM,EAAE,CAAC;CACjC;AAED;;;GAGG;AACH,MAAM,WAAW,8BAA8B;IAC7C;;;;OAIG;IACH,gBAAgB,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;IAEvC;;;;;OAKG;IACH,kBAAkB,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;IAEzC;;;;;;;OAOG;IACH,wBAAwB,CAAC,EAAE,CAAC,UAAU,EAAE,UAAU,EAAE,OAAO,EAAE,UAAU,KAAK,OAAO,CAAC;IAEpF;;;OAGG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAE7B;;OAEG;IACH,UAAU,CAAC,EAAE,gBAAgB,CAAC;IAE9B;;OAEG;IACH,UAAU,CAAC,EAAE,gBAAgB,CAAC;IAE9B;;OAEG;IACH,aAAa,CAAC,EAAE,CAAC,MAAM,GAAG;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,CAAA;KAAE,CAAC,EAAE,CAAC;CACvE;AAgBD;;GAEG;AACH,eAAO,MAAM,4BAA4B,UAOxC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,4BAA4B,UAAQ,CAAC;AAElD;;GAEG;AACH,eAAO,MAAM,4BAA4B,OAAQ,CAAC;AAElD;;GAEG;AACH,eAAO,MAAM,4BAA4B,OAAQ,CAAC;AAElD;;GAEG;AACH,eAAO,MAAM,4BAA4B,IAAI,CAAC;AAE9C;;GAEG;AACH,eAAO,MAAM,0CAA0C,KAAK,CAAC;AAK7D,KAAK,UAAU,GAAG,OAAO,UAAU,SAAS;IAC1C,OAAO,EAAE,KAAK,GAAG,IAAI,EAAE,GAAG,KAAK,MAAM,CAAC,CAAC;CACxC,GACG,CAAC,GACD,KAAK,CAAC"}
1
+ {"version":3,"file":"frustration-interactions.d.ts","sourceRoot":"","sources":["../../../src/types/frustration-interactions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC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AK,GAAG,IAAI,EAAE,GAAG,KAAK,MAAM,CAAC,CAAC;CACxC,GACG,CAAC,GACD,KAAK,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"frustration-interactions.js","sourceRoot":"","sources":["../../../src/types/frustration-interactions.ts"],"names":[],"mappings":";;;;AA4EA,IAAM,2BAA2B,GAAG;IAClC,GAAG;IACH,QAAQ;IACR,iBAAiB;IACjB,eAAe;IACf,mBAAmB;IACnB,2BAA2B;IAC3B,wBAAwB;IACxB,iBAAiB;IACjB,cAAc;IACd,mBAAmB;IACnB,4BAA4B;CAC7B,CAAC;AAEF;;GAEG;AACU,QAAA,4BAA4B;IACvC,sBAAsB;IACtB,sBAAsB;IACtB,qBAAqB;IACrB,qBAAqB;IACrB,oBAAoB;kBACjB,2BAA2B,UAC9B;AAEF;;GAEG;AACU,QAAA,4BAA4B,GAAG,CAAC,GAAG,CAAC,CAAC;AAElD;;GAEG;AACU,QAAA,4BAA4B,GAAG,IAAK,CAAC;AAElD;;GAEG;AACU,QAAA,4BAA4B,GAAG,IAAK,CAAC;AAElD;;GAEG;AACU,QAAA,4BAA4B,GAAG,CAAC,CAAC;AAE9C;;GAEG;AACU,QAAA,0CAA0C,GAAG,EAAE,CAAC,CAAC,SAAS","sourcesContent":["import { ActionType } from './element-interactions';\n\n/**\n * Configuration options for dead clicks tracking\n */\nexport interface DeadClickOptions {\n /**\n * CSS selectors to define which elements on the page to track for dead clicks.\n * A dead click is a click that doesn't result in any visible change or navigation.\n */\n cssSelectorAllowlist?: string[];\n}\n\n/**\n * Configuration options for rage clicks tracking\n */\nexport interface RageClickOptions {\n /**\n * CSS selectors to define which elements on the page to track for rage clicks.\n * A rage click is multiple rapid clicks on the same element within a 3s time window.\n */\n cssSelectorAllowlist?: string[];\n}\n\n/**\n * Configuration options for frustration interactions tracking.\n * This includes dead clicks and rage clicks tracking.\n */\nexport interface FrustrationInteractionsOptions {\n /**\n * List of page URLs to allow auto tracking on.\n * When provided, only allow tracking on these URLs.\n * Both full URLs and regex are supported.\n */\n pageUrlAllowlist?: (string | RegExp)[];\n\n /**\n * List of page URLs to exclude from auto tracking.\n * When provided, tracking will be blocked on these URLs.\n * Both full URLs and regex are supported.\n * This takes precedence over pageUrlAllowlist.\n */\n pageUrlExcludelist?: (string | RegExp)[];\n\n /**\n * Function to determine whether an event should be tracked.\n * When provided, this function overwrites all other allowlists and configurations.\n * If the function returns true, the event will be tracked.\n * If the function returns false, the event will not be tracked.\n * @param actionType - The type of action that triggered the event.\n * @param element - The [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) that triggered the event.\n */\n shouldTrackEventResolver?: (actionType: ActionType, element: DomElement) => boolean;\n\n /**\n * Prefix for data attributes to allow auto collecting.\n * Default is 'data-amp-track-'.\n */\n dataAttributePrefix?: string;\n\n /**\n * Configuration for dead clicks tracking\n */\n deadClicks?: DeadClickOptions;\n\n /**\n * Configuration for rage clicks tracking\n */\n rageClicks?: RageClickOptions;\n\n /**\n * RegExp pattern list to allow custom patterns for text masking\n */\n maskTextRegex?: (RegExp | { pattern: string; description: string })[];\n}\n\nconst CLICKABLE_ELEMENT_SELECTORS = [\n 'a',\n 'button',\n '[role=\"button\"]',\n '[role=\"link\"]',\n '[role=\"menuitem\"]',\n '[role=\"menuitemcheckbox\"]',\n '[role=\"menuitemradio\"]',\n '[role=\"option\"]',\n '[role=\"tab\"]',\n '[role=\"treeitem\"]',\n '[contenteditable=\"true\" i]',\n];\n\n/**\n * Default CSS selectors for dead clicks tracking\n */\nexport const DEFAULT_DEAD_CLICK_ALLOWLIST = [\n 'input[type=\"button\"]',\n 'input[type=\"submit\"]',\n 'input[type=\"reset\"]',\n 'input[type=\"image\"]',\n 'input[type=\"file\"]',\n ...CLICKABLE_ELEMENT_SELECTORS,\n];\n\n/**\n * Default CSS selectors for rage clicks tracking\n */\nexport const DEFAULT_RAGE_CLICK_ALLOWLIST = ['*'];\n\n/**\n * Default time window for dead clicks (3 seconds)\n */\nexport const DEFAULT_DEAD_CLICK_WINDOW_MS = 3_000;\n\n/**\n * Default time window for rage clicks (1 second)\n */\nexport const DEFAULT_RAGE_CLICK_WINDOW_MS = 1_000;\n\n/**\n * Default threshold for rage clicks (4 clicks)\n */\nexport const DEFAULT_RAGE_CLICK_THRESHOLD = 4;\n\n/**\n * Default threshold for rage clicks to be considered out of bounds (50 pixels)\n */\nexport const DEFAULT_RAGE_CLICK_OUT_OF_BOUNDS_THRESHOLD = 50; // pixels\n\n// DomElement is [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) if the dom library is included in tsconfig.json\n// and never if it is not included\n// eslint-disable-next-line no-restricted-globals\ntype DomElement = typeof globalThis extends {\n Element: new (...args: any) => infer T;\n}\n ? T\n : never;\n"]}
1
+ {"version":3,"file":"frustration-interactions.js","sourceRoot":"","sources":["../../../src/types/frustration-interactions.ts"],"names":[],"mappings":";;;;AAgFA,IAAM,2BAA2B,GAAG;IAClC,GAAG;IACH,QAAQ;IACR,iBAAiB;IACjB,eAAe;IACf,mBAAmB;IACnB,2BAA2B;IAC3B,wBAAwB;IACxB,iBAAiB;IACjB,cAAc;IACd,mBAAmB;IACnB,4BAA4B;CAC7B,CAAC;AAEF;;GAEG;AACU,QAAA,4BAA4B;IACvC,sBAAsB;IACtB,sBAAsB;IACtB,qBAAqB;IACrB,qBAAqB;IACrB,oBAAoB;kBACjB,2BAA2B,UAC9B;AAEF;;GAEG;AACU,QAAA,4BAA4B,GAAG,CAAC,GAAG,CAAC,CAAC;AAElD;;GAEG;AACU,QAAA,4BAA4B,GAAG,IAAK,CAAC;AAElD;;GAEG;AACU,QAAA,4BAA4B,GAAG,IAAK,CAAC;AAElD;;GAEG;AACU,QAAA,4BAA4B,GAAG,CAAC,CAAC;AAE9C;;GAEG;AACU,QAAA,0CAA0C,GAAG,EAAE,CAAC,CAAC,SAAS","sourcesContent":["import { ActionType } from './element-interactions';\n\n/**\n * Configuration options for dead clicks tracking\n */\nexport interface DeadClickOptions {\n /**\n * CSS selectors to define which elements on the page to track for dead clicks.\n * A dead click is a click that doesn't result in any visible change or navigation.\n */\n cssSelectorAllowlist?: string[];\n}\n\n/**\n * Configuration options for rage clicks tracking\n */\nexport interface RageClickOptions {\n /**\n * CSS selectors to define which elements on the page to track for rage clicks.\n * A rage click is multiple rapid clicks on the same element within a 3s time window.\n */\n cssSelectorAllowlist?: string[];\n}\n\n/**\n * Configuration options for frustration interactions tracking.\n * This includes dead clicks and rage clicks tracking.\n */\nexport interface FrustrationInteractionsOptions {\n /**\n * List of page URLs to allow auto tracking on.\n * When provided, only allow tracking on these URLs.\n * Both full URLs and regex are supported.\n */\n pageUrlAllowlist?: (string | RegExp)[];\n\n /**\n * List of page URLs to exclude from auto tracking.\n * When provided, tracking will be blocked on these URLs.\n * Both full URLs and regex are supported.\n * This takes precedence over pageUrlAllowlist.\n */\n pageUrlExcludelist?: (string | RegExp)[];\n\n /**\n * Function to determine whether an event should be tracked.\n * When provided, this function overwrites all other allowlists and configurations.\n * If the function returns true, the event will be tracked.\n * If the function returns false, the event will not be tracked.\n * @param actionType - The type of action that triggered the event.\n * @param element - The [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) that triggered the event.\n */\n shouldTrackEventResolver?: (actionType: ActionType, element: DomElement) => boolean;\n\n /**\n * Prefix for data attributes to allow auto collecting.\n * Default is 'data-amp-track-'.\n */\n dataAttributePrefix?: string;\n\n /**\n * Configuration for dead clicks tracking.\n * Set to `false` to disable dead click tracking.\n * Set to `true` or an options object to enable with default or custom settings.\n */\n deadClicks?: boolean | DeadClickOptions;\n\n /**\n * Configuration for rage clicks tracking.\n * Set to `false` to disable rage click tracking.\n * Set to `true` or an options object to enable with default or custom settings.\n */\n rageClicks?: boolean | RageClickOptions;\n\n /**\n * RegExp pattern list to allow custom patterns for text masking\n */\n maskTextRegex?: (RegExp | { pattern: string; description: string })[];\n}\n\nconst CLICKABLE_ELEMENT_SELECTORS = [\n 'a',\n 'button',\n '[role=\"button\"]',\n '[role=\"link\"]',\n '[role=\"menuitem\"]',\n '[role=\"menuitemcheckbox\"]',\n '[role=\"menuitemradio\"]',\n '[role=\"option\"]',\n '[role=\"tab\"]',\n '[role=\"treeitem\"]',\n '[contenteditable=\"true\" i]',\n];\n\n/**\n * Default CSS selectors for dead clicks tracking\n */\nexport const DEFAULT_DEAD_CLICK_ALLOWLIST = [\n 'input[type=\"button\"]',\n 'input[type=\"submit\"]',\n 'input[type=\"reset\"]',\n 'input[type=\"image\"]',\n 'input[type=\"file\"]',\n ...CLICKABLE_ELEMENT_SELECTORS,\n];\n\n/**\n * Default CSS selectors for rage clicks tracking\n */\nexport const DEFAULT_RAGE_CLICK_ALLOWLIST = ['*'];\n\n/**\n * Default time window for dead clicks (3 seconds)\n */\nexport const DEFAULT_DEAD_CLICK_WINDOW_MS = 3_000;\n\n/**\n * Default time window for rage clicks (1 second)\n */\nexport const DEFAULT_RAGE_CLICK_WINDOW_MS = 1_000;\n\n/**\n * Default threshold for rage clicks (4 clicks)\n */\nexport const DEFAULT_RAGE_CLICK_THRESHOLD = 4;\n\n/**\n * Default threshold for rage clicks to be considered out of bounds (50 pixels)\n */\nexport const DEFAULT_RAGE_CLICK_OUT_OF_BOUNDS_THRESHOLD = 50; // pixels\n\n// DomElement is [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) if the dom library is included in tsconfig.json\n// and never if it is not included\n// eslint-disable-next-line no-restricted-globals\ntype DomElement = typeof globalThis extends {\n Element: new (...args: any) => infer T;\n}\n ? T\n : never;\n"]}
@@ -0,0 +1,21 @@
1
+ type ConsoleLogLevel = keyof Console;
2
+ type Callback = (logLevel: ConsoleLogLevel, args: any[]) => void;
3
+ /**
4
+ * Observe a console log method (log, warn, error, etc.)
5
+ * @param level - The console log level to observe
6
+ * @param callback - The callback function to call when the console log level is observed
7
+ */
8
+ declare function addListener(level: ConsoleLogLevel, callback: Callback): Error | void;
9
+ /**
10
+ * Disconnect a callback function from a console log method
11
+ * @param callback - The callback function to disconnect
12
+ */
13
+ declare function removeListener(callback: Callback): void;
14
+ declare function _restoreConsole(): void;
15
+ declare const consoleObserver: {
16
+ addListener: typeof addListener;
17
+ removeListener: typeof removeListener;
18
+ _restoreConsole: typeof _restoreConsole;
19
+ };
20
+ export { consoleObserver, ConsoleLogLevel };
21
+ //# sourceMappingURL=console.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"console.d.ts","sourceRoot":"","sources":["../../../src/observers/console.ts"],"names":[],"mappings":"AAEA,KAAK,eAAe,GAAG,MAAM,OAAO,CAAC;AACrC,KAAK,QAAQ,GAAG,CAAC,QAAQ,EAAE,eAAe,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;AA4DjE;;;;GAIG;AACH,iBAAS,WAAW,CAAC,KAAK,EAAE,eAAe,EAAE,QAAQ,EAAE,QAAQ,GAAG,KAAK,GAAG,IAAI,CAc7E;AAED;;;GAGG;AACH,iBAAS,cAAc,CAAC,QAAQ,EAAE,QAAQ,QAUzC;AAID,iBAAS,eAAe,SASvB;AAED,QAAA,MAAM,eAAe;;;;CAIpB,CAAC;AAEF,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,CAAC"}
@@ -0,0 +1,133 @@
1
+ import { __read, __values } from "tslib";
2
+ import { getGlobalScope } from '../global-scope';
3
+ var globalScope = getGlobalScope();
4
+ /* istanbul ignore next */
5
+ var originalConsole = globalScope === null || globalScope === void 0 ? void 0 : globalScope.console;
6
+ var handlers = {};
7
+ // keeps reference to original console methods
8
+ var originalFn = {};
9
+ var inConsoleOverride = false;
10
+ function overrideConsole(logLevel) {
11
+ /* istanbul ignore if */
12
+ if (!originalConsole) {
13
+ return false;
14
+ }
15
+ // should not override if original console property is not a function
16
+ /* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access */
17
+ if (typeof originalConsole[logLevel] !== 'function') {
18
+ return false;
19
+ }
20
+ // if console is already overridden, return true
21
+ if (originalFn[logLevel]) {
22
+ return true;
23
+ }
24
+ // override console method
25
+ var handler = function () {
26
+ var args = [];
27
+ for (var _i = 0; _i < arguments.length; _i++) {
28
+ args[_i] = arguments[_i];
29
+ }
30
+ try {
31
+ if (handlers[logLevel] && !inConsoleOverride) {
32
+ // add a re-entrancy guard to prevent infinite recursion
33
+ inConsoleOverride = true;
34
+ var callbacks = handlers[logLevel];
35
+ if (callbacks) {
36
+ callbacks.forEach(function (callback) {
37
+ try {
38
+ callback(logLevel, args);
39
+ }
40
+ catch (_a) {
41
+ // do nothing
42
+ }
43
+ });
44
+ }
45
+ }
46
+ }
47
+ catch (_a) {
48
+ // do nothing
49
+ }
50
+ inConsoleOverride = false;
51
+ return originalFn[logLevel].apply(originalConsole, args);
52
+ };
53
+ /* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access */
54
+ originalFn[logLevel] = originalConsole[logLevel];
55
+ /* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access */
56
+ originalConsole[logLevel] = handler;
57
+ return true;
58
+ }
59
+ /**
60
+ * Observe a console log method (log, warn, error, etc.)
61
+ * @param level - The console log level to observe
62
+ * @param callback - The callback function to call when the console log level is observed
63
+ */
64
+ function addListener(level, callback) {
65
+ var res = overrideConsole(level);
66
+ /* istanbul ignore if */
67
+ if (!res) {
68
+ return new Error('Console override failed');
69
+ }
70
+ if (handlers[level]) {
71
+ // using ! is safe because we know the key exists based on condition above
72
+ handlers[level].push(callback);
73
+ }
74
+ else {
75
+ handlers[level] = [callback];
76
+ }
77
+ }
78
+ /**
79
+ * Disconnect a callback function from a console log method
80
+ * @param callback - The callback function to disconnect
81
+ */
82
+ function removeListener(callback) {
83
+ var e_1, _a;
84
+ try {
85
+ for (var _b = __values(Object.values(handlers)), _c = _b.next(); !_c.done; _c = _b.next()) {
86
+ var callbacks = _c.value;
87
+ // iterate backwards to avoid index shifting
88
+ for (var i = callbacks.length - 1; i >= 0; i--) {
89
+ if (callbacks[i] === callback) {
90
+ callbacks.splice(i, 1);
91
+ break;
92
+ }
93
+ }
94
+ }
95
+ }
96
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
97
+ finally {
98
+ try {
99
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
100
+ }
101
+ finally { if (e_1) throw e_1.error; }
102
+ }
103
+ }
104
+ // this should only be used for testing
105
+ // restoring console can break console overrides
106
+ function _restoreConsole() {
107
+ var e_2, _a;
108
+ try {
109
+ for (var _b = __values(Object.entries(originalFn)), _c = _b.next(); !_c.done; _c = _b.next()) {
110
+ var _d = __read(_c.value, 2), key = _d[0], originalHandler = _d[1];
111
+ if (originalHandler) {
112
+ /* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access */
113
+ originalConsole[key] = originalHandler;
114
+ }
115
+ }
116
+ }
117
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
118
+ finally {
119
+ try {
120
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
121
+ }
122
+ finally { if (e_2) throw e_2.error; }
123
+ }
124
+ originalFn = {};
125
+ handlers = {};
126
+ }
127
+ var consoleObserver = {
128
+ addListener: addListener,
129
+ removeListener: removeListener,
130
+ _restoreConsole: _restoreConsole,
131
+ };
132
+ export { consoleObserver };
133
+ //# sourceMappingURL=console.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"console.js","sourceRoot":"","sources":["../../../src/observers/console.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAKjD,IAAM,WAAW,GAAG,cAAc,EAAE,CAAC;AACrC,0BAA0B;AAC1B,IAAM,eAAe,GAAQ,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,OAAO,CAAC;AAElD,IAAI,QAAQ,GAAmD,EAAE,CAAC;AAElE,8CAA8C;AAC9C,IAAI,UAAU,GAA4D,EAAE,CAAC;AAE7E,IAAI,iBAAiB,GAAG,KAAK,CAAC;AAE9B,SAAS,eAAe,CAAC,QAAyB;IAChD,wBAAwB;IACxB,IAAI,CAAC,eAAe,EAAE;QACpB,OAAO,KAAK,CAAC;KACd;IAED,qEAAqE;I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n\ntype ConsoleLogLevel = keyof Console;\ntype Callback = (logLevel: ConsoleLogLevel, args: any[]) => void;\n\nconst globalScope = getGlobalScope();\n/* istanbul ignore next */\nconst originalConsole: any = globalScope?.console;\n\nlet handlers: { [key in ConsoleLogLevel]?: Array<Callback> } = {};\n\n// keeps reference to original console methods\nlet originalFn: { [key in ConsoleLogLevel]?: (...args: any[]) => void } = {};\n\nlet inConsoleOverride = false;\n\nfunction overrideConsole(logLevel: ConsoleLogLevel): boolean {\n /* istanbul ignore if */\n if (!originalConsole) {\n return false;\n }\n\n // should not override if original console property is not a function\n /* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access */\n if (typeof originalConsole[logLevel] !== 'function') {\n return false;\n }\n\n // if console is already overridden, return true\n if (originalFn[logLevel]) {\n return true;\n }\n\n // override console method\n const handler = function (...args: any[]) {\n try {\n if (handlers[logLevel] && !inConsoleOverride) {\n // add a re-entrancy guard to prevent infinite recursion\n inConsoleOverride = true;\n const callbacks = handlers[logLevel];\n if (callbacks) {\n callbacks.forEach((callback) => {\n try {\n callback(logLevel, args);\n } catch {\n // do nothing\n }\n });\n }\n }\n } catch {\n // do nothing\n }\n inConsoleOverride = false;\n return originalFn[logLevel]!.apply(originalConsole, args);\n };\n /* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access */\n originalFn[logLevel] = originalConsole[logLevel] as (...args: any[]) => void;\n /* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access */\n originalConsole[logLevel] = handler;\n return true;\n}\n\n/**\n * Observe a console log method (log, warn, error, etc.)\n * @param level - The console log level to observe\n * @param callback - The callback function to call when the console log level is observed\n */\nfunction addListener(level: ConsoleLogLevel, callback: Callback): Error | void {\n const res = overrideConsole(level);\n\n /* istanbul ignore if */\n if (!res) {\n return new Error('Console override failed');\n }\n\n if (handlers[level]) {\n // using ! is safe because we know the key exists based on condition above\n handlers[level]!.push(callback);\n } else {\n handlers[level] = [callback];\n }\n}\n\n/**\n * Disconnect a callback function from a console log method\n * @param callback - The callback function to disconnect\n */\nfunction removeListener(callback: Callback) {\n for (const callbacks of Object.values(handlers)) {\n // iterate backwards to avoid index shifting\n for (let i = callbacks.length - 1; i >= 0; i--) {\n if (callbacks[i] === callback) {\n callbacks.splice(i, 1);\n break;\n }\n }\n }\n}\n\n// this should only be used for testing\n// restoring console can break console overrides\nfunction _restoreConsole() {\n for (const [key, originalHandler] of Object.entries(originalFn)) {\n if (originalHandler) {\n /* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access */\n originalConsole[key] = originalHandler;\n }\n }\n originalFn = {};\n handlers = {};\n}\n\nconst consoleObserver = {\n addListener,\n removeListener,\n _restoreConsole,\n};\n\nexport { consoleObserver, ConsoleLogLevel };\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"browser-storage.d.ts","sourceRoot":"","sources":["../../../src/storage/browser-storage.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,IAAI,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAE/D,qBAAa,cAAc,CAAC,CAAC,CAAE,YAAW,gBAAgB,CAAC,CAAC,CAAC;IAC/C,OAAO,CAAC,OAAO,CAAC;gBAAR,OAAO,CAAC,qBAAS;IAE/B,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IAqB7B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAcxC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAIhD,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAQzC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQlC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAO7B"}
1
+ {"version":3,"file":"browser-storage.d.ts","sourceRoot":"","sources":["../../../src/storage/browser-storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAE/D,qBAAa,cAAc,CAAC,CAAC,CAAE,YAAW,gBAAgB,CAAC,CAAC,CAAC;IAC/C,OAAO,CAAC,OAAO,CAAC;gBAAR,OAAO,CAAC,qBAAS;IAE/B,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IAqB7B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAcxC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAIhD,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAQzC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQlC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAO7B"}
@@ -1,5 +1,4 @@
1
1
  import { __awaiter, __generator } from "tslib";
2
- import { UUID } from '../utils/uuid';
3
2
  var BrowserStorage = /** @class */ (function () {
4
3
  function BrowserStorage(storage) {
5
4
  this.storage = storage;
@@ -16,7 +15,7 @@ var BrowserStorage = /** @class */ (function () {
16
15
  }
17
16
  random = String(Date.now());
18
17
  testStorage = new BrowserStorage(this.storage);
19
- testKey = "AMP_TEST_".concat(UUID().substring(0, 8));
18
+ testKey = 'AMP_TEST';
20
19
  _b.label = 1;
21
20
  case 1:
22
21
  _b.trys.push([1, 4, 5, 7]);
@@ -1 +1 @@
1
- {"version":3,"file":"browser-storage.js","sourceRoot":"","sources":["../../../src/storage/browser-storage.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAGrC;IACE,wBAAoB,OAAiB;QAAjB,YAAO,GAAP,OAAO,CAAU;IAAG,CAAC;IAEnC,kCAAS,GAAf;;;;;;wBACE,wBAAwB;wBACxB,IAAI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AO,0CAAE,OAAO,CAAC,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;iBACnD;gBAAC,WAAM;oBACN,EAAE;iBACH;;;;KACF;IAEK,+BAAM,GAAZ,UAAa,GAAW;;;;gBACtB,IAAI;oBACF,MAAA,IAAI,CAAC,OAAO,0CAAE,UAAU,CAAC,GAAG,CAAC,CAAC;iBAC/B;gBAAC,WAAM;oBACN,EAAE;iBACH;;;;KACF;IAEK,8BAAK,GAAX;;;;gBACE,IAAI;oBACF,MAAA,IAAI,CAAC,OAAO,0CAAE,KAAK,EAAE,CAAC;iBACvB;gBAAC,WAAM;oBACN,EAAE;iBACH;;;;KACF;IACH,qBAAC;AAAD,CAAC,AAjED,IAiEC","sourcesContent":["import { UUID } from '../utils/uuid';\nimport { Storage as AmplitudeStorage } from '../types/storage';\n\nexport class BrowserStorage<T> implements AmplitudeStorage<T> {\n constructor(private storage?: Storage) {}\n\n async isEnabled(): Promise<boolean> {\n /* istanbul ignore if */\n if (!this.storage) {\n return false;\n }\n\n const random = String(Date.now());\n const testStorage = new BrowserStorage<string>(this.storage);\n const testKey = `AMP_TEST_${UUID().substring(0, 8)}`;\n try {\n await testStorage.set(testKey, random);\n const value = await testStorage.get(testKey);\n return value === random;\n } catch {\n /* istanbul ignore next */\n return false;\n } finally {\n await testStorage.remove(testKey);\n }\n }\n\n async get(key: string): Promise<T | undefined> {\n try {\n const value = await this.getRaw(key);\n if (!value) {\n return undefined;\n }\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return JSON.parse(value);\n } catch {\n console.error(`[Amplitude] Error: Could not get value from storage`);\n return undefined;\n }\n }\n\n async getRaw(key: string): Promise<string | undefined> {\n return this.storage?.getItem(key) || undefined;\n }\n\n async set(key: string, value: T): Promise<void> {\n try {\n this.storage?.setItem(key, JSON.stringify(value));\n } catch {\n //\n }\n }\n\n async remove(key: string): Promise<void> {\n try {\n this.storage?.removeItem(key);\n } catch {\n //\n }\n }\n\n async reset(): Promise<void> {\n try {\n this.storage?.clear();\n } catch {\n //\n }\n }\n}\n"]}
1
+ {"version":3,"file":"browser-storage.js","sourceRoot":"","sources":["../../../src/storage/browser-storage.ts"],"names":[],"mappings":";AAEA;IACE,wBAAoB,OAAiB;QAAjB,YAAO,GAAP,OAAO,CAAU;IAAG,CAAC;IAEnC,kCAAS,GAAf;;;;;;wBACE,wBAAwB;wBACxB,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE;4BACjB,sBAAO,KAAK,EAAC;yBACd;wBAEK,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;wBAC5B,WAAW,GAAG,IAAI,cAAc,CAAS,IAAI,CAAC,OAAO,CAAC,CAAC;wBACvD,OAAO,GAAG,UAAU,CAAC;;;;wBAEzB,qBAAM,WAAW,CAAC,GAAG,CAAC,OAAO,EAAE,MAAM,CAAC,EAAA;;wBAAtC,SAAsC,CAAC;wBACzB,qBAAM,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,EAAA;;wBAAtC,KAAK,GAAG,SAA8B;wBAC5C,sBAAO,KAAK,KAAK,MAAM,EAAC;;;wBAExB,0BAA0B;wBAC1B,sBAAO,KAAK,EAAC;4BAEb,qBAAM,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,EAAA;;wBAAjC,SAAiC,CAAC;;;;;;KAErC;IAEK,4BAAG,GAAT,UAAU,GAAW;;;;;;;wBAEH,qBAAM,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,EAAA;;wBAA9B,KAAK,GAAG,SAAsB;wBACpC,IAAI,CAAC,KAAK,EAAE;4BACV,sBAAO,SAAS,EAAC;yBAClB;wBACD,+DAA+D;wBAC/D,sBAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,EAAC;;;wBAEzB,OAAO,CAAC,KAAK,CAAC,qDAAqD,CAAC,CAAC;wBACrE,sBAAO,SAAS,EAAC;;;;;KAEpB;IAEK,+BAAM,GAAZ,UAAa,GAAW;;;;gBACtB,sBAAO,CAAA,MAAA,IAAI,CAAC,OAAO,0CAAE,OAAO,CAAC,GAAG,CAAC,KAAI,SAAS,EAAC;;;KAChD;IAEK,4BAAG,GAAT,UAAU,GAAW,EAAE,KAAQ;;;;gBAC7B,IAAI;oBACF,MAAA,IAAI,CAAC,OAAO,0CAAE,OAAO,CAAC,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;iBACnD;gBAAC,WAAM;oBACN,EAAE;iBACH;;;;KACF;IAEK,+BAAM,GAAZ,UAAa,GAAW;;;;gBACtB,IAAI;oBACF,MAAA,IAAI,CAAC,OAAO,0CAAE,UAAU,CAAC,GAAG,CAAC,CAAC;iBAC/B;gBAAC,WAAM;oBACN,EAAE;iBACH;;;;KACF;IAEK,8BAAK,GAAX;;;;gBACE,IAAI;oBACF,MAAA,IAAI,CAAC,OAAO,0CAAE,KAAK,EAAE,CAAC;iBACvB;gBAAC,WAAM;oBACN,EAAE;iBACH;;;;KACF;IACH,qBAAC;AAAD,CAAC,AAjED,IAiEC","sourcesContent":["import { Storage as AmplitudeStorage } from '../types/storage';\n\nexport class BrowserStorage<T> implements AmplitudeStorage<T> {\n constructor(private storage?: Storage) {}\n\n async isEnabled(): Promise<boolean> {\n /* istanbul ignore if */\n if (!this.storage) {\n return false;\n }\n\n const random = String(Date.now());\n const testStorage = new BrowserStorage<string>(this.storage);\n const testKey = 'AMP_TEST';\n try {\n await testStorage.set(testKey, random);\n const value = await testStorage.get(testKey);\n return value === random;\n } catch {\n /* istanbul ignore next */\n return false;\n } finally {\n await testStorage.remove(testKey);\n }\n }\n\n async get(key: string): Promise<T | undefined> {\n try {\n const value = await this.getRaw(key);\n if (!value) {\n return undefined;\n }\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return JSON.parse(value);\n } catch {\n console.error(`[Amplitude] Error: Could not get value from storage`);\n return undefined;\n }\n }\n\n async getRaw(key: string): Promise<string | undefined> {\n return this.storage?.getItem(key) || undefined;\n }\n\n async set(key: string, value: T): Promise<void> {\n try {\n this.storage?.setItem(key, JSON.stringify(value));\n } catch {\n //\n }\n }\n\n async remove(key: string): Promise<void> {\n try {\n this.storage?.removeItem(key);\n } catch {\n //\n }\n }\n\n async reset(): Promise<void> {\n try {\n this.storage?.clear();\n } catch {\n //\n }\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"cookie.d.ts","sourceRoot":"","sources":["../../../src/storage/cookie.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAqBtF,qBAAa,aAAa,CAAC,CAAC,CAAE,YAAW,OAAO,CAAC,CAAC,CAAC;IACjD,OAAO,EAAE,oBAAoB,CAAC;IAC9B,MAAM,EAAE,mBAAmB,CAAC;IAC5B,OAAO,CAAC,MAAM,CAAC,SAAS,CAAqB;gBAEjC,OAAO,CAAC,EAAE,oBAAoB,EAAE,MAAM,GAAE,mBAAwB;IAKtE,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IAqB7B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAmBxC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IA6DhD,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAkChD,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIlC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAG7B;AAoBD;;;GAGG;AACH,eAAO,MAAM,iBAAiB,UAAW,MAAM,KAAG,MAAM,GAAG,SAE1D,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,aAAa,YAAa,MAAM,GAAG,SAAS,WAAW,MAAM,GAAG,SAAS,KAAG,OAOxF,CAAC"}
1
+ {"version":3,"file":"cookie.d.ts","sourceRoot":"","sources":["../../../src/storage/cookie.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAsBtF,qBAAa,aAAa,CAAC,CAAC,CAAE,YAAW,OAAO,CAAC,CAAC,CAAC;IACjD,OAAO,EAAE,oBAAoB,CAAC;IAC9B,MAAM,EAAE,mBAAmB,CAAC;IAC5B,OAAO,CAAC,MAAM,CAAC,SAAS,CAAqB;gBAEjC,OAAO,CAAC,EAAE,oBAAoB,EAAE,MAAM,GAAE,mBAAwB;IAKtE,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IAyB7B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAmBxC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IA6DhD,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAkChD,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIlC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAG7B;AAoBD;;;GAGG;AACH,eAAO,MAAM,iBAAiB,UAAW,MAAM,KAAG,MAAM,GAAG,SAE1D,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,aAAa,YAAa,MAAM,GAAG,SAAS,WAAW,MAAM,GAAG,SAAS,KAAG,OAOxF,CAAC"}
@@ -1,5 +1,6 @@
1
1
  import { __assign, __awaiter, __generator, __values } from "tslib";
2
2
  import { getGlobalScope } from '../global-scope';
3
+ import { UUID } from '../utils/uuid';
3
4
  var CookieStorage = /** @class */ (function () {
4
5
  function CookieStorage(options, config) {
5
6
  if (config === void 0) { config = {}; }
@@ -8,7 +9,7 @@ var CookieStorage = /** @class */ (function () {
8
9
  }
9
10
  CookieStorage.prototype.isEnabled = function () {
10
11
  return __awaiter(this, void 0, void 0, function () {
11
- var testStorage, testKey, value, _a;
12
+ var testCookieOptions, testStorage, testKey, value, _a;
12
13
  return __generator(this, function (_b) {
13
14
  switch (_b.label) {
14
15
  case 0:
@@ -17,8 +18,9 @@ var CookieStorage = /** @class */ (function () {
17
18
  return [2 /*return*/, false];
18
19
  }
19
20
  CookieStorage.testValue = String(Date.now());
20
- testStorage = new CookieStorage(this.options);
21
- testKey = 'AMP_TEST';
21
+ testCookieOptions = __assign(__assign({}, this.options), { expirationDays: 0.003 });
22
+ testStorage = new CookieStorage(testCookieOptions);
23
+ testKey = "AMP_TEST_".concat(UUID().substring(0, 8));
22
24
  _b.label = 1;
23
25
  case 1:
24
26
  _b.trys.push([1, 4, 5, 7]);
@@ -1 +1 @@
1
- {"version":3,"file":"cookie.js","sourceRoot":"","sources":["../../../src/storage/cookie.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAoBjD;IAKE,uBAAY,OAA8B,EAAE,MAAgC;QAAhC,uBAAA,EAAA,WAAgC;QAC1E,IAAI,CAAC,OAAO,gBAAQ,OAAO,CAAE,CAAC;QAC9B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAEK,iCAAS,GAAf;;;;;;wBACE,wBAAwB;wBACxB,IAAI,CAAC,cAAc,EAAE,EAAE;4BACrB,sBAAO,KAAK,EAAC;yBACd;wBAED,aAAa,CAAC,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;wBACvC,WAAW,GAAG,IAAI,aAAa,CAAS,IAAI,CAAC,OAAO,CAAC,CAAC;wBACtD,OAAO,GAAG,UAAU,CAAC;;;;wBAEzB,qBAAM,WAAW,CAAC,GAAG,CAAC,OAAO,EAAE,aAAa,CAAC,SAAS,CAAC,EAAA;;w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,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;KAC5D;IAAC,WAAM;QACN,OAAO,SAAS,CAAC;KAClB;AACH,CAAC,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,IAAM,iBAAiB,GAAG,UAAC,KAAa;;IAC7C,OAAO,MAAA,sBAAsB,CAAC,KAAK,CAAC,mCAAI,kCAAkC,CAAC,KAAK,CAAC,CAAC;AACpF,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,IAAM,aAAa,GAAG,UAAC,OAA2B,EAAE,OAA2B;IACpF,IAAI,CAAC,OAAO,IAAI,CAAC,OAAO,EAAE;QACxB,OAAO,KAAK,CAAC;KACd;IACD,IAAM,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;IAC7E,IAAM,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;IAC7E,OAAO,WAAW,CAAC,WAAW,EAAE,KAAK,WAAW,CAAC,WAAW,EAAE,CAAC;AACjE,CAAC,CAAC","sourcesContent":["import { Storage, CookieStorageOptions, CookieStorageConfig } from '../types/storage';\nimport { getGlobalScope } from '../global-scope';\n\n// CookieStore is a Web API not included in standard TypeScript lib types\n// https://developer.mozilla.org/en-US/docs/Web/API/CookieStore\ninterface CookieStoreSetOptions {\n name: string;\n value: string;\n expires?: number;\n domain?: string;\n sameSite?: 'strict' | 'lax' | 'none';\n}\n\ninterface CookieStore {\n getAll(key: string): Promise<CookieStoreSetOptions[] | undefined>;\n}\n\ntype GlobalScopeWithCookieStore = {\n cookieStore?: CookieStore;\n} & typeof global;\n\nexport class CookieStorage<T> implements Storage<T> {\n options: CookieStorageOptions;\n config: CookieStorageConfig;\n private static testValue: undefined | string;\n\n constructor(options?: CookieStorageOptions, config: CookieStorageConfig = {}) {\n this.options = { ...options };\n this.config = config;\n }\n\n async isEnabled(): Promise<boolean> {\n /* istanbul ignore if */\n if (!getGlobalScope()) {\n return false;\n }\n\n CookieStorage.testValue = String(Date.now());\n const testStorage = new CookieStorage<string>(this.options);\n const testKey = 'AMP_TEST';\n try {\n await testStorage.set(testKey, CookieStorage.testValue);\n const value = await testStorage.get(testKey);\n return value === CookieStorage.testValue;\n } catch {\n /* istanbul ignore next */\n return false;\n } finally {\n await testStorage.remove(testKey);\n }\n }\n\n async get(key: string): Promise<T | undefined> {\n const value = await this.getRaw(key);\n if (!value) {\n return undefined;\n }\n try {\n const decodedValue = decodeCookieValue(value);\n if (decodedValue === undefined) {\n console.error(`Amplitude Logger [Error]: Failed to decode cookie value for key: ${key}, value: ${value}`);\n return undefined;\n }\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return JSON.parse(decodedValue);\n } catch {\n console.error(`Amplitude Logger [Error]: Failed to parse cookie value for key: ${key}, value: ${value}`);\n return undefined;\n }\n }\n\n async getRaw(key: string): Promise<string | undefined> {\n const globalScope = getGlobalScope();\n\n // use CookieStore if available and enabled\n const globalScopeWithCookiesStore = globalScope as GlobalScopeWithCookieStore;\n try {\n const cookieStore = globalScopeWithCookiesStore?.cookieStore;\n if (cookieStore) {\n const cookies = await cookieStore.getAll(key);\n if (cookies) {\n /* istanbul ignore if */\n if (cookies.length > 1) {\n this.config.diagnosticsClient?.recordEvent('cookies.duplicate', {\n cookies: cookies.map((cookie) => cookie.domain),\n });\n this.config.diagnosticsClient?.increment('cookies.duplicate.occurrence.cookieStore');\n }\n\n for (const cookie of cookies) {\n if (isDomainEqual(cookie.domain, this.options.domain)) {\n return cookie.value;\n }\n }\n }\n }\n } catch (ignoreError) {\n /* istanbul ignore next */\n // if cookieStore had a surprise failure, fallback to document.cookie\n }\n\n const cookies = (globalScope?.document?.cookie.split('; ') ?? []).filter((c) => c.indexOf(key + '=') === 0);\n let match: string | undefined = undefined;\n\n // if matcher function is provided, use it to de-duplicate when there's more than one cookie\n /* istanbul ignore if */\n const duplicateResolverFn = this.config.duplicateResolverFn;\n if (typeof duplicateResolverFn === 'function' && cookies.length > 1) {\n match = cookies.find((c) => {\n try {\n const res = duplicateResolverFn(c.substring(key.length + 1));\n if (!res) {\n this.config.diagnosticsClient?.increment('cookies.duplicate.occurrence.document.cookie');\n }\n return res;\n } catch (ignoreError) {\n /* istanbul ignore next */\n return false;\n }\n });\n }\n\n // if match was not found, just get the first one that matches the key\n if (!match) {\n match = cookies[0];\n }\n if (!match) {\n return undefined;\n }\n return match.substring(key.length + 1);\n }\n\n async set(key: string, value: T | null): Promise<void> {\n try {\n const expirationDays = this.options.expirationDays ?? 0;\n const expires = value !== null ? expirationDays : -1;\n let expireDate: Date | undefined = undefined;\n if (expires) {\n const date = new Date();\n date.setTime(date.getTime() + expires * 24 * 60 * 60 * 1000);\n expireDate = date;\n }\n let str = `${key}=${btoa(encodeURIComponent(JSON.stringify(value)))}`;\n if (expireDate) {\n str += `; expires=${expireDate.toUTCString()}`;\n }\n str += '; path=/';\n if (this.options.domain) {\n str += `; domain=${this.options.domain}`;\n }\n if (this.options.secure) {\n str += '; Secure';\n }\n if (this.options.sameSite) {\n str += `; SameSite=${this.options.sameSite}`;\n }\n const globalScope = getGlobalScope();\n if (globalScope) {\n globalScope.document.cookie = str;\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n console.error(`Amplitude Logger [Error]: Failed to set cookie for key: ${key}. Error: ${errorMessage}`);\n }\n }\n\n async remove(key: string): Promise<void> {\n await this.set(key, null);\n }\n\n async reset(): Promise<void> {\n return;\n }\n}\n\nconst decodeCookiesAsDefault = (value: string): string | undefined => {\n try {\n return decodeURIComponent(atob(value));\n } catch {\n return undefined;\n }\n};\n\nconst decodeCookiesWithDoubleUrlEncoding = (value: string): string | undefined => {\n // Modern Ruby (v7+) automatically encodes cookies with URL encoding by\n // https://api.rubyonrails.org/classes/ActionDispatch/Cookies.html\n try {\n return decodeURIComponent(atob(decodeURIComponent(value)));\n } catch {\n return undefined;\n }\n};\n\n/**\n * Decodes a cookie value that was encoded with btoa(encodeURIComponent(...)).\n * Handles both standard encoding and double URL encoding (used by Ruby Rails v7+).\n */\nexport const decodeCookieValue = (value: string): string | undefined => {\n return decodeCookiesAsDefault(value) ?? decodeCookiesWithDoubleUrlEncoding(value);\n};\n\n/**\n * Compares two domain strings for equality, ignoring leading dots.\n * This is useful for comparing cookie domains since \".example.com\" and \"example.com\"\n * are effectively equivalent for cookie scoping.\n */\nexport const isDomainEqual = (domain1: string | undefined, domain2: string | undefined): boolean => {\n if (!domain1 || !domain2) {\n return false;\n }\n const normalized1 = domain1.startsWith('.') ? domain1.substring(1) : domain1;\n const normalized2 = domain2.startsWith('.') ? domain2.substring(1) : domain2;\n return normalized1.toLowerCase() === normalized2.toLowerCase();\n};\n"]}
1
+ {"version":3,"file":"cookie.js","sourceRoot":"","sources":["../../../src/storage/cookie.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAoBrC;IAKE,uBAAY,OAA8B,EAAE,MAAgC;QAAhC,uBAAA,EAAA,WAAgC;QAC1E,IAAI,CAAC,OAAO,gBAAQ,OAAO,CAAE,CAAC;QAC9B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAEK,iCAAS,GAAf;;;;;;wBACE,wBAAwB;wBACxB,IAAI,CAAC,cAAc,EAAE,EAAE;4BACrB,sBAAO,KAAK,EAAC;yBACd;wBAED,aAAa,CAAC,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;wBACvC,iBAAiB,yBAClB,IAAI,CAAC,OAAO,KACf,cAAc,EAAE,KAAK,GACtB,CAAC;wBACI,WAAW,GAAG,IAAI,aAAa,CAAS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CAAC,KAAK,CAAC,CAAC,CAAC;KACxC;IAAC,WAAM;QACN,OAAO,SAAS,CAAC;KAClB;AACH,CAAC,CAAC;AAEF,IAAM,kCAAkC,GAAG,UAAC,KAAa;IACvD,uEAAuE;IACvE,kEAAkE;IAClE,IAAI;QACF,OAAO,kBAAkB,CAAC,IAAI,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;KAC5D;IAAC,WAAM;QACN,OAAO,SAAS,CAAC;KAClB;AACH,CAAC,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,IAAM,iBAAiB,GAAG,UAAC,KAAa;;IAC7C,OAAO,MAAA,sBAAsB,CAAC,KAAK,CAAC,mCAAI,kCAAkC,CAAC,KAAK,CAAC,CAAC;AACpF,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,IAAM,aAAa,GAAG,UAAC,OAA2B,EAAE,OAA2B;IACpF,IAAI,CAAC,OAAO,IAAI,CAAC,OAAO,EAAE;QACxB,OAAO,KAAK,CAAC;KACd;IACD,IAAM,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;IAC7E,IAAM,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;IAC7E,OAAO,WAAW,CAAC,WAAW,EAAE,KAAK,WAAW,CAAC,WAAW,EAAE,CAAC;AACjE,CAAC,CAAC","sourcesContent":["import { Storage, CookieStorageOptions, CookieStorageConfig } from '../types/storage';\nimport { getGlobalScope } from '../global-scope';\nimport { UUID } from '../utils/uuid';\n\n// CookieStore is a Web API not included in standard TypeScript lib types\n// https://developer.mozilla.org/en-US/docs/Web/API/CookieStore\ninterface CookieStoreSetOptions {\n name: string;\n value: string;\n expires?: number;\n domain?: string;\n sameSite?: 'strict' | 'lax' | 'none';\n}\n\ninterface CookieStore {\n getAll(key: string): Promise<CookieStoreSetOptions[] | undefined>;\n}\n\ntype GlobalScopeWithCookieStore = {\n cookieStore?: CookieStore;\n} & typeof global;\n\nexport class CookieStorage<T> implements Storage<T> {\n options: CookieStorageOptions;\n config: CookieStorageConfig;\n private static testValue: undefined | string;\n\n constructor(options?: CookieStorageOptions, config: CookieStorageConfig = {}) {\n this.options = { ...options };\n this.config = config;\n }\n\n async isEnabled(): Promise<boolean> {\n /* istanbul ignore if */\n if (!getGlobalScope()) {\n return false;\n }\n\n CookieStorage.testValue = String(Date.now());\n const testCookieOptions = {\n ...this.options,\n expirationDays: 0.003, // expire in ~5 minutes\n };\n const testStorage = new CookieStorage<string>(testCookieOptions);\n const testKey = `AMP_TEST_${UUID().substring(0, 8)}`;\n try {\n await testStorage.set(testKey, CookieStorage.testValue);\n const value = await testStorage.get(testKey);\n return value === CookieStorage.testValue;\n } catch {\n /* istanbul ignore next */\n return false;\n } finally {\n await testStorage.remove(testKey);\n }\n }\n\n async get(key: string): Promise<T | undefined> {\n const value = await this.getRaw(key);\n if (!value) {\n return undefined;\n }\n try {\n const decodedValue = decodeCookieValue(value);\n if (decodedValue === undefined) {\n console.error(`Amplitude Logger [Error]: Failed to decode cookie value for key: ${key}, value: ${value}`);\n return undefined;\n }\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return JSON.parse(decodedValue);\n } catch {\n console.error(`Amplitude Logger [Error]: Failed to parse cookie value for key: ${key}, value: ${value}`);\n return undefined;\n }\n }\n\n async getRaw(key: string): Promise<string | undefined> {\n const globalScope = getGlobalScope();\n\n // use CookieStore if available and enabled\n const globalScopeWithCookiesStore = globalScope as GlobalScopeWithCookieStore;\n try {\n const cookieStore = globalScopeWithCookiesStore?.cookieStore;\n if (cookieStore) {\n const cookies = await cookieStore.getAll(key);\n if (cookies) {\n /* istanbul ignore if */\n if (cookies.length > 1) {\n this.config.diagnosticsClient?.recordEvent('cookies.duplicate', {\n cookies: cookies.map((cookie) => cookie.domain),\n });\n this.config.diagnosticsClient?.increment('cookies.duplicate.occurrence.cookieStore');\n }\n\n for (const cookie of cookies) {\n if (isDomainEqual(cookie.domain, this.options.domain)) {\n return cookie.value;\n }\n }\n }\n }\n } catch (ignoreError) {\n /* istanbul ignore next */\n // if cookieStore had a surprise failure, fallback to document.cookie\n }\n\n const cookies = (globalScope?.document?.cookie.split('; ') ?? []).filter((c) => c.indexOf(key + '=') === 0);\n let match: string | undefined = undefined;\n\n // if matcher function is provided, use it to de-duplicate when there's more than one cookie\n /* istanbul ignore if */\n const duplicateResolverFn = this.config.duplicateResolverFn;\n if (typeof duplicateResolverFn === 'function' && cookies.length > 1) {\n match = cookies.find((c) => {\n try {\n const res = duplicateResolverFn(c.substring(key.length + 1));\n if (!res) {\n this.config.diagnosticsClient?.increment('cookies.duplicate.occurrence.document.cookie');\n }\n return res;\n } catch (ignoreError) {\n /* istanbul ignore next */\n return false;\n }\n });\n }\n\n // if match was not found, just get the first one that matches the key\n if (!match) {\n match = cookies[0];\n }\n if (!match) {\n return undefined;\n }\n return match.substring(key.length + 1);\n }\n\n async set(key: string, value: T | null): Promise<void> {\n try {\n const expirationDays = this.options.expirationDays ?? 0;\n const expires = value !== null ? expirationDays : -1;\n let expireDate: Date | undefined = undefined;\n if (expires) {\n const date = new Date();\n date.setTime(date.getTime() + expires * 24 * 60 * 60 * 1000);\n expireDate = date;\n }\n let str = `${key}=${btoa(encodeURIComponent(JSON.stringify(value)))}`;\n if (expireDate) {\n str += `; expires=${expireDate.toUTCString()}`;\n }\n str += '; path=/';\n if (this.options.domain) {\n str += `; domain=${this.options.domain}`;\n }\n if (this.options.secure) {\n str += '; Secure';\n }\n if (this.options.sameSite) {\n str += `; SameSite=${this.options.sameSite}`;\n }\n const globalScope = getGlobalScope();\n if (globalScope) {\n globalScope.document.cookie = str;\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n console.error(`Amplitude Logger [Error]: Failed to set cookie for key: ${key}. Error: ${errorMessage}`);\n }\n }\n\n async remove(key: string): Promise<void> {\n await this.set(key, null);\n }\n\n async reset(): Promise<void> {\n return;\n }\n}\n\nconst decodeCookiesAsDefault = (value: string): string | undefined => {\n try {\n return decodeURIComponent(atob(value));\n } catch {\n return undefined;\n }\n};\n\nconst decodeCookiesWithDoubleUrlEncoding = (value: string): string | undefined => {\n // Modern Ruby (v7+) automatically encodes cookies with URL encoding by\n // https://api.rubyonrails.org/classes/ActionDispatch/Cookies.html\n try {\n return decodeURIComponent(atob(decodeURIComponent(value)));\n } catch {\n return undefined;\n }\n};\n\n/**\n * Decodes a cookie value that was encoded with btoa(encodeURIComponent(...)).\n * Handles both standard encoding and double URL encoding (used by Ruby Rails v7+).\n */\nexport const decodeCookieValue = (value: string): string | undefined => {\n return decodeCookiesAsDefault(value) ?? decodeCookiesWithDoubleUrlEncoding(value);\n};\n\n/**\n * Compares two domain strings for equality, ignoring leading dots.\n * This is useful for comparing cookie domains since \".example.com\" and \"example.com\"\n * are effectively equivalent for cookie scoping.\n */\nexport const isDomainEqual = (domain1: string | undefined, domain2: string | undefined): boolean => {\n if (!domain1 || !domain2) {\n return false;\n }\n const normalized1 = domain1.startsWith('.') ? domain1.substring(1) : domain1;\n const normalized2 = domain2.startsWith('.') ? domain2.substring(1) : domain2;\n return normalized1.toLowerCase() === normalized2.toLowerCase();\n};\n"]}
@@ -52,13 +52,17 @@ export interface FrustrationInteractionsOptions {
52
52
  */
53
53
  dataAttributePrefix?: string;
54
54
  /**
55
- * Configuration for dead clicks tracking
55
+ * Configuration for dead clicks tracking.
56
+ * Set to `false` to disable dead click tracking.
57
+ * Set to `true` or an options object to enable with default or custom settings.
56
58
  */
57
- deadClicks?: DeadClickOptions;
59
+ deadClicks?: boolean | DeadClickOptions;
58
60
  /**
59
- * Configuration for rage clicks tracking
61
+ * Configuration for rage clicks tracking.
62
+ * Set to `false` to disable rage click tracking.
63
+ * Set to `true` or an options object to enable with default or custom settings.
60
64
  */
61
- rageClicks?: RageClickOptions;
65
+ rageClicks?: boolean | RageClickOptions;
62
66
  /**
63
67
  * RegExp pattern list to allow custom patterns for text masking
64
68
  */
@@ -1 +1 @@
1
- {"version":3,"file":"frustration-interactions.d.ts","sourceRoot":"","sources":["../../../src/types/frustration-interactions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAEpD;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,oBAAoB,CAAC,EAAE,MAAM,EAAE,CAAC;CACjC;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,oBAAoB,CAAC,EAAE,MAAM,EAAE,CAAC;CACjC;AAED;;;GAGG;AACH,MAAM,WAAW,8BAA8B;IAC7C;;;;OAIG;IACH,gBAAgB,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;IAEvC;;;;;OAKG;IACH,kBAAkB,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;IAEzC;;;;;;;OAOG;IACH,wBAAwB,CAAC,EAAE,CAAC,UAAU,EAAE,UAAU,EAAE,OAAO,EAAE,UAAU,KAAK,OAAO,CAAC;IAEpF;;;OAGG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAE7B;;OAEG;IACH,UAAU,CAAC,EAAE,gBAAgB,CAAC;IAE9B;;OAEG;IACH,UAAU,CAAC,EAAE,gBAAgB,CAAC;IAE9B;;OAEG;IACH,aAAa,CAAC,EAAE,CAAC,MAAM,GAAG;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,CAAA;KAAE,CAAC,EAAE,CAAC;CACvE;AAgBD;;GAEG;AACH,eAAO,MAAM,4BAA4B,UAOxC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,4BAA4B,UAAQ,CAAC;AAElD;;GAEG;AACH,eAAO,MAAM,4BAA4B,OAAQ,CAAC;AAElD;;GAEG;AACH,eAAO,MAAM,4BAA4B,OAAQ,CAAC;AAElD;;GAEG;AACH,eAAO,MAAM,4BAA4B,IAAI,CAAC;AAE9C;;GAEG;AACH,eAAO,MAAM,0CAA0C,KAAK,CAAC;AAK7D,KAAK,UAAU,GAAG,OAAO,UAAU,SAAS;IAC1C,OAAO,EAAE,KAAK,GAAG,IAAI,EAAE,GAAG,KAAK,MAAM,CAAC,CAAC;CACxC,GACG,CAAC,GACD,KAAK,CAAC"}
1
+ {"version":3,"file":"frustration-interactions.d.ts","sourceRoot":"","sources":["../../../src/types/frustration-interactions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAEpD;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,oBAAoB,CAAC,EAAE,MAAM,EAAE,CAAC;CACjC;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,oBAAoB,CAAC,EAAE,MAAM,EAAE,CAAC;CACjC;AAED;;;GAGG;AACH,MAAM,WAAW,8BAA8B;IAC7C;;;;OAIG;IACH,gBAAgB,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;IAEvC;;;;;OAKG;IACH,kBAAkB,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;IAEzC;;;;;;;OAOG;IACH,wBAAwB,CAAC,EAAE,CAAC,UAAU,EAAE,UAAU,EAAE,OAAO,EAAE,UAAU,KAAK,OAAO,CAAC;IAEpF;;;OAGG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAE7B;;;;OAIG;IACH,UAAU,CAAC,EAAE,OAAO,GAAG,gBAAgB,CAAC;IAExC;;;;OAIG;IACH,UAAU,CAAC,EAAE,OAAO,GAAG,gBAAgB,CAAC;IAExC;;OAEG;IACH,aAAa,CAAC,EAAE,CAAC,MAAM,GAAG;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,CAAA;KAAE,CAAC,EAAE,CAAC;CACvE;AAgBD;;GAEG;AACH,eAAO,MAAM,4BAA4B,UAOxC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,4BAA4B,UAAQ,CAAC;AAElD;;GAEG;AACH,eAAO,MAAM,4BAA4B,OAAQ,CAAC;AAElD;;GAEG;AACH,eAAO,MAAM,4BAA4B,OAAQ,CAAC;AAElD;;GAEG;AACH,eAAO,MAAM,4BAA4B,IAAI,CAAC;AAE9C;;GAEG;AACH,eAAO,MAAM,0CAA0C,KAAK,CAAC;AAK7D,KAAK,UAAU,GAAG,OAAO,UAAU,SAAS;IAC1C,OAAO,EAAE,KAAK,GAAG,IAAI,EAAE,GAAG,KAAK,MAAM,CAAC,CAAC;CACxC,GACG,CAAC,GACD,KAAK,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"frustration-interactions.js","sourceRoot":"","sources":["../../../src/types/frustration-interactions.ts"],"names":[],"mappings":";AA4EA,IAAM,2BAA2B,GAAG;IAClC,GAAG;IACH,QAAQ;IACR,iBAAiB;IACjB,eAAe;IACf,mBAAmB;IACnB,2BAA2B;IAC3B,wBAAwB;IACxB,iBAAiB;IACjB,cAAc;IACd,mBAAmB;IACnB,4BAA4B;CAC7B,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,IAAM,4BAA4B;IACvC,sBAAsB;IACtB,sBAAsB;IACtB,qBAAqB;IACrB,qBAAqB;IACrB,oBAAoB;UACjB,2BAA2B,SAC/B,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,IAAM,4BAA4B,GAAG,CAAC,GAAG,CAAC,CAAC;AAElD;;GAEG;AACH,MAAM,CAAC,IAAM,4BAA4B,GAAG,IAAK,CAAC;AAElD;;GAEG;AACH,MAAM,CAAC,IAAM,4BAA4B,GAAG,IAAK,CAAC;AAElD;;GAEG;AACH,MAAM,CAAC,IAAM,4BAA4B,GAAG,CAAC,CAAC;AAE9C;;GAEG;AACH,MAAM,CAAC,IAAM,0CAA0C,GAAG,EAAE,CAAC,CAAC,SAAS","sourcesContent":["import { ActionType } from './element-interactions';\n\n/**\n * Configuration options for dead clicks tracking\n */\nexport interface DeadClickOptions {\n /**\n * CSS selectors to define which elements on the page to track for dead clicks.\n * A dead click is a click that doesn't result in any visible change or navigation.\n */\n cssSelectorAllowlist?: string[];\n}\n\n/**\n * Configuration options for rage clicks tracking\n */\nexport interface RageClickOptions {\n /**\n * CSS selectors to define which elements on the page to track for rage clicks.\n * A rage click is multiple rapid clicks on the same element within a 3s time window.\n */\n cssSelectorAllowlist?: string[];\n}\n\n/**\n * Configuration options for frustration interactions tracking.\n * This includes dead clicks and rage clicks tracking.\n */\nexport interface FrustrationInteractionsOptions {\n /**\n * List of page URLs to allow auto tracking on.\n * When provided, only allow tracking on these URLs.\n * Both full URLs and regex are supported.\n */\n pageUrlAllowlist?: (string | RegExp)[];\n\n /**\n * List of page URLs to exclude from auto tracking.\n * When provided, tracking will be blocked on these URLs.\n * Both full URLs and regex are supported.\n * This takes precedence over pageUrlAllowlist.\n */\n pageUrlExcludelist?: (string | RegExp)[];\n\n /**\n * Function to determine whether an event should be tracked.\n * When provided, this function overwrites all other allowlists and configurations.\n * If the function returns true, the event will be tracked.\n * If the function returns false, the event will not be tracked.\n * @param actionType - The type of action that triggered the event.\n * @param element - The [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) that triggered the event.\n */\n shouldTrackEventResolver?: (actionType: ActionType, element: DomElement) => boolean;\n\n /**\n * Prefix for data attributes to allow auto collecting.\n * Default is 'data-amp-track-'.\n */\n dataAttributePrefix?: string;\n\n /**\n * Configuration for dead clicks tracking\n */\n deadClicks?: DeadClickOptions;\n\n /**\n * Configuration for rage clicks tracking\n */\n rageClicks?: RageClickOptions;\n\n /**\n * RegExp pattern list to allow custom patterns for text masking\n */\n maskTextRegex?: (RegExp | { pattern: string; description: string })[];\n}\n\nconst CLICKABLE_ELEMENT_SELECTORS = [\n 'a',\n 'button',\n '[role=\"button\"]',\n '[role=\"link\"]',\n '[role=\"menuitem\"]',\n '[role=\"menuitemcheckbox\"]',\n '[role=\"menuitemradio\"]',\n '[role=\"option\"]',\n '[role=\"tab\"]',\n '[role=\"treeitem\"]',\n '[contenteditable=\"true\" i]',\n];\n\n/**\n * Default CSS selectors for dead clicks tracking\n */\nexport const DEFAULT_DEAD_CLICK_ALLOWLIST = [\n 'input[type=\"button\"]',\n 'input[type=\"submit\"]',\n 'input[type=\"reset\"]',\n 'input[type=\"image\"]',\n 'input[type=\"file\"]',\n ...CLICKABLE_ELEMENT_SELECTORS,\n];\n\n/**\n * Default CSS selectors for rage clicks tracking\n */\nexport const DEFAULT_RAGE_CLICK_ALLOWLIST = ['*'];\n\n/**\n * Default time window for dead clicks (3 seconds)\n */\nexport const DEFAULT_DEAD_CLICK_WINDOW_MS = 3_000;\n\n/**\n * Default time window for rage clicks (1 second)\n */\nexport const DEFAULT_RAGE_CLICK_WINDOW_MS = 1_000;\n\n/**\n * Default threshold for rage clicks (4 clicks)\n */\nexport const DEFAULT_RAGE_CLICK_THRESHOLD = 4;\n\n/**\n * Default threshold for rage clicks to be considered out of bounds (50 pixels)\n */\nexport const DEFAULT_RAGE_CLICK_OUT_OF_BOUNDS_THRESHOLD = 50; // pixels\n\n// DomElement is [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) if the dom library is included in tsconfig.json\n// and never if it is not included\n// eslint-disable-next-line no-restricted-globals\ntype DomElement = typeof globalThis extends {\n Element: new (...args: any) => infer T;\n}\n ? T\n : never;\n"]}
1
+ {"version":3,"file":"frustration-interactions.js","sourceRoot":"","sources":["../../../src/types/frustration-interactions.ts"],"names":[],"mappings":";AAgFA,IAAM,2BAA2B,GAAG;IAClC,GAAG;IACH,QAAQ;IACR,iBAAiB;IACjB,eAAe;IACf,mBAAmB;IACnB,2BAA2B;IAC3B,wBAAwB;IACxB,iBAAiB;IACjB,cAAc;IACd,mBAAmB;IACnB,4BAA4B;CAC7B,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,IAAM,4BAA4B;IACvC,sBAAsB;IACtB,sBAAsB;IACtB,qBAAqB;IACrB,qBAAqB;IACrB,oBAAoB;UACjB,2BAA2B,SAC/B,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,IAAM,4BAA4B,GAAG,CAAC,GAAG,CAAC,CAAC;AAElD;;GAEG;AACH,MAAM,CAAC,IAAM,4BAA4B,GAAG,IAAK,CAAC;AAElD;;GAEG;AACH,MAAM,CAAC,IAAM,4BAA4B,GAAG,IAAK,CAAC;AAElD;;GAEG;AACH,MAAM,CAAC,IAAM,4BAA4B,GAAG,CAAC,CAAC;AAE9C;;GAEG;AACH,MAAM,CAAC,IAAM,0CAA0C,GAAG,EAAE,CAAC,CAAC,SAAS","sourcesContent":["import { ActionType } from './element-interactions';\n\n/**\n * Configuration options for dead clicks tracking\n */\nexport interface DeadClickOptions {\n /**\n * CSS selectors to define which elements on the page to track for dead clicks.\n * A dead click is a click that doesn't result in any visible change or navigation.\n */\n cssSelectorAllowlist?: string[];\n}\n\n/**\n * Configuration options for rage clicks tracking\n */\nexport interface RageClickOptions {\n /**\n * CSS selectors to define which elements on the page to track for rage clicks.\n * A rage click is multiple rapid clicks on the same element within a 3s time window.\n */\n cssSelectorAllowlist?: string[];\n}\n\n/**\n * Configuration options for frustration interactions tracking.\n * This includes dead clicks and rage clicks tracking.\n */\nexport interface FrustrationInteractionsOptions {\n /**\n * List of page URLs to allow auto tracking on.\n * When provided, only allow tracking on these URLs.\n * Both full URLs and regex are supported.\n */\n pageUrlAllowlist?: (string | RegExp)[];\n\n /**\n * List of page URLs to exclude from auto tracking.\n * When provided, tracking will be blocked on these URLs.\n * Both full URLs and regex are supported.\n * This takes precedence over pageUrlAllowlist.\n */\n pageUrlExcludelist?: (string | RegExp)[];\n\n /**\n * Function to determine whether an event should be tracked.\n * When provided, this function overwrites all other allowlists and configurations.\n * If the function returns true, the event will be tracked.\n * If the function returns false, the event will not be tracked.\n * @param actionType - The type of action that triggered the event.\n * @param element - The [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) that triggered the event.\n */\n shouldTrackEventResolver?: (actionType: ActionType, element: DomElement) => boolean;\n\n /**\n * Prefix for data attributes to allow auto collecting.\n * Default is 'data-amp-track-'.\n */\n dataAttributePrefix?: string;\n\n /**\n * Configuration for dead clicks tracking.\n * Set to `false` to disable dead click tracking.\n * Set to `true` or an options object to enable with default or custom settings.\n */\n deadClicks?: boolean | DeadClickOptions;\n\n /**\n * Configuration for rage clicks tracking.\n * Set to `false` to disable rage click tracking.\n * Set to `true` or an options object to enable with default or custom settings.\n */\n rageClicks?: boolean | RageClickOptions;\n\n /**\n * RegExp pattern list to allow custom patterns for text masking\n */\n maskTextRegex?: (RegExp | { pattern: string; description: string })[];\n}\n\nconst CLICKABLE_ELEMENT_SELECTORS = [\n 'a',\n 'button',\n '[role=\"button\"]',\n '[role=\"link\"]',\n '[role=\"menuitem\"]',\n '[role=\"menuitemcheckbox\"]',\n '[role=\"menuitemradio\"]',\n '[role=\"option\"]',\n '[role=\"tab\"]',\n '[role=\"treeitem\"]',\n '[contenteditable=\"true\" i]',\n];\n\n/**\n * Default CSS selectors for dead clicks tracking\n */\nexport const DEFAULT_DEAD_CLICK_ALLOWLIST = [\n 'input[type=\"button\"]',\n 'input[type=\"submit\"]',\n 'input[type=\"reset\"]',\n 'input[type=\"image\"]',\n 'input[type=\"file\"]',\n ...CLICKABLE_ELEMENT_SELECTORS,\n];\n\n/**\n * Default CSS selectors for rage clicks tracking\n */\nexport const DEFAULT_RAGE_CLICK_ALLOWLIST = ['*'];\n\n/**\n * Default time window for dead clicks (3 seconds)\n */\nexport const DEFAULT_DEAD_CLICK_WINDOW_MS = 3_000;\n\n/**\n * Default time window for rage clicks (1 second)\n */\nexport const DEFAULT_RAGE_CLICK_WINDOW_MS = 1_000;\n\n/**\n * Default threshold for rage clicks (4 clicks)\n */\nexport const DEFAULT_RAGE_CLICK_THRESHOLD = 4;\n\n/**\n * Default threshold for rage clicks to be considered out of bounds (50 pixels)\n */\nexport const DEFAULT_RAGE_CLICK_OUT_OF_BOUNDS_THRESHOLD = 50; // pixels\n\n// DomElement is [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) if the dom library is included in tsconfig.json\n// and never if it is not included\n// eslint-disable-next-line no-restricted-globals\ntype DomElement = typeof globalThis extends {\n Element: new (...args: any) => infer T;\n}\n ? T\n : never;\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amplitude/analytics-core",
3
- "version": "2.35.1",
3
+ "version": "2.36.0",
4
4
  "description": "",
5
5
  "author": "Amplitude Inc",
6
6
  "homepage": "https://github.com/amplitude/Amplitude-TypeScript",