@posthog/browser 0.0.0 → 0.1.1
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/ARCHITECTURE.md +559 -0
- package/LICENSE +417 -0
- package/README.md +286 -28
- package/dist/analytics-buffer.d.ts +4 -0
- package/dist/analytics-buffer.js +200 -0
- package/dist/analytics-buffer.mjs +162 -0
- package/dist/analytics-delivery.d.ts +3 -0
- package/dist/analytics-delivery.js +231 -0
- package/dist/analytics-delivery.mjs +193 -0
- package/dist/analytics-internal.d.ts +64 -0
- package/dist/analytics-internal.js +52 -0
- package/dist/analytics-internal.mjs +5 -0
- package/dist/analytics-options.d.ts +14 -0
- package/dist/analytics-options.js +18 -0
- package/dist/analytics-options.mjs +1 -0
- package/dist/analytics.d.ts +5 -0
- package/dist/analytics.js +42 -0
- package/dist/analytics.mjs +4 -0
- package/dist/autocapture-options.d.ts +32 -0
- package/dist/autocapture-options.js +49 -0
- package/dist/autocapture-options.mjs +11 -0
- package/dist/autocapture.d.ts +5 -0
- package/dist/autocapture.js +108 -0
- package/dist/autocapture.mjs +70 -0
- package/dist/automatic-analytics.d.ts +3 -0
- package/dist/automatic-analytics.js +44 -0
- package/dist/automatic-analytics.mjs +6 -0
- package/dist/automatic-surveys.d.ts +2 -0
- package/dist/automatic-surveys.js +41 -0
- package/dist/automatic-surveys.mjs +3 -0
- package/dist/bot-filter.d.ts +2 -0
- package/dist/bot-filter.js +87 -0
- package/dist/bot-filter.mjs +49 -0
- package/dist/browser-client.d.ts +30 -0
- package/dist/browser-client.js +18 -0
- package/dist/browser-client.mjs +1 -0
- package/dist/capture-summary.d.ts +3 -0
- package/dist/capture-summary.js +63 -0
- package/dist/capture-summary.mjs +22 -0
- package/dist/capture-v1.d.ts +105 -0
- package/dist/capture-v1.js +781 -0
- package/dist/capture-v1.mjs +722 -0
- package/dist/core.d.ts +7 -0
- package/dist/core.js +49 -0
- package/dist/core.mjs +5 -0
- package/dist/event-buffer.d.ts +41 -0
- package/dist/event-buffer.js +158 -0
- package/dist/event-buffer.mjs +120 -0
- package/dist/extensions/registry.d.ts +16 -0
- package/dist/extensions/registry.js +128 -0
- package/dist/extensions/registry.mjs +90 -0
- package/dist/flags-internal.d.ts +8 -0
- package/dist/flags-internal.js +18 -0
- package/dist/flags-internal.mjs +1 -0
- package/dist/flags-options.d.ts +22 -0
- package/dist/flags-options.js +18 -0
- package/dist/flags-options.mjs +1 -0
- package/dist/flags-token.d.ts +15 -0
- package/dist/flags-token.js +40 -0
- package/dist/flags-token.mjs +2 -0
- package/dist/flags.d.ts +6 -0
- package/dist/flags.js +164 -0
- package/dist/flags.mjs +123 -0
- package/dist/id.d.ts +1 -0
- package/dist/id.js +61 -0
- package/dist/id.mjs +23 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +119 -0
- package/dist/index.mjs +75 -0
- package/dist/lane.d.ts +57 -0
- package/dist/lane.js +363 -0
- package/dist/lane.mjs +325 -0
- package/dist/logger.d.ts +4 -0
- package/dist/logger.js +57 -0
- package/dist/logger.mjs +19 -0
- package/dist/logs-internal.d.ts +7 -0
- package/dist/logs-internal.js +18 -0
- package/dist/logs-internal.mjs +1 -0
- package/dist/logs-options.d.ts +11 -0
- package/dist/logs-options.js +57 -0
- package/dist/logs-options.mjs +19 -0
- package/dist/logs.d.ts +6 -0
- package/dist/logs.js +140 -0
- package/dist/logs.mjs +102 -0
- package/dist/named-error.d.ts +7 -0
- package/dist/named-error.js +49 -0
- package/dist/named-error.mjs +11 -0
- package/dist/posthog.d.ts +3 -0
- package/dist/posthog.js +829 -0
- package/dist/posthog.mjs +791 -0
- package/dist/rate-limiter.d.ts +24 -0
- package/dist/rate-limiter.js +95 -0
- package/dist/rate-limiter.mjs +57 -0
- package/dist/remote-config.d.ts +3 -0
- package/dist/remote-config.js +47 -0
- package/dist/remote-config.mjs +9 -0
- package/dist/request.d.ts +14 -0
- package/dist/request.js +130 -0
- package/dist/request.mjs +89 -0
- package/dist/state.d.ts +82 -0
- package/dist/state.js +625 -0
- package/dist/state.mjs +581 -0
- package/dist/surveys-extension.d.ts +9 -0
- package/dist/surveys-extension.js +272 -0
- package/dist/surveys-extension.mjs +234 -0
- package/dist/surveys-internal.d.ts +12 -0
- package/dist/surveys-internal.js +18 -0
- package/dist/surveys-internal.mjs +1 -0
- package/dist/surveys-options.d.ts +18 -0
- package/dist/surveys-options.js +42 -0
- package/dist/surveys-options.mjs +4 -0
- package/dist/surveys.d.ts +6 -0
- package/dist/surveys.js +45 -0
- package/dist/surveys.mjs +4 -0
- package/dist/types.d.ts +114 -0
- package/dist/types.js +18 -0
- package/dist/types.mjs +1 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +40 -0
- package/dist/version.mjs +2 -0
- package/package.json +89 -8
package/dist/posthog.js
ADDED
|
@@ -0,0 +1,829 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, getters, values)=>{
|
|
5
|
+
var define = (defs, kind)=>{
|
|
6
|
+
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
[kind]: defs[key]
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
define(getters, "get");
|
|
12
|
+
define(values, "value");
|
|
13
|
+
};
|
|
14
|
+
})();
|
|
15
|
+
(()=>{
|
|
16
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
17
|
+
})();
|
|
18
|
+
(()=>{
|
|
19
|
+
__webpack_require__.r = (exports1)=>{
|
|
20
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
21
|
+
value: 'Module'
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
24
|
+
value: true
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
})();
|
|
28
|
+
var __webpack_exports__ = {};
|
|
29
|
+
__webpack_require__.r(__webpack_exports__);
|
|
30
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
31
|
+
createPostHogCore: ()=>createPostHogCore
|
|
32
|
+
});
|
|
33
|
+
const external_remote_config_js_namespaceObject = require("./remote-config.js");
|
|
34
|
+
const pubsub_namespaceObject = require("@posthog/browser-common/pubsub");
|
|
35
|
+
const external_analytics_buffer_js_namespaceObject = require("./analytics-buffer.js");
|
|
36
|
+
const external_analytics_internal_js_namespaceObject = require("./analytics-internal.js");
|
|
37
|
+
const external_bot_filter_js_namespaceObject = require("./bot-filter.js");
|
|
38
|
+
const external_capture_summary_js_namespaceObject = require("./capture-summary.js");
|
|
39
|
+
const registry_js_namespaceObject = require("./extensions/registry.js");
|
|
40
|
+
const external_id_js_namespaceObject = require("./id.js");
|
|
41
|
+
const external_logger_js_namespaceObject = require("./logger.js");
|
|
42
|
+
const external_rate_limiter_js_namespaceObject = require("./rate-limiter.js");
|
|
43
|
+
const external_request_js_namespaceObject = require("./request.js");
|
|
44
|
+
const external_state_js_namespaceObject = require("./state.js");
|
|
45
|
+
const external_version_js_namespaceObject = require("./version.js");
|
|
46
|
+
const CONSENT_CHANGE_EVENT = '__posthog_browser_consent_change__';
|
|
47
|
+
const DEFAULT_SHUTDOWN_TIMEOUT_MS = 30000;
|
|
48
|
+
const CLIENT_RATE_LIMIT_WARNING = '$$client_ingestion_warning';
|
|
49
|
+
const isNonEmptyString = (value)=>'string' == typeof value && value.trim().length > 0;
|
|
50
|
+
const isRecord = (value)=>'object' == typeof value && null !== value && !Array.isArray(value);
|
|
51
|
+
const normalizeHost = (host)=>{
|
|
52
|
+
let end = host.length;
|
|
53
|
+
while(end > 0 && 47 === host.charCodeAt(end - 1))end--;
|
|
54
|
+
return end === host.length ? host : host.slice(0, end);
|
|
55
|
+
};
|
|
56
|
+
const getAssetsHost = (apiHost)=>{
|
|
57
|
+
try {
|
|
58
|
+
const url = new URL(apiHost);
|
|
59
|
+
if ('https:' === url.protocol) {
|
|
60
|
+
const region = /^(app|us|us-assets|eu|eu-assets)(\.i)?\.posthog\.com$/.exec(url.hostname)?.[1];
|
|
61
|
+
if (region) return `https://${region.startsWith('eu') ? 'eu' : 'us'}-assets.i.posthog.com`;
|
|
62
|
+
}
|
|
63
|
+
} catch {}
|
|
64
|
+
return apiHost;
|
|
65
|
+
};
|
|
66
|
+
const isValidDistinctId = (value)=>isNonEmptyString(value) && ![
|
|
67
|
+
'$posthog_cookieless',
|
|
68
|
+
'distinct_id',
|
|
69
|
+
'distinctid',
|
|
70
|
+
'undefined',
|
|
71
|
+
'null'
|
|
72
|
+
].includes(value.toLowerCase());
|
|
73
|
+
const deepFreeze = (value)=>{
|
|
74
|
+
if (value && 'object' == typeof value) {
|
|
75
|
+
Object.values(value).forEach(deepFreeze);
|
|
76
|
+
Object.freeze(value);
|
|
77
|
+
}
|
|
78
|
+
return value;
|
|
79
|
+
};
|
|
80
|
+
const getDefaultFetch = ()=>{
|
|
81
|
+
try {
|
|
82
|
+
return globalThis.fetch?.bind(globalThis);
|
|
83
|
+
} catch {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
const getDefaultNavigator = ()=>{
|
|
88
|
+
try {
|
|
89
|
+
return globalThis.navigator;
|
|
90
|
+
} catch {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
const eventTimestamp = (timestamp)=>{
|
|
95
|
+
try {
|
|
96
|
+
return (timestamp ?? new Date()).toISOString();
|
|
97
|
+
} catch {
|
|
98
|
+
return new Date().toISOString();
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
const utf8Bytes = (value)=>{
|
|
102
|
+
let bytes = 0;
|
|
103
|
+
for(let index = 0; index < value.length; index++){
|
|
104
|
+
const code = value.charCodeAt(index);
|
|
105
|
+
if (code < 0x80) bytes++;
|
|
106
|
+
else if (code < 0x800) bytes += 2;
|
|
107
|
+
else if (code >= 0xd800 && code <= 0xdbff && value.charCodeAt(index + 1) >= 0xdc00) {
|
|
108
|
+
bytes += 4;
|
|
109
|
+
index++;
|
|
110
|
+
} else bytes += 3;
|
|
111
|
+
}
|
|
112
|
+
return bytes;
|
|
113
|
+
};
|
|
114
|
+
class PostHogBrowserClient {
|
|
115
|
+
static create(options, capture) {
|
|
116
|
+
return new PostHogBrowserClient(options, capture);
|
|
117
|
+
}
|
|
118
|
+
constructor(options, capture){
|
|
119
|
+
this.library = Object.freeze({
|
|
120
|
+
name: 'web',
|
|
121
|
+
version: external_version_js_namespaceObject.version
|
|
122
|
+
});
|
|
123
|
+
this._dynamicEventProperties = [];
|
|
124
|
+
this._rateLimiter = new external_rate_limiter_js_namespaceObject.ClientRateLimiter();
|
|
125
|
+
this._immediateAuthority = {};
|
|
126
|
+
this._closing = false;
|
|
127
|
+
this._disposed = false;
|
|
128
|
+
this._initialPageviewPending = true;
|
|
129
|
+
let projectToken = '';
|
|
130
|
+
try {
|
|
131
|
+
projectToken = options.projectToken ?? '';
|
|
132
|
+
} catch {}
|
|
133
|
+
this.projectToken = projectToken;
|
|
134
|
+
this._capturePageview = options.capturePageview ?? true;
|
|
135
|
+
this.logger = (0, external_logger_js_namespaceObject.createLogger)('[PostHog]', options.debug ?? false);
|
|
136
|
+
if (!projectToken) this.logger.error('A PostHog project token is required');
|
|
137
|
+
const initialPersonProperties = options.initialPersonProperties;
|
|
138
|
+
try {
|
|
139
|
+
this.initialPersonProperties = initialPersonProperties ? deepFreeze(JSON.parse(JSON.stringify(initialPersonProperties))) : Object.freeze({});
|
|
140
|
+
} catch {
|
|
141
|
+
this.initialPersonProperties = Object.freeze({});
|
|
142
|
+
}
|
|
143
|
+
this._remoteConfigPublisher = new pubsub_namespaceObject.Publisher((error)=>this.logger.error('A remote configuration listener failed', error));
|
|
144
|
+
this._eventPublisher = new pubsub_namespaceObject.Publisher((error)=>this.logger.error('An event listener failed', error));
|
|
145
|
+
this._identifyPublisher = new pubsub_namespaceObject.Publisher((error)=>this.logger.error('An identify listener failed', error));
|
|
146
|
+
this._groupPublisher = new pubsub_namespaceObject.Publisher((error)=>this.logger.error('A group listener failed', error));
|
|
147
|
+
this._resetPublisher = new pubsub_namespaceObject.Publisher((error)=>this.logger.error('A reset listener failed', error));
|
|
148
|
+
this._newSessionPublisher = new pubsub_namespaceObject.Publisher((error)=>this.logger.error('A session listener failed', error));
|
|
149
|
+
this._consentChangePublisher = new pubsub_namespaceObject.Publisher((error)=>this.logger.error('A consent change listener failed', error));
|
|
150
|
+
const browserNavigator = false === options.navigator ? void 0 : options.navigator ?? getDefaultNavigator();
|
|
151
|
+
const browserFetch = false === options.fetch ? void 0 : options.fetch ?? getDefaultFetch();
|
|
152
|
+
this._blocked = !projectToken || !(options.disableBotDetection ?? false) && (0, external_bot_filter_js_namespaceObject.isLikelyBot)(browserNavigator, options.blockedUserAgents ?? []);
|
|
153
|
+
const requestedStorage = false === options.storage ? void 0 : options.storage ?? (0, external_state_js_namespaceObject.getDefaultStorage)();
|
|
154
|
+
const storage = this._blocked ? void 0 : requestedStorage;
|
|
155
|
+
this._state = new external_state_js_namespaceObject.BrowserState(projectToken, storage, options.persistenceKey, options.consentPersistenceName, options.optOutByDefault ?? false, !this._blocked && void 0 === options.storage && void 0 !== requestedStorage ? external_state_js_namespaceObject.getDefaultSessionStorage : void 0, (consent, previous)=>{
|
|
156
|
+
if ('denied' === consent) {
|
|
157
|
+
this._immediateAuthority = {};
|
|
158
|
+
this._captureSink?.purge();
|
|
159
|
+
}
|
|
160
|
+
this._consentChangePublisher.publish({
|
|
161
|
+
current: consent,
|
|
162
|
+
previous
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
this._consentObservation = this._observeConsent(storage, !this._blocked && void 0 === options.storage && void 0 !== requestedStorage);
|
|
166
|
+
const apiHost = normalizeHost(options.apiHost ?? 'https://us.i.posthog.com');
|
|
167
|
+
this._requestRuntime = [
|
|
168
|
+
{
|
|
169
|
+
api: apiHost,
|
|
170
|
+
flags: normalizeHost(options.flagsHost ?? apiHost),
|
|
171
|
+
assets: getAssetsHost(apiHost)
|
|
172
|
+
},
|
|
173
|
+
projectToken,
|
|
174
|
+
browserFetch,
|
|
175
|
+
browserNavigator
|
|
176
|
+
];
|
|
177
|
+
this._remoteConfig = options.remoteConfig;
|
|
178
|
+
this._remoteConfigTimeoutMs = void 0 !== options.remoteConfigTimeoutMs && Number.isFinite(options.remoteConfigTimeoutMs) ? Math.max(0, options.remoteConfigTimeoutMs) : 10000;
|
|
179
|
+
this.kv = this._state.keyValueStore('core', ()=>!this._closing && !this._disposed && this._state.prepare());
|
|
180
|
+
this._latestRemoteConfigResult = this._remoteConfig ? {
|
|
181
|
+
ok: true,
|
|
182
|
+
config: this._remoteConfig
|
|
183
|
+
} : void 0;
|
|
184
|
+
this.onRemoteConfig = (handler)=>{
|
|
185
|
+
if (this._closing || this._disposed) return {
|
|
186
|
+
dispose () {}
|
|
187
|
+
};
|
|
188
|
+
const subscription = this._remoteConfigPublisher.listener(handler);
|
|
189
|
+
if (this._latestRemoteConfigResult) try {
|
|
190
|
+
handler(this._latestRemoteConfigResult);
|
|
191
|
+
} catch (error) {
|
|
192
|
+
this.logger.error('A remote configuration listener failed', error);
|
|
193
|
+
}
|
|
194
|
+
else if (!this._closing && !this._disposed) this.getRemoteConfig();
|
|
195
|
+
return subscription;
|
|
196
|
+
};
|
|
197
|
+
this.onEvent = this._eventPublisher.listener;
|
|
198
|
+
this.onIdentify = this._identifyPublisher.listener;
|
|
199
|
+
this.onGroup = this._groupPublisher.listener;
|
|
200
|
+
this.onReset = this._resetPublisher.listener;
|
|
201
|
+
this.onConsentChange = this._consentChangePublisher.listener;
|
|
202
|
+
this.onNewSession = this._newSessionPublisher.listener;
|
|
203
|
+
this.onSession = (listener)=>this.onNewSession((session)=>listener(session.sessionId));
|
|
204
|
+
this._registry = new registry_js_namespaceObject.ExtensionRegistry((extensionName)=>this._createExtensionClient(extensionName), this.logger);
|
|
205
|
+
this._captureSink = capture;
|
|
206
|
+
}
|
|
207
|
+
get distinctId() {
|
|
208
|
+
this._state.prepare();
|
|
209
|
+
return this._state.distinctId;
|
|
210
|
+
}
|
|
211
|
+
get anonymousId() {
|
|
212
|
+
this._state.prepare();
|
|
213
|
+
return this._state.anonymousId;
|
|
214
|
+
}
|
|
215
|
+
get deviceId() {
|
|
216
|
+
this._state.prepare();
|
|
217
|
+
return this._state.deviceId;
|
|
218
|
+
}
|
|
219
|
+
get groups() {
|
|
220
|
+
this._state.prepare();
|
|
221
|
+
return this._state.groups;
|
|
222
|
+
}
|
|
223
|
+
get session() {
|
|
224
|
+
this._state.prepare();
|
|
225
|
+
return this._state.session;
|
|
226
|
+
}
|
|
227
|
+
get isOptedOut() {
|
|
228
|
+
return this.hasOptedOut();
|
|
229
|
+
}
|
|
230
|
+
get canCapture() {
|
|
231
|
+
return !this._closing && !this._disposed && !this._blocked && !this.hasOptedOut() && this._state.prepare();
|
|
232
|
+
}
|
|
233
|
+
get isRateLimited() {
|
|
234
|
+
return this._rateLimiter.wouldLimit;
|
|
235
|
+
}
|
|
236
|
+
capture(event, properties = null, options = {}) {
|
|
237
|
+
try {
|
|
238
|
+
if ('unload' === options.delivery && !this._captureSink?.isTearingDown) {
|
|
239
|
+
const authority = this._immediateAuthority;
|
|
240
|
+
const message = this._admitCapture(event, properties, options, false, true);
|
|
241
|
+
if (message) this._captureSink?.deliverUnload(message, ()=>this._immediateAuthority === authority);
|
|
242
|
+
return !!message || !this._rateLimiter.limited;
|
|
243
|
+
}
|
|
244
|
+
} catch (error) {
|
|
245
|
+
this.logger.error('Unload capture failed', error);
|
|
246
|
+
return true;
|
|
247
|
+
}
|
|
248
|
+
return this._capture(event, properties, options) || !this._rateLimiter.limited;
|
|
249
|
+
}
|
|
250
|
+
async captureImmediate(event, properties = null, options = {}) {
|
|
251
|
+
try {
|
|
252
|
+
const authority = this._immediateAuthority;
|
|
253
|
+
const message = this._admitCapture(event, properties, options, false, true);
|
|
254
|
+
if (!message) return external_capture_summary_js_namespaceObject.EMPTY_CAPTURE_SUMMARY;
|
|
255
|
+
const capture = this._captureSink;
|
|
256
|
+
if (!capture) return (0, external_capture_summary_js_namespaceObject.captureFailure)(new Error('Immediate analytics delivery is unavailable'));
|
|
257
|
+
return await capture.deliverImmediate(message, ()=>this._immediateAuthority === authority);
|
|
258
|
+
} catch (error) {
|
|
259
|
+
return (0, external_capture_summary_js_namespaceObject.captureFailure)(error);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
_capture(event, properties = null, options = {}, skipRateLimit = false) {
|
|
263
|
+
return void 0 !== this._admitCapture(event, properties, options, skipRateLimit, false);
|
|
264
|
+
}
|
|
265
|
+
_admitCapture(event, properties, options, skipRateLimit, immediate) {
|
|
266
|
+
if (!isNonEmptyString(event) || this._closing || this._disposed || this._blocked || this.hasOptedOut() || !this._state.prepare()) return;
|
|
267
|
+
if (!skipRateLimit) {
|
|
268
|
+
const rateLimit = this._rateLimiter.consume();
|
|
269
|
+
if (!rateLimit.allowed) {
|
|
270
|
+
if (void 0 !== rateLimit.reportDropped) {
|
|
271
|
+
const warning = `${rateLimit.reportDropped} event(s) dropped since the last warning. Client limit is 10 events per second with a burst of 100.`;
|
|
272
|
+
this.logger.warn(`PostHog client rate limited: ${warning}`);
|
|
273
|
+
if (this._capture(CLIENT_RATE_LIMIT_WARNING, {
|
|
274
|
+
$$client_ingestion_warning_message: `posthog-js client rate limited: ${warning}`
|
|
275
|
+
}, {}, true)) this._rateLimiter.reported();
|
|
276
|
+
}
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
const dynamicProperties = {};
|
|
281
|
+
for (const producer of this._dynamicEventProperties.slice())try {
|
|
282
|
+
Object.assign(dynamicProperties, producer());
|
|
283
|
+
} catch (error) {
|
|
284
|
+
this.logger.error('An event property producer failed', error);
|
|
285
|
+
}
|
|
286
|
+
let messageProperties;
|
|
287
|
+
let observedProperties;
|
|
288
|
+
let uuid;
|
|
289
|
+
let timestamp;
|
|
290
|
+
try {
|
|
291
|
+
const callerProperties = {
|
|
292
|
+
...dynamicProperties,
|
|
293
|
+
...properties ?? {}
|
|
294
|
+
};
|
|
295
|
+
const set = options.set;
|
|
296
|
+
const setOnce = options.setOnce;
|
|
297
|
+
uuid = options.uuid;
|
|
298
|
+
timestamp = options.timestamp;
|
|
299
|
+
if (set) callerProperties['$set'] = set;
|
|
300
|
+
if (setOnce) callerProperties['$set_once'] = setOnce;
|
|
301
|
+
const serializedProperties = JSON.stringify(callerProperties);
|
|
302
|
+
const parsedMessageProperties = JSON.parse(serializedProperties, function(_key, value) {
|
|
303
|
+
return null !== value || Array.isArray(this) ? value : void 0;
|
|
304
|
+
});
|
|
305
|
+
const parsedObservedProperties = JSON.parse(serializedProperties);
|
|
306
|
+
if (!isRecord(parsedMessageProperties) || !isRecord(parsedObservedProperties)) throw new Error('Event properties must serialize to an object');
|
|
307
|
+
messageProperties = parsedMessageProperties;
|
|
308
|
+
observedProperties = parsedObservedProperties;
|
|
309
|
+
} catch (error) {
|
|
310
|
+
this.logger.error('Event properties are not JSON-serializable', error);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
const preparedSession = this._state.prepareSessionForEvent();
|
|
314
|
+
const session = preparedSession.context;
|
|
315
|
+
const distinctId = this._state.distinctId;
|
|
316
|
+
const deviceId = this._state.deviceId;
|
|
317
|
+
const groups = this._state.groups;
|
|
318
|
+
Object.assign(messageProperties, {
|
|
319
|
+
token: this.projectToken,
|
|
320
|
+
distinct_id: distinctId,
|
|
321
|
+
$device_id: deviceId,
|
|
322
|
+
$groups: groups,
|
|
323
|
+
$session_id: session.sessionId,
|
|
324
|
+
$window_id: session.windowId,
|
|
325
|
+
$lib: 'web',
|
|
326
|
+
$lib_version: external_version_js_namespaceObject.version
|
|
327
|
+
});
|
|
328
|
+
Object.assign(observedProperties, {
|
|
329
|
+
token: this.projectToken,
|
|
330
|
+
distinct_id: distinctId,
|
|
331
|
+
$device_id: deviceId,
|
|
332
|
+
$groups: {
|
|
333
|
+
...groups
|
|
334
|
+
},
|
|
335
|
+
$session_id: session.sessionId,
|
|
336
|
+
$window_id: session.windowId,
|
|
337
|
+
$lib: 'web',
|
|
338
|
+
$lib_version: external_version_js_namespaceObject.version
|
|
339
|
+
});
|
|
340
|
+
const message = {
|
|
341
|
+
uuid: uuid ?? (0, external_id_js_namespaceObject.createId)(),
|
|
342
|
+
event,
|
|
343
|
+
distinct_id: distinctId,
|
|
344
|
+
properties: messageProperties,
|
|
345
|
+
timestamp: eventTimestamp(timestamp)
|
|
346
|
+
};
|
|
347
|
+
let bytes;
|
|
348
|
+
try {
|
|
349
|
+
bytes = utf8Bytes(JSON.stringify(message));
|
|
350
|
+
} catch (error) {
|
|
351
|
+
this.logger.error('The finalized event could not be measured', error);
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
const capture = this._captureSink;
|
|
355
|
+
if (immediate ? bytes > external_analytics_internal_js_namespaceObject.MAX_ANALYTICS_BYTES : !capture?.enqueue(message, bytes)) {
|
|
356
|
+
if (bytes > external_analytics_internal_js_namespaceObject.MAX_ANALYTICS_BYTES) this.logger.warn(`Event "${event}" (${bytes} bytes) exceeds the local analytics limit and was dropped`);
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
if (!this._state.sessionAdmitted(preparedSession)) {
|
|
360
|
+
if (!immediate) capture?.discardQueued(message);
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
if (preparedSession.reason) this._newSessionPublisher.publish({
|
|
364
|
+
...session,
|
|
365
|
+
reason: preparedSession.reason
|
|
366
|
+
});
|
|
367
|
+
this._eventPublisher.publish(deepFreeze({
|
|
368
|
+
event,
|
|
369
|
+
properties: observedProperties
|
|
370
|
+
}));
|
|
371
|
+
if (!immediate) capture?.admitted();
|
|
372
|
+
return message;
|
|
373
|
+
}
|
|
374
|
+
async identify(distinctId, set, setOnce) {
|
|
375
|
+
if (!isValidDistinctId(distinctId) || this._closing || this._disposed) return;
|
|
376
|
+
this._state.prepare();
|
|
377
|
+
const previousDistinctId = this._state.distinctId;
|
|
378
|
+
const wasIdentified = this._state.isIdentified;
|
|
379
|
+
const captureOptions = {};
|
|
380
|
+
if (set) captureOptions.set = set;
|
|
381
|
+
if (setOnce) captureOptions.setOnce = setOnce;
|
|
382
|
+
const hasPersonProperties = !!set || !!setOnce;
|
|
383
|
+
if (distinctId === previousDistinctId) {
|
|
384
|
+
if (wasIdentified) {
|
|
385
|
+
if (hasPersonProperties) {
|
|
386
|
+
this._identifyPublisher.publish({
|
|
387
|
+
distinctId,
|
|
388
|
+
previousDistinctId,
|
|
389
|
+
wasIdentified,
|
|
390
|
+
set,
|
|
391
|
+
setOnce
|
|
392
|
+
});
|
|
393
|
+
this.capture('$set', null, captureOptions);
|
|
394
|
+
}
|
|
395
|
+
} else {
|
|
396
|
+
this._state.identify(distinctId);
|
|
397
|
+
this._identifyPublisher.publish({
|
|
398
|
+
distinctId,
|
|
399
|
+
previousDistinctId,
|
|
400
|
+
wasIdentified,
|
|
401
|
+
set,
|
|
402
|
+
setOnce
|
|
403
|
+
});
|
|
404
|
+
this.capture('$set', null, {
|
|
405
|
+
set: set ?? {},
|
|
406
|
+
setOnce: setOnce ?? {}
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
this._state.identify(distinctId);
|
|
412
|
+
this._identifyPublisher.publish({
|
|
413
|
+
distinctId,
|
|
414
|
+
previousDistinctId,
|
|
415
|
+
wasIdentified,
|
|
416
|
+
set,
|
|
417
|
+
setOnce
|
|
418
|
+
});
|
|
419
|
+
if (wasIdentified) {
|
|
420
|
+
if (hasPersonProperties) this.capture('$set', null, captureOptions);
|
|
421
|
+
} else this.capture('$identify', {
|
|
422
|
+
$anon_distinct_id: previousDistinctId
|
|
423
|
+
}, captureOptions);
|
|
424
|
+
}
|
|
425
|
+
async group(type, key, properties) {
|
|
426
|
+
if (!isNonEmptyString(type) || !isNonEmptyString(key) || this._closing || this._disposed) return;
|
|
427
|
+
this._state.prepare();
|
|
428
|
+
const changed = this._state.group(type, key);
|
|
429
|
+
if (!changed && !properties) return;
|
|
430
|
+
this._groupPublisher.publish({
|
|
431
|
+
type,
|
|
432
|
+
key,
|
|
433
|
+
changed,
|
|
434
|
+
properties
|
|
435
|
+
});
|
|
436
|
+
this.capture('$groupidentify', {
|
|
437
|
+
$group_type: type,
|
|
438
|
+
$group_key: key,
|
|
439
|
+
...properties ? {
|
|
440
|
+
$group_set: properties
|
|
441
|
+
} : {}
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
reset() {
|
|
445
|
+
if (this._closing || this._disposed) return;
|
|
446
|
+
this._state.prepare();
|
|
447
|
+
this._state.reset();
|
|
448
|
+
this._resetPublisher.publish(void 0);
|
|
449
|
+
}
|
|
450
|
+
async flush() {
|
|
451
|
+
await this._registry.flush();
|
|
452
|
+
}
|
|
453
|
+
optIn() {
|
|
454
|
+
if (this._disposed) return;
|
|
455
|
+
if (this._state.optIn()) this._dispatchConsentChange();
|
|
456
|
+
this._startInitialPageview();
|
|
457
|
+
}
|
|
458
|
+
optOut() {
|
|
459
|
+
if (!this._disposed && this._state.optOut()) this._dispatchConsentChange();
|
|
460
|
+
}
|
|
461
|
+
hasOptedOut() {
|
|
462
|
+
return 'denied' === this._state.consent;
|
|
463
|
+
}
|
|
464
|
+
registerDynamicEventProperties(producer) {
|
|
465
|
+
if (this._disposed) return {
|
|
466
|
+
dispose () {}
|
|
467
|
+
};
|
|
468
|
+
this._dynamicEventProperties.push(producer);
|
|
469
|
+
let active = true;
|
|
470
|
+
return {
|
|
471
|
+
dispose: ()=>{
|
|
472
|
+
if (!active) return;
|
|
473
|
+
active = false;
|
|
474
|
+
const index = this._dynamicEventProperties.indexOf(producer);
|
|
475
|
+
if (-1 !== index) this._dynamicEventProperties.splice(index, 1);
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
sendRequest(path, init) {
|
|
480
|
+
return (0, external_request_js_namespaceObject.sendRequest)(this._requestRuntime, path, init, ()=>!this._blocked && !this.hasOptedOut());
|
|
481
|
+
}
|
|
482
|
+
async getRemoteConfig() {
|
|
483
|
+
if (this._closing || this._disposed) return;
|
|
484
|
+
if (void 0 !== this._remoteConfig) return this._remoteConfig;
|
|
485
|
+
if (!this._remoteConfigPromise) {
|
|
486
|
+
let timeout;
|
|
487
|
+
let invalidated = false;
|
|
488
|
+
let controller;
|
|
489
|
+
let cancelWait;
|
|
490
|
+
const timeoutResult = new Promise((resolve)=>{
|
|
491
|
+
cancelWait = ()=>{
|
|
492
|
+
if (void 0 !== timeout) {
|
|
493
|
+
try {
|
|
494
|
+
globalThis.clearTimeout(timeout);
|
|
495
|
+
} catch {}
|
|
496
|
+
timeout = void 0;
|
|
497
|
+
}
|
|
498
|
+
resolve(void 0);
|
|
499
|
+
};
|
|
500
|
+
this._cancelRemoteConfigWait = cancelWait;
|
|
501
|
+
try {
|
|
502
|
+
timeout = globalThis.setTimeout(cancelWait, this._remoteConfigTimeoutMs);
|
|
503
|
+
} catch {
|
|
504
|
+
cancelWait();
|
|
505
|
+
}
|
|
506
|
+
});
|
|
507
|
+
this._remoteConfigPromise = Promise.race([
|
|
508
|
+
Promise.resolve().then(()=>{
|
|
509
|
+
if (this._closing || this._disposed) {
|
|
510
|
+
invalidated = true;
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
controller = 'function' == typeof globalThis.AbortController ? new globalThis.AbortController() : void 0;
|
|
514
|
+
return (0, external_remote_config_js_namespaceObject.loadRemoteConfig)(this._requestRuntime, controller?.signal, ()=>!this._closing && !this._disposed && !this._blocked);
|
|
515
|
+
}),
|
|
516
|
+
timeoutResult
|
|
517
|
+
]).then((remoteConfig)=>{
|
|
518
|
+
if (this._closing || this._disposed) {
|
|
519
|
+
invalidated = true;
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
this._latestRemoteConfigResult = remoteConfig ? {
|
|
523
|
+
ok: true,
|
|
524
|
+
config: remoteConfig
|
|
525
|
+
} : {
|
|
526
|
+
ok: false
|
|
527
|
+
};
|
|
528
|
+
if (remoteConfig) this._remoteConfig = remoteConfig;
|
|
529
|
+
this._remoteConfigPublisher.publish(this._latestRemoteConfigResult);
|
|
530
|
+
return remoteConfig;
|
|
531
|
+
}).catch((error)=>{
|
|
532
|
+
if (this._closing || this._disposed) {
|
|
533
|
+
invalidated = true;
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
this.logger.error('Remote configuration failed', error);
|
|
537
|
+
this._latestRemoteConfigResult = {
|
|
538
|
+
ok: false
|
|
539
|
+
};
|
|
540
|
+
this._remoteConfigPublisher.publish(this._latestRemoteConfigResult);
|
|
541
|
+
}).finally(()=>{
|
|
542
|
+
try {
|
|
543
|
+
controller?.abort();
|
|
544
|
+
} catch {}
|
|
545
|
+
if (void 0 !== timeout) try {
|
|
546
|
+
globalThis.clearTimeout(timeout);
|
|
547
|
+
} catch {}
|
|
548
|
+
if (this._cancelRemoteConfigWait === cancelWait) this._cancelRemoteConfigWait = void 0;
|
|
549
|
+
if (invalidated) this._remoteConfigPromise = void 0;
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
return this._remoteConfigPromise;
|
|
553
|
+
}
|
|
554
|
+
getExtension(name) {
|
|
555
|
+
return this._registry.get(name);
|
|
556
|
+
}
|
|
557
|
+
shutdown(shutdownTimeoutMs = DEFAULT_SHUTDOWN_TIMEOUT_MS) {
|
|
558
|
+
if (!this._shutdownPromise) {
|
|
559
|
+
this._closing = true;
|
|
560
|
+
const extensionFlush = this._registry.flush('shutdown');
|
|
561
|
+
this._removePageviewListener();
|
|
562
|
+
try {
|
|
563
|
+
this._consentObservation.dispose();
|
|
564
|
+
} catch {}
|
|
565
|
+
try {
|
|
566
|
+
this._cancelRemoteConfigWait?.();
|
|
567
|
+
} catch {}
|
|
568
|
+
this._shutdownPromise = this._shutdown(shutdownTimeoutMs, extensionFlush);
|
|
569
|
+
}
|
|
570
|
+
return this._shutdownPromise;
|
|
571
|
+
}
|
|
572
|
+
dispose() {
|
|
573
|
+
return this.shutdown();
|
|
574
|
+
}
|
|
575
|
+
async _shutdown(shutdownTimeoutMs, extensionFlush) {
|
|
576
|
+
const timeoutMs = Math.max(0, Math.floor(Number.isFinite(shutdownTimeoutMs) ? shutdownTimeoutMs : DEFAULT_SHUTDOWN_TIMEOUT_MS));
|
|
577
|
+
let timer;
|
|
578
|
+
let timedOut = false;
|
|
579
|
+
const timeout = new Promise((resolve)=>{
|
|
580
|
+
try {
|
|
581
|
+
timer = globalThis.setTimeout(()=>{
|
|
582
|
+
timedOut = true;
|
|
583
|
+
resolve();
|
|
584
|
+
}, timeoutMs);
|
|
585
|
+
} catch {
|
|
586
|
+
timedOut = true;
|
|
587
|
+
resolve();
|
|
588
|
+
}
|
|
589
|
+
});
|
|
590
|
+
try {
|
|
591
|
+
await Promise.race([
|
|
592
|
+
extensionFlush,
|
|
593
|
+
timeout
|
|
594
|
+
]);
|
|
595
|
+
this._disposed = true;
|
|
596
|
+
try {
|
|
597
|
+
this._state.dispose();
|
|
598
|
+
} catch (error) {
|
|
599
|
+
this.logger.error('Failed to dispose browser state', error);
|
|
600
|
+
}
|
|
601
|
+
this._captureSink = void 0;
|
|
602
|
+
const cleanup = this._registry.dispose().catch((error)=>this.logger.error('Failed to dispose extensions', error));
|
|
603
|
+
this._remoteConfigPublisher.dispose();
|
|
604
|
+
this._eventPublisher.dispose();
|
|
605
|
+
this._identifyPublisher.dispose();
|
|
606
|
+
this._groupPublisher.dispose();
|
|
607
|
+
this._resetPublisher.dispose();
|
|
608
|
+
this._consentChangePublisher.dispose();
|
|
609
|
+
this._newSessionPublisher.dispose();
|
|
610
|
+
this._dynamicEventProperties.splice(0);
|
|
611
|
+
await Promise.race([
|
|
612
|
+
cleanup,
|
|
613
|
+
timeout
|
|
614
|
+
]);
|
|
615
|
+
if (timedOut) this.logger.warn(`PostHog shutdown stopped waiting for cleanup after ${timeoutMs}ms`);
|
|
616
|
+
} catch (error) {
|
|
617
|
+
this.logger.error('PostHog shutdown failed', error);
|
|
618
|
+
} finally{
|
|
619
|
+
if (void 0 !== timer) try {
|
|
620
|
+
globalThis.clearTimeout(timer);
|
|
621
|
+
} catch {}
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
_startInitialPageview() {
|
|
625
|
+
if (!this._capturePageview || !this._initialPageviewPending || this._closing || this._disposed) return;
|
|
626
|
+
let document;
|
|
627
|
+
let visibility;
|
|
628
|
+
try {
|
|
629
|
+
document = globalThis.document;
|
|
630
|
+
visibility = document?.visibilityState;
|
|
631
|
+
} catch {
|
|
632
|
+
this._removePageviewListener();
|
|
633
|
+
this._initialPageviewPending = false;
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
if (!document || void 0 === visibility) {
|
|
637
|
+
this._removePageviewListener();
|
|
638
|
+
this._initialPageviewPending = false;
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
if ('visible' !== visibility) {
|
|
642
|
+
if (this._pageviewListener?.[0] !== document) this._removePageviewListener();
|
|
643
|
+
if (!this._pageviewListener) {
|
|
644
|
+
const listener = ()=>this._startInitialPageview();
|
|
645
|
+
this._pageviewListener = [
|
|
646
|
+
document,
|
|
647
|
+
listener
|
|
648
|
+
];
|
|
649
|
+
try {
|
|
650
|
+
document.addEventListener('visibilitychange', listener);
|
|
651
|
+
} catch {
|
|
652
|
+
this._removePageviewListener();
|
|
653
|
+
this._initialPageviewPending = false;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
this._removePageviewListener();
|
|
659
|
+
this._initialPageviewPending = false;
|
|
660
|
+
if (!this._capture('$pageview')) this._initialPageviewPending = !this._disposed && !this._blocked;
|
|
661
|
+
}
|
|
662
|
+
_removePageviewListener() {
|
|
663
|
+
const listener = this._pageviewListener;
|
|
664
|
+
this._pageviewListener = void 0;
|
|
665
|
+
if (listener) try {
|
|
666
|
+
listener[0].removeEventListener('visibilitychange', listener[1]);
|
|
667
|
+
} catch {}
|
|
668
|
+
}
|
|
669
|
+
_observeConsent(storage, observeNativeStorage) {
|
|
670
|
+
if (!storage) return {
|
|
671
|
+
dispose () {}
|
|
672
|
+
};
|
|
673
|
+
const refresh = ()=>{
|
|
674
|
+
if (!this._disposed) this._state.refreshConsent();
|
|
675
|
+
};
|
|
676
|
+
const storageListener = (event)=>{
|
|
677
|
+
try {
|
|
678
|
+
const storageEvent = event;
|
|
679
|
+
if (storageEvent.storageArea === storage && (null === storageEvent.key || storageEvent.key === this._state.consentKey)) {
|
|
680
|
+
if (!this._disposed) this._state.observeConsent(storageEvent.newValue);
|
|
681
|
+
}
|
|
682
|
+
} catch {}
|
|
683
|
+
};
|
|
684
|
+
const localListener = (event)=>{
|
|
685
|
+
try {
|
|
686
|
+
if (event.detail === this._state.consentKey) refresh();
|
|
687
|
+
} catch {}
|
|
688
|
+
};
|
|
689
|
+
let adapter;
|
|
690
|
+
let storageListening = false;
|
|
691
|
+
let localListening = false;
|
|
692
|
+
if (observeNativeStorage) try {
|
|
693
|
+
globalThis.addEventListener('storage', storageListener);
|
|
694
|
+
storageListening = true;
|
|
695
|
+
} catch {}
|
|
696
|
+
try {
|
|
697
|
+
globalThis.addEventListener(CONSENT_CHANGE_EVENT, localListener);
|
|
698
|
+
localListening = true;
|
|
699
|
+
} catch {}
|
|
700
|
+
try {
|
|
701
|
+
adapter = storage?.subscribe?.(this._state.consentKey, refresh);
|
|
702
|
+
} catch {}
|
|
703
|
+
return {
|
|
704
|
+
dispose: ()=>{
|
|
705
|
+
if (storageListening) {
|
|
706
|
+
try {
|
|
707
|
+
globalThis.removeEventListener('storage', storageListener);
|
|
708
|
+
} catch {}
|
|
709
|
+
storageListening = false;
|
|
710
|
+
}
|
|
711
|
+
if (localListening) {
|
|
712
|
+
try {
|
|
713
|
+
globalThis.removeEventListener(CONSENT_CHANGE_EVENT, localListener);
|
|
714
|
+
} catch {}
|
|
715
|
+
localListening = false;
|
|
716
|
+
}
|
|
717
|
+
try {
|
|
718
|
+
adapter?.dispose();
|
|
719
|
+
} catch {}
|
|
720
|
+
}
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
_dispatchConsentChange() {
|
|
724
|
+
try {
|
|
725
|
+
globalThis.dispatchEvent(new CustomEvent(CONSENT_CHANGE_EVENT, {
|
|
726
|
+
detail: this._state.consentKey
|
|
727
|
+
}));
|
|
728
|
+
} catch {}
|
|
729
|
+
}
|
|
730
|
+
_createExtensionClient(extensionName) {
|
|
731
|
+
const host = this;
|
|
732
|
+
const logger = this.logger.createLogger(extensionName);
|
|
733
|
+
const kv = this._state.keyValueStore(extensionName, ()=>!this._closing && !this._disposed && this._state.prepare());
|
|
734
|
+
return {
|
|
735
|
+
get distinctId () {
|
|
736
|
+
return host.distinctId;
|
|
737
|
+
},
|
|
738
|
+
get anonymousId () {
|
|
739
|
+
return host.anonymousId;
|
|
740
|
+
},
|
|
741
|
+
get deviceId () {
|
|
742
|
+
return host.deviceId;
|
|
743
|
+
},
|
|
744
|
+
get library () {
|
|
745
|
+
return host.library;
|
|
746
|
+
},
|
|
747
|
+
get initialPersonProperties () {
|
|
748
|
+
return host.initialPersonProperties;
|
|
749
|
+
},
|
|
750
|
+
get groups () {
|
|
751
|
+
return host.groups;
|
|
752
|
+
},
|
|
753
|
+
get session () {
|
|
754
|
+
return host.session;
|
|
755
|
+
},
|
|
756
|
+
get isOptedOut () {
|
|
757
|
+
return host.isOptedOut;
|
|
758
|
+
},
|
|
759
|
+
get canCapture () {
|
|
760
|
+
return host.canCapture;
|
|
761
|
+
},
|
|
762
|
+
get isRateLimited () {
|
|
763
|
+
return host.isRateLimited;
|
|
764
|
+
},
|
|
765
|
+
capture: (event, properties, options)=>host.capture(event, properties, options),
|
|
766
|
+
registerDynamicEventProperties: (producer)=>host.registerDynamicEventProperties(producer),
|
|
767
|
+
getExtension: (name)=>host.getExtension(name),
|
|
768
|
+
get projectToken () {
|
|
769
|
+
return host.projectToken;
|
|
770
|
+
},
|
|
771
|
+
sendRequest: (path, init)=>host.sendRequest(path, init),
|
|
772
|
+
onConsentChange: host.onConsentChange,
|
|
773
|
+
onRemoteConfig: host.onRemoteConfig,
|
|
774
|
+
onEvent: host.onEvent,
|
|
775
|
+
onIdentify: host.onIdentify,
|
|
776
|
+
onGroup: host.onGroup,
|
|
777
|
+
onReset: host.onReset,
|
|
778
|
+
onSession: host.onSession,
|
|
779
|
+
kv,
|
|
780
|
+
logger
|
|
781
|
+
};
|
|
782
|
+
}
|
|
783
|
+
_canDeliver() {
|
|
784
|
+
return !this._disposed && !this._blocked && !this.hasOptedOut();
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
const createPostHogCore = async (options, configured = options?.extensions ?? [])=>{
|
|
788
|
+
const analytics = configured.find(external_analytics_internal_js_namespaceObject.isAnalyticsExtension) ?? (0, external_analytics_buffer_js_namespaceObject.createAnalyticsExtension)();
|
|
789
|
+
const extensions = configured.filter((extension)=>extension !== analytics);
|
|
790
|
+
const client = PostHogBrowserClient.create(options ?? {
|
|
791
|
+
projectToken: ''
|
|
792
|
+
}, analytics);
|
|
793
|
+
client.getRemoteConfig();
|
|
794
|
+
let analyticsReady = false;
|
|
795
|
+
try {
|
|
796
|
+
await client._registry.install(analytics);
|
|
797
|
+
analytics.initialize({
|
|
798
|
+
runtime: client._requestRuntime,
|
|
799
|
+
canRetry: ()=>client._canDeliver(),
|
|
800
|
+
reportFailure: (error)=>client.logger.error('Event delivery failed', error),
|
|
801
|
+
reportWarning: (message)=>client.logger.warn(message),
|
|
802
|
+
onAvailable: ()=>client._startInitialPageview()
|
|
803
|
+
});
|
|
804
|
+
analyticsReady = true;
|
|
805
|
+
} catch (error) {
|
|
806
|
+
client._captureSink = void 0;
|
|
807
|
+
try {
|
|
808
|
+
await client._registry.rollback(analytics);
|
|
809
|
+
} catch (cleanupError) {
|
|
810
|
+
client.logger.error(`Failed to dispose disabled extension "${analytics.name}"`, cleanupError);
|
|
811
|
+
}
|
|
812
|
+
client.logger.error(`Failed to install configured extension "${analytics.name}"`, error);
|
|
813
|
+
}
|
|
814
|
+
for (const extension of extensions)try {
|
|
815
|
+
await client._registry.install(extension);
|
|
816
|
+
} catch (error) {
|
|
817
|
+
client.logger.error(`Failed to install configured extension "${extension.name}"`, error);
|
|
818
|
+
}
|
|
819
|
+
if (analyticsReady) await analytics.start();
|
|
820
|
+
client._startInitialPageview();
|
|
821
|
+
return client;
|
|
822
|
+
};
|
|
823
|
+
exports.createPostHogCore = __webpack_exports__.createPostHogCore;
|
|
824
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
825
|
+
"createPostHogCore"
|
|
826
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
827
|
+
Object.defineProperty(exports, '__esModule', {
|
|
828
|
+
value: true
|
|
829
|
+
});
|