posthog-js-lite 4.10.4 → 4.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,459 +1,299 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var core = require('@posthog/core');
6
-
7
- const version = "4.10.4";
8
-
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
4
+ });
5
+ let _posthog_core = require("@posthog/core");
6
+ //#region src/version.ts
7
+ const version = "4.12.0";
8
+ //#endregion
9
+ //#region src/context.ts
9
10
  function getContext(window, disableCaptureUrlHashes = false) {
10
- let context = {};
11
- if (window?.navigator) {
12
- const userAgent = window.navigator.userAgent;
13
- const osValue = os(window);
14
- context = {
15
- ...context,
16
- ...(osValue !== undefined && {
17
- $os: osValue
18
- }),
19
- $browser: browser(userAgent, window.navigator.vendor, !!window.opera),
20
- $referrer: window.document.referrer,
21
- $referring_domain: referringDomain(window.document.referrer),
22
- $device: device(userAgent),
23
- $current_url: disableCaptureUrlHashes ? core.stripUrlHash(window.location.href) : window.location.href,
24
- $host: window.location.host,
25
- $pathname: window.location.pathname,
26
- $browser_version: browserVersion(userAgent, window.navigator.vendor, !!window.opera),
27
- $screen_height: window.screen.height,
28
- $screen_width: window.screen.width,
29
- $screen_dpr: window.devicePixelRatio
30
- };
31
- }
32
- context = {
33
- ...context,
34
- $lib: 'js',
35
- $lib_version: version,
36
- $insert_id: Math.random().toString(36).substring(2, 10) + Math.random().toString(36).substring(2, 10),
37
- $time: core.currentTimestamp() / 1000 // epoch time in seconds
38
- };
39
- return context; // TODO: strip empty props?
40
- }
41
- function includes(haystack, needle) {
42
- return haystack.indexOf(needle) >= 0;
43
- }
44
- function browser(userAgent, vendor, opera) {
45
- vendor = vendor || ''; // vendor is undefined for at least IE9
46
- if (opera || includes(userAgent, ' OPR/')) {
47
- if (includes(userAgent, 'Mini')) {
48
- return 'Opera Mini';
49
- }
50
- return 'Opera';
51
- } else if (/(BlackBerry|PlayBook|BB10)/i.test(userAgent)) {
52
- return 'BlackBerry';
53
- } else if (includes(userAgent, 'IEMobile') || includes(userAgent, 'WPDesktop')) {
54
- return 'Internet Explorer Mobile';
55
- } else if (includes(userAgent, 'SamsungBrowser/')) {
56
- // https://developer.samsung.com/internet/user-agent-string-format
57
- return 'Samsung Internet';
58
- } else if (includes(userAgent, 'Edge') || includes(userAgent, 'Edg/')) {
59
- return 'Microsoft Edge';
60
- } else if (includes(userAgent, 'FBIOS')) {
61
- return 'Facebook Mobile';
62
- } else if (includes(userAgent, 'Chrome')) {
63
- return 'Chrome';
64
- } else if (includes(userAgent, 'CriOS')) {
65
- return 'Chrome iOS';
66
- } else if (includes(userAgent, 'UCWEB') || includes(userAgent, 'UCBrowser')) {
67
- return 'UC Browser';
68
- } else if (includes(userAgent, 'FxiOS')) {
69
- return 'Firefox iOS';
70
- } else if (includes(vendor, 'Apple')) {
71
- if (includes(userAgent, 'Mobile')) {
72
- return 'Mobile Safari';
73
- }
74
- return 'Safari';
75
- } else if (includes(userAgent, 'Android')) {
76
- return 'Android Mobile';
77
- } else if (includes(userAgent, 'Konqueror')) {
78
- return 'Konqueror';
79
- } else if (includes(userAgent, 'Firefox')) {
80
- return 'Firefox';
81
- } else if (includes(userAgent, 'MSIE') || includes(userAgent, 'Trident/')) {
82
- return 'Internet Explorer';
83
- } else if (includes(userAgent, 'Gecko')) {
84
- return 'Mozilla';
85
- } else {
86
- return '';
87
- }
88
- }
89
- function browserVersion(userAgent, vendor, opera) {
90
- const regexList = {
91
- 'Internet Explorer Mobile': /rv:(\d+(\.\d+)?)/,
92
- 'Microsoft Edge': /Edge?\/(\d+(\.\d+)?)/,
93
- Chrome: /Chrome\/(\d+(\.\d+)?)/,
94
- 'Chrome iOS': /CriOS\/(\d+(\.\d+)?)/,
95
- 'UC Browser': /(UCBrowser|UCWEB)\/(\d+(\.\d+)?)/,
96
- Safari: /Version\/(\d+(\.\d+)?)/,
97
- 'Mobile Safari': /Version\/(\d+(\.\d+)?)/,
98
- Opera: /(Opera|OPR)\/(\d+(\.\d+)?)/,
99
- Firefox: /Firefox\/(\d+(\.\d+)?)/,
100
- 'Firefox iOS': /FxiOS\/(\d+(\.\d+)?)/,
101
- Konqueror: /Konqueror:(\d+(\.\d+)?)/,
102
- BlackBerry: /BlackBerry (\d+(\.\d+)?)/,
103
- 'Android Mobile': /android\s(\d+(\.\d+)?)/,
104
- 'Samsung Internet': /SamsungBrowser\/(\d+(\.\d+)?)/,
105
- 'Internet Explorer': /(rv:|MSIE )(\d+(\.\d+)?)/,
106
- Mozilla: /rv:(\d+(\.\d+)?)/
107
- };
108
- const browserString = browser(userAgent, vendor, opera);
109
- const regex = regexList[browserString] || undefined;
110
- if (regex === undefined) {
111
- return null;
112
- }
113
- const matches = userAgent.match(regex);
114
- if (!matches) {
115
- return null;
116
- }
117
- return parseFloat(matches[matches.length - 2]);
11
+ let context = {};
12
+ if (window?.navigator) {
13
+ const userAgent = window.navigator.userAgent;
14
+ const osValue = os(window);
15
+ const [webviewApp, webviewAppVersion] = (0, _posthog_core.detectWebviewApp)(userAgent);
16
+ context = {
17
+ ...context,
18
+ ...osValue !== void 0 && { $os: osValue },
19
+ $browser: (0, _posthog_core.detectBrowser)(userAgent, window.navigator.vendor),
20
+ ...webviewApp && { $webview_app: webviewApp },
21
+ ...webviewAppVersion && { $webview_app_version: webviewAppVersion },
22
+ $referrer: window.document.referrer,
23
+ $referring_domain: referringDomain(window.document.referrer),
24
+ $device: device(userAgent),
25
+ $current_url: disableCaptureUrlHashes ? (0, _posthog_core.stripUrlHash)(window.location.href) : window.location.href,
26
+ $host: window.location.host,
27
+ $pathname: window.location.pathname,
28
+ $browser_version: (0, _posthog_core.detectBrowserVersion)(userAgent, window.navigator.vendor),
29
+ $screen_height: window.screen.height,
30
+ $screen_width: window.screen.width,
31
+ $screen_dpr: window.devicePixelRatio
32
+ };
33
+ }
34
+ context = {
35
+ ...context,
36
+ $lib: "js",
37
+ $lib_version: version,
38
+ $insert_id: Math.random().toString(36).substring(2, 10) + Math.random().toString(36).substring(2, 10),
39
+ $time: (0, _posthog_core.currentTimestamp)() / 1e3
40
+ };
41
+ return context;
118
42
  }
