@posthog/browser 0.1.1 → 0.3.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 +28 -22
- package/README.md +94 -29
- package/dist/analytics-api.d.ts +32 -0
- package/dist/analytics-api.js +18 -0
- package/dist/analytics-api.mjs +1 -0
- package/dist/analytics-buffer.d.ts +3 -3
- package/dist/analytics-buffer.js +2 -161
- package/dist/analytics-buffer.mjs +2 -161
- package/dist/analytics-extension.d.ts +87 -0
- package/dist/analytics-extension.js +649 -0
- package/dist/analytics-extension.mjs +611 -0
- package/dist/analytics-internal.d.ts +33 -18
- package/dist/analytics-options.d.ts +7 -2
- package/dist/analytics-options.js +38 -1
- package/dist/analytics-options.mjs +17 -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/analytics.d.ts +4 -3
- package/dist/autocapture.d.ts +2 -2
- package/dist/autocapture.js +21 -10
- package/dist/autocapture.mjs +21 -10
- package/dist/automatic-analytics.d.ts +2 -2
- package/dist/browser-client.d.ts +44 -4
- package/dist/capture-properties.d.ts +15 -0
- package/dist/capture-properties.js +101 -0
- package/dist/capture-properties.mjs +57 -0
- package/dist/capture-v1.js +7 -19
- package/dist/capture-v1.mjs +1 -13
- package/dist/core.d.ts +7 -5
- package/dist/extensions/registry.d.ts +42 -7
- package/dist/extensions/registry.js +194 -37
- package/dist/extensions/registry.mjs +194 -37
- package/dist/flags-token.d.ts +4 -4
- package/dist/flags.js +15 -2
- package/dist/flags.mjs +15 -2
- package/dist/immutable.d.ts +1 -0
- package/dist/immutable.js +46 -0
- package/dist/immutable.mjs +8 -0
- package/dist/index.d.ts +7 -5
- package/dist/index.js +3 -67
- package/dist/index.mjs +3 -67
- 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/logs-internal.d.ts +2 -2
- package/dist/logs.js +5 -2
- package/dist/logs.mjs +5 -2
- package/dist/person-processing.d.ts +11 -0
- package/dist/person-processing.js +84 -0
- package/dist/person-processing.mjs +37 -0
- package/dist/posthog.d.ts +4 -3
- package/dist/posthog.js +154 -353
- package/dist/posthog.mjs +154 -353
- package/dist/root-composition.d.ts +11 -0
- package/dist/root-composition.js +138 -0
- package/dist/root-composition.mjs +100 -0
- package/dist/state.d.ts +6 -0
- package/dist/state.js +50 -56
- package/dist/state.mjs +50 -56
- package/dist/surveys-extension.js +11 -6
- package/dist/surveys-extension.mjs +11 -6
- package/dist/surveys-internal.d.ts +3 -2
- package/dist/types.d.ts +13 -19
- package/dist/utf8.d.ts +1 -0
- package/dist/utf8.js +52 -0
- package/dist/utf8.mjs +14 -0
- package/dist/version.js +1 -1
- package/dist/version.mjs +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,138 @@
|
|
|
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
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
31
|
+
createProductComposition: ()=>createProductComposition
|
|
32
|
+
});
|
|
33
|
+
const constants_namespaceObject = require("@posthog/browser-common/constants");
|
|
34
|
+
const surveys_config_namespaceObject = require("@posthog/browser-common/surveys-config");
|
|
35
|
+
const external_autocapture_options_js_namespaceObject = require("./autocapture-options.js");
|
|
36
|
+
const external_surveys_options_js_namespaceObject = require("./surveys-options.js");
|
|
37
|
+
const external_logs_options_js_namespaceObject = require("./logs-options.js");
|
|
38
|
+
const external_analytics_options_js_namespaceObject = require("./analytics-options.js");
|
|
39
|
+
const activateFromSignals = (hasSaved, enabled)=>(host, acquire)=>{
|
|
40
|
+
let loading;
|
|
41
|
+
host.onRemoteConfig((result)=>{
|
|
42
|
+
if (result.ok && enabled(result.config)) loading = acquire();
|
|
43
|
+
});
|
|
44
|
+
return hasSaved(host) ? acquire() : loading;
|
|
45
|
+
};
|
|
46
|
+
const registerProduct = (composition, name, label, configure)=>{
|
|
47
|
+
if (composition.extensions.some((extension)=>extension.name === name)) return;
|
|
48
|
+
try {
|
|
49
|
+
const definition = configure();
|
|
50
|
+
if (definition) {
|
|
51
|
+
composition.loaders[name] = definition.load;
|
|
52
|
+
composition.automatic.push({
|
|
53
|
+
name,
|
|
54
|
+
activate: definition.activate ?? ((_host, acquire)=>acquire())
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
} catch (error) {
|
|
58
|
+
composition.loadingErrors.push([
|
|
59
|
+
label,
|
|
60
|
+
error
|
|
61
|
+
]);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
const configureAnalytics = (options, composition)=>{
|
|
65
|
+
let configuration;
|
|
66
|
+
if (composition.extensions.some((extension)=>'analytics' === extension.name)) configuration = false;
|
|
67
|
+
else try {
|
|
68
|
+
configuration = options?.analytics;
|
|
69
|
+
} catch {}
|
|
70
|
+
const snapshot = (0, external_analytics_options_js_namespaceObject.snapshotAutomaticAnalyticsOptions)(configuration || {});
|
|
71
|
+
if (false !== configuration) composition.loaders.analytics = ()=>import("./automatic-analytics.js").then(({ analytics })=>analytics(snapshot));
|
|
72
|
+
return snapshot;
|
|
73
|
+
};
|
|
74
|
+
const configureFlags = (options)=>{
|
|
75
|
+
const configuration = options?.flags;
|
|
76
|
+
if (false === configuration) return;
|
|
77
|
+
const snapshot = configuration && JSON.parse(JSON.stringify(configuration));
|
|
78
|
+
return {
|
|
79
|
+
load: ()=>import("./flags.js").then(({ flags })=>flags(snapshot))
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
const configureLogs = (options)=>{
|
|
83
|
+
const configuration = options?.logs;
|
|
84
|
+
if (false === configuration) return;
|
|
85
|
+
const snapshot = (0, external_logs_options_js_namespaceObject.snapshotLogsOptions)(configuration);
|
|
86
|
+
return {
|
|
87
|
+
load: ()=>import("./logs.js").then(({ logs })=>logs(snapshot)),
|
|
88
|
+
activate: void 0 === configuration ? activateFromSignals((host)=>true === host.kv.get('consoleCaptureEnabled'), (config)=>config.logs?.captureConsoleLogs === true) : void 0
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
const configureSurveys = (options)=>{
|
|
92
|
+
const configuration = options?.surveys;
|
|
93
|
+
if (false === configuration) return;
|
|
94
|
+
const snapshot = (0, external_surveys_options_js_namespaceObject.snapshotSurveysOptions)(configuration);
|
|
95
|
+
return {
|
|
96
|
+
load: ()=>import("./automatic-surveys.js").then(({ surveys })=>surveys(snapshot)),
|
|
97
|
+
activate: void 0 === configuration ? activateFromSignals((host)=>{
|
|
98
|
+
const cached = host.kv.get(surveys_config_namespaceObject.SURVEYS);
|
|
99
|
+
return Array.isArray(cached) && cached.length > 0;
|
|
100
|
+
}, (config)=>true === config.surveys || Array.isArray(config.surveys) && config.surveys.length > 0) : void 0
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
const configureAutocapture = (options)=>{
|
|
104
|
+
const configuration = options?.autocapture;
|
|
105
|
+
if (false === configuration) return;
|
|
106
|
+
const snapshot = (0, external_autocapture_options_js_namespaceObject.snapshotAutocaptureOptions)(configuration);
|
|
107
|
+
return {
|
|
108
|
+
load: ()=>import("./autocapture.js").then(({ autocapture })=>autocapture(snapshot)),
|
|
109
|
+
activate: void 0 === configuration ? activateFromSignals((host)=>false === host.kv.get(constants_namespaceObject.AUTOCAPTURE_DISABLED_SERVER_SIDE), (config)=>false === config.autocapture_opt_out) : void 0
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
const createProductComposition = (options)=>{
|
|
113
|
+
const composition = {
|
|
114
|
+
extensions: [
|
|
115
|
+
...options?.extensions ?? []
|
|
116
|
+
],
|
|
117
|
+
loaders: {},
|
|
118
|
+
automatic: [],
|
|
119
|
+
loadingErrors: []
|
|
120
|
+
};
|
|
121
|
+
const analyticsOptions = configureAnalytics(options, composition);
|
|
122
|
+
if ('eager' === analyticsOptions.load) composition.automatic.push({
|
|
123
|
+
name: 'analytics',
|
|
124
|
+
activate: (_host, acquire)=>acquire()
|
|
125
|
+
});
|
|
126
|
+
registerProduct(composition, 'featureFlags', 'flags', ()=>configureFlags(options));
|
|
127
|
+
registerProduct(composition, 'logs', 'logs', ()=>configureLogs(options));
|
|
128
|
+
registerProduct(composition, 'surveys', 'surveys', ()=>configureSurveys(options));
|
|
129
|
+
registerProduct(composition, 'autocapture', 'autocapture', ()=>configureAutocapture(options));
|
|
130
|
+
return composition;
|
|
131
|
+
};
|
|
132
|
+
exports.createProductComposition = __webpack_exports__.createProductComposition;
|
|
133
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
134
|
+
"createProductComposition"
|
|
135
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
136
|
+
Object.defineProperty(exports, '__esModule', {
|
|
137
|
+
value: true
|
|
138
|
+
});
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { AUTOCAPTURE_DISABLED_SERVER_SIDE } from "@posthog/browser-common/constants";
|
|
2
|
+
import { SURVEYS } from "@posthog/browser-common/surveys-config";
|
|
3
|
+
import { snapshotAutocaptureOptions } from "./autocapture-options.mjs";
|
|
4
|
+
import { snapshotSurveysOptions } from "./surveys-options.mjs";
|
|
5
|
+
import { snapshotLogsOptions } from "./logs-options.mjs";
|
|
6
|
+
import { snapshotAutomaticAnalyticsOptions } from "./analytics-options.mjs";
|
|
7
|
+
const activateFromSignals = (hasSaved, enabled)=>(host, acquire)=>{
|
|
8
|
+
let loading;
|
|
9
|
+
host.onRemoteConfig((result)=>{
|
|
10
|
+
if (result.ok && enabled(result.config)) loading = acquire();
|
|
11
|
+
});
|
|
12
|
+
return hasSaved(host) ? acquire() : loading;
|
|
13
|
+
};
|
|
14
|
+
const registerProduct = (composition, name, label, configure)=>{
|
|
15
|
+
if (composition.extensions.some((extension)=>extension.name === name)) return;
|
|
16
|
+
try {
|
|
17
|
+
const definition = configure();
|
|
18
|
+
if (definition) {
|
|
19
|
+
composition.loaders[name] = definition.load;
|
|
20
|
+
composition.automatic.push({
|
|
21
|
+
name,
|
|
22
|
+
activate: definition.activate ?? ((_host, acquire)=>acquire())
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
} catch (error) {
|
|
26
|
+
composition.loadingErrors.push([
|
|
27
|
+
label,
|
|
28
|
+
error
|
|
29
|
+
]);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
const configureAnalytics = (options, composition)=>{
|
|
33
|
+
let configuration;
|
|
34
|
+
if (composition.extensions.some((extension)=>'analytics' === extension.name)) configuration = false;
|
|
35
|
+
else try {
|
|
36
|
+
configuration = options?.analytics;
|
|
37
|
+
} catch {}
|
|
38
|
+
const snapshot = snapshotAutomaticAnalyticsOptions(configuration || {});
|
|
39
|
+
if (false !== configuration) composition.loaders.analytics = ()=>import("./automatic-analytics.mjs").then(({ analytics })=>analytics(snapshot));
|
|
40
|
+
return snapshot;
|
|
41
|
+
};
|
|
42
|
+
const configureFlags = (options)=>{
|
|
43
|
+
const configuration = options?.flags;
|
|
44
|
+
if (false === configuration) return;
|
|
45
|
+
const snapshot = configuration && JSON.parse(JSON.stringify(configuration));
|
|
46
|
+
return {
|
|
47
|
+
load: ()=>import("./flags.mjs").then(({ flags })=>flags(snapshot))
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
const configureLogs = (options)=>{
|
|
51
|
+
const configuration = options?.logs;
|
|
52
|
+
if (false === configuration) return;
|
|
53
|
+
const snapshot = snapshotLogsOptions(configuration);
|
|
54
|
+
return {
|
|
55
|
+
load: ()=>import("./logs.mjs").then(({ logs })=>logs(snapshot)),
|
|
56
|
+
activate: void 0 === configuration ? activateFromSignals((host)=>true === host.kv.get('consoleCaptureEnabled'), (config)=>config.logs?.captureConsoleLogs === true) : void 0
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
const configureSurveys = (options)=>{
|
|
60
|
+
const configuration = options?.surveys;
|
|
61
|
+
if (false === configuration) return;
|
|
62
|
+
const snapshot = snapshotSurveysOptions(configuration);
|
|
63
|
+
return {
|
|
64
|
+
load: ()=>import("./automatic-surveys.mjs").then(({ surveys })=>surveys(snapshot)),
|
|
65
|
+
activate: void 0 === configuration ? activateFromSignals((host)=>{
|
|
66
|
+
const cached = host.kv.get(SURVEYS);
|
|
67
|
+
return Array.isArray(cached) && cached.length > 0;
|
|
68
|
+
}, (config)=>true === config.surveys || Array.isArray(config.surveys) && config.surveys.length > 0) : void 0
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
const configureAutocapture = (options)=>{
|
|
72
|
+
const configuration = options?.autocapture;
|
|
73
|
+
if (false === configuration) return;
|
|
74
|
+
const snapshot = snapshotAutocaptureOptions(configuration);
|
|
75
|
+
return {
|
|
76
|
+
load: ()=>import("./autocapture.mjs").then(({ autocapture })=>autocapture(snapshot)),
|
|
77
|
+
activate: void 0 === configuration ? activateFromSignals((host)=>false === host.kv.get(AUTOCAPTURE_DISABLED_SERVER_SIDE), (config)=>false === config.autocapture_opt_out) : void 0
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
const createProductComposition = (options)=>{
|
|
81
|
+
const composition = {
|
|
82
|
+
extensions: [
|
|
83
|
+
...options?.extensions ?? []
|
|
84
|
+
],
|
|
85
|
+
loaders: {},
|
|
86
|
+
automatic: [],
|
|
87
|
+
loadingErrors: []
|
|
88
|
+
};
|
|
89
|
+
const analyticsOptions = configureAnalytics(options, composition);
|
|
90
|
+
if ('eager' === analyticsOptions.load) composition.automatic.push({
|
|
91
|
+
name: 'analytics',
|
|
92
|
+
activate: (_host, acquire)=>acquire()
|
|
93
|
+
});
|
|
94
|
+
registerProduct(composition, 'featureFlags', 'flags', ()=>configureFlags(options));
|
|
95
|
+
registerProduct(composition, 'logs', 'logs', ()=>configureLogs(options));
|
|
96
|
+
registerProduct(composition, 'surveys', 'surveys', ()=>configureSurveys(options));
|
|
97
|
+
registerProduct(composition, 'autocapture', 'autocapture', ()=>configureAutocapture(options));
|
|
98
|
+
return composition;
|
|
99
|
+
};
|
|
100
|
+
export { createProductComposition };
|
package/dist/state.d.ts
CHANGED
|
@@ -50,12 +50,15 @@ export declare class BrowserState {
|
|
|
50
50
|
get distinctId(): string;
|
|
51
51
|
get isIdentified(): boolean;
|
|
52
52
|
get groups(): Record<string, string>;
|
|
53
|
+
get sessionId(): string | undefined;
|
|
54
|
+
get activeSessionId(): string | undefined;
|
|
53
55
|
get session(): SessionContext | undefined;
|
|
54
56
|
get consent(): ConsentState;
|
|
55
57
|
refreshConsent(): ConsentState;
|
|
56
58
|
observeConsent(value: unknown): ConsentState;
|
|
57
59
|
identify(distinctId: string): void;
|
|
58
60
|
group(type: string, key: string): boolean;
|
|
61
|
+
resetGroups(): void;
|
|
59
62
|
prepareSessionForEvent(now?: number): PreparedSession;
|
|
60
63
|
sessionAdmitted(prepared: PreparedSession): boolean;
|
|
61
64
|
reset(): void;
|
|
@@ -63,7 +66,10 @@ export declare class BrowserState {
|
|
|
63
66
|
optIn(): boolean;
|
|
64
67
|
optOut(): boolean;
|
|
65
68
|
keyValueStore(namespace: string, canAccess?: () => boolean): KeyValueStore;
|
|
69
|
+
tabKeyValueStore(namespace: string, canAccess: () => boolean): KeyValueStore;
|
|
70
|
+
private _tabStorage;
|
|
66
71
|
private _readPersistedState;
|
|
72
|
+
private _readSessionAuthority;
|
|
67
73
|
private _prepareWindow;
|
|
68
74
|
private _initializeWindowStorage;
|
|
69
75
|
private _isReload;
|
package/dist/state.js
CHANGED
|
@@ -33,12 +33,8 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
33
33
|
getDefaultStorage: ()=>getDefaultStorage
|
|
34
34
|
});
|
|
35
35
|
const external_id_js_namespaceObject = require("./id.js");
|
|
36
|
+
const external_key_value_store_js_namespaceObject = require("./key-value-store.js");
|
|
36
37
|
const emptyRecord = ()=>Object.create(null);
|
|
37
|
-
const cloneJson = (value)=>{
|
|
38
|
-
const json = JSON.stringify(value);
|
|
39
|
-
if (void 0 === json) throw new TypeError('The value must be JSON-serializable');
|
|
40
|
-
return JSON.parse(json);
|
|
41
|
-
};
|
|
42
38
|
const isRecord = (value)=>'object' == typeof value && null !== value && !Array.isArray(value);
|
|
43
39
|
const readGroups = (value)=>{
|
|
44
40
|
const groups = emptyRecord();
|
|
@@ -48,6 +44,12 @@ const readGroups = (value)=>{
|
|
|
48
44
|
return groups;
|
|
49
45
|
};
|
|
50
46
|
const isTimestamp = (value)=>'number' == typeof value && Number.isFinite(value) && value >= 0;
|
|
47
|
+
const sessionExpiryReason = (session, now)=>{
|
|
48
|
+
if (!session) return;
|
|
49
|
+
const safeNow = isTimestamp(now) ? now : 0;
|
|
50
|
+
if (Math.abs(safeNow - session.lastActivityTimestamp) > 1800000) return 'idleTimeout';
|
|
51
|
+
if (Math.abs(safeNow - session.sessionStartTimestamp) > 86400000) return 'maxLength';
|
|
52
|
+
};
|
|
51
53
|
const readRevision = (value, fallback)=>{
|
|
52
54
|
if ('string' == typeof value && /^(0|[1-9]\d*)$/.test(value)) return value;
|
|
53
55
|
const number = void 0 === value ? fallback : value;
|
|
@@ -247,6 +249,14 @@ class BrowserState {
|
|
|
247
249
|
...this._state.groups
|
|
248
250
|
};
|
|
249
251
|
}
|
|
252
|
+
get sessionId() {
|
|
253
|
+
return this._state.session?.sessionId;
|
|
254
|
+
}
|
|
255
|
+
get activeSessionId() {
|
|
256
|
+
if (this._pendingSessionReason) return;
|
|
257
|
+
const { session } = this._readSessionAuthority();
|
|
258
|
+
return sessionExpiryReason(session, Date.now()) ? void 0 : session?.sessionId;
|
|
259
|
+
}
|
|
250
260
|
get session() {
|
|
251
261
|
const session = this._state.session;
|
|
252
262
|
if (!session || !this._windowId) return;
|
|
@@ -282,24 +292,17 @@ class BrowserState {
|
|
|
282
292
|
this._save();
|
|
283
293
|
return true;
|
|
284
294
|
}
|
|
295
|
+
resetGroups() {
|
|
296
|
+
this._state.groups = emptyRecord();
|
|
297
|
+
this._save();
|
|
298
|
+
}
|
|
285
299
|
prepareSessionForEvent(now = Date.now()) {
|
|
286
300
|
const safeNow = isTimestamp(now) ? now : 0;
|
|
287
|
-
const
|
|
288
|
-
let session = this._state.session;
|
|
289
|
-
let lastActivityWriteTimestamp = this._lastActivityWriteTimestamp;
|
|
290
|
-
let adoptedAfterReset = false;
|
|
291
|
-
if (sharedRead && external && persistedAuthorityWins(external, this._state)) {
|
|
292
|
-
session = external.session;
|
|
293
|
-
if (session) {
|
|
294
|
-
lastActivityWriteTimestamp = session.lastActivityTimestamp;
|
|
295
|
-
adoptedAfterReset = 'reset' === this._pendingSessionReason;
|
|
296
|
-
}
|
|
297
|
-
}
|
|
301
|
+
const { external, session, lastActivityWriteTimestamp, adoptedAfterReset } = this._readSessionAuthority();
|
|
298
302
|
const pendingReason = this._pendingSessionReason;
|
|
299
|
-
const
|
|
300
|
-
const
|
|
301
|
-
const
|
|
302
|
-
const reason = adoptedAfterReset ? 'reset' : pendingReason ?? (idle ? 'idleTimeout' : maximum ? 'maxLength' : void 0);
|
|
303
|
+
const expiry = sessionExpiryReason(session, safeNow);
|
|
304
|
+
const rotated = !!(pendingReason || !session || expiry);
|
|
305
|
+
const reason = adoptedAfterReset ? 'reset' : pendingReason ?? expiry;
|
|
303
306
|
const preparedSession = rotated ? createSession(safeNow, nextRevision(safeNow, this._state, external)) : {
|
|
304
307
|
...session,
|
|
305
308
|
lastActivityTimestamp: Math.max(safeNow, session.lastActivityTimestamp)
|
|
@@ -383,42 +386,22 @@ class BrowserState {
|
|
|
383
386
|
return this._writeConsent('0');
|
|
384
387
|
}
|
|
385
388
|
keyValueStore(namespace, canAccess = ()=>true) {
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
},
|
|
403
|
-
set: (keyOrValues, value)=>{
|
|
404
|
-
if (!canAccess()) return;
|
|
405
|
-
const namespaceValues = values();
|
|
406
|
-
const entries = 'string' == typeof keyOrValues ? {
|
|
407
|
-
[keyOrValues]: value
|
|
408
|
-
} : keyOrValues;
|
|
409
|
-
for (const [key, entry] of Object.entries(entries))if (void 0 === entry) delete namespaceValues[key];
|
|
410
|
-
else namespaceValues[key] = cloneJson(entry);
|
|
411
|
-
this._save();
|
|
412
|
-
},
|
|
413
|
-
remove: (keyOrKeys)=>{
|
|
414
|
-
if (!canAccess()) return;
|
|
415
|
-
const namespaceValues = values();
|
|
416
|
-
for (const key of 'string' == typeof keyOrKeys ? [
|
|
417
|
-
keyOrKeys
|
|
418
|
-
] : keyOrKeys)delete namespaceValues[key];
|
|
419
|
-
this._save();
|
|
420
|
-
}
|
|
421
|
-
};
|
|
389
|
+
return (0, external_key_value_store_js_namespaceObject.createKeyValueStore)({
|
|
390
|
+
values: ()=>this._state.extensionData[namespace] ??= emptyRecord(),
|
|
391
|
+
save: ()=>this._save(),
|
|
392
|
+
canAccess
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
tabKeyValueStore(namespace, canAccess) {
|
|
396
|
+
const values = new external_key_value_store_js_namespaceObject.TabValues(`${this._stateKey}_tab_${namespace}`, ()=>this._tabStorage());
|
|
397
|
+
return (0, external_key_value_store_js_namespaceObject.createKeyValueStore)({
|
|
398
|
+
values: ()=>values.values(),
|
|
399
|
+
save: ()=>values.save(),
|
|
400
|
+
canAccess
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
_tabStorage() {
|
|
404
|
+
return this._windowStorage ?? this._windowStorageFactory?.();
|
|
422
405
|
}
|
|
423
406
|
_readPersistedState() {
|
|
424
407
|
if (!this._storage) return [
|
|
@@ -442,6 +425,17 @@ class BrowserState {
|
|
|
442
425
|
false
|
|
443
426
|
];
|
|
444
427
|
}
|
|
428
|
+
_readSessionAuthority() {
|
|
429
|
+
const [read, external] = this._readPersistedState();
|
|
430
|
+
const adopted = read && void 0 !== external && persistedAuthorityWins(external, this._state);
|
|
431
|
+
const session = adopted ? external.session : this._state.session;
|
|
432
|
+
return {
|
|
433
|
+
external,
|
|
434
|
+
session,
|
|
435
|
+
lastActivityWriteTimestamp: adopted && session ? session.lastActivityTimestamp : this._lastActivityWriteTimestamp,
|
|
436
|
+
adoptedAfterReset: adopted && void 0 !== session && 'reset' === this._pendingSessionReason
|
|
437
|
+
};
|
|
438
|
+
}
|
|
445
439
|
_prepareWindow(rotated) {
|
|
446
440
|
if (rotated || this._windowId) return {
|
|
447
441
|
windowId: rotated ? (0, external_id_js_namespaceObject.createId)() : this._windowId,
|
package/dist/state.mjs
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import { createId } from "./id.mjs";
|
|
2
|
+
import { TabValues, createKeyValueStore } from "./key-value-store.mjs";
|
|
2
3
|
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
4
|
const isRecord = (value)=>'object' == typeof value && null !== value && !Array.isArray(value);
|
|
9
5
|
const readGroups = (value)=>{
|
|
10
6
|
const groups = emptyRecord();
|
|
@@ -14,6 +10,12 @@ const readGroups = (value)=>{
|
|
|
14
10
|
return groups;
|
|
15
11
|
};
|
|
16
12
|
const isTimestamp = (value)=>'number' == typeof value && Number.isFinite(value) && value >= 0;
|
|
13
|
+
const sessionExpiryReason = (session, now)=>{
|
|
14
|
+
if (!session) return;
|
|
15
|
+
const safeNow = isTimestamp(now) ? now : 0;
|
|
16
|
+
if (Math.abs(safeNow - session.lastActivityTimestamp) > 1800000) return 'idleTimeout';
|
|
17
|
+
if (Math.abs(safeNow - session.sessionStartTimestamp) > 86400000) return 'maxLength';
|
|
18
|
+
};
|
|
17
19
|
const readRevision = (value, fallback)=>{
|
|
18
20
|
if ('string' == typeof value && /^(0|[1-9]\d*)$/.test(value)) return value;
|
|
19
21
|
const number = void 0 === value ? fallback : value;
|
|
@@ -213,6 +215,14 @@ class BrowserState {
|
|
|
213
215
|
...this._state.groups
|
|
214
216
|
};
|
|
215
217
|
}
|
|
218
|
+
get sessionId() {
|
|
219
|
+
return this._state.session?.sessionId;
|
|
220
|
+
}
|
|
221
|
+
get activeSessionId() {
|
|
222
|
+
if (this._pendingSessionReason) return;
|
|
223
|
+
const { session } = this._readSessionAuthority();
|
|
224
|
+
return sessionExpiryReason(session, Date.now()) ? void 0 : session?.sessionId;
|
|
225
|
+
}
|
|
216
226
|
get session() {
|
|
217
227
|
const session = this._state.session;
|
|
218
228
|
if (!session || !this._windowId) return;
|
|
@@ -248,24 +258,17 @@ class BrowserState {
|
|
|
248
258
|
this._save();
|
|
249
259
|
return true;
|
|
250
260
|
}
|
|
261
|
+
resetGroups() {
|
|
262
|
+
this._state.groups = emptyRecord();
|
|
263
|
+
this._save();
|
|
264
|
+
}
|
|
251
265
|
prepareSessionForEvent(now = Date.now()) {
|
|
252
266
|
const safeNow = isTimestamp(now) ? now : 0;
|
|
253
|
-
const
|
|
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
|
-
}
|
|
267
|
+
const { external, session, lastActivityWriteTimestamp, adoptedAfterReset } = this._readSessionAuthority();
|
|
264
268
|
const pendingReason = this._pendingSessionReason;
|
|
265
|
-
const
|
|
266
|
-
const
|
|
267
|
-
const
|
|
268
|
-
const reason = adoptedAfterReset ? 'reset' : pendingReason ?? (idle ? 'idleTimeout' : maximum ? 'maxLength' : void 0);
|
|
269
|
+
const expiry = sessionExpiryReason(session, safeNow);
|
|
270
|
+
const rotated = !!(pendingReason || !session || expiry);
|
|
271
|
+
const reason = adoptedAfterReset ? 'reset' : pendingReason ?? expiry;
|
|
269
272
|
const preparedSession = rotated ? createSession(safeNow, nextRevision(safeNow, this._state, external)) : {
|
|
270
273
|
...session,
|
|
271
274
|
lastActivityTimestamp: Math.max(safeNow, session.lastActivityTimestamp)
|
|
@@ -349,42 +352,22 @@ class BrowserState {
|
|
|
349
352
|
return this._writeConsent('0');
|
|
350
353
|
}
|
|
351
354
|
keyValueStore(namespace, canAccess = ()=>true) {
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
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
|
-
};
|
|
355
|
+
return createKeyValueStore({
|
|
356
|
+
values: ()=>this._state.extensionData[namespace] ??= emptyRecord(),
|
|
357
|
+
save: ()=>this._save(),
|
|
358
|
+
canAccess
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
tabKeyValueStore(namespace, canAccess) {
|
|
362
|
+
const values = new TabValues(`${this._stateKey}_tab_${namespace}`, ()=>this._tabStorage());
|
|
363
|
+
return createKeyValueStore({
|
|
364
|
+
values: ()=>values.values(),
|
|
365
|
+
save: ()=>values.save(),
|
|
366
|
+
canAccess
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
_tabStorage() {
|
|
370
|
+
return this._windowStorage ?? this._windowStorageFactory?.();
|
|
388
371
|
}
|
|
389
372
|
_readPersistedState() {
|
|
390
373
|
if (!this._storage) return [
|
|
@@ -408,6 +391,17 @@ class BrowserState {
|
|
|
408
391
|
false
|
|
409
392
|
];
|
|
410
393
|
}
|
|
394
|
+
_readSessionAuthority() {
|
|
395
|
+
const [read, external] = this._readPersistedState();
|
|
396
|
+
const adopted = read && void 0 !== external && persistedAuthorityWins(external, this._state);
|
|
397
|
+
const session = adopted ? external.session : this._state.session;
|
|
398
|
+
return {
|
|
399
|
+
external,
|
|
400
|
+
session,
|
|
401
|
+
lastActivityWriteTimestamp: adopted && session ? session.lastActivityTimestamp : this._lastActivityWriteTimestamp,
|
|
402
|
+
adoptedAfterReset: adopted && void 0 !== session && 'reset' === this._pendingSessionReason
|
|
403
|
+
};
|
|
404
|
+
}
|
|
411
405
|
_prepareWindow(rotated) {
|
|
412
406
|
if (rotated || this._windowId) return {
|
|
413
407
|
windowId: rotated ? createId() : this._windowId,
|
|
@@ -52,7 +52,7 @@ const createSurveys = (options, load)=>{
|
|
|
52
52
|
let loading;
|
|
53
53
|
let remoteSubscription;
|
|
54
54
|
let resetSubscription;
|
|
55
|
-
const getFlagsContext = ()=>client?.
|
|
55
|
+
const getFlagsContext = ()=>client?.featureFlags?.getSurveyContext?.();
|
|
56
56
|
const reset = ()=>{
|
|
57
57
|
shared.reset();
|
|
58
58
|
};
|
|
@@ -92,7 +92,7 @@ const createSurveys = (options, load)=>{
|
|
|
92
92
|
getActionUrl: ()=>runtimeHost ? (0, url_targeting_utils_namespaceObject.getTargetingUrl)(runtimeHost) : void 0,
|
|
93
93
|
setElementSelectors: (selectors)=>{
|
|
94
94
|
elementSelectors = new Set(selectors);
|
|
95
|
-
|
|
95
|
+
client?.autocapture?.setElementSelectors(new Set(elementSelectors));
|
|
96
96
|
}
|
|
97
97
|
}, onActivationChanged);
|
|
98
98
|
return eventReceiver;
|
|
@@ -147,12 +147,17 @@ const createSurveys = (options, load)=>{
|
|
|
147
147
|
};
|
|
148
148
|
return {
|
|
149
149
|
name: 'surveys',
|
|
150
|
+
bindings: {
|
|
151
|
+
surveys: shared
|
|
152
|
+
},
|
|
150
153
|
setup: async (value)=>{
|
|
151
154
|
client = value;
|
|
155
|
+
await value.resolveExtension('analytics');
|
|
156
|
+
if (disposed) return;
|
|
152
157
|
await value.kv.initialize();
|
|
153
158
|
if (disposed) return;
|
|
154
159
|
resetSubscription = value.onReset(reset);
|
|
155
|
-
const renderClient = Object.create(value);
|
|
160
|
+
const renderClient = Object.create(value.sharedClient);
|
|
156
161
|
const read = (key)=>key === constants_namespaceObject.STORED_PERSON_PROPERTIES_KEY ? getFlagsContext()?.personProperties : value.kv.get(key);
|
|
157
162
|
const renderKv = {
|
|
158
163
|
...value.kv,
|
|
@@ -178,9 +183,9 @@ const createSurveys = (options, load)=>{
|
|
|
178
183
|
if (remoteEnabled && !disposed) manager?.startAutomaticDisplay?.();
|
|
179
184
|
}
|
|
180
185
|
});
|
|
181
|
-
await shared.setup(value);
|
|
186
|
+
await shared.setup(value.sharedClient);
|
|
182
187
|
if (disposed) return void shared.dispose();
|
|
183
|
-
teardownSubscription = client.
|
|
188
|
+
teardownSubscription = client.analytics?.onBeforeTeardown?.(()=>shared.handlePageUnload());
|
|
184
189
|
if ("u" > typeof window) window.addEventListener('pagehide', pagehide);
|
|
185
190
|
},
|
|
186
191
|
getSurveys: (callback, forceReload)=>run(callback, (notify)=>shared.getSurveys(notify, forceReload)),
|
|
@@ -247,7 +252,7 @@ const createSurveys = (options, load)=>{
|
|
|
247
252
|
disposed = true;
|
|
248
253
|
elementSelectors.clear();
|
|
249
254
|
try {
|
|
250
|
-
client?.
|
|
255
|
+
client?.autocapture?.setElementSelectors(new Set());
|
|
251
256
|
} catch (error) {
|
|
252
257
|
client?.logger.error('Failed to clear survey action selectors', error);
|
|
253
258
|
}
|