@shware/analytics 16.0.0 → 16.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 (200) hide show
  1. package/README.md +90 -5
  2. package/dist/native/fbsdk.cjs +1 -2
  3. package/dist/native/fbsdk.cjs.map +1 -1
  4. package/dist/native/fbsdk.d.cts.map +1 -1
  5. package/dist/native/fbsdk.d.mts.map +1 -1
  6. package/dist/native/fbsdk.mjs +3 -4
  7. package/dist/native/fbsdk.mjs.map +1 -1
  8. package/dist/react/index.cjs +11 -2
  9. package/dist/react/index.cjs.map +1 -1
  10. package/dist/react/index.d.cts +3 -2
  11. package/dist/react/index.d.cts.map +1 -1
  12. package/dist/react/index.d.mts +3 -2
  13. package/dist/react/index.d.mts.map +1 -1
  14. package/dist/react/index.mjs +3 -2
  15. package/dist/react/index.mjs.map +1 -1
  16. package/dist/react/posthog-error-boundary.cjs +119 -0
  17. package/dist/react/posthog-error-boundary.cjs.map +1 -0
  18. package/dist/react/posthog-error-boundary.d.cts +60 -0
  19. package/dist/react/posthog-error-boundary.d.cts.map +1 -0
  20. package/dist/react/posthog-error-boundary.d.mts +60 -0
  21. package/dist/react/posthog-error-boundary.d.mts.map +1 -0
  22. package/dist/react/posthog-error-boundary.mjs +117 -0
  23. package/dist/react/posthog-error-boundary.mjs.map +1 -0
  24. package/dist/react/posthog.cjs +97 -0
  25. package/dist/react/posthog.cjs.map +1 -0
  26. package/dist/react/posthog.d.cts +41 -0
  27. package/dist/react/posthog.d.cts.map +1 -0
  28. package/dist/react/posthog.d.mts +41 -0
  29. package/dist/react/posthog.d.mts.map +1 -0
  30. package/dist/react/posthog.mjs +90 -0
  31. package/dist/react/posthog.mjs.map +1 -0
  32. package/dist/server/conversions-api-error.cjs +20 -0
  33. package/dist/server/conversions-api-error.cjs.map +1 -0
  34. package/dist/server/conversions-api-error.d.cts +17 -0
  35. package/dist/server/conversions-api-error.d.cts.map +1 -0
  36. package/dist/server/conversions-api-error.d.mts +17 -0
  37. package/dist/server/conversions-api-error.d.mts.map +1 -0
  38. package/dist/server/conversions-api-error.mjs +19 -0
  39. package/dist/server/conversions-api-error.mjs.map +1 -0
  40. package/dist/server/google-data-manager.cjs +24 -26
  41. package/dist/server/google-data-manager.cjs.map +1 -1
  42. package/dist/server/google-data-manager.d.cts +13 -5
  43. package/dist/server/google-data-manager.d.cts.map +1 -1
  44. package/dist/server/google-data-manager.d.mts +13 -5
  45. package/dist/server/google-data-manager.d.mts.map +1 -1
  46. package/dist/server/google-data-manager.mjs +24 -27
  47. package/dist/server/google-data-manager.mjs.map +1 -1
  48. package/dist/server/index.cjs +11 -0
  49. package/dist/server/index.d.cts +10 -7
  50. package/dist/server/index.d.mts +10 -7
  51. package/dist/server/index.mjs +9 -7
  52. package/dist/server/linkedin-conversions-api.cjs +29 -23
  53. package/dist/server/linkedin-conversions-api.cjs.map +1 -1
  54. package/dist/server/linkedin-conversions-api.d.cts +7 -2
  55. package/dist/server/linkedin-conversions-api.d.cts.map +1 -1
  56. package/dist/server/linkedin-conversions-api.d.mts +7 -2
  57. package/dist/server/linkedin-conversions-api.d.mts.map +1 -1
  58. package/dist/server/linkedin-conversions-api.mjs +29 -24
  59. package/dist/server/linkedin-conversions-api.mjs.map +1 -1
  60. package/dist/server/meta-capi.cjs +32 -31
  61. package/dist/server/meta-capi.cjs.map +1 -1
  62. package/dist/server/meta-capi.d.cts +12 -3
  63. package/dist/server/meta-capi.d.cts.map +1 -1
  64. package/dist/server/meta-capi.d.mts +12 -3
  65. package/dist/server/meta-capi.d.mts.map +1 -1
  66. package/dist/server/meta-capi.mjs +33 -33
  67. package/dist/server/meta-capi.mjs.map +1 -1
  68. package/dist/server/microsoft-conversions-api.cjs +33 -28
  69. package/dist/server/microsoft-conversions-api.cjs.map +1 -1
  70. package/dist/server/microsoft-conversions-api.d.cts +14 -2
  71. package/dist/server/microsoft-conversions-api.d.cts.map +1 -1
  72. package/dist/server/microsoft-conversions-api.d.mts +14 -2
  73. package/dist/server/microsoft-conversions-api.d.mts.map +1 -1
  74. package/dist/server/microsoft-conversions-api.mjs +33 -29
  75. package/dist/server/microsoft-conversions-api.mjs.map +1 -1
  76. package/dist/server/openai-conversions-api.cjs +20 -18
  77. package/dist/server/openai-conversions-api.cjs.map +1 -1
  78. package/dist/server/openai-conversions-api.d.cts +7 -0
  79. package/dist/server/openai-conversions-api.d.cts.map +1 -1
  80. package/dist/server/openai-conversions-api.d.mts +7 -0
  81. package/dist/server/openai-conversions-api.d.mts.map +1 -1
  82. package/dist/server/openai-conversions-api.mjs +21 -20
  83. package/dist/server/openai-conversions-api.mjs.map +1 -1
  84. package/dist/server/reddit-conversions-api.cjs +20 -18
  85. package/dist/server/reddit-conversions-api.cjs.map +1 -1
  86. package/dist/server/reddit-conversions-api.d.cts +7 -0
  87. package/dist/server/reddit-conversions-api.d.cts.map +1 -1
  88. package/dist/server/reddit-conversions-api.d.mts +7 -0
  89. package/dist/server/reddit-conversions-api.d.mts.map +1 -1
  90. package/dist/server/reddit-conversions-api.mjs +21 -20
  91. package/dist/server/reddit-conversions-api.mjs.map +1 -1
  92. package/dist/server/setup-conversions.cjs +62 -0
  93. package/dist/server/setup-conversions.cjs.map +1 -0
  94. package/dist/server/setup-conversions.d.cts +97 -0
  95. package/dist/server/setup-conversions.d.cts.map +1 -0
  96. package/dist/server/setup-conversions.d.mts +97 -0
  97. package/dist/server/setup-conversions.d.mts.map +1 -0
  98. package/dist/server/setup-conversions.mjs +60 -0
  99. package/dist/server/setup-conversions.mjs.map +1 -0
  100. package/dist/third-parties/index.d.cts +3 -3
  101. package/dist/third-parties/index.d.mts +3 -3
  102. package/dist/third-parties/integration.cjs +13 -6
  103. package/dist/third-parties/integration.cjs.map +1 -1
  104. package/dist/third-parties/integration.d.cts +12 -8
  105. package/dist/third-parties/integration.d.cts.map +1 -1
  106. package/dist/third-parties/integration.d.mts +12 -8
  107. package/dist/third-parties/integration.d.mts.map +1 -1
  108. package/dist/third-parties/integration.mjs +13 -7
  109. package/dist/third-parties/integration.mjs.map +1 -1
  110. package/dist/third-parties/linkedin-insight-tag.cjs +0 -4
  111. package/dist/third-parties/linkedin-insight-tag.cjs.map +1 -1
  112. package/dist/third-parties/linkedin-insight-tag.d.cts +4 -2
  113. package/dist/third-parties/linkedin-insight-tag.d.cts.map +1 -1
  114. package/dist/third-parties/linkedin-insight-tag.d.mts +4 -2
  115. package/dist/third-parties/linkedin-insight-tag.d.mts.map +1 -1
  116. package/dist/third-parties/linkedin-insight-tag.mjs +0 -4
  117. package/dist/third-parties/linkedin-insight-tag.mjs.map +1 -1
  118. package/dist/third-parties/meta-pixel.cjs +1 -3
  119. package/dist/third-parties/meta-pixel.cjs.map +1 -1
  120. package/dist/third-parties/meta-pixel.d.cts +4 -2
  121. package/dist/third-parties/meta-pixel.d.cts.map +1 -1
  122. package/dist/third-parties/meta-pixel.d.mts +4 -2
  123. package/dist/third-parties/meta-pixel.d.mts.map +1 -1
  124. package/dist/third-parties/meta-pixel.mjs +3 -5
  125. package/dist/third-parties/meta-pixel.mjs.map +1 -1
  126. package/dist/third-parties/microsoft-uet.cjs +7 -2
  127. package/dist/third-parties/microsoft-uet.cjs.map +1 -1
  128. package/dist/third-parties/microsoft-uet.d.cts +10 -3
  129. package/dist/third-parties/microsoft-uet.d.cts.map +1 -1
  130. package/dist/third-parties/microsoft-uet.d.mts +10 -3
  131. package/dist/third-parties/microsoft-uet.d.mts.map +1 -1
  132. package/dist/third-parties/microsoft-uet.mjs +8 -4
  133. package/dist/third-parties/microsoft-uet.mjs.map +1 -1
  134. package/dist/third-parties/openai-pixel.cjs +1 -3
  135. package/dist/third-parties/openai-pixel.cjs.map +1 -1
  136. package/dist/third-parties/openai-pixel.d.cts +4 -2
  137. package/dist/third-parties/openai-pixel.d.cts.map +1 -1
  138. package/dist/third-parties/openai-pixel.d.mts +4 -2
  139. package/dist/third-parties/openai-pixel.d.mts.map +1 -1
  140. package/dist/third-parties/openai-pixel.mjs +3 -5
  141. package/dist/third-parties/openai-pixel.mjs.map +1 -1
  142. package/dist/third-parties/posthog.cjs +3 -3
  143. package/dist/third-parties/posthog.cjs.map +1 -1
  144. package/dist/third-parties/posthog.d.cts +3 -3
  145. package/dist/third-parties/posthog.d.mts +3 -3
  146. package/dist/third-parties/posthog.mjs +3 -3
  147. package/dist/third-parties/posthog.mjs.map +1 -1
  148. package/dist/third-parties/reddit-pixel.cjs +1 -3
  149. package/dist/third-parties/reddit-pixel.cjs.map +1 -1
  150. package/dist/third-parties/reddit-pixel.d.cts +4 -2
  151. package/dist/third-parties/reddit-pixel.d.cts.map +1 -1
  152. package/dist/third-parties/reddit-pixel.d.mts +4 -2
  153. package/dist/third-parties/reddit-pixel.d.mts.map +1 -1
  154. package/dist/third-parties/reddit-pixel.mjs +3 -5
  155. package/dist/third-parties/reddit-pixel.mjs.map +1 -1
  156. package/dist/track/fbq.cjs +5 -0
  157. package/dist/track/fbq.cjs.map +1 -1
  158. package/dist/track/fbq.d.cts +2 -0
  159. package/dist/track/fbq.d.cts.map +1 -1
  160. package/dist/track/fbq.d.mts +2 -0
  161. package/dist/track/fbq.d.mts.map +1 -1
  162. package/dist/track/fbq.mjs +5 -1
  163. package/dist/track/fbq.mjs.map +1 -1
  164. package/dist/track/oaiq.cjs +5 -0
  165. package/dist/track/oaiq.cjs.map +1 -1
  166. package/dist/track/oaiq.d.cts +2 -0
  167. package/dist/track/oaiq.d.cts.map +1 -1
  168. package/dist/track/oaiq.d.mts +2 -0
  169. package/dist/track/oaiq.d.mts.map +1 -1
  170. package/dist/track/oaiq.mjs +5 -1
  171. package/dist/track/oaiq.mjs.map +1 -1
  172. package/dist/track/rdt.cjs +5 -0
  173. package/dist/track/rdt.cjs.map +1 -1
  174. package/dist/track/rdt.d.cts +2 -0
  175. package/dist/track/rdt.d.cts.map +1 -1
  176. package/dist/track/rdt.d.mts +2 -0
  177. package/dist/track/rdt.d.mts.map +1 -1
  178. package/dist/track/rdt.mjs +5 -1
  179. package/dist/track/rdt.mjs.map +1 -1
  180. package/package.json +1 -16
  181. package/dist/react/use-feature-flag.cjs +0 -23
  182. package/dist/react/use-feature-flag.cjs.map +0 -1
  183. package/dist/react/use-feature-flag.d.cts +0 -10
  184. package/dist/react/use-feature-flag.d.cts.map +0 -1
  185. package/dist/react/use-feature-flag.d.mts +0 -10
  186. package/dist/react/use-feature-flag.d.mts.map +0 -1
  187. package/dist/react/use-feature-flag.mjs +0 -22
  188. package/dist/react/use-feature-flag.mjs.map +0 -1
  189. package/dist/server/legacy.cjs +0 -3
  190. package/dist/server/legacy.d.cts +0 -2
  191. package/dist/server/legacy.d.mts +0 -2
  192. package/dist/server/legacy.mjs +0 -1
  193. package/dist/server/meta-conversions-api.cjs +0 -205
  194. package/dist/server/meta-conversions-api.cjs.map +0 -1
  195. package/dist/server/meta-conversions-api.d.cts +0 -8
  196. package/dist/server/meta-conversions-api.d.cts.map +0 -1
  197. package/dist/server/meta-conversions-api.d.mts +0 -8
  198. package/dist/server/meta-conversions-api.d.mts.map +0 -1
  199. package/dist/server/meta-conversions-api.mjs +0 -201
  200. package/dist/server/meta-conversions-api.mjs.map +0 -1