119
43
  function os(window) {
120
- if (!window?.navigator) {
121
- return undefined;
122
- }
123
- const a = window.navigator.userAgent;
124
- if (/Windows/i.test(a)) {
125
- if (/Phone/.test(a) || /WPDesktop/.test(a)) {
126
- return 'Windows Phone';
127
- }
128
- return 'Windows';
129
- } else if (/(iPhone|iPad|iPod)/.test(a)) {
130
- return 'iOS';
131
- } else if (/Android/.test(a)) {
132
- return 'Android';
133
- } else if (/(BlackBerry|PlayBook|BB10)/i.test(a)) {
134
- return 'BlackBerry';
135
- } else if (/Mac/i.test(a)) {
136
- return 'Mac OS X';
137
- } else if (/Linux/.test(a)) {
138
- return 'Linux';
139
- } else if (/CrOS/.test(a)) {
140
- return 'Chrome OS';
141
- } else {
142
- return undefined;
143
- }
44
+ if (!window?.navigator) return;
45
+ const a = window.navigator.userAgent;
46
+ if (/Windows/i.test(a)) {
47
+ if (/Phone/.test(a) || /WPDesktop/.test(a)) return "Windows Phone";
48
+ return "Windows";
49
+ } else if (/(iPhone|iPad|iPod)/.test(a)) return "iOS";
50
+ else if (/Android/.test(a)) return "Android";
51
+ else if (/(BlackBerry|PlayBook|BB10)/i.test(a)) return "BlackBerry";
52
+ else if (/Mac/i.test(a)) return "Mac OS X";
53
+ else if (/Linux/.test(a)) return "Linux";
54
+ else if (/CrOS/.test(a)) return "Chrome OS";
55
+ else return;
144
56
  }
