posthog-js-lite 3.2.0 → 3.3.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/lib/index.d.ts CHANGED
@@ -317,6 +317,12 @@ declare abstract class PostHogCore extends PostHogCoreStateless {
317
317
  onFeatureFlags(cb: (flags: PostHogDecideResponse['featureFlags']) => void): () => void;
318
318
  onFeatureFlag(key: string, cb: (value: string | boolean) => void): () => void;
319
319
  overrideFeatureFlag(flags: PostHogDecideResponse['featureFlags'] | null): Promise<void>;
320
+ /***
321
+ *** ERROR TRACKING
322
+ ***/
323
+ captureException(error: Error, additionalProperties?: {
324
+ [key: string]: any;
325
+ }): void;
320
326
  }
321
327
 
322
328
  type PostHogOptions = {
@@ -330,6 +336,7 @@ declare class PostHog extends PostHogCore {
330
336
  private _storageCache;
331
337
  private _storageKey;
332
338
  constructor(apiKey: string, options?: PostHogOptions);
339
+ private getWindow;
333
340
  getPersistedProperty<T>(key: PostHogPersistedProperty): T | undefined;
334
341
  setPersistedProperty<T>(key: PostHogPersistedProperty, value: T | null): void;
335
342
  fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse>;
package/lib/index.esm.js CHANGED
@@ -61,6 +61,9 @@ function safeSetTimeout(fn, timeout) {
61
61
  // We unref if available to prevent Node.js hanging on exit
62
62
  t?.unref && t?.unref();
63
63
  return t;
64
+ }
65
+ function getFetch() {
66
+ return typeof fetch !== 'undefined' ? fetch : typeof global.fetch !== 'undefined' ? global.fetch : undefined;
64
67
  }
65
68
 
66
69
  // Copyright (c) 2013 Pieroxy <pieroxy@pieroxy.net>
@@ -1911,17 +1914,41 @@ class PostHogCore extends PostHogCoreStateless {
1911
1914
  return this.setPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags, flags);
1912
1915
  });
1913
1916
  }
1917
+ /***
1918
+ *** ERROR TRACKING
1919
+ ***/
1920
+ captureException(error, additionalProperties) {
1921
+ const properties = {
1922
+ $exception_level: 'error',
1923
+ $exception_list: [
1924
+ {
1925
+ type: error.name,
1926
+ value: error.message,
1927
+ mechanism: {
1928
+ handled: true,
1929
+ synthetic: false,
1930
+ },
1931
+ },
1932
+ ],
1933
+ ...additionalProperties,
1934
+ };
1935
+ properties.$exception_personURL = new URL(`/project/${this.apiKey}/person/${this.getDistinctId()}`, this.host).toString();
1936
+ this.capture('$exception', properties);
1937
+ }
1914
1938
  }
1915
1939
 
1916
- var version = "3.2.0";
1940
+ var version = "3.3.0";
1917
1941
 
1918
1942
  function getContext(window) {
1919
1943
  let context = {};
1920
- if (window.navigator) {
1944
+ if (window?.navigator) {
1921
1945
  const userAgent = window.navigator.userAgent;
1946
+ const osValue = os(window);
1922
1947
  context = {
1923
1948
  ...context,
1924
- $os: os(window),
1949
+ ...(osValue !== undefined && {
1950
+ $os: osValue
1951
+ }),
1925
1952
  $browser: browser(userAgent, window.navigator.vendor, !!window.opera),
1926
1953
  $referrer: window.document.referrer,
1927
1954
  $referring_domain: referringDomain(window.document.referrer),
@@ -2023,6 +2050,9 @@ function browserVersion(userAgent, vendor, opera) {
2023
2050
  return parseFloat(matches[matches.length - 2]);
2024
2051
  }
2025
2052
  function os(window) {
2053
+ if (!window?.navigator) {
2054
+ return undefined;
2055
+ }
2026
2056
  const a = window.navigator.userAgent;
2027
2057
  if (/Windows/i.test(a)) {
2028
2058
  if (/Phone/.test(a) || /WPDesktop/.test(a)) {
@@ -2042,7 +2072,7 @@ function os(window) {
2042
2072
  } else if (/CrOS/.test(a)) {
2043
2073
  return 'Chrome OS';
2044
2074
  } else {
2045
- return '';
2075
+ return undefined;
2046
2076
  }
2047
2077
  }
2048
2078
  function device(userAgent) {
@@ -2146,9 +2176,6 @@ const createStorageLike = store => {
2146
2176
  };
2147
2177
  };
2148
2178
  const checkStoreIsSupported = (storage, key = '__mplssupport__') => {
2149
- if (!window) {
2150
- return false;
2151
- }
2152
2179
  try {
2153
2180
  const val = 'xyz';
2154
2181
  storage.setItem(key, val);
@@ -2220,12 +2247,15 @@ class PostHog extends PostHogCore {
2220
2247
  super(apiKey, options);
2221
2248
  // posthog-js stores options in one object on
2222
2249
  this._storageKey = options?.persistence_name ? `ph_${options.persistence_name}` : `ph_${apiKey}_posthog`;
2223
- this._storage = getStorage(options?.persistence || 'localStorage', window);
2250
+ this._storage = getStorage(options?.persistence || 'localStorage', this.getWindow());
2224
2251
  this.setupBootstrap(options);
2225
2252
  if (options?.preloadFeatureFlags !== false) {
2226
2253
  this.reloadFeatureFlags();
2227
2254
  }
2228
2255
  }
2256
+ getWindow() {
2257
+ return typeof window !== 'undefined' ? window : undefined;
2258
+ }
2229
2259
  getPersistedProperty(key) {
2230
2260
  if (!this._storageCache) {
2231
2261
  this._storageCache = JSON.parse(this._storage.getItem(this._storageKey) || '{}') || {};
@@ -2244,7 +2274,12 @@ class PostHog extends PostHogCore {
2244
2274
  this._storage.setItem(this._storageKey, JSON.stringify(this._storageCache));
2245
2275
  }
2246
2276
  fetch(url, options) {
2247
- return window.fetch(url, options);
2277
+ const fetchFn = getFetch();
2278
+ if (!fetchFn) {
2279
+ // error will be handled by the caller (fetchWithRetry)
2280
+ return Promise.reject(new Error('Fetch API is not available in this environment.'));
2281
+ }
2282
+ return fetchFn(url, options);
2248
2283
  }
2249
2284
  getLibraryId() {
2250
2285
  return 'posthog-js-lite';
@@ -2258,7 +2293,7 @@ class PostHog extends PostHogCore {
2258
2293
  getCommonEventProperties() {
2259
2294
  return {
2260
2295
  ...super.getCommonEventProperties(),
2261
- ...getContext(window)
2296
+ ...getContext(this.getWindow())
2262
2297
  };
2263
2298
  }
2264
2299
  }