@funnelsgrove/runtime 0.1.34 → 0.1.36
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.
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import posthog from 'posthog-js';
|
|
2
|
+
import { runtimePublicConfig } from '../services/public-env.js';
|
|
2
3
|
let initialized = false;
|
|
3
4
|
let readyPromise = null;
|
|
4
5
|
export const POSTHOG_FEATURE_FLAG_READY_TIMEOUT_MS = 2500;
|
|
6
|
+
const STORED_PERSON_PROPERTIES_KEY = '$stored_person_properties';
|
|
5
7
|
const buildFlagScopeProperties = (config) => {
|
|
6
8
|
var _a, _b;
|
|
7
9
|
const properties = {};
|
|
8
|
-
const projectId = (_a = config.projectId) === null || _a === void 0 ? void 0 : _a.trim();
|
|
9
|
-
const funnelId = (_b = config.funnelId) === null || _b === void 0 ? void 0 : _b.trim();
|
|
10
|
+
const projectId = ((_a = config.projectId) === null || _a === void 0 ? void 0 : _a.trim()) || runtimePublicConfig.projectId;
|
|
11
|
+
const funnelId = ((_b = config.funnelId) === null || _b === void 0 ? void 0 : _b.trim()) || runtimePublicConfig.funnelId;
|
|
10
12
|
if (projectId) {
|
|
11
13
|
properties.project_id = projectId;
|
|
12
14
|
}
|
|
@@ -15,6 +17,42 @@ const buildFlagScopeProperties = (config) => {
|
|
|
15
17
|
}
|
|
16
18
|
return properties;
|
|
17
19
|
};
|
|
20
|
+
const parseStorageObject = (value) => {
|
|
21
|
+
if (!value) {
|
|
22
|
+
return {};
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
const parsed = JSON.parse(value);
|
|
26
|
+
return parsed && typeof parsed === 'object' && !Array.isArray(parsed)
|
|
27
|
+
? parsed
|
|
28
|
+
: {};
|
|
29
|
+
}
|
|
30
|
+
catch (_a) {
|
|
31
|
+
return {};
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const toPostHogPersistenceKey = (apiKey) => {
|
|
35
|
+
const token = apiKey.replace(/\+/g, 'PL').replace(/\//g, 'SL').replace(/=/g, 'EQ');
|
|
36
|
+
return `ph_${token}_posthog`;
|
|
37
|
+
};
|
|
38
|
+
const primePostHogFlagScopePersistence = (apiKey, scopeProperties) => {
|
|
39
|
+
var _a;
|
|
40
|
+
if (Object.keys(scopeProperties).length === 0 || typeof window === 'undefined') {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
const storage = window.localStorage;
|
|
45
|
+
const persistenceKey = toPostHogPersistenceKey(apiKey);
|
|
46
|
+
const stored = parseStorageObject(storage.getItem(persistenceKey));
|
|
47
|
+
const currentPersonProperties = parseStorageObject(typeof stored[STORED_PERSON_PROPERTIES_KEY] === 'string'
|
|
48
|
+
? stored[STORED_PERSON_PROPERTIES_KEY]
|
|
49
|
+
: JSON.stringify((_a = stored[STORED_PERSON_PROPERTIES_KEY]) !== null && _a !== void 0 ? _a : {}));
|
|
50
|
+
storage.setItem(persistenceKey, JSON.stringify(Object.assign(Object.assign({}, stored), { [STORED_PERSON_PROPERTIES_KEY]: Object.assign(Object.assign({}, currentPersonProperties), scopeProperties) })));
|
|
51
|
+
}
|
|
52
|
+
catch (_b) {
|
|
53
|
+
// PostHog can still initialize without persistence priming when storage is unavailable.
|
|
54
|
+
}
|
|
55
|
+
};
|
|
18
56
|
export const bootstrapPostHog = (config) => {
|
|
19
57
|
if (readyPromise) {
|
|
20
58
|
return readyPromise;
|
|
@@ -36,6 +74,8 @@ export const bootstrapPostHog = (config) => {
|
|
|
36
74
|
resolve();
|
|
37
75
|
};
|
|
38
76
|
readyTimeoutId = setTimeout(markReady, POSTHOG_FEATURE_FLAG_READY_TIMEOUT_MS);
|
|
77
|
+
const scopeProperties = buildFlagScopeProperties(config);
|
|
78
|
+
primePostHogFlagScopePersistence(config.apiKey, scopeProperties);
|
|
39
79
|
posthog.init(config.apiKey, {
|
|
40
80
|
api_host: config.apiHost,
|
|
41
81
|
persistence: 'localStorage+cookie',
|
|
@@ -43,7 +83,6 @@ export const bootstrapPostHog = (config) => {
|
|
|
43
83
|
advanced_disable_feature_flags_on_first_load: true,
|
|
44
84
|
bootstrap: { distinctID: config.distinctId },
|
|
45
85
|
loaded: () => {
|
|
46
|
-
const scopeProperties = buildFlagScopeProperties(config);
|
|
47
86
|
let unsubscribe = null;
|
|
48
87
|
unsubscribe = posthog.onFeatureFlags(() => {
|
|
49
88
|
unsubscribe === null || unsubscribe === void 0 ? void 0 : unsubscribe();
|