145
57
  function device(userAgent) {
146
- if (/Windows Phone/i.test(userAgent) || /WPDesktop/.test(userAgent)) {
147
- return 'Windows Phone';
148
- } else if (/iPad/.test(userAgent)) {
149
- return 'iPad';
150
- } else if (/iPod/.test(userAgent)) {
151
- return 'iPod Touch';
152
- } else if (/iPhone/.test(userAgent)) {
153
- return 'iPhone';
154
- } else if (/(BlackBerry|PlayBook|BB10)/i.test(userAgent)) {
155
- return 'BlackBerry';
156
- } else if (/Android/.test(userAgent)) {
157
- return 'Android';
158
- } else {
159
- return '';
160
- }
58
+ if (/Windows Phone/i.test(userAgent) || /WPDesktop/.test(userAgent)) return "Windows Phone";
59
+ else if (/iPad/.test(userAgent)) return "iPad";
60
+ else if (/iPod/.test(userAgent)) return "iPod Touch";
61
+ else if (/iPhone/.test(userAgent)) return "iPhone";
62
+ else if (/(BlackBerry|PlayBook|BB10)/i.test(userAgent)) return "BlackBerry";
63
+ else if (/Android/.test(userAgent)) return "Android";
64
+ else return "";
161
65
  }
162
66
  function referringDomain(referrer) {
163
- const split = referrer.split('/');
164
- if (split.length >= 3) {
165
- return split[2];
166
- }
167
- return '';
67
+ const split = referrer.split("/");
68
+ if (split.length >= 3) return split[2];
69
+ return "";
168
70
  }
169
-
170
- // Methods partially borrowed from quirksmode.org/js/cookies.html
71
+ //#endregion
72
+ //#region src/storage.ts
171
73
  const cookieStore = {
172
- getItem(key) {
173
- try {
174
- const nameEQ = key + '=';
175
- const ca = document.cookie.split(';');
176
- for (let i = 0; i < ca.length; i++) {
177
- let c = ca[i];
178
- while (c.charAt(0) == ' ') {
179
- c = c.substring(1, c.length);
180
- }
181
- if (c.indexOf(nameEQ) === 0) {
182
- return decodeURIComponent(c.substring(nameEQ.length, c.length));
183
- }
184
- }
185
- } catch (err) {}
186
- return null;
187
- },
188
- setItem(key, value) {
189
- try {
190
- const cdomain = '',
191
- expires = '',
192
- secure = '';
193
- const new_cookie_val = key + '=' + encodeURIComponent(value) + expires + '; path=/' + cdomain + secure;
194
- document.cookie = new_cookie_val;
195
- } catch (err) {
196
- return;
197
- }
198
- },
199
- removeItem(name) {
200
- try {
201
- cookieStore.setItem(name, '');
202
- } catch (err) {
203
- return;
204
- }
205
- },
206
- clear() {
207
- document.cookie = '';
208
- },
209
- getAllKeys() {
210
- const ca = document.cookie.split(';');
211
- const keys = [];
212
- for (let i = 0; i < ca.length; i++) {
213
- let c = ca[i];
214
- while (c.charAt(0) == ' ') {
215
- c = c.substring(1, c.length);
216
- }
217
- keys.push(c.split('=')[0]);
218
- }
219
- return keys;
220
- }
74
+ getItem(key) {
75
+ try {
76
+ const nameEQ = key + "=";
77
+ const ca = document.cookie.split(";");
78
+ for (let i = 0; i < ca.length; i++) {
79
+ let c = ca[i];
80
+ while (c.charAt(0) == " ") c = c.substring(1, c.length);
81
+ if (c.indexOf(nameEQ) === 0) return decodeURIComponent(c.substring(nameEQ.length, c.length));
82
+ }
83
+ } catch (err) {}
84
+ return null;
85
+ },
86
+ setItem(key, value) {
87
+ try {
88
+ const new_cookie_val = key + "=" + encodeURIComponent(value) + "; path=/";
89
+ document.cookie = new_cookie_val;
90
+ } catch (err) {
91
+ return;
92
+ }
93
+ },
94
+ removeItem(name) {
95
+ try {
96
+ cookieStore.setItem(name, "");
97
+ } catch (err) {
98
+ return;
99
+ }
100
+ },
101
+ clear() {
102
+ document.cookie = "";
103
+ },
104
+ getAllKeys() {
105
+ const ca = document.cookie.split(";");
106
+ const keys = [];
107
+ for (let i = 0; i < ca.length; i++) {
108
+ let c = ca[i];
109
+ while (c.charAt(0) == " ") c = c.substring(1, c.length);
110
+ keys.push(c.split("=")[0]);
111
+ }
112
+ return keys;
113
+ }
221
114
  };