@@ -0,0 +1,117 @@
1
+ import { getPosthog } from "../third-parties/posthog.mjs";
2
+ import { Component, isValidElement } from "react";
3
+ // src/react/posthog-error-boundary.tsx
4
+ const INITIAL_STATE = {
5
+ error: null,
6
+ componentStack: null,
7
+ exceptionEvent: void 0
8
+ };
9
+ /**
10
+ * The error as PostHog's own boundary reports it: the React component stack added as a cause, so
11
+ * the issue shows which components it was thrown in.
12
+ */
13
+ function withComponentStack(error, componentStack) {
14
+ if (!componentStack) return error;
15
+ const isError = error instanceof Error;
16
+ const stackError = new Error(isError ? error.message : `Primitive value captured as exception: ${String(error)}`);
17
+ stackError.name = `React ErrorBoundary ${isError ? error.name : "Error"}`;
18
+ stackError.stack = componentStack;
19
+ if (!isError) return stackError;
20
+ const seen = /* @__PURE__ */ new WeakSet();
21
+ let current = error;
22
+ while (!seen.has(current)) {
23
+ seen.add(current);
24
+ if (!(current.cause instanceof Error)) {
25
+ if (current.cause !== void 0) stackError.cause = current.cause;
26
+ current.cause = stackError;
27
+ break;
28
+ }
29
+ current = current.cause;
30
+ }
31
+ return error;
32
+ }
33
+ /**
34
+ * `posthog-js/react`'s `PostHogErrorBoundary`, with its props, for the snippet-loaded library: a
35
+ * render error in `children` is sent to PostHog's error tracking (`captureException`, as an
36
+ * `$exception` event tied to the session's replay) and `fallback` renders instead — a
37
+ * component gets the error and `resetError`. An error thrown before the library has loaded is
38
+ * queued on the stub and sent once it has.
39
+ *
40
+ * The boundary itself never throws: a failure to report (no PostHog, an ad blocker that stopped the
41
+ * library, a throwing `additionalProperties`) is logged and the fallback still renders. It
42
+ * catches render errors only, as any error boundary; errors outside React's render (event
43
+ * handlers, unhandled rejections) are the library's exception autocapture's, enabled in the
44
+ * snippet's `config` (`capture_exceptions`).
45
+ */
46
+ var PosthogErrorBoundary = class extends Component {
47
+ constructor(..._args) {
48
+ super(..._args);
49
+ this.state = INITIAL_STATE;
50
+ this.resetError = () => {
51
+ this.setState(INITIAL_STATE);
52
+ };
53
+ }
54
+ componentDidCatch(error, errorInfo) {
55
+ const componentStack = errorInfo.componentStack ?? "";
56
+ let exceptionEvent;
57
+ try {
58
+ const { additionalProperties } = this.props;
59
+ const properties = typeof additionalProperties === "function" ? additionalProperties(error, errorInfo) : additionalProperties;
60
+ exceptionEvent = getPosthog()?.captureException(withComponentStack(error, componentStack), properties);
61
+ } catch (e) {
62
+ console.error("PosthogErrorBoundary: the error was not reported", e);
63
+ }
64
+ this.setState({
65
+ error,
66
+ componentStack,
67
+ exceptionEvent
68
+ });
69
+ }
70
+ render() {
71
+ const { children, fallback } = this.props;
72
+ const { error, componentStack, exceptionEvent } = this.state;
73
+ if (componentStack === null) return typeof children === "function" ? children() : children;
74
+ const element = typeof fallback === "function" ? fallback({
75
+ error,
76
+ componentStack,
77
+ exceptionEvent,
78
+ resetError: this.resetError
79
+ }) : fallback;
80
+ if (isValidElement(element)) return element;
81
+ console.warn("PosthogErrorBoundary: fallback is not a React element; rendering nothing");
82
+ return null;
83
+ }
84
+ };
85
+ /**
86
+ * `posthog-js/react`'s `setupReactErrorHandler`, for the snippet-loaded library, so without its
87
+ * `client`: a handler for React 19's root options, which report what no `PosthogErrorBoundary`
88
+ * sees — `onUncaughtError` (no boundary caught it), `onCaughtError` (a framework's boundary did,
89
+ * e.g. TanStack Router's `errorComponent`) and `onRecoverableError` (a hydration mismatch React
90
+ * recovered from). The error goes to PostHog with its component stack; `callback` gets the event
91
+ * (`undefined` while only the stub is there), as React's own logging does not run once a handler
92
+ * is set.
93
+ *
94
+ * ```tsx
95
+ * hydrateRoot(document, <StartClient />, {
96
+ * onUncaughtError: setupReactErrorHandler(),
97
+ * onCaughtError: setupReactErrorHandler(),
98
+ * onRecoverableError: setupReactErrorHandler(),
99
+ * });
100
+ * ```
101
+ *
102
+ * Never throws, as `PosthogErrorBoundary`: a failure to report is logged.
103
+ */
104
+ function setupReactErrorHandler(callback) {
105
+ return (error, errorInfo) => {
106
+ let event;
107
+ try {
108
+ event = getPosthog()?.captureException(withComponentStack(error, errorInfo.componentStack));
109
+ } catch (e) {
110
+ console.error("setupReactErrorHandler: the error was not reported", e);
111
+ }
112
+ callback?.(event, error, errorInfo);
113
+ };
114
+ }
115
+ export { PosthogErrorBoundary, setupReactErrorHandler };
116
+
117
+ //# sourceMappingURL=posthog-error-boundary.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"posthog-error-boundary.mjs","names":[],"sources":["../../src/react/posthog-error-boundary.tsx"],"sourcesContent":["import type { CaptureResult, Properties } from 'posthog-js';\nimport {\n Component,\n type ErrorInfo,\n type FunctionComponent,\n type ReactNode,\n isValidElement,\n} from 'react';\nimport { getPosthog } from '../third-parties/posthog';\n\nexport interface PosthogErrorBoundaryFallbackProps {\n error: unknown;\n /** The `$exception` event sent; `undefined` while only the stub is there, which queues it. */\n exceptionEvent: CaptureResult | undefined;\n componentStack: string;\n resetError: () => void;\n}\n\nexport interface PosthogErrorBoundaryProps {\n children?: ReactNode | (() => ReactNode);\n fallback?: ReactNode | FunctionComponent<PosthogErrorBoundaryFallbackProps>;\n additionalProperties?: Properties | ((error: unknown, errorInfo: ErrorInfo) => Properties);\n}\n\ninterface State {\n error: unknown;\n componentStack: string | null;\n exceptionEvent: CaptureResult | undefined;\n}\n\nconst INITIAL_STATE: State = { error: null, componentStack: null, exceptionEvent: undefined };\n\n/**\n * The error as PostHog's own boundary reports it: the React component stack added as a cause, so\n * the issue shows which components it was thrown in.\n */\nfunction withComponentStack(error: unknown, componentStack: string | null | undefined): unknown {\n if (!componentStack) return error;\n const isError = error instanceof Error;\n const stackError = new Error(\n isError ? error.message : `Primitive value captured as exception: ${String(error)}`\n );\n stackError.name = `React ErrorBoundary ${isError ? error.name : 'Error'}`;\n stackError.stack = componentStack;\n if (!isError) return stackError;\n // At the end of the error's cause chain, keeping a last cause that is not an error under it.\n const seen = new WeakSet<Error>();\n let current = error;\n while (!seen.has(current)) {\n seen.add(current);\n if (!(current.cause instanceof Error)) {\n if (current.cause !== undefined) stackError.cause = current.cause;\n current.cause = stackError;\n break;\n }\n current = current.cause;\n }\n return error;\n}\n\n/**\n * `posthog-js/react`'s `PostHogErrorBoundary`, with its props, for the snippet-loaded library: a\n * render error in `children` is sent to PostHog's error tracking (`captureException`, as an\n * `$exception` event tied to the session's replay) and `fallback` renders instead — a\n * component gets the error and `resetError`. An error thrown before the library has loaded is\n * queued on the stub and sent once it has.\n *\n * The boundary itself never throws: a failure to report (no PostHog, an ad blocker that stopped the\n * library, a throwing `additionalProperties`) is logged and the fallback still renders. It\n * catches render errors only, as any error boundary; errors outside React's render (event\n * handlers, unhandled rejections) are the library's exception autocapture's, enabled in the\n * snippet's `config` (`capture_exceptions`).\n */\nexport class PosthogErrorBoundary extends Component<PosthogErrorBoundaryProps, State> {\n override state = INITIAL_STATE;\n\n override componentDidCatch(error: unknown, errorInfo: ErrorInfo) {\n const componentStack = errorInfo.componentStack ?? '';\n let exceptionEvent: CaptureResult | undefined;\n // Reporting must never fail the boundary: an error thrown here would go to the boundary\n // above it, if any — `additionalProperties` throwing, or an error that cannot take a cause.\n try {\n const { additionalProperties } = this.props;\n const properties =\n typeof additionalProperties === 'function'\n ? additionalProperties(error, errorInfo)\n : additionalProperties;\n exceptionEvent = getPosthog()?.captureException(\n withComponentStack(error, componentStack),\n properties\n );\n } catch (e: unknown) {\n console.error('PosthogErrorBoundary: the error was not reported', e);\n }\n this.setState({ error, componentStack, exceptionEvent });\n }\n\n resetError = () => {\n this.setState(INITIAL_STATE);\n };\n\n override render() {\n const { children, fallback } = this.props;\n const { error, componentStack, exceptionEvent } = this.state;\n if (componentStack === null) return typeof children === 'function' ? children() : children;\n\n const element =\n typeof fallback === 'function'\n ? fallback({ error, componentStack, exceptionEvent, resetError: this.resetError })\n : fallback;\n if (isValidElement(element)) return element;\n console.warn('PosthogErrorBoundary: fallback is not a React element; rendering nothing');\n return null;\n }\n}\n\n/**\n * `posthog-js/react`'s `setupReactErrorHandler`, for the snippet-loaded library, so without its\n * `client`: a handler for React 19's root options, which report what no `PosthogErrorBoundary`\n * sees — `onUncaughtError` (no boundary caught it), `onCaughtError` (a framework's boundary did,\n * e.g. TanStack Router's `errorComponent`) and `onRecoverableError` (a hydration mismatch React\n * recovered from). The error goes to PostHog with its component stack; `callback` gets the event\n * (`undefined` while only the stub is there), as React's own logging does not run once a handler\n * is set.\n *\n * ```tsx\n * hydrateRoot(document, <StartClient />, {\n * onUncaughtError: setupReactErrorHandler(),\n * onCaughtError: setupReactErrorHandler(),\n * onRecoverableError: setupReactErrorHandler(),\n * });\n * ```\n *\n * Never throws, as `PosthogErrorBoundary`: a failure to report is logged.\n */\nexport function setupReactErrorHandler(\n callback?: (event: CaptureResult | undefined, error: unknown, errorInfo: ErrorInfo) => void\n): (error: unknown, errorInfo: ErrorInfo) => void {\n return (error, errorInfo) => {\n let event: CaptureResult | undefined;\n try {\n event = getPosthog()?.captureException(withComponentStack(error, errorInfo.componentStack));\n } catch (e: unknown) {\n console.error('setupReactErrorHandler: the error was not reported', e);\n }\n callback?.(event, error, errorInfo);\n };\n}\n"],"mappings":";;;AA8BA,MAAM,gBAAuB;CAAE,OAAO;CAAM,gBAAgB;CAAM,gBAAgB,KAAA;AAAU;;;;;AAM5F,SAAS,mBAAmB,OAAgB,gBAAoD;CAC9F,IAAI,CAAC,gBAAgB,OAAO;CAC5B,MAAM,UAAU,iBAAiB;CACjC,MAAM,aAAa,IAAI,MACrB,UAAU,MAAM,UAAU,0CAA0C,OAAO,KAAK,GAClF;CACA,WAAW,OAAO,uBAAuB,UAAU,MAAM,OAAO;CAChE,WAAW,QAAQ;CACnB,IAAI,CAAC,SAAS,OAAO;CAErB,MAAM,uBAAO,IAAI,QAAe;CAChC,IAAI,UAAU;CACd,OAAO,CAAC,KAAK,IAAI,OAAO,GAAG;EACzB,KAAK,IAAI,OAAO;EAChB,IAAI,EAAE,QAAQ,iBAAiB,QAAQ;GACrC,IAAI,QAAQ,UAAU,KAAA,GAAW,WAAW,QAAQ,QAAQ;GAC5D,QAAQ,QAAQ;GAChB;EACF;EACA,UAAU,QAAQ;CACpB;CACA,OAAO;AACT;;;;;;;;;;;;;;AAeA,IAAa,uBAAb,cAA0C,UAA4C;;;EACnE,KAAA,QAAA;EAuBE,KAAA,mBAAA;GACjB,KAAK,SAAS,aAAa;EAC7B;;CAvBA,kBAA2B,OAAgB,WAAsB;EAC/D,MAAM,iBAAiB,UAAU,kBAAkB;EACnD,IAAI;EAGJ,IAAI;GACF,MAAM,EAAE,yBAAyB,KAAK;GACtC,MAAM,aACJ,OAAO,yBAAyB,aAC5B,qBAAqB,OAAO,SAAS,IACrC;GACN,iBAAiB,WAAW,CAAC,EAAE,iBAC7B,mBAAmB,OAAO,cAAc,GACxC,UACF;EACF,SAAS,GAAY;GACnB,QAAQ,MAAM,oDAAoD,CAAC;EACrE;EACA,KAAK,SAAS;GAAE;GAAO;GAAgB;EAAe,CAAC;CACzD;CAMA,SAAkB;EAChB,MAAM,EAAE,UAAU,aAAa,KAAK;EACpC,MAAM,EAAE,OAAO,gBAAgB,mBAAmB,KAAK;EACvD,IAAI,mBAAmB,MAAM,OAAO,OAAO,aAAa,aAAa,SAAS,IAAI;EAElF,MAAM,UACJ,OAAO,aAAa,aAChB,SAAS;GAAE;GAAO;GAAgB;GAAgB,YAAY,KAAK;EAAW,CAAC,IAC/E;EACN,IAAI,eAAe,OAAO,GAAG,OAAO;EACpC,QAAQ,KAAK,0EAA0E;EACvF,OAAO;CACT;AACF;;;;;;;;;;;;;;;;;;;;AAqBA,SAAgB,uBACd,UACgD;CAChD,QAAQ,OAAO,cAAc;EAC3B,IAAI;EACJ,IAAI;GACF,QAAQ,WAAW,CAAC,EAAE,iBAAiB,mBAAmB,OAAO,UAAU,cAAc,CAAC;EAC5F,SAAS,GAAY;GACnB,QAAQ,MAAM,sDAAsD,CAAC;EACvE;EACA,WAAW,OAAO,OAAO,SAAS;CACpC;AACF"}
@@ -0,0 +1,97 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_third_parties_posthog = require("../third-parties/posthog.cjs");
3
+ let react = require("react");
4
+ // src/react/posthog.ts
5
+ const noop = () => {};
6
+ const Bootstrap = (0, react.createContext)(void 0);
7
+ /**
8
+ * The counterpart of `posthog-js/react`'s `PostHogProvider`, for the snippet-loaded library. It
9
+ * does not initialize PostHog — `posthogAnalytics` does — only carries what the hooks need from
10
+ * the server: the request's server-evaluated flags, the same object as the snippet's
11
+ * `config.bootstrap`, for the flag hooks to render with on the server, through hydration and until
12
+ * the library has loaded its flags — an experiment's variant renders at once, with no flicker and
13
+ * no hydration mismatch. Wrap the app in it, e.g. in the root route.
14
+ */
15
+ function PosthogProvider({ bootstrap, children }) {
16
+ return (0, react.createElement)(Bootstrap.Provider, { value: bootstrap }, children);
17
+ }
18
+ /** Subscribes to flag (re)loads; on the stub the subscription is queued and has no unsubscribe. */
19
+ function subscribe(onChange) {
20
+ return require_third_parties_posthog.getPosthog()?.onFeatureFlags(onChange) ?? noop;
21
+ }
22
+ /**
23
+ * What `read` returns once the library has loaded its flags, and `bootstrapped` until then and on
24
+ * the server, re-read when flags reload. The value is kept while its JSON is unchanged, as
25
+ * `useSyncExternalStore` needs: PostHog builds a new result or payload object on every read.
26
+ */
27
+ function useFlags(read, bootstrapped) {
28
+ const cache = (0, react.useRef)(void 0);
29
+ const stable = (value) => {
30
+ const json = JSON.stringify(value);
31
+ if (!cache.current || cache.current.json !== json) cache.current = {
32
+ json,
33
+ value
34
+ };
35
+ return cache.current.value;
36
+ };
37
+ return (0, react.useSyncExternalStore)(subscribe, () => {
38
+ const posthog = require_third_parties_posthog.getLoadedPosthog();
39
+ return stable(posthog?.featureFlags.hasLoadedFlags ? read(posthog) : bootstrapped);
40
+ }, () => stable(bootstrapped));
41
+ }
42
+ /** A flag's value: `true`/`false`, a multivariate flag's variant, or `undefined` until loaded. */
43
+ function useFeatureFlagVariantKey(flag) {
44
+ const bootstrapped = (0, react.use)(Bootstrap)?.featureFlags?.[flag];
45
+ return useFlags((posthog) => posthog.getFeatureFlag(flag), bootstrapped);
46
+ }
47
+ function useFeatureFlagEnabled(flag, defaultValue) {
48
+ const value = (0, react.use)(Bootstrap)?.featureFlags?.[flag];
49
+ return useFlags((posthog) => posthog.isFeatureEnabled(flag), value === void 0 ? void 0 : !!value) ?? defaultValue;
50
+ }
51
+ /** A flag's payload, read without recording an exposure. */
52
+ function useFeatureFlagPayload(flag) {
53
+ const bootstrapped = (0, react.use)(Bootstrap)?.featureFlagPayloads?.[flag];
54
+ return useFlags((posthog) => posthog.getFeatureFlagResult(flag, { send_event: false })?.payload, bootstrapped);
55
+ }
56
+ /** A flag's value, variant and payload together; `undefined` while it is unknown. */
57
+ function useFeatureFlagResult(flag) {
58
+ const bootstrap = (0, react.use)(Bootstrap);
59
+ const value = bootstrap?.featureFlags?.[flag];
60
+ return useFlags((posthog) => posthog.getFeatureFlagResult(flag), value === void 0 ? void 0 : {
61
+ key: flag,
62
+ enabled: typeof value === "string" ? true : value,
63
+ variant: typeof value === "string" ? value : void 0,
64
+ payload: bootstrap?.featureFlagPayloads?.[flag]
65
+ });
66
+ }
67
+ /**
68
+ * The keys of the flags: only those that are on while bootstrapped, every flag the library has
69
+ * once loaded — as `posthog-js/react`'s does.
70
+ */
71
+ function useActiveFeatureFlags() {
72
+ const flags = (0, react.use)(Bootstrap)?.featureFlags ?? {};
73
+ return useFlags((posthog) => posthog.featureFlags.getFlags(), Object.keys(flags).filter((key) => flags[key]));
74
+ }
75
+ /**
76
+ * `window.posthog`, as `posthog-js/react`'s `usePostHog`: the snippet's stub until the library
77
+ * loads — which queues the calls returning nothing (`capture`, `identify`, …) — then the library,
78
+ * re-rendering when it replaces the stub; `undefined` on the server. Use the value of the latest
79
+ * render rather than one kept from before (e.g. in a ref): calls on a stub kept past the load are
80
+ * lost. A method that returns a value returns `undefined` on the stub; read flags with the hooks.
81
+ *
82
+ * The re-render comes from the flags loading, as the library answers the stub's queued
83
+ * `onFeatureFlags`: with feature flags disabled it stays on the stub until something else
84
+ * re-renders.
85
+ */
86
+ function usePostHog() {
87
+ return (0, react.useSyncExternalStore)(subscribe, require_third_parties_posthog.getPosthog, () => void 0);
88
+ }
89
+ exports.PosthogProvider = PosthogProvider;
90
+ exports.useActiveFeatureFlags = useActiveFeatureFlags;
91
+ exports.useFeatureFlagEnabled = useFeatureFlagEnabled;
92
+ exports.useFeatureFlagPayload = useFeatureFlagPayload;
93
+ exports.useFeatureFlagResult = useFeatureFlagResult;
94
+ exports.useFeatureFlagVariantKey = useFeatureFlagVariantKey;
95
+ exports.usePostHog = usePostHog;
96
+
97
+ //# sourceMappingURL=posthog.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"posthog.cjs","names":["createContext","createElement","getPosthog","useRef","useSyncExternalStore","getLoadedPosthog","use"],"sources":["../../src/react/posthog.ts"],"sourcesContent":["import type { BootstrapConfig, FeatureFlagResult, JsonType, PostHog } from 'posthog-js';\nimport {\n type ReactNode,\n createContext,\n createElement,\n use,\n useRef,\n useSyncExternalStore,\n} from 'react';\nimport { getLoadedPosthog, getPosthog } from '../third-parties/posthog';\n\nconst noop = () => {};\n\nconst Bootstrap = createContext<BootstrapConfig | undefined>(undefined);\n\n/**\n * The counterpart of `posthog-js/react`'s `PostHogProvider`, for the snippet-loaded library. It\n * does not initialize PostHog — `posthogAnalytics` does — only carries what the hooks need from\n * the server: the request's server-evaluated flags, the same object as the snippet's\n * `config.bootstrap`, for the flag hooks to render with on the server, through hydration and until\n * the library has loaded its flags — an experiment's variant renders at once, with no flicker and\n * no hydration mismatch. Wrap the app in it, e.g. in the root route.\n */\nexport function PosthogProvider({\n bootstrap,\n children,\n}: {\n bootstrap: BootstrapConfig | undefined;\n children: ReactNode;\n}) {\n return createElement(Bootstrap.Provider, { value: bootstrap }, children);\n}\n\n/** Subscribes to flag (re)loads; on the stub the subscription is queued and has no unsubscribe. */\nfunction subscribe(onChange: () => void): () => void {\n const unsubscribe: (() => void) | undefined = getPosthog()?.onFeatureFlags(onChange);\n return unsubscribe ?? noop;\n}\n\n/**\n * What `read` returns once the library has loaded its flags, and `bootstrapped` until then and on\n * the server, re-read when flags reload. The value is kept while its JSON is unchanged, as\n * `useSyncExternalStore` needs: PostHog builds a new result or payload object on every read.\n */\nfunction useFlags<T>(read: (posthog: PostHog) => T, bootstrapped: T): T {\n const cache = useRef<{ json: string | undefined; value: T }>(undefined);\n const stable = (value: T) => {\n // Typed wider: `JSON.stringify(undefined)` is `undefined`, whatever its type says.\n const json: string | undefined = JSON.stringify(value);\n if (!cache.current || cache.current.json !== json) cache.current = { json, value };\n return cache.current.value;\n };\n return useSyncExternalStore(\n subscribe,\n () => {\n const posthog = getLoadedPosthog();\n // As `posthog-js/react`: the bootstrap until the flags request has answered, not only until\n // the library has loaded, so no flag is read (and no exposure recorded) before then.\n return stable(posthog?.featureFlags.hasLoadedFlags ? read(posthog) : bootstrapped);\n },\n () => stable(bootstrapped)\n );\n}\n\n/*\n * The hooks of `posthog-js/react`, with its names and signatures, for the snippet-loaded library.\n *\n * Under a `<PosthogProvider>` each is the server's value until the library has loaded its\n * flags, on the server too. Without one it is `undefined` on the server and through hydration, so\n * the default renders first and the variant once flags arrive; to wait for them instead, call\n * `use(browser())` and `use(posthogFlagsReady())` above it, under a `<Suspense>`.\n *\n * Reading a flag records the exposure (`$feature_flag_called`) once the flags have loaded, as\n * PostHog's do — except the payload's — not while only a bootstrapped value is shown.\n */\n\n/** A flag's value: `true`/`false`, a multivariate flag's variant, or `undefined` until loaded. */\nexport function useFeatureFlagVariantKey(flag: string): string | boolean | undefined {\n const bootstrapped = use(Bootstrap)?.featureFlags?.[flag];\n return useFlags((posthog) => posthog.getFeatureFlag(flag), bootstrapped);\n}\n\n/** Whether a flag is on (a variant counts as on); `defaultValue` while it is unknown. */\nexport function useFeatureFlagEnabled(flag: string): boolean | undefined;\nexport function useFeatureFlagEnabled(flag: string, defaultValue: boolean): boolean;\nexport function useFeatureFlagEnabled(flag: string, defaultValue?: boolean) {\n const value = use(Bootstrap)?.featureFlags?.[flag];\n const bootstrapped = value === undefined ? undefined : !!value;\n return useFlags((posthog) => posthog.isFeatureEnabled(flag), bootstrapped) ?? defaultValue;\n}\n\n/** A flag's payload, read without recording an exposure. */\nexport function useFeatureFlagPayload(flag: string): JsonType {\n const bootstrapped = use(Bootstrap)?.featureFlagPayloads?.[flag];\n return useFlags(\n (posthog) => posthog.getFeatureFlagResult(flag, { send_event: false })?.payload,\n bootstrapped\n );\n}\n\n/** A flag's value, variant and payload together; `undefined` while it is unknown. */\nexport function useFeatureFlagResult(flag: string): FeatureFlagResult | undefined {\n const bootstrap = use(Bootstrap);\n const value = bootstrap?.featureFlags?.[flag];\n const bootstrapped: FeatureFlagResult | undefined =\n value === undefined\n ? undefined\n : {\n key: flag,\n enabled: typeof value === 'string' ? true : value,\n variant: typeof value === 'string' ? value : undefined,\n payload: bootstrap?.featureFlagPayloads?.[flag],\n };\n return useFlags((posthog) => posthog.getFeatureFlagResult(flag), bootstrapped);\n}\n\n/**\n * The keys of the flags: only those that are on while bootstrapped, every flag the library has\n * once loaded — as `posthog-js/react`'s does.\n */\nexport function useActiveFeatureFlags(): string[] {\n const flags = use(Bootstrap)?.featureFlags ?? {};\n const bootstrapped = Object.keys(flags).filter((key) => flags[key]);\n return useFlags((posthog) => posthog.featureFlags.getFlags(), bootstrapped);\n}\n\n/**\n * `window.posthog`, as `posthog-js/react`'s `usePostHog`: the snippet's stub until the library\n * loads — which queues the calls returning nothing (`capture`, `identify`, …) — then the library,\n * re-rendering when it replaces the stub; `undefined` on the server. Use the value of the latest\n * render rather than one kept from before (e.g. in a ref): calls on a stub kept past the load are\n * lost. A method that returns a value returns `undefined` on the stub; read flags with the hooks.\n *\n * The re-render comes from the flags loading, as the library answers the stub's queued\n * `onFeatureFlags`: with feature flags disabled it stays on the stub until something else\n * re-renders.\n */\nexport function usePostHog(): PostHog | undefined {\n return useSyncExternalStore(subscribe, getPosthog, () => undefined);\n}\n"],"mappings":";;;;AAWA,MAAM,aAAa,CAAC;AAEpB,MAAM,aAAA,GAAYA,MAAAA,cAAAA,CAA2C,KAAA,CAAS;;;;;;;;;AAUtE,SAAgB,gBAAgB,EAC9B,WACA,YAIC;CACD,QAAA,GAAOC,MAAAA,cAAAA,CAAc,UAAU,UAAU,EAAE,OAAO,UAAU,GAAG,QAAQ;AACzE;;AAGA,SAAS,UAAU,UAAkC;CAEnD,OAD8CC,8BAAAA,WAAW,CAAC,EAAE,eAAe,QAAQ,KAC7D;AACxB;;;;;;AAOA,SAAS,SAAY,MAA+B,cAAoB;CACtE,MAAM,SAAA,GAAQC,MAAAA,OAAAA,CAA+C,KAAA,CAAS;CACtE,MAAM,UAAU,UAAa;EAE3B,MAAM,OAA2B,KAAK,UAAU,KAAK;EACrD,IAAI,CAAC,MAAM,WAAW,MAAM,QAAQ,SAAS,MAAM,MAAM,UAAU;GAAE;GAAM;EAAM;EACjF,OAAO,MAAM,QAAQ;CACvB;CACA,QAAA,GAAOC,MAAAA,qBAAAA,CACL,iBACM;EACJ,MAAM,UAAUC,8BAAAA,iBAAiB;EAGjC,OAAO,OAAO,SAAS,aAAa,iBAAiB,KAAK,OAAO,IAAI,YAAY;CACnF,SACM,OAAO,YAAY,CAC3B;AACF;;AAeA,SAAgB,yBAAyB,MAA4C;CACnF,MAAM,gBAAA,GAAeC,MAAAA,IAAAA,CAAI,SAAS,CAAC,EAAE,eAAe;CACpD,OAAO,UAAU,YAAY,QAAQ,eAAe,IAAI,GAAG,YAAY;AACzE;AAKA,SAAgB,sBAAsB,MAAc,cAAwB;CAC1E,MAAM,SAAA,GAAQA,MAAAA,IAAAA,CAAI,SAAS,CAAC,EAAE,eAAe;CAE7C,OAAO,UAAU,YAAY,QAAQ,iBAAiB,IAAI,GADrC,UAAU,KAAA,IAAY,KAAA,IAAY,CAAC,CAAC,KACgB,KAAK;AAChF;;AAGA,SAAgB,sBAAsB,MAAwB;CAC5D,MAAM,gBAAA,GAAeA,MAAAA,IAAAA,CAAI,SAAS,CAAC,EAAE,sBAAsB;CAC3D,OAAO,UACJ,YAAY,QAAQ,qBAAqB,MAAM,EAAE,YAAY,MAAM,CAAC,CAAC,EAAE,SACxE,YACF;AACF;;AAGA,SAAgB,qBAAqB,MAA6C;CAChF,MAAM,aAAA,GAAYA,MAAAA,IAAAA,CAAI,SAAS;CAC/B,MAAM,QAAQ,WAAW,eAAe;CAUxC,OAAO,UAAU,YAAY,QAAQ,qBAAqB,IAAI,GAR5D,UAAU,KAAA,IACN,KAAA,IACA;EACE,KAAK;EACL,SAAS,OAAO,UAAU,WAAW,OAAO;EAC5C,SAAS,OAAO,UAAU,WAAW,QAAQ,KAAA;EAC7C,SAAS,WAAW,sBAAsB;CAC5C,CACuE;AAC/E;;;;;AAMA,SAAgB,wBAAkC;CAChD,MAAM,SAAA,GAAQA,MAAAA,IAAAA,CAAI,SAAS,CAAC,EAAE,gBAAgB,CAAC;CAE/C,OAAO,UAAU,YAAY,QAAQ,aAAa,SAAS,GADtC,OAAO,KAAK,KAAK,CAAC,CAAC,QAAQ,QAAQ,MAAM,IACW,CAAC;AAC5E;;;;;;;;;;;;AAaA,SAAgB,aAAkC;CAChD,QAAA,GAAOF,MAAAA,qBAAAA,CAAqB,WAAWF,8BAAAA,kBAAkB,KAAA,CAAS;AACpE"}
@@ -0,0 +1,41 @@
1
+ import { ReactNode } from "react";
2
+ import { BootstrapConfig, FeatureFlagResult, JsonType, PostHog } from "posthog-js";
3
+ /**
4
+ * The counterpart of `posthog-js/react`'s `PostHogProvider`, for the snippet-loaded library. It
5
+ * does not initialize PostHog — `posthogAnalytics` does — only carries what the hooks need from
6
+ * the server: the request's server-evaluated flags, the same object as the snippet's
7
+ * `config.bootstrap`, for the flag hooks to render with on the server, through hydration and until
8
+ * the library has loaded its flags — an experiment's variant renders at once, with no flicker and
9
+ * no hydration mismatch. Wrap the app in it, e.g. in the root route.
10
+ */
11
+ export declare function PosthogProvider({ bootstrap, children }: {
12
+ bootstrap: BootstrapConfig | undefined;
13
+ children: ReactNode;
14
+ }): import("react").FunctionComponentElement<import("react").ProviderProps<BootstrapConfig | undefined>>;
15
+ /** A flag's value: `true`/`false`, a multivariate flag's variant, or `undefined` until loaded. */
16
+ export declare function useFeatureFlagVariantKey(flag: string): string | boolean | undefined;
17
+ /** Whether a flag is on (a variant counts as on); `defaultValue` while it is unknown. */
18
+ export declare function useFeatureFlagEnabled(flag: string): boolean | undefined;
19
+ export declare function useFeatureFlagEnabled(flag: string, defaultValue: boolean): boolean;
20
+ /** A flag's payload, read without recording an exposure. */
21
+ export declare function useFeatureFlagPayload(flag: string): JsonType;
22
+ /** A flag's value, variant and payload together; `undefined` while it is unknown. */
23
+ export declare function useFeatureFlagResult(flag: string): FeatureFlagResult | undefined;
24
+ /**
25
+ * The keys of the flags: only those that are on while bootstrapped, every flag the library has
26
+ * once loaded — as `posthog-js/react`'s does.
27
+ */
28
+ export declare function useActiveFeatureFlags(): string[];
29
+ /**
30
+ * `window.posthog`, as `posthog-js/react`'s `usePostHog`: the snippet's stub until the library
31
+ * loads — which queues the calls returning nothing (`capture`, `identify`, …) — then the library,
32
+ * re-rendering when it replaces the stub; `undefined` on the server. Use the value of the latest
33
+ * render rather than one kept from before (e.g. in a ref): calls on a stub kept past the load are
34
+ * lost. A method that returns a value returns `undefined` on the stub; read flags with the hooks.
35
+ *
36
+ * The re-render comes from the flags loading, as the library answers the stub's queued
37
+ * `onFeatureFlags`: with feature flags disabled it stays on the stub until something else
38
+ * re-renders.
39
+ */
40
+ export declare function usePostHog(): PostHog | undefined;
41
+ //# sourceMappingURL=posthog.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"posthog.d.cts","names":[],"sources":["../../src/react/posthog.ts"],"mappings":";;;;;;;;;;wBAuBgB,kBACd,WACA;EAEA,WAAW;EACX,UAAU;oBACX,yCAAA,cAAA;;wBAgDe,yBAAyB;;wBAMzB,sBAAsB;wBACtB,sBAAsB,cAAc;;wBAQpC,sBAAsB,eAAe;;wBASrC,qBAAqB,eAAe;;;;;wBAmBpC;;;;;;;;;;;;wBAiBA,cAAc"}
@@ -0,0 +1,41 @@
1
+ import { ReactNode } from "react";
2
+ import { BootstrapConfig, FeatureFlagResult, JsonType, PostHog } from "posthog-js";
3
+ /**
4
+ * The counterpart of `posthog-js/react`'s `PostHogProvider`, for the snippet-loaded library. It
5
+ * does not initialize PostHog — `posthogAnalytics` does — only carries what the hooks need from
6
+ * the server: the request's server-evaluated flags, the same object as the snippet's
7
+ * `config.bootstrap`, for the flag hooks to render with on the server, through hydration and until
8
+ * the library has loaded its flags — an experiment's variant renders at once, with no flicker and
9
+ * no hydration mismatch. Wrap the app in it, e.g. in the root route.
10
+ */
11
+ export declare function PosthogProvider({ bootstrap, children }: {
12
+ bootstrap: BootstrapConfig | undefined;
13
+ children: ReactNode;
14
+ }): import("react").FunctionComponentElement<import("react").ProviderProps<BootstrapConfig | undefined>>;
15
+ /** A flag's value: `true`/`false`, a multivariate flag's variant, or `undefined` until loaded. */
16
+ export declare function useFeatureFlagVariantKey(flag: string): string | boolean | undefined;
17
+ /** Whether a flag is on (a variant counts as on); `defaultValue` while it is unknown. */
18
+ export declare function useFeatureFlagEnabled(flag: string): boolean | undefined;
19
+ export declare function useFeatureFlagEnabled(flag: string, defaultValue: boolean): boolean;
20
+ /** A flag's payload, read without recording an exposure. */
21
+ export declare function useFeatureFlagPayload(flag: string): JsonType;
22
+ /** A flag's value, variant and payload together; `undefined` while it is unknown. */
23
+ export declare function useFeatureFlagResult(flag: string): FeatureFlagResult | undefined;
24
+ /**
25
+ * The keys of the flags: only those that are on while bootstrapped, every flag the library has
26
+ * once loaded — as `posthog-js/react`'s does.
27
+ */
28
+ export declare function useActiveFeatureFlags(): string[];
29
+ /**
30
+ * `window.posthog`, as `posthog-js/react`'s `usePostHog`: the snippet's stub until the library
31
+ * loads — which queues the calls returning nothing (`capture`, `identify`, …) — then the library,
32
+ * re-rendering when it replaces the stub; `undefined` on the server. Use the value of the latest
33
+ * render rather than one kept from before (e.g. in a ref): calls on a stub kept past the load are
34
+ * lost. A method that returns a value returns `undefined` on the stub; read flags with the hooks.
35
+ *
36
+ * The re-render comes from the flags loading, as the library answers the stub's queued
37
+ * `onFeatureFlags`: with feature flags disabled it stays on the stub until something else
38
+ * re-renders.
39
+ */
40
+ export declare function usePostHog(): PostHog | undefined;
41
+ //# sourceMappingURL=posthog.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"posthog.d.mts","names":[],"sources":["../../src/react/posthog.ts"],"mappings":";;;;;;;;;;wBAuBgB,kBACd,WACA;EAEA,WAAW;EACX,UAAU;oBACX,yCAAA,cAAA;;wBAgDe,yBAAyB;;wBAMzB,sBAAsB;wBACtB,sBAAsB,cAAc;;wBAQpC,sBAAsB,eAAe;;wBASrC,qBAAqB,eAAe;;;;;wBAmBpC;;;;;;;;;;;;wBAiBA,cAAc"}
@@ -0,0 +1,90 @@
1
+ import { getLoadedPosthog, getPosthog } from "../third-parties/posthog.mjs";
2
+ import { createContext, createElement, use, useRef, useSyncExternalStore } from "react";
3
+ // src/react/posthog.ts
4
+ const noop = () => {};
5
+ const Bootstrap = createContext(void 0);
6
+ /**
7
+ * The counterpart of `posthog-js/react`'s `PostHogProvider`, for the snippet-loaded library. It
8
+ * does not initialize PostHog — `posthogAnalytics` does — only carries what the hooks need from
9
+ * the server: the request's server-evaluated flags, the same object as the snippet's
10
+ * `config.bootstrap`, for the flag hooks to render with on the server, through hydration and until
11
+ * the library has loaded its flags — an experiment's variant renders at once, with no flicker and
12
+ * no hydration mismatch. Wrap the app in it, e.g. in the root route.
13
+ */
14
+ function PosthogProvider({ bootstrap, children }) {
15
+ return createElement(Bootstrap.Provider, { value: bootstrap }, children);
16
+ }
17
+ /** Subscribes to flag (re)loads; on the stub the subscription is queued and has no unsubscribe. */
18
+ function subscribe(onChange) {
19
+ return getPosthog()?.onFeatureFlags(onChange) ?? noop;
20
+ }
21
+ /**
22
+ * What `read` returns once the library has loaded its flags, and `bootstrapped` until then and on
23
+ * the server, re-read when flags reload. The value is kept while its JSON is unchanged, as
24
+ * `useSyncExternalStore` needs: PostHog builds a new result or payload object on every read.
25
+ */
26
+ function useFlags(read, bootstrapped) {
27
+ const cache = useRef(void 0);
28
+ const stable = (value) => {
29
+ const json = JSON.stringify(value);
30
+ if (!cache.current || cache.current.json !== json) cache.current = {
31
+ json,
32
+ value
33
+ };
34
+ return cache.current.value;
35
+ };
36
+ return useSyncExternalStore(subscribe, () => {
37
+ const posthog = getLoadedPosthog();
38
+ return stable(posthog?.featureFlags.hasLoadedFlags ? read(posthog) : bootstrapped);
39
+ }, () => stable(bootstrapped));
40
+ }
41
+ /** A flag's value: `true`/`false`, a multivariate flag's variant, or `undefined` until loaded. */
42
+ function useFeatureFlagVariantKey(flag) {
43
+ const bootstrapped = use(Bootstrap)?.featureFlags?.[flag];
44
+ return useFlags((posthog) => posthog.getFeatureFlag(flag), bootstrapped);
45
+ }
46
+ function useFeatureFlagEnabled(flag, defaultValue) {
47
+ const value = use(Bootstrap)?.featureFlags?.[flag];
48
+ return useFlags((posthog) => posthog.isFeatureEnabled(flag), value === void 0 ? void 0 : !!value) ?? defaultValue;
49
+ }
50
+ /** A flag's payload, read without recording an exposure. */
51
+ function useFeatureFlagPayload(flag) {
52
+ const bootstrapped = use(Bootstrap)?.featureFlagPayloads?.[flag];
53
+ return useFlags((posthog) => posthog.getFeatureFlagResult(flag, { send_event: false })?.payload, bootstrapped);
54
+ }
55
+ /** A flag's value, variant and payload together; `undefined` while it is unknown. */
56
+ function useFeatureFlagResult(flag) {
57
+ const bootstrap = use(Bootstrap);
58
+ const value = bootstrap?.featureFlags?.[flag];
59
+ return useFlags((posthog) => posthog.getFeatureFlagResult(flag), value === void 0 ? void 0 : {
60
+ key: flag,
61
+ enabled: typeof value === "string" ? true : value,
62
+ variant: typeof value === "string" ? value : void 0,
63
+ payload: bootstrap?.featureFlagPayloads?.[flag]
64
+ });
65
+ }
66
+ /**
67
+ * The keys of the flags: only those that are on while bootstrapped, every flag the library has
68
+ * once loaded — as `posthog-js/react`'s does.
69
+ */
70
+ function useActiveFeatureFlags() {
71
+ const flags = use(Bootstrap)?.featureFlags ?? {};
72
+ return useFlags((posthog) => posthog.featureFlags.getFlags(), Object.keys(flags).filter((key) => flags[key]));
73
+ }
74
+ /**
75
+ * `window.posthog`, as `posthog-js/react`'s `usePostHog`: the snippet's stub until the library
76
+ * loads — which queues the calls returning nothing (`capture`, `identify`, …) — then the library,
77
+ * re-rendering when it replaces the stub; `undefined` on the server. Use the value of the latest
78
+ * render rather than one kept from before (e.g. in a ref): calls on a stub kept past the load are
79
+ * lost. A method that returns a value returns `undefined` on the stub; read flags with the hooks.
80
+ *
81
+ * The re-render comes from the flags loading, as the library answers the stub's queued
82
+ * `onFeatureFlags`: with feature flags disabled it stays on the stub until something else
83
+ * re-renders.
84
+ */
85
+ function usePostHog() {
86
+ return useSyncExternalStore(subscribe, getPosthog, () => void 0);
87
+ }
88
+ export { PosthogProvider, useActiveFeatureFlags, useFeatureFlagEnabled, useFeatureFlagPayload, useFeatureFlagResult, useFeatureFlagVariantKey, usePostHog };
89
+
90
+ //# sourceMappingURL=posthog.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"posthog.mjs","names":[],"sources":["../../src/react/posthog.ts"],"sourcesContent":["import type { BootstrapConfig, FeatureFlagResult, JsonType, PostHog } from 'posthog-js';\nimport {\n type ReactNode,\n createContext,\n createElement,\n use,\n useRef,\n useSyncExternalStore,\n} from 'react';\nimport { getLoadedPosthog, getPosthog } from '../third-parties/posthog';\n\nconst noop = () => {};\n\nconst Bootstrap = createContext<BootstrapConfig | undefined>(undefined);\n\n/**\n * The counterpart of `posthog-js/react`'s `PostHogProvider`, for the snippet-loaded library. It\n * does not initialize PostHog — `posthogAnalytics` does — only carries what the hooks need from\n * the server: the request's server-evaluated flags, the same object as the snippet's\n * `config.bootstrap`, for the flag hooks to render with on the server, through hydration and until\n * the library has loaded its flags — an experiment's variant renders at once, with no flicker and\n * no hydration mismatch. Wrap the app in it, e.g. in the root route.\n */\nexport function PosthogProvider({\n bootstrap,\n children,\n}: {\n bootstrap: BootstrapConfig | undefined;\n children: ReactNode;\n}) {\n return createElement(Bootstrap.Provider, { value: bootstrap }, children);\n}\n\n/** Subscribes to flag (re)loads; on the stub the subscription is queued and has no unsubscribe. */\nfunction subscribe(onChange: () => void): () => void {\n const unsubscribe: (() => void) | undefined = getPosthog()?.onFeatureFlags(onChange);\n return unsubscribe ?? noop;\n}\n\n/**\n * What `read` returns once the library has loaded its flags, and `bootstrapped` until then and on\n * the server, re-read when flags reload. The value is kept while its JSON is unchanged, as\n * `useSyncExternalStore` needs: PostHog builds a new result or payload object on every read.\n */\nfunction useFlags<T>(read: (posthog: PostHog) => T, bootstrapped: T): T {\n const cache = useRef<{ json: string | undefined; value: T }>(undefined);\n const stable = (value: T) => {\n // Typed wider: `JSON.stringify(undefined)` is `undefined`, whatever its type says.\n const json: string | undefined = JSON.stringify(value);\n if (!cache.current || cache.current.json !== json) cache.current = { json, value };\n return cache.current.value;\n };\n return useSyncExternalStore(\n subscribe,\n () => {\n const posthog = getLoadedPosthog();\n // As `posthog-js/react`: the bootstrap until the flags request has answered, not only until\n // the library has loaded, so no flag is read (and no exposure recorded) before then.\n return stable(posthog?.featureFlags.hasLoadedFlags ? read(posthog) : bootstrapped);\n },\n () => stable(bootstrapped)\n );\n}\n\n/*\n * The hooks of `posthog-js/react`, with its names and signatures, for the snippet-loaded library.\n *\n * Under a `<PosthogProvider>` each is the server's value until the library has loaded its\n * flags, on the server too. Without one it is `undefined` on the server and through hydration, so\n * the default renders first and the variant once flags arrive; to wait for them instead, call\n * `use(browser())` and `use(posthogFlagsReady())` above it, under a `<Suspense>`.\n *\n * Reading a flag records the exposure (`$feature_flag_called`) once the flags have loaded, as\n * PostHog's do — except the payload's — not while only a bootstrapped value is shown.\n */\n\n/** A flag's value: `true`/`false`, a multivariate flag's variant, or `undefined` until loaded. */\nexport function useFeatureFlagVariantKey(flag: string): string | boolean | undefined {\n const bootstrapped = use(Bootstrap)?.featureFlags?.[flag];\n return useFlags((posthog) => posthog.getFeatureFlag(flag), bootstrapped);\n}\n\n/** Whether a flag is on (a variant counts as on); `defaultValue` while it is unknown. */\nexport function useFeatureFlagEnabled(flag: string): boolean | undefined;\nexport function useFeatureFlagEnabled(flag: string, defaultValue: boolean): boolean;\nexport function useFeatureFlagEnabled(flag: string, defaultValue?: boolean) {\n const value = use(Bootstrap)?.featureFlags?.[flag];\n const bootstrapped = value === undefined ? undefined : !!value;\n return useFlags((posthog) => posthog.isFeatureEnabled(flag), bootstrapped) ?? defaultValue;\n}\n\n/** A flag's payload, read without recording an exposure. */\nexport function useFeatureFlagPayload(flag: string): JsonType {\n const bootstrapped = use(Bootstrap)?.featureFlagPayloads?.[flag];\n return useFlags(\n (posthog) => posthog.getFeatureFlagResult(flag, { send_event: false })?.payload,\n bootstrapped\n );\n}\n\n/** A flag's value, variant and payload together; `undefined` while it is unknown. */\nexport function useFeatureFlagResult(flag: string): FeatureFlagResult | undefined {\n const bootstrap = use(Bootstrap);\n const value = bootstrap?.featureFlags?.[flag];\n const bootstrapped: FeatureFlagResult | undefined =\n value === undefined\n ? undefined\n : {\n key: flag,\n enabled: typeof value === 'string' ? true : value,\n variant: typeof value === 'string' ? value : undefined,\n payload: bootstrap?.featureFlagPayloads?.[flag],\n };\n return useFlags((posthog) => posthog.getFeatureFlagResult(flag), bootstrapped);\n}\n\n/**\n * The keys of the flags: only those that are on while bootstrapped, every flag the library has\n * once loaded — as `posthog-js/react`'s does.\n */\nexport function useActiveFeatureFlags(): string[] {\n const flags = use(Bootstrap)?.featureFlags ?? {};\n const bootstrapped = Object.keys(flags).filter((key) => flags[key]);\n return useFlags((posthog) => posthog.featureFlags.getFlags(), bootstrapped);\n}\n\n/**\n * `window.posthog`, as `posthog-js/react`'s `usePostHog`: the snippet's stub until the library\n * loads — which queues the calls returning nothing (`capture`, `identify`, …) — then the library,\n * re-rendering when it replaces the stub; `undefined` on the server. Use the value of the latest\n * render rather than one kept from before (e.g. in a ref): calls on a stub kept past the load are\n * lost. A method that returns a value returns `undefined` on the stub; read flags with the hooks.\n *\n * The re-render comes from the flags loading, as the library answers the stub's queued\n * `onFeatureFlags`: with feature flags disabled it stays on the stub until something else\n * re-renders.\n */\nexport function usePostHog(): PostHog | undefined {\n return useSyncExternalStore(subscribe, getPosthog, () => undefined);\n}\n"],"mappings":";;;AAWA,MAAM,aAAa,CAAC;AAEpB,MAAM,YAAY,cAA2C,KAAA,CAAS;;;;;;;;;AAUtE,SAAgB,gBAAgB,EAC9B,WACA,YAIC;CACD,OAAO,cAAc,UAAU,UAAU,EAAE,OAAO,UAAU,GAAG,QAAQ;AACzE;;AAGA,SAAS,UAAU,UAAkC;CAEnD,OAD8C,WAAW,CAAC,EAAE,eAAe,QAAQ,KAC7D;AACxB;;;;;;AAOA,SAAS,SAAY,MAA+B,cAAoB;CACtE,MAAM,QAAQ,OAA+C,KAAA,CAAS;CACtE,MAAM,UAAU,UAAa;EAE3B,MAAM,OAA2B,KAAK,UAAU,KAAK;EACrD,IAAI,CAAC,MAAM,WAAW,MAAM,QAAQ,SAAS,MAAM,MAAM,UAAU;GAAE;GAAM;EAAM;EACjF,OAAO,MAAM,QAAQ;CACvB;CACA,OAAO,qBACL,iBACM;EACJ,MAAM,UAAU,iBAAiB;EAGjC,OAAO,OAAO,SAAS,aAAa,iBAAiB,KAAK,OAAO,IAAI,YAAY;CACnF,SACM,OAAO,YAAY,CAC3B;AACF;;AAeA,SAAgB,yBAAyB,MAA4C;CACnF,MAAM,eAAe,IAAI,SAAS,CAAC,EAAE,eAAe;CACpD,OAAO,UAAU,YAAY,QAAQ,eAAe,IAAI,GAAG,YAAY;AACzE;AAKA,SAAgB,sBAAsB,MAAc,cAAwB;CAC1E,MAAM,QAAQ,IAAI,SAAS,CAAC,EAAE,eAAe;CAE7C,OAAO,UAAU,YAAY,QAAQ,iBAAiB,IAAI,GADrC,UAAU,KAAA,IAAY,KAAA,IAAY,CAAC,CAAC,KACgB,KAAK;AAChF;;AAGA,SAAgB,sBAAsB,MAAwB;CAC5D,MAAM,eAAe,IAAI,SAAS,CAAC,EAAE,sBAAsB;CAC3D,OAAO,UACJ,YAAY,QAAQ,qBAAqB,MAAM,EAAE,YAAY,MAAM,CAAC,CAAC,EAAE,SACxE,YACF;AACF;;AAGA,SAAgB,qBAAqB,MAA6C;CAChF,MAAM,YAAY,IAAI,SAAS;CAC/B,MAAM,QAAQ,WAAW,eAAe;CAUxC,OAAO,UAAU,YAAY,QAAQ,qBAAqB,IAAI,GAR5D,UAAU,KAAA,IACN,KAAA,IACA;EACE,KAAK;EACL,SAAS,OAAO,UAAU,WAAW,OAAO;EAC5C,SAAS,OAAO,UAAU,WAAW,QAAQ,KAAA;EAC7C,SAAS,WAAW,sBAAsB;CAC5C,CACuE;AAC/E;;;;;AAMA,SAAgB,wBAAkC;CAChD,MAAM,QAAQ,IAAI,SAAS,CAAC,EAAE,gBAAgB,CAAC;CAE/C,OAAO,UAAU,YAAY,QAAQ,aAAa,SAAS,GADtC,OAAO,KAAK,KAAK,CAAC,CAAC,QAAQ,QAAQ,MAAM,IACW,CAAC;AAC5E;;;;;;;;;;;;AAaA,SAAgB,aAAkC;CAChD,OAAO,qBAAqB,WAAW,kBAAkB,KAAA,CAAS;AACpE"}
@@ -0,0 +1,20 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ // src/server/conversions-api-error.ts
3
+ /**
4
+ * A conversions API that answered with an error status. The senders throw it, after the retries
5
+ * of a transient failure have run out, and leave what to do about it to the caller —
6
+ * `setupConversions` sends to every other destination still, then rejects with all of them. A
7
+ * network failure rejects with the fetch error itself. Neither carries the access token.
8
+ */
9
+ var ConversionsApiError = class extends Error {
10
+ constructor(platform, status, body) {
11
+ super(`Failed to send ${platform} conversions, status: ${status}, body: ${body}`);
12
+ this.platform = platform;
13
+ this.status = status;
14
+ this.body = body;
15
+ this.name = "ConversionsApiError";
16
+ }
17
+ };
18
+ exports.ConversionsApiError = ConversionsApiError;
19
+
20
+ //# sourceMappingURL=conversions-api-error.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"conversions-api-error.cjs","names":[],"sources":["../../src/server/conversions-api-error.ts"],"sourcesContent":["/**\n * A conversions API that answered with an error status. The senders throw it, after the retries\n * of a transient failure have run out, and leave what to do about it to the caller —\n * `setupConversions` sends to every other destination still, then rejects with all of them. A\n * network failure rejects with the fetch error itself. Neither carries the access token.\n */\nexport class ConversionsApiError extends Error {\n override readonly name = 'ConversionsApiError';\n\n constructor(\n readonly platform: 'meta' | 'reddit' | 'openai' | 'linkedin' | 'microsoft' | 'google',\n readonly status: number,\n /** The response body, as the platform explains the rejection. */\n readonly body: string\n ) {\n super(`Failed to send ${platform} conversions, status: ${status}, body: ${body}`);\n }\n}\n"],"mappings":";;;;;;;;AAMA,IAAa,sBAAb,cAAyC,MAAM;CAG7C,YACE,UACA,QAEA,MACA;EACA,MAAM,kBAAkB,SAAS,wBAAwB,OAAO,UAAU,MAAM;EALvE,KAAA,WAAA;EACA,KAAA,SAAA;EAEA,KAAA,OAAA;EANc,KAAA,OAAA;CASzB;AACF"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * A conversions API that answered with an error status. The senders throw it, after the retries
3
+ * of a transient failure have run out, and leave what to do about it to the caller —
4
+ * `setupConversions` sends to every other destination still, then rejects with all of them. A
5
+ * network failure rejects with the fetch error itself. Neither carries the access token.
6
+ */
7
+ export declare class ConversionsApiError extends Error {
8
+ readonly platform: 'meta' | 'reddit' | 'openai' | 'linkedin' | 'microsoft' | 'google';
9
+ readonly status: number;
10
+ /** The response body, as the platform explains the rejection. */
11
+ readonly body: string;
12
+ readonly name = "ConversionsApiError";
13
+ constructor(platform: 'meta' | 'reddit' | 'openai' | 'linkedin' | 'microsoft' | 'google', status: number,
14
+ /** The response body, as the platform explains the rejection. */
15
+ body: string);
16
+ }
17
+ //# sourceMappingURL=conversions-api-error.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"conversions-api-error.d.cts","names":[],"sources":["../../src/server/conversions-api-error.ts"],"mappings":";;;;;;qBAMa,4BAA4B;WAI5B;WACA;;WAEA;WANO;EAElB,YACW,8EACA;;EAEA"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * A conversions API that answered with an error status. The senders throw it, after the retries
3
+ * of a transient failure have run out, and leave what to do about it to the caller —
4
+ * `setupConversions` sends to every other destination still, then rejects with all of them. A
5
+ * network failure rejects with the fetch error itself. Neither carries the access token.
6
+ */
7
+ export declare class ConversionsApiError extends Error {
8
+ readonly platform: 'meta' | 'reddit' | 'openai' | 'linkedin' | 'microsoft' | 'google';
9
+ readonly status: number;
10
+ /** The response body, as the platform explains the rejection. */
11
+ readonly body: string;
12
+ readonly name = "ConversionsApiError";
13
+ constructor(platform: 'meta' | 'reddit' | 'openai' | 'linkedin' | 'microsoft' | 'google', status: number,
14
+ /** The response body, as the platform explains the rejection. */
15
+ body: string);
16
+ }
17
+ //# sourceMappingURL=conversions-api-error.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"conversions-api-error.d.mts","names":[],"sources":["../../src/server/conversions-api-error.ts"],"mappings":";;;;;;qBAMa,4BAA4B;WAI5B;WACA;;WAEA;WANO;EAElB,YACW,8EACA;;EAEA"}
@@ -0,0 +1,19 @@
1
+ // src/server/conversions-api-error.ts
2
+ /**
3
+ * A conversions API that answered with an error status. The senders throw it, after the retries
4
+ * of a transient failure have run out, and leave what to do about it to the caller —
5
+ * `setupConversions` sends to every other destination still, then rejects with all of them. A
6
+ * network failure rejects with the fetch error itself. Neither carries the access token.
7
+ */
8
+ var ConversionsApiError = class extends Error {
9
+ constructor(platform, status, body) {
10
+ super(`Failed to send ${platform} conversions, status: ${status}, body: ${body}`);
11
+ this.platform = platform;
12
+ this.status = status;
13
+ this.body = body;
14
+ this.name = "ConversionsApiError";
15
+ }
16
+ };
17
+ export { ConversionsApiError };
18
+
19
+ //# sourceMappingURL=conversions-api-error.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"conversions-api-error.mjs","names":[],"sources":["../../src/server/conversions-api-error.ts"],"sourcesContent":["/**\n * A conversions API that answered with an error status. The senders throw it, after the retries\n * of a transient failure have run out, and leave what to do about it to the caller —\n * `setupConversions` sends to every other destination still, then rejects with all of them. A\n * network failure rejects with the fetch error itself. Neither carries the access token.\n */\nexport class ConversionsApiError extends Error {\n override readonly name = 'ConversionsApiError';\n\n constructor(\n readonly platform: 'meta' | 'reddit' | 'openai' | 'linkedin' | 'microsoft' | 'google',\n readonly status: number,\n /** The response body, as the platform explains the rejection. */\n readonly body: string\n ) {\n super(`Failed to send ${platform} conversions, status: ${status}, body: ${body}`);\n }\n}\n"],"mappings":";;;;;;;AAMA,IAAa,sBAAb,cAAyC,MAAM;CAG7C,YACE,UACA,QAEA,MACA;EACA,MAAM,kBAAkB,SAAS,wBAAwB,OAAO,UAAU,MAAM;EALvE,KAAA,WAAA;EACA,KAAA,SAAA;EAEA,KAAA,OAAA;EANc,KAAA,OAAA;CASzB;AACF"}
@@ -2,6 +2,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_third_parties_ignored_events = require("../third-parties/ignored-events.cjs");
3
3
  const require_server_action_source = require("./action-source.cjs");
