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