posthog-js 1.84.2 → 1.84.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/dist/array.full.js +1 -10
  2. package/dist/array.full.js.map +1 -1
  3. package/dist/array.js +1 -10
  4. package/dist/array.js.map +1 -1
  5. package/dist/es.js +1 -10
  6. package/dist/es.js.map +1 -1
  7. package/dist/module.d.ts +32 -148
  8. package/dist/module.js +1 -10
  9. package/dist/module.js.map +1 -1
  10. package/dist/recorder-v2.js +2 -2
  11. package/dist/recorder-v2.js.map +1 -1
  12. package/dist/recorder.js +1 -1
  13. package/dist/recorder.js.map +1 -1
  14. package/dist/surveys.js +1 -1
  15. package/dist/surveys.js.map +1 -1
  16. package/lib/package.json +9 -4
  17. package/lib/src/autocapture-utils.js +6 -6
  18. package/lib/src/autocapture-utils.js.map +1 -1
  19. package/lib/src/autocapture.js +5 -5
  20. package/lib/src/autocapture.js.map +1 -1
  21. package/lib/src/constants.d.ts +1 -0
  22. package/lib/src/constants.js +1 -0
  23. package/lib/src/constants.js.map +1 -1
  24. package/lib/src/extensions/exceptions/error-conversion.js +3 -2
  25. package/lib/src/extensions/exceptions/error-conversion.js.map +1 -1
  26. package/lib/src/extensions/exceptions/exception-autocapture.js +4 -7
  27. package/lib/src/extensions/exceptions/exception-autocapture.js.map +1 -1
  28. package/lib/src/extensions/exceptions/stack-trace.js +3 -2
  29. package/lib/src/extensions/exceptions/stack-trace.js.map +1 -1
  30. package/lib/src/extensions/exceptions/type-checking.js +3 -2
  31. package/lib/src/extensions/exceptions/type-checking.js.map +1 -1
  32. package/lib/src/extensions/{sessionrecording-utils.js → replay/sessionrecording-utils.js} +3 -2
  33. package/lib/src/extensions/replay/sessionrecording-utils.js.map +1 -0
  34. package/lib/src/extensions/{sessionrecording.d.ts → replay/sessionrecording.d.ts} +27 -19
  35. package/lib/src/extensions/{sessionrecording.js → replay/sessionrecording.js} +217 -92
  36. package/lib/src/extensions/replay/sessionrecording.js.map +1 -0
  37. package/lib/src/extensions/{web-performance.d.ts → replay/web-performance.d.ts} +2 -2
  38. package/lib/src/extensions/{web-performance.js → replay/web-performance.js} +3 -3
  39. package/lib/src/extensions/replay/web-performance.js.map +1 -0
  40. package/lib/src/extensions/surveys.js +3 -2
  41. package/lib/src/extensions/surveys.js.map +1 -1
  42. package/lib/src/loader-recorder-v2.js +2 -3
  43. package/lib/src/loader-recorder-v2.js.map +1 -1
  44. package/lib/src/loader-recorder.js +2 -1
  45. package/lib/src/loader-recorder.js.map +1 -1
  46. package/lib/src/loader-surveys.js +2 -1
  47. package/lib/src/loader-surveys.js.map +1 -1
  48. package/lib/src/posthog-core.d.ts +2 -2
  49. package/lib/src/posthog-core.js +14 -14
  50. package/lib/src/posthog-core.js.map +1 -1
  51. package/lib/src/posthog-featureflags.js +4 -4
  52. package/lib/src/posthog-featureflags.js.map +1 -1
  53. package/lib/src/posthog-persistence.js +4 -4
  54. package/lib/src/posthog-persistence.js.map +1 -1
  55. package/lib/src/request-queue.js +2 -2
  56. package/lib/src/request-queue.js.map +1 -1
  57. package/lib/src/retry-queue.js +7 -3
  58. package/lib/src/retry-queue.js.map +1 -1
  59. package/lib/src/send-request.js +3 -3
  60. package/lib/src/send-request.js.map +1 -1
  61. package/lib/src/sessionid.d.ts +6 -4
  62. package/lib/src/sessionid.js +17 -15
  63. package/lib/src/sessionid.js.map +1 -1
  64. package/lib/src/storage.js +7 -7
  65. package/lib/src/storage.js.map +1 -1
  66. package/lib/src/types.d.ts +3 -0
  67. package/lib/src/types.js.map +1 -1
  68. package/lib/src/utils.d.ts +8 -10
  69. package/lib/src/utils.js +42 -31
  70. package/lib/src/utils.js.map +1 -1
  71. package/lib/src/uuidv7.js +6 -4
  72. package/lib/src/uuidv7.js.map +1 -1
  73. package/package.json +9 -4
  74. package/lib/src/extensions/sessionrecording-utils.js.map +0 -1
  75. package/lib/src/extensions/sessionrecording.js.map +0 -1
  76. package/lib/src/extensions/web-performance.js.map +0 -1
  77. /package/lib/src/extensions/{sessionrecording-utils.d.ts → replay/sessionrecording-utils.d.ts} +0 -0
