@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
@@ -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 };
@@ -1,6 +1,6 @@
1
- import type { Extension } from '@posthog/browser-common';
1
+ import type { BrowserExtension } from './browser-client';
2
2
  import type { CaptureLogOptions } from '@posthog/types';
3
- export interface LogsExtension extends Extension {
3
+ export interface LogsExtension extends BrowserExtension {
4
4
  captureLog(options: CaptureLogOptions): void;
5
5
  flush(): Promise<void>;
6
6
  reset(): void;
package/dist/logs.js CHANGED
@@ -91,15 +91,18 @@ const logs = (options = {})=>{
91
91
  };
92
92
  return {
93
93
  name: 'logs',
94
+ bindings: {
95
+ logs: shared
96
+ },
94
97
  setup: async (value)=>{
95
- environment.client = value;
98
+ environment.client = value.sharedClient;
96
99
  try {
97
100
  environment.browserWindow = globalThis.window;
98
101
  } catch {}
99
102
  subscriptions.push(value.onReset(()=>shared.reset()), value.onConsentChange(({ current })=>{
100
103
  if ('denied' === current) shared.reset();
101
104
  }));
102
- await shared.setup(value);
105
+ await shared.setup(value.sharedClient);
103
106
  if (environment.disposed) return;
104
107
  environment.browserWindow?.addEventListener('pagehide', pagehide);
105
108
  },
package/dist/logs.mjs CHANGED
@@ -59,15 +59,18 @@ const logs = (options = {})=>{
59
59
  };
60
60
  return {
61
61
  name: 'logs',
62
+ bindings: {
63
+ logs: shared
64
+ },
62
65
  setup: async (value)=>{
63
- environment.client = value;
66
+ environment.client = value.sharedClient;
64
67
  try {
65
68
  environment.browserWindow = globalThis.window;
66
69
  } catch {}
67
70
  subscriptions.push(value.onReset(()=>shared.reset()), value.onConsentChange(({ current })=>{
68
71
  if ('denied' === current) shared.reset();
69
72
  }));
70
- await shared.setup(value);
73
+ await shared.setup(value.sharedClient);
71
74
  if (environment.disposed) return;
72
75
  environment.browserWindow?.addEventListener('pagehide', pagehide);
73
76
  },
@@ -0,0 +1,11 @@
1
+ import type { PersonProfileMode } from './analytics-options';
2
+ export declare const snapshotPersonProfileMode: (read: () => unknown) => PersonProfileMode;
3
+ export declare const shouldProcessPerson: (mode: PersonProfileMode, identified: boolean, grouped: boolean, explicitlyEnabled: boolean) => boolean;
4
+ export interface PersonProperties {
5
+ readonly signature: string;
6
+ readonly set: Record<string, unknown>;
7
+ readonly setOnce: Record<string, unknown>;
8
+ }
9
+ /** Snapshot caller-controlled values before local flag state or analytics state consumes them. */
10
+ export declare const snapshotPersonProperties: (distinctId: string, set: Record<string, unknown> | undefined, setOnce: Record<string, unknown> | undefined) => PersonProperties | undefined;
11
+ export declare const personPropertyNames: (value: string | readonly string[]) => string[];
@@ -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
+ const snapshotPersonProfileMode = (read)=>{
31
+ try {
32
+ const value = read();
33
+ if ('always' === value || 'never' === value) return value;
34
+ } catch {}
35
+ return 'identified_only';
36
+ };
37
+ const shouldProcessPerson = (mode, identified, grouped, explicitlyEnabled)=>'always' === mode || 'never' !== mode && (identified || grouped || explicitlyEnabled);
38
+ const isRecord = (value)=>null !== value && 'object' == typeof value && !Array.isArray(value);
39
+ const snapshotPersonProperties = (distinctId, set, setOnce)=>{
40
+ try {
41
+ const signature = JSON.stringify([
42
+ distinctId,
43
+ set ?? {},
44
+ setOnce ?? {}
45
+ ]);
46
+ const values = JSON.parse(signature);
47
+ if (!Array.isArray(values) || !isRecord(values[1]) || !isRecord(values[2])) return;
48
+ return {
49
+ signature,
50
+ set: values[1],
51
+ setOnce: values[2]
52
+ };
53
+ } catch {
54
+ return;
55
+ }
56
+ };
57
+ const personPropertyNames = (value)=>{
58
+ try {
59
+ return (Array.isArray(value) ? value : [
60
+ value
61
+ ]).filter((name)=>'string' == typeof name && name.length > 0);
62
+ } catch {
63
+ return [];
64
+ }
65
+ };
66
+ __webpack_require__.d(__webpack_exports__, {}, {
67
+ personPropertyNames: personPropertyNames,
68
+ shouldProcessPerson: shouldProcessPerson,
69
+ snapshotPersonProfileMode: snapshotPersonProfileMode,
70
+ snapshotPersonProperties: snapshotPersonProperties
71
+ });
72
+ exports.personPropertyNames = __webpack_exports__.personPropertyNames;
73
+ exports.shouldProcessPerson = __webpack_exports__.shouldProcessPerson;
74
+ exports.snapshotPersonProfileMode = __webpack_exports__.snapshotPersonProfileMode;
75
+ exports.snapshotPersonProperties = __webpack_exports__.snapshotPersonProperties;
76
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
77
+ "personPropertyNames",
78
+ "shouldProcessPerson",
79
+ "snapshotPersonProfileMode",
80
+ "snapshotPersonProperties"
81
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
82
+ Object.defineProperty(exports, '__esModule', {
83
+ value: true
84
+ });
@@ -0,0 +1,37 @@
1
+ const snapshotPersonProfileMode = (read)=>{
2
+ try {
3
+ const value = read();
4
+ if ('always' === value || 'never' === value) return value;
5
+ } catch {}
6
+ return 'identified_only';
7
+ };
8
+ const shouldProcessPerson = (mode, identified, grouped, explicitlyEnabled)=>'always' === mode || 'never' !== mode && (identified || grouped || explicitlyEnabled);
9
+ const isRecord = (value)=>null !== value && 'object' == typeof value && !Array.isArray(value);
10
+ const snapshotPersonProperties = (distinctId, set, setOnce)=>{
11
+ try {
12
+ const signature = JSON.stringify([
13
+ distinctId,
14
+ set ?? {},
15
+ setOnce ?? {}
16
+ ]);
17
+ const values = JSON.parse(signature);
18
+ if (!Array.isArray(values) || !isRecord(values[1]) || !isRecord(values[2])) return;
19
+ return {
20
+ signature,
21
+ set: values[1],
22
+ setOnce: values[2]
23
+ };
24
+ } catch {
25
+ return;
26
+ }
27
+ };
28
+ const personPropertyNames = (value)=>{
29
+ try {
30
+ return (Array.isArray(value) ? value : [
31
+ value
32
+ ]).filter((name)=>'string' == typeof name && name.length > 0);
33
+ } catch {
34
+ return [];
35
+ }
36
+ };
37
+ export { personPropertyNames, shouldProcessPerson, snapshotPersonProfileMode, snapshotPersonProperties };
package/dist/posthog.d.ts CHANGED
@@ -1,3 +1,4 @@
1
- import { type Extension } from '@posthog/browser-common';
2
- import type { CorePostHogOptions, PostHog } from './types';
3
- export declare const createPostHogCore: (options: CorePostHogOptions, configured?: readonly Extension[]) => Promise<PostHog>;
1
+ import type { IPostHogInternal, BrowserExtension as Extension } from './browser-client';
2
+ import { type AutomaticExtension, type ExtensionLoader } from './extensions/registry';
3
+ import type { CorePostHogOptions, IPostHog } from './types';
4
+ export declare const createPostHogCore: (options: CorePostHogOptions, configured?: readonly Extension[], automaticLoaders?: Readonly<Record<string, ExtensionLoader<IPostHogInternal>>>, automatic?: readonly AutomaticExtension<IPostHogInternal>[]) => Promise<IPostHog>;