posthog-js-lite 4.12.1 → 4.13.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/LICENSE +64 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -4
- package/src/version.ts +1 -1
package/LICENSE
CHANGED
|
@@ -217,6 +217,70 @@ limitations under the License.
|
|
|
217
217
|
limitations under the License.
|
|
218
218
|
|
|
219
219
|
|
|
220
|
+
---
|
|
221
|
+
|
|
222
|
+
The @posthog/core package in packages/core is licensed under the MIT License below.
|
|
223
|
+
Third-party code remains subject to its respective notices in this file.
|
|
224
|
+
This owning notice is preserved from posthog-js-lite, from which @posthog/core was migrated:
|
|
225
|
+
https://github.com/PostHog/posthog-js-lite/blob/5fb7bee96f739b243dfea5589e2027f16629e8cd/LICENSE
|
|
226
|
+
|
|
227
|
+
MIT License
|
|
228
|
+
|
|
229
|
+
Copyright (c) 2022 PostHog (part of Hiberly Inc)
|
|
230
|
+
|
|
231
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
232
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
233
|
+
in the Software without restriction, including without limitation the rights
|
|
234
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
235
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
236
|
+
furnished to do so, subject to the following conditions:
|
|
237
|
+
|
|
238
|
+
The above copyright notice and this permission notice shall be included in all
|
|
239
|
+
copies or substantial portions of the Software.
|
|
240
|
+
|
|
241
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
242
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
243
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
244
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
245
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
246
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
247
|
+
SOFTWARE.
|
|
248
|
+
|
|
249
|
+
---
|
|
250
|
+
|
|
251
|
+
packages/core/src/error-tracking/parsers/index.ts and its compiled distributions contain code
|
|
252
|
+
derived from TraceKit. This license only applies to the relevant code.
|
|
253
|
+
|
|
254
|
+
MIT License
|
|
255
|
+
|
|
256
|
+
Copyright (c) 2013 Onur Can Cakmak onur.cakmak@gmail.com and all TraceKit contributors.
|
|
257
|
+
|
|
258
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
|
259
|
+
software and associated documentation files(the 'Software'), to deal in the Software
|
|
260
|
+
without restriction, including without limitation the rights to use, copy, modify,
|
|
261
|
+
merge, publish, distribute, sublicense, and / or sell copies of the Software, and to
|
|
262
|
+
permit persons to whom the Software is furnished to do so, subject to the following
|
|
263
|
+
conditions:
|
|
264
|
+
|
|
265
|
+
The above copyright notice and this permission notice shall be included in all copies
|
|
266
|
+
or substantial portions of the Software.
|
|
267
|
+
|
|
268
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
269
|
+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
270
|
+
PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
271
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
272
|
+
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
|
273
|
+
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
274
|
+
|
|
275
|
+
---
|
|
276
|
+
|
|
277
|
+
packages/core/src/vendor/uuidv7.ts and its compiled distributions contain code from uuidv7:
|
|
278
|
+
https://github.com/LiosK/uuidv7/blob/f30b7a7faff73afbce0b27a46c638310f96912ba/src/index.ts
|
|
279
|
+
|
|
280
|
+
Copyright 2021-2023 LiosK
|
|
281
|
+
|
|
282
|
+
This code is licensed under the Apache License, Version 2.0, reproduced above.
|
|
283
|
+
|
|
220
284
|
---
|
|
221
285
|
|
|
222
286
|
Some files in this codebase contain code from getsentry/sentry-javascript.
|
package/dist/index.cjs
CHANGED
|
@@ -4,7 +4,7 @@ Object.defineProperties(exports, {
|
|
|
4
4
|
});
|
|
5
5
|
let _posthog_core = require("@posthog/core");
|
|
6
6
|
//#region src/version.ts
|
|
7
|
-
const version = "4.
|
|
7
|
+
const version = "4.13.0";
|
|
8
8
|
//#endregion
|
|
9
9
|
//#region src/context.ts
|
|
10
10
|
function getContext(window, disableCaptureUrlHashes = false) {
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["detectWebviewApp","detectBrowser","stripUrlHash","detectBrowserVersion","currentTimestamp","PostHogCore","getFetch"],"sources":["../src/version.ts","../src/context.ts","../src/storage.ts","../src/patch.ts","../src/posthog-web.ts","../src/index.ts"],"sourcesContent":["export const version = \"4.12.1\"\n","import { currentTimestamp, detectBrowser, detectBrowserVersion, detectWebviewApp, stripUrlHash } from '@posthog/core'\nimport { version } from './version'\n\nexport function getContext(window: Window | undefined, disableCaptureUrlHashes: boolean = false): any {\n let context = {}\n if (window?.navigator) {\n const userAgent = window.navigator.userAgent\n const osValue = os(window)\n const [webviewApp, webviewAppVersion] = detectWebviewApp(userAgent)\n context = {\n ...context,\n ...(osValue !== undefined && { $os: osValue }),\n $browser: detectBrowser(userAgent, window.navigator.vendor),\n ...(webviewApp && { $webview_app: webviewApp }),\n ...(webviewAppVersion && { $webview_app_version: webviewAppVersion }),\n $referrer: window.document.referrer,\n $referring_domain: referringDomain(window.document.referrer),\n $device: device(userAgent),\n $current_url: disableCaptureUrlHashes ? stripUrlHash(window.location.href) : window.location.href,\n $host: window.location.host,\n $pathname: window.location.pathname,\n $browser_version: detectBrowserVersion(userAgent, window.navigator.vendor),\n $screen_height: window.screen.height,\n $screen_width: window.screen.width,\n $screen_dpr: window.devicePixelRatio,\n }\n }\n\n context = {\n ...context,\n $lib: 'js',\n $lib_version: version,\n $insert_id: Math.random().toString(36).substring(2, 10) + Math.random().toString(36).substring(2, 10),\n $time: currentTimestamp() / 1000, // epoch time in seconds\n }\n return context // TODO: strip empty props?\n}\n\nfunction os(window: Window | undefined): string | undefined {\n if (!window?.navigator) {\n return undefined\n }\n const a = window.navigator.userAgent\n if (/Windows/i.test(a)) {\n if (/Phone/.test(a) || /WPDesktop/.test(a)) {\n return 'Windows Phone'\n }\n return 'Windows'\n } else if (/(iPhone|iPad|iPod)/.test(a)) {\n return 'iOS'\n } else if (/Android/.test(a)) {\n return 'Android'\n } else if (/(BlackBerry|PlayBook|BB10)/i.test(a)) {\n return 'BlackBerry'\n } else if (/Mac/i.test(a)) {\n return 'Mac OS X'\n } else if (/Linux/.test(a)) {\n return 'Linux'\n } else if (/CrOS/.test(a)) {\n return 'Chrome OS'\n } else {\n return undefined\n }\n}\n\nfunction device(userAgent: string): string {\n if (/Windows Phone/i.test(userAgent) || /WPDesktop/.test(userAgent)) {\n return 'Windows Phone'\n } else if (/iPad/.test(userAgent)) {\n return 'iPad'\n } else if (/iPod/.test(userAgent)) {\n return 'iPod Touch'\n } else if (/iPhone/.test(userAgent)) {\n return 'iPhone'\n } else if (/(BlackBerry|PlayBook|BB10)/i.test(userAgent)) {\n return 'BlackBerry'\n } else if (/Android/.test(userAgent)) {\n return 'Android'\n } else {\n return ''\n }\n}\n\nfunction referringDomain(referrer: string): string {\n const split = referrer.split('/')\n if (split.length >= 3) {\n return split[2]\n }\n return ''\n}\n","import { PostHogOptions } from './types'\n\nexport type PostHogStorage = {\n getItem: (key: string) => string | null | undefined\n setItem: (key: string, value: string) => void\n removeItem: (key: string) => void\n clear: () => void\n getAllKeys: () => readonly string[]\n}\n\n// Methods partially borrowed from quirksmode.org/js/cookies.html\nexport const cookieStore: PostHogStorage = {\n getItem(key) {\n try {\n const nameEQ = key + '='\n const ca = document.cookie.split(';')\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 setItem(key: string, value: string) {\n try {\n const cdomain = '',\n expires = '',\n secure = ''\n\n const new_cookie_val = key + '=' + encodeURIComponent(value) + expires + '; path=/' + cdomain + secure\n document.cookie = new_cookie_val\n } catch (err) {\n return\n }\n },\n\n removeItem(name) {\n try {\n cookieStore.setItem(name, '')\n } catch (err) {\n return\n }\n },\n clear() {\n document.cookie = ''\n },\n getAllKeys() {\n const ca = document.cookie.split(';')\n const keys = []\n\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 keys.push(c.split('=')[0])\n }\n\n return keys\n },\n}\n\nconst createStorageLike = (store: any): PostHogStorage => {\n return {\n getItem(key) {\n return store.getItem(key)\n },\n\n setItem(key, value) {\n store.setItem(key, value)\n },\n\n removeItem(key) {\n store.removeItem(key)\n },\n clear() {\n store.clear()\n },\n getAllKeys() {\n const keys = []\n for (const key in localStorage) {\n keys.push(key)\n }\n return keys\n },\n }\n}\n\nconst checkStoreIsSupported = (storage: PostHogStorage, key = '__mplssupport__'): boolean => {\n try {\n const val = 'xyz'\n storage.setItem(key, val)\n if (storage.getItem(key) !== val) {\n return false\n }\n storage.removeItem(key)\n return true\n } catch (err) {\n return false\n }\n}\n\nlet localStore: PostHogStorage | undefined = undefined\nlet sessionStore: PostHogStorage | undefined = undefined\n\nconst createMemoryStorage = (): PostHogStorage => {\n const _cache: { [key: string]: any | undefined } = {}\n\n const store: PostHogStorage = {\n getItem(key) {\n return _cache[key]\n },\n\n setItem(key, value) {\n _cache[key] = value !== null ? value : undefined\n },\n\n removeItem(key) {\n delete _cache[key]\n },\n clear() {\n for (const key in _cache) {\n delete _cache[key]\n }\n },\n getAllKeys() {\n const keys = []\n for (const key in _cache) {\n keys.push(key)\n }\n return keys\n },\n }\n return store\n}\n\nexport const getStorage = (type: PostHogOptions['persistence'], window: Window | undefined): PostHogStorage => {\n if (window) {\n if (!localStore) {\n const _localStore = createStorageLike(window.localStorage)\n localStore = checkStoreIsSupported(_localStore) ? _localStore : undefined\n }\n\n if (!sessionStore) {\n const _sessionStore = createStorageLike(window.sessionStorage)\n sessionStore = checkStoreIsSupported(_sessionStore) ? _sessionStore : undefined\n }\n }\n\n switch (type) {\n case 'cookie':\n return cookieStore || localStore || sessionStore || createMemoryStorage()\n case 'localStorage':\n return localStore || sessionStore || createMemoryStorage()\n case 'sessionStorage':\n return sessionStore || createMemoryStorage()\n case 'memory':\n return createMemoryStorage()\n default:\n return createMemoryStorage()\n }\n}\n","// Portions of this file are derived from getsentry/sentry-javascript\n// Copyright (c) 2012 Functional Software, Inc. dba Sentry\n// Licensed under the MIT License: https://github.com/getsentry/sentry-javascript/blob/develop/LICENSE\n\n// import { patch } from 'rrweb/typings/utils'\n// copied from: https://github.com/PostHog/posthog-js/blob/main/src/extensions/replay/rrweb-plugins/patch.ts\n// which was copied from https://github.com/rrweb-io/rrweb/blob/8aea5b00a4dfe5a6f59bd2ae72bb624f45e51e81/packages/rrweb/src/utils.ts#L129\n\n// copied from: https://github.com/PostHog/posthog-js/blob/main/react/src/utils/type-utils.ts#L4\nexport const isFunction = function (f: any): f is (...args: any[]) => any {\n return typeof f === 'function'\n}\n\nexport function patch(\n source: { [key: string]: any },\n name: string,\n replacement: (...args: unknown[]) => unknown\n): () => void {\n try {\n if (!(name in source)) {\n return () => {\n //\n }\n }\n\n const original = source[name] as () => unknown\n const wrapped = replacement(original)\n\n // Make sure it's a function first, as we need to attach an empty prototype for `defineProperties` to work\n // otherwise it'll throw \"TypeError: Object.defineProperties called on non-object\"\n if (isFunction(wrapped)) {\n wrapped.prototype = wrapped.prototype || {}\n Object.defineProperties(wrapped, {\n __posthog_wrapped__: {\n enumerable: false,\n value: true,\n },\n })\n }\n\n source[name] = wrapped\n\n return () => {\n source[name] = original\n }\n } catch {\n return () => {\n //\n }\n // This can throw when multiple instrumentation layers try to wrap the same global object,\n // such as XMLHttpRequest, and redefine the same non-configurable wrapper marker.\n }\n}\n","import { version } from './version'\n\nimport { PostHogCore, getFetch } from '@posthog/core'\nimport type {\n PostHogEventProperties,\n PostHogFetchOptions,\n PostHogFetchResponse,\n PostHogPersistedProperty,\n} from '@posthog/core'\n\nimport { getContext } from './context'\nimport { PostHogStorage, getStorage } from './storage'\nimport { PostHogOptions } from './types'\nimport { patch } from './patch'\n\nexport class PostHog extends PostHogCore {\n private _storage: PostHogStorage\n private _storageCache: any\n private _storageKey: string\n private _lastPathname: string = ''\n private _disableCaptureUrlHashes: boolean\n\n constructor(apiKey: string, options?: PostHogOptions) {\n super(apiKey, options)\n\n // posthog-js stores options in one object on\n this._storageKey = options?.persistence_name ? `ph_${options.persistence_name}` : `ph_${apiKey}_posthog`\n\n this._disableCaptureUrlHashes = options?.disable_capture_url_hashes ?? false\n\n this._storage = getStorage(options?.persistence || 'localStorage', this.getWindow())\n this.setupBootstrap(options)\n\n if (options?.preloadFeatureFlags !== false) {\n this.reloadFeatureFlags()\n }\n\n if (options?.captureHistoryEvents && typeof window !== 'undefined') {\n this._lastPathname = window?.location?.pathname || ''\n this.setupHistoryEventTracking()\n }\n }\n\n private getWindow(): Window | undefined {\n return typeof window !== 'undefined' ? window : undefined\n }\n\n getPersistedProperty<T>(key: PostHogPersistedProperty): T | undefined {\n if (!this._storageCache) {\n this._storageCache = JSON.parse(this._storage.getItem(this._storageKey) || '{}') || {}\n }\n\n return this._storageCache[key]\n }\n\n setPersistedProperty<T>(key: PostHogPersistedProperty, value: T | null): void {\n if (!this._storageCache) {\n this._storageCache = JSON.parse(this._storage.getItem(this._storageKey) || '{}') || {}\n }\n\n if (value === null) {\n delete this._storageCache[key]\n } else {\n this._storageCache[key] = value\n }\n\n this._storage.setItem(this._storageKey, JSON.stringify(this._storageCache))\n }\n\n fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse> {\n const fetchFn = getFetch()\n\n if (!fetchFn) {\n // error will be handled by the caller (fetchWithRetry)\n return Promise.reject(new Error('Fetch API is not available in this environment.'))\n }\n\n return fetchFn(url, options)\n }\n\n getLibraryId(): string {\n return 'posthog-js-lite'\n }\n\n getLibraryVersion(): string {\n return version\n }\n\n getCustomUserAgent(): void {\n return\n }\n\n getCommonEventProperties(): PostHogEventProperties {\n return {\n ...super.getCommonEventProperties(),\n ...getContext(this.getWindow(), this._disableCaptureUrlHashes),\n }\n }\n\n private setupHistoryEventTracking(): void {\n const window = this.getWindow()\n if (!window) {\n return\n }\n\n // Old fashioned, we could also use arrow functions but I think the closure for a patch is more reliable\n const self = this\n\n patch(window.history, 'pushState', (originalPushState) => {\n return function patchedPushState(this: History, state: any, title: string, url?: string | URL | null): void {\n ;(originalPushState as History['pushState']).call(this, state, title, url)\n self.captureNavigationEvent('pushState')\n }\n })\n\n patch(window.history, 'replaceState', (originalReplaceState) => {\n return function patchedReplaceState(this: History, state: any, title: string, url?: string | URL | null): void {\n ;(originalReplaceState as History['replaceState']).call(this, state, title, url)\n self.captureNavigationEvent('replaceState')\n }\n })\n\n // For popstate we need to listen to the event instead of overriding a method\n window.addEventListener('popstate', () => {\n this.captureNavigationEvent('popstate')\n })\n }\n\n private captureNavigationEvent(navigationType: 'pushState' | 'replaceState' | 'popstate'): void {\n const window = this.getWindow()\n if (!window) {\n return\n }\n\n const currentPathname = window.location.pathname\n\n // Only capture pageview if the pathname has changed\n if (currentPathname !== this._lastPathname) {\n this.capture('$pageview', { navigation_type: navigationType })\n this._lastPathname = currentPathname\n }\n }\n}\n","import { PostHog } from './posthog-web'\n\nexport default PostHog\n\nexport * from './posthog-web'\n"],"mappings":";;;;;;AAAA,MAAa,UAAU;;;ACGvB,SAAgB,WAAW,QAA4B,0BAAmC,OAAY;CACpG,IAAI,UAAU,CAAC;CACf,IAAI,QAAQ,WAAW;EACrB,MAAM,YAAY,OAAO,UAAU;EACnC,MAAM,UAAU,GAAG,MAAM;EACzB,MAAM,CAAC,YAAY,sBAAA,GAAqBA,cAAAA,iBAAAA,CAAiB,SAAS;EAClE,UAAU;GACR,GAAG;GACH,GAAI,YAAY,KAAA,KAAa,EAAE,KAAK,QAAQ;GAC5C,WAAA,GAAUC,cAAAA,cAAAA,CAAc,WAAW,OAAO,UAAU,MAAM;GAC1D,GAAI,cAAc,EAAE,cAAc,WAAW;GAC7C,GAAI,qBAAqB,EAAE,sBAAsB,kBAAkB;GACnE,WAAW,OAAO,SAAS;GAC3B,mBAAmB,gBAAgB,OAAO,SAAS,QAAQ;GAC3D,SAAS,OAAO,SAAS;GACzB,cAAc,2BAAA,GAA0BC,cAAAA,aAAAA,CAAa,OAAO,SAAS,IAAI,IAAI,OAAO,SAAS;GAC7F,OAAO,OAAO,SAAS;GACvB,WAAW,OAAO,SAAS;GAC3B,mBAAA,GAAkBC,cAAAA,qBAAAA,CAAqB,WAAW,OAAO,UAAU,MAAM;GACzE,gBAAgB,OAAO,OAAO;GAC9B,eAAe,OAAO,OAAO;GAC7B,aAAa,OAAO;EACtB;CACF;CAEA,UAAU;EACR,GAAG;EACH,MAAM;EACN,cAAc;EACd,YAAY,KAAK,OAAO,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,UAAU,GAAG,EAAE,IAAI,KAAK,OAAO,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,UAAU,GAAG,EAAE;EACpG,QAAA,GAAOC,cAAAA,iBAAAA,CAAiB,IAAI;CAC9B;CACA,OA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","getFetch"],"sources":["../src/version.ts","../src/context.ts","../src/storage.ts","../src/patch.ts","../src/posthog-web.ts","../src/index.ts"],"sourcesContent":["export const version = \"4.13.0\"\n","import { currentTimestamp, detectBrowser, detectBrowserVersion, detectWebviewApp, stripUrlHash } from '@posthog/core'\nimport { version } from './version'\n\nexport function getContext(window: Window | undefined, disableCaptureUrlHashes: boolean = false): any {\n let context = {}\n if (window?.navigator) {\n const userAgent = window.navigator.userAgent\n const osValue = os(window)\n const [webviewApp, webviewAppVersion] = detectWebviewApp(userAgent)\n context = {\n ...context,\n ...(osValue !== undefined && { $os: osValue }),\n $browser: detectBrowser(userAgent, window.navigator.vendor),\n ...(webviewApp && { $webview_app: webviewApp }),\n ...(webviewAppVersion && { $webview_app_version: webviewAppVersion }),\n $referrer: window.document.referrer,\n $referring_domain: referringDomain(window.document.referrer),\n $device: device(userAgent),\n $current_url: disableCaptureUrlHashes ? stripUrlHash(window.location.href) : window.location.href,\n $host: window.location.host,\n $pathname: window.location.pathname,\n $browser_version: detectBrowserVersion(userAgent, window.navigator.vendor),\n $screen_height: window.screen.height,\n $screen_width: window.screen.width,\n $screen_dpr: window.devicePixelRatio,\n }\n }\n\n context = {\n ...context,\n $lib: 'js',\n $lib_version: version,\n $insert_id: Math.random().toString(36).substring(2, 10) + Math.random().toString(36).substring(2, 10),\n $time: currentTimestamp() / 1000, // epoch time in seconds\n }\n return context // TODO: strip empty props?\n}\n\nfunction os(window: Window | undefined): string | undefined {\n if (!window?.navigator) {\n return undefined\n }\n const a = window.navigator.userAgent\n if (/Windows/i.test(a)) {\n if (/Phone/.test(a) || /WPDesktop/.test(a)) {\n return 'Windows Phone'\n }\n return 'Windows'\n } else if (/(iPhone|iPad|iPod)/.test(a)) {\n return 'iOS'\n } else if (/Android/.test(a)) {\n return 'Android'\n } else if (/(BlackBerry|PlayBook|BB10)/i.test(a)) {\n return 'BlackBerry'\n } else if (/Mac/i.test(a)) {\n return 'Mac OS X'\n } else if (/Linux/.test(a)) {\n return 'Linux'\n } else if (/CrOS/.test(a)) {\n return 'Chrome OS'\n } else {\n return undefined\n }\n}\n\nfunction device(userAgent: string): string {\n if (/Windows Phone/i.test(userAgent) || /WPDesktop/.test(userAgent)) {\n return 'Windows Phone'\n } else if (/iPad/.test(userAgent)) {\n return 'iPad'\n } else if (/iPod/.test(userAgent)) {\n return 'iPod Touch'\n } else if (/iPhone/.test(userAgent)) {\n return 'iPhone'\n } else if (/(BlackBerry|PlayBook|BB10)/i.test(userAgent)) {\n return 'BlackBerry'\n } else if (/Android/.test(userAgent)) {\n return 'Android'\n } else {\n return ''\n }\n}\n\nfunction referringDomain(referrer: string): string {\n const split = referrer.split('/')\n if (split.length >= 3) {\n return split[2]\n }\n return ''\n}\n","import { PostHogOptions } from './types'\n\nexport type PostHogStorage = {\n getItem: (key: string) => string | null | undefined\n setItem: (key: string, value: string) => void\n removeItem: (key: string) => void\n clear: () => void\n getAllKeys: () => readonly string[]\n}\n\n// Methods partially borrowed from quirksmode.org/js/cookies.html\nexport const cookieStore: PostHogStorage = {\n getItem(key) {\n try {\n const nameEQ = key + '='\n const ca = document.cookie.split(';')\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 setItem(key: string, value: string) {\n try {\n const cdomain = '',\n expires = '',\n secure = ''\n\n const new_cookie_val = key + '=' + encodeURIComponent(value) + expires + '; path=/' + cdomain + secure\n document.cookie = new_cookie_val\n } catch (err) {\n return\n }\n },\n\n removeItem(name) {\n try {\n cookieStore.setItem(name, '')\n } catch (err) {\n return\n }\n },\n clear() {\n document.cookie = ''\n },\n getAllKeys() {\n const ca = document.cookie.split(';')\n const keys = []\n\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 keys.push(c.split('=')[0])\n }\n\n return keys\n },\n}\n\nconst createStorageLike = (store: any): PostHogStorage => {\n return {\n getItem(key) {\n return store.getItem(key)\n },\n\n setItem(key, value) {\n store.setItem(key, value)\n },\n\n removeItem(key) {\n store.removeItem(key)\n },\n clear() {\n store.clear()\n },\n getAllKeys() {\n const keys = []\n for (const key in localStorage) {\n keys.push(key)\n }\n return keys\n },\n }\n}\n\nconst checkStoreIsSupported = (storage: PostHogStorage, key = '__mplssupport__'): boolean => {\n try {\n const val = 'xyz'\n storage.setItem(key, val)\n if (storage.getItem(key) !== val) {\n return false\n }\n storage.removeItem(key)\n return true\n } catch (err) {\n return false\n }\n}\n\nlet localStore: PostHogStorage | undefined = undefined\nlet sessionStore: PostHogStorage | undefined = undefined\n\nconst createMemoryStorage = (): PostHogStorage => {\n const _cache: { [key: string]: any | undefined } = {}\n\n const store: PostHogStorage = {\n getItem(key) {\n return _cache[key]\n },\n\n setItem(key, value) {\n _cache[key] = value !== null ? value : undefined\n },\n\n removeItem(key) {\n delete _cache[key]\n },\n clear() {\n for (const key in _cache) {\n delete _cache[key]\n }\n },\n getAllKeys() {\n const keys = []\n for (const key in _cache) {\n keys.push(key)\n }\n return keys\n },\n }\n return store\n}\n\nexport const getStorage = (type: PostHogOptions['persistence'], window: Window | undefined): PostHogStorage => {\n if (window) {\n if (!localStore) {\n const _localStore = createStorageLike(window.localStorage)\n localStore = checkStoreIsSupported(_localStore) ? _localStore : undefined\n }\n\n if (!sessionStore) {\n const _sessionStore = createStorageLike(window.sessionStorage)\n sessionStore = checkStoreIsSupported(_sessionStore) ? _sessionStore : undefined\n }\n }\n\n switch (type) {\n case 'cookie':\n return cookieStore || localStore || sessionStore || createMemoryStorage()\n case 'localStorage':\n return localStore || sessionStore || createMemoryStorage()\n case 'sessionStorage':\n return sessionStore || createMemoryStorage()\n case 'memory':\n return createMemoryStorage()\n default:\n return createMemoryStorage()\n }\n}\n","// Portions of this file are derived from getsentry/sentry-javascript\n// Copyright (c) 2012 Functional Software, Inc. dba Sentry\n// Licensed under the MIT License: https://github.com/getsentry/sentry-javascript/blob/develop/LICENSE\n\n// import { patch } from 'rrweb/typings/utils'\n// copied from: https://github.com/PostHog/posthog-js/blob/main/src/extensions/replay/rrweb-plugins/patch.ts\n// which was copied from https://github.com/rrweb-io/rrweb/blob/8aea5b00a4dfe5a6f59bd2ae72bb624f45e51e81/packages/rrweb/src/utils.ts#L129\n\n// copied from: https://github.com/PostHog/posthog-js/blob/main/react/src/utils/type-utils.ts#L4\nexport const isFunction = function (f: any): f is (...args: any[]) => any {\n return typeof f === 'function'\n}\n\nexport function patch(\n source: { [key: string]: any },\n name: string,\n replacement: (...args: unknown[]) => unknown\n): () => void {\n try {\n if (!(name in source)) {\n return () => {\n //\n }\n }\n\n const original = source[name] as () => unknown\n const wrapped = replacement(original)\n\n // Make sure it's a function first, as we need to attach an empty prototype for `defineProperties` to work\n // otherwise it'll throw \"TypeError: Object.defineProperties called on non-object\"\n if (isFunction(wrapped)) {\n wrapped.prototype = wrapped.prototype || {}\n Object.defineProperties(wrapped, {\n __posthog_wrapped__: {\n enumerable: false,\n value: true,\n },\n })\n }\n\n source[name] = wrapped\n\n return () => {\n source[name] = original\n }\n } catch {\n return () => {\n //\n }\n // This can throw when multiple instrumentation layers try to wrap the same global object,\n // such as XMLHttpRequest, and redefine the same non-configurable wrapper marker.\n }\n}\n","import { version } from './version'\n\nimport { PostHogCore, getFetch } from '@posthog/core'\nimport type {\n PostHogEventProperties,\n PostHogFetchOptions,\n PostHogFetchResponse,\n PostHogPersistedProperty,\n} from '@posthog/core'\n\nimport { getContext } from './context'\nimport { PostHogStorage, getStorage } from './storage'\nimport { PostHogOptions } from './types'\nimport { patch } from './patch'\n\nexport class PostHog extends PostHogCore {\n private _storage: PostHogStorage\n private _storageCache: any\n private _storageKey: string\n private _lastPathname: string = ''\n private _disableCaptureUrlHashes: boolean\n\n constructor(apiKey: string, options?: PostHogOptions) {\n super(apiKey, options)\n\n // posthog-js stores options in one object on\n this._storageKey = options?.persistence_name ? `ph_${options.persistence_name}` : `ph_${apiKey}_posthog`\n\n this._disableCaptureUrlHashes = options?.disable_capture_url_hashes ?? false\n\n this._storage = getStorage(options?.persistence || 'localStorage', this.getWindow())\n this.setupBootstrap(options)\n\n if (options?.preloadFeatureFlags !== false) {\n this.reloadFeatureFlags()\n }\n\n if (options?.captureHistoryEvents && typeof window !== 'undefined') {\n this._lastPathname = window?.location?.pathname || ''\n this.setupHistoryEventTracking()\n }\n }\n\n private getWindow(): Window | undefined {\n return typeof window !== 'undefined' ? window : undefined\n }\n\n getPersistedProperty<T>(key: PostHogPersistedProperty): T | undefined {\n if (!this._storageCache) {\n this._storageCache = JSON.parse(this._storage.getItem(this._storageKey) || '{}') || {}\n }\n\n return this._storageCache[key]\n }\n\n setPersistedProperty<T>(key: PostHogPersistedProperty, value: T | null): void {\n if (!this._storageCache) {\n this._storageCache = JSON.parse(this._storage.getItem(this._storageKey) || '{}') || {}\n }\n\n if (value === null) {\n delete this._storageCache[key]\n } else {\n this._storageCache[key] = value\n }\n\n this._storage.setItem(this._storageKey, JSON.stringify(this._storageCache))\n }\n\n fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse> {\n const fetchFn = getFetch()\n\n if (!fetchFn) {\n // error will be handled by the caller (fetchWithRetry)\n return Promise.reject(new Error('Fetch API is not available in this environment.'))\n }\n\n return fetchFn(url, options)\n }\n\n getLibraryId(): string {\n return 'posthog-js-lite'\n }\n\n getLibraryVersion(): string {\n return version\n }\n\n getCustomUserAgent(): void {\n return\n }\n\n getCommonEventProperties(): PostHogEventProperties {\n return {\n ...super.getCommonEventProperties(),\n ...getContext(this.getWindow(), this._disableCaptureUrlHashes),\n }\n }\n\n private setupHistoryEventTracking(): void {\n const window = this.getWindow()\n if (!window) {\n return\n }\n\n // Old fashioned, we could also use arrow functions but I think the closure for a patch is more reliable\n const self = this\n\n patch(window.history, 'pushState', (originalPushState) => {\n return function patchedPushState(this: History, state: any, title: string, url?: string | URL | null): void {\n ;(originalPushState as History['pushState']).call(this, state, title, url)\n self.captureNavigationEvent('pushState')\n }\n })\n\n patch(window.history, 'replaceState', (originalReplaceState) => {\n return function patchedReplaceState(this: History, state: any, title: string, url?: string | URL | null): void {\n ;(originalReplaceState as History['replaceState']).call(this, state, title, url)\n self.captureNavigationEvent('replaceState')\n }\n })\n\n // For popstate we need to listen to the event instead of overriding a method\n window.addEventListener('popstate', () => {\n this.captureNavigationEvent('popstate')\n })\n }\n\n private captureNavigationEvent(navigationType: 'pushState' | 'replaceState' | 'popstate'): void {\n const window = this.getWindow()\n if (!window) {\n return\n }\n\n const currentPathname = window.location.pathname\n\n // Only capture pageview if the pathname has changed\n if (currentPathname !== this._lastPathname) {\n this.capture('$pageview', { navigation_type: navigationType })\n this._lastPathname = currentPathname\n }\n }\n}\n","import { PostHog } from './posthog-web'\n\nexport default PostHog\n\nexport * from './posthog-web'\n"],"mappings":";;;;;;AAAA,MAAa,UAAU;;;ACGvB,SAAgB,WAAW,QAA4B,0BAAmC,OAAY;CACpG,IAAI,UAAU,CAAC;CACf,IAAI,QAAQ,WAAW;EACrB,MAAM,YAAY,OAAO,UAAU;EACnC,MAAM,UAAU,GAAG,MAAM;EACzB,MAAM,CAAC,YAAY,sBAAA,GAAqBA,cAAAA,iBAAAA,CAAiB,SAAS;EAClE,UAAU;GACR,GAAG;GACH,GAAI,YAAY,KAAA,KAAa,EAAE,KAAK,QAAQ;GAC5C,WAAA,GAAUC,cAAAA,cAAAA,CAAc,WAAW,OAAO,UAAU,MAAM;GAC1D,GAAI,cAAc,EAAE,cAAc,WAAW;GAC7C,GAAI,qBAAqB,EAAE,sBAAsB,kBAAkB;GACnE,WAAW,OAAO,SAAS;GAC3B,mBAAmB,gBAAgB,OAAO,SAAS,QAAQ;GAC3D,SAAS,OAAO,SAAS;GACzB,cAAc,2BAAA,GAA0BC,cAAAA,aAAAA,CAAa,OAAO,SAAS,IAAI,IAAI,OAAO,SAAS;GAC7F,OAA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AAc;GAC5C;EACF,CAAC;EAGD,OAAO,iBAAiB,kBAAkB;GACxC,KAAK,uBAAuB,UAAU;EACxC,CAAC;CACH;CAEA,uBAA+B,gBAAiE;EAC9F,MAAM,SAAS,KAAK,UAAU;EAC9B,IAAI,CAAC,QACH;EAGF,MAAM,kBAAkB,OAAO,SAAS;EAGxC,IAAI,oBAAoB,KAAK,eAAe;GAC1C,KAAK,QAAQ,aAAa,EAAE,iBAAiB,eAAe,CAAC;GAC7D,KAAK,gBAAgB;EACvB;CACF;AACF;;;AC5IA,IAAA,cAAe"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { PostHogCore, currentTimestamp, detectBrowser, detectBrowserVersion, detectWebviewApp, getFetch, stripUrlHash } from "@posthog/core";
|
|
2
2
|
//#region src/version.ts
|
|
3
|
-
const version = "4.
|
|
3
|
+
const version = "4.13.0";
|
|
4
4
|
//#endregion
|
|
5
5
|
//#region src/context.ts
|
|
6
6
|
function getContext(window, disableCaptureUrlHashes = false) {
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/version.ts","../src/context.ts","../src/storage.ts","../src/patch.ts","../src/posthog-web.ts","../src/index.ts"],"sourcesContent":["export const version = \"4.12.1\"\n","import { currentTimestamp, detectBrowser, detectBrowserVersion, detectWebviewApp, stripUrlHash } from '@posthog/core'\nimport { version } from './version'\n\nexport function getContext(window: Window | undefined, disableCaptureUrlHashes: boolean = false): any {\n let context = {}\n if (window?.navigator) {\n const userAgent = window.navigator.userAgent\n const osValue = os(window)\n const [webviewApp, webviewAppVersion] = detectWebviewApp(userAgent)\n context = {\n ...context,\n ...(osValue !== undefined && { $os: osValue }),\n $browser: detectBrowser(userAgent, window.navigator.vendor),\n ...(webviewApp && { $webview_app: webviewApp }),\n ...(webviewAppVersion && { $webview_app_version: webviewAppVersion }),\n $referrer: window.document.referrer,\n $referring_domain: referringDomain(window.document.referrer),\n $device: device(userAgent),\n $current_url: disableCaptureUrlHashes ? stripUrlHash(window.location.href) : window.location.href,\n $host: window.location.host,\n $pathname: window.location.pathname,\n $browser_version: detectBrowserVersion(userAgent, window.navigator.vendor),\n $screen_height: window.screen.height,\n $screen_width: window.screen.width,\n $screen_dpr: window.devicePixelRatio,\n }\n }\n\n context = {\n ...context,\n $lib: 'js',\n $lib_version: version,\n $insert_id: Math.random().toString(36).substring(2, 10) + Math.random().toString(36).substring(2, 10),\n $time: currentTimestamp() / 1000, // epoch time in seconds\n }\n return context // TODO: strip empty props?\n}\n\nfunction os(window: Window | undefined): string | undefined {\n if (!window?.navigator) {\n return undefined\n }\n const a = window.navigator.userAgent\n if (/Windows/i.test(a)) {\n if (/Phone/.test(a) || /WPDesktop/.test(a)) {\n return 'Windows Phone'\n }\n return 'Windows'\n } else if (/(iPhone|iPad|iPod)/.test(a)) {\n return 'iOS'\n } else if (/Android/.test(a)) {\n return 'Android'\n } else if (/(BlackBerry|PlayBook|BB10)/i.test(a)) {\n return 'BlackBerry'\n } else if (/Mac/i.test(a)) {\n return 'Mac OS X'\n } else if (/Linux/.test(a)) {\n return 'Linux'\n } else if (/CrOS/.test(a)) {\n return 'Chrome OS'\n } else {\n return undefined\n }\n}\n\nfunction device(userAgent: string): string {\n if (/Windows Phone/i.test(userAgent) || /WPDesktop/.test(userAgent)) {\n return 'Windows Phone'\n } else if (/iPad/.test(userAgent)) {\n return 'iPad'\n } else if (/iPod/.test(userAgent)) {\n return 'iPod Touch'\n } else if (/iPhone/.test(userAgent)) {\n return 'iPhone'\n } else if (/(BlackBerry|PlayBook|BB10)/i.test(userAgent)) {\n return 'BlackBerry'\n } else if (/Android/.test(userAgent)) {\n return 'Android'\n } else {\n return ''\n }\n}\n\nfunction referringDomain(referrer: string): string {\n const split = referrer.split('/')\n if (split.length >= 3) {\n return split[2]\n }\n return ''\n}\n","import { PostHogOptions } from './types'\n\nexport type PostHogStorage = {\n getItem: (key: string) => string | null | undefined\n setItem: (key: string, value: string) => void\n removeItem: (key: string) => void\n clear: () => void\n getAllKeys: () => readonly string[]\n}\n\n// Methods partially borrowed from quirksmode.org/js/cookies.html\nexport const cookieStore: PostHogStorage = {\n getItem(key) {\n try {\n const nameEQ = key + '='\n const ca = document.cookie.split(';')\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 setItem(key: string, value: string) {\n try {\n const cdomain = '',\n expires = '',\n secure = ''\n\n const new_cookie_val = key + '=' + encodeURIComponent(value) + expires + '; path=/' + cdomain + secure\n document.cookie = new_cookie_val\n } catch (err) {\n return\n }\n },\n\n removeItem(name) {\n try {\n cookieStore.setItem(name, '')\n } catch (err) {\n return\n }\n },\n clear() {\n document.cookie = ''\n },\n getAllKeys() {\n const ca = document.cookie.split(';')\n const keys = []\n\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 keys.push(c.split('=')[0])\n }\n\n return keys\n },\n}\n\nconst createStorageLike = (store: any): PostHogStorage => {\n return {\n getItem(key) {\n return store.getItem(key)\n },\n\n setItem(key, value) {\n store.setItem(key, value)\n },\n\n removeItem(key) {\n store.removeItem(key)\n },\n clear() {\n store.clear()\n },\n getAllKeys() {\n const keys = []\n for (const key in localStorage) {\n keys.push(key)\n }\n return keys\n },\n }\n}\n\nconst checkStoreIsSupported = (storage: PostHogStorage, key = '__mplssupport__'): boolean => {\n try {\n const val = 'xyz'\n storage.setItem(key, val)\n if (storage.getItem(key) !== val) {\n return false\n }\n storage.removeItem(key)\n return true\n } catch (err) {\n return false\n }\n}\n\nlet localStore: PostHogStorage | undefined = undefined\nlet sessionStore: PostHogStorage | undefined = undefined\n\nconst createMemoryStorage = (): PostHogStorage => {\n const _cache: { [key: string]: any | undefined } = {}\n\n const store: PostHogStorage = {\n getItem(key) {\n return _cache[key]\n },\n\n setItem(key, value) {\n _cache[key] = value !== null ? value : undefined\n },\n\n removeItem(key) {\n delete _cache[key]\n },\n clear() {\n for (const key in _cache) {\n delete _cache[key]\n }\n },\n getAllKeys() {\n const keys = []\n for (const key in _cache) {\n keys.push(key)\n }\n return keys\n },\n }\n return store\n}\n\nexport const getStorage = (type: PostHogOptions['persistence'], window: Window | undefined): PostHogStorage => {\n if (window) {\n if (!localStore) {\n const _localStore = createStorageLike(window.localStorage)\n localStore = checkStoreIsSupported(_localStore) ? _localStore : undefined\n }\n\n if (!sessionStore) {\n const _sessionStore = createStorageLike(window.sessionStorage)\n sessionStore = checkStoreIsSupported(_sessionStore) ? _sessionStore : undefined\n }\n }\n\n switch (type) {\n case 'cookie':\n return cookieStore || localStore || sessionStore || createMemoryStorage()\n case 'localStorage':\n return localStore || sessionStore || createMemoryStorage()\n case 'sessionStorage':\n return sessionStore || createMemoryStorage()\n case 'memory':\n return createMemoryStorage()\n default:\n return createMemoryStorage()\n }\n}\n","// Portions of this file are derived from getsentry/sentry-javascript\n// Copyright (c) 2012 Functional Software, Inc. dba Sentry\n// Licensed under the MIT License: https://github.com/getsentry/sentry-javascript/blob/develop/LICENSE\n\n// import { patch } from 'rrweb/typings/utils'\n// copied from: https://github.com/PostHog/posthog-js/blob/main/src/extensions/replay/rrweb-plugins/patch.ts\n// which was copied from https://github.com/rrweb-io/rrweb/blob/8aea5b00a4dfe5a6f59bd2ae72bb624f45e51e81/packages/rrweb/src/utils.ts#L129\n\n// copied from: https://github.com/PostHog/posthog-js/blob/main/react/src/utils/type-utils.ts#L4\nexport const isFunction = function (f: any): f is (...args: any[]) => any {\n return typeof f === 'function'\n}\n\nexport function patch(\n source: { [key: string]: any },\n name: string,\n replacement: (...args: unknown[]) => unknown\n): () => void {\n try {\n if (!(name in source)) {\n return () => {\n //\n }\n }\n\n const original = source[name] as () => unknown\n const wrapped = replacement(original)\n\n // Make sure it's a function first, as we need to attach an empty prototype for `defineProperties` to work\n // otherwise it'll throw \"TypeError: Object.defineProperties called on non-object\"\n if (isFunction(wrapped)) {\n wrapped.prototype = wrapped.prototype || {}\n Object.defineProperties(wrapped, {\n __posthog_wrapped__: {\n enumerable: false,\n value: true,\n },\n })\n }\n\n source[name] = wrapped\n\n return () => {\n source[name] = original\n }\n } catch {\n return () => {\n //\n }\n // This can throw when multiple instrumentation layers try to wrap the same global object,\n // such as XMLHttpRequest, and redefine the same non-configurable wrapper marker.\n }\n}\n","import { version } from './version'\n\nimport { PostHogCore, getFetch } from '@posthog/core'\nimport type {\n PostHogEventProperties,\n PostHogFetchOptions,\n PostHogFetchResponse,\n PostHogPersistedProperty,\n} from '@posthog/core'\n\nimport { getContext } from './context'\nimport { PostHogStorage, getStorage } from './storage'\nimport { PostHogOptions } from './types'\nimport { patch } from './patch'\n\nexport class PostHog extends PostHogCore {\n private _storage: PostHogStorage\n private _storageCache: any\n private _storageKey: string\n private _lastPathname: string = ''\n private _disableCaptureUrlHashes: boolean\n\n constructor(apiKey: string, options?: PostHogOptions) {\n super(apiKey, options)\n\n // posthog-js stores options in one object on\n this._storageKey = options?.persistence_name ? `ph_${options.persistence_name}` : `ph_${apiKey}_posthog`\n\n this._disableCaptureUrlHashes = options?.disable_capture_url_hashes ?? false\n\n this._storage = getStorage(options?.persistence || 'localStorage', this.getWindow())\n this.setupBootstrap(options)\n\n if (options?.preloadFeatureFlags !== false) {\n this.reloadFeatureFlags()\n }\n\n if (options?.captureHistoryEvents && typeof window !== 'undefined') {\n this._lastPathname = window?.location?.pathname || ''\n this.setupHistoryEventTracking()\n }\n }\n\n private getWindow(): Window | undefined {\n return typeof window !== 'undefined' ? window : undefined\n }\n\n getPersistedProperty<T>(key: PostHogPersistedProperty): T | undefined {\n if (!this._storageCache) {\n this._storageCache = JSON.parse(this._storage.getItem(this._storageKey) || '{}') || {}\n }\n\n return this._storageCache[key]\n }\n\n setPersistedProperty<T>(key: PostHogPersistedProperty, value: T | null): void {\n if (!this._storageCache) {\n this._storageCache = JSON.parse(this._storage.getItem(this._storageKey) || '{}') || {}\n }\n\n if (value === null) {\n delete this._storageCache[key]\n } else {\n this._storageCache[key] = value\n }\n\n this._storage.setItem(this._storageKey, JSON.stringify(this._storageCache))\n }\n\n fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse> {\n const fetchFn = getFetch()\n\n if (!fetchFn) {\n // error will be handled by the caller (fetchWithRetry)\n return Promise.reject(new Error('Fetch API is not available in this environment.'))\n }\n\n return fetchFn(url, options)\n }\n\n getLibraryId(): string {\n return 'posthog-js-lite'\n }\n\n getLibraryVersion(): string {\n return version\n }\n\n getCustomUserAgent(): void {\n return\n }\n\n getCommonEventProperties(): PostHogEventProperties {\n return {\n ...super.getCommonEventProperties(),\n ...getContext(this.getWindow(), this._disableCaptureUrlHashes),\n }\n }\n\n private setupHistoryEventTracking(): void {\n const window = this.getWindow()\n if (!window) {\n return\n }\n\n // Old fashioned, we could also use arrow functions but I think the closure for a patch is more reliable\n const self = this\n\n patch(window.history, 'pushState', (originalPushState) => {\n return function patchedPushState(this: History, state: any, title: string, url?: string | URL | null): void {\n ;(originalPushState as History['pushState']).call(this, state, title, url)\n self.captureNavigationEvent('pushState')\n }\n })\n\n patch(window.history, 'replaceState', (originalReplaceState) => {\n return function patchedReplaceState(this: History, state: any, title: string, url?: string | URL | null): void {\n ;(originalReplaceState as History['replaceState']).call(this, state, title, url)\n self.captureNavigationEvent('replaceState')\n }\n })\n\n // For popstate we need to listen to the event instead of overriding a method\n window.addEventListener('popstate', () => {\n this.captureNavigationEvent('popstate')\n })\n }\n\n private captureNavigationEvent(navigationType: 'pushState' | 'replaceState' | 'popstate'): void {\n const window = this.getWindow()\n if (!window) {\n return\n }\n\n const currentPathname = window.location.pathname\n\n // Only capture pageview if the pathname has changed\n if (currentPathname !== this._lastPathname) {\n this.capture('$pageview', { navigation_type: navigationType })\n this._lastPathname = currentPathname\n }\n }\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BAA4B;EAC1B,OAAO;CACT;CAEA,qBAA2B,CAE3B;CAEA,2BAAmD;EACjD,OAAO;GACL,GAAG,MAAM,yBAAyB;GAClC,GAAG,WAAW,KAAK,UAAU,GAAG,KAAK,wBAAwB;EAC/D;CACF;CAEA,4BAA0C;EACxC,MAAM,SAAS,KAAK,UAAU;EAC9B,IAAI,CAAC,QACH;EAIF,MAAM,OAAO;EAEb,MAAM,OAAO,SAAS,cAAc,sBAAsB;GACxD,OAAO,SAAS,iBAAgC,OAAY,OAAe,KAAiC;IACzG,kBAA4C,KAAK,MAAM,OAAO,OAAO,GAAG;IACzE,KAAK,uBAAuB,WAAW;GACzC;EACF,CAAC;EAED,MAAM,OAAO,SAAS,iBAAiB,yBAAyB;GAC9D,OAAO,SAAS,oBAAmC,OAAY,OAAe,KAAiC;IAC5G,qBAAkD,KAAK,MAAM,OAAO,OAAO,GAAG;IAC/E,KAAK,uBAAuB,cAAc;GAC5C;EACF,CAAC;EAGD,OAAO,iBAAiB,kBAAkB;GACxC,KAAK,uBAAuB,UAAU;EACxC,CAAC;CACH;CAEA,uBAA+B,gBAAiE;EAC9F,MAAM,SAAS,KAAK,UAAU;EAC9B,IAAI,CAAC,QACH;EAGF,MAAM,kBAAkB,OAAO,SAAS;EAGxC,IAAI,oBAAoB,KAAK,eAAe;GAC1C,KAAK,QAAQ,aAAa,EAAE,iBAAiB,eAAe,CAAC;GAC7D,KAAK,gBAAgB;EACvB;CACF;AACF;;;AC5IA,IAAA,cAAe"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/version.ts","../src/context.ts","../src/storage.ts","../src/patch.ts","../src/posthog-web.ts","../src/index.ts"],"sourcesContent":["export const version = \"4.13.0\"\n","import { currentTimestamp, detectBrowser, detectBrowserVersion, detectWebviewApp, stripUrlHash } from '@posthog/core'\nimport { version } from './version'\n\nexport function getContext(window: Window | undefined, disableCaptureUrlHashes: boolean = false): any {\n let context = {}\n if (window?.navigator) {\n const userAgent = window.navigator.userAgent\n const osValue = os(window)\n const [webviewApp, webviewAppVersion] = detectWebviewApp(userAgent)\n context = {\n ...context,\n ...(osValue !== undefined && { $os: osValue }),\n $browser: detectBrowser(userAgent, window.navigator.vendor),\n ...(webviewApp && { $webview_app: webviewApp }),\n ...(webviewAppVersion && { $webview_app_version: webviewAppVersion }),\n $referrer: window.document.referrer,\n $referring_domain: referringDomain(window.document.referrer),\n $device: device(userAgent),\n $current_url: disableCaptureUrlHashes ? stripUrlHash(window.location.href) : window.location.href,\n $host: window.location.host,\n $pathname: window.location.pathname,\n $browser_version: detectBrowserVersion(userAgent, window.navigator.vendor),\n $screen_height: window.screen.height,\n $screen_width: window.screen.width,\n $screen_dpr: window.devicePixelRatio,\n }\n }\n\n context = {\n ...context,\n $lib: 'js',\n $lib_version: version,\n $insert_id: Math.random().toString(36).substring(2, 10) + Math.random().toString(36).substring(2, 10),\n $time: currentTimestamp() / 1000, // epoch time in seconds\n }\n return context // TODO: strip empty props?\n}\n\nfunction os(window: Window | undefined): string | undefined {\n if (!window?.navigator) {\n return undefined\n }\n const a = window.navigator.userAgent\n if (/Windows/i.test(a)) {\n if (/Phone/.test(a) || /WPDesktop/.test(a)) {\n return 'Windows Phone'\n }\n return 'Windows'\n } else if (/(iPhone|iPad|iPod)/.test(a)) {\n return 'iOS'\n } else if (/Android/.test(a)) {\n return 'Android'\n } else if (/(BlackBerry|PlayBook|BB10)/i.test(a)) {\n return 'BlackBerry'\n } else if (/Mac/i.test(a)) {\n return 'Mac OS X'\n } else if (/Linux/.test(a)) {\n return 'Linux'\n } else if (/CrOS/.test(a)) {\n return 'Chrome OS'\n } else {\n return undefined\n }\n}\n\nfunction device(userAgent: string): string {\n if (/Windows Phone/i.test(userAgent) || /WPDesktop/.test(userAgent)) {\n return 'Windows Phone'\n } else if (/iPad/.test(userAgent)) {\n return 'iPad'\n } else if (/iPod/.test(userAgent)) {\n return 'iPod Touch'\n } else if (/iPhone/.test(userAgent)) {\n return 'iPhone'\n } else if (/(BlackBerry|PlayBook|BB10)/i.test(userAgent)) {\n return 'BlackBerry'\n } else if (/Android/.test(userAgent)) {\n return 'Android'\n } else {\n return ''\n }\n}\n\nfunction referringDomain(referrer: string): string {\n const split = referrer.split('/')\n if (split.length >= 3) {\n return split[2]\n }\n return ''\n}\n","import { PostHogOptions } from './types'\n\nexport type PostHogStorage = {\n getItem: (key: string) => string | null | undefined\n setItem: (key: string, value: string) => void\n removeItem: (key: string) => void\n clear: () => void\n getAllKeys: () => readonly string[]\n}\n\n// Methods partially borrowed from quirksmode.org/js/cookies.html\nexport const cookieStore: PostHogStorage = {\n getItem(key) {\n try {\n const nameEQ = key + '='\n const ca = document.cookie.split(';')\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 setItem(key: string, value: string) {\n try {\n const cdomain = '',\n expires = '',\n secure = ''\n\n const new_cookie_val = key + '=' + encodeURIComponent(value) + expires + '; path=/' + cdomain + secure\n document.cookie = new_cookie_val\n } catch (err) {\n return\n }\n },\n\n removeItem(name) {\n try {\n cookieStore.setItem(name, '')\n } catch (err) {\n return\n }\n },\n clear() {\n document.cookie = ''\n },\n getAllKeys() {\n const ca = document.cookie.split(';')\n const keys = []\n\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 keys.push(c.split('=')[0])\n }\n\n return keys\n },\n}\n\nconst createStorageLike = (store: any): PostHogStorage => {\n return {\n getItem(key) {\n return store.getItem(key)\n },\n\n setItem(key, value) {\n store.setItem(key, value)\n },\n\n removeItem(key) {\n store.removeItem(key)\n },\n clear() {\n store.clear()\n },\n getAllKeys() {\n const keys = []\n for (const key in localStorage) {\n keys.push(key)\n }\n return keys\n },\n }\n}\n\nconst checkStoreIsSupported = (storage: PostHogStorage, key = '__mplssupport__'): boolean => {\n try {\n const val = 'xyz'\n storage.setItem(key, val)\n if (storage.getItem(key) !== val) {\n return false\n }\n storage.removeItem(key)\n return true\n } catch (err) {\n return false\n }\n}\n\nlet localStore: PostHogStorage | undefined = undefined\nlet sessionStore: PostHogStorage | undefined = undefined\n\nconst createMemoryStorage = (): PostHogStorage => {\n const _cache: { [key: string]: any | undefined } = {}\n\n const store: PostHogStorage = {\n getItem(key) {\n return _cache[key]\n },\n\n setItem(key, value) {\n _cache[key] = value !== null ? value : undefined\n },\n\n removeItem(key) {\n delete _cache[key]\n },\n clear() {\n for (const key in _cache) {\n delete _cache[key]\n }\n },\n getAllKeys() {\n const keys = []\n for (const key in _cache) {\n keys.push(key)\n }\n return keys\n },\n }\n return store\n}\n\nexport const getStorage = (type: PostHogOptions['persistence'], window: Window | undefined): PostHogStorage => {\n if (window) {\n if (!localStore) {\n const _localStore = createStorageLike(window.localStorage)\n localStore = checkStoreIsSupported(_localStore) ? _localStore : undefined\n }\n\n if (!sessionStore) {\n const _sessionStore = createStorageLike(window.sessionStorage)\n sessionStore = checkStoreIsSupported(_sessionStore) ? _sessionStore : undefined\n }\n }\n\n switch (type) {\n case 'cookie':\n return cookieStore || localStore || sessionStore || createMemoryStorage()\n case 'localStorage':\n return localStore || sessionStore || createMemoryStorage()\n case 'sessionStorage':\n return sessionStore || createMemoryStorage()\n case 'memory':\n return createMemoryStorage()\n default:\n return createMemoryStorage()\n }\n}\n","// Portions of this file are derived from getsentry/sentry-javascript\n// Copyright (c) 2012 Functional Software, Inc. dba Sentry\n// Licensed under the MIT License: https://github.com/getsentry/sentry-javascript/blob/develop/LICENSE\n\n// import { patch } from 'rrweb/typings/utils'\n// copied from: https://github.com/PostHog/posthog-js/blob/main/src/extensions/replay/rrweb-plugins/patch.ts\n// which was copied from https://github.com/rrweb-io/rrweb/blob/8aea5b00a4dfe5a6f59bd2ae72bb624f45e51e81/packages/rrweb/src/utils.ts#L129\n\n// copied from: https://github.com/PostHog/posthog-js/blob/main/react/src/utils/type-utils.ts#L4\nexport const isFunction = function (f: any): f is (...args: any[]) => any {\n return typeof f === 'function'\n}\n\nexport function patch(\n source: { [key: string]: any },\n name: string,\n replacement: (...args: unknown[]) => unknown\n): () => void {\n try {\n if (!(name in source)) {\n return () => {\n //\n }\n }\n\n const original = source[name] as () => unknown\n const wrapped = replacement(original)\n\n // Make sure it's a function first, as we need to attach an empty prototype for `defineProperties` to work\n // otherwise it'll throw \"TypeError: Object.defineProperties called on non-object\"\n if (isFunction(wrapped)) {\n wrapped.prototype = wrapped.prototype || {}\n Object.defineProperties(wrapped, {\n __posthog_wrapped__: {\n enumerable: false,\n value: true,\n },\n })\n }\n\n source[name] = wrapped\n\n return () => {\n source[name] = original\n }\n } catch {\n return () => {\n //\n }\n // This can throw when multiple instrumentation layers try to wrap the same global object,\n // such as XMLHttpRequest, and redefine the same non-configurable wrapper marker.\n }\n}\n","import { version } from './version'\n\nimport { PostHogCore, getFetch } from '@posthog/core'\nimport type {\n PostHogEventProperties,\n PostHogFetchOptions,\n PostHogFetchResponse,\n PostHogPersistedProperty,\n} from '@posthog/core'\n\nimport { getContext } from './context'\nimport { PostHogStorage, getStorage } from './storage'\nimport { PostHogOptions } from './types'\nimport { patch } from './patch'\n\nexport class PostHog extends PostHogCore {\n private _storage: PostHogStorage\n private _storageCache: any\n private _storageKey: string\n private _lastPathname: string = ''\n private _disableCaptureUrlHashes: boolean\n\n constructor(apiKey: string, options?: PostHogOptions) {\n super(apiKey, options)\n\n // posthog-js stores options in one object on\n this._storageKey = options?.persistence_name ? `ph_${options.persistence_name}` : `ph_${apiKey}_posthog`\n\n this._disableCaptureUrlHashes = options?.disable_capture_url_hashes ?? false\n\n this._storage = getStorage(options?.persistence || 'localStorage', this.getWindow())\n this.setupBootstrap(options)\n\n if (options?.preloadFeatureFlags !== false) {\n this.reloadFeatureFlags()\n }\n\n if (options?.captureHistoryEvents && typeof window !== 'undefined') {\n this._lastPathname = window?.location?.pathname || ''\n this.setupHistoryEventTracking()\n }\n }\n\n private getWindow(): Window | undefined {\n return typeof window !== 'undefined' ? window : undefined\n }\n\n getPersistedProperty<T>(key: PostHogPersistedProperty): T | undefined {\n if (!this._storageCache) {\n this._storageCache = JSON.parse(this._storage.getItem(this._storageKey) || '{}') || {}\n }\n\n return this._storageCache[key]\n }\n\n setPersistedProperty<T>(key: PostHogPersistedProperty, value: T | null): void {\n if (!this._storageCache) {\n this._storageCache = JSON.parse(this._storage.getItem(this._storageKey) || '{}') || {}\n }\n\n if (value === null) {\n delete this._storageCache[key]\n } else {\n this._storageCache[key] = value\n }\n\n this._storage.setItem(this._storageKey, JSON.stringify(this._storageCache))\n }\n\n fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse> {\n const fetchFn = getFetch()\n\n if (!fetchFn) {\n // error will be handled by the caller (fetchWithRetry)\n return Promise.reject(new Error('Fetch API is not available in this environment.'))\n }\n\n return fetchFn(url, options)\n }\n\n getLibraryId(): string {\n return 'posthog-js-lite'\n }\n\n getLibraryVersion(): string {\n return version\n }\n\n getCustomUserAgent(): void {\n return\n }\n\n getCommonEventProperties(): PostHogEventProperties {\n return {\n ...super.getCommonEventProperties(),\n ...getContext(this.getWindow(), this._disableCaptureUrlHashes),\n }\n }\n\n private setupHistoryEventTracking(): void {\n const window = this.getWindow()\n if (!window) {\n return\n }\n\n // Old fashioned, we could also use arrow functions but I think the closure for a patch is more reliable\n const self = this\n\n patch(window.history, 'pushState', (originalPushState) => {\n return function patchedPushState(this: History, state: any, title: string, url?: string | URL | null): void {\n ;(originalPushState as History['pushState']).call(this, state, title, url)\n self.captureNavigationEvent('pushState')\n }\n })\n\n patch(window.history, 'replaceState', (originalReplaceState) => {\n return function patchedReplaceState(this: History, state: any, title: string, url?: string | URL | null): void {\n ;(originalReplaceState as History['replaceState']).call(this, state, title, url)\n self.captureNavigationEvent('replaceState')\n }\n })\n\n // For popstate we need to listen to the event instead of overriding a method\n window.addEventListener('popstate', () => {\n this.captureNavigationEvent('popstate')\n })\n }\n\n private captureNavigationEvent(navigationType: 'pushState' | 'replaceState' | 'popstate'): void {\n const window = this.getWindow()\n if (!window) {\n return\n }\n\n const currentPathname = window.location.pathname\n\n // Only capture pageview if the pathname has changed\n if (currentPathname !== this._lastPathname) {\n this.capture('$pageview', { navigation_type: navigationType })\n this._lastPathname = currentPathname\n }\n }\n}\n","import { PostHog } from './posthog-web'\n\nexport default PostHog\n\nexport * from './posthog-web'\n"],"mappings":";;AAAA,MAAa,UAAU;;;ACGvB,SAAgB,WAAW,QAA4B,0BAAmC,OAAY;CACpG,IAAI,UAAU,CAAC;CACf,IAAI,QAAQ,WAAW;EACrB,MAAM,YAAY,OAAO,UAAU;EACnC,MAAM,UAAU,GAAG,MAAM;EACzB,MAAM,CAAC,YAAY,qBAAqB,iBAAiB,SAAS;EAClE,UAAU;GACR,GAAG;GACH,GAAI,YAAY,KAAA,KAAa,EAAE,KAAK,QAAQ;GAC5C,UAAU,cAAc,WAAW,OAAO,UAAU,MAAM;GAC1D,GAAI,cAAc,EAAE,cAAc,WAAW;GAC7C,GAAI,qBAAqB,EAAE,sBAAsB,kBAAkB;GACnE,WAAW,OAAO,SAAS;GAC3B,mBAAmB,gBAAgB,OAAO,SAAS,QAAQ;GAC3D,SAAS,OAAO,SAAS;GACzB,cAAc,0BAA0B,aAAa,OAAO,SAAS,IAAI,IAAI,OAAO,SAAS;GAC7F,OAAO,OAAO,SAAS;GACvB,WAAW,OAAO,SAAS;GAC3B,kBAAkB,qBAAqB,WAAW,OAAO,UAAU,MAAM;GACzE,gBAAgB,OAAO,OAAO;GAC9B,eAAe,OAAO,OAAO;GAC7B,aAAa,OAAO;EACtB;CACF;CAEA,UAAU;EACR,GAAG;EACH,MAAM;EACN,cAAc;EACd,YAAY,KAAK,OAAO,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,UAAU,GAAG,EAAE,IAAI,KAAK,OAAO,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,U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|
"name": "posthog-js-lite",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.13.0",
|
|
4
4
|
"bugs": {
|
|
5
5
|
"url": "https://github.com/PostHog/posthog-js/issues"
|
|
6
6
|
},
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"src"
|
|
19
19
|
],
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@posthog/core": "^1.
|
|
21
|
+
"@posthog/core": "^1.58.0"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@babel/preset-env": "^7.27.1",
|
|
@@ -27,15 +27,16 @@
|
|
|
27
27
|
"jsdom": "^26.1.0",
|
|
28
28
|
"vitest": "^1.6.1",
|
|
29
29
|
"tsdown": "0.22.14",
|
|
30
|
-
"typescript": "
|
|
30
|
+
"typescript": "7.0.2",
|
|
31
31
|
"@posthog-tooling/tsconfig-base": "1.1.1"
|
|
32
32
|
},
|
|
33
33
|
"scripts": {
|
|
34
|
+
"check-types": "pnpm prebuild && tsc --noEmit --project tsconfig.json",
|
|
34
35
|
"clean": "rimraf dist coverage",
|
|
35
36
|
"test:unit": "vitest run",
|
|
36
37
|
"lint": "oxlint --report-unused-disable-directives-severity error src test",
|
|
37
38
|
"lint:fix": "oxlint --report-unused-disable-directives-severity error src test --fix",
|
|
38
|
-
"prebuild": "node -
|
|
39
|
+
"prebuild": "node ../../tooling/write-version.mjs",
|
|
39
40
|
"build": "tsdown",
|
|
40
41
|
"dev": "tsdown --watch",
|
|
41
42
|
"package": "pnpm pack --out $PACKAGE_DEST/%s.tgz"
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "4.
|
|
1
|
+
export const version = "4.13.0"
|