@posthog/browser 0.1.0 → 0.2.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.
Files changed (72) hide show
  1. package/ARCHITECTURE.md +29 -23
  2. package/README.md +66 -30
  3. package/dist/analytics-api.d.ts +26 -0
  4. package/dist/analytics-api.js +18 -0
  5. package/dist/analytics-api.mjs +1 -0
  6. package/dist/analytics-buffer.d.ts +3 -3
  7. package/dist/analytics-buffer.js +2 -161
  8. package/dist/analytics-buffer.mjs +2 -161
  9. package/dist/analytics-extension.d.ts +76 -0
  10. package/dist/analytics-extension.js +589 -0
  11. package/dist/analytics-extension.mjs +551 -0
  12. package/dist/analytics-internal.d.ts +32 -18
  13. package/dist/analytics-options.d.ts +7 -2
  14. package/dist/analytics-options.js +38 -1
  15. package/dist/analytics-options.mjs +17 -1
  16. package/dist/analytics.d.ts +4 -3
  17. package/dist/autocapture.d.ts +2 -2
  18. package/dist/autocapture.js +21 -10
  19. package/dist/autocapture.mjs +21 -10
  20. package/dist/automatic-analytics.d.ts +2 -2
  21. package/dist/browser-client.d.ts +44 -4
  22. package/dist/capture-properties.d.ts +9 -0
  23. package/dist/capture-properties.js +68 -0
  24. package/dist/capture-properties.mjs +30 -0
  25. package/dist/capture-v1.js +7 -19
  26. package/dist/capture-v1.mjs +1 -13
  27. package/dist/core.d.ts +7 -5
  28. package/dist/extensions/registry.d.ts +42 -7
  29. package/dist/extensions/registry.js +194 -37
  30. package/dist/extensions/registry.mjs +194 -37
  31. package/dist/flags-token.d.ts +4 -4
  32. package/dist/flags.js +16 -2
  33. package/dist/flags.mjs +16 -2
  34. package/dist/immutable.d.ts +1 -0
  35. package/dist/immutable.js +46 -0
  36. package/dist/immutable.mjs +8 -0
  37. package/dist/index.d.ts +7 -5
  38. package/dist/index.js +3 -67
  39. package/dist/index.mjs +3 -67
  40. package/dist/lane.js +1 -1
  41. package/dist/lane.mjs +1 -1
  42. package/dist/logs-internal.d.ts +2 -2
  43. package/dist/logs.js +5 -2
  44. package/dist/logs.mjs +5 -2
  45. package/dist/person-processing.d.ts +11 -0
  46. package/dist/person-processing.js +84 -0
  47. package/dist/person-processing.mjs +37 -0
  48. package/dist/posthog.d.ts +4 -3
  49. package/dist/posthog.js +155 -349
  50. package/dist/posthog.mjs +155 -349
  51. package/dist/rate-limiter.d.ts +5 -0
  52. package/dist/rate-limiter.js +14 -8
  53. package/dist/rate-limiter.mjs +14 -8
  54. package/dist/root-composition.d.ts +11 -0
  55. package/dist/root-composition.js +138 -0
  56. package/dist/root-composition.mjs +100 -0
  57. package/dist/state.d.ts +1 -0
  58. package/dist/state.js +4 -0
  59. package/dist/state.mjs +4 -0
  60. package/dist/surveys-extension.js +11 -6
  61. package/dist/surveys-extension.mjs +11 -6
  62. package/dist/surveys-internal.d.ts +3 -2
  63. package/dist/surveys.d.ts +0 -1
  64. package/dist/surveys.js +1 -4
  65. package/dist/surveys.mjs +1 -4
  66. package/dist/types.d.ts +13 -19
  67. package/dist/utf8.d.ts +1 -0
  68. package/dist/utf8.js +52 -0
  69. package/dist/utf8.mjs +14 -0
  70. package/dist/version.js +1 -1
  71. package/dist/version.mjs +1 -1
  72. package/package.json +3 -3
@@ -40,14 +40,9 @@ class ClientRateLimiter {
40
40
  }
41
41
  consume() {
42
42
  const now = this._now();
43
- if (this._initialized) {
44
- const elapsed = Math.max(0, now - this._last);
45
- this._last = Math.max(this._last, now);
46
- this._tokens = Math.min(this._burst, this._tokens + elapsed / 1000 * this._eventsPerSecond);
47
- } else {
48
- this._initialized = true;
49
- this._last = now;
50
- }
43
+ this._tokens = this._tokensAt(now);
44
+ this._last = this._initialized ? Math.max(this._last, now) : now;
45
+ this._initialized = true;
51
46
  if (this._tokens >= 1) {
52
47
  this._tokens = Math.max(0, this._tokens - 1);
53
48
  this._limited = false;
@@ -65,9 +60,20 @@ class ClientRateLimiter {
65
60
  }
66
61
  };
67
62
  }
