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