@posthog/core 1.0.2 → 1.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 (204) hide show
  1. package/dist/error-tracking/chunk-ids.d.ts +5 -0
  2. package/dist/error-tracking/chunk-ids.d.ts.map +1 -0
  3. package/dist/error-tracking/chunk-ids.js +68 -0
  4. package/dist/error-tracking/chunk-ids.mjs +34 -0
  5. package/dist/error-tracking/coercers/dom-exception-coercer.d.ts +10 -0
  6. package/dist/error-tracking/coercers/dom-exception-coercer.d.ts.map +1 -0
  7. package/dist/error-tracking/coercers/dom-exception-coercer.js +65 -0
  8. package/dist/error-tracking/coercers/dom-exception-coercer.mjs +31 -0
  9. package/dist/error-tracking/coercers/error-coercer.d.ts +9 -0
  10. package/dist/error-tracking/coercers/error-coercer.d.ts.map +1 -0
  11. package/dist/error-tracking/coercers/error-coercer.js +61 -0
  12. package/dist/error-tracking/coercers/error-coercer.mjs +27 -0
  13. package/dist/error-tracking/coercers/error-event-coercer.d.ts +7 -0
  14. package/dist/error-tracking/coercers/error-event-coercer.d.ts.map +1 -0
  15. package/dist/error-tracking/coercers/error-event-coercer.js +52 -0
  16. package/dist/error-tracking/coercers/error-event-coercer.mjs +18 -0
  17. package/dist/error-tracking/coercers/event-coercer.d.ts +6 -0
  18. package/dist/error-tracking/coercers/event-coercer.d.ts.map +1 -0
  19. package/dist/error-tracking/coercers/event-coercer.js +51 -0
  20. package/dist/error-tracking/coercers/event-coercer.mjs +17 -0
  21. package/dist/error-tracking/coercers/index.d.ts +9 -0
  22. package/dist/error-tracking/coercers/index.d.ts.map +1 -0
  23. package/dist/error-tracking/coercers/index.js +123 -0
  24. package/dist/error-tracking/coercers/index.mjs +8 -0
  25. package/dist/error-tracking/coercers/object-coercer.d.ts +14 -0
  26. package/dist/error-tracking/coercers/object-coercer.d.ts.map +1 -0
  27. package/dist/error-tracking/coercers/object-coercer.js +85 -0
  28. package/dist/error-tracking/coercers/object-coercer.mjs +51 -0
  29. package/dist/error-tracking/coercers/primitive-coercer.d.ts +7 -0
  30. package/dist/error-tracking/coercers/primitive-coercer.d.ts.map +1 -0
  31. package/dist/error-tracking/coercers/primitive-coercer.js +49 -0
  32. package/dist/error-tracking/coercers/primitive-coercer.mjs +15 -0
  33. package/dist/error-tracking/coercers/promise-rejection-event.d.ts +7 -0
  34. package/dist/error-tracking/coercers/promise-rejection-event.d.ts.map +1 -0
  35. package/dist/error-tracking/coercers/promise-rejection-event.js +59 -0
  36. package/dist/error-tracking/coercers/promise-rejection-event.mjs +25 -0
  37. package/dist/error-tracking/coercers/string-coercer.d.ts +7 -0
  38. package/dist/error-tracking/coercers/string-coercer.d.ts.map +1 -0
  39. package/dist/error-tracking/coercers/string-coercer.js +63 -0
  40. package/dist/error-tracking/coercers/string-coercer.mjs +29 -0
  41. package/dist/error-tracking/coercers/utils.d.ts +8 -0
  42. package/dist/error-tracking/coercers/utils.d.ts.map +1 -0
  43. package/dist/error-tracking/coercers/utils.js +55 -0
  44. package/dist/error-tracking/coercers/utils.mjs +18 -0
  45. package/dist/error-tracking/error-properties-builder.d.ts +18 -0
  46. package/dist/error-tracking/error-properties-builder.d.ts.map +1 -0
  47. package/dist/error-tracking/error-properties-builder.js +152 -0
  48. package/dist/error-tracking/error-properties-builder.mjs +118 -0
  49. package/dist/error-tracking/index.d.ts +6 -0
  50. package/dist/error-tracking/index.d.ts.map +1 -0
  51. package/dist/error-tracking/index.js +87 -0
  52. package/dist/error-tracking/index.mjs +4 -0
  53. package/dist/error-tracking/parsers/base.d.ts +9 -0
  54. package/dist/error-tracking/parsers/base.d.ts.map +1 -0
  55. package/dist/error-tracking/parsers/base.js +71 -0
  56. package/dist/error-tracking/parsers/base.mjs +19 -0
  57. package/dist/error-tracking/parsers/chrome.d.ts +3 -0
  58. package/dist/error-tracking/parsers/chrome.d.ts.map +1 -0
  59. package/dist/error-tracking/parsers/chrome.js +61 -0
  60. package/dist/error-tracking/parsers/chrome.mjs +27 -0
  61. package/dist/error-tracking/parsers/gecko.d.ts +3 -0
  62. package/dist/error-tracking/parsers/gecko.d.ts.map +1 -0
  63. package/dist/error-tracking/parsers/gecko.js +58 -0
  64. package/dist/error-tracking/parsers/gecko.mjs +24 -0
  65. package/dist/error-tracking/parsers/index.d.ts +9 -0
  66. package/dist/error-tracking/parsers/index.d.ts.map +1 -0
  67. package/dist/error-tracking/parsers/index.js +99 -0
  68. package/dist/error-tracking/parsers/index.mjs +44 -0
  69. package/dist/error-tracking/parsers/node.d.ts +3 -0
  70. package/dist/error-tracking/parsers/node.d.ts.map +1 -0
  71. package/dist/error-tracking/parsers/node.js +99 -0
  72. package/dist/error-tracking/parsers/node.mjs +65 -0
  73. package/dist/error-tracking/parsers/opera.d.ts +4 -0
  74. package/dist/error-tracking/parsers/opera.d.ts.map +1 -0
  75. package/dist/error-tracking/parsers/opera.js +49 -0
  76. package/dist/error-tracking/parsers/opera.mjs +12 -0
  77. package/dist/error-tracking/parsers/react-native.d.ts +1 -0
  78. package/dist/error-tracking/parsers/react-native.d.ts.map +1 -0
  79. package/dist/error-tracking/parsers/react-native.js +5 -0
  80. package/dist/error-tracking/parsers/react-native.mjs +0 -0
  81. package/dist/error-tracking/parsers/safari.d.ts +22 -0
  82. package/dist/error-tracking/parsers/safari.d.ts.map +1 -0
  83. package/dist/error-tracking/parsers/safari.js +47 -0
  84. package/dist/error-tracking/parsers/safari.mjs +13 -0
  85. package/dist/error-tracking/parsers/winjs.d.ts +3 -0
  86. package/dist/error-tracking/parsers/winjs.d.ts.map +1 -0
  87. package/dist/error-tracking/parsers/winjs.js +41 -0
  88. package/dist/error-tracking/parsers/winjs.mjs +7 -0
  89. package/dist/error-tracking/types.d.ts +87 -0
  90. package/dist/error-tracking/types.d.ts.map +1 -0
  91. package/dist/error-tracking/types.js +43 -0
  92. package/dist/error-tracking/types.mjs +9 -0
  93. package/dist/error-tracking/utils.d.ts +13 -0
  94. package/dist/error-tracking/utils.d.ts.map +1 -0
  95. package/dist/error-tracking/utils.js +57 -0
  96. package/dist/error-tracking/utils.mjs +23 -0
  97. package/dist/eventemitter.js +4 -4
  98. package/dist/eventemitter.mjs +4 -4
  99. package/dist/featureFlagUtils.js +20 -45
  100. package/dist/featureFlagUtils.mjs +20 -45
  101. package/dist/gzip.js +1 -2
  102. package/dist/gzip.mjs +1 -2
  103. package/dist/index.d.ts +5 -255
  104. package/dist/index.d.ts.map +1 -1
  105. package/dist/index.js +61 -1223
  106. package/dist/index.mjs +6 -1190
  107. package/dist/posthog-core-stateless.d.ts +204 -0
  108. package/dist/posthog-core-stateless.d.ts.map +1 -0
  109. package/dist/posthog-core-stateless.js +675 -0
  110. package/dist/posthog-core-stateless.mjs +632 -0
  111. package/dist/posthog-core.d.ts +171 -0
  112. package/dist/posthog-core.d.ts.map +1 -0
  113. package/dist/posthog-core.js +554 -0
  114. package/dist/posthog-core.mjs +520 -0
  115. package/dist/testing/PostHogCoreTestClient.d.ts +2 -1
  116. package/dist/testing/PostHogCoreTestClient.d.ts.map +1 -1
  117. package/dist/testing/PostHogCoreTestClient.js +9 -11
  118. package/dist/testing/PostHogCoreTestClient.mjs +8 -10
  119. package/dist/testing/test-utils.d.ts +2 -0
  120. package/dist/testing/test-utils.d.ts.map +1 -1
  121. package/dist/testing/test-utils.js +13 -1
  122. package/dist/testing/test-utils.mjs +11 -2
  123. package/dist/utils/bucketed-rate-limiter.d.ts.map +1 -1
  124. package/dist/utils/bucketed-rate-limiter.js +8 -12
  125. package/dist/utils/bucketed-rate-limiter.mjs +8 -12
  126. package/dist/utils/index.js +3 -3
  127. package/dist/utils/index.mjs +3 -3
  128. package/dist/utils/number-utils.d.ts.map +1 -1
  129. package/dist/utils/type-utils.d.ts +8 -1
  130. package/dist/utils/type-utils.d.ts.map +1 -1
  131. package/dist/utils/type-utils.js +61 -6
  132. package/dist/utils/type-utils.mjs +36 -2
  133. package/dist/vendor/uuidv7.js +12 -16
  134. package/dist/vendor/uuidv7.mjs +12 -16
  135. package/package.json +10 -4
  136. package/src/__tests__/featureFlagUtils.spec.ts +427 -0
  137. package/src/__tests__/gzip.spec.ts +69 -0
  138. package/src/__tests__/posthog.ai.spec.ts +110 -0
  139. package/src/__tests__/posthog.capture.spec.ts +91 -0
  140. package/src/__tests__/posthog.core.spec.ts +135 -0
  141. package/src/__tests__/posthog.debug.spec.ts +36 -0
  142. package/src/__tests__/posthog.enqueue.spec.ts +93 -0
  143. package/src/__tests__/posthog.featureflags.spec.ts +1106 -0
  144. package/src/__tests__/posthog.featureflags.v1.spec.ts +922 -0
  145. package/src/__tests__/posthog.flush.spec.ts +237 -0
  146. package/src/__tests__/posthog.gdpr.spec.ts +50 -0
  147. package/src/__tests__/posthog.groups.spec.ts +96 -0
  148. package/src/__tests__/posthog.identify.spec.ts +194 -0
  149. package/src/__tests__/posthog.init.spec.ts +110 -0
  150. package/src/__tests__/posthog.listeners.spec.ts +51 -0
  151. package/src/__tests__/posthog.register.spec.ts +47 -0
  152. package/src/__tests__/posthog.reset.spec.ts +76 -0
  153. package/src/__tests__/posthog.sessions.spec.ts +63 -0
  154. package/src/__tests__/posthog.setProperties.spec.ts +102 -0
  155. package/src/__tests__/posthog.shutdown.spec.ts +88 -0
  156. package/src/__tests__/utils.spec.ts +36 -0
  157. package/src/error-tracking/chunk-ids.ts +58 -0
  158. package/src/error-tracking/coercers/dom-exception-coercer.ts +38 -0
  159. package/src/error-tracking/coercers/error-coercer.ts +36 -0
  160. package/src/error-tracking/coercers/error-event-coercer.ts +24 -0
  161. package/src/error-tracking/coercers/event-coercer.ts +19 -0
  162. package/src/error-tracking/coercers/index.ts +8 -0
  163. package/src/error-tracking/coercers/object-coercer.ts +76 -0
  164. package/src/error-tracking/coercers/primitive-coercer.ts +19 -0
  165. package/src/error-tracking/coercers/promise-rejection-event.spec.ts +77 -0
  166. package/src/error-tracking/coercers/promise-rejection-event.ts +53 -0
  167. package/src/error-tracking/coercers/string-coercer.spec.ts +26 -0
  168. package/src/error-tracking/coercers/string-coercer.ts +31 -0
  169. package/src/error-tracking/coercers/utils.ts +33 -0
  170. package/src/error-tracking/error-properties-builder.coerce.spec.ts +202 -0
  171. package/src/error-tracking/error-properties-builder.parse.spec.ts +30 -0
  172. package/src/error-tracking/error-properties-builder.ts +169 -0
  173. package/src/error-tracking/index.ts +5 -0
  174. package/src/error-tracking/parsers/base.ts +29 -0
  175. package/src/error-tracking/parsers/chrome.ts +53 -0
  176. package/src/error-tracking/parsers/gecko.ts +38 -0
  177. package/src/error-tracking/parsers/index.ts +104 -0
  178. package/src/error-tracking/parsers/node.ts +111 -0
  179. package/src/error-tracking/parsers/opera.ts +18 -0
  180. package/src/error-tracking/parsers/react-native.ts +0 -0
  181. package/src/error-tracking/parsers/safari.ts +33 -0
  182. package/src/error-tracking/parsers/winjs.ts +12 -0
  183. package/src/error-tracking/types.ts +107 -0
  184. package/src/error-tracking/utils.ts +39 -0
  185. package/src/eventemitter.ts +27 -0
  186. package/src/featureFlagUtils.ts +192 -0
  187. package/src/gzip.ts +29 -0
  188. package/src/index.ts +8 -0
  189. package/src/posthog-core-stateless.ts +1226 -0
  190. package/src/posthog-core.ts +958 -0
  191. package/src/testing/PostHogCoreTestClient.ts +91 -0
  192. package/src/testing/index.ts +2 -0
  193. package/src/testing/test-utils.ts +47 -0
  194. package/src/types.ts +544 -0
  195. package/src/utils/bucketed-rate-limiter.spec.ts +33 -0
  196. package/src/utils/bucketed-rate-limiter.ts +85 -0
  197. package/src/utils/index.ts +98 -0
  198. package/src/utils/number-utils.spec.ts +89 -0
  199. package/src/utils/number-utils.ts +30 -0
  200. package/src/utils/promise-queue.spec.ts +55 -0
  201. package/src/utils/promise-queue.ts +30 -0
  202. package/src/utils/string-utils.ts +23 -0
  203. package/src/utils/type-utils.ts +134 -0
  204. package/src/vendor/uuidv7.ts +479 -0