4
4
  const require_server_click_ids = require("./click-ids.cjs");
5
+ const require_server_conversions_api_error = require("./conversions-api-error.cjs");
6
+ const require_server_setup_conversions = require("./setup-conversions.cjs");
5
7
  let _shware_utils = require("@shware/utils");
6
8
  let node_crypto = require("node:crypto");
7
9
  // src/server/google-data-manager.ts
@@ -9,8 +11,8 @@ let node_crypto = require("node:crypto");
9
11
  * Google Ads conversion sender over the Data Manager API (`events:ingest`) — Google's
10
12
  * counterpart to Meta's Conversions API, and the mandated successor to the Google Ads API's
11
13
  * `UploadClickConversions`, which stopped accepting new adopters on 2026-06-15. Same shape as
12
- * the other senders here: a pure per-event builder, a never-throws `sendEvents`, credentials
13
- * in headers and never in a URL or a log line.
14
+ * the other senders here: a pure per-event builder, a `sendEvents` that rejects on failure,
15
+ * credentials in headers and never in a URL or an error.
14
16
  *
15
17
  * Google's recommended deployment is *hybrid* ("boost your tag with additional data sources"):
16
18
  * this upload attaches to the SAME conversion action the gtag tag reports to, matched by
@@ -98,8 +100,9 @@ function getDataManagerEvent(event, config, data = {}, actionType = "webpage") {
98
100
  /** The API takes at most 2000 events per request. */
99
101
  const MAX_EVENTS_PER_REQUEST = 2e3;
100
102
  /**
101
- * Uploads the configured conversions from a batch of events. Never throws: an unusable event
102
- * is skipped and a failed request is logged and swallowed, like every other sender here. The
103
+ * Uploads the configured conversions from a batch of events. An unusable event is skipped; a
104
+ * failed request rejects (a `ConversionsApiError`, or the fetch error), as with every sender here,
105
+ * and sends no further ones. The
103
106
  * Data Manager API has no partial-failure mode — a rejected request costs the whole batch, so
104
107
  * the builder is strict about only emitting events the API can accept. A batch beyond the
105
108
  * 2000-event request cap goes out as sequential requests; the first response is returned.
@@ -134,32 +137,27 @@ async function sendEvents(auth, config, events, data = {}, options = {}) {
134
137
  };
135
138
  if (chunk.some((dmEvent) => dmEvent.userData)) body.encoding = "HEX";
136
139
  if (options.consent) body.consent = options.consent;
137
- try {
138
- const response = await (0, _shware_utils.fetch)(ENDPOINT, {
139
- method: "POST",
140
- retries: 3,
141
- headers: {
142
- "Content-Type": "application/json",
143
- Accept: "application/json",
144
- Authorization: `Bearer ${auth.accessToken}`
145
- },
146
- body: JSON.stringify(body)
147
- });
148
- if (response.ok) {
149
- firstResponse ??= await response.json();
150
- continue;
151
- }
152
- const { status } = response;
153
- const message = await response.text();
154
- console.error(`Failed to send Google Ads conversions, status: ${status}, body: ${message}`);
155
- } catch (error) {
156
- const message = error instanceof Error ? error.message : String(error);
157
- console.error(`Failed to send Google Ads conversions, network error: ${message}`);
158
- }
140
+ const response = await (0, _shware_utils.fetch)(ENDPOINT, {
141
+ method: "POST",
142
+ retries: 3,
143
+ headers: {
144
+ "Content-Type": "application/json",
145
+ Accept: "application/json",
146
+ Authorization: `Bearer ${auth.accessToken}`
147
+ },
148
+ body: JSON.stringify(body)
149
+ });
150
+ if (!response.ok) throw new require_server_conversions_api_error.ConversionsApiError("google", response.status, await response.text());
151
+ firstResponse ??= await response.json();
159
152
  }
160
153
  return firstResponse;
161
154
  }
155
+ /** Google Ads through the Data Manager API: the events in `conversions`, each as its action. */
156
+ function googleAdsConversions(options) {
157
+ return require_server_setup_conversions.conversionDestination((name) => options.conversions[name] !== void 0, (events, data) => sendEvents(options.auth, options.conversions, events, data, options));
158
+ }
162
159
  exports.getDataManagerEvent = getDataManagerEvent;
160
+ exports.googleAdsConversions = googleAdsConversions;
163
161
  exports.normalizeEmail = normalizeEmail;
164
162
  exports.sendEvents = sendEvents;
165
163