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/CHANGELOG.md CHANGED
@@ -1,18 +1,29 @@
1
1
  # Next
2
2
 
3
+ # 3.3.0 - 2025-02-06
4
+
5
+ ## Added
6
+
7
+ 1. Adds `captureException` function to allow manual capture of exceptions
8
+
9
+ # 3.2.1 - 2025-01-17
10
+
11
+ ## Fixed
12
+
13
+ 1. fix: check if window and fetch are available before using on web env
14
+
3
15
  # 3.2.0 - 2024-12-12
4
16
 
5
17
  ## Changed
6
18
 
7
19
  1. Add new debugging property `$feature_flag_bootstrapped_response`, `$feature_flag_bootstrapped_payload` and `$used_bootstrap_value` to `$feature_flag_called` event
8
20
 
9
-
10
21
  # 3.1.0 - 2024-11-21
11
22
 
12
23
  ## Changed
13
24
 
14
25
  1. chore: default `captureMode` changed to `json`.
15
- 1. To keep using the `form` mode, just set the `captureMode` option to `form` when initializing the PostHog client.
26
+ 1. To keep using the `form` mode, just set the `captureMode` option to `form` when initializing the PostHog client.
16
27
  2. fix: identify method allows passing a $set_once object
17
28
 
18
29
  # 3.0.2 - 2024-06-15
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  > 🚧 This is a reduced feature set package. Currently the only officially supported feature complete way of using PostHog on the web is [posthog-js](https://github.com/PostHog/posthog-js)
4
4
 
5
- This package is currently published to npm as [posthog-js-lite](https://www.npmjs.com/package/posthog-js-lite) and is a simplified version of the recommended and offically supported `posthog-js`.
5
+ This package is currently published to npm as [posthog-js-lite](https://www.npmjs.com/package/posthog-js-lite) and is a simplified version of the recommended and officially supported `posthog-js`.
6
6
 
7
7
  You'd want to use this only if you're very conscious about package sizes, and this reduced feature set (only analytics and feature flags) works for your use case. The most common use case is in chrome extensions.
8
8
 
package/lib/index.cjs.js CHANGED
@@ -65,6 +65,9 @@ function safeSetTimeout(fn, timeout) {
65
65
  // We unref if available to prevent Node.js hanging on exit
66
66
  t?.unref && t?.unref();
67
67
  return t;
68
+ }
69
+ function getFetch() {
70
+ return typeof fetch !== 'undefined' ? fetch : typeof global.fetch !== 'undefined' ? global.fetch : undefined;
68
71
  }
69
72
 
70
73
  // Copyright (c) 2013 Pieroxy <pieroxy@pieroxy.net>
@@ -1915,17 +1918,41 @@ class PostHogCore extends PostHogCoreStateless {
1915
1918
  return this.setPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags, flags);
1916
1919
  });
1917
1920
  }
1921
+ /***
1922
+ *** ERROR TRACKING
1923
+ ***/
1924
+ captureException(error, additionalProperties) {
1925
+ const properties = {
1926
+ $exception_level: 'error',
1927
+ $exception_list: [
1928
+ {
1929
+ type: error.name,
1930
+ value: error.message,
1931
+ mechanism: {
1932
+ handled: true,
1933
+ synthetic: false,
1934
+ },
1935
+ },
1936
+ ],
1937
+ ...additionalProperties,
1938
+ };
1939
+ properties.$exception_personURL = new URL(`/project/${this.apiKey}/person/${this.getDistinctId()}`, this.host).toString();
1940
+ this.capture('$exception', properties);
1941
+ }
1918
1942
  }
1919
1943
 
1920
- var version = "3.2.0";
1944
+ var version = "3.3.0";
1921
1945
 
1922
1946
  function getContext(window) {
1923
1947
  let context = {};
1924
- if (window.navigator) {
1948
+ if (window?.navigator) {
1925
1949
  const userAgent = window.navigator.userAgent;
1950
+ const osValue = os(window);
1926
1951
  context = {
1927
1952
  ...context,
1928
- $os: os(window),
1953
+ ...(osValue !== undefined && {
1954
+ $os: osValue
1955
+ }),
1929
1956
  $browser: browser(userAgent, window.navigator.vendor, !!window.opera),
1930
1957
  $referrer: window.document.referrer,
1931
1958
  $referring_domain: referringDomain(window.document.referrer),
@@ -2027,6 +2054,9 @@ function browserVersion(userAgent, vendor, opera) {
2027
2054
  return parseFloat(matches[matches.length - 2]);
2028
2055
  }
2029
2056
  function os(window) {
2057
+ if (!window?.navigator) {
2058
+ return undefined;
2059
+ }
2030
2060
  const a = window.navigator.userAgent;
2031
2061
  if (/Windows/i.test(a)) {
2032
2062
  if (/Phone/.test(a) || /WPDesktop/.test(a)) {
@@ -2046,7 +2076,7 @@ function os(window) {
2046
2076
  } else if (/CrOS/.test(a)) {
2047
2077
  return 'Chrome OS';
2048
2078
  } else {
2049
- return '';
2079
+ return undefined;
2050
2080
  }
2051
2081
  }
2052
2082
  function device(userAgent) {
@@ -2150,9 +2180,6 @@ const createStorageLike = store => {
2150
2180
  };
2151
2181
  };
2152
2182
  const checkStoreIsSupported = (storage, key = '__mplssupport__') => {
2153
- if (!window) {
2154
- return false;
2155
- }
2156
2183
  try {
2157
2184
  const val = 'xyz';
2158
2185
  storage.setItem(key, val);
@@ -2224,12 +2251,15 @@ class PostHog extends PostHogCore {
2224
2251
  super(apiKey, options);
2225
2252
  // posthog-js stores options in one object on
2226
2253
  this._storageKey = options?.persistence_name ? `ph_${options.persistence_name}` : `ph_${apiKey}_posthog`;
2227
- this._storage = getStorage(options?.persistence || 'localStorage', window);
2254
+ this._storage = getStorage(options?.persistence || 'localStorage', this.getWindow());
2228
2255
  this.setupBootstrap(options);
2229
2256
  if (options?.preloadFeatureFlags !== false) {
2230
2257
  this.reloadFeatureFlags();
2231
2258
  }
2232
2259
  }
2260
+ getWindow() {
2261
+ return typeof window !== 'undefined' ? window : undefined;
2262
+ }
2233
2263
  getPersistedProperty(key) {
2234
2264
  if (!this._storageCache) {
2235
2265
  this._storageCache = JSON.parse(this._storage.getItem(this._storageKey) || '{}') || {};
@@ -2248,7 +2278,12 @@ class PostHog extends PostHogCore {
2248
2278
  this._storage.setItem(this._storageKey, JSON.stringify(this._storageCache));
2249
2279
  }
2250
2280
  fetch(url, options) {
2251
- return window.fetch(url, options);
2281
+ const fetchFn = getFetch();
2282
+ if (!fetchFn) {
2283
+ // error will be handled by the caller (fetchWithRetry)
2284
+ return Promise.reject(new Error('Fetch API is not available in this environment.'));
2285
+ }
2286
+ return fetchFn(url, options);
2252
2287
  }
2253
2288
  getLibraryId() {
2254
2289
  return 'posthog-js-lite';
@@ -2262,7 +2297,7 @@ class PostHog extends PostHogCore {
2262
2297
  getCommonEventProperties() {
2263
2298
  return {
2264
2299
  ...super.getCommonEventProperties(),
2265
- ...getContext(window)
2300
+ ...getContext(this.getWindow())
2266
2301
  };
2267
2302
  }
2268
2303
  }