@posthog/browser 0.4.2 → 0.6.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/ARCHITECTURE.md +36 -48
- package/README.md +97 -69
- package/dist/composition/npm/core.d.ts +3 -4
- package/dist/composition/npm/core.js +2 -1
- package/dist/composition/npm/core.mjs +2 -1
- package/dist/composition/npm/index.d.ts +4 -5
- package/dist/composition/npm/index.js +2 -1
- package/dist/composition/npm/index.mjs +2 -1
- package/dist/composition/npm/root-composition.d.ts +4 -4
- package/dist/composition/npm/root-composition.js +0 -16
- package/dist/composition/npm/root-composition.mjs +0 -16
- package/dist/core/context/browser-client.d.ts +4 -35
- package/dist/core/extensions/registry.d.ts +15 -36
- package/dist/core/extensions/registry.js +67 -198
- package/dist/core/extensions/registry.mjs +67 -198
- package/dist/core/posthog.d.ts +3 -2
- package/dist/core/posthog.js +190 -173
- package/dist/core/posthog.mjs +191 -174
- package/dist/core/state/state.d.ts +33 -3
- package/dist/core/state/state.js +122 -27
- package/dist/core/state/state.mjs +107 -27
- package/dist/core/types.d.ts +134 -14
- package/dist/persistence/cookie.d.ts +19 -0
- package/dist/persistence/cookie.js +273 -0
- package/dist/persistence/cookie.mjs +235 -0
- package/dist/platform/logger.d.ts +5 -1
- package/dist/platform/logger.js +3 -3
- package/dist/platform/logger.mjs +3 -3
- package/dist/primitives/id.d.ts +5 -0
- package/dist/primitives/id.js +10 -2
- package/dist/primitives/id.mjs +6 -1
- package/dist/primitives/version.js +1 -1
- package/dist/primitives/version.mjs +1 -1
- package/dist/products/analytics/analytics-core.d.ts +89 -0
- package/dist/products/analytics/analytics-core.js +563 -0
- package/dist/products/analytics/analytics-core.mjs +516 -0
- package/dist/products/analytics/analytics-internal.d.ts +16 -37
- package/dist/products/analytics/analytics-internal.js +2 -10
- package/dist/products/analytics/analytics-internal.mjs +1 -3
- package/dist/products/analytics/analytics-options.d.ts +0 -9
- package/dist/products/analytics/analytics-options.js +1 -38
- package/dist/products/analytics/analytics-options.mjs +1 -17
- package/dist/products/analytics/delivery/analytics-delivery.js +1 -1
- package/dist/products/analytics/delivery/analytics-delivery.mjs +1 -1
- package/dist/products/analytics/delivery/lane.d.ts +8 -1
- package/dist/products/analytics/delivery/lane.js +16 -4
- package/dist/products/analytics/delivery/lane.mjs +16 -4
- package/dist/products/analytics/groups.d.ts +8 -0
- package/dist/products/analytics/groups.js +84 -0
- package/dist/products/analytics/groups.mjs +40 -0
- package/dist/products/analytics/person.d.ts +11 -0
- package/dist/products/analytics/person.js +91 -0
- package/dist/products/analytics/person.mjs +41 -0
- package/dist/products/analytics/properties/analytics-properties.d.ts +1 -0
- package/dist/products/analytics/properties/analytics-properties.js +3 -0
- package/dist/products/analytics/properties/analytics-properties.mjs +1 -1
- package/dist/products/autocapture/autocapture.js +2 -6
- package/dist/products/autocapture/autocapture.mjs +2 -6
- package/dist/products/flags/flags-internal.d.ts +4 -2
- package/dist/products/flags/flags-options.d.ts +16 -2
- package/dist/products/flags/flags-token.d.ts +31 -4
- package/dist/products/flags/flags.d.ts +1 -1
- package/dist/products/flags/flags.js +112 -11
- package/dist/products/flags/flags.mjs +112 -11
- package/dist/products/logs/logs.js +2 -5
- package/dist/products/logs/logs.mjs +2 -5
- package/dist/products/surveys/surveys-extension.js +78 -46
- package/dist/products/surveys/surveys-extension.mjs +80 -48
- package/dist/products/surveys/surveys-internal.d.ts +6 -0
- package/package.json +7 -7
- package/dist/products/analytics/analytics-api.d.ts +0 -32
- package/dist/products/analytics/analytics-api.js +0 -18
- package/dist/products/analytics/analytics-api.mjs +0 -1
- package/dist/products/analytics/analytics-buffer.d.ts +0 -4
- package/dist/products/analytics/analytics-buffer.js +0 -41
- package/dist/products/analytics/analytics-buffer.mjs +0 -3
- package/dist/products/analytics/analytics-extension.d.ts +0 -87
- package/dist/products/analytics/analytics-extension.js +0 -649
- package/dist/products/analytics/analytics-extension.mjs +0 -611
- package/dist/products/analytics/analytics.d.ts +0 -6
- package/dist/products/analytics/analytics.js +0 -42
- package/dist/products/analytics/analytics.mjs +0 -4
- package/dist/products/analytics/automatic-analytics.d.ts +0 -3
- package/dist/products/analytics/automatic-analytics.js +0 -44
- package/dist/products/analytics/automatic-analytics.mjs +0 -6
package/dist/core/posthog.js
CHANGED
|
@@ -32,16 +32,29 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
32
32
|
});
|
|
33
33
|
const external_remote_config_js_namespaceObject = require("./remote-config.js");
|
|
34
34
|
const pubsub_namespaceObject = require("@posthog/browser-common/pubsub");
|
|
35
|
-
const
|
|
35
|
+
const analytics_core_js_namespaceObject = require("../products/analytics/analytics-core.js");
|
|
36
|
+
const groups_js_namespaceObject = require("../products/analytics/groups.js");
|
|
37
|
+
const person_js_namespaceObject = require("../products/analytics/person.js");
|
|
36
38
|
const bot_filter_js_namespaceObject = require("./state/bot-filter.js");
|
|
37
39
|
const registry_js_namespaceObject = require("./extensions/registry.js");
|
|
38
40
|
const immutable_js_namespaceObject = require("../primitives/immutable.js");
|
|
39
41
|
const logger_js_namespaceObject = require("../platform/logger.js");
|
|
42
|
+
const person_processing_js_namespaceObject = require("../products/analytics/properties/person-processing.js");
|
|
40
43
|
const request_js_namespaceObject = require("../platform/request.js");
|
|
41
44
|
const state_js_namespaceObject = require("./state/state.js");
|
|
42
45
|
const version_js_namespaceObject = require("../primitives/version.js");
|
|
43
46
|
const CONSENT_CHANGE_EVENT = '__posthog_browser_consent_change__';
|
|
44
47
|
const DEFAULT_SHUTDOWN_TIMEOUT_MS = 30000;
|
|
48
|
+
const EARLY_ACCESS_CATALOGUE_PATH = /^\/api\/early_access_features\/\?token=[^&#]*(&stage=(concept|alpha|beta|general-availability))*&?$/;
|
|
49
|
+
const resolveRequestPath = (host, path)=>{
|
|
50
|
+
try {
|
|
51
|
+
const base = new URL(`${host}/`);
|
|
52
|
+
const { pathname } = new URL(`.${path}`, base);
|
|
53
|
+
return pathname.startsWith(base.pathname) ? pathname.slice(base.pathname.length - 1) : void 0;
|
|
54
|
+
} catch {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
45
58
|
const normalizeHost = (host)=>{
|
|
46
59
|
let end = host.length;
|
|
47
60
|
while(end > 0 && 47 === host.charCodeAt(end - 1))end--;
|
|
@@ -72,15 +85,16 @@ const getDefaultNavigator = ()=>{
|
|
|
72
85
|
}
|
|
73
86
|
};
|
|
74
87
|
class PostHog {
|
|
75
|
-
static create(options, loaders) {
|
|
76
|
-
return new PostHog(options, loaders);
|
|
88
|
+
static create(options, createDelivery, loaders) {
|
|
89
|
+
return new PostHog(options, createDelivery, loaders);
|
|
77
90
|
}
|
|
78
|
-
constructor(options, loaders){
|
|
91
|
+
constructor(options, createDelivery, loaders){
|
|
79
92
|
this.library = Object.freeze({
|
|
80
93
|
name: 'web',
|
|
81
94
|
version: version_js_namespaceObject.version
|
|
82
95
|
});
|
|
83
96
|
this._immediateAuthority = {};
|
|
97
|
+
this._clearedChoiceRetainsWork = false;
|
|
84
98
|
this._closing = false;
|
|
85
99
|
this._disposed = false;
|
|
86
100
|
this._initialPageviewPending = true;
|
|
@@ -91,7 +105,10 @@ class PostHog {
|
|
|
91
105
|
} catch {}
|
|
92
106
|
this.projectToken = projectToken;
|
|
93
107
|
this._capturePageview = options.capturePageview ?? true;
|
|
94
|
-
this.
|
|
108
|
+
this._logState = {
|
|
109
|
+
enabled: options.debug ?? false
|
|
110
|
+
};
|
|
111
|
+
this.logger = (0, logger_js_namespaceObject.createLogger)('[PostHog]', this._logState);
|
|
95
112
|
if (!projectToken) this.logger.error('A PostHog project token is required');
|
|
96
113
|
const initialPersonProperties = options.initialPersonProperties;
|
|
97
114
|
try {
|
|
@@ -100,29 +117,25 @@ class PostHog {
|
|
|
100
117
|
this.initialPersonProperties = Object.freeze({});
|
|
101
118
|
}
|
|
102
119
|
this._remoteConfigPublisher = new pubsub_namespaceObject.Publisher((error)=>this.logger.error('A remote configuration listener failed', error));
|
|
103
|
-
this._eventPublisher = new pubsub_namespaceObject.Publisher((error)=>this.logger.error('An event listener failed', error));
|
|
104
|
-
this._identifyPublisher = new pubsub_namespaceObject.Publisher((error)=>this.logger.error('An identify listener failed', error));
|
|
105
|
-
this._changePropertiesPublisher = new pubsub_namespaceObject.Publisher((error)=>this.logger.error('A person-property listener failed', error));
|
|
106
|
-
this._groupPublisher = new pubsub_namespaceObject.Publisher((error)=>this.logger.error('A group listener failed', error));
|
|
107
|
-
this._groupsResetPublisher = new pubsub_namespaceObject.Publisher((error)=>this.logger.error('A group reset listener failed', error));
|
|
108
|
-
this._resetPublisher = new pubsub_namespaceObject.Publisher((error)=>this.logger.error('A reset listener failed', error));
|
|
109
|
-
this._newSessionPublisher = new pubsub_namespaceObject.Publisher((error)=>this.logger.error('A session listener failed', error));
|
|
110
120
|
this._consentChangePublisher = new pubsub_namespaceObject.Publisher((error)=>this.logger.error('A consent change listener failed', error));
|
|
111
121
|
const browserNavigator = false === options.navigator ? void 0 : options.navigator ?? getDefaultNavigator();
|
|
112
122
|
const browserFetch = false === options.fetch ? void 0 : options.fetch ?? getDefaultFetch();
|
|
113
123
|
this._blocked = !projectToken || !(options.disableBotDetection ?? false) && (0, bot_filter_js_namespaceObject.isLikelyBot)(browserNavigator, options.blockedUserAgents ?? []);
|
|
114
124
|
const requestedStorage = false === options.storage ? void 0 : options.storage ?? (0, state_js_namespaceObject.getDefaultStorage)();
|
|
115
125
|
const storage = this._blocked ? void 0 : requestedStorage;
|
|
116
|
-
this._state = new state_js_namespaceObject.BrowserState(projectToken, storage, options.persistenceKey, options.consentPersistenceName, options.optOutByDefault ?? false, !this._blocked && void 0 === options.storage && void 0 !== requestedStorage ? state_js_namespaceObject.getDefaultSessionStorage : void 0, (consent, previous)=>{
|
|
117
|
-
if ('denied'
|
|
118
|
-
this.
|
|
119
|
-
this.
|
|
120
|
-
}
|
|
121
|
-
this.
|
|
126
|
+
this._state = new state_js_namespaceObject.BrowserState(projectToken, storage, options.persistenceKey, options.consentPersistenceName, options.optOutByDefault ?? false, !this._blocked && void 0 === options.storage && void 0 !== requestedStorage ? state_js_namespaceObject.getDefaultSessionStorage : void 0, (consent, previous, explicit)=>{
|
|
127
|
+
if ('denied' !== consent) {
|
|
128
|
+
if (this._clearedChoiceRetainsWork) Promise.resolve().then(()=>this._analytics?.flush());
|
|
129
|
+
this._clearedChoiceRetainsWork = false;
|
|
130
|
+
} else if (explicit) this._revokeAdmittedWork();
|
|
131
|
+
else this._clearedChoiceRetainsWork = true;
|
|
132
|
+
const change = {
|
|
122
133
|
current: consent,
|
|
123
134
|
previous
|
|
124
|
-
}
|
|
125
|
-
|
|
135
|
+
};
|
|
136
|
+
if (this._heldConsentChanges) this._heldConsentChanges.push(change);
|
|
137
|
+
else this._consentChangePublisher.publish(change);
|
|
138
|
+
}, options.cookie);
|
|
126
139
|
this._consentObservation = this._observeConsent(storage, !this._blocked && void 0 === options.storage && void 0 !== requestedStorage);
|
|
127
140
|
const apiHost = normalizeHost(options.apiHost ?? 'https://us.i.posthog.com');
|
|
128
141
|
this._requestRuntime = [
|
|
@@ -137,7 +150,8 @@ class PostHog {
|
|
|
137
150
|
];
|
|
138
151
|
this._remoteConfig = options.remoteConfig;
|
|
139
152
|
this._remoteConfigTimeoutMs = void 0 !== options.remoteConfigTimeoutMs && Number.isFinite(options.remoteConfigTimeoutMs) ? Math.max(0, options.remoteConfigTimeoutMs) : 10000;
|
|
140
|
-
this.kv = this._state.keyValueStore(
|
|
153
|
+
this.kv = this._state.keyValueStore(state_js_namespaceObject.CLIENT_KV_NAMESPACE, ()=>this._canMutate() && this._state.prepare());
|
|
154
|
+
this._sessionKv = this._state.tabKeyValueStore('analytics', ()=>this._canMutate() && this._state.prepare());
|
|
141
155
|
this._latestRemoteConfigResult = this._remoteConfig ? {
|
|
142
156
|
ok: true,
|
|
143
157
|
config: this._remoteConfig
|
|
@@ -155,15 +169,33 @@ class PostHog {
|
|
|
155
169
|
else if (!this._closing && !this._disposed) this.getRemoteConfig();
|
|
156
170
|
return subscription;
|
|
157
171
|
};
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
172
|
+
let analyticsOptions = {};
|
|
173
|
+
try {
|
|
174
|
+
analyticsOptions = options.analytics ?? {};
|
|
175
|
+
if (false === analyticsOptions) this.logger.warn('The `analytics: false` option was removed in this version, so events are captured. Set `capturePageview: false` to skip the initial $pageview and, on the root entrypoint, `autocapture: false` to turn off autocapture.');
|
|
176
|
+
} catch {}
|
|
177
|
+
this._analytics = new analytics_core_js_namespaceObject.Analytics(this, analyticsOptions, createDelivery);
|
|
178
|
+
this.onEvent = this._analytics._eventPublisher.listener;
|
|
179
|
+
this.onIdentify = this._analytics._identifyPublisher.listener;
|
|
180
|
+
this.onChangeProperties = this._analytics._changePropertiesPublisher.listener;
|
|
181
|
+
this.onGroup = this._analytics._groupPublisher.listener;
|
|
182
|
+
this.onGroupsReset = this._analytics._groupsResetPublisher.listener;
|
|
183
|
+
this.onReset = this._analytics._resetPublisher.listener;
|
|
184
|
+
this.onNewSession = this._analytics._newSessionPublisher.listener;
|
|
162
185
|
this.onConsentChange = this._consentChangePublisher.listener;
|
|
163
|
-
this.onNewSession = this._newSessionPublisher.listener;
|
|
164
186
|
this.onSession = (listener)=>this.onNewSession((session)=>listener(session.sessionId));
|
|
165
|
-
this.
|
|
166
|
-
|
|
187
|
+
this.onSessionId = (handler)=>{
|
|
188
|
+
const subscription = this._analytics._sessionIdPublisher.listener(handler);
|
|
189
|
+
const session = this._canMutate() ? this.session : void 0;
|
|
190
|
+
if (session) try {
|
|
191
|
+
handler(session);
|
|
192
|
+
} catch (error) {
|
|
193
|
+
this.logger.error('A session ID listener failed', error);
|
|
194
|
+
}
|
|
195
|
+
return subscription;
|
|
196
|
+
};
|
|
197
|
+
this._registry = new registry_js_namespaceObject.ExtensionRegistry(this, this.logger, loaders, ()=>this._canMutate());
|
|
198
|
+
if (options.bootstrap) this._analytics.bootstrap(options.bootstrap);
|
|
167
199
|
}
|
|
168
200
|
get distinctId() {
|
|
169
201
|
this._state.prepare();
|
|
@@ -191,8 +223,87 @@ class PostHog {
|
|
|
191
223
|
get canCapture() {
|
|
192
224
|
return !this._closing && !this._disposed && !this._blocked && !this.hasOptedOut() && this._state.prepare();
|
|
193
225
|
}
|
|
226
|
+
get isRateLimited() {
|
|
227
|
+
return this._analytics.isRateLimited;
|
|
228
|
+
}
|
|
229
|
+
capture(event, properties, options) {
|
|
230
|
+
return this._analytics.capture(event, properties, options);
|
|
231
|
+
}
|
|
232
|
+
captureImmediate(event, properties, options) {
|
|
233
|
+
return this._analytics.captureImmediate(event, properties, options);
|
|
234
|
+
}
|
|
235
|
+
identify(distinctId, set, setOnce) {
|
|
236
|
+
this._analytics.identify(distinctId, set, setOnce);
|
|
237
|
+
}
|
|
238
|
+
reset(options) {
|
|
239
|
+
if (!this._canMutate()) return;
|
|
240
|
+
const held = this._heldConsentChanges = [];
|
|
241
|
+
let bootstrap;
|
|
242
|
+
try {
|
|
243
|
+
this.clearOptInOut();
|
|
244
|
+
bootstrap = this._analytics.reset(options);
|
|
245
|
+
} finally{
|
|
246
|
+
this._heldConsentChanges = void 0;
|
|
247
|
+
}
|
|
248
|
+
for (const change of held)this._consentChangePublisher.publish(change);
|
|
249
|
+
if (bootstrap) this.featureFlags?.bootstrap?.(bootstrap);
|
|
250
|
+
}
|
|
251
|
+
register(properties) {
|
|
252
|
+
this._analytics.register(properties);
|
|
253
|
+
}
|
|
254
|
+
registerOnce(properties, defaultValue) {
|
|
255
|
+
this._analytics.registerOnce(properties, defaultValue);
|
|
256
|
+
}
|
|
257
|
+
unregister(property) {
|
|
258
|
+
this._analytics.unregister(property);
|
|
259
|
+
}
|
|
260
|
+
getProperty(property) {
|
|
261
|
+
return this._analytics.getProperty(property);
|
|
262
|
+
}
|
|
263
|
+
registerForSession(properties) {
|
|
264
|
+
this._analytics.registerForSession(properties);
|
|
265
|
+
}
|
|
266
|
+
unregisterForSession(property) {
|
|
267
|
+
this._analytics.unregisterForSession(property);
|
|
268
|
+
}
|
|
269
|
+
alias(aliasId) {
|
|
270
|
+
person_js_namespaceObject.alias(this, aliasId);
|
|
271
|
+
}
|
|
272
|
+
group(type, key, properties) {
|
|
273
|
+
groups_js_namespaceObject.group(this, type, key, properties);
|
|
274
|
+
}
|
|
275
|
+
groupIdentify(type, key, properties, options) {
|
|
276
|
+
groups_js_namespaceObject.groupIdentify(this, type, key, properties, options);
|
|
277
|
+
}
|
|
278
|
+
resetGroups() {
|
|
279
|
+
groups_js_namespaceObject.resetGroups(this);
|
|
280
|
+
}
|
|
281
|
+
createPersonProfile() {
|
|
282
|
+
person_js_namespaceObject.createPersonProfile(this);
|
|
283
|
+
}
|
|
284
|
+
setPersonProperties(set, setOnce) {
|
|
285
|
+
person_js_namespaceObject.setPersonProperties(this, set, setOnce);
|
|
286
|
+
}
|
|
287
|
+
unsetPersonProperties(names) {
|
|
288
|
+
person_js_namespaceObject.unsetPersonProperties(this, names);
|
|
289
|
+
}
|
|
290
|
+
setInternalOrTestUser() {
|
|
291
|
+
person_js_namespaceObject.setInternalOrTestUser(this);
|
|
292
|
+
}
|
|
293
|
+
registerDynamicEventProperties(producer) {
|
|
294
|
+
return this._analytics.registerDynamicEventProperties(producer);
|
|
295
|
+
}
|
|
296
|
+
getPageViewId() {
|
|
297
|
+
return this._analytics._pageviewIdFor(this._state.activeSessionId);
|
|
298
|
+
}
|
|
299
|
+
getExtension(name) {
|
|
300
|
+
return this._registry.get(name);
|
|
301
|
+
}
|
|
194
302
|
async flush() {
|
|
195
|
-
await
|
|
303
|
+
await Promise.all([
|
|
304
|
+
this._registry.flush(),
|
|
305
|
+
this._analytics.flush()
|
|
306
|
+
]);
|
|
196
307
|
}
|
|
197
308
|
optIn() {
|
|
198
309
|
if (this._disposed) return;
|
|
@@ -200,13 +311,25 @@ class PostHog {
|
|
|
200
311
|
this._startInitialPageview();
|
|
201
312
|
}
|
|
202
313
|
optOut() {
|
|
203
|
-
if (
|
|
314
|
+
if (this._disposed) return;
|
|
315
|
+
if (this._clearedChoiceRetainsWork) this._revokeAdmittedWork();
|
|
316
|
+
if (this._state.optOut()) this._dispatchConsentChange();
|
|
204
317
|
}
|
|
205
318
|
hasOptedOut() {
|
|
206
319
|
return 'denied' === this._state.consent;
|
|
207
320
|
}
|
|
321
|
+
getExplicitConsentStatus() {
|
|
322
|
+
return this._state.consentChoice ?? 'pending';
|
|
323
|
+
}
|
|
324
|
+
clearOptInOut() {
|
|
325
|
+
if (this._disposed) return;
|
|
326
|
+
if (this._state.clearConsent()) this._dispatchConsentChange();
|
|
327
|
+
}
|
|
208
328
|
sendRequest(path, init) {
|
|
209
|
-
|
|
329
|
+
const options = init ?? {};
|
|
330
|
+
const resolvedPath = resolveRequestPath(this._requestRuntime[0][options.target ?? 'api'], path);
|
|
331
|
+
const allowedWhileOptedOut = ('flags' === options.target ? '/flags/' === resolvedPath : EARLY_ACCESS_CATALOGUE_PATH.test(path) && (options.method ?? 'GET') === 'GET' && void 0 === options.body) && !options.query && !options.headers;
|
|
332
|
+
return (0, request_js_namespaceObject.sendRequest)(this._requestRuntime, path, options, ()=>!this._blocked && (allowedWhileOptedOut || !this.hasOptedOut()));
|
|
210
333
|
}
|
|
211
334
|
async getRemoteConfig() {
|
|
212
335
|
if (this._closing || this._disposed) return;
|
|
@@ -280,13 +403,31 @@ class PostHog {
|
|
|
280
403
|
}
|
|
281
404
|
return this._remoteConfigPromise;
|
|
282
405
|
}
|
|
406
|
+
setConfig(options) {
|
|
407
|
+
try {
|
|
408
|
+
const debug = options.debug;
|
|
409
|
+
if ('boolean' == typeof debug) this._logState.enabled = debug;
|
|
410
|
+
const personProfiles = options.analytics?.personProfiles;
|
|
411
|
+
if (void 0 !== personProfiles) this._analytics._personProfiles = (0, person_processing_js_namespaceObject.snapshotPersonProfileMode)(()=>personProfiles);
|
|
412
|
+
} catch (error) {
|
|
413
|
+
this.logger.error('Configuration update failed', error);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
debug(enabled = true) {
|
|
417
|
+
this.setConfig({
|
|
418
|
+
debug: enabled
|
|
419
|
+
});
|
|
420
|
+
}
|
|
283
421
|
resolveExtension(name) {
|
|
284
422
|
return this._closing || this._disposed ? Promise.resolve(void 0) : this._registry.resolve(name);
|
|
285
423
|
}
|
|
286
424
|
shutdown(shutdownTimeoutMs = DEFAULT_SHUTDOWN_TIMEOUT_MS) {
|
|
287
425
|
if (!this._shutdownPromise) {
|
|
288
426
|
this._closing = true;
|
|
289
|
-
const extensionFlush =
|
|
427
|
+
const extensionFlush = Promise.all([
|
|
428
|
+
this._registry.flush('shutdown'),
|
|
429
|
+
this._analytics.flush()
|
|
430
|
+
]).then(()=>void 0);
|
|
290
431
|
this._removePageviewListener();
|
|
291
432
|
try {
|
|
292
433
|
this._consentObservation.dispose();
|
|
@@ -327,17 +468,12 @@ class PostHog {
|
|
|
327
468
|
} catch (error) {
|
|
328
469
|
this.logger.error('Failed to dispose browser state', error);
|
|
329
470
|
}
|
|
330
|
-
|
|
331
|
-
|
|
471
|
+
const cleanup = Promise.all([
|
|
472
|
+
this._registry.dispose(),
|
|
473
|
+
this._analytics.dispose()
|
|
474
|
+
]).catch((error)=>this.logger.error('Failed to dispose extensions', error));
|
|
332
475
|
this._remoteConfigPublisher.dispose();
|
|
333
|
-
this._eventPublisher.dispose();
|
|
334
|
-
this._identifyPublisher.dispose();
|
|
335
|
-
this._changePropertiesPublisher.dispose();
|
|
336
|
-
this._groupPublisher.dispose();
|
|
337
|
-
this._groupsResetPublisher.dispose();
|
|
338
|
-
this._resetPublisher.dispose();
|
|
339
476
|
this._consentChangePublisher.dispose();
|
|
340
|
-
this._newSessionPublisher.dispose();
|
|
341
477
|
await Promise.race([
|
|
342
478
|
cleanup,
|
|
343
479
|
timeout
|
|
@@ -351,16 +487,13 @@ class PostHog {
|
|
|
351
487
|
} catch {}
|
|
352
488
|
}
|
|
353
489
|
}
|
|
354
|
-
|
|
355
|
-
await this._analytics?.start();
|
|
490
|
+
_completeInitialization() {
|
|
356
491
|
this._ready = true;
|
|
357
492
|
this._startInitialPageview();
|
|
358
|
-
await this._pageviewLoading;
|
|
359
493
|
}
|
|
360
494
|
_startInitialPageview() {
|
|
361
495
|
if (!this._ready || !this._capturePageview || !this._initialPageviewPending || this._closing || this._disposed || !this._pageviewIsVisible()) return;
|
|
362
496
|
this._removePageviewListener();
|
|
363
|
-
if (!this._analytics) return void this._loadInitialPageviewAnalytics();
|
|
364
497
|
this._initialPageviewPending = false;
|
|
365
498
|
if (!this._analytics.captureInitialPageview()) this._initialPageviewPending = !this._disposed && !this._blocked;
|
|
366
499
|
}
|
|
@@ -402,13 +535,6 @@ class PostHog {
|
|
|
402
535
|
this._removePageviewListener();
|
|
403
536
|
this._initialPageviewPending = false;
|
|
404
537
|
}
|
|
405
|
-
_loadInitialPageviewAnalytics() {
|
|
406
|
-
if (this._pageviewLoading || this._blocked || this.hasOptedOut()) return;
|
|
407
|
-
this._pageviewLoading = this.resolveExtension('analytics').then(()=>{
|
|
408
|
-
this._pageviewLoading = void 0;
|
|
409
|
-
if (this._analytics) this._startInitialPageview();
|
|
410
|
-
});
|
|
411
|
-
}
|
|
412
538
|
_removePageviewListener() {
|
|
413
539
|
const listener = this._pageviewListener;
|
|
414
540
|
this._pageviewListener = void 0;
|
|
@@ -477,124 +603,16 @@ class PostHog {
|
|
|
477
603
|
}));
|
|
478
604
|
} catch {}
|
|
479
605
|
}
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
const logger = this.logger.createLogger(extensionName);
|
|
483
|
-
const kv = this._state.keyValueStore(extensionName, ()=>!this._closing && !this._disposed && this._state.prepare());
|
|
484
|
-
return {
|
|
485
|
-
get distinctId () {
|
|
486
|
-
return host.distinctId;
|
|
487
|
-
},
|
|
488
|
-
get anonymousId () {
|
|
489
|
-
return host.anonymousId;
|
|
490
|
-
},
|
|
491
|
-
get deviceId () {
|
|
492
|
-
return host.deviceId;
|
|
493
|
-
},
|
|
494
|
-
get library () {
|
|
495
|
-
return host.library;
|
|
496
|
-
},
|
|
497
|
-
get initialPersonProperties () {
|
|
498
|
-
return host.initialPersonProperties;
|
|
499
|
-
},
|
|
500
|
-
get groups () {
|
|
501
|
-
return host.groups;
|
|
502
|
-
},
|
|
503
|
-
get session () {
|
|
504
|
-
return host.session;
|
|
505
|
-
},
|
|
506
|
-
get isOptedOut () {
|
|
507
|
-
return host.isOptedOut;
|
|
508
|
-
},
|
|
509
|
-
get canCapture () {
|
|
510
|
-
return host.canCapture;
|
|
511
|
-
},
|
|
512
|
-
get isRateLimited () {
|
|
513
|
-
return host._analytics?.isRateLimited ?? false;
|
|
514
|
-
},
|
|
515
|
-
capture: (event, properties, options)=>host._analytics?.capture(event, properties, options) ?? true,
|
|
516
|
-
registerDynamicEventProperties: (producer)=>host._analytics?.registerDynamicEventProperties(producer) ?? {
|
|
517
|
-
dispose () {}
|
|
518
|
-
},
|
|
519
|
-
getExtension: (name)=>host._registry.getShared(name),
|
|
520
|
-
get projectToken () {
|
|
521
|
-
return host.projectToken;
|
|
522
|
-
},
|
|
523
|
-
sendRequest: (path, init)=>host.sendRequest(path, init),
|
|
524
|
-
onRemoteConfig: host.onRemoteConfig,
|
|
525
|
-
onEvent: host._eventPublisher.listener,
|
|
526
|
-
onSession: host.onSession,
|
|
527
|
-
kv,
|
|
528
|
-
logger
|
|
529
|
-
};
|
|
530
|
-
}
|
|
531
|
-
_createExtensionClient(extensionName, resolve) {
|
|
532
|
-
const host = this;
|
|
533
|
-
const sharedClient = this._createSharedExtensionClient(extensionName);
|
|
534
|
-
const { getExtension: _lookup, capture: _capture, registerDynamicEventProperties: _registration, isRateLimited: _rateLimit, ...capabilities } = Object.getOwnPropertyDescriptors(sharedClient);
|
|
535
|
-
const browserClient = Object.defineProperties({
|
|
536
|
-
sharedClient,
|
|
537
|
-
resolveExtension: (name)=>this._closing || this._disposed ? Promise.resolve(void 0) : resolve(name),
|
|
538
|
-
onConsentChange: host.onConsentChange,
|
|
539
|
-
onIdentify: host.onIdentify,
|
|
540
|
-
onChangeProperties: host._changePropertiesPublisher.listener,
|
|
541
|
-
onGroup: host.onGroup,
|
|
542
|
-
onGroupsReset: host.onGroupsReset,
|
|
543
|
-
onReset: host.onReset
|
|
544
|
-
}, capabilities);
|
|
545
|
-
this._registry.expose(browserClient);
|
|
546
|
-
return browserClient;
|
|
547
|
-
}
|
|
548
|
-
async _setupExtension(extension, host) {
|
|
549
|
-
if (this._closing || this._disposed) throw new Error('The browser client is closing');
|
|
550
|
-
if (!(0, analytics_internal_js_namespaceObject.isAnalyticsExtension)(extension)) {
|
|
551
|
-
await extension.setup(host);
|
|
552
|
-
if (this._closing || this._disposed) throw new Error('The browser client is closing');
|
|
553
|
-
return;
|
|
554
|
-
}
|
|
555
|
-
this._analytics = extension;
|
|
556
|
-
try {
|
|
557
|
-
extension.bindCaptureHost(this._analyticsCaptureHost(extension));
|
|
558
|
-
await extension.setup(host);
|
|
559
|
-
if (this._closing || this._disposed) throw new Error('The browser client is closing');
|
|
560
|
-
extension.initialize({
|
|
561
|
-
runtime: this._requestRuntime,
|
|
562
|
-
canRetry: ()=>this._canDeliver(),
|
|
563
|
-
reportFailure: (error)=>this.logger.error('Event delivery failed', error),
|
|
564
|
-
reportWarning: (message)=>this.logger.warn(message),
|
|
565
|
-
onAvailable: ()=>this._startInitialPageview()
|
|
566
|
-
});
|
|
567
|
-
} catch (error) {
|
|
568
|
-
if (this._analytics === extension) this._analytics = void 0;
|
|
569
|
-
throw error;
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
_canUseAnalytics(owner) {
|
|
573
|
-
return this._analytics === owner && !this._closing && !this._disposed;
|
|
574
|
-
}
|
|
575
|
-
_analyticsCaptureHost(owner) {
|
|
576
|
-
const canMutate = ()=>this._canUseAnalytics(owner);
|
|
577
|
-
return {
|
|
578
|
-
projectToken: this.projectToken,
|
|
579
|
-
logger: this.logger,
|
|
580
|
-
sessionKv: this._state.tabKeyValueStore('analytics', ()=>canMutate() && this._state.prepare()),
|
|
581
|
-
state: this._state,
|
|
582
|
-
canCapture: ()=>canMutate() && !this._blocked && !this.hasOptedOut(),
|
|
583
|
-
canMutate,
|
|
584
|
-
isDisposed: ()=>this._disposed,
|
|
585
|
-
authority: ()=>this._immediateAuthority,
|
|
586
|
-
onEvent: this._eventPublisher.listener,
|
|
587
|
-
publishEvent: (event)=>this._eventPublisher.publish(event),
|
|
588
|
-
publishNewSession: (session)=>this._newSessionPublisher.publish(session),
|
|
589
|
-
publishIdentify: (info)=>this._identifyPublisher.publish(info),
|
|
590
|
-
publishChangeProperties: (info)=>this._changePropertiesPublisher.publish(info),
|
|
591
|
-
publishGroup: (info)=>this._groupPublisher.publish(info),
|
|
592
|
-
publishGroupsReset: ()=>this._groupsResetPublisher.publish(void 0),
|
|
593
|
-
publishReset: ()=>this._resetPublisher.publish(void 0)
|
|
594
|
-
};
|
|
606
|
+
_canMutate() {
|
|
607
|
+
return !this._closing && !this._disposed;
|
|
595
608
|
}
|
|
596
609
|
_canDeliver() {
|
|
597
|
-
return !this._disposed && !this._blocked && !this.hasOptedOut();
|
|
610
|
+
return !this._disposed && !this._blocked && (!this.hasOptedOut() || this._clearedChoiceRetainsWork && void 0 === this._state.consentChoice);
|
|
611
|
+
}
|
|
612
|
+
_revokeAdmittedWork() {
|
|
613
|
+
this._clearedChoiceRetainsWork = false;
|
|
614
|
+
this._immediateAuthority = {};
|
|
615
|
+
this._analytics?.purge();
|
|
598
616
|
}
|
|
599
617
|
}
|
|
600
618
|
const selectConfiguredExtensions = (configured)=>{
|
|
@@ -602,7 +620,7 @@ const selectConfiguredExtensions = (configured)=>{
|
|
|
602
620
|
for (const extension of configured)if (!selected.has(extension.name)) selected.set(extension.name, extension);
|
|
603
621
|
return selected;
|
|
604
622
|
};
|
|
605
|
-
const createPostHogCore = async (options, configured = options?.extensions ?? [], automaticLoaders = {}, automatic = [])=>{
|
|
623
|
+
const createPostHogCore = async (options, createDelivery, configured = options?.extensions ?? [], automaticLoaders = {}, automatic = [])=>{
|
|
606
624
|
const selected = selectConfiguredExtensions(configured);
|
|
607
625
|
const loaders = Object.fromEntries([
|
|
608
626
|
...Object.entries(automaticLoaders),
|
|
@@ -613,12 +631,11 @@ const createPostHogCore = async (options, configured = options?.extensions ?? []
|
|
|
613
631
|
]);
|
|
614
632
|
const client = PostHog.create(options ?? {
|
|
615
633
|
projectToken: ''
|
|
616
|
-
}, loaders);
|
|
634
|
+
}, createDelivery, loaders);
|
|
617
635
|
client.getRemoteConfig();
|
|
618
|
-
|
|
619
|
-
for (const name of selected.keys())if ('analytics' !== name) await client.resolveExtension(name);
|
|
636
|
+
for (const name of selected.keys())await client.resolveExtension(name);
|
|
620
637
|
for (const extension of automatic)await client._registry.activate(extension);
|
|
621
|
-
|
|
638
|
+
client._completeInitialization();
|
|
622
639
|
return client;
|
|
623
640
|
};
|
|
624
641
|
exports.createPostHogCore = __webpack_exports__.createPostHogCore;
|