@posthog/browser 0.2.0 → 0.4.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/README.md +30 -1
- package/dist/analytics-api.d.ts +6 -0
- package/dist/analytics-extension.d.ts +12 -1
- package/dist/analytics-extension.js +72 -12
- package/dist/analytics-extension.mjs +73 -13
- package/dist/analytics-internal.d.ts +2 -1
- package/dist/analytics-properties.d.ts +12 -0
- package/dist/analytics-properties.js +88 -0
- package/dist/analytics-properties.mjs +46 -0
- package/dist/analytics-session-properties.d.ts +12 -0
- package/dist/analytics-session-properties.js +84 -0
- package/dist/analytics-session-properties.mjs +46 -0
- package/dist/capture-properties.d.ts +7 -1
- package/dist/capture-properties.js +37 -4
- package/dist/capture-properties.mjs +30 -3
- package/dist/key-value-store.d.ts +21 -0
- package/dist/key-value-store.js +117 -0
- package/dist/key-value-store.mjs +72 -0
- package/dist/posthog.js +1 -0
- package/dist/posthog.mjs +1 -0
- package/dist/state.d.ts +5 -0
- package/dist/state.js +46 -56
- package/dist/state.mjs +46 -56
- package/dist/version.js +1 -1
- package/dist/version.mjs +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -57,6 +57,35 @@ analytics?.groupIdentify('organization', 'another-group', { plan: 'enterprise' }
|
|
|
57
57
|
analytics?.resetGroups()
|
|
58
58
|
```
|
|
59
59
|
|
|
60
|
+
## Persistent event properties
|
|
61
|
+
|
|
62
|
+
Use the installed analytics product to register context for future events:
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
const analytics = await posthog.resolveExtension('analytics')
|
|
66
|
+
analytics?.register({ plan: 'pro', region: 'eu' })
|
|
67
|
+
analytics?.registerOnce({ acquisitionSource: 'website' })
|
|
68
|
+
analytics?.capture('dashboard', { plan: 'enterprise' })
|
|
69
|
+
const region = analytics?.getProperty('region')
|
|
70
|
+
analytics?.unregister('region')
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
`register(properties)` merges JSON-serializable properties into analytics' Core-owned persistent storage. `registerOnce(properties, defaultValue = 'None')` adds missing keys or replaces existing values strictly equal to the sentinel; other existing values, including `false`, `0`, `''` and `null`, are retained. `getProperty(key)` returns an isolated stored value, or `undefined` for a missing key. Registration inputs and captured events are snapshotted, so later input or stored-property changes do not rewrite queued events.
|
|
74
|
+
|
|
75
|
+
Persistent properties are overridden by session properties, then dynamic event-property producers, then caller properties and capture options. SDK identity, group, session, library and person-processing metadata remains authoritative. Registration and removal only change local context, do not emit events, and remain available when capture consent is denied or person processing is disabled. Properties survive reload with persistent storage, until overwritten, unregistered or cleared by full `reset()`; `resetGroups()` retains them.
|
|
76
|
+
|
|
77
|
+
## Session event properties
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
const analytics = await posthog.resolveExtension('analytics')
|
|
81
|
+
analytics?.registerForSession({ checkoutFlow: 'express' })
|
|
82
|
+
analytics?.unregisterForSession('checkoutFlow')
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
With default browser storage, session properties use Core-owned per-tab storage, surviving reloads in the same SDK session. Independent tabs are isolated; copied tabs inherit a snapshot and then diverge. With a custom `storage` adapter, `storage: false` or unavailable native session storage, properties remain in per-instance memory. They override persistent defaults, but dynamic producers, caller properties, capture options and protected SDK metadata take precedence.
|
|
86
|
+
|
|
87
|
+
Registration and removal are local, do not create a session, and remain available under capture denial or `personProfiles: 'never'`. Properties registered before the first event or after the current session expires remain pending for the next admitted session. Registration does not extend session activity. On Core session rotation or adoption, old properties are excluded from the new session's events and cleared only after successful admission; rejected events retain the old scope. Full `reset()` clears session properties; `resetGroups()` retains them. Already-admitted events retain their finalized properties.
|
|
88
|
+
|
|
60
89
|
## Migrating to analytics-owned operations
|
|
61
90
|
|
|
62
91
|
Analytics operations now belong to `posthog.analytics`, not the Core client:
|
|
@@ -298,7 +327,7 @@ Definitions and event activation state use the host-provided surveys KV namespac
|
|
|
298
327
|
|
|
299
328
|
## Capture and delivery
|
|
300
329
|
|
|
301
|
-
Capture, immediate capture, identity and group controls, reset and
|
|
330
|
+
Capture, immediate capture, identity and group controls, reset and event-property registration live on the analytics extension. Core owns shared identity, sessions, groups, consent, persistence and lifecycle. The npm root acquires the complete analytics API through asynchronous resolution; delivery remains a separate lazy chunk until an admitted capture or an operation needs it. An enabled initial pageview or capture-producing product also acquires it. Feature flags initialize during startup. When their options are omitted, logs, surveys orchestration and autocapture load on existing saved-state or remote-config signals, or explicit asynchronous acquisition. Their collection and display retain their own consent and remote-configuration gates; survey rendering is a further deferred chunk.
|
|
302
331
|
|
|
303
332
|
`analytics.capture()` runs synchronously. Its `false` result is a rate-limit backpressure signal, not a general rejection signal. `true` is neither an admission nor a delivery acknowledgement: consent, lifecycle, filtering, invalid properties or queue limits can suppress an event while it still returns `true`. Use `captureImmediate()` and check its summary when persistence matters. Capture never waits for code or network delivery. With pending queued work, `flush()` joins an in-progress delivery load and can retry failed automatic loading; without available delivery it resolves without discarding unexpired queued events. Analytics retains at most 1,000 queued events and 8 MiB of active-plus-queued finalized analytics messages; queued work expires strictly after one hour on the next queue interaction. Queue overflow evicts the oldest queued prefix, while active bytes cannot be recalled and can cause a new event to be rejected.
|
|
304
333
|
|
package/dist/analytics-api.d.ts
CHANGED
|
@@ -21,6 +21,12 @@ export interface IAnalytics extends BrowserExtension {
|
|
|
21
21
|
setPersonProperties(set?: Record<string, unknown>, setOnce?: Record<string, unknown>): void;
|
|
22
22
|
unsetPersonProperties(names: string | readonly string[]): void;
|
|
23
23
|
setInternalOrTestUser(): void;
|
|
24
|
+
register(properties: Record<string, unknown>): void;
|
|
25
|
+
registerOnce(properties: Record<string, unknown>, defaultValue?: unknown): void;
|
|
26
|
+
registerForSession(properties: Record<string, unknown>): void;
|
|
27
|
+
unregisterForSession(property: string): void;
|
|
28
|
+
unregister(property: string): void;
|
|
29
|
+
getProperty(property: string): unknown;
|
|
24
30
|
registerDynamicEventProperties(producer: () => Record<string, unknown>): Disposable;
|
|
25
31
|
flush(): Promise<void>;
|
|
26
32
|
}
|
|
@@ -16,6 +16,8 @@ export declare class Analytics implements IAnalyticsExtension {
|
|
|
16
16
|
private _isTearingDown;
|
|
17
17
|
private _failures;
|
|
18
18
|
private _kv;
|
|
19
|
+
private _registeredProperties;
|
|
20
|
+
private _sessionProperties;
|
|
19
21
|
private _lastPersonProperties;
|
|
20
22
|
private readonly _personProfiles;
|
|
21
23
|
private readonly _rateLimiter;
|
|
@@ -52,7 +54,8 @@ export declare class Analytics implements IAnalyticsExtension {
|
|
|
52
54
|
private _dynamicProperties;
|
|
53
55
|
private _snapshotProperties;
|
|
54
56
|
private _prepareCapture;
|
|
55
|
-
private
|
|
57
|
+
private _enrichAndMeasure;
|
|
58
|
+
private _fitsUnavoidableProperties;
|
|
56
59
|
private _warnOversized;
|
|
57
60
|
private _enqueueCapture;
|
|
58
61
|
private _commitCapture;
|
|
@@ -72,5 +75,13 @@ export declare class Analytics implements IAnalyticsExtension {
|
|
|
72
75
|
readonly groupIdentify: (type: string, key: string | number, properties?: Record<string, unknown>, options?: CaptureOptions) => void;
|
|
73
76
|
readonly resetGroups: () => void;
|
|
74
77
|
readonly reset: () => void;
|
|
78
|
+
private _propertyStore;
|
|
79
|
+
private _prepareRegistration;
|
|
80
|
+
readonly register: (properties: Record<string, unknown>) => void;
|
|
81
|
+
readonly registerOnce: (properties: Record<string, unknown>, defaultValue?: unknown) => void;
|
|
82
|
+
readonly registerForSession: (properties: Record<string, unknown>) => void;
|
|
83
|
+
readonly unregisterForSession: (property: string) => void;
|
|
84
|
+
readonly unregister: (property: string) => void;
|
|
85
|
+
readonly getProperty: (property: string) => unknown;
|
|
75
86
|
readonly registerDynamicEventProperties: (producer: () => Record<string, unknown>) => Disposable;
|
|
76
87
|
}
|
|
@@ -37,6 +37,8 @@ const external_id_js_namespaceObject = require("./id.js");
|
|
|
37
37
|
const external_immutable_js_namespaceObject = require("./immutable.js");
|
|
38
38
|
const external_rate_limiter_js_namespaceObject = require("./rate-limiter.js");
|
|
39
39
|
const external_capture_properties_js_namespaceObject = require("./capture-properties.js");
|
|
40
|
+
const external_analytics_session_properties_js_namespaceObject = require("./analytics-session-properties.js");
|
|
41
|
+
const external_analytics_properties_js_namespaceObject = require("./analytics-properties.js");
|
|
40
42
|
const external_person_processing_js_namespaceObject = require("./person-processing.js");
|
|
41
43
|
const external_version_js_namespaceObject = require("./version.js");
|
|
42
44
|
const external_utf8_js_namespaceObject = require("./utf8.js");
|
|
@@ -76,10 +78,12 @@ class Analytics {
|
|
|
76
78
|
this._teardownCallbacks = new Set();
|
|
77
79
|
this.bindCaptureHost = (host)=>{
|
|
78
80
|
this._host = host;
|
|
81
|
+
this._sessionProperties = new external_analytics_session_properties_js_namespaceObject.SessionRegisteredProperties(host.sessionKv);
|
|
79
82
|
};
|
|
80
83
|
this.setup = (value)=>{
|
|
81
84
|
this._client = value.sharedClient;
|
|
82
85
|
this._kv = value.kv;
|
|
86
|
+
this._registeredProperties = new external_analytics_properties_js_namespaceObject.RegisteredProperties(value.kv);
|
|
83
87
|
};
|
|
84
88
|
this.initialize = (host)=>{
|
|
85
89
|
this._deliveryHost = host;
|
|
@@ -107,6 +111,8 @@ class Analytics {
|
|
|
107
111
|
this.purge = ()=>this._buffer.purge();
|
|
108
112
|
this.dispose = ()=>{
|
|
109
113
|
this._disposed = true;
|
|
114
|
+
this._registeredProperties = void 0;
|
|
115
|
+
this._sessionProperties = void 0;
|
|
110
116
|
this._dynamicEventProperties.splice(0);
|
|
111
117
|
for (const release of this._disposalWaiters)release();
|
|
112
118
|
this._disposalWaiters.clear();
|
|
@@ -286,8 +292,28 @@ class Analytics {
|
|
|
286
292
|
this._host.state.prepare();
|
|
287
293
|
this._host.state.reset();
|
|
288
294
|
this._lastPersonProperties = void 0;
|
|
295
|
+
this._sessionProperties?.clear();
|
|
289
296
|
this._host.publishReset(void 0);
|
|
290
297
|
};
|
|
298
|
+
this.register = (properties)=>{
|
|
299
|
+
const registration = this._prepareRegistration(properties);
|
|
300
|
+
registration?.store.register(registration.properties);
|
|
301
|
+
};
|
|
302
|
+
this.registerOnce = (properties, defaultValue = 'None')=>{
|
|
303
|
+
const registration = this._prepareRegistration(properties);
|
|
304
|
+
registration?.store.registerOnce(registration.properties, defaultValue);
|
|
305
|
+
};
|
|
306
|
+
this.registerForSession = (properties)=>{
|
|
307
|
+
const registration = this._prepareRegistration(properties);
|
|
308
|
+
if (registration) this._sessionProperties?.register(registration.properties, this._host?.state.activeSessionId);
|
|
309
|
+
};
|
|
310
|
+
this.unregisterForSession = (property)=>{
|
|
311
|
+
if (this._propertyStore()) this._sessionProperties?.unregister(property, this._host?.state.activeSessionId);
|
|
312
|
+
};
|
|
313
|
+
this.unregister = (property)=>{
|
|
314
|
+
this._propertyStore()?.unregister(property);
|
|
315
|
+
};
|
|
316
|
+
this.getProperty = (property)=>this._propertyStore()?.get(property);
|
|
291
317
|
this.registerDynamicEventProperties = (producer)=>{
|
|
292
318
|
if (this._disposed || !this._host || this._host.isDisposed()) return {
|
|
293
319
|
dispose () {}
|
|
@@ -436,7 +462,7 @@ class Analytics {
|
|
|
436
462
|
_snapshotProperties(properties, options) {
|
|
437
463
|
const dynamic = this._dynamicProperties();
|
|
438
464
|
try {
|
|
439
|
-
return (0, external_capture_properties_js_namespaceObject.snapshotCaptureProperties)(dynamic, properties, options);
|
|
465
|
+
return (0, external_capture_properties_js_namespaceObject.snapshotCaptureProperties)(this._registeredProperties?.values(), dynamic, properties, options);
|
|
440
466
|
} catch (error) {
|
|
441
467
|
this._host?.logger.error('Event properties are not JSON-serializable', error);
|
|
442
468
|
return;
|
|
@@ -461,13 +487,6 @@ class Analytics {
|
|
|
461
487
|
$lib_version: external_version_js_namespaceObject.version,
|
|
462
488
|
$process_person_profile: processPersonProfile
|
|
463
489
|
};
|
|
464
|
-
const observedProperties = {
|
|
465
|
-
...snapshot.observed,
|
|
466
|
-
...metadata,
|
|
467
|
-
$groups: {
|
|
468
|
-
...groups
|
|
469
|
-
}
|
|
470
|
-
};
|
|
471
490
|
const message = {
|
|
472
491
|
uuid: snapshot.uuid ?? (0, external_id_js_namespaceObject.createId)(),
|
|
473
492
|
event,
|
|
@@ -478,23 +497,47 @@ class Analytics {
|
|
|
478
497
|
},
|
|
479
498
|
timestamp: eventTimestamp(snapshot.timestamp)
|
|
480
499
|
};
|
|
481
|
-
const bytes = this.
|
|
500
|
+
const bytes = this._enrichAndMeasure(message, snapshot, metadata, session.sessionId);
|
|
482
501
|
if (void 0 === bytes) return;
|
|
483
502
|
return {
|
|
484
503
|
message,
|
|
485
504
|
bytes,
|
|
486
505
|
preparedSession,
|
|
487
|
-
observedProperties
|
|
506
|
+
observedProperties: {
|
|
507
|
+
...snapshot.observed,
|
|
508
|
+
...metadata,
|
|
509
|
+
$groups: {
|
|
510
|
+
...groups
|
|
511
|
+
}
|
|
512
|
+
}
|
|
488
513
|
};
|
|
489
514
|
}
|
|
490
|
-
|
|
515
|
+
_enrichAndMeasure(message, snapshot, metadata, sessionId) {
|
|
491
516
|
try {
|
|
492
|
-
|
|
517
|
+
let bytes = (0, external_utf8_js_namespaceObject.utf8Bytes)(JSON.stringify(message));
|
|
518
|
+
if (!this._fitsUnavoidableProperties(message, snapshot, metadata, bytes)) return;
|
|
519
|
+
const changed = (0, external_capture_properties_js_namespaceObject.applySessionProperties)(snapshot, this._sessionProperties?.values(sessionId) ?? {});
|
|
520
|
+
message.properties = {
|
|
521
|
+
...snapshot.message,
|
|
522
|
+
...metadata
|
|
523
|
+
};
|
|
524
|
+
if (changed) bytes = (0, external_utf8_js_namespaceObject.utf8Bytes)(JSON.stringify(message));
|
|
525
|
+
return bytes;
|
|
493
526
|
} catch (error) {
|
|
494
527
|
this._host?.logger.error('The finalized event could not be measured', error);
|
|
495
528
|
return;
|
|
496
529
|
}
|
|
497
530
|
}
|
|
531
|
+
_fitsUnavoidableProperties(message, snapshot, metadata, bytes) {
|
|
532
|
+
if (bytes <= external_analytics_internal_js_namespaceObject.MAX_ANALYTICS_BYTES) return true;
|
|
533
|
+
const minimum = (0, external_utf8_js_namespaceObject.utf8Bytes)(JSON.stringify({
|
|
534
|
+
...message,
|
|
535
|
+
properties: (0, external_capture_properties_js_namespaceObject.unavoidableProperties)(snapshot, metadata)
|
|
536
|
+
}));
|
|
537
|
+
if (minimum <= external_analytics_internal_js_namespaceObject.MAX_ANALYTICS_BYTES) return true;
|
|
538
|
+
this._warnOversized(message.event, bytes);
|
|
539
|
+
return false;
|
|
540
|
+
}
|
|
498
541
|
_warnOversized(event, bytes) {
|
|
499
542
|
this._host?.logger.warn(`Event "${event}" (${bytes} bytes) exceeds the local analytics limit and was dropped`);
|
|
500
543
|
}
|
|
@@ -514,6 +557,7 @@ class Analytics {
|
|
|
514
557
|
}
|
|
515
558
|
if (personProperties) this._lastPersonProperties = personProperties.signature;
|
|
516
559
|
else if (Object.hasOwn(candidate.message.properties, '$set') || Object.hasOwn(candidate.message.properties, '$set_once') || Object.hasOwn(candidate.message.properties, '$unset')) this._lastPersonProperties = void 0;
|
|
560
|
+
this._sessionProperties?.admitted(candidate.preparedSession.context.sessionId);
|
|
517
561
|
this._publishCapture(candidate, host);
|
|
518
562
|
if (!immediate) this._ensureDelivery('capture');
|
|
519
563
|
return candidate.message;
|
|
@@ -579,6 +623,22 @@ class Analytics {
|
|
|
579
623
|
}, false, personProperties);
|
|
580
624
|
return hasProperties ? this._capture('$set', null, options, false, personProperties) : false;
|
|
581
625
|
}
|
|
626
|
+
_propertyStore() {
|
|
627
|
+
const host = this._host;
|
|
628
|
+
if (this._disposed || !host?.canMutate() || !host.state.prepare()) return;
|
|
629
|
+
return this._registeredProperties;
|
|
630
|
+
}
|
|
631
|
+
_prepareRegistration(properties) {
|
|
632
|
+
const store = this._propertyStore();
|
|
633
|
+
if (!store) return;
|
|
634
|
+
const snapshot = (0, external_analytics_properties_js_namespaceObject.snapshotRegisteredProperties)(properties);
|
|
635
|
+
if (this._propertyStore() !== store) return;
|
|
636
|
+
if (!snapshot) return void this._host?.logger.warn('Super properties must serialize to an object');
|
|
637
|
+
return {
|
|
638
|
+
store,
|
|
639
|
+
properties: snapshot
|
|
640
|
+
};
|
|
641
|
+
}
|
|
582
642
|
}
|
|
583
643
|
exports.Analytics = __webpack_exports__.Analytics;
|
|
584
644
|
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
@@ -4,7 +4,9 @@ import { EMPTY_CAPTURE_SUMMARY, captureFailure } from "./capture-summary.mjs";
|
|
|
4
4
|
import { createId } from "./id.mjs";
|
|
5
5
|
import { deepFreeze } from "./immutable.mjs";
|
|
6
6
|
import { ClientRateLimiter } from "./rate-limiter.mjs";
|
|
7
|
-
import { snapshotCaptureProperties } from "./capture-properties.mjs";
|
|
7
|
+
import { applySessionProperties, snapshotCaptureProperties, unavoidableProperties } from "./capture-properties.mjs";
|
|
8
|
+
import { SessionRegisteredProperties } from "./analytics-session-properties.mjs";
|
|
9
|
+
import { RegisteredProperties, snapshotRegisteredProperties } from "./analytics-properties.mjs";
|
|
8
10
|
import { personPropertyNames, shouldProcessPerson, snapshotPersonProfileMode, snapshotPersonProperties } from "./person-processing.mjs";
|
|
9
11
|
import { version } from "./version.mjs";
|
|
10
12
|
import { utf8Bytes } from "./utf8.mjs";
|
|
@@ -44,10 +46,12 @@ class analytics_extension_Analytics {
|
|
|
44
46
|
this._teardownCallbacks = new Set();
|
|
45
47
|
this.bindCaptureHost = (host)=>{
|
|
46
48
|
this._host = host;
|
|
49
|
+
this._sessionProperties = new SessionRegisteredProperties(host.sessionKv);
|
|
47
50
|
};
|
|
48
51
|
this.setup = (value)=>{
|
|
49
52
|
this._client = value.sharedClient;
|
|
50
53
|
this._kv = value.kv;
|
|
54
|
+
this._registeredProperties = new RegisteredProperties(value.kv);
|
|
51
55
|
};
|
|
52
56
|
this.initialize = (host)=>{
|
|
53
57
|
this._deliveryHost = host;
|
|
@@ -75,6 +79,8 @@ class analytics_extension_Analytics {
|
|
|
75
79
|
this.purge = ()=>this._buffer.purge();
|
|
76
80
|
this.dispose = ()=>{
|
|
77
81
|
this._disposed = true;
|
|
82
|
+
this._registeredProperties = void 0;
|
|
83
|
+
this._sessionProperties = void 0;
|
|
78
84
|
this._dynamicEventProperties.splice(0);
|
|
79
85
|
for (const release of this._disposalWaiters)release();
|
|
80
86
|
this._disposalWaiters.clear();
|
|
@@ -254,8 +260,28 @@ class analytics_extension_Analytics {
|
|
|
254
260
|
this._host.state.prepare();
|
|
255
261
|
this._host.state.reset();
|
|
256
262
|
this._lastPersonProperties = void 0;
|
|
263
|
+
this._sessionProperties?.clear();
|
|
257
264
|
this._host.publishReset(void 0);
|
|
258
265
|
};
|
|
266
|
+
this.register = (properties)=>{
|
|
267
|
+
const registration = this._prepareRegistration(properties);
|
|
268
|
+
registration?.store.register(registration.properties);
|
|
269
|
+
};
|
|
270
|
+
this.registerOnce = (properties, defaultValue = 'None')=>{
|
|
271
|
+
const registration = this._prepareRegistration(properties);
|
|
272
|
+
registration?.store.registerOnce(registration.properties, defaultValue);
|
|
273
|
+
};
|
|
274
|
+
this.registerForSession = (properties)=>{
|
|
275
|
+
const registration = this._prepareRegistration(properties);
|
|
276
|
+
if (registration) this._sessionProperties?.register(registration.properties, this._host?.state.activeSessionId);
|
|
277
|
+
};
|
|
278
|
+
this.unregisterForSession = (property)=>{
|
|
279
|
+
if (this._propertyStore()) this._sessionProperties?.unregister(property, this._host?.state.activeSessionId);
|
|
280
|
+
};
|
|
281
|
+
this.unregister = (property)=>{
|
|
282
|
+
this._propertyStore()?.unregister(property);
|
|
283
|
+
};
|
|
284
|
+
this.getProperty = (property)=>this._propertyStore()?.get(property);
|
|
259
285
|
this.registerDynamicEventProperties = (producer)=>{
|
|
260
286
|
if (this._disposed || !this._host || this._host.isDisposed()) return {
|
|
261
287
|
dispose () {}
|
|
@@ -404,7 +430,7 @@ class analytics_extension_Analytics {
|
|
|
404
430
|
_snapshotProperties(properties, options) {
|
|
405
431
|
const dynamic = this._dynamicProperties();
|
|
406
432
|
try {
|
|
407
|
-
return snapshotCaptureProperties(dynamic, properties, options);
|
|
433
|
+
return snapshotCaptureProperties(this._registeredProperties?.values(), dynamic, properties, options);
|
|
408
434
|
} catch (error) {
|
|
409
435
|
this._host?.logger.error('Event properties are not JSON-serializable', error);
|
|
410
436
|
return;
|
|
@@ -429,13 +455,6 @@ class analytics_extension_Analytics {
|
|
|
429
455
|
$lib_version: version,
|
|
430
456
|
$process_person_profile: processPersonProfile
|
|
431
457
|
};
|
|
432
|
-
const observedProperties = {
|
|
433
|
-
...snapshot.observed,
|
|
434
|
-
...metadata,
|
|
435
|
-
$groups: {
|
|
436
|
-
...groups
|
|
437
|
-
}
|
|
438
|
-
};
|
|
439
458
|
const message = {
|
|
440
459
|
uuid: snapshot.uuid ?? createId(),
|
|
441
460
|
event,
|
|
@@ -446,23 +465,47 @@ class analytics_extension_Analytics {
|
|
|
446
465
|
},
|
|
447
466
|
timestamp: eventTimestamp(snapshot.timestamp)
|
|
448
467
|
};
|
|
449
|
-
const bytes = this.
|
|
468
|
+
const bytes = this._enrichAndMeasure(message, snapshot, metadata, session.sessionId);
|
|
450
469
|
if (void 0 === bytes) return;
|
|
451
470
|
return {
|
|
452
471
|
message,
|
|
453
472
|
bytes,
|
|
454
473
|
preparedSession,
|
|
455
|
-
observedProperties
|
|
474
|
+
observedProperties: {
|
|
475
|
+
...snapshot.observed,
|
|
476
|
+
...metadata,
|
|
477
|
+
$groups: {
|
|
478
|
+
...groups
|
|
479
|
+
}
|
|
480
|
+
}
|
|
456
481
|
};
|
|
457
482
|
}
|
|
458
|
-
|
|
483
|
+
_enrichAndMeasure(message, snapshot, metadata, sessionId) {
|
|
459
484
|
try {
|
|
460
|
-
|
|
485
|
+
let bytes = utf8Bytes(JSON.stringify(message));
|
|
486
|
+
if (!this._fitsUnavoidableProperties(message, snapshot, metadata, bytes)) return;
|
|
487
|
+
const changed = applySessionProperties(snapshot, this._sessionProperties?.values(sessionId) ?? {});
|
|
488
|
+
message.properties = {
|
|
489
|
+
...snapshot.message,
|
|
490
|
+
...metadata
|
|
491
|
+
};
|
|
492
|
+
if (changed) bytes = utf8Bytes(JSON.stringify(message));
|
|
493
|
+
return bytes;
|
|
461
494
|
} catch (error) {
|
|
462
495
|
this._host?.logger.error('The finalized event could not be measured', error);
|
|
463
496
|
return;
|
|
464
497
|
}
|
|
465
498
|
}
|
|
499
|
+
_fitsUnavoidableProperties(message, snapshot, metadata, bytes) {
|
|
500
|
+
if (bytes <= MAX_ANALYTICS_BYTES) return true;
|
|
501
|
+
const minimum = utf8Bytes(JSON.stringify({
|
|
502
|
+
...message,
|
|
503
|
+
properties: unavoidableProperties(snapshot, metadata)
|
|
504
|
+
}));
|
|
505
|
+
if (minimum <= MAX_ANALYTICS_BYTES) return true;
|
|
506
|
+
this._warnOversized(message.event, bytes);
|
|
507
|
+
return false;
|
|
508
|
+
}
|
|
466
509
|
_warnOversized(event, bytes) {
|
|
467
510
|
this._host?.logger.warn(`Event "${event}" (${bytes} bytes) exceeds the local analytics limit and was dropped`);
|
|
468
511
|
}
|
|
@@ -482,6 +525,7 @@ class analytics_extension_Analytics {
|
|
|
482
525
|
}
|
|
483
526
|
if (personProperties) this._lastPersonProperties = personProperties.signature;
|
|
484
527
|
else if (Object.hasOwn(candidate.message.properties, '$set') || Object.hasOwn(candidate.message.properties, '$set_once') || Object.hasOwn(candidate.message.properties, '$unset')) this._lastPersonProperties = void 0;
|
|
528
|
+
this._sessionProperties?.admitted(candidate.preparedSession.context.sessionId);
|
|
485
529
|
this._publishCapture(candidate, host);
|
|
486
530
|
if (!immediate) this._ensureDelivery('capture');
|
|
487
531
|
return candidate.message;
|
|
@@ -547,5 +591,21 @@ class analytics_extension_Analytics {
|
|
|
547
591
|
}, false, personProperties);
|
|
548
592
|
return hasProperties ? this._capture('$set', null, options, false, personProperties) : false;
|
|
549
593
|
}
|
|
594
|
+
_propertyStore() {
|
|
595
|
+
const host = this._host;
|
|
596
|
+
if (this._disposed || !host?.canMutate() || !host.state.prepare()) return;
|
|
597
|
+
return this._registeredProperties;
|
|
598
|
+
}
|
|
599
|
+
_prepareRegistration(properties) {
|
|
600
|
+
const store = this._propertyStore();
|
|
601
|
+
if (!store) return;
|
|
602
|
+
const snapshot = snapshotRegisteredProperties(properties);
|
|
603
|
+
if (this._propertyStore() !== store) return;
|
|
604
|
+
if (!snapshot) return void this._host?.logger.warn('Super properties must serialize to an object');
|
|
605
|
+
return {
|
|
606
|
+
store,
|
|
607
|
+
properties: snapshot
|
|
608
|
+
};
|
|
609
|
+
}
|
|
550
610
|
}
|
|
551
611
|
export { analytics_extension_Analytics as Analytics };
|
|
@@ -51,7 +51,8 @@ export interface AnalyticsTeardownSubscription {
|
|
|
51
51
|
export interface AnalyticsCaptureHost {
|
|
52
52
|
readonly projectToken: string;
|
|
53
53
|
readonly logger: Client['logger'];
|
|
54
|
-
readonly
|
|
54
|
+
readonly sessionKv: Client['kv'];
|
|
55
|
+
readonly state: Pick<BrowserState, 'prepare' | 'distinctId' | 'deviceId' | 'groups' | 'activeSessionId' | 'isIdentified' | 'prepareSessionForEvent' | 'sessionAdmitted' | 'identify' | 'group' | 'reset' | 'resetGroups'>;
|
|
55
56
|
readonly onEvent: Listener<CapturedEventInfo>;
|
|
56
57
|
canCapture(): boolean;
|
|
57
58
|
canMutate(): boolean;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { KeyValueStore } from '@posthog/browser-common';
|
|
2
|
+
export declare const snapshotRegisteredProperties: (value: Record<string, unknown>) => Record<string, unknown> | undefined;
|
|
3
|
+
/** Persistent event context in analytics' existing host-owned namespace. */
|
|
4
|
+
export declare class RegisteredProperties {
|
|
5
|
+
private readonly _kv;
|
|
6
|
+
constructor(_kv: KeyValueStore);
|
|
7
|
+
values(): Record<string, unknown>;
|
|
8
|
+
get(property: string): unknown;
|
|
9
|
+
register(properties: Record<string, unknown>): void;
|
|
10
|
+
registerOnce(properties: Record<string, unknown>, defaultValue: unknown): void;
|
|
11
|
+
unregister(property: string): void;
|
|
12
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
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
|
+
const REGISTERED_PROPERTIES = 'superProperties';
|
|
31
|
+
const isPropertyMap = (value)=>'object' == typeof value && null !== value && !Array.isArray(value);
|
|
32
|
+
const snapshotRegisteredProperties = (value)=>{
|
|
33
|
+
try {
|
|
34
|
+
if (!isPropertyMap(value)) return;
|
|
35
|
+
const snapshot = JSON.parse(JSON.stringify(value));
|
|
36
|
+
return isPropertyMap(snapshot) ? snapshot : void 0;
|
|
37
|
+
} catch {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
class RegisteredProperties {
|
|
42
|
+
constructor(_kv){
|
|
43
|
+
this._kv = _kv;
|
|
44
|
+
}
|
|
45
|
+
values() {
|
|
46
|
+
const value = this._kv.get(REGISTERED_PROPERTIES);
|
|
47
|
+
return isPropertyMap(value) ? value : {};
|
|
48
|
+
}
|
|
49
|
+
get(property) {
|
|
50
|
+
const values = this.values();
|
|
51
|
+
return Object.prototype.hasOwnProperty.call(values, property) ? values[property] : void 0;
|
|
52
|
+
}
|
|
53
|
+
register(properties) {
|
|
54
|
+
this._kv.set(REGISTERED_PROPERTIES, {
|
|
55
|
+
...this.values(),
|
|
56
|
+
...properties
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
registerOnce(properties, defaultValue) {
|
|
60
|
+
const values = this.values();
|
|
61
|
+
const additions = Object.fromEntries(Object.entries(properties).filter(([key])=>!Object.prototype.hasOwnProperty.call(values, key) || values[key] === defaultValue));
|
|
62
|
+
if (0 === Object.keys(additions).length) return;
|
|
63
|
+
this._kv.set(REGISTERED_PROPERTIES, {
|
|
64
|
+
...values,
|
|
65
|
+
...additions
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
unregister(property) {
|
|
69
|
+
const values = this.values();
|
|
70
|
+
if (!Object.prototype.hasOwnProperty.call(values, property)) return;
|
|
71
|
+
delete values[property];
|
|
72
|
+
this._kv.set(REGISTERED_PROPERTIES, values);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
76
|
+
RegisteredProperties: ()=>RegisteredProperties
|
|
77
|
+
}, {
|
|
78
|
+
snapshotRegisteredProperties: snapshotRegisteredProperties
|
|
79
|
+
});
|
|
80
|
+
exports.RegisteredProperties = __webpack_exports__.RegisteredProperties;
|
|
81
|
+
exports.snapshotRegisteredProperties = __webpack_exports__.snapshotRegisteredProperties;
|
|
82
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
83
|
+
"RegisteredProperties",
|
|
84
|
+
"snapshotRegisteredProperties"
|
|
85
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
86
|
+
Object.defineProperty(exports, '__esModule', {
|
|
87
|
+
value: true
|
|
88
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
const REGISTERED_PROPERTIES = 'superProperties';
|
|
2
|
+
const isPropertyMap = (value)=>'object' == typeof value && null !== value && !Array.isArray(value);
|
|
3
|
+
const snapshotRegisteredProperties = (value)=>{
|
|
4
|
+
try {
|
|
5
|
+
if (!isPropertyMap(value)) return;
|
|
6
|
+
const snapshot = JSON.parse(JSON.stringify(value));
|
|
7
|
+
return isPropertyMap(snapshot) ? snapshot : void 0;
|
|
8
|
+
} catch {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
class RegisteredProperties {
|
|
13
|
+
constructor(_kv){
|
|
14
|
+
this._kv = _kv;
|
|
15
|
+
}
|
|
16
|
+
values() {
|
|
17
|
+
const value = this._kv.get(REGISTERED_PROPERTIES);
|
|
18
|
+
return isPropertyMap(value) ? value : {};
|
|
19
|
+
}
|
|
20
|
+
get(property) {
|
|
21
|
+
const values = this.values();
|
|
22
|
+
return Object.prototype.hasOwnProperty.call(values, property) ? values[property] : void 0;
|
|
23
|
+
}
|
|
24
|
+
register(properties) {
|
|
25
|
+
this._kv.set(REGISTERED_PROPERTIES, {
|
|
26
|
+
...this.values(),
|
|
27
|
+
...properties
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
registerOnce(properties, defaultValue) {
|
|
31
|
+
const values = this.values();
|
|
32
|
+
const additions = Object.fromEntries(Object.entries(properties).filter(([key])=>!Object.prototype.hasOwnProperty.call(values, key) || values[key] === defaultValue));
|
|
33
|
+
if (0 === Object.keys(additions).length) return;
|
|
34
|
+
this._kv.set(REGISTERED_PROPERTIES, {
|
|
35
|
+
...values,
|
|
36
|
+
...additions
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
unregister(property) {
|
|
40
|
+
const values = this.values();
|
|
41
|
+
if (!Object.prototype.hasOwnProperty.call(values, property)) return;
|
|
42
|
+
delete values[property];
|
|
43
|
+
this._kv.set(REGISTERED_PROPERTIES, values);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export { RegisteredProperties, snapshotRegisteredProperties };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { KeyValueStore } from '@posthog/browser-common';
|
|
2
|
+
/** Property scope is labelled by core's SDK session, never by an independent clock. */
|
|
3
|
+
export declare class SessionRegisteredProperties {
|
|
4
|
+
private readonly _kv;
|
|
5
|
+
private readonly _properties;
|
|
6
|
+
constructor(_kv: KeyValueStore);
|
|
7
|
+
values(sessionId: string | undefined): Record<string, unknown>;
|
|
8
|
+
register(properties: Record<string, unknown>, sessionId: string | undefined): void;
|
|
9
|
+
unregister(property: string, sessionId: string | undefined): void;
|
|
10
|
+
admitted(sessionId: string): void;
|
|
11
|
+
clear(): void;
|
|
12
|
+
}
|