@posthog/browser 0.0.0 → 0.1.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 +559 -0
- package/LICENSE +417 -0
- package/README.md +286 -28
- package/dist/analytics-buffer.d.ts +4 -0
- package/dist/analytics-buffer.js +200 -0
- package/dist/analytics-buffer.mjs +162 -0
- package/dist/analytics-delivery.d.ts +3 -0
- package/dist/analytics-delivery.js +231 -0
- package/dist/analytics-delivery.mjs +193 -0
- package/dist/analytics-internal.d.ts +64 -0
- package/dist/analytics-internal.js +52 -0
- package/dist/analytics-internal.mjs +5 -0
- package/dist/analytics-options.d.ts +14 -0
- package/dist/analytics-options.js +18 -0
- package/dist/analytics-options.mjs +1 -0
- package/dist/analytics.d.ts +5 -0
- package/dist/analytics.js +42 -0
- package/dist/analytics.mjs +4 -0
- package/dist/autocapture-options.d.ts +32 -0
- package/dist/autocapture-options.js +49 -0
- package/dist/autocapture-options.mjs +11 -0
- package/dist/autocapture.d.ts +5 -0
- package/dist/autocapture.js +108 -0
- package/dist/autocapture.mjs +70 -0
- package/dist/automatic-analytics.d.ts +3 -0
- package/dist/automatic-analytics.js +44 -0
- package/dist/automatic-analytics.mjs +6 -0
- package/dist/automatic-surveys.d.ts +2 -0
- package/dist/automatic-surveys.js +41 -0
- package/dist/automatic-surveys.mjs +3 -0
- package/dist/bot-filter.d.ts +2 -0
- package/dist/bot-filter.js +87 -0
- package/dist/bot-filter.mjs +49 -0
- package/dist/browser-client.d.ts +30 -0
- package/dist/browser-client.js +18 -0
- package/dist/browser-client.mjs +1 -0
- package/dist/capture-summary.d.ts +3 -0
- package/dist/capture-summary.js +63 -0
- package/dist/capture-summary.mjs +22 -0
- package/dist/capture-v1.d.ts +105 -0
- package/dist/capture-v1.js +781 -0
- package/dist/capture-v1.mjs +722 -0
- package/dist/core.d.ts +7 -0
- package/dist/core.js +49 -0
- package/dist/core.mjs +5 -0
- package/dist/event-buffer.d.ts +41 -0
- package/dist/event-buffer.js +158 -0
- package/dist/event-buffer.mjs +120 -0
- package/dist/extensions/registry.d.ts +16 -0
- package/dist/extensions/registry.js +128 -0
- package/dist/extensions/registry.mjs +90 -0
- package/dist/flags-internal.d.ts +8 -0
- package/dist/flags-internal.js +18 -0
- package/dist/flags-internal.mjs +1 -0
- package/dist/flags-options.d.ts +22 -0
- package/dist/flags-options.js +18 -0
- package/dist/flags-options.mjs +1 -0
- package/dist/flags-token.d.ts +15 -0
- package/dist/flags-token.js +40 -0
- package/dist/flags-token.mjs +2 -0
- package/dist/flags.d.ts +6 -0
- package/dist/flags.js +163 -0
- package/dist/flags.mjs +122 -0
- package/dist/id.d.ts +1 -0
- package/dist/id.js +61 -0
- package/dist/id.mjs +23 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +119 -0
- package/dist/index.mjs +75 -0
- package/dist/lane.d.ts +57 -0
- package/dist/lane.js +363 -0
- package/dist/lane.mjs +325 -0
- package/dist/logger.d.ts +4 -0
- package/dist/logger.js +57 -0
- package/dist/logger.mjs +19 -0
- package/dist/logs-internal.d.ts +7 -0
- package/dist/logs-internal.js +18 -0
- package/dist/logs-internal.mjs +1 -0
- package/dist/logs-options.d.ts +11 -0
- package/dist/logs-options.js +57 -0
- package/dist/logs-options.mjs +19 -0
- package/dist/logs.d.ts +6 -0
- package/dist/logs.js +140 -0
- package/dist/logs.mjs +102 -0
- package/dist/named-error.d.ts +7 -0
- package/dist/named-error.js +49 -0
- package/dist/named-error.mjs +11 -0
- package/dist/posthog.d.ts +3 -0
- package/dist/posthog.js +823 -0
- package/dist/posthog.mjs +785 -0
- package/dist/rate-limiter.d.ts +19 -0
- package/dist/rate-limiter.js +89 -0
- package/dist/rate-limiter.mjs +51 -0
- package/dist/remote-config.d.ts +3 -0
- package/dist/remote-config.js +47 -0
- package/dist/remote-config.mjs +9 -0
- package/dist/request.d.ts +14 -0
- package/dist/request.js +130 -0
- package/dist/request.mjs +89 -0
- package/dist/state.d.ts +82 -0
- package/dist/state.js +625 -0
- package/dist/state.mjs +581 -0
- package/dist/surveys-extension.d.ts +9 -0
- package/dist/surveys-extension.js +272 -0
- package/dist/surveys-extension.mjs +234 -0
- package/dist/surveys-internal.d.ts +12 -0
- package/dist/surveys-internal.js +18 -0
- package/dist/surveys-internal.mjs +1 -0
- package/dist/surveys-options.d.ts +18 -0
- package/dist/surveys-options.js +42 -0
- package/dist/surveys-options.mjs +4 -0
- package/dist/surveys.d.ts +7 -0
- package/dist/surveys.js +48 -0
- package/dist/surveys.mjs +7 -0
- package/dist/types.d.ts +114 -0
- package/dist/types.js +18 -0
- package/dist/types.mjs +1 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +40 -0
- package/dist/version.mjs +2 -0
- package/package.json +89 -8
package/dist/state.mjs
ADDED
|
@@ -0,0 +1,581 @@
|
|
|
1
|
+
import { createId } from "./id.mjs";
|
|
2
|
+
const emptyRecord = ()=>Object.create(null);
|
|
3
|
+
const cloneJson = (value)=>{
|
|
4
|
+
const json = JSON.stringify(value);
|
|
5
|
+
if (void 0 === json) throw new TypeError('The value must be JSON-serializable');
|
|
6
|
+
return JSON.parse(json);
|
|
7
|
+
};
|
|
8
|
+
const isRecord = (value)=>'object' == typeof value && null !== value && !Array.isArray(value);
|
|
9
|
+
const readGroups = (value)=>{
|
|
10
|
+
const groups = emptyRecord();
|
|
11
|
+
if (isRecord(value)) Object.entries(value).forEach(([key, group])=>{
|
|
12
|
+
if ('string' == typeof group) groups[key] = group;
|
|
13
|
+
});
|
|
14
|
+
return groups;
|
|
15
|
+
};
|
|
16
|
+
const isTimestamp = (value)=>'number' == typeof value && Number.isFinite(value) && value >= 0;
|
|
17
|
+
const readRevision = (value, fallback)=>{
|
|
18
|
+
if ('string' == typeof value && /^(0|[1-9]\d*)$/.test(value)) return value;
|
|
19
|
+
const number = void 0 === value ? fallback : value;
|
|
20
|
+
return isTimestamp(number) && Number.isSafeInteger(number) ? String(number) : void 0;
|
|
21
|
+
};
|
|
22
|
+
const compareRevisions = (left, right)=>{
|
|
23
|
+
if (left === right) return 0;
|
|
24
|
+
if (void 0 === left) return -1;
|
|
25
|
+
if (void 0 === right) return 1;
|
|
26
|
+
return left.length === right.length ? left > right ? 1 : -1 : left.length > right.length ? 1 : -1;
|
|
27
|
+
};
|
|
28
|
+
const incrementRevision = (revision)=>{
|
|
29
|
+
const digits = revision.split('');
|
|
30
|
+
for(let index = digits.length - 1; index >= 0; index--){
|
|
31
|
+
if ('9' !== digits[index]) {
|
|
32
|
+
digits[index] = String(Number(digits[index]) + 1);
|
|
33
|
+
return digits.join('');
|
|
34
|
+
}
|
|
35
|
+
digits[index] = '0';
|
|
36
|
+
}
|
|
37
|
+
return `1${digits.join('')}`;
|
|
38
|
+
};
|
|
39
|
+
const readSession = (value)=>{
|
|
40
|
+
if (!isRecord(value)) return;
|
|
41
|
+
const { sessionId, sessionStartTimestamp, lastActivityTimestamp } = value;
|
|
42
|
+
const revision = readRevision(value.revision, sessionStartTimestamp);
|
|
43
|
+
if ('string' != typeof sessionId || 0 === sessionId.length || !isTimestamp(sessionStartTimestamp) || !isTimestamp(lastActivityTimestamp) || !revision) return;
|
|
44
|
+
return {
|
|
45
|
+
sessionId,
|
|
46
|
+
sessionStartTimestamp,
|
|
47
|
+
lastActivityTimestamp,
|
|
48
|
+
revision
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
const readExtensionData = (value)=>{
|
|
52
|
+
const extensionData = emptyRecord();
|
|
53
|
+
if (isRecord(value)) Object.entries(value).forEach(([key, data])=>{
|
|
54
|
+
if (isRecord(data)) {
|
|
55
|
+
const namespaceData = emptyRecord();
|
|
56
|
+
Object.entries(data).forEach(([dataKey, dataValue])=>{
|
|
57
|
+
namespaceData[dataKey] = dataValue;
|
|
58
|
+
});
|
|
59
|
+
extensionData[key] = namespaceData;
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
return extensionData;
|
|
63
|
+
};
|
|
64
|
+
const createSession = (now, revision)=>({
|
|
65
|
+
sessionId: createId(),
|
|
66
|
+
sessionStartTimestamp: now,
|
|
67
|
+
lastActivityTimestamp: now,
|
|
68
|
+
revision
|
|
69
|
+
});
|
|
70
|
+
const authorityRevision = (state)=>state?.session?.revision ?? state?.sessionReset;
|
|
71
|
+
const persistedAuthorityWins = (persisted, local)=>{
|
|
72
|
+
const persistedRevision = authorityRevision(persisted);
|
|
73
|
+
const localRevision = authorityRevision(local);
|
|
74
|
+
const comparison = compareRevisions(persistedRevision, localRevision);
|
|
75
|
+
if (0 !== comparison) return comparison > 0;
|
|
76
|
+
if (void 0 === persistedRevision) return false;
|
|
77
|
+
if (!persisted.session || !local.session) return !!persisted.session !== !!local.session;
|
|
78
|
+
return persisted.session.sessionId !== local.session.sessionId || persisted.session.lastActivityTimestamp > local.session.lastActivityTimestamp;
|
|
79
|
+
};
|
|
80
|
+
const copyAuthority = (target, source)=>{
|
|
81
|
+
delete target.session;
|
|
82
|
+
delete target.sessionReset;
|
|
83
|
+
if (source.session) target.session = source.session;
|
|
84
|
+
else if (void 0 !== source.sessionReset) target.sessionReset = source.sessionReset;
|
|
85
|
+
};
|
|
86
|
+
const nextRevision = (now, local, external)=>{
|
|
87
|
+
const localRevision = authorityRevision(local);
|
|
88
|
+
const externalRevision = authorityRevision(external);
|
|
89
|
+
const latest = compareRevisions(localRevision, externalRevision) >= 0 ? localRevision : externalRevision;
|
|
90
|
+
const incremented = latest ? incrementRevision(latest) : '0';
|
|
91
|
+
const clock = readRevision(now) ?? '0';
|
|
92
|
+
return compareRevisions(clock, incremented) > 0 ? clock : incremented;
|
|
93
|
+
};
|
|
94
|
+
const createInitialState = (deviceId = createId(), anonymousId = deviceId)=>({
|
|
95
|
+
version: 1,
|
|
96
|
+
deviceId,
|
|
97
|
+
anonymousId,
|
|
98
|
+
distinctId: anonymousId,
|
|
99
|
+
isIdentified: false,
|
|
100
|
+
groups: emptyRecord(),
|
|
101
|
+
extensionData: emptyRecord()
|
|
102
|
+
});
|
|
103
|
+
const decodeConsent = (value)=>{
|
|
104
|
+
const normalized = 'string' == typeof value ? value.trim().toLowerCase() : value;
|
|
105
|
+
return true === normalized || 1 === normalized || '1' === normalized || 'true' === normalized || 'yes' === normalized ? 'granted' : false === normalized || 0 === normalized || '0' === normalized || 'false' === normalized || 'no' === normalized ? 'denied' : void 0;
|
|
106
|
+
};
|
|
107
|
+
const parseState = (value)=>{
|
|
108
|
+
if (!value) return;
|
|
109
|
+
try {
|
|
110
|
+
const parsed = JSON.parse(value);
|
|
111
|
+
if (!isRecord(parsed) || 1 !== parsed.version) return;
|
|
112
|
+
const anonymousId = parsed.anonymousId;
|
|
113
|
+
const distinctId = parsed.distinctId;
|
|
114
|
+
const deviceId = 'string' == typeof parsed.deviceId ? parsed.deviceId : anonymousId;
|
|
115
|
+
const hasSession = Object.prototype.hasOwnProperty.call(parsed, 'session');
|
|
116
|
+
const hasReset = Object.prototype.hasOwnProperty.call(parsed, 'sessionReset');
|
|
117
|
+
const session = readSession(parsed.session);
|
|
118
|
+
const sessionReset = readRevision(parsed.sessionReset);
|
|
119
|
+
if ('string' != typeof deviceId || 'string' != typeof anonymousId || 'string' != typeof distinctId || hasSession && !session || hasReset && !sessionReset || session && hasReset) return;
|
|
120
|
+
return {
|
|
121
|
+
version: 1,
|
|
122
|
+
deviceId,
|
|
123
|
+
anonymousId,
|
|
124
|
+
distinctId,
|
|
125
|
+
isIdentified: true === parsed.isIdentified || distinctId !== anonymousId,
|
|
126
|
+
groups: readGroups(parsed.groups),
|
|
127
|
+
...session ? {
|
|
128
|
+
session
|
|
129
|
+
} : {},
|
|
130
|
+
...sessionReset ? {
|
|
131
|
+
sessionReset
|
|
132
|
+
} : {},
|
|
133
|
+
extensionData: readExtensionData(parsed.extensionData)
|
|
134
|
+
};
|
|
135
|
+
} catch {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
const getDefaultStorage = ()=>{
|
|
140
|
+
try {
|
|
141
|
+
return globalThis.localStorage;
|
|
142
|
+
} catch {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
const getDefaultSessionStorage = ()=>{
|
|
147
|
+
try {
|
|
148
|
+
return globalThis.sessionStorage;
|
|
149
|
+
} catch {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
class BrowserState {
|
|
154
|
+
constructor(projectToken, storage, persistenceKey, consentPersistenceName, optOutByDefault, windowStorageFactory, onConsentChange){
|
|
155
|
+
this._windowId = '';
|
|
156
|
+
this._windowStorageInitialized = false;
|
|
157
|
+
this._windowListenerInstalled = false;
|
|
158
|
+
this._lastActivityWriteTimestamp = 0;
|
|
159
|
+
this._stateReadPending = false;
|
|
160
|
+
this._lastConsentValue = null;
|
|
161
|
+
this._hasIgnoredConsentValue = false;
|
|
162
|
+
this._beforeUnload = ()=>{
|
|
163
|
+
this._flushSession();
|
|
164
|
+
this._windowRemove(this._primaryWindowKey);
|
|
165
|
+
};
|
|
166
|
+
this._storage = storage;
|
|
167
|
+
this._windowStorageFactory = windowStorageFactory;
|
|
168
|
+
this._stateKey = persistenceKey ?? `ph_${projectToken}_posthog_browser_v2`;
|
|
169
|
+
const windowPrefix = persistenceKey ?? `ph_${projectToken}`;
|
|
170
|
+
this._windowKey = `${windowPrefix}_window_id`;
|
|
171
|
+
this._primaryWindowKey = `${windowPrefix}_primary_window_exists`;
|
|
172
|
+
this.consentKey = void 0 === consentPersistenceName ? `__ph_opt_in_out_${projectToken}` : consentPersistenceName;
|
|
173
|
+
this._defaultConsent = optOutByDefault ? 'denied' : 'implicit';
|
|
174
|
+
this._consent = this._defaultConsent;
|
|
175
|
+
this._onConsentChange = onConsentChange;
|
|
176
|
+
const [consentRead, storedConsent] = this._read(this.consentKey);
|
|
177
|
+
if (consentRead) {
|
|
178
|
+
this._lastConsentValue = storedConsent;
|
|
179
|
+
this._consent = decodeConsent(storedConsent) ?? this._defaultConsent;
|
|
180
|
+
}
|
|
181
|
+
const [stateRead, storedState] = this._read(this._stateKey);
|
|
182
|
+
this._state = parseState('string' == typeof storedState ? storedState : null) ?? createInitialState();
|
|
183
|
+
this._lastActivityWriteTimestamp = this._state.session?.lastActivityTimestamp ?? 0;
|
|
184
|
+
this._stateReadPending = !stateRead;
|
|
185
|
+
if (stateRead) this._save();
|
|
186
|
+
}
|
|
187
|
+
prepare() {
|
|
188
|
+
if (!this._stateReadPending) return true;
|
|
189
|
+
const [read, storedState] = this._read(this._stateKey);
|
|
190
|
+
if (!read) return false;
|
|
191
|
+
this._stateReadPending = false;
|
|
192
|
+
const persisted = parseState('string' == typeof storedState ? storedState : null);
|
|
193
|
+
if (persisted) {
|
|
194
|
+
this._state = persisted;
|
|
195
|
+
this._lastActivityWriteTimestamp = persisted.session?.lastActivityTimestamp ?? 0;
|
|
196
|
+
} else this._save(false);
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
get deviceId() {
|
|
200
|
+
return this._state.deviceId;
|
|
201
|
+
}
|
|
202
|
+
get anonymousId() {
|
|
203
|
+
return this._state.anonymousId;
|
|
204
|
+
}
|
|
205
|
+
get distinctId() {
|
|
206
|
+
return this._state.distinctId;
|
|
207
|
+
}
|
|
208
|
+
get isIdentified() {
|
|
209
|
+
return this._state.isIdentified;
|
|
210
|
+
}
|
|
211
|
+
get groups() {
|
|
212
|
+
return {
|
|
213
|
+
...this._state.groups
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
get session() {
|
|
217
|
+
const session = this._state.session;
|
|
218
|
+
if (!session || !this._windowId) return;
|
|
219
|
+
return {
|
|
220
|
+
sessionId: session.sessionId,
|
|
221
|
+
windowId: this._windowId,
|
|
222
|
+
sessionStartTimestamp: session.sessionStartTimestamp,
|
|
223
|
+
lastActivityTimestamp: session.lastActivityTimestamp
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
get consent() {
|
|
227
|
+
return this.refreshConsent();
|
|
228
|
+
}
|
|
229
|
+
refreshConsent() {
|
|
230
|
+
const [read, value] = this._read(this.consentKey);
|
|
231
|
+
return read ? this.observeConsent(value) : this._consent;
|
|
232
|
+
}
|
|
233
|
+
observeConsent(value) {
|
|
234
|
+
if (this._hasIgnoredConsentValue && value === this._ignoredConsentValue) return this._consent;
|
|
235
|
+
this._hasIgnoredConsentValue = false;
|
|
236
|
+
this._lastConsentValue = value;
|
|
237
|
+
this._applyConsent(decodeConsent(value) ?? this._defaultConsent);
|
|
238
|
+
return this._consent;
|
|
239
|
+
}
|
|
240
|
+
identify(distinctId) {
|
|
241
|
+
this._state.distinctId = distinctId;
|
|
242
|
+
this._state.isIdentified = true;
|
|
243
|
+
this._save();
|
|
244
|
+
}
|
|
245
|
+
group(type, key) {
|
|
246
|
+
if (this._state.groups[type] === key) return false;
|
|
247
|
+
this._state.groups[type] = key;
|
|
248
|
+
this._save();
|
|
249
|
+
return true;
|
|
250
|
+
}
|
|
251
|
+
prepareSessionForEvent(now = Date.now()) {
|
|
252
|
+
const safeNow = isTimestamp(now) ? now : 0;
|
|
253
|
+
const [sharedRead, external] = this._readPersistedState();
|
|
254
|
+
let session = this._state.session;
|
|
255
|
+
let lastActivityWriteTimestamp = this._lastActivityWriteTimestamp;
|
|
256
|
+
let adoptedAfterReset = false;
|
|
257
|
+
if (sharedRead && external && persistedAuthorityWins(external, this._state)) {
|
|
258
|
+
session = external.session;
|
|
259
|
+
if (session) {
|
|
260
|
+
lastActivityWriteTimestamp = session.lastActivityTimestamp;
|
|
261
|
+
adoptedAfterReset = 'reset' === this._pendingSessionReason;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
const pendingReason = this._pendingSessionReason;
|
|
265
|
+
const idle = session ? Math.abs(safeNow - session.lastActivityTimestamp) > 1800000 : false;
|
|
266
|
+
const maximum = session ? Math.abs(safeNow - session.sessionStartTimestamp) > 86400000 : false;
|
|
267
|
+
const rotated = !!(pendingReason || !session || idle || maximum);
|
|
268
|
+
const reason = adoptedAfterReset ? 'reset' : pendingReason ?? (idle ? 'idleTimeout' : maximum ? 'maxLength' : void 0);
|
|
269
|
+
const preparedSession = rotated ? createSession(safeNow, nextRevision(safeNow, this._state, external)) : {
|
|
270
|
+
...session,
|
|
271
|
+
lastActivityTimestamp: Math.max(safeNow, session.lastActivityTimestamp)
|
|
272
|
+
};
|
|
273
|
+
const window = this._prepareWindow(rotated);
|
|
274
|
+
return {
|
|
275
|
+
context: {
|
|
276
|
+
sessionId: preparedSession.sessionId,
|
|
277
|
+
windowId: window.windowId,
|
|
278
|
+
sessionStartTimestamp: preparedSession.sessionStartTimestamp,
|
|
279
|
+
lastActivityTimestamp: preparedSession.lastActivityTimestamp
|
|
280
|
+
},
|
|
281
|
+
session: preparedSession,
|
|
282
|
+
reason,
|
|
283
|
+
rotated,
|
|
284
|
+
lastActivityWriteTimestamp,
|
|
285
|
+
windowStorage: window.storage,
|
|
286
|
+
windowStorageResolved: window.storageResolved,
|
|
287
|
+
previousWindowId: this._windowId,
|
|
288
|
+
previousWindowStorageInitialized: this._windowStorageInitialized,
|
|
289
|
+
previousPendingReason: this._pendingSessionReason
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
sessionAdmitted(prepared) {
|
|
293
|
+
const [, external] = this._readPersistedState();
|
|
294
|
+
const preparedState = {
|
|
295
|
+
...this._state,
|
|
296
|
+
session: prepared.session
|
|
297
|
+
};
|
|
298
|
+
delete preparedState.sessionReset;
|
|
299
|
+
if (external && persistedAuthorityWins(external, preparedState)) return false;
|
|
300
|
+
this._state.session = prepared.session;
|
|
301
|
+
delete this._state.sessionReset;
|
|
302
|
+
this._pendingSessionReason = void 0;
|
|
303
|
+
this._windowId = prepared.context.windowId;
|
|
304
|
+
this._initializeWindowStorage(prepared.windowStorage, prepared.windowStorageResolved);
|
|
305
|
+
this._setWindowId(prepared.context.windowId);
|
|
306
|
+
if (prepared.rotated) {
|
|
307
|
+
this._lastActivityWriteTimestamp = prepared.session.lastActivityTimestamp;
|
|
308
|
+
this._save();
|
|
309
|
+
} else if (Math.abs(prepared.session.lastActivityTimestamp - prepared.lastActivityWriteTimestamp) >= 60000) {
|
|
310
|
+
this._lastActivityWriteTimestamp = prepared.session.lastActivityTimestamp;
|
|
311
|
+
this._save();
|
|
312
|
+
} else this._lastActivityWriteTimestamp = prepared.lastActivityWriteTimestamp;
|
|
313
|
+
const [, committed] = this._readPersistedState();
|
|
314
|
+
if (committed && persistedAuthorityWins(committed, preparedState)) {
|
|
315
|
+
copyAuthority(this._state, committed);
|
|
316
|
+
this._pendingSessionReason = prepared.previousPendingReason;
|
|
317
|
+
this._lastActivityWriteTimestamp = committed.session?.lastActivityTimestamp ?? 0;
|
|
318
|
+
if (prepared.previousWindowStorageInitialized) this._setWindowId(prepared.previousWindowId);
|
|
319
|
+
else this._clearWindow(true);
|
|
320
|
+
return false;
|
|
321
|
+
}
|
|
322
|
+
return true;
|
|
323
|
+
}
|
|
324
|
+
reset() {
|
|
325
|
+
const [, external] = this._readPersistedState();
|
|
326
|
+
const now = Date.now();
|
|
327
|
+
const safeNow = isTimestamp(now) ? now : 0;
|
|
328
|
+
const resetRevision = nextRevision(safeNow, this._state, external);
|
|
329
|
+
this._state = createInitialState(this._state.deviceId, createId());
|
|
330
|
+
this._state.sessionReset = resetRevision;
|
|
331
|
+
this._lastActivityWriteTimestamp = 0;
|
|
332
|
+
this._pendingSessionReason = 'reset';
|
|
333
|
+
this._clearWindow(true);
|
|
334
|
+
this._save(false);
|
|
335
|
+
}
|
|
336
|
+
dispose() {
|
|
337
|
+
this._flushSession();
|
|
338
|
+
this._removeWindowListener();
|
|
339
|
+
this._windowRemove(this._primaryWindowKey);
|
|
340
|
+
}
|
|
341
|
+
optIn() {
|
|
342
|
+
this._applyConsent('granted');
|
|
343
|
+
const written = this._writeConsent('1');
|
|
344
|
+
this._save();
|
|
345
|
+
return written;
|
|
346
|
+
}
|
|
347
|
+
optOut() {
|
|
348
|
+
this._applyConsent('denied');
|
|
349
|
+
return this._writeConsent('0');
|
|
350
|
+
}
|
|
351
|
+
keyValueStore(namespace, canAccess = ()=>true) {
|
|
352
|
+
const values = ()=>this._state.extensionData[namespace] ??= emptyRecord();
|
|
353
|
+
const read = (key)=>{
|
|
354
|
+
if (!canAccess()) return;
|
|
355
|
+
const value = values()[key];
|
|
356
|
+
return void 0 === value ? void 0 : cloneJson(value);
|
|
357
|
+
};
|
|
358
|
+
return {
|
|
359
|
+
initialize () {},
|
|
360
|
+
get: (keyOrKeys)=>{
|
|
361
|
+
if ('string' == typeof keyOrKeys) return read(keyOrKeys);
|
|
362
|
+
const entries = emptyRecord();
|
|
363
|
+
for (const key of keyOrKeys){
|
|
364
|
+
const value = read(key);
|
|
365
|
+
if (void 0 !== value) entries[key] = value;
|
|
366
|
+
}
|
|
367
|
+
return entries;
|
|
368
|
+
},
|
|
369
|
+
set: (keyOrValues, value)=>{
|
|
370
|
+
if (!canAccess()) return;
|
|
371
|
+
const namespaceValues = values();
|
|
372
|
+
const entries = 'string' == typeof keyOrValues ? {
|
|
373
|
+
[keyOrValues]: value
|
|
374
|
+
} : keyOrValues;
|
|
375
|
+
for (const [key, entry] of Object.entries(entries))if (void 0 === entry) delete namespaceValues[key];
|
|
376
|
+
else namespaceValues[key] = cloneJson(entry);
|
|
377
|
+
this._save();
|
|
378
|
+
},
|
|
379
|
+
remove: (keyOrKeys)=>{
|
|
380
|
+
if (!canAccess()) return;
|
|
381
|
+
const namespaceValues = values();
|
|
382
|
+
for (const key of 'string' == typeof keyOrKeys ? [
|
|
383
|
+
keyOrKeys
|
|
384
|
+
] : keyOrKeys)delete namespaceValues[key];
|
|
385
|
+
this._save();
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
_readPersistedState() {
|
|
390
|
+
if (!this._storage) return [
|
|
391
|
+
false
|
|
392
|
+
];
|
|
393
|
+
const [read, value] = this._read(this._stateKey);
|
|
394
|
+
if (!read) return [
|
|
395
|
+
false
|
|
396
|
+
];
|
|
397
|
+
if (null === value) return [
|
|
398
|
+
true
|
|
399
|
+
];
|
|
400
|
+
if ('string' != typeof value) return [
|
|
401
|
+
false
|
|
402
|
+
];
|
|
403
|
+
const state = parseState(value);
|
|
404
|
+
return state ? [
|
|
405
|
+
true,
|
|
406
|
+
state
|
|
407
|
+
] : [
|
|
408
|
+
false
|
|
409
|
+
];
|
|
410
|
+
}
|
|
411
|
+
_prepareWindow(rotated) {
|
|
412
|
+
if (rotated || this._windowId) return {
|
|
413
|
+
windowId: rotated ? createId() : this._windowId,
|
|
414
|
+
storage: void 0,
|
|
415
|
+
storageResolved: false
|
|
416
|
+
};
|
|
417
|
+
let storage;
|
|
418
|
+
try {
|
|
419
|
+
storage = this._windowStorageFactory?.();
|
|
420
|
+
} catch {
|
|
421
|
+
storage = void 0;
|
|
422
|
+
}
|
|
423
|
+
let lastWindowId = null;
|
|
424
|
+
let primaryWindowExists = null;
|
|
425
|
+
try {
|
|
426
|
+
lastWindowId = storage?.getItem(this._windowKey) ?? null;
|
|
427
|
+
} catch {}
|
|
428
|
+
try {
|
|
429
|
+
primaryWindowExists = storage?.getItem(this._primaryWindowKey) ?? null;
|
|
430
|
+
} catch {}
|
|
431
|
+
const reusable = lastWindowId && (!primaryWindowExists || this._isReload()) ? lastWindowId : void 0;
|
|
432
|
+
return {
|
|
433
|
+
windowId: reusable ?? createId(),
|
|
434
|
+
storage,
|
|
435
|
+
storageResolved: true
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
_initializeWindowStorage(preparedStorage, storageResolved = false) {
|
|
439
|
+
if (this._windowStorageInitialized) return;
|
|
440
|
+
this._windowStorageInitialized = true;
|
|
441
|
+
if (storageResolved) this._windowStorage = preparedStorage;
|
|
442
|
+
else try {
|
|
443
|
+
this._windowStorage = this._windowStorageFactory?.();
|
|
444
|
+
} catch {
|
|
445
|
+
this._windowStorage = void 0;
|
|
446
|
+
}
|
|
447
|
+
const lastWindowId = this._windowRead(this._windowKey);
|
|
448
|
+
const primaryWindowExists = this._windowRead(this._primaryWindowKey);
|
|
449
|
+
if (lastWindowId && (!primaryWindowExists || this._isReload())) this._windowId = lastWindowId;
|
|
450
|
+
else if (primaryWindowExists) this._windowRemove(this._windowKey);
|
|
451
|
+
if (!this._windowStorage) return;
|
|
452
|
+
try {
|
|
453
|
+
globalThis.addEventListener('beforeunload', this._beforeUnload);
|
|
454
|
+
this._windowListenerInstalled = true;
|
|
455
|
+
} catch {}
|
|
456
|
+
try {
|
|
457
|
+
globalThis.addEventListener('unload', this._beforeUnload);
|
|
458
|
+
this._windowListenerInstalled = true;
|
|
459
|
+
} catch {}
|
|
460
|
+
if (this._windowListenerInstalled) this._windowSet(this._primaryWindowKey, '1');
|
|
461
|
+
else {
|
|
462
|
+
this._windowRemove(this._windowKey);
|
|
463
|
+
this._windowRemove(this._primaryWindowKey);
|
|
464
|
+
this._windowStorage = void 0;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
_isReload() {
|
|
468
|
+
try {
|
|
469
|
+
return globalThis.performance?.getEntriesByType('navigation')[0]?.type === 'reload';
|
|
470
|
+
} catch {
|
|
471
|
+
return false;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
_setWindowId(windowId) {
|
|
475
|
+
this._windowId = windowId;
|
|
476
|
+
this._windowSet(this._windowKey, windowId);
|
|
477
|
+
}
|
|
478
|
+
_clearWindow(removeWindow) {
|
|
479
|
+
this._removeWindowListener();
|
|
480
|
+
if (removeWindow) this._windowRemove(this._windowKey);
|
|
481
|
+
this._windowRemove(this._primaryWindowKey);
|
|
482
|
+
this._windowStorage = void 0;
|
|
483
|
+
this._windowStorageInitialized = false;
|
|
484
|
+
this._windowId = '';
|
|
485
|
+
}
|
|
486
|
+
_removeWindowListener() {
|
|
487
|
+
if (!this._windowListenerInstalled) return;
|
|
488
|
+
for (const event of [
|
|
489
|
+
'beforeunload',
|
|
490
|
+
'unload'
|
|
491
|
+
])try {
|
|
492
|
+
globalThis.removeEventListener(event, this._beforeUnload);
|
|
493
|
+
} catch {}
|
|
494
|
+
this._windowListenerInstalled = false;
|
|
495
|
+
}
|
|
496
|
+
_flushSession() {
|
|
497
|
+
const session = this._state.session;
|
|
498
|
+
if (!session || session.lastActivityTimestamp === this._lastActivityWriteTimestamp) return;
|
|
499
|
+
const [sharedRead, sharedState] = this._readPersistedState();
|
|
500
|
+
const shared = sharedState?.session;
|
|
501
|
+
if (sharedRead && (!shared || shared.revision !== session.revision || shared.sessionId !== session.sessionId || shared.sessionStartTimestamp !== session.sessionStartTimestamp || shared.lastActivityTimestamp > session.lastActivityTimestamp)) return;
|
|
502
|
+
this._lastActivityWriteTimestamp = session.lastActivityTimestamp;
|
|
503
|
+
this._save(false);
|
|
504
|
+
}
|
|
505
|
+
_windowRead(key) {
|
|
506
|
+
try {
|
|
507
|
+
return this._windowStorage?.getItem(key) ?? null;
|
|
508
|
+
} catch {
|
|
509
|
+
return null;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
_windowSet(key, value) {
|
|
513
|
+
try {
|
|
514
|
+
this._windowStorage?.setItem(key, value);
|
|
515
|
+
} catch {}
|
|
516
|
+
}
|
|
517
|
+
_windowRemove(key) {
|
|
518
|
+
try {
|
|
519
|
+
this._windowStorage?.removeItem(key);
|
|
520
|
+
} catch {}
|
|
521
|
+
}
|
|
522
|
+
_applyConsent(consent) {
|
|
523
|
+
if (consent === this._consent) return;
|
|
524
|
+
const previousConsent = this._consent;
|
|
525
|
+
this._consent = consent;
|
|
526
|
+
try {
|
|
527
|
+
this._onConsentChange(consent, previousConsent);
|
|
528
|
+
} catch {}
|
|
529
|
+
}
|
|
530
|
+
_save(preserveSession = true) {
|
|
531
|
+
if (!this.prepare()) return;
|
|
532
|
+
let state = this._state;
|
|
533
|
+
if (preserveSession) {
|
|
534
|
+
const [read, persisted] = this._readPersistedState();
|
|
535
|
+
if (read && persisted && persistedAuthorityWins(persisted, state)) {
|
|
536
|
+
state = {
|
|
537
|
+
...state
|
|
538
|
+
};
|
|
539
|
+
copyAuthority(state, persisted);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
try {
|
|
543
|
+
this._storage?.setItem(this._stateKey, JSON.stringify(state));
|
|
544
|
+
} catch {}
|
|
545
|
+
}
|
|
546
|
+
_writeConsent(value) {
|
|
547
|
+
if (!this._storage) {
|
|
548
|
+
this._ignoredConsentValue = this._lastConsentValue;
|
|
549
|
+
this._hasIgnoredConsentValue = true;
|
|
550
|
+
return false;
|
|
551
|
+
}
|
|
552
|
+
try {
|
|
553
|
+
this._storage.setItem(this.consentKey, value);
|
|
554
|
+
this._lastConsentValue = value;
|
|
555
|
+
this._hasIgnoredConsentValue = false;
|
|
556
|
+
return true;
|
|
557
|
+
} catch {
|
|
558
|
+
this._ignoredConsentValue = this._lastConsentValue;
|
|
559
|
+
this._hasIgnoredConsentValue = true;
|
|
560
|
+
return false;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
_read(key) {
|
|
564
|
+
if (!this._storage) return [
|
|
565
|
+
true,
|
|
566
|
+
null
|
|
567
|
+
];
|
|
568
|
+
try {
|
|
569
|
+
return [
|
|
570
|
+
true,
|
|
571
|
+
this._storage.getItem(key)
|
|
572
|
+
];
|
|
573
|
+
} catch {
|
|
574
|
+
return [
|
|
575
|
+
false,
|
|
576
|
+
null
|
|
577
|
+
];
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
export { BrowserState, getDefaultSessionStorage, getDefaultStorage };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type SurveysManager } from '@posthog/browser-common/surveys-config';
|
|
2
|
+
import type { SurveyRenderContext } from '@posthog/browser-common/survey-render-context';
|
|
3
|
+
import type { SurveysExtension } from './surveys-internal';
|
|
4
|
+
import { type SurveysOptions } from './surveys-options';
|
|
5
|
+
type Renderer = {
|
|
6
|
+
generateSurveys(host: SurveyRenderContext, enabled: boolean): SurveysManager | undefined;
|
|
7
|
+
};
|
|
8
|
+
export declare const createSurveys: (options: SurveysOptions, load: () => Promise<Renderer>) => SurveysExtension;
|
|
9
|
+
export {};
|