@@ -0,0 +1,554 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ PostHogCore: ()=>PostHogCore
28
+ });
29
+ const external_featureFlagUtils_js_namespaceObject = require("./featureFlagUtils.js");
30
+ const external_types_js_namespaceObject = require("./types.js");
31
+ const external_posthog_core_stateless_js_namespaceObject = require("./posthog-core-stateless.js");
32
+ const uuidv7_js_namespaceObject = require("./vendor/uuidv7.js");
33
+ const index_js_namespaceObject = require("./utils/index.js");
34
+ class PostHogCore extends external_posthog_core_stateless_js_namespaceObject.PostHogCoreStateless {
35
+ constructor(apiKey, options){
36
+ const disableGeoipOption = options?.disableGeoip ?? false;
37
+ const featureFlagsRequestTimeoutMs = options?.featureFlagsRequestTimeoutMs ?? 10000;
38
+ super(apiKey, {
39
+ ...options,
40
+ disableGeoip: disableGeoipOption,
41
+ featureFlagsRequestTimeoutMs
42
+ }), this.flagCallReported = {}, this._sessionMaxLengthSeconds = 86400, this.sessionProps = {};
43
+ this.sendFeatureFlagEvent = options?.sendFeatureFlagEvent ?? true;
44
+ this._sessionExpirationTimeSeconds = options?.sessionExpirationTimeSeconds ?? 1800;
45
+ }
46
+ setupBootstrap(options) {
47
+ const bootstrap = options?.bootstrap;
48
+ if (!bootstrap) return;
49
+ if (bootstrap.distinctId) if (bootstrap.isIdentifiedId) {
50
+ const distinctId = this.getPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.DistinctId);
51
+ if (!distinctId) this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.DistinctId, bootstrap.distinctId);
52
+ } else {
53
+ const anonymousId = this.getPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.AnonymousId);
54
+ if (!anonymousId) this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.AnonymousId, bootstrap.distinctId);
55
+ }
56
+ const bootstrapFeatureFlags = bootstrap.featureFlags;
57
+ const bootstrapFeatureFlagPayloads = bootstrap.featureFlagPayloads ?? {};
58
+ if (bootstrapFeatureFlags && Object.keys(bootstrapFeatureFlags).length) {
59
+ const normalizedBootstrapFeatureFlagDetails = (0, external_featureFlagUtils_js_namespaceObject.createFlagsResponseFromFlagsAndPayloads)(bootstrapFeatureFlags, bootstrapFeatureFlagPayloads);
60
+ if (Object.keys(normalizedBootstrapFeatureFlagDetails.flags).length > 0) {
61
+ this.setBootstrappedFeatureFlagDetails(normalizedBootstrapFeatureFlagDetails);
62
+ const currentFeatureFlagDetails = this.getKnownFeatureFlagDetails() || {
63
+ flags: {},
64
+ requestId: void 0
65
+ };
66
+ const newFeatureFlagDetails = {
67
+ flags: {
68
+ ...normalizedBootstrapFeatureFlagDetails.flags,
69
+ ...currentFeatureFlagDetails.flags
70
+ },
71
+ requestId: normalizedBootstrapFeatureFlagDetails.requestId
72
+ };
73
+ this.setKnownFeatureFlagDetails(newFeatureFlagDetails);
74
+ }
75
+ }
76
+ }
77
+ clearProps() {
78
+ this.props = void 0;
79
+ this.sessionProps = {};
80
+ this.flagCallReported = {};
81
+ }
82
+ on(event, cb) {
83
+ return this._events.on(event, cb);
84
+ }
85
+ reset(propertiesToKeep) {
86
+ this.wrap(()=>{
87
+ const allPropertiesToKeep = [
88
+ external_types_js_namespaceObject.PostHogPersistedProperty.Queue,
89
+ ...propertiesToKeep || []
90
+ ];
91
+ this.clearProps();
92
+ for (const key of Object.keys(external_types_js_namespaceObject.PostHogPersistedProperty))if (!allPropertiesToKeep.includes(external_types_js_namespaceObject.PostHogPersistedProperty[key])) this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty[key], null);
93
+ this.reloadFeatureFlags();
94
+ });
95
+ }
96
+ getCommonEventProperties() {
97
+ const featureFlags = this.getFeatureFlags();
98
+ const featureVariantProperties = {};
99
+ if (featureFlags) for (const [feature, variant] of Object.entries(featureFlags))featureVariantProperties[`$feature/${feature}`] = variant;
100
+ return {
101
+ ...(0, external_posthog_core_stateless_js_namespaceObject.maybeAdd)('$active_feature_flags', featureFlags ? Object.keys(featureFlags) : void 0),
102
+ ...featureVariantProperties,
103
+ ...super.getCommonEventProperties()
104
+ };
105
+ }
106
+ enrichProperties(properties) {
107
+ return {
108
+ ...this.props,
109
+ ...this.sessionProps,
110
+ ...properties || {},
111
+ ...this.getCommonEventProperties(),
112
+ $session_id: this.getSessionId()
113
+ };
114
+ }
115
+ getSessionId() {
116
+ if (!this._isInitialized) return '';
117
+ let sessionId = this.getPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.SessionId);
118
+ const sessionLastTimestamp = this.getPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.SessionLastTimestamp) || 0;
119
+ const sessionStartTimestamp = this.getPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.SessionStartTimestamp) || 0;
120
+ const now = Date.now();
121
+ const sessionLastDif = now - sessionLastTimestamp;
122
+ const sessionStartDif = now - sessionStartTimestamp;
123
+ if (!sessionId || sessionLastDif > 1000 * this._sessionExpirationTimeSeconds || sessionStartDif > 1000 * this._sessionMaxLengthSeconds) {
124
+ sessionId = (0, uuidv7_js_namespaceObject.uuidv7)();
125
+ this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.SessionId, sessionId);
126
+ this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.SessionStartTimestamp, now);
127
+ }
128
+ this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.SessionLastTimestamp, now);
129
+ return sessionId;
130
+ }
131
+ resetSessionId() {
132
+ this.wrap(()=>{
133
+ this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.SessionId, null);
134
+ this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.SessionLastTimestamp, null);
135
+ this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.SessionStartTimestamp, null);
136
+ });
137
+ }
138
+ getAnonymousId() {
139
+ if (!this._isInitialized) return '';
140
+ let anonId = this.getPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.AnonymousId);
141
+ if (!anonId) {
142
+ anonId = (0, uuidv7_js_namespaceObject.uuidv7)();
143
+ this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.AnonymousId, anonId);
144
+ }
145
+ return anonId;
146
+ }
147
+ getDistinctId() {
148
+ if (!this._isInitialized) return '';
149
+ return this.getPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.DistinctId) || this.getAnonymousId();
150
+ }
151
+ registerForSession(properties) {
152
+ this.sessionProps = {
153
+ ...this.sessionProps,
154
+ ...properties
155
+ };
156
+ }
157
+ unregisterForSession(property) {
158
+ delete this.sessionProps[property];
159
+ }
160
+ identify(distinctId, properties, options) {
161
+ this.wrap(()=>{
162
+ const previousDistinctId = this.getDistinctId();
163
+ distinctId = distinctId || previousDistinctId;
164
+ if (properties?.$groups) this.groups(properties.$groups);
165
+ const userPropsOnce = properties?.$set_once;
166
+ delete properties?.$set_once;
167
+ const userProps = properties?.$set || properties;
168
+ const allProperties = this.enrichProperties({
169
+ $anon_distinct_id: this.getAnonymousId(),
170
+ ...(0, external_posthog_core_stateless_js_namespaceObject.maybeAdd)('$set', userProps),
171
+ ...(0, external_posthog_core_stateless_js_namespaceObject.maybeAdd)('$set_once', userPropsOnce)
172
+ });
173
+ if (distinctId !== previousDistinctId) {
174
+ this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.AnonymousId, previousDistinctId);
175
+ this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.DistinctId, distinctId);
176
+ this.reloadFeatureFlags();
177
+ }
178
+ super.identifyStateless(distinctId, allProperties, options);
179
+ });
180
+ }
181
+ capture(event, properties, options) {
182
+ this.wrap(()=>{
183
+ const distinctId = this.getDistinctId();
184
+ if (properties?.$groups) this.groups(properties.$groups);
185
+ const allProperties = this.enrichProperties(properties);
186
+ super.captureStateless(distinctId, event, allProperties, options);
187
+ });
188
+ }
189
+ alias(alias) {
190
+ this.wrap(()=>{
191
+ const distinctId = this.getDistinctId();
192
+ const allProperties = this.enrichProperties({});
193
+ super.aliasStateless(alias, distinctId, allProperties);
194
+ });
195
+ }
196
+ autocapture(eventType, elements, properties = {}, options) {
197
+ this.wrap(()=>{
198
+ const distinctId = this.getDistinctId();
199
+ const payload = {
200
+ distinct_id: distinctId,
201
+ event: '$autocapture',
202
+ properties: {
203
+ ...this.enrichProperties(properties),
204
+ $event_type: eventType,
205
+ $elements: elements
206
+ }
207
+ };
208
+ this.enqueue('autocapture', payload, options);
209
+ });
210
+ }
211
+ groups(groups) {
212
+ this.wrap(()=>{
213
+ const existingGroups = this.props.$groups || {};
214
+ this.register({
215
+ $groups: {
216
+ ...existingGroups,
217
+ ...groups
218
+ }
219
+ });
220
+ if (Object.keys(groups).find((type)=>existingGroups[type] !== groups[type])) this.reloadFeatureFlags();
221
+ });
222
+ }
223
+ group(groupType, groupKey, groupProperties, options) {
224
+ this.wrap(()=>{
225
+ this.groups({
226
+ [groupType]: groupKey
227
+ });
228
+ if (groupProperties) this.groupIdentify(groupType, groupKey, groupProperties, options);
229
+ });
230
+ }
231
+ groupIdentify(groupType, groupKey, groupProperties, options) {
232
+ this.wrap(()=>{
233
+ const distinctId = this.getDistinctId();
234
+ const eventProperties = this.enrichProperties({});
235
+ super.groupIdentifyStateless(groupType, groupKey, groupProperties, options, distinctId, eventProperties);
236
+ });
237
+ }
238
+ setPersonPropertiesForFlags(properties) {
239
+ this.wrap(()=>{
240
+ const existingProperties = this.getPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.PersonProperties) || {};
241
+ this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.PersonProperties, {
242
+ ...existingProperties,
243
+ ...properties
244
+ });
245
+ });
246
+ }
247
+ resetPersonPropertiesForFlags() {
248
+ this.wrap(()=>{
249
+ this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.PersonProperties, null);
250
+ });
251
+ }
252
+ setGroupPropertiesForFlags(properties) {
253
+ this.wrap(()=>{
254
+ const existingProperties = this.getPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.GroupProperties) || {};
255
+ if (0 !== Object.keys(existingProperties).length) Object.keys(existingProperties).forEach((groupType)=>{
256
+ existingProperties[groupType] = {
257
+ ...existingProperties[groupType],
258
+ ...properties[groupType]
259
+ };
260
+ delete properties[groupType];
261
+ });
262
+ this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.GroupProperties, {
263
+ ...existingProperties,
264
+ ...properties
265
+ });
266
+ });
267
+ }
268
+ resetGroupPropertiesForFlags() {
269
+ this.wrap(()=>{
270
+ this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.GroupProperties, null);
271
+ });
272
+ }
273
+ async remoteConfigAsync() {
274
+ await this._initPromise;
275
+ if (this._remoteConfigResponsePromise) return this._remoteConfigResponsePromise;
276
+ return this._remoteConfigAsync();
277
+ }
278
+ async flagsAsync(sendAnonDistinctId = true) {
279
+ await this._initPromise;
280
+ if (this._flagsResponsePromise) return this._flagsResponsePromise;
281
+ return this._flagsAsync(sendAnonDistinctId);
282
+ }
283
+ cacheSessionReplay(source, response) {
284
+ const sessionReplay = response?.sessionRecording;
285
+ if (sessionReplay) {
286
+ this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.SessionReplay, sessionReplay);
287
+ this.logMsgIfDebug(()=>console.log('PostHog Debug', `Session replay config from ${source}: `, JSON.stringify(sessionReplay)));
288
+ } else if ('boolean' == typeof sessionReplay && false === sessionReplay) {
289
+ this.logMsgIfDebug(()=>console.info('PostHog Debug', `Session replay config from ${source} disabled.`));
290
+ this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.SessionReplay, null);
291
+ }
292
+ }
293
+ async _remoteConfigAsync() {
294
+ this._remoteConfigResponsePromise = this._initPromise.then(()=>{
295
+ let remoteConfig = this.getPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.RemoteConfig);
296
+ this.logMsgIfDebug(()=>console.log('PostHog Debug', 'Cached remote config: ', JSON.stringify(remoteConfig)));
297
+ return super.getRemoteConfig().then((response)=>{
298
+ if (response) {
299
+ const remoteConfigWithoutSurveys = {
300
+ ...response
301
+ };
302
+ delete remoteConfigWithoutSurveys.surveys;
303
+ this.logMsgIfDebug(()=>console.log('PostHog Debug', 'Fetched remote config: ', JSON.stringify(remoteConfigWithoutSurveys)));
304
+ if (false === this.disableSurveys) {
305
+ const surveys = response.surveys;
306
+ let hasSurveys = true;
307
+ if (Array.isArray(surveys)) this.logMsgIfDebug(()=>console.log('PostHog Debug', 'Surveys fetched from remote config: ', JSON.stringify(surveys)));
308
+ else {
309
+ this.logMsgIfDebug(()=>console.log('PostHog Debug', 'There are no surveys.'));
310
+ hasSurveys = false;
311
+ }
312
+ if (hasSurveys) this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.Surveys, surveys);
313
+ else this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.Surveys, null);
314
+ } else this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.Surveys, null);
315
+ this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.RemoteConfig, remoteConfigWithoutSurveys);
316
+ this.cacheSessionReplay('remote config', response);
317
+ if (false === response.hasFeatureFlags) {
318
+ this.setKnownFeatureFlagDetails({
319
+ flags: {}
320
+ });
321
+ this.logMsgIfDebug(()=>console.warn('Remote config has no feature flags, will not load feature flags.'));
322
+ } else if (false !== this.preloadFeatureFlags) this.reloadFeatureFlags();
323
+ if (!response.supportedCompression?.includes(external_types_js_namespaceObject.Compression.GZipJS)) this.disableCompression = true;
324
+ remoteConfig = response;
325
+ }
326
+ return remoteConfig;
327
+ });
328
+ }).finally(()=>{
329
+ this._remoteConfigResponsePromise = void 0;
330
+ });
331
+ return this._remoteConfigResponsePromise;
332
+ }
333
+ async _flagsAsync(sendAnonDistinctId = true) {
334
+ this._flagsResponsePromise = this._initPromise.then(async ()=>{
335
+ const distinctId = this.getDistinctId();
336
+ const groups = this.props.$groups || {};
337
+ const personProperties = this.getPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.PersonProperties) || {};
338
+ const groupProperties = this.getPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.GroupProperties) || {};
339
+ const extraProperties = {
340
+ $anon_distinct_id: sendAnonDistinctId ? this.getAnonymousId() : void 0
341
+ };
342
+ const res = await super.getFlags(distinctId, groups, personProperties, groupProperties, extraProperties);
343
+ if (res?.quotaLimited?.includes(external_posthog_core_stateless_js_namespaceObject.QuotaLimitedFeature.FeatureFlags)) {
344
+ this.setKnownFeatureFlagDetails(null);
345
+ console.warn('[FEATURE FLAGS] Feature flags quota limit exceeded - unsetting all flags. Learn more about billing limits at https://posthog.com/docs/billing/limits-alerts');
346
+ return res;
347
+ }
348
+ if (res?.featureFlags) {
349
+ if (this.sendFeatureFlagEvent) this.flagCallReported = {};
350
+ let newFeatureFlagDetails = res;
351
+ if (res.errorsWhileComputingFlags) {
352
+ const currentFlagDetails = this.getKnownFeatureFlagDetails();
353
+ this.logMsgIfDebug(()=>console.log('PostHog Debug', 'Cached feature flags: ', JSON.stringify(currentFlagDetails)));
354
+ newFeatureFlagDetails = {
355
+ ...res,
356
+ flags: {
357
+ ...currentFlagDetails?.flags,
358
+ ...res.flags
359
+ }
360
+ };
361
+ }
362
+ this.setKnownFeatureFlagDetails(newFeatureFlagDetails);
363
+ this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.FlagsEndpointWasHit, true);
364
+ this.cacheSessionReplay('flags', res);
365
+ }
366
+ return res;
367
+ }).finally(()=>{
368
+ this._flagsResponsePromise = void 0;
369
+ });
370
+ return this._flagsResponsePromise;
371
+ }
372
+ setKnownFeatureFlagDetails(flagsResponse) {
373
+ this.wrap(()=>{
374
+ this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.FeatureFlagDetails, flagsResponse);
375
+ this._events.emit('featureflags', (0, external_featureFlagUtils_js_namespaceObject.getFlagValuesFromFlags)(flagsResponse?.flags ?? {}));
376
+ });
377
+ }
378
+ getKnownFeatureFlagDetails() {
379
+ const storedDetails = this.getPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.FeatureFlagDetails);
380
+ if (!storedDetails) {
381
+ const featureFlags = this.getPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.FeatureFlags);
382
+ const featureFlagPayloads = this.getPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.FeatureFlagPayloads);
383
+ if (void 0 === featureFlags && void 0 === featureFlagPayloads) return;
384
+ return (0, external_featureFlagUtils_js_namespaceObject.createFlagsResponseFromFlagsAndPayloads)(featureFlags ?? {}, featureFlagPayloads ?? {});
385
+ }
386
+ return (0, external_featureFlagUtils_js_namespaceObject.normalizeFlagsResponse)(storedDetails);
387
+ }
388
+ getKnownFeatureFlags() {
389
+ const featureFlagDetails = this.getKnownFeatureFlagDetails();
390
+ if (!featureFlagDetails) return;
391
+ return (0, external_featureFlagUtils_js_namespaceObject.getFlagValuesFromFlags)(featureFlagDetails.flags);
392
+ }
393
+ getKnownFeatureFlagPayloads() {
394
+ const featureFlagDetails = this.getKnownFeatureFlagDetails();
395
+ if (!featureFlagDetails) return;
396
+ return (0, external_featureFlagUtils_js_namespaceObject.getPayloadsFromFlags)(featureFlagDetails.flags);
397
+ }
398
+ getBootstrappedFeatureFlagDetails() {
399
+ const details = this.getPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.BootstrapFeatureFlagDetails);
400
+ if (!details) return;
401
+ return details;
402
+ }
403
+ setBootstrappedFeatureFlagDetails(details) {
404
+ this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.BootstrapFeatureFlagDetails, details);
405
+ }
406
+ getBootstrappedFeatureFlags() {
407
+ const details = this.getBootstrappedFeatureFlagDetails();
408
+ if (!details) return;
409
+ return (0, external_featureFlagUtils_js_namespaceObject.getFlagValuesFromFlags)(details.flags);
410
+ }
411
+ getBootstrappedFeatureFlagPayloads() {
412
+ const details = this.getBootstrappedFeatureFlagDetails();
413
+ if (!details) return;
414
+ return (0, external_featureFlagUtils_js_namespaceObject.getPayloadsFromFlags)(details.flags);
415
+ }
416
+ getFeatureFlag(key) {
417
+ const details = this.getFeatureFlagDetails();
418
+ if (!details) return;
419
+ const featureFlag = details.flags[key];
420
+ let response = (0, external_featureFlagUtils_js_namespaceObject.getFeatureFlagValue)(featureFlag);
421
+ if (void 0 === response) response = false;
422
+ if (this.sendFeatureFlagEvent && !this.flagCallReported[key]) {
423
+ const bootstrappedResponse = this.getBootstrappedFeatureFlags()?.[key];
424
+ const bootstrappedPayload = this.getBootstrappedFeatureFlagPayloads()?.[key];
425
+ this.flagCallReported[key] = true;
426
+ this.capture('$feature_flag_called', {
427
+ $feature_flag: key,
428
+ $feature_flag_response: response,
429
+ ...(0, external_posthog_core_stateless_js_namespaceObject.maybeAdd)('$feature_flag_id', featureFlag?.metadata?.id),
430
+ ...(0, external_posthog_core_stateless_js_namespaceObject.maybeAdd)('$feature_flag_version', featureFlag?.metadata?.version),
431
+ ...(0, external_posthog_core_stateless_js_namespaceObject.maybeAdd)('$feature_flag_reason', featureFlag?.reason?.description ?? featureFlag?.reason?.code),
432
+ ...(0, external_posthog_core_stateless_js_namespaceObject.maybeAdd)('$feature_flag_bootstrapped_response', bootstrappedResponse),
433
+ ...(0, external_posthog_core_stateless_js_namespaceObject.maybeAdd)('$feature_flag_bootstrapped_payload', bootstrappedPayload),
434
+ $used_bootstrap_value: !this.getPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.FlagsEndpointWasHit),
435
+ ...(0, external_posthog_core_stateless_js_namespaceObject.maybeAdd)('$feature_flag_request_id', details.requestId)
436
+ });
437
+ }
438
+ return response;
439
+ }
440
+ getFeatureFlagPayload(key) {
441
+ const payloads = this.getFeatureFlagPayloads();
442
+ if (!payloads) return;
443
+ const response = payloads[key];
444
+ if (void 0 === response) return null;
445
+ return response;
446
+ }
447
+ getFeatureFlagPayloads() {
448
+ return this.getFeatureFlagDetails()?.featureFlagPayloads;
449
+ }
450
+ getFeatureFlags() {
451
+ return this.getFeatureFlagDetails()?.featureFlags;
452
+ }
453
+ getFeatureFlagDetails() {
454
+ let details = this.getKnownFeatureFlagDetails();
455
+ const overriddenFlags = this.getPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.OverrideFeatureFlags);
456
+ if (!overriddenFlags) return details;
457
+ details = details ?? {
458
+ featureFlags: {},
459
+ featureFlagPayloads: {},
460
+ flags: {}
461
+ };
462
+ const flags = details.flags ?? {};
463
+ for(const key in overriddenFlags)if (overriddenFlags[key]) flags[key] = (0, external_featureFlagUtils_js_namespaceObject.updateFlagValue)(flags[key], overriddenFlags[key]);
464
+ else delete flags[key];
465
+ const result = {
466
+ ...details,
467
+ flags
468
+ };
469
+ return (0, external_featureFlagUtils_js_namespaceObject.normalizeFlagsResponse)(result);
470
+ }
471
+ getFeatureFlagsAndPayloads() {
472
+ const flags = this.getFeatureFlags();
473
+ const payloads = this.getFeatureFlagPayloads();
474
+ return {
475
+ flags,
476
+ payloads
477
+ };
478
+ }
479
+ isFeatureEnabled(key) {
480
+ const response = this.getFeatureFlag(key);
481
+ if (void 0 === response) return;
482
+ return !!response;
483
+ }
484
+ reloadFeatureFlags(options) {
485
+ this.flagsAsync(true).then((res)=>{
486
+ options?.cb?.(void 0, res?.featureFlags);
487
+ }).catch((e)=>{
488
+ options?.cb?.(e, void 0);
489
+ if (!options?.cb) this.logMsgIfDebug(()=>console.log('PostHog Debug', 'Error reloading feature flags', e));
490
+ });
491
+ }
492
+ async reloadRemoteConfigAsync() {
493
+ return await this.remoteConfigAsync();
494
+ }
495
+ async reloadFeatureFlagsAsync(sendAnonDistinctId) {
496
+ return (await this.flagsAsync(sendAnonDistinctId ?? true))?.featureFlags;
497
+ }
498
+ onFeatureFlags(cb) {
499
+ return this.on('featureflags', async ()=>{
500
+ const flags = this.getFeatureFlags();
501
+ if (flags) cb(flags);
502
+ });
503
+ }
504
+ onFeatureFlag(key, cb) {
505
+ return this.on('featureflags', async ()=>{
506
+ const flagResponse = this.getFeatureFlag(key);
507
+ if (void 0 !== flagResponse) cb(flagResponse);
508
+ });
509
+ }
510
+ async overrideFeatureFlag(flags) {
511
+ this.wrap(()=>{
512
+ if (null === flags) return this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.OverrideFeatureFlags, null);
513
+ return this.setPersistedProperty(external_types_js_namespaceObject.PostHogPersistedProperty.OverrideFeatureFlags, flags);
514
+ });
515
+ }
516
+ captureException(error, additionalProperties) {
517
+ const properties = {
518
+ $exception_level: 'error',
519
+ $exception_list: [
520
+ {
521
+ type: (0, index_js_namespaceObject.isPlainError)(error) ? error.name : 'Error',
522
+ value: (0, index_js_namespaceObject.isPlainError)(error) ? error.message : error,
523
+ mechanism: {
524
+ handled: true,
525
+ synthetic: false
526
+ }
527
+ }
528
+ ],
529
+ ...additionalProperties
530
+ };
531
+ properties.$exception_personURL = new URL(`/project/${this.apiKey}/person/${this.getDistinctId()}`, this.host).toString();
532
+ this.capture('$exception', properties);
533
+ }
534
+ captureTraceFeedback(traceId, userFeedback) {
535
+ this.capture('$ai_feedback', {
536
+ $ai_feedback_text: userFeedback,
537
+ $ai_trace_id: String(traceId)
538
+ });
539
+ }
540
+ captureTraceMetric(traceId, metricName, metricValue) {
541
+ this.capture('$ai_metric', {
542
+ $ai_metric_name: metricName,
543
+ $ai_metric_value: String(metricValue),
544
+ $ai_trace_id: String(traceId)
545
+ });
546
+ }
547
+ }
548
+ exports.PostHogCore = __webpack_exports__.PostHogCore;
549
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
550
+ "PostHogCore"
551
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
552
+ Object.defineProperty(exports, '__esModule', {
553
+ value: true
554
+ });