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.
@@ -183,6 +183,12 @@ export declare abstract class PostHogCore extends PostHogCoreStateless {
183
183
  onFeatureFlags(cb: (flags: PostHogDecideResponse['featureFlags']) => void): () => void;
184
184
  onFeatureFlag(key: string, cb: (value: string | boolean) => void): () => void;
185
185
  overrideFeatureFlag(flags: PostHogDecideResponse['featureFlags'] | null): Promise<void>;
186
+ /***
187
+ *** ERROR TRACKING
188
+ ***/
189
+ captureException(error: Error, additionalProperties?: {
190
+ [key: string]: any;
191
+ }): void;
186
192
  }
187
193
  export * from './types';
188
194
  export { LZString };
@@ -129,3 +129,4 @@ export type PostHogFlagsAndPayloadsResponse = {
129
129
  export type JsonType = string | number | boolean | null | {
130
130
  [key: string]: JsonType;
131
131
  } | Array<JsonType>;
132
+ export type FetchLike = (url: string, options: PostHogFetchOptions) => Promise<PostHogFetchResponse>;
@@ -1,3 +1,4 @@
1
+ import { FetchLike } from './types';
1
2
  export declare function assert(truthyValue: any, message: string): void;
2
3
  export declare function removeTrailingSlash(url: string): string;
3
4
  export interface RetriableOptions {
@@ -10,3 +11,4 @@ export declare function currentTimestamp(): number;
10
11
  export declare function currentISOTime(): string;
11
12
  export declare function safeSetTimeout(fn: () => void, timeout: number): any;
12
13
  export declare const isPromise: (obj: any) => obj is Promise<any>;
14
+ export declare function getFetch(): FetchLike | undefined;
@@ -1 +1 @@
1
- export declare function getContext(window: Window): any;
1
+ export declare function getContext(window: Window | undefined): any;
@@ -5,6 +5,7 @@ export declare class PostHog extends PostHogCore {
5
5
  private _storageCache;
6
6
  private _storageKey;
7
7
  constructor(apiKey: string, options?: PostHogOptions);
8
+ private getWindow;
8
9
  getPersistedProperty<T>(key: PostHogPersistedProperty): T | undefined;
9
10
  setPersistedProperty<T>(key: PostHogPersistedProperty, value: T | null): void;
10
11
  fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "posthog-js-lite",
3
- "version": "3.2.0",
3
+ "version": "3.3.0",
4
4
  "main": "lib/index.cjs.js",
5
5
  "module": "lib/index.esm.js",
6
6
  "types": "lib/index.d.ts",
package/src/context.ts CHANGED
@@ -1,13 +1,14 @@
1
1
  import { utils } from '../../posthog-core'
2
2
  import { version } from '../package.json'
3
3
 
4
- export function getContext(window: Window): any {
4
+ export function getContext(window: Window | undefined): any {
5
5
  let context = {}
6
- if (window.navigator) {
6
+ if (window?.navigator) {
7
7
  const userAgent = window.navigator.userAgent
8
+ const osValue = os(window)
8
9
  context = {
9
10
  ...context,
10
- $os: os(window),
11
+ ...(osValue !== undefined && { $os: osValue }),
11
12
  $browser: browser(userAgent, window.navigator.vendor, !!(window as any).opera),
12
13
  $referrer: window.document.referrer,
13
14
  $referring_domain: referringDomain(window.document.referrer),
@@ -21,6 +22,7 @@ export function getContext(window: Window): any {
21
22
  $screen_dpr: window.devicePixelRatio,
22
23
  }
23
24
  }
25
+
24
26
  context = {
25
27
  ...context,
26
28
  $lib: 'js',
@@ -114,7 +116,10 @@ function browserVersion(userAgent: string, vendor: string, opera: boolean): numb
114
116
  return parseFloat(matches[matches.length - 2])
115
117
  }
116
118
 
117
- function os(window: Window): string {
119
+ function os(window: Window | undefined): string | undefined {
120
+ if (!window?.navigator) {
121
+ return undefined
122
+ }
118
123
  const a = window.navigator.userAgent
119
124
  if (/Windows/i.test(a)) {
120
125
  if (/Phone/.test(a) || /WPDesktop/.test(a)) {
@@ -134,7 +139,7 @@ function os(window: Window): string {
134
139
  } else if (/CrOS/.test(a)) {
135
140
  return 'Chrome OS'
136
141
  } else {
137
- return ''
142
+ return undefined
138
143
  }
139
144
  }
140
145
 
@@ -8,6 +8,7 @@ import { getContext } from './context'
8
8
  import { PostHogStorage, getStorage } from './storage'
9
9
  import { version } from '../package.json'
10
10
  import { PostHogOptions } from './types'
11
+ import { getFetch } from 'posthog-core/src/utils'
11
12
 
12
13
  export class PostHog extends PostHogCore {
13
14
  private _storage: PostHogStorage
@@ -19,7 +20,8 @@ export class PostHog extends PostHogCore {
19
20
 
20
21
  // posthog-js stores options in one object on
21
22
  this._storageKey = options?.persistence_name ? `ph_${options.persistence_name}` : `ph_${apiKey}_posthog`
22
- this._storage = getStorage(options?.persistence || 'localStorage', window)
23
+
24
+ this._storage = getStorage(options?.persistence || 'localStorage', this.getWindow())
23
25
  this.setupBootstrap(options)
24
26
 
25
27
  if (options?.preloadFeatureFlags !== false) {
@@ -27,6 +29,10 @@ export class PostHog extends PostHogCore {
27
29
  }
28
30
  }
29
31
 
32
+ private getWindow(): Window | undefined {
33
+ return typeof window !== 'undefined' ? window : undefined
34
+ }
35
+
30
36
  getPersistedProperty<T>(key: PostHogPersistedProperty): T | undefined {
31
37
  if (!this._storageCache) {
32
38
  this._storageCache = JSON.parse(this._storage.getItem(this._storageKey) || '{}') || {}
@@ -50,7 +56,14 @@ export class PostHog extends PostHogCore {
50
56
  }
51
57
 
52
58
  fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse> {
53
- return window.fetch(url, options)
59
+ const fetchFn = getFetch()
60
+
61
+ if (!fetchFn) {
62
+ // error will be handled by the caller (fetchWithRetry)
63
+ return Promise.reject(new Error('Fetch API is not available in this environment.'))
64
+ }
65
+
66
+ return fetchFn(url, options)
54
67
  }
55
68
 
56
69
  getLibraryId(): string {
@@ -68,7 +81,7 @@ export class PostHog extends PostHogCore {
68
81
  getCommonEventProperties(): any {
69
82
  return {
70
83
  ...super.getCommonEventProperties(),
71
- ...getContext(window),
84
+ ...getContext(this.getWindow()),
72
85
  }
73
86
  }
74
87
  }
package/src/storage.ts CHANGED
@@ -93,9 +93,6 @@ const createStorageLike = (store: any): PostHogStorage => {
93
93
  }
94
94
 
95
95
  const checkStoreIsSupported = (storage: PostHogStorage, key = '__mplssupport__'): boolean => {
96
- if (!window) {
97
- return false
98
- }
99
96
  try {
100
97
  const val = 'xyz'
101
98
  storage.setItem(key, val)