@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.
Files changed (73) hide show
  1. package/ARCHITECTURE.md +28 -22
  2. package/README.md +94 -29
  3. package/dist/analytics-api.d.ts +32 -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 +87 -0
  10. package/dist/analytics-extension.js +649 -0
  11. package/dist/analytics-extension.mjs +611 -0
  12. package/dist/analytics-internal.d.ts +33 -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-properties.d.ts +12 -0
  17. package/dist/analytics-properties.js +88 -0
  18. package/dist/analytics-properties.mjs +46 -0
  19. package/dist/analytics-session-properties.d.ts +12 -0
  20. package/dist/analytics-session-properties.js +84 -0
  21. package/dist/analytics-session-properties.mjs +46 -0
  22. package/dist/analytics.d.ts +4 -3
  23. package/dist/autocapture.d.ts +2 -2
  24. package/dist/autocapture.js +21 -10
  25. package/dist/autocapture.mjs +21 -10
  26. package/dist/automatic-analytics.d.ts +2 -2
  27. package/dist/browser-client.d.ts +44 -4
  28. package/dist/capture-properties.d.ts +15 -0
  29. package/dist/capture-properties.js +101 -0
  30. package/dist/capture-properties.mjs +57 -0
  31. package/dist/capture-v1.js +7 -19
  32. package/dist/capture-v1.mjs +1 -13
  33. package/dist/core.d.ts +7 -5
  34. package/dist/extensions/registry.d.ts +42 -7
  35. package/dist/extensions/registry.js +194 -37
  36. package/dist/extensions/registry.mjs +194 -37
  37. package/dist/flags-token.d.ts +4 -4
  38. package/dist/flags.js +15 -2
  39. package/dist/flags.mjs +15 -2
  40. package/dist/immutable.d.ts +1 -0
  41. package/dist/immutable.js +46 -0
  42. package/dist/immutable.mjs +8 -0
  43. package/dist/index.d.ts +7 -5
  44. package/dist/index.js +3 -67
  45. package/dist/index.mjs +3 -67
  46. package/dist/key-value-store.d.ts +21 -0
  47. package/dist/key-value-store.js +117 -0
  48. package/dist/key-value-store.mjs +72 -0
  49. package/dist/logs-internal.d.ts +2 -2
  50. package/dist/logs.js +5 -2
  51. package/dist/logs.mjs +5 -2
  52. package/dist/person-processing.d.ts +11 -0
  53. package/dist/person-processing.js +84 -0
  54. package/dist/person-processing.mjs +37 -0
  55. package/dist/posthog.d.ts +4 -3
  56. package/dist/posthog.js +154 -353
  57. package/dist/posthog.mjs +154 -353
  58. package/dist/root-composition.d.ts +11 -0
  59. package/dist/root-composition.js +138 -0
  60. package/dist/root-composition.mjs +100 -0
  61. package/dist/state.d.ts +6 -0
  62. package/dist/state.js +50 -56
  63. package/dist/state.mjs +50 -56
  64. package/dist/surveys-extension.js +11 -6
  65. package/dist/surveys-extension.mjs +11 -6
  66. package/dist/surveys-internal.d.ts +3 -2
  67. package/dist/types.d.ts +13 -19
  68. package/dist/utf8.d.ts +1 -0
  69. package/dist/utf8.js +52 -0
  70. package/dist/utf8.mjs +14 -0
  71. package/dist/version.js +1 -1
  72. package/dist/version.mjs +1 -1
  73. package/package.json +3 -3
@@ -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/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
+ });
package/dist/utf8.mjs ADDED
@@ -0,0 +1,14 @@
1
+ const utf8Bytes = (value)=>{
2
+ let bytes = 0;
3
+ for(let index = 0; index < value.length; index++){
4
+ const code = value.charCodeAt(index);
5
+ if (code < 0x80) bytes++;
6
+ else if (code < 0x800) bytes += 2;
7
+ else if (code >= 0xd800 && code <= 0xdbff && value.charCodeAt(index + 1) >= 0xdc00) {
8
+ bytes += 4;
9
+ index++;
10
+ } else bytes += 3;
11
+ }
12
+ return bytes;
13
+ };
14
+ export { utf8Bytes };
package/dist/version.js CHANGED
@@ -27,7 +27,7 @@ var __webpack_require__ = {};
27
27
  })();
28
28
  var __webpack_exports__ = {};
29
29
  __webpack_require__.r(__webpack_exports__);
30
- const version = "0.1.1";
30
+ const version = "0.3.0";
31
31
  __webpack_require__.d(__webpack_exports__, {}, {
32
32
  version: version
33
33
  });
package/dist/version.mjs CHANGED
@@ -1,2 +1,2 @@
1
- const version = "0.1.1";
1
+ const version = "0.3.0";
2
2
  export { version };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@posthog/browser",
3
- "version": "0.1.1",
3
+ "version": "0.3.0",
4
4
  "description": "A small, extension-based PostHog client for modern browsers",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -55,8 +55,8 @@
55
55
  }
56
56
  },
57
57
  "dependencies": {
58
- "@posthog/browser-common": "^0.10.6",
59
- "@posthog/types": "^1.419.0"
58
+ "@posthog/browser-common": "^0.11.0",
59
+ "@posthog/types": "^1.425.1"
60
60
  },
61
61
  "devDependencies": {
62
62
  "@playwright/test": "1.52.0",