@posthog/browser 0.4.2 → 0.5.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 +34 -46
- package/README.md +56 -63
- package/dist/composition/npm/core.d.ts +2 -3
- package/dist/composition/npm/core.js +2 -1
- package/dist/composition/npm/core.mjs +2 -1
- package/dist/composition/npm/index.d.ts +3 -4
- 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 +104 -161
- package/dist/core/posthog.mjs +105 -162
- package/dist/core/state/state.d.ts +1 -0
- package/dist/core/state/state.js +5 -2
- package/dist/core/state/state.mjs +3 -3
- package/dist/core/types.d.ts +50 -12
- package/dist/primitives/version.js +1 -1
- package/dist/primitives/version.mjs +1 -1
- package/dist/products/analytics/analytics-core.d.ts +76 -0
- package/dist/products/analytics/analytics-core.js +472 -0
- package/dist/products/analytics/analytics-core.mjs +425 -0
- package/dist/products/analytics/analytics-internal.d.ts +14 -36
- 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/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/autocapture/autocapture.js +2 -6
- package/dist/products/autocapture/autocapture.mjs +2 -6
- package/dist/products/flags/flags-internal.d.ts +1 -2
- package/dist/products/flags/flags-token.d.ts +3 -2
- package/dist/products/flags/flags.js +2 -7
- package/dist/products/flags/flags.mjs +2 -7
- package/dist/products/logs/logs.js +2 -5
- package/dist/products/logs/logs.mjs +2 -5
- package/dist/products/surveys/surveys-extension.js +4 -21
- package/dist/products/surveys/surveys-extension.mjs +4 -21
- package/package.json +4 -9
- 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.mjs
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { loadRemoteConfig } from "./remote-config.mjs";
|
|
2
2
|
import { Publisher } from "@posthog/browser-common/pubsub";
|
|
3
|
-
import {
|
|
3
|
+
import { Analytics } from "../products/analytics/analytics-core.mjs";
|
|
4
4
|
import { isLikelyBot } from "./state/bot-filter.mjs";
|
|
5
5
|
import { ExtensionRegistry } from "./extensions/registry.mjs";
|
|
6
6
|
import { deepFreeze } from "../primitives/immutable.mjs";
|
|
7
7
|
import { createLogger } from "../platform/logger.mjs";
|
|
8
8
|
import { sendRequest } from "../platform/request.mjs";
|
|
9
|
-
import { BrowserState, getDefaultSessionStorage, getDefaultStorage } from "./state/state.mjs";
|
|
9
|
+
import { BrowserState, CLIENT_KV_NAMESPACE, getDefaultSessionStorage, getDefaultStorage } from "./state/state.mjs";
|
|
10
10
|
import { version } from "../primitives/version.mjs";
|
|
11
|
+
import * as __rspack_external__products_analytics_groups_mjs_441733a9 from "../products/analytics/groups.mjs";
|
|
12
|
+
import * as __rspack_external__products_analytics_person_mjs_3dfa168e from "../products/analytics/person.mjs";
|
|
11
13
|
const CONSENT_CHANGE_EVENT = '__posthog_browser_consent_change__';
|
|
12
14
|
const DEFAULT_SHUTDOWN_TIMEOUT_MS = 30000;
|
|
13
15
|
const normalizeHost = (host)=>{
|
|
@@ -40,10 +42,10 @@ const getDefaultNavigator = ()=>{
|
|
|
40
42
|
}
|
|
41
43
|
};
|
|
42
44
|
class PostHog {
|
|
43
|
-
static create(options, loaders) {
|
|
44
|
-
return new PostHog(options, loaders);
|
|
45
|
+
static create(options, createDelivery, loaders) {
|
|
46
|
+
return new PostHog(options, createDelivery, loaders);
|
|
45
47
|
}
|
|
46
|
-
constructor(options, loaders){
|
|
48
|
+
constructor(options, createDelivery, loaders){
|
|
47
49
|
this.library = Object.freeze({
|
|
48
50
|
name: 'web',
|
|
49
51
|
version: version
|
|
@@ -68,13 +70,6 @@ class PostHog {
|
|
|
68
70
|
this.initialPersonProperties = Object.freeze({});
|
|
69
71
|
}
|
|
70
72
|
this._remoteConfigPublisher = new Publisher((error)=>this.logger.error('A remote configuration listener failed', error));
|
|
71
|
-
this._eventPublisher = new Publisher((error)=>this.logger.error('An event listener failed', error));
|
|
72
|
-
this._identifyPublisher = new Publisher((error)=>this.logger.error('An identify listener failed', error));
|
|
73
|
-
this._changePropertiesPublisher = new Publisher((error)=>this.logger.error('A person-property listener failed', error));
|
|
74
|
-
this._groupPublisher = new Publisher((error)=>this.logger.error('A group listener failed', error));
|
|
75
|
-
this._groupsResetPublisher = new Publisher((error)=>this.logger.error('A group reset listener failed', error));
|
|
76
|
-
this._resetPublisher = new Publisher((error)=>this.logger.error('A reset listener failed', error));
|
|
77
|
-
this._newSessionPublisher = new Publisher((error)=>this.logger.error('A session listener failed', error));
|
|
78
73
|
this._consentChangePublisher = new Publisher((error)=>this.logger.error('A consent change listener failed', error));
|
|
79
74
|
const browserNavigator = false === options.navigator ? void 0 : options.navigator ?? getDefaultNavigator();
|
|
80
75
|
const browserFetch = false === options.fetch ? void 0 : options.fetch ?? getDefaultFetch();
|
|
@@ -105,7 +100,8 @@ class PostHog {
|
|
|
105
100
|
];
|
|
106
101
|
this._remoteConfig = options.remoteConfig;
|
|
107
102
|
this._remoteConfigTimeoutMs = void 0 !== options.remoteConfigTimeoutMs && Number.isFinite(options.remoteConfigTimeoutMs) ? Math.max(0, options.remoteConfigTimeoutMs) : 10000;
|
|
108
|
-
this.kv = this._state.keyValueStore(
|
|
103
|
+
this.kv = this._state.keyValueStore(CLIENT_KV_NAMESPACE, ()=>this._canMutate() && this._state.prepare());
|
|
104
|
+
this._sessionKv = this._state.tabKeyValueStore('analytics', ()=>this._canMutate() && this._state.prepare());
|
|
109
105
|
this._latestRemoteConfigResult = this._remoteConfig ? {
|
|
110
106
|
ok: true,
|
|
111
107
|
config: this._remoteConfig
|
|
@@ -123,15 +119,22 @@ class PostHog {
|
|
|
123
119
|
else if (!this._closing && !this._disposed) this.getRemoteConfig();
|
|
124
120
|
return subscription;
|
|
125
121
|
};
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
122
|
+
let analyticsOptions = {};
|
|
123
|
+
try {
|
|
124
|
+
analyticsOptions = options.analytics ?? {};
|
|
125
|
+
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.');
|
|
126
|
+
} catch {}
|
|
127
|
+
this._analytics = new Analytics(this, analyticsOptions, createDelivery);
|
|
128
|
+
this.onEvent = this._analytics._eventPublisher.listener;
|
|
129
|
+
this.onIdentify = this._analytics._identifyPublisher.listener;
|
|
130
|
+
this.onChangeProperties = this._analytics._changePropertiesPublisher.listener;
|
|
131
|
+
this.onGroup = this._analytics._groupPublisher.listener;
|
|
132
|
+
this.onGroupsReset = this._analytics._groupsResetPublisher.listener;
|
|
133
|
+
this.onReset = this._analytics._resetPublisher.listener;
|
|
134
|
+
this.onNewSession = this._analytics._newSessionPublisher.listener;
|
|
130
135
|
this.onConsentChange = this._consentChangePublisher.listener;
|
|
131
|
-
this.onNewSession = this._newSessionPublisher.listener;
|
|
132
136
|
this.onSession = (listener)=>this.onNewSession((session)=>listener(session.sessionId));
|
|
133
|
-
this._registry = new ExtensionRegistry(
|
|
134
|
-
this._registry.expose(this);
|
|
137
|
+
this._registry = new ExtensionRegistry(this, this.logger, loaders, ()=>this._canMutate());
|
|
135
138
|
}
|
|
136
139
|
get distinctId() {
|
|
137
140
|
this._state.prepare();
|
|
@@ -159,8 +162,74 @@ class PostHog {
|
|
|
159
162
|
get canCapture() {
|
|
160
163
|
return !this._closing && !this._disposed && !this._blocked && !this.hasOptedOut() && this._state.prepare();
|
|
161
164
|
}
|
|
165
|
+
get isRateLimited() {
|
|
166
|
+
return this._analytics.isRateLimited;
|
|
167
|
+
}
|
|
168
|
+
capture(event, properties, options) {
|
|
169
|
+
return this._analytics.capture(event, properties, options);
|
|
170
|
+
}
|
|
171
|
+
captureImmediate(event, properties, options) {
|
|
172
|
+
return this._analytics.captureImmediate(event, properties, options);
|
|
173
|
+
}
|
|
174
|
+
identify(distinctId, set, setOnce) {
|
|
175
|
+
this._analytics.identify(distinctId, set, setOnce);
|
|
176
|
+
}
|
|
177
|
+
reset() {
|
|
178
|
+
this._analytics.reset();
|
|
179
|
+
}
|
|
180
|
+
register(properties) {
|
|
181
|
+
this._analytics.register(properties);
|
|
182
|
+
}
|
|
183
|
+
registerOnce(properties, defaultValue) {
|
|
184
|
+
this._analytics.registerOnce(properties, defaultValue);
|
|
185
|
+
}
|
|
186
|
+
unregister(property) {
|
|
187
|
+
this._analytics.unregister(property);
|
|
188
|
+
}
|
|
189
|
+
getProperty(property) {
|
|
190
|
+
return this._analytics.getProperty(property);
|
|
191
|
+
}
|
|
192
|
+
registerForSession(properties) {
|
|
193
|
+
this._analytics.registerForSession(properties);
|
|
194
|
+
}
|
|
195
|
+
unregisterForSession(property) {
|
|
196
|
+
this._analytics.unregisterForSession(property);
|
|
197
|
+
}
|
|
198
|
+
alias(aliasId) {
|
|
199
|
+
__rspack_external__products_analytics_person_mjs_3dfa168e.alias(this, aliasId);
|
|
200
|
+
}
|
|
201
|
+
group(type, key, properties) {
|
|
202
|
+
__rspack_external__products_analytics_groups_mjs_441733a9.group(this, type, key, properties);
|
|
203
|
+
}
|
|
204
|
+
groupIdentify(type, key, properties, options) {
|
|
205
|
+
__rspack_external__products_analytics_groups_mjs_441733a9.groupIdentify(this, type, key, properties, options);
|
|
206
|
+
}
|
|
207
|
+
resetGroups() {
|
|
208
|
+
__rspack_external__products_analytics_groups_mjs_441733a9.resetGroups(this);
|
|
209
|
+
}
|
|
210
|
+
createPersonProfile() {
|
|
211
|
+
__rspack_external__products_analytics_person_mjs_3dfa168e.createPersonProfile(this);
|
|
212
|
+
}
|
|
213
|
+
setPersonProperties(set, setOnce) {
|
|
214
|
+
__rspack_external__products_analytics_person_mjs_3dfa168e.setPersonProperties(this, set, setOnce);
|
|
215
|
+
}
|
|
216
|
+
unsetPersonProperties(names) {
|
|
217
|
+
__rspack_external__products_analytics_person_mjs_3dfa168e.unsetPersonProperties(this, names);
|
|
218
|
+
}
|
|
219
|
+
setInternalOrTestUser() {
|
|
220
|
+
__rspack_external__products_analytics_person_mjs_3dfa168e.setInternalOrTestUser(this);
|
|
221
|
+
}
|
|
222
|
+
registerDynamicEventProperties(producer) {
|
|
223
|
+
return this._analytics.registerDynamicEventProperties(producer);
|
|
224
|
+
}
|
|
225
|
+
getExtension(name) {
|
|
226
|
+
return this._registry.get(name);
|
|
227
|
+
}
|
|
162
228
|
async flush() {
|
|
163
|
-
await
|
|
229
|
+
await Promise.all([
|
|
230
|
+
this._registry.flush(),
|
|
231
|
+
this._analytics.flush()
|
|
232
|
+
]);
|
|
164
233
|
}
|
|
165
234
|
optIn() {
|
|
166
235
|
if (this._disposed) return;
|
|
@@ -254,7 +323,10 @@ class PostHog {
|
|
|
254
323
|
shutdown(shutdownTimeoutMs = DEFAULT_SHUTDOWN_TIMEOUT_MS) {
|
|
255
324
|
if (!this._shutdownPromise) {
|
|
256
325
|
this._closing = true;
|
|
257
|
-
const extensionFlush =
|
|
326
|
+
const extensionFlush = Promise.all([
|
|
327
|
+
this._registry.flush('shutdown'),
|
|
328
|
+
this._analytics.flush()
|
|
329
|
+
]).then(()=>void 0);
|
|
258
330
|
this._removePageviewListener();
|
|
259
331
|
try {
|
|
260
332
|
this._consentObservation.dispose();
|
|
@@ -295,17 +367,12 @@ class PostHog {
|
|
|
295
367
|
} catch (error) {
|
|
296
368
|
this.logger.error('Failed to dispose browser state', error);
|
|
297
369
|
}
|
|
298
|
-
|
|
299
|
-
|
|
370
|
+
const cleanup = Promise.all([
|
|
371
|
+
this._registry.dispose(),
|
|
372
|
+
this._analytics.dispose()
|
|
373
|
+
]).catch((error)=>this.logger.error('Failed to dispose extensions', error));
|
|
300
374
|
this._remoteConfigPublisher.dispose();
|
|
301
|
-
this._eventPublisher.dispose();
|
|
302
|
-
this._identifyPublisher.dispose();
|
|
303
|
-
this._changePropertiesPublisher.dispose();
|
|
304
|
-
this._groupPublisher.dispose();
|
|
305
|
-
this._groupsResetPublisher.dispose();
|
|
306
|
-
this._resetPublisher.dispose();
|
|
307
375
|
this._consentChangePublisher.dispose();
|
|
308
|
-
this._newSessionPublisher.dispose();
|
|
309
376
|
await Promise.race([
|
|
310
377
|
cleanup,
|
|
311
378
|
timeout
|
|
@@ -319,16 +386,13 @@ class PostHog {
|
|
|
319
386
|
} catch {}
|
|
320
387
|
}
|
|
321
388
|
}
|
|
322
|
-
|
|
323
|
-
await this._analytics?.start();
|
|
389
|
+
_completeInitialization() {
|
|
324
390
|
this._ready = true;
|
|
325
391
|
this._startInitialPageview();
|
|
326
|
-
await this._pageviewLoading;
|
|
327
392
|
}
|
|
328
393
|
_startInitialPageview() {
|
|
329
394
|
if (!this._ready || !this._capturePageview || !this._initialPageviewPending || this._closing || this._disposed || !this._pageviewIsVisible()) return;
|
|
330
395
|
this._removePageviewListener();
|
|
331
|
-
if (!this._analytics) return void this._loadInitialPageviewAnalytics();
|
|
332
396
|
this._initialPageviewPending = false;
|
|
333
397
|
if (!this._analytics.captureInitialPageview()) this._initialPageviewPending = !this._disposed && !this._blocked;
|
|
334
398
|
}
|
|
@@ -370,13 +434,6 @@ class PostHog {
|
|
|
370
434
|
this._removePageviewListener();
|
|
371
435
|
this._initialPageviewPending = false;
|
|
372
436
|
}
|
|
373
|
-
_loadInitialPageviewAnalytics() {
|
|
374
|
-
if (this._pageviewLoading || this._blocked || this.hasOptedOut()) return;
|
|
375
|
-
this._pageviewLoading = this.resolveExtension('analytics').then(()=>{
|
|
376
|
-
this._pageviewLoading = void 0;
|
|
377
|
-
if (this._analytics) this._startInitialPageview();
|
|
378
|
-
});
|
|
379
|
-
}
|
|
380
437
|
_removePageviewListener() {
|
|
381
438
|
const listener = this._pageviewListener;
|
|
382
439
|
this._pageviewListener = void 0;
|
|
@@ -445,121 +502,8 @@ class PostHog {
|
|
|
445
502
|
}));
|
|
446
503
|
} catch {}
|
|
447
504
|
}
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
const logger = this.logger.createLogger(extensionName);
|
|
451
|
-
const kv = this._state.keyValueStore(extensionName, ()=>!this._closing && !this._disposed && this._state.prepare());
|
|
452
|
-
return {
|
|
453
|
-
get distinctId () {
|
|
454
|
-
return host.distinctId;
|
|
455
|
-
},
|
|
456
|
-
get anonymousId () {
|
|
457
|
-
return host.anonymousId;
|
|
458
|
-
},
|
|
459
|
-
get deviceId () {
|
|
460
|
-
return host.deviceId;
|
|
461
|
-
},
|
|
462
|
-
get library () {
|
|
463
|
-
return host.library;
|
|
464
|
-
},
|
|
465
|
-
get initialPersonProperties () {
|
|
466
|
-
return host.initialPersonProperties;
|
|
467
|
-
},
|
|
468
|
-
get groups () {
|
|
469
|
-
return host.groups;
|
|
470
|
-
},
|
|
471
|
-
get session () {
|
|
472
|
-
return host.session;
|
|
473
|
-
},
|
|
474
|
-
get isOptedOut () {
|
|
475
|
-
return host.isOptedOut;
|
|
476
|
-
},
|
|
477
|
-
get canCapture () {
|
|
478
|
-
return host.canCapture;
|
|
479
|
-
},
|
|
480
|
-
get isRateLimited () {
|
|
481
|
-
return host._analytics?.isRateLimited ?? false;
|
|
482
|
-
},
|
|
483
|
-
capture: (event, properties, options)=>host._analytics?.capture(event, properties, options) ?? true,
|
|
484
|
-
registerDynamicEventProperties: (producer)=>host._analytics?.registerDynamicEventProperties(producer) ?? {
|
|
485
|
-
dispose () {}
|
|
486
|
-
},
|
|
487
|
-
getExtension: (name)=>host._registry.getShared(name),
|
|
488
|
-
get projectToken () {
|
|
489
|
-
return host.projectToken;
|
|
490
|
-
},
|
|
491
|
-
sendRequest: (path, init)=>host.sendRequest(path, init),
|
|
492
|
-
onRemoteConfig: host.onRemoteConfig,
|
|
493
|
-
onEvent: host._eventPublisher.listener,
|
|
494
|
-
onSession: host.onSession,
|
|
495
|
-
kv,
|
|
496
|
-
logger
|
|
497
|
-
};
|
|
498
|
-
}
|
|
499
|
-
_createExtensionClient(extensionName, resolve) {
|
|
500
|
-
const host = this;
|
|
501
|
-
const sharedClient = this._createSharedExtensionClient(extensionName);
|
|
502
|
-
const { getExtension: _lookup, capture: _capture, registerDynamicEventProperties: _registration, isRateLimited: _rateLimit, ...capabilities } = Object.getOwnPropertyDescriptors(sharedClient);
|
|
503
|
-
const browserClient = Object.defineProperties({
|
|
504
|
-
sharedClient,
|
|
505
|
-
resolveExtension: (name)=>this._closing || this._disposed ? Promise.resolve(void 0) : resolve(name),
|
|
506
|
-
onConsentChange: host.onConsentChange,
|
|
507
|
-
onIdentify: host.onIdentify,
|
|
508
|
-
onChangeProperties: host._changePropertiesPublisher.listener,
|
|
509
|
-
onGroup: host.onGroup,
|
|
510
|
-
onGroupsReset: host.onGroupsReset,
|
|
511
|
-
onReset: host.onReset
|
|
512
|
-
}, capabilities);
|
|
513
|
-
this._registry.expose(browserClient);
|
|
514
|
-
return browserClient;
|
|
515
|
-
}
|
|
516
|
-
async _setupExtension(extension, host) {
|
|
517
|
-
if (this._closing || this._disposed) throw new Error('The browser client is closing');
|
|
518
|
-
if (!isAnalyticsExtension(extension)) {
|
|
519
|
-
await extension.setup(host);
|
|
520
|
-
if (this._closing || this._disposed) throw new Error('The browser client is closing');
|
|
521
|
-
return;
|
|
522
|
-
}
|
|
523
|
-
this._analytics = extension;
|
|
524
|
-
try {
|
|
525
|
-
extension.bindCaptureHost(this._analyticsCaptureHost(extension));
|
|
526
|
-
await extension.setup(host);
|
|
527
|
-
if (this._closing || this._disposed) throw new Error('The browser client is closing');
|
|
528
|
-
extension.initialize({
|
|
529
|
-
runtime: this._requestRuntime,
|
|
530
|
-
canRetry: ()=>this._canDeliver(),
|
|
531
|
-
reportFailure: (error)=>this.logger.error('Event delivery failed', error),
|
|
532
|
-
reportWarning: (message)=>this.logger.warn(message),
|
|
533
|
-
onAvailable: ()=>this._startInitialPageview()
|
|
534
|
-
});
|
|
535
|
-
} catch (error) {
|
|
536
|
-
if (this._analytics === extension) this._analytics = void 0;
|
|
537
|
-
throw error;
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
_canUseAnalytics(owner) {
|
|
541
|
-
return this._analytics === owner && !this._closing && !this._disposed;
|
|
542
|
-
}
|
|
543
|
-
_analyticsCaptureHost(owner) {
|
|
544
|
-
const canMutate = ()=>this._canUseAnalytics(owner);
|
|
545
|
-
return {
|
|
546
|
-
projectToken: this.projectToken,
|
|
547
|
-
logger: this.logger,
|
|
548
|
-
sessionKv: this._state.tabKeyValueStore('analytics', ()=>canMutate() && this._state.prepare()),
|
|
549
|
-
state: this._state,
|
|
550
|
-
canCapture: ()=>canMutate() && !this._blocked && !this.hasOptedOut(),
|
|
551
|
-
canMutate,
|
|
552
|
-
isDisposed: ()=>this._disposed,
|
|
553
|
-
authority: ()=>this._immediateAuthority,
|
|
554
|
-
onEvent: this._eventPublisher.listener,
|
|
555
|
-
publishEvent: (event)=>this._eventPublisher.publish(event),
|
|
556
|
-
publishNewSession: (session)=>this._newSessionPublisher.publish(session),
|
|
557
|
-
publishIdentify: (info)=>this._identifyPublisher.publish(info),
|
|
558
|
-
publishChangeProperties: (info)=>this._changePropertiesPublisher.publish(info),
|
|
559
|
-
publishGroup: (info)=>this._groupPublisher.publish(info),
|
|
560
|
-
publishGroupsReset: ()=>this._groupsResetPublisher.publish(void 0),
|
|
561
|
-
publishReset: ()=>this._resetPublisher.publish(void 0)
|
|
562
|
-
};
|
|
505
|
+
_canMutate() {
|
|
506
|
+
return !this._closing && !this._disposed;
|
|
563
507
|
}
|
|
564
508
|
_canDeliver() {
|
|
565
509
|
return !this._disposed && !this._blocked && !this.hasOptedOut();
|
|
@@ -570,7 +514,7 @@ const selectConfiguredExtensions = (configured)=>{
|
|
|
570
514
|
for (const extension of configured)if (!selected.has(extension.name)) selected.set(extension.name, extension);
|
|
571
515
|
return selected;
|
|
572
516
|
};
|
|
573
|
-
const createPostHogCore = async (options, configured = options?.extensions ?? [], automaticLoaders = {}, automatic = [])=>{
|
|
517
|
+
const createPostHogCore = async (options, createDelivery, configured = options?.extensions ?? [], automaticLoaders = {}, automatic = [])=>{
|
|
574
518
|
const selected = selectConfiguredExtensions(configured);
|
|
575
519
|
const loaders = Object.fromEntries([
|
|
576
520
|
...Object.entries(automaticLoaders),
|
|
@@ -581,12 +525,11 @@ const createPostHogCore = async (options, configured = options?.extensions ?? []
|
|
|
581
525
|
]);
|
|
582
526
|
const client = PostHog.create(options ?? {
|
|
583
527
|
projectToken: ''
|
|
584
|
-
}, loaders);
|
|
528
|
+
}, createDelivery, loaders);
|
|
585
529
|
client.getRemoteConfig();
|
|
586
|
-
|
|
587
|
-
for (const name of selected.keys())if ('analytics' !== name) await client.resolveExtension(name);
|
|
530
|
+
for (const name of selected.keys())await client.resolveExtension(name);
|
|
588
531
|
for (const extension of automatic)await client._registry.activate(extension);
|
|
589
|
-
|
|
532
|
+
client._completeInitialization();
|
|
590
533
|
return client;
|
|
591
534
|
};
|
|
592
535
|
export { createPostHogCore };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { KeyValueStore, SessionContext } from '@posthog/browser-common';
|
|
2
2
|
import type { NewSessionReason, StorageLike } from '../types';
|
|
3
|
+
export declare const CLIENT_KV_NAMESPACE = "core";
|
|
3
4
|
export type ConsentState = 'implicit' | 'granted' | 'denied';
|
|
4
5
|
interface PersistedSession {
|
|
5
6
|
sessionId: string;
|
package/dist/core/state/state.js
CHANGED
|
@@ -29,12 +29,13 @@ var __webpack_exports__ = {};
|
|
|
29
29
|
__webpack_require__.r(__webpack_exports__);
|
|
30
30
|
__webpack_require__.d(__webpack_exports__, {
|
|
31
31
|
BrowserState: ()=>BrowserState,
|
|
32
|
+
CLIENT_KV_NAMESPACE: ()=>CLIENT_KV_NAMESPACE,
|
|
32
33
|
getDefaultSessionStorage: ()=>getDefaultSessionStorage,
|
|
33
34
|
getDefaultStorage: ()=>getDefaultStorage
|
|
34
35
|
});
|
|
35
36
|
const id_js_namespaceObject = require("../../primitives/id.js");
|
|
36
37
|
const external_key_value_store_js_namespaceObject = require("./key-value-store.js");
|
|
37
|
-
const
|
|
38
|
+
const CLIENT_KV_NAMESPACE = 'core';
|
|
38
39
|
const STORED_GROUP_PROPERTIES_KEY = '$stored_group_properties';
|
|
39
40
|
const emptyRecord = ()=>Object.create(null);
|
|
40
41
|
const isRecord = (value)=>'object' == typeof value && null !== value && !Array.isArray(value);
|
|
@@ -296,7 +297,7 @@ class BrowserState {
|
|
|
296
297
|
}
|
|
297
298
|
resetGroups() {
|
|
298
299
|
this._state.groups = emptyRecord();
|
|
299
|
-
const flagsData = this._state.extensionData[
|
|
300
|
+
const flagsData = this._state.extensionData[CLIENT_KV_NAMESPACE];
|
|
300
301
|
if (flagsData && STORED_GROUP_PROPERTIES_KEY in flagsData) delete flagsData[STORED_GROUP_PROPERTIES_KEY];
|
|
301
302
|
this._save();
|
|
302
303
|
}
|
|
@@ -611,10 +612,12 @@ class BrowserState {
|
|
|
611
612
|
}
|
|
612
613
|
}
|
|
613
614
|
exports.BrowserState = __webpack_exports__.BrowserState;
|
|
615
|
+
exports.CLIENT_KV_NAMESPACE = __webpack_exports__.CLIENT_KV_NAMESPACE;
|
|
614
616
|
exports.getDefaultSessionStorage = __webpack_exports__.getDefaultSessionStorage;
|
|
615
617
|
exports.getDefaultStorage = __webpack_exports__.getDefaultStorage;
|
|
616
618
|
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
617
619
|
"BrowserState",
|
|
620
|
+
"CLIENT_KV_NAMESPACE",
|
|
618
621
|
"getDefaultSessionStorage",
|
|
619
622
|
"getDefaultStorage"
|
|
620
623
|
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createId } from "../../primitives/id.mjs";
|
|
2
2
|
import { TabValues, createKeyValueStore } from "./key-value-store.mjs";
|
|
3
|
-
const
|
|
3
|
+
const CLIENT_KV_NAMESPACE = 'core';
|
|
4
4
|
const STORED_GROUP_PROPERTIES_KEY = '$stored_group_properties';
|
|
5
5
|
const emptyRecord = ()=>Object.create(null);
|
|
6
6
|
const isRecord = (value)=>'object' == typeof value && null !== value && !Array.isArray(value);
|
|
@@ -262,7 +262,7 @@ class BrowserState {
|
|
|
262
262
|
}
|
|
263
263
|
resetGroups() {
|
|
264
264
|
this._state.groups = emptyRecord();
|
|
265
|
-
const flagsData = this._state.extensionData[
|
|
265
|
+
const flagsData = this._state.extensionData[CLIENT_KV_NAMESPACE];
|
|
266
266
|
if (flagsData && STORED_GROUP_PROPERTIES_KEY in flagsData) delete flagsData[STORED_GROUP_PROPERTIES_KEY];
|
|
267
267
|
this._save();
|
|
268
268
|
}
|
|
@@ -576,4 +576,4 @@ class BrowserState {
|
|
|
576
576
|
}
|
|
577
577
|
}
|
|
578
578
|
}
|
|
579
|
-
export { BrowserState, getDefaultSessionStorage, getDefaultStorage };
|
|
579
|
+
export { BrowserState, CLIENT_KV_NAMESPACE, getDefaultSessionStorage, getDefaultStorage };
|
package/dist/core/types.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { FlagsConfiguration } from '../products/flags/flags-options';
|
|
2
|
-
import type {
|
|
2
|
+
import type { BrowserExtension as Extension, BrowserProducts, ChangePropertiesInfo, ConsentChangeInfo, GroupInfo, IdentifyInfo } from './context/browser-client';
|
|
3
3
|
import type { LogsConfiguration } from '../products/logs/logs-options';
|
|
4
4
|
import type { SurveysConfiguration } from '../products/surveys/surveys-options';
|
|
5
5
|
import type { AutocaptureConfiguration } from '../products/autocapture/autocapture-options';
|
|
6
|
-
import type {
|
|
7
|
-
import type { ApiResponse, CaptureOptions, Disposable, Listener, RemoteConfig, SendRequestInit, SessionContext } from '@posthog/browser-common';
|
|
6
|
+
import type { AnalyticsOptions, PersonProfileMode } from '../products/analytics/analytics-options';
|
|
7
|
+
import type { ApiResponse, CaptureOptions, Client, Disposable, ExtensionToken, Listener, RemoteConfig, SendRequestInit, SessionContext } from '@posthog/browser-common';
|
|
8
8
|
export interface StorageLike {
|
|
9
9
|
getItem(key: string): string | null;
|
|
10
10
|
setItem(key: string, value: string): void;
|
|
@@ -56,11 +56,8 @@ export interface PostHogOptions {
|
|
|
56
56
|
remoteConfig?: RemoteConfig;
|
|
57
57
|
/** Stop waiting for remote configuration after this duration in milliseconds. Defaults to 10,000. */
|
|
58
58
|
remoteConfigTimeoutMs?: number;
|
|
59
|
-
/**
|
|
60
|
-
|
|
61
|
-
* or the initial pageview. Pass `false` to omit implicit analytics; explicit instances take precedence.
|
|
62
|
-
*/
|
|
63
|
-
analytics?: AnalyticsConfiguration;
|
|
59
|
+
/** Capture scheduling and person-processing policy. */
|
|
60
|
+
analytics?: AnalyticsOptions;
|
|
64
61
|
/** Acquire DOM autocapture from saved/fresh signals, or during initialization when configured explicitly. */
|
|
65
62
|
autocapture?: AutocaptureConfiguration;
|
|
66
63
|
/** Dynamically include flags by default. Explicit extensions take precedence over this option. */
|
|
@@ -73,7 +70,7 @@ export interface PostHogOptions {
|
|
|
73
70
|
extensions?: readonly Extension[];
|
|
74
71
|
}
|
|
75
72
|
/** Options for the manually composed `@posthog/browser/core` entrypoint. */
|
|
76
|
-
export type CorePostHogOptions = Omit<PostHogOptions, '
|
|
73
|
+
export type CorePostHogOptions = Omit<PostHogOptions, 'flags' | 'logs' | 'surveys' | 'autocapture'>;
|
|
77
74
|
/** Capture V1's terminal verdict for one reported event. */
|
|
78
75
|
export type CaptureOutcomeStatus = 'ok' | 'warning' | 'drop' | 'retry';
|
|
79
76
|
/** A backend-reported Capture V1 event verdict. */
|
|
@@ -94,9 +91,50 @@ export interface CaptureSummary {
|
|
|
94
91
|
/** Operation-level failure, with any known partial results retained. Errors always set `allPersisted` to false. */
|
|
95
92
|
readonly error?: Error;
|
|
96
93
|
}
|
|
97
|
-
/**
|
|
98
|
-
export interface IPostHog extends
|
|
94
|
+
/** The one browser client: the shared `Client` contract plus browser lifecycle, identity and product access. */
|
|
95
|
+
export interface IPostHog extends Client, Readonly<Partial<BrowserProducts>>, Disposable {
|
|
96
|
+
readonly isRateLimited: boolean;
|
|
97
|
+
captureImmediate(event: string, properties?: Record<string, unknown> | null, options?: CaptureOptions): Promise<CaptureSummary>;
|
|
98
|
+
identify(distinctId: string, set?: Record<string, unknown>, setOnce?: Record<string, unknown>): void;
|
|
99
|
+
reset(): void;
|
|
100
|
+
register(properties: Record<string, unknown>): void;
|
|
101
|
+
registerOnce(properties: Record<string, unknown>, defaultValue?: unknown): void;
|
|
102
|
+
unregister(property: string): void;
|
|
103
|
+
getProperty(property: string): unknown;
|
|
104
|
+
registerForSession(properties: Record<string, unknown>): void;
|
|
105
|
+
unregisterForSession(property: string): void;
|
|
106
|
+
/** Links another distinct ID to the current person without changing the current identity. */
|
|
107
|
+
alias(aliasId: string): void;
|
|
108
|
+
/** Associates future events with a group and, when person processing is enabled, records its properties. */
|
|
109
|
+
group(type: string, key: string, properties?: Record<string, unknown>): void;
|
|
110
|
+
/** Sends a group-profile update without changing group membership. */
|
|
111
|
+
groupIdentify(type: string, key: string | number, properties?: Record<string, unknown>, options?: CaptureOptions): void;
|
|
112
|
+
/** Clears group memberships, retaining identity, sessions and registered properties. */
|
|
113
|
+
resetGroups(): void;
|
|
114
|
+
/** Enables person processing for the current identity without identifying it. */
|
|
115
|
+
createPersonProfile(): void;
|
|
116
|
+
/** Sets person properties for the current identity. */
|
|
117
|
+
setPersonProperties(set?: Record<string, unknown>, setOnce?: Record<string, unknown>): void;
|
|
118
|
+
/** Removes person properties from the current identity. */
|
|
119
|
+
unsetPersonProperties(names: string | readonly string[]): void;
|
|
120
|
+
/** Marks the current person as an internal or test user. */
|
|
121
|
+
setInternalOrTestUser(): void;
|
|
99
122
|
readonly onNewSession: Listener<NewSessionInfo>;
|
|
123
|
+
/** Fires when consent changes, including changes observed from another tab. Does not replay. */
|
|
124
|
+
readonly onConsentChange: Listener<ConsentChangeInfo>;
|
|
125
|
+
/** Fires after identification, independently of capture consent. Does not replay. */
|
|
126
|
+
readonly onIdentify: Listener<IdentifyInfo>;
|
|
127
|
+
/** Local person-property changes, independently of capture consent. Does not replay. */
|
|
128
|
+
readonly onChangeProperties: Listener<ChangePropertiesInfo>;
|
|
129
|
+
/** Fires after group membership or property updates, independently of capture consent. Does not replay. */
|
|
130
|
+
readonly onGroup: Listener<GroupInfo>;
|
|
131
|
+
/** Fires after group-only reset clears local membership. Does not replay. */
|
|
132
|
+
readonly onGroupsReset: Listener<void>;
|
|
133
|
+
/** Fires after local identity and persisted extension state are reset. Does not replay. */
|
|
134
|
+
readonly onReset: Listener<void>;
|
|
135
|
+
resolveExtension<T extends Extension>(token: ExtensionToken<T>): Promise<T | undefined>;
|
|
136
|
+
resolveExtension<Name extends keyof BrowserProducts>(name: Name): Promise<BrowserProducts[Name] | undefined>;
|
|
137
|
+
resolveExtension<T extends Extension = Extension>(name: string): Promise<T | undefined>;
|
|
100
138
|
flush(): Promise<void>;
|
|
101
139
|
shutdown(shutdownTimeoutMs?: number): Promise<void>;
|
|
102
140
|
dispose(): Promise<void>;
|
|
@@ -105,4 +143,4 @@ export interface IPostHog extends ICoreCapabilities, BrowserExtensionAccess, Dis
|
|
|
105
143
|
hasOptedOut(): boolean;
|
|
106
144
|
getRemoteConfig(): Promise<RemoteConfig | undefined>;
|
|
107
145
|
}
|
|
108
|
-
export type {
|
|
146
|
+
export type { AnalyticsOptions, PersonProfileMode, ApiResponse, CaptureOptions, Disposable, Extension, RemoteConfig, SendRequestInit, SessionContext, };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const version = "0.
|
|
1
|
+
const version = "0.5.0";
|
|
2
2
|
export { version };
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { CaptureOptions, CapturedEventInfo, Disposable } from '@posthog/browser-common';
|
|
2
|
+
import { Publisher } from '@posthog/browser-common/pubsub';
|
|
3
|
+
import { type AnalyticsCore, type AnalyticsDeliveryFactory, type AnalyticsTeardownSubscription } from './analytics-internal';
|
|
4
|
+
import type { ChangePropertiesInfo, GroupInfo, IdentifyInfo } from '../../core/context/browser-client';
|
|
5
|
+
import type { NewSessionInfo, CaptureSummary } from '../../core/types';
|
|
6
|
+
import type { AnalyticsOptions, PersonProfileMode } from './analytics-options';
|
|
7
|
+
import { type PersonProperties } from './properties/person-processing';
|
|
8
|
+
export declare const isNonEmptyString: (value: unknown) => value is string;
|
|
9
|
+
export declare const isValidDistinctId: (value: unknown) => value is string;
|
|
10
|
+
interface PersonPropertiesAdmission {
|
|
11
|
+
readonly signature: string | undefined;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The client's capture pipeline: admission, identity operations, buffering and delivery.
|
|
15
|
+
* Members prefixed with `_` are package-internal and used by the optional groups and person modules.
|
|
16
|
+
*/
|
|
17
|
+
export declare class Analytics {
|
|
18
|
+
readonly _core: AnalyticsCore;
|
|
19
|
+
readonly _eventPublisher: Publisher<CapturedEventInfo>;
|
|
20
|
+
readonly _newSessionPublisher: Publisher<NewSessionInfo>;
|
|
21
|
+
readonly _identifyPublisher: Publisher<IdentifyInfo>;
|
|
22
|
+
readonly _changePropertiesPublisher: Publisher<ChangePropertiesInfo>;
|
|
23
|
+
readonly _groupPublisher: Publisher<GroupInfo>;
|
|
24
|
+
readonly _groupsResetPublisher: Publisher<void>;
|
|
25
|
+
readonly _resetPublisher: Publisher<void>;
|
|
26
|
+
readonly _personProfiles: PersonProfileMode;
|
|
27
|
+
_disposed: boolean;
|
|
28
|
+
_lastPersonProperties: string | undefined;
|
|
29
|
+
private _driver;
|
|
30
|
+
private _isTearingDown;
|
|
31
|
+
private readonly _registeredProperties;
|
|
32
|
+
private readonly _sessionProperties;
|
|
33
|
+
private readonly _rateLimiter;
|
|
34
|
+
private readonly _dynamicEventProperties;
|
|
35
|
+
private readonly _teardownCallbacks;
|
|
36
|
+
private readonly _buffer;
|
|
37
|
+
private readonly _deliveryHost;
|
|
38
|
+
constructor(_core: AnalyticsCore, options: AnalyticsOptions, createDelivery: AnalyticsDeliveryFactory);
|
|
39
|
+
get isRateLimited(): boolean;
|
|
40
|
+
onBeforeTeardown(callback: () => void): AnalyticsTeardownSubscription;
|
|
41
|
+
flush(): Promise<void>;
|
|
42
|
+
purge(): void;
|
|
43
|
+
dispose(): Promise<void> | undefined;
|
|
44
|
+
private readonly _beforeTeardown;
|
|
45
|
+
captureInitialPageview(): boolean;
|
|
46
|
+
capture(event: string, properties?: Record<string, unknown> | null, options?: CaptureOptions): boolean;
|
|
47
|
+
captureImmediate(event: string, properties?: Record<string, unknown> | null, options?: CaptureOptions): Promise<CaptureSummary>;
|
|
48
|
+
_capture(event: string, properties?: Record<string, unknown> | null, options?: CaptureOptions, skipRateLimit?: boolean, personProperties?: PersonPropertiesAdmission): boolean;
|
|
49
|
+
private _admitCapture;
|
|
50
|
+
private _acceptRateLimit;
|
|
51
|
+
private _reportRateLimit;
|
|
52
|
+
private _snapshotProperties;
|
|
53
|
+
private _prepareCapture;
|
|
54
|
+
private _warnOversized;
|
|
55
|
+
private _commitCapture;
|
|
56
|
+
_hasPersonProcessing(): boolean;
|
|
57
|
+
_requirePersonProcessing(): boolean;
|
|
58
|
+
/** Whether a local identity operation may run now. Prepares state without requiring it. */
|
|
59
|
+
_canMutate(): boolean;
|
|
60
|
+
/** Whether registered properties are available now. */
|
|
61
|
+
private _canUseProperties;
|
|
62
|
+
_setPersonProperties(properties: PersonProperties, options?: CaptureOptions, identification?: IdentifyInfo): void;
|
|
63
|
+
identify(distinctId: string, set?: Record<string, unknown>, setOnce?: Record<string, unknown>): void;
|
|
64
|
+
reset(): void;
|
|
65
|
+
private _registration;
|
|
66
|
+
register(properties: Record<string, unknown>): void;
|
|
67
|
+
registerOnce(properties: Record<string, unknown>, defaultValue?: unknown): void;
|
|
68
|
+
registerForSession(properties: Record<string, unknown>): void;
|
|
69
|
+
unregisterForSession(property: string): void;
|
|
70
|
+
unregister(property: string): void;
|
|
71
|
+
getProperty(property: string): unknown;
|
|
72
|
+
registerDynamicEventProperties(producer: () => Record<string, unknown>): Disposable;
|
|
73
|
+
}
|
|
74
|
+
/** The client's capture pipeline, for optional modules that extend it with less common operations. */
|
|
75
|
+
export declare const analyticsOf: (posthog: object) => Analytics | undefined;
|
|
76
|
+
export {};
|