222
- const createStorageLike = store => {
223
- return {
224
- getItem(key) {
225
- return store.getItem(key);
226
- },
227
- setItem(key, value) {
228
- store.setItem(key, value);
229
- },
230
- removeItem(key) {
231
- store.removeItem(key);
232
- },
233
- clear() {
234
- store.clear();
235
- },
236
- getAllKeys() {
237
- const keys = [];
238
- for (const key in localStorage) {
239
- keys.push(key);
240
- }
241
- return keys;
242
- }
243
- };
115
+ const createStorageLike = (store) => {
116
+ return {
117
+ getItem(key) {
118
+ return store.getItem(key);
119
+ },
120
+ setItem(key, value) {
121
+ store.setItem(key, value);
122
+ },
123
+ removeItem(key) {
124
+ store.removeItem(key);
125
+ },
126
+ clear() {
127
+ store.clear();
128
+ },
129
+ getAllKeys() {
130
+ const keys = [];
131
+ for (const key in localStorage) keys.push(key);
132
+ return keys;
133
+ }
134
+ };
244
135
  };
245
- const checkStoreIsSupported = (storage, key = '__mplssupport__') => {
246
- try {
247
- const val = 'xyz';
248
- storage.setItem(key, val);
249
- if (storage.getItem(key) !== val) {
250
- return false;
251
- }
252
- storage.removeItem(key);
253
- return true;
254
- } catch (err) {
255
- return false;
256
- }
136
+ const checkStoreIsSupported = (storage, key = "__mplssupport__") => {
137
+ try {
138
+ const val = "xyz";
139
+ storage.setItem(key, val);
140
+ if (storage.getItem(key) !== val) return false;
141
+ storage.removeItem(key);
142
+ return true;
143
+ } catch (err) {
144
+ return false;
145
+ }
257
146
  };
258
- let localStore = undefined;
259
- let sessionStore = undefined;
147
+ let localStore = void 0;
148
+ let sessionStore = void 0;
260
149
  const createMemoryStorage = () => {
261
- const _cache = {};
262
- const store = {
263
- getItem(key) {
264
- return _cache[key];
265
- },
266
- setItem(key, value) {
267
- _cache[key] = value !== null ? value : undefined;
268
- },
269
- removeItem(key) {
270
- delete _cache[key];
271
- },
272
- clear() {
273
- for (const key in _cache) {
274
- delete _cache[key];
275
- }
276
- },
277
- getAllKeys() {
278
- const keys = [];
279
- for (const key in _cache) {
280
- keys.push(key);
281
- }
282
- return keys;
283
- }
284
- };
285
- return store;
150
+ const _cache = {};
151
+ return {
152
+ getItem(key) {
153
+ return _cache[key];
154
+ },
155
+ setItem(key, value) {
156
+ _cache[key] = value !== null ? value : void 0;
157
+ },
158
+ removeItem(key) {
159
+ delete _cache[key];
160
+ },
161
+ clear() {
162
+ for (const key in _cache) delete _cache[key];
163
+ },
164
+ getAllKeys() {
165
+ const keys = [];
166
+ for (const key in _cache) keys.push(key);
167
+ return keys;
168
+ }
169
+ };
286
170
  };
287
171
  const getStorage = (type, window) => {
288
- if (window) {
289
- if (!localStore) {
290
- const _localStore = createStorageLike(window.localStorage);
291
- localStore = checkStoreIsSupported(_localStore) ? _localStore : undefined;
292
- }
293
- if (!sessionStore) {
294
- const _sessionStore = createStorageLike(window.sessionStorage);
295
- sessionStore = checkStoreIsSupported(_sessionStore) ? _sessionStore : undefined;
296
- }
297
- }
298
- switch (type) {
299
- case 'cookie':
300
- return cookieStore || localStore || sessionStore || createMemoryStorage();
301
- case 'localStorage':
302
- return localStore || sessionStore || createMemoryStorage();
303
- case 'sessionStorage':
304
- return sessionStore || createMemoryStorage();
305
- case 'memory':
306
- return createMemoryStorage();
307
- default:
308
- return createMemoryStorage();
309
- }
172
+ if (window) {
173
+ if (!localStore) {
174
+ const _localStore = createStorageLike(window.localStorage);
175
+ localStore = checkStoreIsSupported(_localStore) ? _localStore : void 0;
176
+ }
177
+ if (!sessionStore) {
178
+ const _sessionStore = createStorageLike(window.sessionStorage);
179
+ sessionStore = checkStoreIsSupported(_sessionStore) ? _sessionStore : void 0;
180
+ }
181
+ }
182
+ switch (type) {
183
+ case "cookie": return cookieStore || localStore || sessionStore || createMemoryStorage();
184
+ case "localStorage": return localStore || sessionStore || createMemoryStorage();
185
+ case "sessionStorage": return sessionStore || createMemoryStorage();
186
+ case "memory": return createMemoryStorage();
187
+ default: return createMemoryStorage();
188
+ }
310
189
  };
311
-
312
- // Portions of this file are derived from getsentry/sentry-javascript
313
- // Copyright (c) 2012 Functional Software, Inc. dba Sentry
314
- // Licensed under the MIT License: https://github.com/getsentry/sentry-javascript/blob/develop/LICENSE
315
- // import { patch } from 'rrweb/typings/utils'
316
- // copied from: https://github.com/PostHog/posthog-js/blob/main/src/extensions/replay/rrweb-plugins/patch.ts
317
- // which was copied from https://github.com/rrweb-io/rrweb/blob/8aea5b00a4dfe5a6f59bd2ae72bb624f45e51e81/packages/rrweb/src/utils.ts#L129
318
- // copied from: https://github.com/PostHog/posthog-js/blob/main/react/src/utils/type-utils.ts#L4
319
- const isFunction = function (f) {
320
- return typeof f === 'function';
190
+ //#endregion
191
+ //#region src/patch.ts
192
+ const isFunction = function(f) {
193
+ return typeof f === "function";
321
194
  };
322
195
  function patch(source, name, replacement) {
323
- try {
324
- if (!(name in source)) {
325
- return () => {
326
- //
327
- };
328
- }
329
- const original = source[name];
330
- const wrapped = replacement(original);
331
- // Make sure it's a function first, as we need to attach an empty prototype for `defineProperties` to work
332
- // otherwise it'll throw "TypeError: Object.defineProperties called on non-object"
333
- if (isFunction(wrapped)) {
334
- wrapped.prototype = wrapped.prototype || {};
335
- Object.defineProperties(wrapped, {
336
- __posthog_wrapped__: {
337
- enumerable: false,
338
- value: true
339
- }
340
- });
341
- }
342
- source[name] = wrapped;
343
- return () => {
344
- source[name] = original;
345
- };
346
- } catch {
347
- return () => {
348
- //
349
- };
350
- // This can throw when multiple instrumentation layers try to wrap the same global object,
351
- // such as XMLHttpRequest, and redefine the same non-configurable wrapper marker.
352
- }
353
- }
354
-
355
- class PostHog extends core.PostHogCore {
356
- constructor(apiKey, options) {
357
- super(apiKey, options);
358
- this._lastPathname = '';
359
- // posthog-js stores options in one object on
360
- this._storageKey = options?.persistence_name ? `ph_${options.persistence_name}` : `ph_${apiKey}_posthog`;
361
- this._disableCaptureUrlHashes = options?.disable_capture_url_hashes ?? false;
362
- this._storage = getStorage(options?.persistence || 'localStorage', this.getWindow());
363
- this.setupBootstrap(options);
364
- if (options?.preloadFeatureFlags !== false) {
365
- this.reloadFeatureFlags();
366
- }
367
- if (options?.captureHistoryEvents && typeof window !== 'undefined') {
368
- this._lastPathname = window?.location?.pathname || '';
369
- this.setupHistoryEventTracking();
370
- }
371
- }
372
- getWindow() {
373
- return typeof window !== 'undefined' ? window : undefined;
374
- }
375
- getPersistedProperty(key) {
376
- if (!this._storageCache) {
377
- this._storageCache = JSON.parse(this._storage.getItem(this._storageKey) || '{}') || {};
378
- }
379
- return this._storageCache[key];
380
- }
381
- setPersistedProperty(key, value) {
382
- if (!this._storageCache) {
383
- this._storageCache = JSON.parse(this._storage.getItem(this._storageKey) || '{}') || {};
384
- }
385
- if (value === null) {
386
- delete this._storageCache[key];
387
- } else {
388
- this._storageCache[key] = value;
389
- }
390
- this._storage.setItem(this._storageKey, JSON.stringify(this._storageCache));
391
- }
392
- fetch(url, options) {
393
- const fetchFn = core.getFetch();
394
- if (!fetchFn) {
395
- // error will be handled by the caller (fetchWithRetry)
396
- return Promise.reject(new Error('Fetch API is not available in this environment.'));
397
- }
398
- return fetchFn(url, options);
399
- }
400
- getLibraryId() {
401
- return 'posthog-js-lite';
402
- }
403
- getLibraryVersion() {
404
- return version;
405
- }
406
- getCustomUserAgent() {
407
- return;
408
- }
409
- getCommonEventProperties() {
410
- return {
411
- ...super.getCommonEventProperties(),
412
- ...getContext(this.getWindow(), this._disableCaptureUrlHashes)
413
- };
414
- }
415
- setupHistoryEventTracking() {
416
- const window = this.getWindow();
417
- if (!window) {
418
- return;
419
- }
420
- // Old fashioned, we could also use arrow functions but I think the closure for a patch is more reliable
421
- const self = this;
422
- patch(window.history, 'pushState', originalPushState => {
423
- return function patchedPushState(state, title, url) {
424
- ;
425
- originalPushState.call(this, state, title, url);
426
- self.captureNavigationEvent('pushState');
427
- };
428
- });
429
- patch(window.history, 'replaceState', originalReplaceState => {
430
- return function patchedReplaceState(state, title, url) {
431
- ;
432
- originalReplaceState.call(this, state, title, url);
433
- self.captureNavigationEvent('replaceState');
434
- };
435
- });
436
- // For popstate we need to listen to the event instead of overriding a method
437
- window.addEventListener('popstate', () => {
438
- this.captureNavigationEvent('popstate');
439
- });
440
- }
441
- captureNavigationEvent(navigationType) {
442
- const window = this.getWindow();
443
- if (!window) {
444
- return;
445
- }
446
- const currentPathname = window.location.pathname;
447
- // Only capture pageview if the pathname has changed
448
- if (currentPathname !== this._lastPathname) {
449
- this.capture('$pageview', {
450
- navigation_type: navigationType
451
- });
452
- this._lastPathname = currentPathname;
453
- }
454
- }
196
+ try {
197
+ if (!(name in source)) return () => {};
198
+ const original = source[name];
199
+ const wrapped = replacement(original);
200
+ if (isFunction(wrapped)) {
201
+ wrapped.prototype = wrapped.prototype || {};
202
+ Object.defineProperties(wrapped, { __posthog_wrapped__: {
203
+ enumerable: false,
204
+ value: true
205
+ } });
206
+ }
207
+ source[name] = wrapped;
208
+ return () => {
209
+ source[name] = original;
210
+ };
211
+ } catch {
212
+ return () => {};
213
+ }
455
214
  }
456
-
215
+ //#endregion
216
+ //#region src/posthog-web.ts
217
+ var PostHog = class extends _posthog_core.PostHogCore {
218
+ constructor(apiKey, options) {
219
+ super(apiKey, options);
220
+ this._lastPathname = "";
221
+ this._storageKey = options?.persistence_name ? `ph_${options.persistence_name}` : `ph_${apiKey}_posthog`;
222
+ this._disableCaptureUrlHashes = options?.disable_capture_url_hashes ?? false;
223
+ this._storage = getStorage(options?.persistence || "localStorage", this.getWindow());
224
+ this.setupBootstrap(options);
225
+ if (options?.preloadFeatureFlags !== false) this.reloadFeatureFlags();
226
+ if (options?.captureHistoryEvents && typeof window !== "undefined") {
227
+ this._lastPathname = window?.location?.pathname || "";
228
+ this.setupHistoryEventTracking();
229
+ }
230
+ }
231
+ getWindow() {
232
+ return typeof window !== "undefined" ? window : void 0;
233
+ }
234
+ getPersistedProperty(key) {
235
+ if (!this._storageCache) this._storageCache = JSON.parse(this._storage.getItem(this._storageKey) || "{}") || {};
236
+ return this._storageCache[key];
237
+ }
238
+ setPersistedProperty(key, value) {
239
+ if (!this._storageCache) this._storageCache = JSON.parse(this._storage.getItem(this._storageKey) || "{}") || {};
240
+ if (value === null) delete this._storageCache[key];
241
+ else this._storageCache[key] = value;
242
+ this._storage.setItem(this._storageKey, JSON.stringify(this._storageCache));
243
+ }
244
+ fetch(url, options) {
245
+ const fetchFn = (0, _posthog_core.getFetch)();
246
+ if (!fetchFn) return Promise.reject(/* @__PURE__ */ new Error("Fetch API is not available in this environment."));
247
+ return fetchFn(url, options);
248
+ }
249
+ getLibraryId() {
250
+ return "posthog-js-lite";
251
+ }
252
+ getLibraryVersion() {
253
+ return version;
254
+ }
255
+ getCustomUserAgent() {}
256
+ getCommonEventProperties() {
257
+ return {
258
+ ...super.getCommonEventProperties(),
259
+ ...getContext(this.getWindow(), this._disableCaptureUrlHashes)
260
+ };
261
+ }
262
+ setupHistoryEventTracking() {
263
+ const window = this.getWindow();
264
+ if (!window) return;
265
+ const self = this;
266
+ patch(window.history, "pushState", (originalPushState) => {
267
+ return function patchedPushState(state, title, url) {
268
+ originalPushState.call(this, state, title, url);
269
+ self.captureNavigationEvent("pushState");
270
+ };
271
+ });
272
+ patch(window.history, "replaceState", (originalReplaceState) => {
273
+ return function patchedReplaceState(state, title, url) {
274
+ originalReplaceState.call(this, state, title, url);
275
+ self.captureNavigationEvent("replaceState");
276
+ };
277
+ });
278
+ window.addEventListener("popstate", () => {
279
+ this.captureNavigationEvent("popstate");
280
+ });
281
+ }
282
+ captureNavigationEvent(navigationType) {
283
+ const window = this.getWindow();
284
+ if (!window) return;
285
+ const currentPathname = window.location.pathname;
286
+ if (currentPathname !== this._lastPathname) {
287
+ this.capture("$pageview", { navigation_type: navigationType });
288
+ this._lastPathname = currentPathname;
289
+ }
290
+ }
291
+ };
292
+ //#endregion
293
+ //#region src/index.ts
294
+ var src_default = PostHog;
295
+ //#endregion
457
296
  exports.PostHog = PostHog;
458
- exports.default = PostHog;
459
- //# sourceMappingURL=index.cjs.map
297
+ exports.default = src_default;
298
+
299
+ //# sourceMappingURL=index.cjs.map