@@ -17,12 +17,12 @@ var __read = (this && this.__read) || function (o, n) {
17
17
  import { SESSION_ID } from './constants';
18
18
  import { sessionStore } from './storage';
19
19
  import { uuidv7 } from './uuidv7';
20
- import { logger } from './utils';
21
- var MAX_SESSION_IDLE_TIMEOUT = 30 * 60; // 30 mins
22
- var MIN_SESSION_IDLE_TIMEOUT = 60; // 1 mins
20
+ import { _isArray, _isNumber, _isUndefined, logger, window } from './utils';
21
+ var MAX_SESSION_IDLE_TIMEOUT = 30 * 60; // 30 minutes
22
+ var MIN_SESSION_IDLE_TIMEOUT = 60; // 1 minute
23
23
  var SESSION_LENGTH_LIMIT = 24 * 3600 * 1000; // 24 hours
24
24
  var SessionIdManager = /** @class */ (function () {
25
- function SessionIdManager(config, persistence) {
25
+ function SessionIdManager(config, persistence, sessionIdGenerator, windowIdGenerator) {
26
26
  this._sessionIdChangedHandlers = [];
27
27
  this.config = config;
28
28
  this.persistence = persistence;
@@ -30,9 +30,11 @@ var SessionIdManager = /** @class */ (function () {
30
30
  this._sessionId = undefined;
31
31
  this._sessionStartTimestamp = null;
32
32
  this._sessionActivityTimestamp = null;
33
+ this._sessionIdGenerator = sessionIdGenerator || uuidv7;
34
+ this._windowIdGenerator = windowIdGenerator || uuidv7;
33
35
  var persistenceName = config['persistence_name'] || config['token'];
34
36
  var desiredTimeout = config['session_idle_timeout_seconds'] || MAX_SESSION_IDLE_TIMEOUT;
35
- if (typeof desiredTimeout !== 'number') {
37
+ if (!_isNumber(desiredTimeout)) {
36
38
  logger.warn('session_idle_timeout_seconds must be a number. Defaulting to 30 minutes.');
37
39
  desiredTimeout = MAX_SESSION_IDLE_TIMEOUT;
38
40
  }
@@ -68,7 +70,7 @@ var SessionIdManager = /** @class */ (function () {
68
70
  var _this = this;
69
71
  // KLUDGE: when running in tests the handlers array was always undefined
70
72
  // it's yucky but safe to set it here so that it's always definitely available
71
- if (this._sessionIdChangedHandlers === undefined) {
73
+ if (_isUndefined(this._sessionIdChangedHandlers)) {
72
74
  this._sessionIdChangedHandlers = [];
73
75
  }
74
76
  this._sessionIdChangedHandlers.push(callback);
@@ -125,7 +127,7 @@ var SessionIdManager = /** @class */ (function () {
125
127
  return [this._sessionActivityTimestamp, this._sessionId, this._sessionStartTimestamp];
126
128
  }
127
129
  var sessionId = this.persistence.props[SESSION_ID];
128
- if (Array.isArray(sessionId) && sessionId.length === 2) {
130
+ if (_isArray(sessionId) && sessionId.length === 2) {
129
131
  // Storage does not yet have a session start time. Add the last activity timestamp as the start time
130
132
  sessionId.push(sessionId[0]);
131
133
  }
@@ -152,8 +154,8 @@ var SessionIdManager = /** @class */ (function () {
152
154
  };
153
155
  /*
154
156
  * This function returns the current sessionId and windowId. It should be used to
155
- * access these values over directly calling `._sessionId` or `._windowId`. In addition
156
- * to returning the sessionId and windowId, this function also manages cycling the
157
+ * access these values over directly calling `._sessionId` or `._windowId`.
158
+ * In addition to returning the sessionId and windowId, this function also manages cycling the
157
159
  * sessionId and windowId when appropriate by doing the following:
158
160
  *
159
161
  * 1. If the sessionId or windowId is not set, it will generate a new one and store it.
@@ -175,16 +177,16 @@ var SessionIdManager = /** @class */ (function () {
175
177
  var windowId = this._getWindowId();
176
178
  var sessionPastMaximumLength = startTimestamp && startTimestamp > 0 && Math.abs(timestamp - startTimestamp) > SESSION_LENGTH_LIMIT;
177
179
  var valuesChanged = false;
178
- if (!sessionId ||
179
- (!readOnly && Math.abs(timestamp - lastTimestamp) > this._sessionTimeoutMs) ||
180
- sessionPastMaximumLength) {
181
- sessionId = uuidv7();
182
- windowId = uuidv7();
180
+ var noSessionId = !sessionId;
181
+ var activityTimeout = !readOnly && Math.abs(timestamp - lastTimestamp) > this._sessionTimeoutMs;
182
+ if (noSessionId || activityTimeout || sessionPastMaximumLength) {
183
+ sessionId = this._sessionIdGenerator();
184
+ windowId = this._windowIdGenerator();
183
185
  startTimestamp = timestamp;
184
186
  valuesChanged = true;
185
187
  }
186
188
  else if (!windowId) {
187
- windowId = uuidv7();
189
+ windowId = this._windowIdGenerator();
188
190
  valuesChanged = true;
189
191
  }
190
192
  var newTimestamp = lastTimestamp === 0 || !readOnly || sessionPastMaximumLength ? timestamp : lastTimestamp;
@@ -1 +1 @@
1
- {"version":3,"file":"sessionid.js","sourceRoot":"","sources":["../../src/sessionid.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AACA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AAExC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AACjC,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAEhC,IAAM,wBAAwB,GAAG,EAAE,GAAG,EAAE,CAAA,CAAC,UAAU;AACnD,IAAM,wBAAwB,GAAG,EAAE,CAAA,CAAC,SAAS;AAC7C,IAAM,oBAAoB,GAAG,EAAE,GAAG,IAAI,GAAG,IAAI,CAAA,CAAC,WAAW;AAEzD;IAYI,0BAAY,MAA8B,EAAE,WAA+B;QAFnE,8BAAyB,GAA+B,EAAE,CAAA;QAG9D,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,WAAW,GAAG,WAAW,CAAA;QAC9B,IAAI,CAAC,SAAS,GAAG,SAAS,CAAA;QAC1B,IAAI,CAAC,UAAU,GAAG,SAAS,CAAA;QAC3B,IAAI,CAAC,sBAAsB,GAAG,IAAI,CAAA;QAClC,IAAI,CAAC,yBAAyB,GAAG,IAAI,CAAA;QAErC,IAAM,eAAe,GAAG,MAAM,CAAC,kBAAkB,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,CAAA;QACrE,IAAI,cAAc,GAAG,MAAM,CAAC,8BAA8B,CAAC,IAAI,wBAAwB,CAAA;QAEvF,IAAI,OAAO,cAAc,KAAK,QAAQ,EAAE;YACpC,MAAM,CAAC,IAA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,CAAC,SAAS,CAAC,CAAC,CAAC,aAAa,CAAA;QAC7G,IAAM,qBAAqB,GAAG,cAAc,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,cAAc,CAAA;QAE1F,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAA;QAC3B,IAAI,CAAC,aAAa,CAAC,SAAS,EAAE,YAAY,EAAE,qBAAqB,CAAC,CAAA;QAElE,IAAI,aAAa,EAAE;YACf,IAAI,CAAC,yBAAyB,CAAC,OAAO,CAAC,UAAC,OAAO,IAAK,OAAA,OAAO,CAAC,SAAS,EAAE,QAAQ,CAAC,EAA5B,CAA4B,CAAC,CAAA;SACpF;QAED,OAAO;YACH,SAAS,WAAA;YACT,QAAQ,UAAA;YACR,qBAAqB,uBAAA;SACxB,CAAA;IACL,CAAC;IACL,uBAAC;AAAD,CAAC,AAtND,IAsNC","sourcesContent":["import { PostHogPersistence } from './posthog-persistence'\nimport { SESSION_ID } from './constants'\nimport { sessionStore } from './storage'\nimport { PostHogConfig, SessionIdChangedCallback } from './types'\nimport { uuidv7 } from './uuidv7'\nimport { logger } from './utils'\n\nconst MAX_SESSION_IDLE_TIMEOUT = 30 * 60 // 30 mins\nconst MIN_SESSION_IDLE_TIMEOUT = 60 // 1 mins\nconst SESSION_LENGTH_LIMIT = 24 * 3600 * 1000 // 24 hours\n\nexport class SessionIdManager {\n private config: Partial<PostHogConfig>\n private persistence: PostHogPersistence\n private _windowId: string | null | undefined\n private _sessionId: string | null | undefined\n private _window_id_storage_key: string\n private _primary_window_exists_storage_key: string\n private _sessionStartTimestamp: number | null\n private _sessionActivityTimestamp: number | null\n private _sessionTimeoutMs: number\n private _sessionIdChangedHandlers: SessionIdChangedCallback[] = []\n\n constructor(config: Partial<PostHogConfig>, persistence: PostHogPersistence) {\n this.config = config\n this.persistence = persistence\n this._windowId = undefined\n this._sessionId = undefined\n this._sessionStartTimestamp = null\n this._sessionActivityTimestamp = null\n\n const persistenceName = config['persistence_name'] || config['token']\n let desiredTimeout = config['session_idle_timeout_seconds'] || MAX_SESSION_IDLE_TIMEOUT\n\n if (typeof desiredTimeout !== 'number') {\n logger.warn('session_idle_timeout_seconds must be a number. Defaulting to 30 minutes.')\n desiredTimeout = MAX_SESSION_IDLE_TIMEOUT\n } else if (desiredTimeout > MAX_SESSION_IDLE_TIMEOUT) {\n logger.warn('session_idle_timeout_seconds cannot be greater than 30 minutes. Using 30 minutes instead.')\n } else if (desiredTimeout < MIN_SESSION_IDLE_TIMEOUT) {\n logger.warn('session_idle_timeout_seconds cannot be less than 60 seconds. Using 60 seconds instead.')\n }\n\n this._sessionTimeoutMs =\n Math.min(Math.max(desiredTimeout, MIN_SESSION_IDLE_TIMEOUT), MAX_SESSION_IDLE_TIMEOUT) * 1000\n this._window_id_storage_key = 'ph_' + persistenceName + '_window_id'\n this._primary_window_exists_storage_key = 'ph_' + persistenceName + '_primary_window_exists'\n\n // primary_window_exists is set when the DOM has been loaded and is cleared on unload\n // if it exists here it means there was no unload which suggests this window is opened as a tab duplication, window.open, etc.\n if (this._canUseSessionStorage()) {\n const lastWindowId = sessionStore.parse(this._window_id_storage_key)\n\n const primaryWindowExists = sessionStore.parse(this._primary_window_exists_storage_key)\n if (lastWindowId && !primaryWindowExists) {\n // Persist window from previous storage state\n this._windowId = lastWindowId\n } else {\n // Wipe any reference to previous window id\n sessionStore.remove(this._window_id_storage_key)\n }\n // Flag this session as having a primary window\n sessionStore.set(this._primary_window_exists_storage_key, true)\n }\n\n this._listenToReloadWindow()\n }\n\n onSessionId(callback: SessionIdChangedCallback): () => void {\n // KLUDGE: when running in tests the handlers array was always undefined\n // it's yucky but safe to set it here so that it's always definitely available\n if (this._sessionIdChangedHandlers === undefined) {\n this._sessionIdChangedHandlers = []\n }\n\n this._sessionIdChangedHandlers.push(callback)\n if (this._sessionId) {\n callback(this._sessionId, this._windowId)\n }\n return () => {\n this._sessionIdChangedHandlers = this._sessionIdChangedHandlers.filter((h) => h !== callback)\n }\n }\n\n private _canUseSessionStorage(): boolean {\n // We only want to use sessionStorage if persistence is enabled and not memory storage\n return this.config.persistence !== 'memory' && !this.persistence.disabled && sessionStore.is_supported()\n }\n\n // Note: this tries to store the windowId in sessionStorage. SessionStorage is unique to the current window/tab,\n // and persists page loads/reloads. So it's uniquely suited for storing the windowId. This function also respects\n // when persistence is disabled (by user config) and when sessionStorage is not supported (it *should* be supported on all browsers),\n // and in that case, it falls back to memory (which sadly, won't persist page loads)\n private _setWindowId(windowId: string): void {\n if (windowId !== this._windowId) {\n this._windowId = windowId\n if (this._canUseSessionStorage()) {\n sessionStore.set(this._window_id_storage_key, windowId)\n }\n }\n }\n\n private _getWindowId(): string | null {\n if (this._windowId) {\n return this._windowId\n }\n if (this._canUseSessionStorage()) {\n return sessionStore.parse(this._window_id_storage_key)\n }\n // New window id will be generated\n return null\n }\n\n // Note: 'this.persistence.register' can be disabled in the config.\n // In that case, this works by storing sessionId and the timestamp in memory.\n private _setSessionId(\n sessionId: string | null,\n sessionActivityTimestamp: number | null,\n sessionStartTimestamp: number | null\n ): void {\n if (\n sessionId !== this._sessionId ||\n sessionActivityTimestamp !== this._sessionActivityTimestamp ||\n sessionStartTimestamp !== this._sessionStartTimestamp\n ) {\n this._sessionStartTimestamp = sessionStartTimestamp\n this._sessionActivityTimestamp = sessionActivityTimestamp\n this._sessionId = sessionId\n this.persistence.register({\n [SESSION_ID]: [sessionActivityTimestamp, sessionId, sessionStartTimestamp],\n })\n }\n }\n\n private _getSessionId(): [number, string, number] {\n if (this._sessionId && this._sessionActivityTimestamp && this._sessionStartTimestamp) {\n return [this._sessionActivityTimestamp, this._sessionId, this._sessionStartTimestamp]\n }\n const sessionId = this.persistence.props[SESSION_ID]\n\n if (Array.isArray(sessionId) && sessionId.length === 2) {\n // Storage does not yet have a session start time. Add the last activity timestamp as the start time\n sessionId.push(sessionId[0])\n }\n\n return sessionId || [0, null, 0]\n }\n\n // Resets the session id by setting it to null. On the subsequent call to checkAndGetSessionAndWindowId,\n // new ids will be generated.\n resetSessionId(): void {\n this._setSessionId(null, null, null)\n }\n\n /*\n * Listens to window unloads and removes the primaryWindowExists key from sessionStorage.\n * Reloaded or fresh tabs created after a DOM unloads (reloading the same tab) WILL NOT have this primaryWindowExists flag in session storage.\n * Cloned sessions (new tab, tab duplication, window.open(), ...) WILL have this primaryWindowExists flag in their copied session storage.\n * We conditionally check the primaryWindowExists value in the constructor to decide if the window id in the last session storage should be carried over.\n */\n private _listenToReloadWindow(): void {\n window.addEventListener('beforeunload', () => {\n if (this._canUseSessionStorage()) {\n sessionStore.remove(this._primary_window_exists_storage_key)\n }\n })\n }\n\n /*\n * This function returns the current sessionId and windowId. It should be used to\n * access these values over directly calling `._sessionId` or `._windowId`. In addition\n * to returning the sessionId and windowId, this function also manages cycling the\n * sessionId and windowId when appropriate by doing the following:\n *\n * 1. If the sessionId or windowId is not set, it will generate a new one and store it.\n * 2. If the readOnly param is set to false, it will:\n * a. Check if it has been > SESSION_CHANGE_THRESHOLD since the last call with this flag set.\n * If so, it will generate a new sessionId and store it.\n * b. Update the timestamp stored with the sessionId to ensure the current session is extended\n * for the appropriate amount of time.\n *\n * @param {boolean} readOnly (optional) Defaults to False. Should be set to True when the call to the function should not extend or cycle the session (e.g. being called for non-user generated events)\n * @param {Number} timestamp (optional) Defaults to the current time. The timestamp to be stored with the sessionId (used when determining if a new sessionId should be generated)\n */\n checkAndGetSessionAndWindowId(readOnly = false, _timestamp: number | null = null) {\n const timestamp = _timestamp || new Date().getTime()\n\n // eslint-disable-next-line prefer-const\n let [lastTimestamp, sessionId, startTimestamp] = this._getSessionId()\n let windowId = this._getWindowId()\n\n const sessionPastMaximumLength =\n startTimestamp && startTimestamp > 0 && Math.abs(timestamp - startTimestamp) > SESSION_LENGTH_LIMIT\n\n let valuesChanged = false\n if (\n !sessionId ||\n (!readOnly && Math.abs(timestamp - lastTimestamp) > this._sessionTimeoutMs) ||\n sessionPastMaximumLength\n ) {\n sessionId = uuidv7()\n windowId = uuidv7()\n startTimestamp = timestamp\n valuesChanged = true\n } else if (!windowId) {\n windowId = uuidv7()\n valuesChanged = true\n }\n\n const newTimestamp = lastTimestamp === 0 || !readOnly || sessionPastMaximumLength ? timestamp : lastTimestamp\n const sessionStartTimestamp = startTimestamp === 0 ? new Date().getTime() : startTimestamp\n\n this._setWindowId(windowId)\n this._setSessionId(sessionId, newTimestamp, sessionStartTimestamp)\n\n if (valuesChanged) {\n this._sessionIdChangedHandlers.forEach((handler) => handler(sessionId, windowId))\n }\n\n return {\n sessionId,\n windowId,\n sessionStartTimestamp,\n }\n }\n}\n"]}
1
+ {"version":3,"file":"sessionid.js","sourceRoot":"","sources":["../../src/sessionid.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AACA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AAExC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AACjC,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAE3E,IAAM,wBAAwB,GAAG,EAAE,GAAG,EAAE,CAAA,CAAC,aAAa;AACtD,IAAM,wBAAwB,GAAG,EAAE,CAAA,CAAC,WAAW;AAC/C,IAAM,oBAAoB,GAAG,EAAE,GAAG,IAAI,GAAG,IAAI,CAAA,CAAC,WAAW;AAEzD;IAeI,0BACI,MAA8B,EAC9B,WAA+B,EAC/B,kBAAiC,EACjC,iBAAgC;QAN5B,8BAAyB,GAA+B,EAAE,CAAA;QAQ9D,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,WAAW,GAAG,WAAW,CAAA;QAC9B,IAAI,CAAC,SAAS,GAAG,SAAS,CAAA;QAC1B,IAAI,CAAC,UAAU,GAAG,SAAS,CAAA;QAC3B,IAAI,CAAC,sBAAsB,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,IAAI,IAAI,CAAC,GAAG,CAAC,SAAS,GAAG,aAAa,CAAC,GAAG,IAAI,CAAC,iBAAiB,CAAA;QACjG,IAAI,WAAW,IAAI,eAAe,IAAI,wBAAwB,EAAE;YAC5D,SAAS,GAAG,IAAI,CAAC,mBAAmB,EAAE,CAAA;YACtC,QAAQ,GAAG,IAAI,CAAC,kBAAkB,EAAE,CAAA;YACpC,cAAc,GAAG,SAAS,CAAA;YAC1B,aAAa,GAAG,IAAI,CAAA;SACvB;aAAM,IAAI,CAAC,QAAQ,EAAE;YAClB,QAAQ,GAAG,IAAI,CAAC,kBAAkB,EAAE,CAAA;YACpC,aAAa,GAAG,IAAI,CAAA;SACvB;QAED,IAAM,YAAY,GAAG,aAAa,KAAK,CAAC,IAAI,CAAC,QAAQ,IAAI,wBAAwB,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,aAAa,CAAA;QAC7G,IAAM,qBAAqB,GAAG,cAAc,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,cAAc,CAAA;QAE1F,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAA;QAC3B,IAAI,CAAC,aAAa,CAAC,SAAS,EAAE,YAAY,EAAE,qBAAqB,CAAC,CAAA;QAElE,IAAI,aAAa,EAAE;YACf,IAAI,CAAC,yBAAyB,CAAC,OAAO,CAAC,UAAC,OAAO,IAAK,OAAA,OAAO,CAAC,SAAS,EAAE,QAAQ,CAAC,EAA5B,CAA4B,CAAC,CAAA;SACpF;QAED,OAAO;YACH,SAAS,WAAA;YACT,QAAQ,UAAA;YACR,qBAAqB,uBAAA;SACxB,CAAA;IACL,CAAC;IACL,uBAAC;AAAD,CAAC,AA/ND,IA+NC","sourcesContent":["import { PostHogPersistence } from './posthog-persistence'\nimport { SESSION_ID } from './constants'\nimport { sessionStore } from './storage'\nimport { PostHogConfig, SessionIdChangedCallback } from './types'\nimport { uuidv7 } from './uuidv7'\nimport { _isArray, _isNumber, _isUndefined, logger, window } from './utils'\n\nconst MAX_SESSION_IDLE_TIMEOUT = 30 * 60 // 30 minutes\nconst MIN_SESSION_IDLE_TIMEOUT = 60 // 1 minute\nconst SESSION_LENGTH_LIMIT = 24 * 3600 * 1000 // 24 hours\n\nexport class SessionIdManager {\n private readonly _sessionIdGenerator: () => string\n private readonly _windowIdGenerator: () => string\n private config: Partial<PostHogConfig>\n private persistence: PostHogPersistence\n private _windowId: string | null | undefined\n private _sessionId: string | null | undefined\n private readonly _window_id_storage_key: string\n private readonly _primary_window_exists_storage_key: string\n private _sessionStartTimestamp: number | null\n\n private _sessionActivityTimestamp: number | null\n private readonly _sessionTimeoutMs: number\n private _sessionIdChangedHandlers: SessionIdChangedCallback[] = []\n\n constructor(\n config: Partial<PostHogConfig>,\n persistence: PostHogPersistence,\n sessionIdGenerator?: () => string,\n windowIdGenerator?: () => string\n ) {\n this.config = config\n this.persistence = persistence\n this._windowId = undefined\n this._sessionId = undefined\n this._sessionStartTimestamp = null\n this._sessionActivityTimestamp = null\n this._sessionIdGenerator = sessionIdGenerator || uuidv7\n this._windowIdGenerator = windowIdGenerator || uuidv7\n\n const persistenceName = config['persistence_name'] || config['token']\n let desiredTimeout = config['session_idle_timeout_seconds'] || MAX_SESSION_IDLE_TIMEOUT\n\n if (!_isNumber(desiredTimeout)) {\n logger.warn('session_idle_timeout_seconds must be a number. Defaulting to 30 minutes.')\n desiredTimeout = MAX_SESSION_IDLE_TIMEOUT\n } else if (desiredTimeout > MAX_SESSION_IDLE_TIMEOUT) {\n logger.warn('session_idle_timeout_seconds cannot be greater than 30 minutes. Using 30 minutes instead.')\n } else if (desiredTimeout < MIN_SESSION_IDLE_TIMEOUT) {\n logger.warn('session_idle_timeout_seconds cannot be less than 60 seconds. Using 60 seconds instead.')\n }\n\n this._sessionTimeoutMs =\n Math.min(Math.max(desiredTimeout, MIN_SESSION_IDLE_TIMEOUT), MAX_SESSION_IDLE_TIMEOUT) * 1000\n this._window_id_storage_key = 'ph_' + persistenceName + '_window_id'\n this._primary_window_exists_storage_key = 'ph_' + persistenceName + '_primary_window_exists'\n\n // primary_window_exists is set when the DOM has been loaded and is cleared on unload\n // if it exists here it means there was no unload which suggests this window is opened as a tab duplication, window.open, etc.\n if (this._canUseSessionStorage()) {\n const lastWindowId = sessionStore.parse(this._window_id_storage_key)\n\n const primaryWindowExists = sessionStore.parse(this._primary_window_exists_storage_key)\n if (lastWindowId && !primaryWindowExists) {\n // Persist window from previous storage state\n this._windowId = lastWindowId\n } else {\n // Wipe any reference to previous window id\n sessionStore.remove(this._window_id_storage_key)\n }\n // Flag this session as having a primary window\n sessionStore.set(this._primary_window_exists_storage_key, true)\n }\n\n this._listenToReloadWindow()\n }\n\n onSessionId(callback: SessionIdChangedCallback): () => void {\n // KLUDGE: when running in tests the handlers array was always undefined\n // it's yucky but safe to set it here so that it's always definitely available\n if (_isUndefined(this._sessionIdChangedHandlers)) {\n this._sessionIdChangedHandlers = []\n }\n\n this._sessionIdChangedHandlers.push(callback)\n if (this._sessionId) {\n callback(this._sessionId, this._windowId)\n }\n return () => {\n this._sessionIdChangedHandlers = this._sessionIdChangedHandlers.filter((h) => h !== callback)\n }\n }\n\n private _canUseSessionStorage(): boolean {\n // We only want to use sessionStorage if persistence is enabled and not memory storage\n return this.config.persistence !== 'memory' && !this.persistence.disabled && sessionStore.is_supported()\n }\n\n // Note: this tries to store the windowId in sessionStorage. SessionStorage is unique to the current window/tab,\n // and persists page loads/reloads. So it's uniquely suited for storing the windowId. This function also respects\n // when persistence is disabled (by user config) and when sessionStorage is not supported (it *should* be supported on all browsers),\n // and in that case, it falls back to memory (which sadly, won't persist page loads)\n private _setWindowId(windowId: string): void {\n if (windowId !== this._windowId) {\n this._windowId = windowId\n if (this._canUseSessionStorage()) {\n sessionStore.set(this._window_id_storage_key, windowId)\n }\n }\n }\n\n private _getWindowId(): string | null {\n if (this._windowId) {\n return this._windowId\n }\n if (this._canUseSessionStorage()) {\n return sessionStore.parse(this._window_id_storage_key)\n }\n // New window id will be generated\n return null\n }\n\n // Note: 'this.persistence.register' can be disabled in the config.\n // In that case, this works by storing sessionId and the timestamp in memory.\n private _setSessionId(\n sessionId: string | null,\n sessionActivityTimestamp: number | null,\n sessionStartTimestamp: number | null\n ): void {\n if (\n sessionId !== this._sessionId ||\n sessionActivityTimestamp !== this._sessionActivityTimestamp ||\n sessionStartTimestamp !== this._sessionStartTimestamp\n ) {\n this._sessionStartTimestamp = sessionStartTimestamp\n this._sessionActivityTimestamp = sessionActivityTimestamp\n this._sessionId = sessionId\n\n this.persistence.register({\n [SESSION_ID]: [sessionActivityTimestamp, sessionId, sessionStartTimestamp],\n })\n }\n }\n\n private _getSessionId(): [number, string, number] {\n if (this._sessionId && this._sessionActivityTimestamp && this._sessionStartTimestamp) {\n return [this._sessionActivityTimestamp, this._sessionId, this._sessionStartTimestamp]\n }\n const sessionId = this.persistence.props[SESSION_ID]\n\n if (_isArray(sessionId) && sessionId.length === 2) {\n // Storage does not yet have a session start time. Add the last activity timestamp as the start time\n sessionId.push(sessionId[0])\n }\n\n return sessionId || [0, null, 0]\n }\n\n // Resets the session id by setting it to null. On the subsequent call to checkAndGetSessionAndWindowId,\n // new ids will be generated.\n resetSessionId(): void {\n this._setSessionId(null, null, null)\n }\n\n /*\n * Listens to window unloads and removes the primaryWindowExists key from sessionStorage.\n * Reloaded or fresh tabs created after a DOM unloads (reloading the same tab) WILL NOT have this primaryWindowExists flag in session storage.\n * Cloned sessions (new tab, tab duplication, window.open(), ...) WILL have this primaryWindowExists flag in their copied session storage.\n * We conditionally check the primaryWindowExists value in the constructor to decide if the window id in the last session storage should be carried over.\n */\n private _listenToReloadWindow(): void {\n window.addEventListener('beforeunload', () => {\n if (this._canUseSessionStorage()) {\n sessionStore.remove(this._primary_window_exists_storage_key)\n }\n })\n }\n\n /*\n * This function returns the current sessionId and windowId. It should be used to\n * access these values over directly calling `._sessionId` or `._windowId`.\n * In addition to returning the sessionId and windowId, this function also manages cycling the\n * sessionId and windowId when appropriate by doing the following:\n *\n * 1. If the sessionId or windowId is not set, it will generate a new one and store it.\n * 2. If the readOnly param is set to false, it will:\n * a. Check if it has been > SESSION_CHANGE_THRESHOLD since the last call with this flag set.\n * If so, it will generate a new sessionId and store it.\n * b. Update the timestamp stored with the sessionId to ensure the current session is extended\n * for the appropriate amount of time.\n *\n * @param {boolean} readOnly (optional) Defaults to False. Should be set to True when the call to the function should not extend or cycle the session (e.g. being called for non-user generated events)\n * @param {Number} timestamp (optional) Defaults to the current time. The timestamp to be stored with the sessionId (used when determining if a new sessionId should be generated)\n */\n checkAndGetSessionAndWindowId(readOnly = false, _timestamp: number | null = null) {\n const timestamp = _timestamp || new Date().getTime()\n\n // eslint-disable-next-line prefer-const\n let [lastTimestamp, sessionId, startTimestamp] = this._getSessionId()\n let windowId = this._getWindowId()\n\n const sessionPastMaximumLength =\n startTimestamp && startTimestamp > 0 && Math.abs(timestamp - startTimestamp) > SESSION_LENGTH_LIMIT\n\n let valuesChanged = false\n const noSessionId = !sessionId\n const activityTimeout = !readOnly && Math.abs(timestamp - lastTimestamp) > this._sessionTimeoutMs\n if (noSessionId || activityTimeout || sessionPastMaximumLength) {\n sessionId = this._sessionIdGenerator()\n windowId = this._windowIdGenerator()\n startTimestamp = timestamp\n valuesChanged = true\n } else if (!windowId) {\n windowId = this._windowIdGenerator()\n valuesChanged = true\n }\n\n const newTimestamp = lastTimestamp === 0 || !readOnly || sessionPastMaximumLength ? timestamp : lastTimestamp\n const sessionStartTimestamp = startTimestamp === 0 ? new Date().getTime() : startTimestamp\n\n this._setWindowId(windowId)\n this._setSessionId(sessionId, newTimestamp, sessionStartTimestamp)\n\n if (valuesChanged) {\n this._sessionIdChangedHandlers.forEach((handler) => handler(sessionId, windowId))\n }\n\n return {\n sessionId,\n windowId,\n sessionStartTimestamp,\n }\n }\n}\n"]}
@@ -9,8 +9,8 @@ var __assign = (this && this.__assign) || function () {
9
9
  };
10
10
  return __assign.apply(this, arguments);
11
11
  };
12
- import { _extend, logger } from './utils';
13
- import { DISTINCT_ID, SESSION_ID } from './constants';
12
+ import { _extend, _isNull, _isUndefined, logger } from './utils';
13
+ import { DISTINCT_ID, SESSION_ID, SESSION_RECORDING_IS_SAMPLED } from './constants';
14
14
  var DOMAIN_MATCH_REGEX = /[a-z0-9][a-z0-9-]+\.[a-z]{2,}$/i;
15
15
  // Methods partially borrowed from quirksmode.org/js/cookies.html
16
16
  export var cookieStore = {
@@ -87,11 +87,11 @@ export var cookieStore = {
87
87
  var _localStorage_supported = null;
88
88
  export var localStore = {
89
89
  is_supported: function () {
90
- if (_localStorage_supported !== null) {
90
+ if (!_isNull(_localStorage_supported)) {
91
91
  return _localStorage_supported;
92
92
  }
93
93
  var supported = true;
94
- if (typeof window !== 'undefined') {
94
+ if (!_isUndefined(window)) {
95
95
  try {
96
96
  var key = '__mplssupport__', val = 'xyz';
97
97
  localStore.set(key, val);
@@ -154,7 +154,7 @@ export var localStore = {
154
154
  // Use localstorage for most data but still use cookie for COOKIE_PERSISTED_PROPERTIES
155
155
  // This solves issues with cookies having too much data in them causing headers too large
156
156
  // Also makes sure we don't have to send a ton of data to the server
157
- var COOKIE_PERSISTED_PROPERTIES = [DISTINCT_ID, SESSION_ID];
157
+ var COOKIE_PERSISTED_PROPERTIES = [DISTINCT_ID, SESSION_ID, SESSION_RECORDING_IS_SAMPLED];
158
158
  export var localPlusCookieStore = __assign(__assign({}, localStore), { parse: function (name) {
159
159
  try {
160
160
  var extend = {};
@@ -225,11 +225,11 @@ export var resetSessionStorageSupported = function () {
225
225
  // Storage that only lasts the length of a tab/window. Survives page refreshes
226
226
  export var sessionStore = {
227
227
  is_supported: function () {
228
- if (sessionStorageSupported !== null) {
228
+ if (!_isNull(sessionStorageSupported)) {
229
229
  return sessionStorageSupported;
230
230
  }
231
231
  sessionStorageSupported = true;
232
- if (typeof window !== 'undefined') {
232
+ if (!_isUndefined(window)) {
233
233
  try {
234
234
  var key = '__support__', val = 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AC,KAAK,CAAC,GAAG,CAAC,CAAA;SAC1B;IACL,CAAC;CACJ,CAAA","sourcesContent":["import { _extend, logger } from './utils'\nimport { PersistentStore, Properties } from './types'\nimport { DISTINCT_ID, SESSION_ID } from './constants'\n\nconst DOMAIN_MATCH_REGEX = /[a-z0-9][a-z0-9-]+\\.[a-z]{2,}$/i\n\n// Methods partially borrowed from quirksmode.org/js/cookies.html\nexport const cookieStore: PersistentStore = {\n is_supported: () => true,\n\n error: function (msg) {\n logger.error('cookieStore error: ' + msg)\n },\n\n get: function (name) {\n try {\n const nameEQ = name + '='\n const ca = document.cookie.split(';').filter((x) => x.length)\n for (let i = 0; i < ca.length; i++) {\n let c = ca[i]\n while (c.charAt(0) == ' ') {\n c = c.substring(1, c.length)\n }\n if (c.indexOf(nameEQ) === 0) {\n return decodeURIComponent(c.substring(nameEQ.length, c.length))\n }\n }\n } catch (err) {}\n return null\n },\n\n parse: function (name) {\n let cookie\n try {\n cookie = JSON.parse(cookieStore.get(name)) || {}\n } catch (err) {\n // noop\n }\n return cookie\n },\n\n set: function (name, value, days, cross_subdomain, is_secure) {\n try {\n let cdomain = '',\n expires = '',\n secure = ''\n\n if (cross_subdomain) {\n // NOTE: Could we use this for cross domain tracking?\n const matches = document.location.hostname.match(DOMAIN_MATCH_REGEX),\n domain = matches ? matches[0] : ''\n\n cdomain = domain ? '; domain=.' + domain : ''\n }\n\n if (days) {\n const date = new Date()\n date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000)\n expires = '; expires=' + date.toUTCString()\n }\n\n if (is_secure) {\n secure = '; secure'\n }\n\n const new_cookie_val =\n name +\n '=' +\n encodeURIComponent(JSON.stringify(value)) +\n expires +\n '; SameSite=Lax; path=/' +\n cdomain +\n secure\n document.cookie = new_cookie_val\n return new_cookie_val\n } catch (err) {\n return\n }\n },\n\n remove: function (name, cross_subdomain) {\n try {\n cookieStore.set(name, '', -1, cross_subdomain)\n } catch (err) {\n return\n }\n },\n}\n\nlet _localStorage_supported: boolean | null = null\n\nexport const localStore: PersistentStore = {\n is_supported: function () {\n if (_localStorage_supported !== null) {\n return _localStorage_supported\n }\n\n let supported = true\n if (typeof window !== 'undefined') {\n try {\n const key = '__mplssupport__',\n val = 'xyz'\n localStore.set(key, val)\n if (localStore.get(key) !== '\"xyz\"') {\n supported = false\n }\n localStore.remove(key)\n } catch (err) {\n supported = false\n }\n } else {\n supported = false\n }\n if (!supported) {\n logger.error('localStorage unsupported; falling back to cookie store')\n }\n\n _localStorage_supported = supported\n return supported\n },\n\n error: function (msg) {\n logger.error('localStorage error: ' + msg)\n },\n\n get: function (name) {\n try {\n return window.localStorage.getItem(name)\n } catch (err) {\n localStore.error(err)\n }\n return null\n },\n\n parse: function (name) {\n try {\n return JSON.parse(localStore.get(name)) || {}\n } catch (err) {\n // noop\n }\n return null\n },\n\n set: function (name, value) {\n try {\n window.localStorage.setItem(name, JSON.stringify(value))\n } catch (err) {\n localStore.error(err)\n }\n },\n\n remove: function (name) {\n try {\n window.localStorage.removeItem(name)\n } catch (err) {\n localStore.error(err)\n }\n },\n}\n\n// Use localstorage for most data but still use cookie for COOKIE_PERSISTED_PROPERTIES\n// This solves issues with cookies having too much data in them causing headers too large\n// Also makes sure we don't have to send a ton of data to the server\nconst COOKIE_PERSISTED_PROPERTIES = [DISTINCT_ID, SESSION_ID]\n\nexport const localPlusCookieStore: PersistentStore = {\n ...localStore,\n parse: function (name) {\n try {\n let extend: Properties = {}\n try {\n // See if there's a cookie stored with data.\n extend = cookieStore.parse(name) || {}\n } catch (err) {}\n const value = _extend(extend, JSON.parse(localStore.get(name) || '{}'))\n localStore.set(name, value)\n return value\n } catch (err) {\n // noop\n }\n return null\n },\n\n set: function (name, value, days, cross_subdomain, is_secure) {\n try {\n localStore.set(name, value)\n const cookiePersistedProperties: Record<string, any> = {}\n COOKIE_PERSISTED_PROPERTIES.forEach((key) => {\n if (value[key]) {\n cookiePersistedProperties[key] = value[key]\n }\n })\n\n if (Object.keys(cookiePersistedProperties).length) {\n cookieStore.set(name, cookiePersistedProperties, days, cross_subdomain, is_secure)\n }\n } catch (err) {\n localStore.error(err)\n }\n },\n\n remove: function (name, cross_subdomain) {\n try {\n window.localStorage.removeItem(name)\n cookieStore.remove(name, cross_subdomain)\n } catch (err) {\n localStore.error(err)\n }\n },\n}\n\nconst memoryStorage: Properties = {}\n\n// Storage that only lasts the length of the pageview if we don't want to use cookies\nexport const memoryStore: PersistentStore = {\n is_supported: function () {\n return true\n },\n\n error: function (msg) {\n logger.error('memoryStorage error: ' + msg)\n },\n\n get: function (name) {\n return memoryStorage[name] || null\n },\n\n parse: function (name) {\n return memoryStorage[name] || null\n },\n\n set: function (name, value) {\n memoryStorage[name] = value\n },\n\n remove: function (name) {\n delete memoryStorage[name]\n },\n}\n\nlet sessionStorageSupported: boolean | null = null\nexport const resetSessionStorageSupported = () => {\n sessionStorageSupported = null\n}\n// Storage that only lasts the length of a tab/window. Survives page refreshes\nexport const sessionStore: PersistentStore = {\n is_supported: function () {\n if (sessionStorageSupported !== null) {\n return sessionStorageSupported\n }\n sessionStorageSupported = true\n if (typeof window !== 'undefined') {\n try {\n const key = '__support__',\n val = 'xyz'\n sessionStore.set(key, val)\n if (sessionStore.get(key) !== '\"xyz\"') {\n sessionStorageSupported = false\n }\n sessionStore.remove(key)\n } catch (err) {\n sessionStorageSupported = false\n }\n } else {\n sessionStorageSupported = false\n }\n return sessionStorageSupported\n },\n\n error: function (msg) {\n logger.error('sessionStorage error: ', msg)\n },\n\n get: function (name) {\n try {\n return window.sessionStorage.getItem(name)\n } catch (err) {\n sessionStore.error(err)\n }\n return null\n },\n\n parse: function (name) {\n try {\n return JSON.parse(sessionStore.get(name)) || null\n } catch (err) {\n // noop\n }\n return null\n },\n\n set: function (name, value) {\n try {\n window.sessionStorage.setItem(name, JSON.stringify(value))\n } catch (err) {\n sessionStore.error(err)\n }\n },\n\n remove: function (name) {\n try {\n window.sessionStorage.removeItem(name)\n } catch (err) {\n sessionStore.error(err)\n }\n },\n}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AC,CAAA;SACzC;QAAC,OAAO,GAAG,EAAE;YACV,YAAY,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;SAC1B;IACL,CAAC;CACJ,CAAA","sourcesContent":["import { _extend, _isNull, _isUndefined, logger } from './utils'\nimport { PersistentStore, Properties } from './types'\nimport { DISTINCT_ID, SESSION_ID, SESSION_RECORDING_IS_SAMPLED } from './constants'\n\nconst DOMAIN_MATCH_REGEX = /[a-z0-9][a-z0-9-]+\\.[a-z]{2,}$/i\n\n// Methods partially borrowed from quirksmode.org/js/cookies.html\nexport const cookieStore: PersistentStore = {\n is_supported: () => true,\n\n error: function (msg) {\n logger.error('cookieStore error: ' + msg)\n },\n\n get: function (name) {\n try {\n const nameEQ = name + '='\n const ca = document.cookie.split(';').filter((x) => x.length)\n for (let i = 0; i < ca.length; i++) {\n let c = ca[i]\n while (c.charAt(0) == ' ') {\n c = c.substring(1, c.length)\n }\n if (c.indexOf(nameEQ) === 0) {\n return decodeURIComponent(c.substring(nameEQ.length, c.length))\n }\n }\n } catch (err) {}\n return null\n },\n\n parse: function (name) {\n let cookie\n try {\n cookie = JSON.parse(cookieStore.get(name)) || {}\n } catch (err) {\n // noop\n }\n return cookie\n },\n\n set: function (name, value, days, cross_subdomain, is_secure) {\n try {\n let cdomain = '',\n expires = '',\n secure = ''\n\n if (cross_subdomain) {\n // NOTE: Could we use this for cross domain tracking?\n const matches = document.location.hostname.match(DOMAIN_MATCH_REGEX),\n domain = matches ? matches[0] : ''\n\n cdomain = domain ? '; domain=.' + domain : ''\n }\n\n if (days) {\n const date = new Date()\n date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000)\n expires = '; expires=' + date.toUTCString()\n }\n\n if (is_secure) {\n secure = '; secure'\n }\n\n const new_cookie_val =\n name +\n '=' +\n encodeURIComponent(JSON.stringify(value)) +\n expires +\n '; SameSite=Lax; path=/' +\n cdomain +\n secure\n document.cookie = new_cookie_val\n return new_cookie_val\n } catch (err) {\n return\n }\n },\n\n remove: function (name, cross_subdomain) {\n try {\n cookieStore.set(name, '', -1, cross_subdomain)\n } catch (err) {\n return\n }\n },\n}\n\nlet _localStorage_supported: boolean | null = null\n\nexport const localStore: PersistentStore = {\n is_supported: function () {\n if (!_isNull(_localStorage_supported)) {\n return _localStorage_supported\n }\n\n let supported = true\n if (!_isUndefined(window)) {\n try {\n const key = '__mplssupport__',\n val = 'xyz'\n localStore.set(key, val)\n if (localStore.get(key) !== '\"xyz\"') {\n supported = false\n }\n localStore.remove(key)\n } catch (err) {\n supported = false\n }\n } else {\n supported = false\n }\n if (!supported) {\n logger.error('localStorage unsupported; falling back to cookie store')\n }\n\n _localStorage_supported = supported\n return supported\n },\n\n error: function (msg) {\n logger.error('localStorage error: ' + msg)\n },\n\n get: function (name) {\n try {\n return window.localStorage.getItem(name)\n } catch (err) {\n localStore.error(err)\n }\n return null\n },\n\n parse: function (name) {\n try {\n return JSON.parse(localStore.get(name)) || {}\n } catch (err) {\n // noop\n }\n return null\n },\n\n set: function (name, value) {\n try {\n window.localStorage.setItem(name, JSON.stringify(value))\n } catch (err) {\n localStore.error(err)\n }\n },\n\n remove: function (name) {\n try {\n window.localStorage.removeItem(name)\n } catch (err) {\n localStore.error(err)\n }\n },\n}\n\n// Use localstorage for most data but still use cookie for COOKIE_PERSISTED_PROPERTIES\n// This solves issues with cookies having too much data in them causing headers too large\n// Also makes sure we don't have to send a ton of data to the server\nconst COOKIE_PERSISTED_PROPERTIES = [DISTINCT_ID, SESSION_ID, SESSION_RECORDING_IS_SAMPLED]\n\nexport const localPlusCookieStore: PersistentStore = {\n ...localStore,\n parse: function (name) {\n try {\n let extend: Properties = {}\n try {\n // See if there's a cookie stored with data.\n extend = cookieStore.parse(name) || {}\n } catch (err) {}\n const value = _extend(extend, JSON.parse(localStore.get(name) || '{}'))\n localStore.set(name, value)\n return value\n } catch (err) {\n // noop\n }\n return null\n },\n\n set: function (name, value, days, cross_subdomain, is_secure) {\n try {\n localStore.set(name, value)\n const cookiePersistedProperties: Record<string, any> = {}\n COOKIE_PERSISTED_PROPERTIES.forEach((key) => {\n if (value[key]) {\n cookiePersistedProperties[key] = value[key]\n }\n })\n\n if (Object.keys(cookiePersistedProperties).length) {\n cookieStore.set(name, cookiePersistedProperties, days, cross_subdomain, is_secure)\n }\n } catch (err) {\n localStore.error(err)\n }\n },\n\n remove: function (name, cross_subdomain) {\n try {\n window.localStorage.removeItem(name)\n cookieStore.remove(name, cross_subdomain)\n } catch (err) {\n localStore.error(err)\n }\n },\n}\n\nconst memoryStorage: Properties = {}\n\n// Storage that only lasts the length of the pageview if we don't want to use cookies\nexport const memoryStore: PersistentStore = {\n is_supported: function () {\n return true\n },\n\n error: function (msg) {\n logger.error('memoryStorage error: ' + msg)\n },\n\n get: function (name) {\n return memoryStorage[name] || null\n },\n\n parse: function (name) {\n return memoryStorage[name] || null\n },\n\n set: function (name, value) {\n memoryStorage[name] = value\n },\n\n remove: function (name) {\n delete memoryStorage[name]\n },\n}\n\nlet sessionStorageSupported: boolean | null = null\nexport const resetSessionStorageSupported = () => {\n sessionStorageSupported = null\n}\n// Storage that only lasts the length of a tab/window. Survives page refreshes\nexport const sessionStore: PersistentStore = {\n is_supported: function () {\n if (!_isNull(sessionStorageSupported)) {\n return sessionStorageSupported\n }\n sessionStorageSupported = true\n if (!_isUndefined(window)) {\n try {\n const key = '__support__',\n val = 'xyz'\n sessionStore.set(key, val)\n if (sessionStore.get(key) !== '\"xyz\"') {\n sessionStorageSupported = false\n }\n sessionStore.remove(key)\n } catch (err) {\n sessionStorageSupported = false\n }\n } else {\n sessionStorageSupported = false\n }\n return sessionStorageSupported\n },\n\n error: function (msg) {\n logger.error('sessionStorage error: ', msg)\n },\n\n get: function (name) {\n try {\n return window.sessionStorage.getItem(name)\n } catch (err) {\n sessionStore.error(err)\n }\n return null\n },\n\n parse: function (name) {\n try {\n return JSON.parse(sessionStore.get(name)) || null\n } catch (err) {\n // noop\n }\n return null\n },\n\n set: function (name, value) {\n try {\n window.sessionStorage.setItem(name, JSON.stringify(value))\n } catch (err) {\n sessionStore.error(err)\n }\n },\n\n remove: function (name) {\n try {\n window.sessionStorage.removeItem(name)\n } catch (err) {\n sessionStore.error(err)\n }\n },\n}\n"]}
@@ -209,6 +209,9 @@ export interface DecideResponse {
209
209
  endpoint?: string;
210
210
  consoleLogRecordingEnabled?: boolean;
211
211
  recorderVersion?: 'v1' | 'v2';
212
+ sampleRate?: string | null;
213
+ minimumDurationMilliseconds?: number;
214
+ linkedFlag?: string | null;
212
215
  };
213
216
  surveys?: boolean;
214
217
  toolbarParams: ToolbarParams;
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAsKA,MAAM,CAAN,IAAY,WAGX;AAHD,WAAY,WAAW;IACnB,iCAAkB,CAAA;IAClB,gCAAiB,CAAA;AACrB,CAAC,EAHW,WAAW,KAAX,WAAW,QAGtB","sourcesContent":["import type { MaskInputOptions, SlimDOMOptions } from 'rrweb-snapshot'\nimport { PostHog } from './posthog-core'\nimport { RetryQueue } from './retry-queue'\n\nexport type Property = any\nexport type Properties = Record<string, Property>\n\nexport interface CaptureResult {\n uuid: string\n event: string\n properties: Properties\n $set?: Properties\n $set_once?: Properties\n timestamp?: Date\n}\nexport type CaptureCallback = (response: any, data: any) => void\n\nexport type AutocaptureCompatibleElement = 'a' | 'button' | 'form' | 'input' | 'select' | 'textarea' | 'label'\nexport type DomAutocaptureEvents = 'click' | 'change' | 'submit'\n\n/**\n * If an array is passed for an allowlist, autocapture events will only be sent for elements matching\n * at least one of the elements in the array. Multiple allowlists can be used\n */\nexport interface AutocaptureConfig {\n /**\n * List of URLs to allow autocapture on, can be strings to match\n * or regexes e.g. ['https://example.com', 'test.com/.*']\n */\n url_allowlist?: (string | RegExp)[]\n\n /**\n * List of DOM events to allow autocapture on e.g. ['click', 'change', 'submit']\n */\n dom_event_allowlist?: DomAutocaptureEvents[]\n\n /**\n * List of DOM elements to allow autocapture on\n * e.g. ['a', 'button', 'form', 'input', 'select', 'textarea', 'label']\n */\n element_allowlist?: AutocaptureCompatibleElement[]\n\n /**\n * List of CSS selectors to allow autocapture on\n * e.g. ['[ph-capture]']\n */\n css_selector_allowlist?: string[]\n\n /**\n * Exclude certain element attributes from autocapture\n * E.g. ['aria-label'] or [data-attr-pii]\n */\n element_attribute_ignorelist?: string[]\n}\n\nexport type UUIDVersion = 'og' | 'v7'\n\nexport interface PostHogConfig {\n api_host: string\n api_method: string\n api_transport: string\n ui_host: string | null\n token: string\n autocapture: boolean | AutocaptureConfig\n rageclick: boolean\n cross_subdomain_cookie: boolean\n persistence: 'localStorage' | 'cookie' | 'memory' | 'localStorage+cookie' | 'sessionStorage'\n persistence_name: string\n cookie_name: string\n loaded: (posthog_instance: PostHog) => void\n store_google: boolean\n custom_campaign_params: string[]\n // a list of strings to be tested against navigator.userAgent to determine if the source is a bot\n // this is **added to** the default list of bots that we check\n // defaults to the empty array\n custom_blocked_useragents: string[]\n save_referrer: boolean\n verbose: boolean\n capture_pageview: boolean\n capture_pageleave: boolean\n debug: boolean\n cookie_expiration: number\n upgrade: boolean\n disable_session_recording: boolean\n disable_persistence: boolean\n disable_cookie: boolean\n enable_recording_console_log?: boolean\n secure_cookie: boolean\n ip: boolean\n opt_out_capturing_by_default: boolean\n opt_out_persistence_by_default: boolean\n opt_out_capturing_persistence_type: 'localStorage' | 'cookie'\n opt_out_capturing_cookie_prefix: string | null\n opt_in_site_apps: boolean\n respect_dnt: boolean\n property_blacklist: string[]\n xhr_headers: { [header_name: string]: string }\n on_xhr_error: (failedRequest: XMLHttpRequest) => void\n inapp_protocol: string\n inapp_link_new_window: boolean\n request_batching: boolean\n sanitize_properties: ((properties: Properties, event_name: string) => Properties) | null\n properties_string_max_length: number\n session_recording: SessionRecordingOptions\n session_idle_timeout_seconds: number\n mask_all_element_attributes: boolean\n mask_all_text: boolean\n advanced_disable_decide: boolean\n advanced_disable_feature_flags: boolean\n advanced_disable_feature_flags_on_first_load: boolean\n advanced_disable_toolbar_metrics: boolean\n get_device_id: (uuid: string) => string\n name: string\n callback_fn: string\n _onCapture: (eventName: string, eventData: CaptureResult) => void\n capture_performance?: boolean\n // Should only be used for testing. Could negatively impact performance.\n disable_compression: boolean\n bootstrap: {\n distinctID?: string\n isIdentifiedID?: boolean\n featureFlags?: Record<string, boolean | string>\n featureFlagPayloads?: Record<string, JsonType>\n }\n segment?: any\n __preview_measure_pageview_stats?: boolean\n}\n\nexport interface OptInOutCapturingOptions {\n capture: (event: string, properties: Properties, options: CaptureOptions) => void\n capture_event_name: string\n capture_properties: Properties\n enable_persistence: boolean\n clear_persistence: boolean\n persistence_type: 'cookie' | 'localStorage' | 'localStorage+cookie'\n cookie_prefix: string\n cookie_expiration: number\n cross_subdomain_cookie: boolean\n secure_cookie: boolean\n}\n\nexport interface isFeatureEnabledOptions {\n send_event: boolean\n}\n\nexport interface SessionRecordingOptions {\n blockClass?: string | RegExp\n blockSelector?: string | null\n ignoreClass?: string\n maskTextClass?: string | RegExp\n maskTextSelector?: string | null\n maskTextFn?: ((text: string) => string) | null\n maskAllInputs?: boolean\n maskInputOptions?: MaskInputOptions\n maskInputFn?: ((text: string, element?: HTMLElement) => string) | null\n /** Modify the network request before it is captured. Returning null stops it being captured */\n maskNetworkRequestFn?: ((url: NetworkRequest) => NetworkRequest | null | undefined) | null\n slimDOMOptions?: SlimDOMOptions | 'all' | true\n collectFonts?: boolean\n inlineStylesheet?: boolean\n recorderVersion?: 'v1' | 'v2'\n recordCrossOriginIframes?: boolean\n}\n\nexport type SessionIdChangedCallback = (sessionId: string, windowId: string | null | undefined) => void\n\nexport enum Compression {\n GZipJS = 'gzip-js',\n Base64 = 'base64',\n}\n\nexport interface XHROptions {\n transport?: 'XHR' | 'sendBeacon'\n method?: 'POST' | 'GET'\n urlQueryArgs?: { compression: Compression }\n verbose?: boolean\n blob?: boolean\n sendBeacon?: boolean\n}\n\nexport interface CaptureOptions extends XHROptions {\n $set?: Properties /** used with $identify */\n $set_once?: Properties /** used with $identify */\n _batchKey?: string /** key of queue, e.g. 'sessionRecording' vs 'event' */\n _metrics?: Properties\n _noTruncate?: boolean /** if set, overrides and disables config.properties_string_max_length */\n endpoint?: string /** defaults to '/e/' */\n send_instantly?: boolean /** if set skips the batched queue */\n timestamp?: Date\n}\n\nexport interface RetryQueueElement {\n retryAt: Date\n requestData: QueuedRequestData\n}\nexport interface QueuedRequestData {\n url: string\n data: Properties\n options: CaptureOptions\n headers?: Properties\n callback?: RequestCallback\n retriesPerformedSoFar?: number\n}\n\nexport interface XHRParams extends QueuedRequestData {\n retryQueue: RetryQueue\n onXHRError: (req: XMLHttpRequest) => void\n timeout?: number\n onResponse?: (req: XMLHttpRequest) => void\n}\n\nexport interface DecideResponse {\n status: number\n supportedCompression: Compression[]\n config: {\n enable_collect_everything: boolean\n }\n custom_properties: AutoCaptureCustomProperty[] // TODO: delete, not sent\n featureFlags: Record<string, string | boolean>\n featureFlagPayloads: Record<string, JsonType>\n errorsWhileComputingFlags: boolean\n autocapture_opt_out?: boolean\n capturePerformance?: boolean\n // this is currently in development and may have breaking changes without a major version bump\n autocaptureExceptions?:\n | boolean\n | {\n endpoint?: string\n errors_to_ignore: string[]\n }\n sessionRecording?: {\n endpoint?: string\n consoleLogRecordingEnabled?: boolean\n recorderVersion?: 'v1' | 'v2'\n }\n surveys?: boolean\n toolbarParams: ToolbarParams\n editorParams?: ToolbarParams /** @deprecated, renamed to toolbarParams, still present on older API responses */\n toolbarVersion: 'toolbar' /** @deprecated, moved to toolbarParams */\n isAuthenticated: boolean\n siteApps: { id: number; url: string }[]\n}\n\nexport type FeatureFlagsCallback = (flags: string[], variants: Record<string, string | boolean>) => void\n\n// TODO: delete custom_properties after changeless typescript refactor\nexport interface AutoCaptureCustomProperty {\n name: string\n css_selector: string\n event_selectors: string[]\n}\n\nexport interface CompressionData {\n data: string\n compression?: Compression\n}\n\nexport interface GDPROptions {\n capture?: (\n event: string,\n properties: Properties,\n options: CaptureOptions\n ) => void /** function used for capturing a PostHog event to record the opt-in action */\n captureEventName?: string /** event name to be used for capturing the opt-in action */\n captureProperties?: Properties /** set of properties to be captured along with the opt-in action */\n /** persistence mechanism used */\n persistenceType?: 'cookie' | 'localStorage' | 'localStorage+cookie'\n persistencePrefix?: string /** [__ph_opt_in_out] - custom prefix to be used in the cookie/localstorage name */\n cookieExpiration?: number /** number of days until the opt-in cookie expires */\n crossSubdomainCookie?: boolean /** whether the opt-in cookie is set as cross-subdomain or not */\n secureCookie?: boolean /** whether the opt-in cookie is set as secure or not */\n respectDnt?: boolean\n window?: Window\n}\n\nexport type RequestCallback = (response: Record<string, any>, data?: Properties) => void\n\nexport interface PersistentStore {\n is_supported: () => boolean\n error: (error: any) => void\n parse: (name: string) => any\n get: (name: string) => any\n set: (name: string, value: any, expire_days?: number | null, cross_subdomain?: boolean, secure?: boolean) => void\n remove: (name: string, cross_subdomain?: boolean) => void\n}\n\n// eslint-disable-next-line @typescript-eslint/ban-types\nexport type Breaker = {}\nexport type EventHandler = (event: Event) => boolean | void\n\nexport type ToolbarUserIntent = 'add-action' | 'edit-action'\nexport type ToolbarSource = 'url' | 'localstorage'\nexport type ToolbarVersion = 'toolbar'\n\n/* sync with posthog */\nexport interface ToolbarParams {\n token?: string /** public posthog-js token */\n temporaryToken?: string /** private temporary user token */\n actionId?: number\n userIntent?: ToolbarUserIntent\n source?: ToolbarSource\n toolbarVersion?: ToolbarVersion\n instrument?: boolean\n distinctId?: string\n userEmail?: string\n dataAttributes?: string[]\n featureFlags?: Record<string, string | boolean>\n}\n\nexport interface PostData {\n buffer?: BlobPart\n compression?: Compression\n data?: string\n}\n\nexport interface JSC {\n (): void\n [key: string]: (response: any) => void\n}\n\nexport type SnippetArrayItem = [method: string, ...args: any[]]\n\nexport type JsonType = string | number | boolean | null | { [key: string]: JsonType } | Array<JsonType>\n\n/** A feature that isn't publicly available yet.*/\nexport interface EarlyAccessFeature {\n // Sync this with the backend's EarlyAccessFeatureSerializer!\n name: string\n description: string\n stage: 'concept' | 'alpha' | 'beta'\n documentationUrl: string | null\n flagKey: string | null\n}\n\nexport type EarlyAccessFeatureCallback = (earlyAccessFeatures: EarlyAccessFeature[]) => void\n\nexport interface EarlyAccessFeatureResponse {\n earlyAccessFeatures: EarlyAccessFeature[]\n}\n\nexport type NetworkRequest = {\n url: string\n}\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAsKA,MAAM,CAAN,IAAY,WAGX;AAHD,WAAY,WAAW;IACnB,iCAAkB,CAAA;IAClB,gCAAiB,CAAA;AACrB,CAAC,EAHW,WAAW,KAAX,WAAW,QAGtB","sourcesContent":["import type { MaskInputOptions, SlimDOMOptions } from 'rrweb-snapshot'\nimport { PostHog } from './posthog-core'\nimport { RetryQueue } from './retry-queue'\n\nexport type Property = any\nexport type Properties = Record<string, Property>\n\nexport interface CaptureResult {\n uuid: string\n event: string\n properties: Properties\n $set?: Properties\n $set_once?: Properties\n timestamp?: Date\n}\nexport type CaptureCallback = (response: any, data: any) => void\n\nexport type AutocaptureCompatibleElement = 'a' | 'button' | 'form' | 'input' | 'select' | 'textarea' | 'label'\nexport type DomAutocaptureEvents = 'click' | 'change' | 'submit'\n\n/**\n * If an array is passed for an allowlist, autocapture events will only be sent for elements matching\n * at least one of the elements in the array. Multiple allowlists can be used\n */\nexport interface AutocaptureConfig {\n /**\n * List of URLs to allow autocapture on, can be strings to match\n * or regexes e.g. ['https://example.com', 'test.com/.*']\n */\n url_allowlist?: (string | RegExp)[]\n\n /**\n * List of DOM events to allow autocapture on e.g. ['click', 'change', 'submit']\n */\n dom_event_allowlist?: DomAutocaptureEvents[]\n\n /**\n * List of DOM elements to allow autocapture on\n * e.g. ['a', 'button', 'form', 'input', 'select', 'textarea', 'label']\n */\n element_allowlist?: AutocaptureCompatibleElement[]\n\n /**\n * List of CSS selectors to allow autocapture on\n * e.g. ['[ph-capture]']\n */\n css_selector_allowlist?: string[]\n\n /**\n * Exclude certain element attributes from autocapture\n * E.g. ['aria-label'] or [data-attr-pii]\n */\n element_attribute_ignorelist?: string[]\n}\n\nexport type UUIDVersion = 'og' | 'v7'\n\nexport interface PostHogConfig {\n api_host: string\n api_method: string\n api_transport: string\n ui_host: string | null\n token: string\n autocapture: boolean | AutocaptureConfig\n rageclick: boolean\n cross_subdomain_cookie: boolean\n persistence: 'localStorage' | 'cookie' | 'memory' | 'localStorage+cookie' | 'sessionStorage'\n persistence_name: string\n cookie_name: string\n loaded: (posthog_instance: PostHog) => void\n store_google: boolean\n custom_campaign_params: string[]\n // a list of strings to be tested against navigator.userAgent to determine if the source is a bot\n // this is **added to** the default list of bots that we check\n // defaults to the empty array\n custom_blocked_useragents: string[]\n save_referrer: boolean\n verbose: boolean\n capture_pageview: boolean\n capture_pageleave: boolean\n debug: boolean\n cookie_expiration: number\n upgrade: boolean\n disable_session_recording: boolean\n disable_persistence: boolean\n disable_cookie: boolean\n enable_recording_console_log?: boolean\n secure_cookie: boolean\n ip: boolean\n opt_out_capturing_by_default: boolean\n opt_out_persistence_by_default: boolean\n opt_out_capturing_persistence_type: 'localStorage' | 'cookie'\n opt_out_capturing_cookie_prefix: string | null\n opt_in_site_apps: boolean\n respect_dnt: boolean\n property_blacklist: string[]\n xhr_headers: { [header_name: string]: string }\n on_xhr_error: (failedRequest: XMLHttpRequest) => void\n inapp_protocol: string\n inapp_link_new_window: boolean\n request_batching: boolean\n sanitize_properties: ((properties: Properties, event_name: string) => Properties) | null\n properties_string_max_length: number\n session_recording: SessionRecordingOptions\n session_idle_timeout_seconds: number\n mask_all_element_attributes: boolean\n mask_all_text: boolean\n advanced_disable_decide: boolean\n advanced_disable_feature_flags: boolean\n advanced_disable_feature_flags_on_first_load: boolean\n advanced_disable_toolbar_metrics: boolean\n get_device_id: (uuid: string) => string\n name: string\n callback_fn: string\n _onCapture: (eventName: string, eventData: CaptureResult) => void\n capture_performance?: boolean\n // Should only be used for testing. Could negatively impact performance.\n disable_compression: boolean\n bootstrap: {\n distinctID?: string\n isIdentifiedID?: boolean\n featureFlags?: Record<string, boolean | string>\n featureFlagPayloads?: Record<string, JsonType>\n }\n segment?: any\n __preview_measure_pageview_stats?: boolean\n}\n\nexport interface OptInOutCapturingOptions {\n capture: (event: string, properties: Properties, options: CaptureOptions) => void\n capture_event_name: string\n capture_properties: Properties\n enable_persistence: boolean\n clear_persistence: boolean\n persistence_type: 'cookie' | 'localStorage' | 'localStorage+cookie'\n cookie_prefix: string\n cookie_expiration: number\n cross_subdomain_cookie: boolean\n secure_cookie: boolean\n}\n\nexport interface isFeatureEnabledOptions {\n send_event: boolean\n}\n\nexport interface SessionRecordingOptions {\n blockClass?: string | RegExp\n blockSelector?: string | null\n ignoreClass?: string\n maskTextClass?: string | RegExp\n maskTextSelector?: string | null\n maskTextFn?: ((text: string) => string) | null\n maskAllInputs?: boolean\n maskInputOptions?: MaskInputOptions\n maskInputFn?: ((text: string, element?: HTMLElement) => string) | null\n /** Modify the network request before it is captured. Returning null stops it being captured */\n maskNetworkRequestFn?: ((url: NetworkRequest) => NetworkRequest | null | undefined) | null\n slimDOMOptions?: SlimDOMOptions | 'all' | true\n collectFonts?: boolean\n inlineStylesheet?: boolean\n recorderVersion?: 'v1' | 'v2'\n recordCrossOriginIframes?: boolean\n}\n\nexport type SessionIdChangedCallback = (sessionId: string, windowId: string | null | undefined) => void\n\nexport enum Compression {\n GZipJS = 'gzip-js',\n Base64 = 'base64',\n}\n\nexport interface XHROptions {\n transport?: 'XHR' | 'sendBeacon'\n method?: 'POST' | 'GET'\n urlQueryArgs?: { compression: Compression }\n verbose?: boolean\n blob?: boolean\n sendBeacon?: boolean\n}\n\nexport interface CaptureOptions extends XHROptions {\n $set?: Properties /** used with $identify */\n $set_once?: Properties /** used with $identify */\n _batchKey?: string /** key of queue, e.g. 'sessionRecording' vs 'event' */\n _metrics?: Properties\n _noTruncate?: boolean /** if set, overrides and disables config.properties_string_max_length */\n endpoint?: string /** defaults to '/e/' */\n send_instantly?: boolean /** if set skips the batched queue */\n timestamp?: Date\n}\n\nexport interface RetryQueueElement {\n retryAt: Date\n requestData: QueuedRequestData\n}\nexport interface QueuedRequestData {\n url: string\n data: Properties\n options: CaptureOptions\n headers?: Properties\n callback?: RequestCallback\n retriesPerformedSoFar?: number\n}\n\nexport interface XHRParams extends QueuedRequestData {\n retryQueue: RetryQueue\n onXHRError: (req: XMLHttpRequest) => void\n timeout?: number\n onResponse?: (req: XMLHttpRequest) => void\n}\n\nexport interface DecideResponse {\n status: number\n supportedCompression: Compression[]\n config: {\n enable_collect_everything: boolean\n }\n custom_properties: AutoCaptureCustomProperty[] // TODO: delete, not sent\n featureFlags: Record<string, string | boolean>\n featureFlagPayloads: Record<string, JsonType>\n errorsWhileComputingFlags: boolean\n autocapture_opt_out?: boolean\n capturePerformance?: boolean\n // this is currently in development and may have breaking changes without a major version bump\n autocaptureExceptions?:\n | boolean\n | {\n endpoint?: string\n errors_to_ignore: string[]\n }\n sessionRecording?: {\n endpoint?: string\n consoleLogRecordingEnabled?: boolean\n recorderVersion?: 'v1' | 'v2'\n // the API returns a decimal between 0 and 1 as a string\n sampleRate?: string | null\n minimumDurationMilliseconds?: number\n linkedFlag?: string | null\n }\n surveys?: boolean\n toolbarParams: ToolbarParams\n editorParams?: ToolbarParams /** @deprecated, renamed to toolbarParams, still present on older API responses */\n toolbarVersion: 'toolbar' /** @deprecated, moved to toolbarParams */\n isAuthenticated: boolean\n siteApps: { id: number; url: string }[]\n}\n\nexport type FeatureFlagsCallback = (flags: string[], variants: Record<string, string | boolean>) => void\n\n// TODO: delete custom_properties after changeless typescript refactor\nexport interface AutoCaptureCustomProperty {\n name: string\n css_selector: string\n event_selectors: string[]\n}\n\nexport interface CompressionData {\n data: string\n compression?: Compression\n}\n\nexport interface GDPROptions {\n capture?: (\n event: string,\n properties: Properties,\n options: CaptureOptions\n ) => void /** function used for capturing a PostHog event to record the opt-in action */\n captureEventName?: string /** event name to be used for capturing the opt-in action */\n captureProperties?: Properties /** set of properties to be captured along with the opt-in action */\n /** persistence mechanism used */\n persistenceType?: 'cookie' | 'localStorage' | 'localStorage+cookie'\n persistencePrefix?: string /** [__ph_opt_in_out] - custom prefix to be used in the cookie/localstorage name */\n cookieExpiration?: number /** number of days until the opt-in cookie expires */\n crossSubdomainCookie?: boolean /** whether the opt-in cookie is set as cross-subdomain or not */\n secureCookie?: boolean /** whether the opt-in cookie is set as secure or not */\n respectDnt?: boolean\n window?: Window\n}\n\nexport type RequestCallback = (response: Record<string, any>, data?: Properties) => void\n\nexport interface PersistentStore {\n is_supported: () => boolean\n error: (error: any) => void\n parse: (name: string) => any\n get: (name: string) => any\n set: (name: string, value: any, expire_days?: number | null, cross_subdomain?: boolean, secure?: boolean) => void\n remove: (name: string, cross_subdomain?: boolean) => void\n}\n\n// eslint-disable-next-line @typescript-eslint/ban-types\nexport type Breaker = {}\nexport type EventHandler = (event: Event) => boolean | void\n\nexport type ToolbarUserIntent = 'add-action' | 'edit-action'\nexport type ToolbarSource = 'url' | 'localstorage'\nexport type ToolbarVersion = 'toolbar'\n\n/* sync with posthog */\nexport interface ToolbarParams {\n token?: string /** public posthog-js token */\n temporaryToken?: string /** private temporary user token */\n actionId?: number\n userIntent?: ToolbarUserIntent\n source?: ToolbarSource\n toolbarVersion?: ToolbarVersion\n instrument?: boolean\n distinctId?: string\n userEmail?: string\n dataAttributes?: string[]\n featureFlags?: Record<string, string | boolean>\n}\n\nexport interface PostData {\n buffer?: BlobPart\n compression?: Compression\n data?: string\n}\n\nexport interface JSC {\n (): void\n [key: string]: (response: any) => void\n}\n\nexport type SnippetArrayItem = [method: string, ...args: any[]]\n\nexport type JsonType = string | number | boolean | null | { [key: string]: JsonType } | Array<JsonType>\n\n/** A feature that isn't publicly available yet.*/\nexport interface EarlyAccessFeature {\n // Sync this with the backend's EarlyAccessFeatureSerializer!\n name: string\n description: string\n stage: 'concept' | 'alpha' | 'beta'\n documentationUrl: string | null\n flagKey: string | null\n}\n\nexport type EarlyAccessFeatureCallback = (earlyAccessFeatures: EarlyAccessFeature[]) => void\n\nexport interface EarlyAccessFeatureResponse {\n earlyAccessFeatures: EarlyAccessFeature[]\n}\n\nexport type NetworkRequest = {\n url: string\n}\n"]}
@@ -29,12 +29,14 @@ export declare function _includes<T = any>(str: T[] | string, needle: T): boolea
29
29
  * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries
30
30
  */
31
31
  export declare function _entries<T = any>(obj: Record<string, T>): [string, T][];
32
- export declare const _isObject: (obj: any) => obj is Record<string, any>;
33
- export declare const _isEmptyObject: (obj: any) => obj is Record<string, any>;
34
- export declare const _isUndefined: (obj: any) => obj is undefined;
35
- export declare const _isString: (obj: any) => obj is string;
36
- export declare const _isDate: (obj: any) => obj is Date;
37
- export declare const _isNumber: (obj: any) => obj is number;
32
+ export declare const _isObject: (x: unknown) => x is Record<string, any>;
33
+ export declare const _isEmptyObject: (x: unknown) => x is Record<string, any>;
34
+ export declare const _isUndefined: (x: unknown) => x is undefined;
35
+ export declare const _isString: (x: unknown) => x is string;
36
+ export declare const _isNull: (x: unknown) => x is null;
37
+ export declare const _isDate: (x: unknown) => x is Date;
38
+ export declare const _isNumber: (x: unknown) => x is number;
39
+ export declare const _isBoolean: (x: unknown) => x is boolean;
38
40
  export declare const _isValidRegex: (str: string) => boolean;
39
41
  export declare const _isUrlMatchingRegex: (url: string, pattern: string) => boolean;
40
42
  export declare const _encodeDates: (obj: Properties) => Properties;
@@ -51,10 +53,6 @@ export declare function _base64Encode(data: string): string;
51
53
  export declare const _utf8Encode: (string: string) => string;
52
54
  export declare const DEFAULT_BLOCKED_UA_STRS: string[];
53
55
  export declare const _isBlockedUA: (ua: string, customBlockedUserAgents: string[]) => boolean;
54
- /**
55
- * @param {Object=} formdata
56
- * @param {string=} arg_separator
57
- */
58
56
  export declare const _HTTPBuildQuery: (formdata: Record<string, any>, arg_separator?: string) => string;
59
57
  export declare const _getQueryParam: (url: string, param: string) => string;
60
58
  export declare const _getHashParam: (hash: string, param: string) => string | null;
package/lib/src/utils.js CHANGED
@@ -37,7 +37,9 @@ var navigator = win.navigator || { userAgent: '' };
37
37
  var document = win.document || {};
38
38
  var userAgent = navigator.userAgent;
39
39
  var localDomains = ['localhost', '127.0.0.1'];
40
- var nativeForEach = ArrayProto.forEach, nativeIndexOf = ArrayProto.indexOf, nativeIsArray = Array.isArray, breaker = {};
40
+ var nativeForEach = ArrayProto.forEach, nativeIndexOf = ArrayProto.indexOf,
41
+ // eslint-disable-next-line posthog-js/no-direct-array-check
42
+ nativeIsArray = Array.isArray, breaker = {};
41
43
  var LOGGER_PREFIX = '[PostHog.js]';
42
44
  export var logger = {
43
45
  _log: function (level) {
@@ -94,13 +96,13 @@ export var _trim = function (str) {
94
96
  };
95
97
  export var _bind_instance_methods = function (obj) {
96
98
  for (var func in obj) {
97
- if (typeof obj[func] === 'function') {
99
+ if (_isFunction(obj[func])) {
98
100
  obj[func] = obj[func].bind(obj);
99
101
  }
100
102
  }
101
103
  };
102
104
  export function _eachArray(obj, iterator, thisArg) {
103
- if (Array.isArray(obj)) {
105
+ if (_isArray(obj)) {
104
106
  if (nativeForEach && obj.forEach === nativeForEach) {
105
107
  obj.forEach(iterator, thisArg);
106
108
  }
@@ -119,10 +121,10 @@ export function _eachArray(obj, iterator, thisArg) {
119
121
  * @param {Object=} thisArg
120
122
  */
121
123
  export function _each(obj, iterator, thisArg) {
122
- if (obj === null || obj === undefined) {
124
+ if (_isNull(obj) || _isUndefined(obj)) {
123
125
  return;
124
126
  }
125
- if (Array.isArray(obj)) {
127
+ if (_isArray(obj)) {
126
128
  return _eachArray(obj, iterator, thisArg);
127
129
  }
128
130
  for (var key in obj) {
@@ -156,6 +158,7 @@ export var _isArray = nativeIsArray ||
156
158
  // let bomb = { toString : undefined, valueOf: function(o) { return "function BOMBA!"; }};
157
159
  export var _isFunction = function (f) {
158
160
  try {
161
+ // eslint-disable-next-line posthog-js/no-direct-function-check
159
162
  return /^\s*\bfunction\b/.test(f);
160
163
  }
161
164
  catch (x) {
@@ -164,7 +167,7 @@ export var _isFunction = function (f) {
164
167
  };
165
168
  export var _include = function (obj, target) {
166
169
  var found = false;
167
- if (obj === null) {
170
+ if (_isNull(obj)) {
168
171
  return found;
169
172
  }
170
173
  if (nativeIndexOf && obj.indexOf === nativeIndexOf) {
@@ -195,13 +198,14 @@ export function _entries(obj) {
195
198
  return resArray;
196
199
  }
197
200
  // Underscore Addons
198
- export var _isObject = function (obj) {
199
- return obj === Object(obj) && !_isArray(obj);
201
+ export var _isObject = function (x) {
202
+ // eslint-disable-next-line posthog-js/no-direct-object-check
203
+ return x === Object(x) && !_isArray(x);
200
204
  };
201
- export var _isEmptyObject = function (obj) {
202
- if (_isObject(obj)) {
203
- for (var key in obj) {
204
- if (hasOwnProperty.call(obj, key)) {
205
+ export var _isEmptyObject = function (x) {
206
+ if (_isObject(x)) {
207
+ for (var key in x) {
208
+ if (hasOwnProperty.call(x, key)) {
205
209
  return false;
206
210
  }
207
211
  }
@@ -209,17 +213,28 @@ export var _isEmptyObject = function (obj) {
209
213
  }
210
214
  return false;
211
215
  };
212
- export var _isUndefined = function (obj) {
213
- return obj === void 0;
216
+ export var _isUndefined = function (x) {
217
+ return x === void 0;
214
218
  };
215
- export var _isString = function (obj) {
216
- return toString.call(obj) == '[object String]';
219
+ export var _isString = function (x) {
220
+ // eslint-disable-next-line posthog-js/no-direct-string-check
221
+ return toString.call(x) == '[object String]';
217
222
  };
218
- export var _isDate = function (obj) {
219
- return toString.call(obj) == '[object Date]';
223
+ export var _isNull = function (x) {
224
+ // eslint-disable-next-line posthog-js/no-direct-null-check
225
+ return x === null;
220
226
  };
221
- export var _isNumber = function (obj) {
222
- return toString.call(obj) == '[object Number]';
227
+ export var _isDate = function (x) {
228
+ // eslint-disable-next-line posthog-js/no-direct-date-check
229
+ return toString.call(x) == '[object Date]';
230
+ };
231
+ export var _isNumber = function (x) {
232
+ // eslint-disable-next-line posthog-js/no-direct-number-check
233
+ return toString.call(x) == '[object Number]';
234
+ };
235
+ export var _isBoolean = function (x) {
236
+ // eslint-disable-next-line posthog-js/no-direct-boolean-check
237
+ return toString.call(x) === '[object Boolean]';
223
238
  };
224
239
  export var _isValidRegex = function (str) {
225
240
  try {
@@ -296,7 +311,7 @@ export var _safewrap_class = function (klass, functions) {
296
311
  };
297
312
  export var _safewrap_instance_methods = function (obj) {
298
313
  for (var func in obj) {
299
- if (typeof obj[func] === 'function') {
314
+ if (_isFunction(obj[func])) {
300
315
  obj[func] = _safewrap(obj[func]);
301
316
  }
302
317
  }
@@ -353,7 +368,7 @@ export function _copyAndTruncateStrings(object, maxStringLength) {
353
368
  if (key && LONG_STRINGS_ALLOW_LIST.indexOf(key) > -1) {
354
369
  return value;
355
370
  }
356
- if (typeof value === 'string' && maxStringLength !== null) {
371
+ if (_isString(value) && !_isNull(maxStringLength)) {
357
372
  return value.slice(0, maxStringLength);
358
373
  }
359
374
  return value;
@@ -409,7 +424,7 @@ export var _utf8Encode = function (string) {
409
424
  else {
410
425
  enc = String.fromCharCode((c1 >> 12) | 224, ((c1 >> 6) & 63) | 128, (c1 & 63) | 128);
411
426
  }
412
- if (enc !== null) {
427
+ if (!_isNull(enc)) {
413
428
  if (end > start) {
414
429
  utftext += string.substring(start, end);
415
430
  }
@@ -481,10 +496,6 @@ export var _isBlockedUA = function (ua, customBlockedUserAgents) {
481
496
  }
482
497
  });
483
498
  };
484
- /**
485
- * @param {Object=} formdata
486
- * @param {string=} arg_separator
487
- */
488
499
  export var _HTTPBuildQuery = function (formdata, arg_separator) {
489
500
  if (arg_separator === void 0) { arg_separator = '&'; }
490
501
  var use_val;
@@ -503,7 +514,7 @@ export var _getQueryParam = function (url, param) {
503
514
  var regexS = '[\\?&]' + cleanParam + '=([^&#]*)';
504
515
  var regex = new RegExp(regexS);
505
516
  var results = regex.exec(url);
506
- if (results === null || (results && typeof results[1] !== 'string' && results[1].length)) {
517
+ if (_isNull(results) || (results && !_isString(results[1]) && results[1].length)) {
507
518
  return '';
508
519
  }
509
520
  else {
@@ -661,7 +672,7 @@ export var _info = {
661
672
  },
662
673
  searchInfo: function () {
663
674
  var search = _info.searchEngine(), param = search != 'yahoo' ? 'q' : 'p', ret = {};
664
- if (search !== null) {
675
+ if (!_isNull(search)) {
665
676
  ret['$search_engine'] = search;
666
677
  var keyword = _getQueryParam(document.referrer, param);
667
678
  if (keyword.length) {
@@ -762,7 +773,7 @@ export var _info = {
762
773
  Mozilla: /rv:(\d+(\.\d+)?)/,
763
774
  };
764
775
  var regex = versionRegexs[browser];
765
- if (regex === undefined) {
776
+ if (_isUndefined(regex)) {
766
777
  return null;
767
778
  }
768
779
  var matches = userAgent.match(regex);
@@ -917,7 +928,7 @@ export function isCrossDomainCookie(documentLocation) {
917
928
  // split and slice isn't a great way to match arbitrary domains,
918
929
  // but it's good enough for ensuring we only match herokuapp.com when it is the TLD
919
930
  // for the hostname
920
- return hostname.split('.').slice(-2).join('.').indexOf('herokuapp.com') === -1;
931
+ return hostname.split('.').slice(-2).join('.') !== 'herokuapp.com';
921
932
  }
922
933
  export { win as window, userAgent, document };
923
934
  //# sourceMappingURL=utils.js.map