@posthog/browser 0.5.0 → 0.6.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 +17 -17
- package/README.md +42 -7
- package/dist/composition/npm/core.d.ts +1 -1
- package/dist/composition/npm/index.d.ts +2 -2
- package/dist/core/posthog.js +88 -14
- package/dist/core/posthog.mjs +88 -14
- package/dist/core/state/state.d.ts +32 -3
- package/dist/core/state/state.js +117 -25
- package/dist/core/state/state.mjs +105 -25
- package/dist/core/types.d.ts +87 -5
- package/dist/persistence/cookie.d.ts +19 -0
- package/dist/persistence/cookie.js +273 -0
- package/dist/persistence/cookie.mjs +235 -0
- package/dist/platform/logger.d.ts +5 -1
- package/dist/platform/logger.js +3 -3
- package/dist/platform/logger.mjs +3 -3
- package/dist/primitives/id.d.ts +5 -0
- package/dist/primitives/id.js +10 -2
- package/dist/primitives/id.mjs +6 -1
- package/dist/primitives/version.js +1 -1
- package/dist/primitives/version.mjs +1 -1
- package/dist/products/analytics/analytics-core.d.ts +17 -4
- package/dist/products/analytics/analytics-core.js +101 -10
- package/dist/products/analytics/analytics-core.mjs +101 -10
- package/dist/products/analytics/analytics-internal.d.ts +3 -2
- package/dist/products/analytics/delivery/analytics-delivery.js +1 -1
- package/dist/products/analytics/delivery/analytics-delivery.mjs +1 -1
- package/dist/products/analytics/delivery/lane.d.ts +8 -1
- package/dist/products/analytics/delivery/lane.js +16 -4
- package/dist/products/analytics/delivery/lane.mjs +16 -4
- package/dist/products/analytics/groups.js +1 -1
- package/dist/products/analytics/groups.mjs +1 -1
- package/dist/products/analytics/person.js +1 -1
- package/dist/products/analytics/person.mjs +1 -1
- package/dist/products/analytics/properties/analytics-properties.d.ts +1 -0
- package/dist/products/analytics/properties/analytics-properties.js +3 -0
- package/dist/products/analytics/properties/analytics-properties.mjs +1 -1
- package/dist/products/flags/flags-internal.d.ts +3 -0
- package/dist/products/flags/flags-options.d.ts +16 -2
- package/dist/products/flags/flags-token.d.ts +28 -2
- package/dist/products/flags/flags.d.ts +1 -1
- package/dist/products/flags/flags.js +110 -4
- package/dist/products/flags/flags.mjs +110 -4
- package/dist/products/surveys/surveys-extension.js +75 -26
- package/dist/products/surveys/surveys-extension.mjs +77 -28
- package/dist/products/surveys/surveys-internal.d.ts +6 -0
- package/package.json +9 -4
|
@@ -36,6 +36,12 @@ const survey_event_receiver_namespaceObject = require("@posthog/browser-common/s
|
|
|
36
36
|
const url_targeting_utils_namespaceObject = require("@posthog/browser-common/utils/url-targeting-utils");
|
|
37
37
|
const analytics_core_js_namespaceObject = require("../analytics/analytics-core.js");
|
|
38
38
|
const external_surveys_options_js_namespaceObject = require("./surveys-options.js");
|
|
39
|
+
const MATCHING_CONDITION_EVENTS = [
|
|
40
|
+
'$pageview',
|
|
41
|
+
surveys_namespaceObject.SurveyEventName.SHOWN,
|
|
42
|
+
surveys_namespaceObject.SurveyEventName.DISMISSED,
|
|
43
|
+
surveys_namespaceObject.SurveyEventName.SENT
|
|
44
|
+
];
|
|
39
45
|
const createSurveys = (options, load)=>{
|
|
40
46
|
const config = (0, external_surveys_options_js_namespaceObject.snapshotSurveysOptions)(options);
|
|
41
47
|
let elementSelectors = new Set();
|
|
@@ -52,6 +58,7 @@ const createSurveys = (options, load)=>{
|
|
|
52
58
|
let loading;
|
|
53
59
|
let remoteSubscription;
|
|
54
60
|
let resetSubscription;
|
|
61
|
+
let seenSubscription;
|
|
55
62
|
const getFlagsContext = ()=>client?.featureFlags?.getSurveyContext?.();
|
|
56
63
|
const reset = ()=>{
|
|
57
64
|
shared.reset();
|
|
@@ -84,7 +91,36 @@ const createSurveys = (options, load)=>{
|
|
|
84
91
|
if (!disposed) callback(error);
|
|
85
92
|
});
|
|
86
93
|
}
|
|
87
|
-
})
|
|
94
|
+
}),
|
|
95
|
+
onMatchingConditionsChanged: (callback)=>{
|
|
96
|
+
const subscriptions = [];
|
|
97
|
+
if (client) subscriptions.push(client.onEvent(({ event })=>{
|
|
98
|
+
if (MATCHING_CONDITION_EVENTS.includes(event)) callback();
|
|
99
|
+
}));
|
|
100
|
+
const onShown = ()=>callback();
|
|
101
|
+
if ("u" > typeof window) window.addEventListener('PHSurveyShown', onShown);
|
|
102
|
+
let listening = false;
|
|
103
|
+
let stopped = false;
|
|
104
|
+
const subscribeToFlags = (flags)=>{
|
|
105
|
+
subscriptions.push(flags.onFeatureFlags(()=>{
|
|
106
|
+
if (listening) callback();
|
|
107
|
+
}));
|
|
108
|
+
listening = true;
|
|
109
|
+
};
|
|
110
|
+
const flags = client?.featureFlags;
|
|
111
|
+
if (flags) subscribeToFlags(flags);
|
|
112
|
+
else if (client) client.resolveExtension('featureFlags').then((installed)=>{
|
|
113
|
+
if (stopped || !installed) return;
|
|
114
|
+
subscribeToFlags(installed);
|
|
115
|
+
callback();
|
|
116
|
+
}, ()=>{});
|
|
117
|
+
return ()=>{
|
|
118
|
+
stopped = true;
|
|
119
|
+
listening = false;
|
|
120
|
+
subscriptions.splice(0).forEach((subscription)=>subscription.dispose());
|
|
121
|
+
if ("u" > typeof window) window.removeEventListener('PHSurveyShown', onShown);
|
|
122
|
+
};
|
|
123
|
+
}
|
|
88
124
|
};
|
|
89
125
|
const shared = new class extends surveys_namespaceObject.PostHogSurveys {
|
|
90
126
|
_createEventReceiver(value, onActivationChanged) {
|
|
@@ -141,6 +177,31 @@ const createSurveys = (options, load)=>{
|
|
|
141
177
|
}
|
|
142
178
|
});
|
|
143
179
|
};
|
|
180
|
+
const subscribe = (callback, listen)=>{
|
|
181
|
+
let active = true;
|
|
182
|
+
const notify = deliver((surveys, context)=>{
|
|
183
|
+
if (active) callback(surveys, context);
|
|
184
|
+
});
|
|
185
|
+
const unsubscribe = listen(notify);
|
|
186
|
+
if (!client || "u" < typeof document) notify([], {
|
|
187
|
+
isLoaded: false,
|
|
188
|
+
error: 'Survey renderer unavailable'
|
|
189
|
+
});
|
|
190
|
+
else if (!disposed) {
|
|
191
|
+
manual = true;
|
|
192
|
+
try {
|
|
193
|
+
shared.loadIfEnabled();
|
|
194
|
+
} catch (error) {
|
|
195
|
+
client.logger.error('Survey renderer loading failed', error);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return {
|
|
199
|
+
dispose: ()=>{
|
|
200
|
+
active = false;
|
|
201
|
+
unsubscribe();
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
};
|
|
144
205
|
let teardownSubscription;
|
|
145
206
|
const pagehide = ()=>{
|
|
146
207
|
if (!teardownSubscription?.deliveryAvailable) shared.handlePageUnload();
|
|
@@ -152,6 +213,16 @@ const createSurveys = (options, load)=>{
|
|
|
152
213
|
await value.kv.initialize();
|
|
153
214
|
if (disposed) return;
|
|
154
215
|
resetSubscription = value.onReset(reset);
|
|
216
|
+
seenSubscription = value.onEvent(({ event, properties })=>{
|
|
217
|
+
if (event !== surveys_namespaceObject.SurveyEventName.DISMISSED && event !== surveys_namespaceObject.SurveyEventName.SENT) return;
|
|
218
|
+
const id = properties[surveys_namespaceObject.SurveyEventProperties.SURVEY_ID];
|
|
219
|
+
const iteration = properties[surveys_namespaceObject.SurveyEventProperties.SURVEY_ITERATION];
|
|
220
|
+
if ('string' != typeof id) return;
|
|
221
|
+
(0, survey_utils_namespaceObject.setSurveySeenOnLocalStorage)({
|
|
222
|
+
id,
|
|
223
|
+
current_iteration: 'number' == typeof iteration ? iteration : null
|
|
224
|
+
});
|
|
225
|
+
});
|
|
155
226
|
runtimeHost = {
|
|
156
227
|
client: value,
|
|
157
228
|
get config () {
|
|
@@ -200,31 +271,8 @@ const createSurveys = (options, load)=>{
|
|
|
200
271
|
reject(error);
|
|
201
272
|
});
|
|
202
273
|
}),
|
|
203
|
-
onSurveysLoaded: (callback)=>
|
|
204
|
-
|
|
205
|
-
const notify = deliver((surveys, context)=>{
|
|
206
|
-
if (active) callback(surveys, context);
|
|
207
|
-
});
|
|
208
|
-
const unsubscribe = shared.onSurveysLoaded(notify);
|
|
209
|
-
if (!client || "u" < typeof document) notify([], {
|
|
210
|
-
isLoaded: false,
|
|
211
|
-
error: 'Survey renderer unavailable'
|
|
212
|
-
});
|
|
213
|
-
else if (!disposed) {
|
|
214
|
-
manual = true;
|
|
215
|
-
try {
|
|
216
|
-
shared.loadIfEnabled();
|
|
217
|
-
} catch (error) {
|
|
218
|
-
client.logger.error('Survey renderer loading failed', error);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
return {
|
|
222
|
-
dispose: ()=>{
|
|
223
|
-
active = false;
|
|
224
|
-
unsubscribe();
|
|
225
|
-
}
|
|
226
|
-
};
|
|
227
|
-
},
|
|
274
|
+
onSurveysLoaded: (callback)=>subscribe(callback, (notify)=>shared.onSurveysLoaded(notify)),
|
|
275
|
+
onActiveMatchingSurveysChanged: (callback)=>subscribe(callback, (notify)=>shared.onActiveMatchingSurveysChanged(notify)),
|
|
228
276
|
getElementSelectors: ()=>new Set(elementSelectors),
|
|
229
277
|
cancelPendingSurvey: (id)=>{
|
|
230
278
|
if (!disposed) shared.cancelPendingSurvey(id);
|
|
@@ -245,6 +293,7 @@ const createSurveys = (options, load)=>{
|
|
|
245
293
|
});
|
|
246
294
|
remoteSubscription?.dispose();
|
|
247
295
|
resetSubscription?.dispose();
|
|
296
|
+
seenSubscription?.dispose();
|
|
248
297
|
if ("u" > typeof window) window.removeEventListener('pagehide', pagehide);
|
|
249
298
|
teardownSubscription?.dispose();
|
|
250
299
|
shared.dispose();
|
|
@@ -1,9 +1,15 @@
|
|
|
1
|
-
import { PostHogSurveys } from "@posthog/browser-common/surveys";
|
|
2
|
-
import { DEFAULT_DISPLAY_SURVEY_OPTIONS } from "@posthog/browser-common/utils/survey-utils";
|
|
1
|
+
import { PostHogSurveys, SurveyEventName, SurveyEventProperties } from "@posthog/browser-common/surveys";
|
|
2
|
+
import { DEFAULT_DISPLAY_SURVEY_OPTIONS, setSurveySeenOnLocalStorage } from "@posthog/browser-common/utils/survey-utils";
|
|
3
3
|
import { SurveyEventReceiver } from "@posthog/browser-common/survey-event-receiver";
|
|
4
4
|
import { getTargetingUrl } from "@posthog/browser-common/utils/url-targeting-utils";
|
|
5
5
|
import { analyticsOf } from "../analytics/analytics-core.mjs";
|
|
6
6
|
import { snapshotSurveysOptions } from "./surveys-options.mjs";
|
|
7
|
+
const MATCHING_CONDITION_EVENTS = [
|
|
8
|
+
'$pageview',
|
|
9
|
+
SurveyEventName.SHOWN,
|
|
10
|
+
SurveyEventName.DISMISSED,
|
|
11
|
+
SurveyEventName.SENT
|
|
12
|
+
];
|
|
7
13
|
const createSurveys = (options, load)=>{
|
|
8
14
|
const config = snapshotSurveysOptions(options);
|
|
9
15
|
let elementSelectors = new Set();
|
|
@@ -20,6 +26,7 @@ const createSurveys = (options, load)=>{
|
|
|
20
26
|
let loading;
|
|
21
27
|
let remoteSubscription;
|
|
22
28
|
let resetSubscription;
|
|
29
|
+
let seenSubscription;
|
|
23
30
|
const getFlagsContext = ()=>client?.featureFlags?.getSurveyContext?.();
|
|
24
31
|
const reset = ()=>{
|
|
25
32
|
shared.reset();
|
|
@@ -52,7 +59,36 @@ const createSurveys = (options, load)=>{
|
|
|
52
59
|
if (!disposed) callback(error);
|
|
53
60
|
});
|
|
54
61
|
}
|
|
55
|
-
})
|
|
62
|
+
}),
|
|
63
|
+
onMatchingConditionsChanged: (callback)=>{
|
|
64
|
+
const subscriptions = [];
|
|
65
|
+
if (client) subscriptions.push(client.onEvent(({ event })=>{
|
|
66
|
+
if (MATCHING_CONDITION_EVENTS.includes(event)) callback();
|
|
67
|
+
}));
|
|
68
|
+
const onShown = ()=>callback();
|
|
69
|
+
if ("u" > typeof window) window.addEventListener('PHSurveyShown', onShown);
|
|
70
|
+
let listening = false;
|
|
71
|
+
let stopped = false;
|
|
72
|
+
const subscribeToFlags = (flags)=>{
|
|
73
|
+
subscriptions.push(flags.onFeatureFlags(()=>{
|
|
74
|
+
if (listening) callback();
|
|
75
|
+
}));
|
|
76
|
+
listening = true;
|
|
77
|
+
};
|
|
78
|
+
const flags = client?.featureFlags;
|
|
79
|
+
if (flags) subscribeToFlags(flags);
|
|
80
|
+
else if (client) client.resolveExtension('featureFlags').then((installed)=>{
|
|
81
|
+
if (stopped || !installed) return;
|
|
82
|
+
subscribeToFlags(installed);
|
|
83
|
+
callback();
|
|
84
|
+
}, ()=>{});
|
|
85
|
+
return ()=>{
|
|
86
|
+
stopped = true;
|
|
87
|
+
listening = false;
|
|
88
|
+
subscriptions.splice(0).forEach((subscription)=>subscription.dispose());
|
|
89
|
+
if ("u" > typeof window) window.removeEventListener('PHSurveyShown', onShown);
|
|
90
|
+
};
|
|
91
|
+
}
|
|
56
92
|
};
|
|
57
93
|
const shared = new class extends PostHogSurveys {
|
|
58
94
|
_createEventReceiver(value, onActivationChanged) {
|
|
@@ -109,6 +145,31 @@ const createSurveys = (options, load)=>{
|
|
|
109
145
|
}
|
|
110
146
|
});
|
|
111
147
|
};
|
|
148
|
+
const subscribe = (callback, listen)=>{
|
|
149
|
+
let active = true;
|
|
150
|
+
const notify = deliver((surveys, context)=>{
|
|
151
|
+
if (active) callback(surveys, context);
|
|
152
|
+
});
|
|
153
|
+
const unsubscribe = listen(notify);
|
|
154
|
+
if (!client || "u" < typeof document) notify([], {
|
|
155
|
+
isLoaded: false,
|
|
156
|
+
error: 'Survey renderer unavailable'
|
|
157
|
+
});
|
|
158
|
+
else if (!disposed) {
|
|
159
|
+
manual = true;
|
|
160
|
+
try {
|
|
161
|
+
shared.loadIfEnabled();
|
|
162
|
+
} catch (error) {
|
|
163
|
+
client.logger.error('Survey renderer loading failed', error);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
dispose: ()=>{
|
|
168
|
+
active = false;
|
|
169
|
+
unsubscribe();
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
};
|
|
112
173
|
let teardownSubscription;
|
|
113
174
|
const pagehide = ()=>{
|
|
114
175
|
if (!teardownSubscription?.deliveryAvailable) shared.handlePageUnload();
|
|
@@ -120,6 +181,16 @@ const createSurveys = (options, load)=>{
|
|
|
120
181
|
await value.kv.initialize();
|
|
121
182
|
if (disposed) return;
|
|
122
183
|
resetSubscription = value.onReset(reset);
|
|
184
|
+
seenSubscription = value.onEvent(({ event, properties })=>{
|
|
185
|
+
if (event !== SurveyEventName.DISMISSED && event !== SurveyEventName.SENT) return;
|
|
186
|
+
const id = properties[SurveyEventProperties.SURVEY_ID];
|
|
187
|
+
const iteration = properties[SurveyEventProperties.SURVEY_ITERATION];
|
|
188
|
+
if ('string' != typeof id) return;
|
|
189
|
+
setSurveySeenOnLocalStorage({
|
|
190
|
+
id,
|
|
191
|
+
current_iteration: 'number' == typeof iteration ? iteration : null
|
|
192
|
+
});
|
|
193
|
+
});
|
|
123
194
|
runtimeHost = {
|
|
124
195
|
client: value,
|
|
125
196
|
get config () {
|
|
@@ -168,31 +239,8 @@ const createSurveys = (options, load)=>{
|
|
|
168
239
|
reject(error);
|
|
169
240
|
});
|
|
170
241
|
}),
|
|
171
|
-
onSurveysLoaded: (callback)=>
|
|
172
|
-
|
|
173
|
-
const notify = deliver((surveys, context)=>{
|
|
174
|
-
if (active) callback(surveys, context);
|
|
175
|
-
});
|
|
176
|
-
const unsubscribe = shared.onSurveysLoaded(notify);
|
|
177
|
-
if (!client || "u" < typeof document) notify([], {
|
|
178
|
-
isLoaded: false,
|
|
179
|
-
error: 'Survey renderer unavailable'
|
|
180
|
-
});
|
|
181
|
-
else if (!disposed) {
|
|
182
|
-
manual = true;
|
|
183
|
-
try {
|
|
184
|
-
shared.loadIfEnabled();
|
|
185
|
-
} catch (error) {
|
|
186
|
-
client.logger.error('Survey renderer loading failed', error);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
return {
|
|
190
|
-
dispose: ()=>{
|
|
191
|
-
active = false;
|
|
192
|
-
unsubscribe();
|
|
193
|
-
}
|
|
194
|
-
};
|
|
195
|
-
},
|
|
242
|
+
onSurveysLoaded: (callback)=>subscribe(callback, (notify)=>shared.onSurveysLoaded(notify)),
|
|
243
|
+
onActiveMatchingSurveysChanged: (callback)=>subscribe(callback, (notify)=>shared.onActiveMatchingSurveysChanged(notify)),
|
|
196
244
|
getElementSelectors: ()=>new Set(elementSelectors),
|
|
197
245
|
cancelPendingSurvey: (id)=>{
|
|
198
246
|
if (!disposed) shared.cancelPendingSurvey(id);
|
|
@@ -213,6 +261,7 @@ const createSurveys = (options, load)=>{
|
|
|
213
261
|
});
|
|
214
262
|
remoteSubscription?.dispose();
|
|
215
263
|
resetSubscription?.dispose();
|
|
264
|
+
seenSubscription?.dispose();
|
|
216
265
|
if ("u" > typeof window) window.removeEventListener('pagehide', pagehide);
|
|
217
266
|
teardownSubscription?.dispose();
|
|
218
267
|
shared.dispose();
|
|
@@ -8,6 +8,12 @@ export interface SurveysExtension extends BrowserExtension {
|
|
|
8
8
|
displaySurvey(id: string, options?: DisplaySurveyOptions): void;
|
|
9
9
|
canRenderSurvey(id: string, forceReload?: boolean): Promise<SurveyRenderReason>;
|
|
10
10
|
onSurveysLoaded(callback: SurveyCallback): Disposable;
|
|
11
|
+
/**
|
|
12
|
+
* Observe the set of active matching surveys. Delivers the current set once loaded, then changes after
|
|
13
|
+
* event and action triggers, captured pageviews, survey display and responses, feature-flag updates,
|
|
14
|
+
* definition refreshes and reset. Unchanged results are not delivered again.
|
|
15
|
+
*/
|
|
16
|
+
onActiveMatchingSurveysChanged(callback: SurveyCallback): Disposable;
|
|
11
17
|
cancelPendingSurvey(id: string): void;
|
|
12
18
|
reset(): void;
|
|
13
19
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@posthog/browser",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "A small, extension-based PostHog client for modern browsers",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -47,10 +47,15 @@
|
|
|
47
47
|
"types": "./dist/products/autocapture/autocapture.d.ts",
|
|
48
48
|
"require": "./dist/products/autocapture/autocapture.js",
|
|
49
49
|
"import": "./dist/products/autocapture/autocapture.mjs"
|
|
50
|
+
},
|
|
51
|
+
"./persistence/cookie": {
|
|
52
|
+
"types": "./dist/persistence/cookie.d.ts",
|
|
53
|
+
"require": "./dist/persistence/cookie.js",
|
|
54
|
+
"import": "./dist/persistence/cookie.mjs"
|
|
50
55
|
}
|
|
51
56
|
},
|
|
52
57
|
"dependencies": {
|
|
53
|
-
"@posthog/browser-common": "^0.
|
|
58
|
+
"@posthog/browser-common": "^0.14.0",
|
|
54
59
|
"@posthog/types": "^1.427.1"
|
|
55
60
|
},
|
|
56
61
|
"devDependencies": {
|
|
@@ -61,8 +66,8 @@
|
|
|
61
66
|
"typescript": "7.0.2",
|
|
62
67
|
"typescript-legacy": "npm:typescript@5.8.2",
|
|
63
68
|
"vitest": "^1.6.1",
|
|
64
|
-
"@posthog-tooling/
|
|
65
|
-
"@posthog-tooling/
|
|
69
|
+
"@posthog-tooling/tsconfig-base": "1.1.1",
|
|
70
|
+
"@posthog-tooling/sdk-mock-server": "0.0.0"
|
|
66
71
|
},
|
|
67
72
|
"scripts": {
|
|
68
73
|
"clean": "rimraf dist dist-cdn",
|