@posthog/browser 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -1
- package/dist/analytics-api.d.ts +6 -0
- package/dist/analytics-extension.d.ts +12 -1
- package/dist/analytics-extension.js +72 -12
- package/dist/analytics-extension.mjs +73 -13
- package/dist/analytics-internal.d.ts +2 -1
- package/dist/analytics-properties.d.ts +12 -0
- package/dist/analytics-properties.js +88 -0
- package/dist/analytics-properties.mjs +46 -0
- package/dist/analytics-session-properties.d.ts +12 -0
- package/dist/analytics-session-properties.js +84 -0
- package/dist/analytics-session-properties.mjs +46 -0
- package/dist/capture-properties.d.ts +7 -1
- package/dist/capture-properties.js +37 -4
- package/dist/capture-properties.mjs +30 -3
- package/dist/key-value-store.d.ts +21 -0
- package/dist/key-value-store.js +117 -0
- package/dist/key-value-store.mjs +72 -0
- package/dist/posthog.js +1 -0
- package/dist/posthog.mjs +1 -0
- package/dist/state.d.ts +5 -0
- package/dist/state.js +46 -56
- package/dist/state.mjs +46 -56
- package/dist/version.js +1 -1
- package/dist/version.mjs +1 -1
- package/package.json +4 -4
|
@@ -0,0 +1,84 @@
|
|
|
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
|
+
SessionRegisteredProperties: ()=>SessionRegisteredProperties
|
|
32
|
+
});
|
|
33
|
+
const external_analytics_properties_js_namespaceObject = require("./analytics-properties.js");
|
|
34
|
+
class SessionRegisteredProperties {
|
|
35
|
+
constructor(_kv){
|
|
36
|
+
this._kv = _kv;
|
|
37
|
+
this._properties = new external_analytics_properties_js_namespaceObject.RegisteredProperties(_kv);
|
|
38
|
+
}
|
|
39
|
+
values(sessionId) {
|
|
40
|
+
const scope = this._kv.get('sessionId');
|
|
41
|
+
return void 0 === scope || scope === sessionId ? this._properties.values() : {};
|
|
42
|
+
}
|
|
43
|
+
register(properties, sessionId) {
|
|
44
|
+
this._kv.set({
|
|
45
|
+
superProperties: {
|
|
46
|
+
...this.values(sessionId),
|
|
47
|
+
...properties
|
|
48
|
+
},
|
|
49
|
+
sessionId
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
unregister(property, sessionId) {
|
|
53
|
+
const values = this.values(sessionId);
|
|
54
|
+
if (!Object.prototype.hasOwnProperty.call(values, property)) return;
|
|
55
|
+
delete values[property];
|
|
56
|
+
this._kv.set({
|
|
57
|
+
superProperties: values,
|
|
58
|
+
sessionId
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
admitted(sessionId) {
|
|
62
|
+
const scope = this._kv.get('sessionId');
|
|
63
|
+
if (scope === sessionId) return;
|
|
64
|
+
const properties = this._properties.values();
|
|
65
|
+
if (void 0 === scope && 0 === Object.keys(properties).length) return;
|
|
66
|
+
this._kv.set({
|
|
67
|
+
superProperties: this.values(sessionId),
|
|
68
|
+
sessionId
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
clear() {
|
|
72
|
+
this._kv.remove([
|
|
73
|
+
'superProperties',
|
|
74
|
+
'sessionId'
|
|
75
|
+
]);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
exports.SessionRegisteredProperties = __webpack_exports__.SessionRegisteredProperties;
|
|
79
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
80
|
+
"SessionRegisteredProperties"
|
|
81
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
82
|
+
Object.defineProperty(exports, '__esModule', {
|
|
83
|
+
value: true
|
|
84
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { RegisteredProperties } from "./analytics-properties.mjs";
|
|
2
|
+
class SessionRegisteredProperties {
|
|
3
|
+
constructor(_kv){
|
|
4
|
+
this._kv = _kv;
|
|
5
|
+
this._properties = new RegisteredProperties(_kv);
|
|
6
|
+
}
|
|
7
|
+
values(sessionId) {
|
|
8
|
+
const scope = this._kv.get('sessionId');
|
|
9
|
+
return void 0 === scope || scope === sessionId ? this._properties.values() : {};
|
|
10
|
+
}
|
|
11
|
+
register(properties, sessionId) {
|
|
12
|
+
this._kv.set({
|
|
13
|
+
superProperties: {
|
|
14
|
+
...this.values(sessionId),
|
|
15
|
+
...properties
|
|
16
|
+
},
|
|
17
|
+
sessionId
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
unregister(property, sessionId) {
|
|
21
|
+
const values = this.values(sessionId);
|
|
22
|
+
if (!Object.prototype.hasOwnProperty.call(values, property)) return;
|
|
23
|
+
delete values[property];
|
|
24
|
+
this._kv.set({
|
|
25
|
+
superProperties: values,
|
|
26
|
+
sessionId
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
admitted(sessionId) {
|
|
30
|
+
const scope = this._kv.get('sessionId');
|
|
31
|
+
if (scope === sessionId) return;
|
|
32
|
+
const properties = this._properties.values();
|
|
33
|
+
if (void 0 === scope && 0 === Object.keys(properties).length) return;
|
|
34
|
+
this._kv.set({
|
|
35
|
+
superProperties: this.values(sessionId),
|
|
36
|
+
sessionId
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
clear() {
|
|
40
|
+
this._kv.remove([
|
|
41
|
+
'superProperties',
|
|
42
|
+
'sessionId'
|
|
43
|
+
]);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export { SessionRegisteredProperties };
|
|
@@ -2,8 +2,14 @@ import type { CaptureOptions } from '@posthog/browser-common';
|
|
|
2
2
|
export interface CaptureProperties {
|
|
3
3
|
message: Record<string, unknown>;
|
|
4
4
|
observed: Record<string, unknown>;
|
|
5
|
+
/** Explicit caller and dynamic keys that session defaults cannot replace. */
|
|
6
|
+
overrides: ReadonlySet<string>;
|
|
5
7
|
uuid: string | undefined;
|
|
6
8
|
timestamp: Date | undefined;
|
|
7
9
|
}
|
|
8
10
|
/** Invoke caller serialization once, before selecting the authoritative session. */
|
|
9
|
-
export declare const snapshotCaptureProperties: (dynamic: Record<string, unknown>, properties: Record<string, unknown> | null, options: CaptureOptions) => CaptureProperties;
|
|
11
|
+
export declare const snapshotCaptureProperties: (registered: Record<string, unknown> | undefined, dynamic: Record<string, unknown>, properties: Record<string, unknown> | null, options: CaptureOptions) => CaptureProperties;
|
|
12
|
+
/** Session values override persistent defaults, not explicit caller or dynamic values. */
|
|
13
|
+
export declare const applySessionProperties: (snapshot: CaptureProperties, values: Record<string, unknown>) => boolean;
|
|
14
|
+
/** A session layer cannot shrink explicit caller values or protected SDK metadata. */
|
|
15
|
+
export declare const unavoidableProperties: (snapshot: CaptureProperties, metadata: Record<string, unknown>) => Record<string, unknown>;
|
|
@@ -35,7 +35,7 @@ const parseMessageProperties = (json)=>{
|
|
|
35
35
|
if (!isRecord(properties)) throw new Error('Event properties must serialize to an object');
|
|
36
36
|
return properties;
|
|
37
37
|
};
|
|
38
|
-
const snapshotCaptureProperties = (dynamic, properties, options)=>{
|
|
38
|
+
const snapshotCaptureProperties = (registered, dynamic, properties, options)=>{
|
|
39
39
|
const supplied = {
|
|
40
40
|
...dynamic,
|
|
41
41
|
...properties ?? {}
|
|
@@ -46,22 +46,55 @@ const snapshotCaptureProperties = (dynamic, properties, options)=>{
|
|
|
46
46
|
const timestamp = options.timestamp;
|
|
47
47
|
if (set) supplied['$set'] = set;
|
|
48
48
|
if (setOnce) supplied['$set_once'] = setOnce;
|
|
49
|
-
const
|
|
49
|
+
const combined = {
|
|
50
|
+
...registered,
|
|
51
|
+
...supplied
|
|
52
|
+
};
|
|
53
|
+
if ('function' == typeof combined.toJSON) delete combined.toJSON;
|
|
54
|
+
const overrides = new Set(Object.keys(supplied));
|
|
55
|
+
const json = JSON.stringify(combined);
|
|
50
56
|
const message = parseMessageProperties(json);
|
|
51
57
|
const observed = JSON.parse(json);
|
|
52
58
|
return {
|
|
53
59
|
message,
|
|
54
60
|
observed,
|
|
61
|
+
overrides,
|
|
55
62
|
uuid,
|
|
56
63
|
timestamp
|
|
57
64
|
};
|
|
58
65
|
};
|
|
66
|
+
const applySessionProperties = (snapshot, values)=>{
|
|
67
|
+
const entries = Object.entries(values).filter(([key])=>!snapshot.overrides.has(key));
|
|
68
|
+
if (0 === entries.length) return false;
|
|
69
|
+
const session = Object.fromEntries(entries);
|
|
70
|
+
const message = parseMessageProperties(JSON.stringify(session));
|
|
71
|
+
for (const [key] of entries)delete snapshot.message[key];
|
|
72
|
+
snapshot.message = {
|
|
73
|
+
...snapshot.message,
|
|
74
|
+
...message
|
|
75
|
+
};
|
|
76
|
+
snapshot.observed = {
|
|
77
|
+
...snapshot.observed,
|
|
78
|
+
...session
|
|
79
|
+
};
|
|
80
|
+
return true;
|
|
81
|
+
};
|
|
82
|
+
const unavoidableProperties = (snapshot, metadata)=>({
|
|
83
|
+
...Object.fromEntries(Object.entries(snapshot.message).filter(([key])=>snapshot.overrides.has(key))),
|
|
84
|
+
...metadata
|
|
85
|
+
});
|
|
59
86
|
__webpack_require__.d(__webpack_exports__, {}, {
|
|
60
|
-
|
|
87
|
+
applySessionProperties: applySessionProperties,
|
|
88
|
+
snapshotCaptureProperties: snapshotCaptureProperties,
|
|
89
|
+
unavoidableProperties: unavoidableProperties
|
|
61
90
|
});
|
|
91
|
+
exports.applySessionProperties = __webpack_exports__.applySessionProperties;
|
|
62
92
|
exports.snapshotCaptureProperties = __webpack_exports__.snapshotCaptureProperties;
|
|
93
|
+
exports.unavoidableProperties = __webpack_exports__.unavoidableProperties;
|
|
63
94
|
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
64
|
-
"
|
|
95
|
+
"applySessionProperties",
|
|
96
|
+
"snapshotCaptureProperties",
|
|
97
|
+
"unavoidableProperties"
|
|
65
98
|
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
66
99
|
Object.defineProperty(exports, '__esModule', {
|
|
67
100
|
value: true
|
|
@@ -6,7 +6,7 @@ const parseMessageProperties = (json)=>{
|
|
|
6
6
|
if (!isRecord(properties)) throw new Error('Event properties must serialize to an object');
|
|
7
7
|
return properties;
|
|
8
8
|
};
|
|
9
|
-
const snapshotCaptureProperties = (dynamic, properties, options)=>{
|
|
9
|
+
const snapshotCaptureProperties = (registered, dynamic, properties, options)=>{
|
|
10
10
|
const supplied = {
|
|
11
11
|
...dynamic,
|
|
12
12
|
...properties ?? {}
|
|
@@ -17,14 +17,41 @@ const snapshotCaptureProperties = (dynamic, properties, options)=>{
|
|
|
17
17
|
const timestamp = options.timestamp;
|
|
18
18
|
if (set) supplied['$set'] = set;
|
|
19
19
|
if (setOnce) supplied['$set_once'] = setOnce;
|
|
20
|
-
const
|
|
20
|
+
const combined = {
|
|
21
|
+
...registered,
|
|
22
|
+
...supplied
|
|
23
|
+
};
|
|
24
|
+
if ('function' == typeof combined.toJSON) delete combined.toJSON;
|
|
25
|
+
const overrides = new Set(Object.keys(supplied));
|
|
26
|
+
const json = JSON.stringify(combined);
|
|
21
27
|
const message = parseMessageProperties(json);
|
|
22
28
|
const observed = JSON.parse(json);
|
|
23
29
|
return {
|
|
24
30
|
message,
|
|
25
31
|
observed,
|
|
32
|
+
overrides,
|
|
26
33
|
uuid,
|
|
27
34
|
timestamp
|
|
28
35
|
};
|
|
29
36
|
};
|
|
30
|
-
|
|
37
|
+
const applySessionProperties = (snapshot, values)=>{
|
|
38
|
+
const entries = Object.entries(values).filter(([key])=>!snapshot.overrides.has(key));
|
|
39
|
+
if (0 === entries.length) return false;
|
|
40
|
+
const session = Object.fromEntries(entries);
|
|
41
|
+
const message = parseMessageProperties(JSON.stringify(session));
|
|
42
|
+
for (const [key] of entries)delete snapshot.message[key];
|
|
43
|
+
snapshot.message = {
|
|
44
|
+
...snapshot.message,
|
|
45
|
+
...message
|
|
46
|
+
};
|
|
47
|
+
snapshot.observed = {
|
|
48
|
+
...snapshot.observed,
|
|
49
|
+
...session
|
|
50
|
+
};
|
|
51
|
+
return true;
|
|
52
|
+
};
|
|
53
|
+
const unavoidableProperties = (snapshot, metadata)=>({
|
|
54
|
+
...Object.fromEntries(Object.entries(snapshot.message).filter(([key])=>snapshot.overrides.has(key))),
|
|
55
|
+
...metadata
|
|
56
|
+
});
|
|
57
|
+
export { applySessionProperties, snapshotCaptureProperties, unavoidableProperties };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { KeyValueStore } from '@posthog/browser-common';
|
|
2
|
+
import type { StorageLike } from './types';
|
|
3
|
+
export declare const cloneJson: <T>(value: T) => T;
|
|
4
|
+
interface StoreContext {
|
|
5
|
+
values(): Record<string, unknown>;
|
|
6
|
+
save(): void;
|
|
7
|
+
canAccess(): boolean;
|
|
8
|
+
}
|
|
9
|
+
/** Own, bound methods preserve the shared KV contract, including copied views. */
|
|
10
|
+
export declare const createKeyValueStore: (context: StoreContext) => KeyValueStore;
|
|
11
|
+
/** Core-owned per-tab buffer; failed native access falls back to this buffer. */
|
|
12
|
+
export declare class TabValues {
|
|
13
|
+
private readonly _key;
|
|
14
|
+
private readonly _storage;
|
|
15
|
+
private _values;
|
|
16
|
+
private _backend;
|
|
17
|
+
constructor(_key: string, _storage: () => StorageLike | undefined);
|
|
18
|
+
values(): Record<string, unknown>;
|
|
19
|
+
save(): void;
|
|
20
|
+
}
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, getters, values)=>{
|
|
5
|
+
var define = (defs, kind)=>{
|
|
6
|
+
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
[kind]: defs[key]
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
define(getters, "get");
|
|
12
|
+
define(values, "value");
|
|
13
|
+
};
|
|
14
|
+
})();
|
|
15
|
+
(()=>{
|
|
16
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
17
|
+
})();
|
|
18
|
+
(()=>{
|
|
19
|
+
__webpack_require__.r = (exports1)=>{
|
|
20
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
21
|
+
value: 'Module'
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
24
|
+
value: true
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
})();
|
|
28
|
+
var __webpack_exports__ = {};
|
|
29
|
+
__webpack_require__.r(__webpack_exports__);
|
|
30
|
+
const cloneJson = (value)=>{
|
|
31
|
+
const json = JSON.stringify(value);
|
|
32
|
+
if (void 0 === json) throw new TypeError('The value must be JSON-serializable');
|
|
33
|
+
return JSON.parse(json);
|
|
34
|
+
};
|
|
35
|
+
const createReader = (context)=>{
|
|
36
|
+
const read = (key)=>{
|
|
37
|
+
if (!context.canAccess()) return;
|
|
38
|
+
const value = context.values()[key];
|
|
39
|
+
return void 0 === value ? void 0 : cloneJson(value);
|
|
40
|
+
};
|
|
41
|
+
return (keyOrKeys)=>{
|
|
42
|
+
if ('string' == typeof keyOrKeys) return read(keyOrKeys);
|
|
43
|
+
const entries = Object.create(null);
|
|
44
|
+
for (const key of keyOrKeys){
|
|
45
|
+
const value = read(key);
|
|
46
|
+
if (void 0 !== value) entries[key] = value;
|
|
47
|
+
}
|
|
48
|
+
return entries;
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
const createWriter = (context)=>(keyOrValues, value)=>{
|
|
52
|
+
if (!context.canAccess()) return;
|
|
53
|
+
const values = context.values();
|
|
54
|
+
const entries = 'string' == typeof keyOrValues ? {
|
|
55
|
+
[keyOrValues]: value
|
|
56
|
+
} : keyOrValues;
|
|
57
|
+
for (const [key, entry] of Object.entries(entries))if (void 0 === entry) delete values[key];
|
|
58
|
+
else values[key] = cloneJson(entry);
|
|
59
|
+
context.save();
|
|
60
|
+
};
|
|
61
|
+
const createRemover = (context)=>(keyOrKeys)=>{
|
|
62
|
+
if (!context.canAccess()) return;
|
|
63
|
+
const values = context.values();
|
|
64
|
+
for (const key of 'string' == typeof keyOrKeys ? [
|
|
65
|
+
keyOrKeys
|
|
66
|
+
] : keyOrKeys)delete values[key];
|
|
67
|
+
context.save();
|
|
68
|
+
};
|
|
69
|
+
const createKeyValueStore = (context)=>({
|
|
70
|
+
initialize () {},
|
|
71
|
+
get: createReader(context),
|
|
72
|
+
set: createWriter(context),
|
|
73
|
+
remove: createRemover(context)
|
|
74
|
+
});
|
|
75
|
+
class TabValues {
|
|
76
|
+
constructor(_key, _storage){
|
|
77
|
+
this._key = _key;
|
|
78
|
+
this._storage = _storage;
|
|
79
|
+
}
|
|
80
|
+
values() {
|
|
81
|
+
if (this._values) return this._values;
|
|
82
|
+
try {
|
|
83
|
+
this._backend = this._storage();
|
|
84
|
+
const raw = this._backend?.getItem(this._key);
|
|
85
|
+
const parsed = raw ? JSON.parse(raw) : void 0;
|
|
86
|
+
const values = Object.create(null);
|
|
87
|
+
Object.assign(values, 'object' != typeof parsed || null === parsed || Array.isArray(parsed) ? {} : parsed);
|
|
88
|
+
this._values ??= values;
|
|
89
|
+
} catch {
|
|
90
|
+
this._backend = void 0;
|
|
91
|
+
this._values ??= Object.create(null);
|
|
92
|
+
}
|
|
93
|
+
return this._values;
|
|
94
|
+
}
|
|
95
|
+
save() {
|
|
96
|
+
try {
|
|
97
|
+
this._backend?.setItem(this._key, JSON.stringify(this.values()));
|
|
98
|
+
} catch {}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
102
|
+
TabValues: ()=>TabValues
|
|
103
|
+
}, {
|
|
104
|
+
cloneJson: cloneJson,
|
|
105
|
+
createKeyValueStore: createKeyValueStore
|
|
106
|
+
});
|
|
107
|
+
exports.TabValues = __webpack_exports__.TabValues;
|
|
108
|
+
exports.cloneJson = __webpack_exports__.cloneJson;
|
|
109
|
+
exports.createKeyValueStore = __webpack_exports__.createKeyValueStore;
|
|
110
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
111
|
+
"TabValues",
|
|
112
|
+
"cloneJson",
|
|
113
|
+
"createKeyValueStore"
|
|
114
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
115
|
+
Object.defineProperty(exports, '__esModule', {
|
|
116
|
+
value: true
|
|
117
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
const cloneJson = (value)=>{
|
|
2
|
+
const json = JSON.stringify(value);
|
|
3
|
+
if (void 0 === json) throw new TypeError('The value must be JSON-serializable');
|
|
4
|
+
return JSON.parse(json);
|
|
5
|
+
};
|
|
6
|
+
const createReader = (context)=>{
|
|
7
|
+
const read = (key)=>{
|
|
8
|
+
if (!context.canAccess()) return;
|
|
9
|
+
const value = context.values()[key];
|
|
10
|
+
return void 0 === value ? void 0 : cloneJson(value);
|
|
11
|
+
};
|
|
12
|
+
return (keyOrKeys)=>{
|
|
13
|
+
if ('string' == typeof keyOrKeys) return read(keyOrKeys);
|
|
14
|
+
const entries = Object.create(null);
|
|
15
|
+
for (const key of keyOrKeys){
|
|
16
|
+
const value = read(key);
|
|
17
|
+
if (void 0 !== value) entries[key] = value;
|
|
18
|
+
}
|
|
19
|
+
return entries;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
const createWriter = (context)=>(keyOrValues, value)=>{
|
|
23
|
+
if (!context.canAccess()) return;
|
|
24
|
+
const values = context.values();
|
|
25
|
+
const entries = 'string' == typeof keyOrValues ? {
|
|
26
|
+
[keyOrValues]: value
|
|
27
|
+
} : keyOrValues;
|
|
28
|
+
for (const [key, entry] of Object.entries(entries))if (void 0 === entry) delete values[key];
|
|
29
|
+
else values[key] = cloneJson(entry);
|
|
30
|
+
context.save();
|
|
31
|
+
};
|
|
32
|
+
const createRemover = (context)=>(keyOrKeys)=>{
|
|
33
|
+
if (!context.canAccess()) return;
|
|
34
|
+
const values = context.values();
|
|
35
|
+
for (const key of 'string' == typeof keyOrKeys ? [
|
|
36
|
+
keyOrKeys
|
|
37
|
+
] : keyOrKeys)delete values[key];
|
|
38
|
+
context.save();
|
|
39
|
+
};
|
|
40
|
+
const createKeyValueStore = (context)=>({
|
|
41
|
+
initialize () {},
|
|
42
|
+
get: createReader(context),
|
|
43
|
+
set: createWriter(context),
|
|
44
|
+
remove: createRemover(context)
|
|
45
|
+
});
|
|
46
|
+
class TabValues {
|
|
47
|
+
constructor(_key, _storage){
|
|
48
|
+
this._key = _key;
|
|
49
|
+
this._storage = _storage;
|
|
50
|
+
}
|
|
51
|
+
values() {
|
|
52
|
+
if (this._values) return this._values;
|
|
53
|
+
try {
|
|
54
|
+
this._backend = this._storage();
|
|
55
|
+
const raw = this._backend?.getItem(this._key);
|
|
56
|
+
const parsed = raw ? JSON.parse(raw) : void 0;
|
|
57
|
+
const values = Object.create(null);
|
|
58
|
+
Object.assign(values, 'object' != typeof parsed || null === parsed || Array.isArray(parsed) ? {} : parsed);
|
|
59
|
+
this._values ??= values;
|
|
60
|
+
} catch {
|
|
61
|
+
this._backend = void 0;
|
|
62
|
+
this._values ??= Object.create(null);
|
|
63
|
+
}
|
|
64
|
+
return this._values;
|
|
65
|
+
}
|
|
66
|
+
save() {
|
|
67
|
+
try {
|
|
68
|
+
this._backend?.setItem(this._key, JSON.stringify(this.values()));
|
|
69
|
+
} catch {}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
export { TabValues, cloneJson, createKeyValueStore };
|
package/dist/posthog.js
CHANGED
|
@@ -577,6 +577,7 @@ class PostHog {
|
|
|
577
577
|
return {
|
|
578
578
|
projectToken: this.projectToken,
|
|
579
579
|
logger: this.logger,
|
|
580
|
+
sessionKv: this._state.tabKeyValueStore('analytics', ()=>canMutate() && this._state.prepare()),
|
|
580
581
|
state: this._state,
|
|
581
582
|
canCapture: ()=>canMutate() && !this._blocked && !this.hasOptedOut(),
|
|
582
583
|
canMutate,
|
package/dist/posthog.mjs
CHANGED
|
@@ -545,6 +545,7 @@ class PostHog {
|
|
|
545
545
|
return {
|
|
546
546
|
projectToken: this.projectToken,
|
|
547
547
|
logger: this.logger,
|
|
548
|
+
sessionKv: this._state.tabKeyValueStore('analytics', ()=>canMutate() && this._state.prepare()),
|
|
548
549
|
state: this._state,
|
|
549
550
|
canCapture: ()=>canMutate() && !this._blocked && !this.hasOptedOut(),
|
|
550
551
|
canMutate,
|
package/dist/state.d.ts
CHANGED
|
@@ -50,6 +50,8 @@ 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;
|
|
@@ -64,7 +66,10 @@ export declare class BrowserState {
|
|
|
64
66
|
optIn(): boolean;
|
|
65
67
|
optOut(): boolean;
|
|
66
68
|
keyValueStore(namespace: string, canAccess?: () => boolean): KeyValueStore;
|
|
69
|
+
tabKeyValueStore(namespace: string, canAccess: () => boolean): KeyValueStore;
|
|
70
|
+
private _tabStorage;
|
|
67
71
|
private _readPersistedState;
|
|
72
|
+
private _readSessionAuthority;
|
|
68
73
|
private _prepareWindow;
|
|
69
74
|
private _initializeWindowStorage;
|
|
70
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;
|
|
@@ -288,22 +298,11 @@ class BrowserState {
|
|
|
288
298
|
}
|
|
289
299
|
prepareSessionForEvent(now = Date.now()) {
|
|
290
300
|
const safeNow = isTimestamp(now) ? now : 0;
|
|
291
|
-
const
|
|
292
|
-
let session = this._state.session;
|
|
293
|
-
let lastActivityWriteTimestamp = this._lastActivityWriteTimestamp;
|
|
294
|
-
let adoptedAfterReset = false;
|
|
295
|
-
if (sharedRead && external && persistedAuthorityWins(external, this._state)) {
|
|
296
|
-
session = external.session;
|
|
297
|
-
if (session) {
|
|
298
|
-
lastActivityWriteTimestamp = session.lastActivityTimestamp;
|
|
299
|
-
adoptedAfterReset = 'reset' === this._pendingSessionReason;
|
|
300
|
-
}
|
|
301
|
-
}
|
|
301
|
+
const { external, session, lastActivityWriteTimestamp, adoptedAfterReset } = this._readSessionAuthority();
|
|
302
302
|
const pendingReason = this._pendingSessionReason;
|
|
303
|
-
const
|
|
304
|
-
const
|
|
305
|
-
const
|
|
306
|
-
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;
|
|
307
306
|
const preparedSession = rotated ? createSession(safeNow, nextRevision(safeNow, this._state, external)) : {
|
|
308
307
|
...session,
|
|
309
308
|
lastActivityTimestamp: Math.max(safeNow, session.lastActivityTimestamp)
|
|
@@ -387,42 +386,22 @@ class BrowserState {
|
|
|
387
386
|
return this._writeConsent('0');
|
|
388
387
|
}
|
|
389
388
|
keyValueStore(namespace, canAccess = ()=>true) {
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
},
|
|
407
|
-
set: (keyOrValues, value)=>{
|
|
408
|
-
if (!canAccess()) return;
|
|
409
|
-
const namespaceValues = values();
|
|
410
|
-
const entries = 'string' == typeof keyOrValues ? {
|
|
411
|
-
[keyOrValues]: value
|
|
412
|
-
} : keyOrValues;
|
|
413
|
-
for (const [key, entry] of Object.entries(entries))if (void 0 === entry) delete namespaceValues[key];
|
|
414
|
-
else namespaceValues[key] = cloneJson(entry);
|
|
415
|
-
this._save();
|
|
416
|
-
},
|
|
417
|
-
remove: (keyOrKeys)=>{
|
|
418
|
-
if (!canAccess()) return;
|
|
419
|
-
const namespaceValues = values();
|
|
420
|
-
for (const key of 'string' == typeof keyOrKeys ? [
|
|
421
|
-
keyOrKeys
|
|
422
|
-
] : keyOrKeys)delete namespaceValues[key];
|
|
423
|
-
this._save();
|
|
424
|
-
}
|
|
425
|
-
};
|
|
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?.();
|
|
426
405
|
}
|
|
427
406
|
_readPersistedState() {
|
|
428
407
|
if (!this._storage) return [
|
|
@@ -446,6 +425,17 @@ class BrowserState {
|
|
|
446
425
|
false
|
|
447
426
|
];
|
|
448
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
|
+
}
|
|
449
439
|
_prepareWindow(rotated) {
|
|
450
440
|
if (rotated || this._windowId) return {
|
|
451
441
|
windowId: rotated ? (0, external_id_js_namespaceObject.createId)() : this._windowId,
|