@posthog/browser 0.4.1 → 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 +7 -0
- package/dist/core/state/state.mjs +5 -1
- 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 +3 -8
- 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
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
import { Publisher } from "@posthog/browser-common/pubsub";
|
|
2
|
+
import { MAX_ANALYTICS_BATCH_EVENTS, MAX_ANALYTICS_BYTES } from "./analytics-internal.mjs";
|
|
3
|
+
import { EventBuffer } from "./admission/event-buffer.mjs";
|
|
4
|
+
import { EMPTY_CAPTURE_SUMMARY, captureFailure } from "./admission/capture-summary.mjs";
|
|
5
|
+
import { createId } from "../../primitives/id.mjs";
|
|
6
|
+
import { deepFreeze } from "../../primitives/immutable.mjs";
|
|
7
|
+
import { ClientRateLimiter } from "./admission/rate-limiter.mjs";
|
|
8
|
+
import { applySessionProperties, snapshotCaptureProperties, unavoidableProperties } from "./admission/capture-properties.mjs";
|
|
9
|
+
import { SessionRegisteredProperties } from "./properties/analytics-session-properties.mjs";
|
|
10
|
+
import { RegisteredProperties, snapshotRegisteredProperties } from "./properties/analytics-properties.mjs";
|
|
11
|
+
import { shouldProcessPerson, snapshotPersonProfileMode, snapshotPersonProperties } from "./properties/person-processing.mjs";
|
|
12
|
+
import { version } from "../../primitives/version.mjs";
|
|
13
|
+
import { utf8Bytes } from "../../primitives/utf8.mjs";
|
|
14
|
+
const numberOption = (read, fallback, minimum, maximum)=>{
|
|
15
|
+
try {
|
|
16
|
+
const value = read();
|
|
17
|
+
return Math.max(minimum, Math.min(maximum, Math.floor('number' == typeof value && Number.isFinite(value) ? value : fallback)));
|
|
18
|
+
} catch {
|
|
19
|
+
return fallback;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
const CLIENT_RATE_LIMIT_WARNING = '$$client_ingestion_warning';
|
|
23
|
+
const isNonEmptyString = (value)=>'string' == typeof value && value.trim().length > 0;
|
|
24
|
+
const isValidDistinctId = (value)=>isNonEmptyString(value) && ![
|
|
25
|
+
'$posthog_cookieless',
|
|
26
|
+
'distinct_id',
|
|
27
|
+
'distinctid',
|
|
28
|
+
'undefined',
|
|
29
|
+
'null'
|
|
30
|
+
].includes(value.toLowerCase());
|
|
31
|
+
const eventTimestamp = (timestamp)=>{
|
|
32
|
+
try {
|
|
33
|
+
return (timestamp ?? new Date()).toISOString();
|
|
34
|
+
} catch {
|
|
35
|
+
return new Date().toISOString();
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
class Analytics {
|
|
39
|
+
constructor(_core, options, createDelivery){
|
|
40
|
+
this._core = _core;
|
|
41
|
+
this._disposed = false;
|
|
42
|
+
this._isTearingDown = false;
|
|
43
|
+
this._dynamicEventProperties = [];
|
|
44
|
+
this._teardownCallbacks = new Set();
|
|
45
|
+
this._beforeTeardown = ()=>{
|
|
46
|
+
this._isTearingDown = true;
|
|
47
|
+
try {
|
|
48
|
+
for (const callback of this._teardownCallbacks)try {
|
|
49
|
+
callback();
|
|
50
|
+
} catch (error) {
|
|
51
|
+
try {
|
|
52
|
+
this._deliveryHost.reportFailure(error);
|
|
53
|
+
} catch {}
|
|
54
|
+
}
|
|
55
|
+
} finally{
|
|
56
|
+
this._isTearingDown = false;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
const report = (label)=>(error)=>_core.logger.error(`${label} listener failed`, error);
|
|
60
|
+
this._eventPublisher = new Publisher(report('An event'));
|
|
61
|
+
this._newSessionPublisher = new Publisher(report('A session'));
|
|
62
|
+
this._identifyPublisher = new Publisher(report('An identify'));
|
|
63
|
+
this._changePropertiesPublisher = new Publisher(report('A person-property'));
|
|
64
|
+
this._groupPublisher = new Publisher(report('A group'));
|
|
65
|
+
this._groupsResetPublisher = new Publisher(report('A group reset'));
|
|
66
|
+
this._resetPublisher = new Publisher(report('A reset'));
|
|
67
|
+
this._personProfiles = snapshotPersonProfileMode(()=>options.personProfiles);
|
|
68
|
+
this._rateLimiter = new ClientRateLimiter();
|
|
69
|
+
this._registeredProperties = new RegisteredProperties(_core.kv);
|
|
70
|
+
this._sessionProperties = new SessionRegisteredProperties(_core._sessionKv);
|
|
71
|
+
this._deliveryHost = {
|
|
72
|
+
runtime: _core._requestRuntime,
|
|
73
|
+
canRetry: ()=>_core._canDeliver(),
|
|
74
|
+
reportFailure: (error)=>_core.logger.error('Event delivery failed', error),
|
|
75
|
+
reportWarning: (message)=>_core.logger.warn(message),
|
|
76
|
+
onAvailable: ()=>_core._startInitialPageview()
|
|
77
|
+
};
|
|
78
|
+
this._buffer = new EventBuffer(1000, (total, count = 1, reason = 'overflow')=>this._deliveryHost.reportWarning(`Analytics queue dropped ${count} ${reason} event${1 === count ? '' : 's'} (${total} total)`), MAX_ANALYTICS_BYTES, 3600000);
|
|
79
|
+
try {
|
|
80
|
+
this._driver = createDelivery(this._buffer, _core, this._deliveryHost, {
|
|
81
|
+
flushAt: numberOption(()=>options.flushAt, 20, 1, MAX_ANALYTICS_BATCH_EVENTS),
|
|
82
|
+
flushInterval: numberOption(()=>options.flushInterval, 3000, 0, Number.MAX_SAFE_INTEGER)
|
|
83
|
+
}, this._beforeTeardown);
|
|
84
|
+
} catch (error) {
|
|
85
|
+
_core.logger.error('Event delivery is unavailable', error);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
get isRateLimited() {
|
|
89
|
+
return this._rateLimiter.wouldLimit;
|
|
90
|
+
}
|
|
91
|
+
onBeforeTeardown(callback) {
|
|
92
|
+
if (!this._disposed) this._teardownCallbacks.add(callback);
|
|
93
|
+
const instance = this;
|
|
94
|
+
return {
|
|
95
|
+
get deliveryAvailable () {
|
|
96
|
+
return !instance._disposed && !!instance._driver;
|
|
97
|
+
},
|
|
98
|
+
dispose: ()=>{
|
|
99
|
+
this._teardownCallbacks.delete(callback);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
async flush() {
|
|
104
|
+
if (!this._disposed) await this._driver?.flush();
|
|
105
|
+
}
|
|
106
|
+
purge() {
|
|
107
|
+
this._buffer.purge();
|
|
108
|
+
}
|
|
109
|
+
dispose() {
|
|
110
|
+
this._disposed = true;
|
|
111
|
+
this._dynamicEventProperties.splice(0);
|
|
112
|
+
this._teardownCallbacks.clear();
|
|
113
|
+
for (const publisher of [
|
|
114
|
+
this._eventPublisher,
|
|
115
|
+
this._newSessionPublisher,
|
|
116
|
+
this._identifyPublisher,
|
|
117
|
+
this._changePropertiesPublisher,
|
|
118
|
+
this._groupPublisher,
|
|
119
|
+
this._groupsResetPublisher,
|
|
120
|
+
this._resetPublisher
|
|
121
|
+
])publisher.dispose();
|
|
122
|
+
if (this._driver) return this._driver.dispose();
|
|
123
|
+
this._buffer.dispose();
|
|
124
|
+
}
|
|
125
|
+
captureInitialPageview() {
|
|
126
|
+
return this._capture('$pageview');
|
|
127
|
+
}
|
|
128
|
+
capture(event, properties = null, options = {}) {
|
|
129
|
+
try {
|
|
130
|
+
if ('unload' === options.delivery && !this._isTearingDown) {
|
|
131
|
+
const authority = this._core._immediateAuthority;
|
|
132
|
+
const message = this._admitCapture(event, properties, options, false, true);
|
|
133
|
+
if (message && !this._disposed && this._core._immediateAuthority === authority) this._driver?.deliverUnload([
|
|
134
|
+
message
|
|
135
|
+
]);
|
|
136
|
+
return !!message || !this._rateLimiter.limited;
|
|
137
|
+
}
|
|
138
|
+
} catch (error) {
|
|
139
|
+
this._core.logger.error('Unload capture failed', error);
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
return this._capture(event, properties, options) || !this._rateLimiter.limited;
|
|
143
|
+
}
|
|
144
|
+
async captureImmediate(event, properties = null, options = {}) {
|
|
145
|
+
try {
|
|
146
|
+
const deliver = this._driver?.deliverImmediate;
|
|
147
|
+
if (!deliver || this._disposed) return captureFailure(new Error('Immediate analytics delivery is unavailable'));
|
|
148
|
+
const authority = this._core._immediateAuthority;
|
|
149
|
+
const message = this._admitCapture(event, properties, options, false, true);
|
|
150
|
+
if (!message) return EMPTY_CAPTURE_SUMMARY;
|
|
151
|
+
if (this._disposed || !this._core._canMutate()) return captureFailure(new Error('Immediate analytics delivery is unavailable'));
|
|
152
|
+
return await deliver.call(this._driver, [
|
|
153
|
+
message
|
|
154
|
+
], ()=>this._core._immediateAuthority === authority);
|
|
155
|
+
} catch (error) {
|
|
156
|
+
return captureFailure(error);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
_capture(event, properties = null, options = {}, skipRateLimit = false, personProperties) {
|
|
160
|
+
return void 0 !== this._admitCapture(event, properties, options, skipRateLimit, false, personProperties);
|
|
161
|
+
}
|
|
162
|
+
_admitCapture(event, properties, options, skipRateLimit, immediate, personProperties) {
|
|
163
|
+
if (!isNonEmptyString(event) || this._disposed || !this._core.canCapture) return;
|
|
164
|
+
if (!this._acceptRateLimit(skipRateLimit)) return;
|
|
165
|
+
const snapshot = this._snapshotProperties(properties, options);
|
|
166
|
+
if (!snapshot) return;
|
|
167
|
+
const candidate = this._prepareCapture(event, snapshot);
|
|
168
|
+
return candidate ? this._commitCapture(candidate, immediate, personProperties) : void 0;
|
|
169
|
+
}
|
|
170
|
+
_acceptRateLimit(skipRateLimit) {
|
|
171
|
+
if (skipRateLimit) return true;
|
|
172
|
+
const rateLimit = this._rateLimiter.consume();
|
|
173
|
+
if (rateLimit.allowed) return true;
|
|
174
|
+
if (void 0 !== rateLimit.reportDropped) this._reportRateLimit(rateLimit.reportDropped);
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
_reportRateLimit(dropped) {
|
|
178
|
+
const warning = `${dropped} event(s) dropped since the last warning. Client limit is 10 events per second with a burst of 100.`;
|
|
179
|
+
this._core.logger.warn(`PostHog client rate limited: ${warning}`);
|
|
180
|
+
if (this._capture(CLIENT_RATE_LIMIT_WARNING, {
|
|
181
|
+
$$client_ingestion_warning_message: `posthog-js client rate limited: ${warning}`
|
|
182
|
+
}, {}, true)) this._rateLimiter.reported();
|
|
183
|
+
}
|
|
184
|
+
_snapshotProperties(properties, options) {
|
|
185
|
+
const dynamic = {};
|
|
186
|
+
for (const producer of this._dynamicEventProperties.slice())try {
|
|
187
|
+
Object.assign(dynamic, producer());
|
|
188
|
+
} catch (error) {
|
|
189
|
+
this._core.logger.error('An event property producer failed', error);
|
|
190
|
+
}
|
|
191
|
+
try {
|
|
192
|
+
return snapshotCaptureProperties(this._registeredProperties.values(), dynamic, properties, options);
|
|
193
|
+
} catch (error) {
|
|
194
|
+
this._core.logger.error('Event properties are not JSON-serializable', error);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
_prepareCapture(event, snapshot) {
|
|
199
|
+
const state = this._core._state;
|
|
200
|
+
const processPersonProfile = this._hasPersonProcessing();
|
|
201
|
+
if (processPersonProfile) this._requirePersonProcessing();
|
|
202
|
+
const preparedSession = state.prepareSessionForEvent();
|
|
203
|
+
const session = preparedSession.context;
|
|
204
|
+
const distinctId = state.distinctId;
|
|
205
|
+
const groups = state.groups;
|
|
206
|
+
const metadata = {
|
|
207
|
+
token: this._core.projectToken,
|
|
208
|
+
distinct_id: distinctId,
|
|
209
|
+
$device_id: state.deviceId,
|
|
210
|
+
$groups: groups,
|
|
211
|
+
$session_id: session.sessionId,
|
|
212
|
+
$window_id: session.windowId,
|
|
213
|
+
$lib: 'web',
|
|
214
|
+
$lib_version: version,
|
|
215
|
+
$process_person_profile: processPersonProfile
|
|
216
|
+
};
|
|
217
|
+
const message = {
|
|
218
|
+
uuid: snapshot.uuid ?? createId(),
|
|
219
|
+
event,
|
|
220
|
+
distinct_id: distinctId,
|
|
221
|
+
properties: {
|
|
222
|
+
...snapshot.message,
|
|
223
|
+
...metadata
|
|
224
|
+
},
|
|
225
|
+
timestamp: eventTimestamp(snapshot.timestamp)
|
|
226
|
+
};
|
|
227
|
+
try {
|
|
228
|
+
let bytes = utf8Bytes(JSON.stringify(message));
|
|
229
|
+
if (bytes > MAX_ANALYTICS_BYTES) {
|
|
230
|
+
const minimum = utf8Bytes(JSON.stringify({
|
|
231
|
+
...message,
|
|
232
|
+
properties: unavoidableProperties(snapshot, metadata)
|
|
233
|
+
}));
|
|
234
|
+
if (minimum > MAX_ANALYTICS_BYTES) return void this._warnOversized(event, bytes);
|
|
235
|
+
}
|
|
236
|
+
const changed = applySessionProperties(snapshot, this._sessionProperties.values(session.sessionId));
|
|
237
|
+
message.properties = {
|
|
238
|
+
...snapshot.message,
|
|
239
|
+
...metadata
|
|
240
|
+
};
|
|
241
|
+
if (changed) bytes = utf8Bytes(JSON.stringify(message));
|
|
242
|
+
return {
|
|
243
|
+
message,
|
|
244
|
+
bytes,
|
|
245
|
+
preparedSession,
|
|
246
|
+
observedProperties: {
|
|
247
|
+
...snapshot.observed,
|
|
248
|
+
...metadata,
|
|
249
|
+
$groups: {
|
|
250
|
+
...groups
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
} catch (error) {
|
|
255
|
+
this._core.logger.error('The finalized event could not be measured', error);
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
_warnOversized(event, bytes) {
|
|
260
|
+
this._core.logger.warn(`Event "${event}" (${bytes} bytes) exceeds the local analytics limit and was dropped`);
|
|
261
|
+
}
|
|
262
|
+
_commitCapture(candidate, immediate, personProperties) {
|
|
263
|
+
const { message, bytes, preparedSession, observedProperties } = candidate;
|
|
264
|
+
if (immediate ? bytes > MAX_ANALYTICS_BYTES : !this._buffer.enqueue(message, bytes)) {
|
|
265
|
+
if (bytes > MAX_ANALYTICS_BYTES) this._warnOversized(message.event, bytes);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
if (!this._core._state.sessionAdmitted(preparedSession)) {
|
|
269
|
+
if (!immediate) this._buffer.discardQueued(message);
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
if (personProperties) this._lastPersonProperties = personProperties.signature;
|
|
273
|
+
else if (Object.hasOwn(message.properties, '$set') || Object.hasOwn(message.properties, '$set_once') || Object.hasOwn(message.properties, '$unset')) this._lastPersonProperties = void 0;
|
|
274
|
+
const session = preparedSession.context;
|
|
275
|
+
this._sessionProperties.admitted(session.sessionId);
|
|
276
|
+
if (preparedSession.reason) this._newSessionPublisher.publish({
|
|
277
|
+
...session,
|
|
278
|
+
reason: preparedSession.reason
|
|
279
|
+
});
|
|
280
|
+
this._eventPublisher.publish(deepFreeze({
|
|
281
|
+
event: message.event,
|
|
282
|
+
properties: observedProperties
|
|
283
|
+
}));
|
|
284
|
+
return message;
|
|
285
|
+
}
|
|
286
|
+
_hasPersonProcessing() {
|
|
287
|
+
const state = this._core._state;
|
|
288
|
+
return shouldProcessPerson(this._personProfiles, state.isIdentified, Object.keys(state.groups).length > 0, true === this._core.kv.get('personProcessingEnabled'));
|
|
289
|
+
}
|
|
290
|
+
_requirePersonProcessing() {
|
|
291
|
+
if ('never' === this._personProfiles) {
|
|
292
|
+
this._core.logger.warn('Person processing is disabled by analytics configuration');
|
|
293
|
+
return false;
|
|
294
|
+
}
|
|
295
|
+
if (true !== this._core.kv.get('personProcessingEnabled')) this._core.kv.set('personProcessingEnabled', true);
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
_canMutate() {
|
|
299
|
+
if (this._disposed || !this._core._canMutate()) return false;
|
|
300
|
+
this._core._state.prepare();
|
|
301
|
+
return true;
|
|
302
|
+
}
|
|
303
|
+
_canUseProperties() {
|
|
304
|
+
return !this._disposed && this._core._canMutate() && this._core._state.prepare();
|
|
305
|
+
}
|
|
306
|
+
_setPersonProperties(properties, options = {
|
|
307
|
+
set: properties.set,
|
|
308
|
+
setOnce: properties.setOnce
|
|
309
|
+
}, identification) {
|
|
310
|
+
if (!this._requirePersonProcessing() || this._lastPersonProperties === properties.signature) return;
|
|
311
|
+
this._lastPersonProperties = void 0;
|
|
312
|
+
this._changePropertiesPublisher.publish({
|
|
313
|
+
set: options.set,
|
|
314
|
+
setOnce: options.setOnce
|
|
315
|
+
});
|
|
316
|
+
if (identification) this._identifyPublisher.publish(identification);
|
|
317
|
+
this._capture('$set', null, options, false, {
|
|
318
|
+
signature: properties.signature
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
identify(distinctId, set, setOnce) {
|
|
322
|
+
if (!isValidDistinctId(distinctId) || !this._canMutate() || !this._requirePersonProcessing()) return;
|
|
323
|
+
const state = this._core._state;
|
|
324
|
+
const previousDistinctId = state.distinctId;
|
|
325
|
+
const wasIdentified = state.isIdentified;
|
|
326
|
+
const hasProperties = !!set || !!setOnce;
|
|
327
|
+
if (distinctId !== previousDistinctId || !wasIdentified) state.identify(distinctId);
|
|
328
|
+
const properties = snapshotPersonProperties(distinctId, set, setOnce);
|
|
329
|
+
if (!properties) {
|
|
330
|
+
this._core.logger.warn('Person properties must be JSON-serializable objects');
|
|
331
|
+
if (distinctId !== previousDistinctId || !wasIdentified || hasProperties) this._identifyPublisher.publish({
|
|
332
|
+
distinctId,
|
|
333
|
+
previousDistinctId,
|
|
334
|
+
wasIdentified,
|
|
335
|
+
set,
|
|
336
|
+
setOnce
|
|
337
|
+
});
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
const options = {};
|
|
341
|
+
if (set) options.set = properties.set;
|
|
342
|
+
if (setOnce) options.setOnce = properties.setOnce;
|
|
343
|
+
const info = {
|
|
344
|
+
distinctId,
|
|
345
|
+
previousDistinctId,
|
|
346
|
+
wasIdentified,
|
|
347
|
+
set: options.set,
|
|
348
|
+
setOnce: options.setOnce
|
|
349
|
+
};
|
|
350
|
+
const signature = {
|
|
351
|
+
signature: properties.signature
|
|
352
|
+
};
|
|
353
|
+
if (distinctId === previousDistinctId && wasIdentified) {
|
|
354
|
+
if (hasProperties) this._setPersonProperties(properties, options, info);
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
this._lastPersonProperties = void 0;
|
|
358
|
+
if (hasProperties) this._changePropertiesPublisher.publish({
|
|
359
|
+
set: options.set,
|
|
360
|
+
setOnce: options.setOnce
|
|
361
|
+
});
|
|
362
|
+
this._identifyPublisher.publish(info);
|
|
363
|
+
if (wasIdentified || distinctId === previousDistinctId) {
|
|
364
|
+
if (distinctId === previousDistinctId) this._capture('$set', null, {
|
|
365
|
+
set: properties.set,
|
|
366
|
+
setOnce: properties.setOnce
|
|
367
|
+
}, false, signature);
|
|
368
|
+
else if (hasProperties) this._capture('$set', null, options, false, signature);
|
|
369
|
+
} else this._capture('$identify', {
|
|
370
|
+
$anon_distinct_id: previousDistinctId
|
|
371
|
+
}, options, false, signature);
|
|
372
|
+
}
|
|
373
|
+
reset() {
|
|
374
|
+
if (!this._canMutate()) return;
|
|
375
|
+
this._core._state.reset();
|
|
376
|
+
this._lastPersonProperties = void 0;
|
|
377
|
+
this._sessionProperties.clear();
|
|
378
|
+
this._resetPublisher.publish(void 0);
|
|
379
|
+
}
|
|
380
|
+
_registration(properties) {
|
|
381
|
+
if (!this._canUseProperties()) return;
|
|
382
|
+
const snapshot = snapshotRegisteredProperties(properties);
|
|
383
|
+
if (!this._canUseProperties()) return;
|
|
384
|
+
if (!snapshot) this._core.logger.warn('Super properties must serialize to an object');
|
|
385
|
+
return snapshot;
|
|
386
|
+
}
|
|
387
|
+
register(properties) {
|
|
388
|
+
const snapshot = this._registration(properties);
|
|
389
|
+
if (snapshot) this._registeredProperties.register(snapshot);
|
|
390
|
+
}
|
|
391
|
+
registerOnce(properties, defaultValue = 'None') {
|
|
392
|
+
const snapshot = this._registration(properties);
|
|
393
|
+
if (snapshot) this._registeredProperties.registerOnce(snapshot, defaultValue);
|
|
394
|
+
}
|
|
395
|
+
registerForSession(properties) {
|
|
396
|
+
const snapshot = this._registration(properties);
|
|
397
|
+
if (snapshot) this._sessionProperties.register(snapshot, this._core._state.activeSessionId);
|
|
398
|
+
}
|
|
399
|
+
unregisterForSession(property) {
|
|
400
|
+
if (this._canUseProperties()) this._sessionProperties.unregister(property, this._core._state.activeSessionId);
|
|
401
|
+
}
|
|
402
|
+
unregister(property) {
|
|
403
|
+
if (this._canUseProperties()) this._registeredProperties.unregister(property);
|
|
404
|
+
}
|
|
405
|
+
getProperty(property) {
|
|
406
|
+
return this._canUseProperties() ? this._registeredProperties.get(property) : void 0;
|
|
407
|
+
}
|
|
408
|
+
registerDynamicEventProperties(producer) {
|
|
409
|
+
if (this._disposed || this._core._disposed) return {
|
|
410
|
+
dispose () {}
|
|
411
|
+
};
|
|
412
|
+
this._dynamicEventProperties.push(producer);
|
|
413
|
+
let active = true;
|
|
414
|
+
return {
|
|
415
|
+
dispose: ()=>{
|
|
416
|
+
if (!active) return;
|
|
417
|
+
active = false;
|
|
418
|
+
const index = this._dynamicEventProperties.indexOf(producer);
|
|
419
|
+
if (-1 !== index) this._dynamicEventProperties.splice(index, 1);
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
const analyticsOf = (posthog)=>posthog._analytics;
|
|
425
|
+
export { Analytics, analyticsOf, isNonEmptyString, isValidDistinctId };
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
import type { Client,
|
|
2
|
-
import type { BrowserExtension, ChangePropertiesInfo, GroupInfo, IdentifyInfo } from '../../core/context/browser-client';
|
|
3
|
-
import type { IAnalytics } from './analytics-api';
|
|
1
|
+
import type { Client, KeyValueStore } from '@posthog/browser-common';
|
|
4
2
|
import type { BrowserState } from '../../core/state/state';
|
|
5
|
-
import type { NewSessionInfo } from '../../core/types';
|
|
6
3
|
import type { AnalyticsOptions } from './analytics-options';
|
|
7
4
|
import type { EventBuffer } from './admission/event-buffer';
|
|
8
5
|
import type { LaneDelivery } from './delivery/lane';
|
|
@@ -17,7 +14,6 @@ export interface AnalyticsMessage {
|
|
|
17
14
|
}
|
|
18
15
|
export declare const MAX_ANALYTICS_BYTES: number;
|
|
19
16
|
export declare const MAX_ANALYTICS_BATCH_EVENTS = 100;
|
|
20
|
-
export declare const Analytics: ExtensionToken<IAnalyticsExtension>;
|
|
21
17
|
/** Browser capabilities supplied to capture delivery, independent of its queue. */
|
|
22
18
|
export interface CaptureHost {
|
|
23
19
|
runtime: RequestRuntime;
|
|
@@ -35,10 +31,10 @@ export interface AnalyticsDeliveryContext extends CaptureHost {
|
|
|
35
31
|
export interface AnalyticsDelivery extends LaneDelivery<AnalyticsMessage> {
|
|
36
32
|
deliverImmediate(messages: readonly AnalyticsMessage[], canContinue?: () => boolean): Promise<CaptureSummary>;
|
|
37
33
|
}
|
|
38
|
-
/**
|
|
34
|
+
/** Delivery driver attached to the client's event buffer. A driver without an immediate path reports immediate capture as unavailable. */
|
|
39
35
|
export interface AnalyticsDriver {
|
|
40
36
|
flush(): Promise<void>;
|
|
41
|
-
deliverImmediate
|
|
37
|
+
deliverImmediate?: AnalyticsDelivery['deliverImmediate'];
|
|
42
38
|
deliverUnload(messages: readonly AnalyticsMessage[]): void;
|
|
43
39
|
dispose(): Promise<void>;
|
|
44
40
|
}
|
|
@@ -47,33 +43,15 @@ export interface AnalyticsTeardownSubscription {
|
|
|
47
43
|
readonly deliveryAvailable: boolean;
|
|
48
44
|
dispose(): void;
|
|
49
45
|
}
|
|
50
|
-
/**
|
|
51
|
-
export interface
|
|
52
|
-
readonly
|
|
53
|
-
readonly
|
|
54
|
-
readonly
|
|
55
|
-
readonly
|
|
56
|
-
readonly
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
publishEvent(event: CapturedEventInfo): void;
|
|
62
|
-
publishNewSession(session: NewSessionInfo): void;
|
|
63
|
-
publishIdentify(info: IdentifyInfo): void;
|
|
64
|
-
publishChangeProperties(info: ChangePropertiesInfo): void;
|
|
65
|
-
publishGroup(info: GroupInfo): void;
|
|
66
|
-
publishReset(value: void): void;
|
|
67
|
-
publishGroupsReset(): void;
|
|
46
|
+
/** The client internals the capture pipeline reads. The client itself satisfies this. */
|
|
47
|
+
export interface AnalyticsCore extends Pick<Client, 'projectToken' | 'logger' | 'kv' | 'library' | 'onRemoteConfig'> {
|
|
48
|
+
readonly canCapture: boolean;
|
|
49
|
+
readonly _state: Pick<BrowserState, 'prepare' | 'distinctId' | 'deviceId' | 'groups' | 'activeSessionId' | 'isIdentified' | 'prepareSessionForEvent' | 'sessionAdmitted' | 'identify' | 'group' | 'reset' | 'resetGroups'>;
|
|
50
|
+
readonly _sessionKv: KeyValueStore;
|
|
51
|
+
readonly _requestRuntime: RequestRuntime;
|
|
52
|
+
readonly _disposed: boolean;
|
|
53
|
+
readonly _immediateAuthority: object;
|
|
54
|
+
_canMutate(): boolean;
|
|
55
|
+
_canDeliver(): boolean;
|
|
56
|
+
_startInitialPageview(): void;
|
|
68
57
|
}
|
|
69
|
-
/** First-party analytics is one configured owner of operations, buffering and delivery. */
|
|
70
|
-
export interface IAnalyticsExtension extends IAnalytics {
|
|
71
|
-
flush(reason?: 'flush' | 'shutdown'): Promise<void>;
|
|
72
|
-
purge(): void;
|
|
73
|
-
bindCaptureHost(host: AnalyticsCaptureHost): void;
|
|
74
|
-
captureInitialPageview(): boolean;
|
|
75
|
-
initialize(host: CaptureHost): void;
|
|
76
|
-
start(): Promise<void>;
|
|
77
|
-
onBeforeTeardown?(callback: () => void): AnalyticsTeardownSubscription;
|
|
78
|
-
}
|
|
79
|
-
export declare const isAnalyticsExtension: (extension: BrowserExtension) => extension is IAnalyticsExtension;
|
|
@@ -29,23 +29,15 @@ var __webpack_exports__ = {};
|
|
|
29
29
|
__webpack_require__.r(__webpack_exports__);
|
|
30
30
|
const MAX_ANALYTICS_BYTES = 8388608;
|
|
31
31
|
const MAX_ANALYTICS_BATCH_EVENTS = 100;
|
|
32
|
-
const Analytics = 'analytics';
|
|
33
|
-
const isAnalyticsExtension = (extension)=>extension.name === Analytics;
|
|
34
32
|
__webpack_require__.d(__webpack_exports__, {}, {
|
|
35
|
-
Analytics: Analytics,
|
|
36
33
|
MAX_ANALYTICS_BATCH_EVENTS: MAX_ANALYTICS_BATCH_EVENTS,
|
|
37
|
-
MAX_ANALYTICS_BYTES: MAX_ANALYTICS_BYTES
|
|
38
|
-
isAnalyticsExtension: isAnalyticsExtension
|
|
34
|
+
MAX_ANALYTICS_BYTES: MAX_ANALYTICS_BYTES
|
|
39
35
|
});
|
|
40
|
-
exports.Analytics = __webpack_exports__.Analytics;
|
|
41
36
|
exports.MAX_ANALYTICS_BATCH_EVENTS = __webpack_exports__.MAX_ANALYTICS_BATCH_EVENTS;
|
|
42
37
|
exports.MAX_ANALYTICS_BYTES = __webpack_exports__.MAX_ANALYTICS_BYTES;
|
|
43
|
-
exports.isAnalyticsExtension = __webpack_exports__.isAnalyticsExtension;
|
|
44
38
|
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
45
|
-
"Analytics",
|
|
46
39
|
"MAX_ANALYTICS_BATCH_EVENTS",
|
|
47
|
-
"MAX_ANALYTICS_BYTES"
|
|
48
|
-
"isAnalyticsExtension"
|
|
40
|
+
"MAX_ANALYTICS_BYTES"
|
|
49
41
|
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
50
42
|
Object.defineProperty(exports, '__esModule', {
|
|
51
43
|
value: true
|
|
@@ -1,5 +1,3 @@
|
|
|
1
1
|
const MAX_ANALYTICS_BYTES = 8388608;
|
|
2
2
|
const MAX_ANALYTICS_BATCH_EVENTS = 100;
|
|
3
|
-
|
|
4
|
-
const isAnalyticsExtension = (extension)=>extension.name === Analytics;
|
|
5
|
-
export { Analytics, MAX_ANALYTICS_BATCH_EVENTS, MAX_ANALYTICS_BYTES, isAnalyticsExtension };
|
|
3
|
+
export { MAX_ANALYTICS_BATCH_EVENTS, MAX_ANALYTICS_BYTES };
|
|
@@ -8,12 +8,3 @@ export interface AnalyticsOptions {
|
|
|
8
8
|
/** Maximum delay in milliseconds before queued events are delivered. Defaults to 3000; 0 disables it. */
|
|
9
9
|
flushInterval?: number;
|
|
10
10
|
}
|
|
11
|
-
export type LoadStrategy = 'lazy' | 'eager';
|
|
12
|
-
/** Automatic analytics loading configured by the default package entrypoint. */
|
|
13
|
-
export interface AutomaticAnalyticsOptions extends AnalyticsOptions {
|
|
14
|
-
/** Acquire analytics on demand and load delivery after admission, or acquire and start delivery during initialization. Defaults to `lazy`. */
|
|
15
|
-
load?: LoadStrategy;
|
|
16
|
-
}
|
|
17
|
-
export type AnalyticsConfiguration = false | AutomaticAnalyticsOptions;
|
|
18
|
-
/** Capture configuration at client creation, independently of when the product loads. */
|
|
19
|
-
export declare const snapshotAutomaticAnalyticsOptions: (options?: AutomaticAnalyticsOptions) => AutomaticAnalyticsOptions;
|
|
@@ -1,20 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
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
3
|
(()=>{
|
|
19
4
|
__webpack_require__.r = (exports1)=>{
|
|
20
5
|
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
@@ -27,29 +12,7 @@ var __webpack_require__ = {};
|
|
|
27
12
|
})();
|
|
28
13
|
var __webpack_exports__ = {};
|
|
29
14
|
__webpack_require__.r(__webpack_exports__);
|
|
30
|
-
|
|
31
|
-
const snapshot = {};
|
|
32
|
-
for (const key of [
|
|
33
|
-
'flushAt',
|
|
34
|
-
'flushInterval',
|
|
35
|
-
'personProfiles',
|
|
36
|
-
'load'
|
|
37
|
-
])try {
|
|
38
|
-
const value = options[key];
|
|
39
|
-
if (void 0 !== value) Object.defineProperty(snapshot, key, {
|
|
40
|
-
value,
|
|
41
|
-
enumerable: true
|
|
42
|
-
});
|
|
43
|
-
} catch {}
|
|
44
|
-
return snapshot;
|
|
45
|
-
};
|
|
46
|
-
__webpack_require__.d(__webpack_exports__, {}, {
|
|
47
|
-
snapshotAutomaticAnalyticsOptions: snapshotAutomaticAnalyticsOptions
|
|
48
|
-
});
|
|
49
|
-
exports.snapshotAutomaticAnalyticsOptions = __webpack_exports__.snapshotAutomaticAnalyticsOptions;
|
|
50
|
-
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
51
|
-
"snapshotAutomaticAnalyticsOptions"
|
|
52
|
-
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
15
|
+
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
53
16
|
Object.defineProperty(exports, '__esModule', {
|
|
54
17
|
value: true
|
|
55
18
|
});
|
|
@@ -1,17 +1 @@
|
|
|
1
|
-
|
|
2
|
-
const snapshot = {};
|
|
3
|
-
for (const key of [
|
|
4
|
-
'flushAt',
|
|
5
|
-
'flushInterval',
|
|
6
|
-
'personProfiles',
|
|
7
|
-
'load'
|
|
8
|
-
])try {
|
|
9
|
-
const value = options[key];
|
|
10
|
-
if (void 0 !== value) Object.defineProperty(snapshot, key, {
|
|
11
|
-
value,
|
|
12
|
-
enumerable: true
|
|
13
|
-
});
|
|
14
|
-
} catch {}
|
|
15
|
-
return snapshot;
|
|
16
|
-
};
|
|
17
|
-
export { snapshotAutomaticAnalyticsOptions };
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { CaptureOptions } from '@posthog/browser-common';
|
|
2
|
+
import type { IPostHog } from '../../core/types';
|
|
3
|
+
/** Associates future events with a group and, when person processing is enabled, records its properties. */
|
|
4
|
+
export declare const group: (posthog: IPostHog, type: string, key: string, properties?: Record<string, unknown>) => void;
|
|
5
|
+
/** Sends a group-profile update without changing group membership. */
|
|
6
|
+
export declare const groupIdentify: (posthog: IPostHog, type: string, key: string | number, properties?: Record<string, unknown>, options?: CaptureOptions) => void;
|
|
7
|
+
/** Clears group memberships, retaining identity, sessions and registered properties. */
|
|
8
|
+
export declare const resetGroups: (posthog: IPostHog) => void;
|