63
+ get wouldLimit() {
64
+ return this._tokensAt(this._now()) < 1;
65
+ }
66
+ get limited() {
67
+ return this._limited;
68
+ }
68
69
  reported() {
69
70
  this._unreportedDropped = 0;
70
71
  }
72
+ _tokensAt(now) {
73
+ if (!this._initialized) return this._tokens;
74
+ const elapsed = Math.max(0, now - this._last);
75
+ return Math.min(this._burst, this._tokens + elapsed / 1000 * this._eventsPerSecond);
76
+ }
71
77
  _now() {
72
78
  try {
73
79
  const value = this._clock();
@@ -11,14 +11,9 @@ class ClientRateLimiter {
11
11
  }
12
12
  consume() {
13
13
  const now = this._now();
14
- if (this._initialized) {
15
- const elapsed = Math.max(0, now - this._last);
16
- this._last = Math.max(this._last, now);
17
- this._tokens = Math.min(this._burst, this._tokens + elapsed / 1000 * this._eventsPerSecond);
18
- } else {
19
- this._initialized = true;
20
- this._last = now;
21
- }
14
+ this._tokens = this._tokensAt(now);
15
+ this._last = this._initialized ? Math.max(this._last, now) : now;
16
+ this._initialized = true;
22
17
  if (this._tokens >= 1) {
23
18
  this._tokens = Math.max(0, this._tokens - 1);
24
19
  this._limited = false;
@@ -36,9 +31,20 @@ class ClientRateLimiter {
36
31
  }
37
32
  };
38
33
  }
34
+ get wouldLimit() {
35
+ return this._tokensAt(this._now()) < 1;
36
+ }
37
+ get limited() {
38
+ return this._limited;
39
+ }
39
40
  reported() {
40
41
  this._unreportedDropped = 0;
41
42
  }
43
+ _tokensAt(now) {
44
+ if (!this._initialized) return this._tokens;
45
+ const elapsed = Math.max(0, now - this._last);
46
+ return Math.min(this._burst, this._tokens + elapsed / 1000 * this._eventsPerSecond);
47
+ }
42
48
  _now() {
43
49
  try {
44
50
  const value = this._clock();
@@ -0,0 +1,11 @@
1
+ import type { IPostHogInternal, BrowserExtension } from './browser-client';
2
+ import type { PostHogOptions } from './types';
3
+ import type { AutomaticExtension, ExtensionLoader } from './extensions/registry';
4
+ interface ProductComposition {
5
+ extensions: BrowserExtension[];
6
+ loaders: Record<string, ExtensionLoader<IPostHogInternal>>;
7
+ automatic: AutomaticExtension<IPostHogInternal>[];
8
+ loadingErrors: Array<[string, unknown]>;
9
+ }
10
+ export declare const createProductComposition: (options: PostHogOptions) => ProductComposition;
11
+ export {};
@@ -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
@@ -56,6 +56,7 @@ export declare class BrowserState {
56
56
  observeConsent(value: unknown): ConsentState;
57
57
  identify(distinctId: string): void;
58
58
  group(type: string, key: string): boolean;
59
+ resetGroups(): void;
59
60
  prepareSessionForEvent(now?: number): PreparedSession;
60
61
  sessionAdmitted(prepared: PreparedSession): boolean;
61
62
  reset(): void;
package/dist/state.js CHANGED
@@ -282,6 +282,10 @@ class BrowserState {
282
282
  this._save();
283
283
  return true;
284
284
  }
285
+ resetGroups() {
286
+ this._state.groups = emptyRecord();
287
+ this._save();
288
+ }
285
289
  prepareSessionForEvent(now = Date.now()) {
286
290
  const safeNow = isTimestamp(now) ? now : 0;
287
291
  const [sharedRead, external] = this._readPersistedState();
package/dist/state.mjs CHANGED
@@ -248,6 +248,10 @@ class BrowserState {
248
248
  this._save();
249
249
  return true;
250
250
  }
251
+ resetGroups() {
252
+ this._state.groups = emptyRecord();
253
+ this._save();
254
+ }
251
255
  prepareSessionForEvent(now = Date.now()) {
252
256
  const safeNow = isTimestamp(now) ? now : 0;
253
257
  const [sharedRead, external] = this._readPersistedState();
@@ -52,7 +52,7 @@ const createSurveys = (options, load)=>{
52
52
  let loading;
53
53
  let remoteSubscription;
54
54
  let resetSubscription;
55
- const getFlagsContext = ()=>client?.getExtension('featureFlags')?.getSurveyContext?.();
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
- value.getExtension('autocapture')?.setElementSelectors(new Set(elementSelectors));
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.getExtension('analytics')?.onBeforeTeardown?.(()=>shared.handlePageUnload());
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?.getExtension('autocapture')?.setElementSelectors?.(new Set());
255
+ client?.autocapture?.setElementSelectors(new Set());
251
256
  } catch (error) {
252
257
  client?.logger.error('Failed to clear survey action selectors', error);
253
258
  }
@@ -20,7 +20,7 @@ const createSurveys = (options, load)=>{
20
20
  let loading;
21
21
  let remoteSubscription;
22
22
  let resetSubscription;
23
- const getFlagsContext = ()=>client?.getExtension('featureFlags')?.getSurveyContext?.();
23
+ const getFlagsContext = ()=>client?.featureFlags?.getSurveyContext?.();
24
24
  const reset = ()=>{
25
25
  shared.reset();
26
26
  };
@@ -60,7 +60,7 @@ const createSurveys = (options, load)=>{
60
60
  getActionUrl: ()=>runtimeHost ? getTargetingUrl(runtimeHost) : void 0,
61
61
  setElementSelectors: (selectors)=>{
62
62
  elementSelectors = new Set(selectors);
63
- value.getExtension('autocapture')?.setElementSelectors(new Set(elementSelectors));
63
+ client?.autocapture?.setElementSelectors(new Set(elementSelectors));
64
64
  }
65
65
  }, onActivationChanged);
66
66
  return eventReceiver;
@@ -115,12 +115,17 @@ const createSurveys = (options, load)=>{
115
115
  };
116
116
  return {
117
117
  name: 'surveys',
118
+ bindings: {
119
+ surveys: shared
120
+ },
118
121
  setup: async (value)=>{
119
122
  client = value;
123
+ await value.resolveExtension('analytics');
124
+ if (disposed) return;
120
125
  await value.kv.initialize();
121
126
  if (disposed) return;
122
127
  resetSubscription = value.onReset(reset);
123
- const renderClient = Object.create(value);
128
+ const renderClient = Object.create(value.sharedClient);
124
129
  const read = (key)=>key === STORED_PERSON_PROPERTIES_KEY ? getFlagsContext()?.personProperties : value.kv.get(key);
125
130
  const renderKv = {
126
131
  ...value.kv,
@@ -146,9 +151,9 @@ const createSurveys = (options, load)=>{
146
151
  if (remoteEnabled && !disposed) manager?.startAutomaticDisplay?.();
147
152
  }
148
153
  });
149
- await shared.setup(value);
154
+ await shared.setup(value.sharedClient);
150
155
  if (disposed) return void shared.dispose();
151
- teardownSubscription = client.getExtension('analytics')?.onBeforeTeardown?.(()=>shared.handlePageUnload());
156
+ teardownSubscription = client.analytics?.onBeforeTeardown?.(()=>shared.handlePageUnload());
152
157
  if ("u" > typeof window) window.addEventListener('pagehide', pagehide);
153
158
  },
154
159
  getSurveys: (callback, forceReload)=>run(callback, (notify)=>shared.getSurveys(notify, forceReload)),
@@ -215,7 +220,7 @@ const createSurveys = (options, load)=>{
215
220
  disposed = true;
216
221
  elementSelectors.clear();
217
222
  try {
218
- client?.getExtension('autocapture')?.setElementSelectors?.(new Set());
223
+ client?.autocapture?.setElementSelectors(new Set());
219
224
  } catch (error) {
220
225
  client?.logger.error('Failed to clear survey action selectors', error);
221
226
  }
@@ -1,6 +1,7 @@
1
- import type { Disposable, Extension } from '@posthog/browser-common';
1
+ import type { Disposable } from '@posthog/browser-common';
2
+ import type { BrowserExtension } from './browser-client';
2
3
  import type { SurveyCallback, DisplaySurveyOptions, SurveyRenderReason } from './surveys-options';
3
- export interface SurveysExtension extends Extension {
4
+ export interface SurveysExtension extends BrowserExtension {
4
5
  getElementSelectors(): Set<string>;
5
6
  getSurveys(callback: SurveyCallback, forceReload?: boolean): void;
6
7
  getActiveMatchingSurveys(callback: SurveyCallback, forceReload?: boolean): void;
package/dist/surveys.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import type { SurveysOptions } from './surveys-options';
2
2
  import type { SurveysExtension } from './surveys-internal';
3
3
  export type { SurveysExtension } from './surveys-internal';
4
- /** Statically include survey orchestration and rendering, avoiding runtime module loading. */
5
4
  export declare const surveys: (options?: SurveysOptions) => SurveysExtension;
6
5
  export type { SurveysOptions, Survey, SurveyCallback, DisplaySurveyOptions, SurveyRenderReason, } from './surveys-options';
7
6
  export { DisplaySurveyType } from '@posthog/browser-common/surveys';
package/dist/surveys.js CHANGED
@@ -31,12 +31,9 @@ __webpack_require__.d(__webpack_exports__, {
31
31
  DisplaySurveyType: ()=>surveys_namespaceObject.DisplaySurveyType,
32
32
  surveys: ()=>surveys
33
33
  });
34
- const surveys_renderer_namespaceObject = require("@posthog/browser-common/surveys-renderer");
35
34
  const external_surveys_extension_js_namespaceObject = require("./surveys-extension.js");
36
35
  const surveys_namespaceObject = require("@posthog/browser-common/surveys");
37
- const surveys = (options = {})=>(0, external_surveys_extension_js_namespaceObject.createSurveys)(options, async ()=>({
38
- generateSurveys: surveys_renderer_namespaceObject.generateSurveys
39
- }));
36
+ const surveys = (options = {})=>(0, external_surveys_extension_js_namespaceObject.createSurveys)(options, ()=>import("@posthog/browser-common/surveys-renderer"));
40
37
  exports.DisplaySurveyType = __webpack_exports__.DisplaySurveyType;
41
38
  exports.surveys = __webpack_exports__.surveys;
42
39
  for(var __rspack_i in __webpack_exports__)if (-1 === [
package/dist/surveys.mjs CHANGED
@@ -1,7 +1,4 @@
1
- import { generateSurveys } from "@posthog/browser-common/surveys-renderer";
2
1
  import { createSurveys } from "./surveys-extension.mjs";
3
- const surveys = (options = {})=>createSurveys(options, async ()=>({
4
- generateSurveys: generateSurveys
5
- }));
2
+ const surveys = (options = {})=>createSurveys(options, ()=>import("@posthog/browser-common/surveys-renderer"));
6
3
  export { DisplaySurveyType } from "@posthog/browser-common/surveys";
7
4
  export { surveys };
package/dist/types.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import type { FlagsConfiguration } from './flags-options';
2
- import type { BrowserClient } from './browser-client';
2
+ import type { ICoreCapabilities, BrowserExtensionAccess, BrowserExtension as Extension } from './browser-client';
3
3
  import type { LogsConfiguration } from './logs-options';
4
4
  import type { SurveysConfiguration } from './surveys-options';
5
5
  import type { AutocaptureConfiguration } from './autocapture-options';
6
- import type { AnalyticsConfiguration, AnalyticsOptions, AutomaticAnalyticsOptions, LoadStrategy } from './analytics-options';
7
- import type { ApiResponse, CaptureOptions, Disposable, Extension, ExtensionToken, Listener, RemoteConfig, SendRequestInit, SessionContext } from '@posthog/browser-common';
6
+ import type { AnalyticsConfiguration, AnalyticsOptions, AutomaticAnalyticsOptions, LoadStrategy, PersonProfileMode } from './analytics-options';
7
+ import type { ApiResponse, CaptureOptions, Disposable, Listener, RemoteConfig, SendRequestInit, SessionContext } from '@posthog/browser-common';
8
8
  export interface StorageLike {
9
9
  getItem(key: string): string | null;
10
10
  setItem(key: string, value: string): void;
@@ -57,22 +57,22 @@ export interface PostHogOptions {
57
57
  /** Stop waiting for remote configuration after this duration in milliseconds. Defaults to 10,000. */
58
58
  remoteConfigTimeoutMs?: number;
59
59
  /**
60
- * Automatic first-party analytics delivery. Defaults to lazy loading after the first admitted event.
61
- * Pass `false` to retain events without automatically loading delivery.
60
+ * First-party analytics acquisition. Defaults to on-demand loading through resolution, product dependencies,
61
+ * or the initial pageview. Pass `false` to omit implicit analytics; explicit instances take precedence.
62
62
  */
63
63
  analytics?: AnalyticsConfiguration;
64
- /** Automatic DOM autocapture. Pass false to omit it; an explicit extension takes precedence. */
64
+ /** Acquire DOM autocapture from saved/fresh signals, or during initialization when configured explicitly. */
65
65
  autocapture?: AutocaptureConfiguration;
66
66
  /** Dynamically include flags by default. Explicit extensions take precedence over this option. */
67
67
  flags?: FlagsConfiguration;
68
- /** Load survey orchestration dynamically; renderer waits for remote enablement or a manual call. */
68
+ /** Acquire surveys from saved/fresh enablement signals, or during initialization when configured explicitly. */
69
69
  surveys?: SurveysConfiguration;
70
- /** Automatically load logs. False disables automatic inclusion; explicit extensions take precedence. */
70
+ /** Acquire logs from saved/fresh console signals, or during initialization when configured explicitly. */
71
71
  logs?: LogsConfiguration;
72
- /** Install extensions before the factory resolves. A preinstalled analytics extension satisfies delivery. */
72
+ /** Install extensions before the factory resolves. Explicit instances take precedence over automatic product options. */
73
73
  extensions?: readonly Extension[];
74
74
  }
75
- /** Options for the delivery-free `@posthog/browser/core` entrypoint. */
75
+ /** Options for the manually composed `@posthog/browser/core` entrypoint. */
76
76
  export type CorePostHogOptions = Omit<PostHogOptions, 'analytics' | 'flags' | 'logs' | 'surveys' | 'autocapture'>;
77
77
  /** Capture V1's terminal verdict for one reported event. */
78
78
  export type CaptureOutcomeStatus = 'ok' | 'warning' | 'drop' | 'retry';
@@ -94,21 +94,15 @@ export interface CaptureSummary {
94
94
  /** Operation-level failure, with any known partial results retained. Errors always set `allPersisted` to false. */
95
95
  readonly error?: Error;
96
96
  }
97
- export interface PostHog extends BrowserClient, Disposable {
97
+ /** Consumer-facing SDK contract; product controls use their public interfaces. */
98
+ export interface IPostHog extends ICoreCapabilities, BrowserExtensionAccess, Disposable {
98
99
  readonly onNewSession: Listener<NewSessionInfo>;
99
- /** Sends one finalized event inline. Resolves with an outcome or `summary.error` instead of rejecting. */
100
- captureImmediate(event: string, properties?: Record<string, unknown> | null, options?: CaptureOptions): Promise<CaptureSummary>;
101
- identify(distinctId: string, set?: Record<string, unknown>, setOnce?: Record<string, unknown>): Promise<void>;
102
- group(type: string, key: string, properties?: Record<string, unknown>): Promise<void>;
103
- reset(): void;
104
100
  flush(): Promise<void>;
105
101
  shutdown(shutdownTimeoutMs?: number): Promise<void>;
106
102
  dispose(): Promise<void>;
107
103
  optIn(): void;
108
104
  optOut(): void;
109
105
  hasOptedOut(): boolean;
110
- getExtension<T extends Extension>(token: ExtensionToken<T>): T | undefined;
111
- getExtension<T extends Extension = Extension>(name: string): T | undefined;
112
106
  getRemoteConfig(): Promise<RemoteConfig | undefined>;
113
107
  }
114
- export type { AnalyticsConfiguration, AnalyticsOptions, AutomaticAnalyticsOptions, LoadStrategy, ApiResponse, CaptureOptions, Disposable, Extension, RemoteConfig, SendRequestInit, SessionContext, };
108
+ export type { AnalyticsConfiguration, AnalyticsOptions, AutomaticAnalyticsOptions, LoadStrategy, PersonProfileMode, ApiResponse, CaptureOptions, Disposable, Extension, RemoteConfig, SendRequestInit, SessionContext, };
package/dist/utf8.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare const utf8Bytes: (value: string) => number;
package/dist/utf8.js ADDED
@@ -0,0 +1,52 @@
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 utf8Bytes = (value)=>{
31
+ let bytes = 0;
32
+ for(let index = 0; index < value.length; index++){
33
+ const code = value.charCodeAt(index);
34
+ if (code < 0x80) bytes++;
35
+ else if (code < 0x800) bytes += 2;
36
+ else if (code >= 0xd800 && code <= 0xdbff && value.charCodeAt(index + 1) >= 0xdc00) {
37
+ bytes += 4;
38
+ index++;
39
+ } else bytes += 3;
40
+ }
41
+ return bytes;
42
+ };
43
+ __webpack_require__.d(__webpack_exports__, {}, {
44
+ utf8Bytes: utf8Bytes
45
+ });
46
+ exports.utf8Bytes = __webpack_exports__.utf8Bytes;
47
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
48
+ "utf8Bytes"
49
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
50
+ Object.defineProperty(exports, '__esModule', {
51
+ value: true
52
+ });