@vercel/analytics 1.6.1 → 2.0.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 (60) hide show
  1. package/LICENSE +21 -373
  2. package/dist/astro/component.ts +1 -3
  3. package/dist/astro/index.astro +17 -2
  4. package/dist/index.d.mts +12 -7
  5. package/dist/index.d.ts +12 -7
  6. package/dist/index.js +66 -30
  7. package/dist/index.js.map +1 -1
  8. package/dist/index.mjs +66 -30
  9. package/dist/index.mjs.map +1 -1
  10. package/dist/next/index.d.mts +4 -1
  11. package/dist/next/index.d.ts +4 -1
  12. package/dist/next/index.js +88 -36
  13. package/dist/next/index.js.map +1 -1
  14. package/dist/next/index.mjs +88 -36
  15. package/dist/next/index.mjs.map +1 -1
  16. package/dist/nuxt/index.d.mts +3 -31
  17. package/dist/nuxt/index.d.ts +3 -31
  18. package/dist/nuxt/index.js +30 -191
  19. package/dist/nuxt/index.js.map +1 -1
  20. package/dist/nuxt/index.mjs +26 -180
  21. package/dist/nuxt/index.mjs.map +1 -1
  22. package/dist/nuxt/runtime/index.d.mts +51 -0
  23. package/dist/nuxt/runtime/index.d.ts +51 -0
  24. package/dist/nuxt/runtime/index.js +365 -0
  25. package/dist/nuxt/runtime/index.js.map +1 -0
  26. package/dist/nuxt/runtime/index.mjs +336 -0
  27. package/dist/nuxt/runtime/index.mjs.map +1 -0
  28. package/dist/react/index.d.mts +5 -1
  29. package/dist/react/index.d.ts +5 -1
  30. package/dist/react/index.js +80 -35
  31. package/dist/react/index.js.map +1 -1
  32. package/dist/react/index.mjs +80 -35
  33. package/dist/react/index.mjs.map +1 -1
  34. package/dist/remix/index.d.mts +4 -1
  35. package/dist/remix/index.d.ts +4 -1
  36. package/dist/remix/index.js +88 -36
  37. package/dist/remix/index.js.map +1 -1
  38. package/dist/remix/index.mjs +88 -36
  39. package/dist/remix/index.mjs.map +1 -1
  40. package/dist/server/index.js +15 -11
  41. package/dist/server/index.js.map +1 -1
  42. package/dist/server/index.mjs +13 -9
  43. package/dist/server/index.mjs.map +1 -1
  44. package/dist/sveltekit/index.d.mts +4 -1
  45. package/dist/sveltekit/index.d.ts +4 -1
  46. package/dist/sveltekit/index.js +82 -37
  47. package/dist/sveltekit/index.js.map +1 -1
  48. package/dist/sveltekit/index.mjs +83 -36
  49. package/dist/sveltekit/index.mjs.map +1 -1
  50. package/dist/vue/index.d.mts +4 -1
  51. package/dist/vue/index.d.ts +4 -1
  52. package/dist/vue/index.js +85 -37
  53. package/dist/vue/index.js.map +1 -1
  54. package/dist/vue/index.mjs +85 -37
  55. package/dist/vue/index.mjs.map +1 -1
  56. package/package.json +17 -22
  57. package/test.setup.ts +3 -0
  58. package/tsup.config.js +17 -2
  59. package/vitest.config.mts +0 -1
  60. package/jest.config.js +0 -9
@@ -39,18 +39,19 @@ var import_react2 = __toESM(require("react"));
39
39
  // src/react/index.tsx
40
40
  var import_react = require("react");
41
41
 
42
- // package.json
43
- var name = "@vercel/analytics";
44
- var version = "1.6.1";
45
-
46
42
  // src/queue.ts
47
43
  var initQueue = () => {
48
44
  if (window.va) return;
49
45
  window.va = function a(...params) {
50
- (window.vaq = window.vaq || []).push(params);
46
+ if (!window.vaq) window.vaq = [];
47
+ window.vaq.push(params);
51
48
  };
52
49
  };
53
50
 
51
+ // package.json
52
+ var name = "@vercel/analytics";
53
+ var version = "2.0.0";
54
+
54
55
  // src/utils.ts
55
56
  function isBrowser() {
56
57
  return typeof window !== "undefined";
@@ -61,7 +62,7 @@ function detectEnvironment() {
61
62
  if (env === "development" || env === "test") {
62
63
  return "development";
63
64
  }
64
- } catch (e) {
65
+ } catch {
65
66
  }
66
67
  return "production";
67
68
  }
@@ -103,7 +104,7 @@ function computeRoute(pathname, pathParams) {
103
104
  }
104
105
  }
105
106
  return result;
106
- } catch (e) {
107
+ } catch {
107
108
  return pathname;
108
109
  }
109
110
  }
@@ -115,55 +116,90 @@ function escapeRegExp(string) {
115
116
  }
116
117
  function getScriptSrc(props) {
117
118
  if (props.scriptSrc) {
118
- return props.scriptSrc;
119
+ return makeAbsolute(props.scriptSrc);
119
120
  }
120
121
  if (isDevelopment()) {
121
122
  return "https://va.vercel-scripts.com/v1/script.debug.js";
122
123
  }
123
124
  if (props.basePath) {
124
- return `${props.basePath}/insights/script.js`;
125
+ return makeAbsolute(`${props.basePath}/insights/script.js`);
125
126
  }
126
127
  return "/_vercel/insights/script.js";
127
128
  }
129
+ function loadProps(explicitProps, confString) {
130
+ var _a;
131
+ let props = explicitProps;
132
+ if (confString) {
133
+ try {
134
+ props = {
135
+ ...(_a = JSON.parse(confString)) == null ? void 0 : _a.analytics,
136
+ ...explicitProps
137
+ };
138
+ } catch {
139
+ }
140
+ }
141
+ setMode(props.mode);
142
+ const dataset = {
143
+ sdkn: name + (props.framework ? `/${props.framework}` : ""),
144
+ sdkv: version
145
+ };
146
+ if (props.disableAutoTrack) {
147
+ dataset.disableAutoTrack = "1";
148
+ }
149
+ if (props.viewEndpoint) {
150
+ dataset.viewEndpoint = makeAbsolute(props.viewEndpoint);
151
+ }
152
+ if (props.eventEndpoint) {
153
+ dataset.eventEndpoint = makeAbsolute(props.eventEndpoint);
154
+ }
155
+ if (props.sessionEndpoint) {
156
+ dataset.sessionEndpoint = makeAbsolute(props.sessionEndpoint);
157
+ }
158
+ if (isDevelopment() && props.debug === false) {
159
+ dataset.debug = "false";
160
+ }
161
+ if (props.dsn) {
162
+ dataset.dsn = props.dsn;
163
+ }
164
+ if (props.endpoint) {
165
+ dataset.endpoint = props.endpoint;
166
+ } else if (props.basePath) {
167
+ dataset.endpoint = makeAbsolute(`${props.basePath}/insights`);
168
+ }
169
+ return {
170
+ beforeSend: props.beforeSend,
171
+ src: getScriptSrc(props),
172
+ dataset
173
+ };
174
+ }
175
+ function makeAbsolute(url) {
176
+ return url.startsWith("http://") || url.startsWith("https://") || url.startsWith("/") ? url : `/${url}`;
177
+ }
128
178
 
129
179
  // src/generic.ts
130
180
  function inject(props = {
131
181
  debug: true
132
- }) {
182
+ }, confString) {
133
183
  var _a;
134
184
  if (!isBrowser()) return;
135
- setMode(props.mode);
185
+ const { beforeSend, src, dataset } = loadProps(props, confString);
136
186
  initQueue();
137
- if (props.beforeSend) {
138
- (_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
187
+ if (beforeSend) {
188
+ (_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", beforeSend);
139
189
  }
140
- const src = getScriptSrc(props);
141
190
  if (document.head.querySelector(`script[src*="${src}"]`)) return;
142
191
  const script = document.createElement("script");
143
192
  script.src = src;
144
- script.defer = true;
145
- script.dataset.sdkn = name + (props.framework ? `/${props.framework}` : "");
146
- script.dataset.sdkv = version;
147
- if (props.disableAutoTrack) {
148
- script.dataset.disableAutoTrack = "1";
149
- }
150
- if (props.endpoint) {
151
- script.dataset.endpoint = props.endpoint;
152
- } else if (props.basePath) {
153
- script.dataset.endpoint = `${props.basePath}/insights`;
154
- }
155
- if (props.dsn) {
156
- script.dataset.dsn = props.dsn;
193
+ for (const [key, value] of Object.entries(dataset)) {
194
+ script.dataset[key] = value;
157
195
  }
196
+ script.defer = true;
158
197
  script.onerror = () => {
159
198
  const errorMessage = isDevelopment() ? "Please check if any ad blockers are enabled and try again." : "Be sure to enable Web Analytics for your project and deploy again. See https://vercel.com/docs/analytics/quickstart for more information.";
160
199
  console.log(
161
200
  `[Vercel Web Analytics] Failed to load script from ${src}. ${errorMessage}`
162
201
  );
163
202
  };
164
- if (isDevelopment() && props.debug === false) {
165
- script.dataset.debug = "false";
166
- }
167
203
  document.head.appendChild(script);
168
204
  }
169
205
  function pageview({
@@ -181,6 +217,12 @@ function getBasePath() {
181
217
  }
182
218
  return process.env.REACT_APP_VERCEL_OBSERVABILITY_BASEPATH;
183
219
  }
220
+ function getConfigString() {
221
+ if (typeof process === "undefined" || typeof process.env === "undefined") {
222
+ return void 0;
223
+ }
224
+ return process.env.REACT_APP_VERCEL_OBSERVABILITY_CLIENT_CONFIG;
225
+ }
184
226
 
185
227
  // src/react/index.tsx
186
228
  function Analytics(props) {
@@ -191,12 +233,15 @@ function Analytics(props) {
191
233
  }
192
234
  }, [props.beforeSend]);
193
235
  (0, import_react.useEffect)(() => {
194
- inject({
195
- framework: props.framework || "react",
196
- basePath: props.basePath ?? getBasePath(),
197
- ...props.route !== void 0 && { disableAutoTrack: true },
198
- ...props
199
- });
236
+ inject(
237
+ {
238
+ framework: props.framework || "react",
239
+ basePath: props.basePath ?? getBasePath(),
240
+ ...props.route !== void 0 && { disableAutoTrack: true },
241
+ ...props
242
+ },
243
+ props.configString ?? getConfigString()
244
+ );
200
245
  }, []);
201
246
  (0, import_react.useEffect)(() => {
202
247
  if (props.route && props.path) {
@@ -224,6 +269,12 @@ function getBasePath2() {
224
269
  }
225
270
  return process.env.NEXT_PUBLIC_VERCEL_OBSERVABILITY_BASEPATH;
226
271
  }
272
+ function getConfigString2() {
273
+ if (typeof process === "undefined" || typeof process.env === "undefined") {
274
+ return void 0;
275
+ }
276
+ return process.env.NEXT_PUBLIC_VERCEL_OBSERVABILITY_CLIENT_CONFIG;
277
+ }
227
278
 
228
279
  // src/nextjs/index.tsx
229
280
  function AnalyticsComponent(props) {
@@ -235,6 +286,7 @@ function AnalyticsComponent(props) {
235
286
  route,
236
287
  ...props,
237
288
  basePath: getBasePath2(),
289
+ configString: getConfigString2(),
238
290
  framework: "next"
239
291
  }
240
292
  );
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/nextjs/index.tsx","../../src/react/index.tsx","../../package.json","../../src/queue.ts","../../src/utils.ts","../../src/generic.ts","../../src/react/utils.ts","../../src/nextjs/utils.ts"],"sourcesContent":["'use client';\nimport React, { Suspense, type ReactNode } from 'react';\nimport { Analytics as AnalyticsScript } from '../react';\nimport type { AnalyticsProps, BeforeSend, BeforeSendEvent } from '../types';\nimport { getBasePath, useRoute } from './utils';\n\ntype Props = Omit<AnalyticsProps, 'route' | 'disableAutoTrack'>;\n\nfunction AnalyticsComponent(props: Props): ReactNode {\n const { route, path } = useRoute();\n return (\n <AnalyticsScript\n path={path}\n route={route}\n {...props}\n basePath={getBasePath()}\n framework=\"next\"\n />\n );\n}\n\nexport function Analytics(props: Props): null {\n // Because of incompatible types between ReactNode in React 19 and React 18 we return null (which is also what we render)\n return (\n <Suspense fallback={null}>\n <AnalyticsComponent {...props} />\n </Suspense>\n ) as never;\n}\n\nexport type { AnalyticsProps, BeforeSend, BeforeSendEvent };\n","'use client';\nimport { useEffect } from 'react';\nimport { inject, track, pageview } from '../generic';\nimport type { AnalyticsProps, BeforeSend, BeforeSendEvent } from '../types';\nimport { getBasePath } from './utils';\n\n/**\n * Injects the Vercel Web Analytics script into the page head and starts tracking page views. Read more in our [documentation](https://vercel.com/docs/concepts/analytics/package).\n * @param [props] - Analytics options.\n * @param [props.mode] - The mode to use for the analytics script. Defaults to `auto`.\n * - `auto` - Automatically detect the environment. Uses `production` if the environment cannot be determined.\n * - `production` - Always use the production script. (Sends events to the server)\n * - `development` - Always use the development script. (Logs events to the console)\n * @param [props.debug] - Whether to enable debug logging in development. Defaults to `true`.\n * @param [props.beforeSend] - A middleware function to modify events before they are sent. Should return the event object or `null` to cancel the event.\n * @example\n * ```js\n * import { Analytics } from '@vercel/analytics/react';\n *\n * export default function App() {\n * return (\n * <div>\n * <Analytics />\n * <h1>My App</h1>\n * </div>\n * );\n * }\n * ```\n */\nfunction Analytics(\n props: AnalyticsProps & {\n framework?: string;\n route?: string | null;\n path?: string | null;\n basePath?: string;\n }\n): null {\n useEffect(() => {\n if (props.beforeSend) {\n window.va?.('beforeSend', props.beforeSend);\n }\n }, [props.beforeSend]);\n\n // biome-ignore lint/correctness/useExhaustiveDependencies: only run once\n useEffect(() => {\n inject({\n framework: props.framework || 'react',\n basePath: props.basePath ?? getBasePath(),\n ...(props.route !== undefined && { disableAutoTrack: true }),\n ...props,\n });\n // eslint-disable-next-line react-hooks/exhaustive-deps -- only run once\n }, []);\n\n useEffect(() => {\n // explicitely track page view, since we disabled auto tracking\n if (props.route && props.path) {\n pageview({ route: props.route, path: props.path });\n }\n }, [props.route, props.path]);\n\n return null;\n}\n\nexport { track, Analytics };\nexport type { AnalyticsProps, BeforeSend, BeforeSendEvent };\n","{\n \"name\": \"@vercel/analytics\",\n \"version\": \"1.6.1\",\n \"description\": \"Gain real-time traffic insights with Vercel Web Analytics\",\n \"keywords\": [\n \"analytics\",\n \"vercel\"\n ],\n \"repository\": {\n \"url\": \"github:vercel/analytics\",\n \"directory\": \"packages/web\"\n },\n \"license\": \"MPL-2.0\",\n \"exports\": {\n \"./package.json\": \"./package.json\",\n \".\": {\n \"browser\": \"./dist/index.mjs\",\n \"import\": \"./dist/index.mjs\",\n \"require\": \"./dist/index.js\"\n },\n \"./astro\": {\n \"import\": \"./dist/astro/component.ts\"\n },\n \"./next\": {\n \"browser\": \"./dist/next/index.mjs\",\n \"import\": \"./dist/next/index.mjs\",\n \"require\": \"./dist/next/index.js\"\n },\n \"./nuxt\": {\n \"browser\": \"./dist/nuxt/index.mjs\",\n \"import\": \"./dist/nuxt/index.mjs\",\n \"require\": \"./dist/nuxt/index.js\"\n },\n \"./react\": {\n \"browser\": \"./dist/react/index.mjs\",\n \"import\": \"./dist/react/index.mjs\",\n \"require\": \"./dist/react/index.js\"\n },\n \"./remix\": {\n \"browser\": \"./dist/remix/index.mjs\",\n \"import\": \"./dist/remix/index.mjs\",\n \"require\": \"./dist/remix/index.js\"\n },\n \"./server\": {\n \"node\": \"./dist/server/index.mjs\",\n \"edge-light\": \"./dist/server/index.mjs\",\n \"import\": \"./dist/server/index.mjs\",\n \"require\": \"./dist/server/index.js\",\n \"default\": \"./dist/server/index.js\"\n },\n \"./sveltekit\": {\n \"svelte\": \"./dist/sveltekit/index.mjs\",\n \"types\": \"./dist/sveltekit/index.d.ts\"\n },\n \"./vue\": {\n \"browser\": \"./dist/vue/index.mjs\",\n \"import\": \"./dist/vue/index.mjs\",\n \"require\": \"./dist/vue/index.js\"\n }\n },\n \"main\": \"./dist/index.mjs\",\n \"types\": \"./dist/index.d.ts\",\n \"typesVersions\": {\n \"*\": {\n \"*\": [\n \"dist/index.d.ts\"\n ],\n \"next\": [\n \"dist/next/index.d.ts\"\n ],\n \"nuxt\": [\n \"dist/nuxt/index.d.ts\"\n ],\n \"react\": [\n \"dist/react/index.d.ts\"\n ],\n \"remix\": [\n \"dist/remix/index.d.ts\"\n ],\n \"server\": [\n \"dist/server/index.d.ts\"\n ],\n \"sveltekit\": [\n \"dist/sveltekit/index.d.ts\"\n ],\n \"vue\": [\n \"dist/vue/index.d.ts\"\n ]\n }\n },\n \"scripts\": {\n \"build\": \"tsup && pnpm copy-astro\",\n \"copy-astro\": \"cp -R src/astro dist/\",\n \"dev\": \"pnpm copy-astro && tsup --watch\",\n \"lint\": \"eslint .\",\n \"lint-fix\": \"eslint . --fix\",\n \"test\": \"vitest\",\n \"type-check\": \"tsc --noEmit\"\n },\n \"eslintConfig\": {\n \"extends\": [\n \"@vercel/eslint-config\"\n ],\n \"rules\": {\n \"tsdoc/syntax\": \"off\"\n },\n \"ignorePatterns\": [\n \"jest.setup.ts\"\n ]\n },\n \"devDependencies\": {\n \"@swc/core\": \"^1.9.2\",\n \"@testing-library/jest-dom\": \"^6.6.3\",\n \"@testing-library/react\": \"^16.0.1\",\n \"@types/node\": \"^22.9.0\",\n \"@types/react\": \"^18.3.12\",\n \"@vercel/eslint-config\": \"workspace:0.0.0\",\n \"server-only\": \"^0.0.1\",\n \"svelte\": \"^5.1.10\",\n \"tsup\": \"8.3.5\",\n \"vitest\": \"^2.1.5\",\n \"vue\": \"^3.5.12\",\n \"vue-router\": \"^4.4.5\"\n },\n \"peerDependencies\": {\n \"@remix-run/react\": \"^2\",\n \"@sveltejs/kit\": \"^1 || ^2\",\n \"next\": \">= 13\",\n \"react\": \"^18 || ^19 || ^19.0.0-rc\",\n \"svelte\": \">= 4\",\n \"vue\": \"^3\",\n \"vue-router\": \"^4\"\n },\n \"peerDependenciesMeta\": {\n \"@remix-run/react\": {\n \"optional\": true\n },\n \"@sveltejs/kit\": {\n \"optional\": true\n },\n \"next\": {\n \"optional\": true\n },\n \"react\": {\n \"optional\": true\n },\n \"svelte\": {\n \"optional\": true\n },\n \"vue\": {\n \"optional\": true\n },\n \"vue-router\": {\n \"optional\": true\n }\n }\n}\n","export const initQueue = (): void => {\n // initialize va until script is loaded\n if (window.va) return;\n\n window.va = function a(...params): void {\n (window.vaq = window.vaq || []).push(params);\n };\n};\n","import type { AllowedPropertyValues, AnalyticsProps, Mode } from './types';\n\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nfunction detectEnvironment(): 'development' | 'production' {\n try {\n const env = process.env.NODE_ENV;\n if (env === 'development' || env === 'test') {\n return 'development';\n }\n } catch (e) {\n // do nothing, this is okay\n }\n return 'production';\n}\n\nexport function setMode(mode: Mode = 'auto'): void {\n if (mode === 'auto') {\n window.vam = detectEnvironment();\n return;\n }\n\n window.vam = mode;\n}\n\nexport function getMode(): Mode {\n const mode = isBrowser() ? window.vam : detectEnvironment();\n return mode || 'production';\n}\n\nexport function isProduction(): boolean {\n return getMode() === 'production';\n}\n\nexport function isDevelopment(): boolean {\n return getMode() === 'development';\n}\n\nfunction removeKey(\n key: string,\n { [key]: _, ...rest }\n): Record<string, unknown> {\n return rest;\n}\n\nexport function parseProperties(\n properties: Record<string, unknown> | undefined,\n options: {\n strip?: boolean;\n }\n): Error | Record<string, AllowedPropertyValues> | undefined {\n if (!properties) return undefined;\n let props = properties;\n const errorProperties: string[] = [];\n for (const [key, value] of Object.entries(properties)) {\n if (typeof value === 'object' && value !== null) {\n if (options.strip) {\n props = removeKey(key, props);\n } else {\n errorProperties.push(key);\n }\n }\n }\n\n if (errorProperties.length > 0 && !options.strip) {\n throw Error(\n `The following properties are not valid: ${errorProperties.join(\n ', '\n )}. Only strings, numbers, booleans, and null are allowed.`\n );\n }\n return props as Record<string, AllowedPropertyValues>;\n}\n\nexport function computeRoute(\n pathname: string | null,\n pathParams: Record<string, string | string[]> | null\n): string | null {\n if (!pathname || !pathParams) {\n return pathname;\n }\n\n let result = pathname;\n try {\n const entries = Object.entries(pathParams);\n // simple keys must be handled first\n for (const [key, value] of entries) {\n if (!Array.isArray(value)) {\n const matcher = turnValueToRegExp(value);\n if (matcher.test(result)) {\n result = result.replace(matcher, `/[${key}]`);\n }\n }\n }\n // array values next\n for (const [key, value] of entries) {\n if (Array.isArray(value)) {\n const matcher = turnValueToRegExp(value.join('/'));\n if (matcher.test(result)) {\n result = result.replace(matcher, `/[...${key}]`);\n }\n }\n }\n return result;\n } catch (e) {\n return pathname;\n }\n}\n\nfunction turnValueToRegExp(value: string): RegExp {\n return new RegExp(`/${escapeRegExp(value)}(?=[/?#]|$)`);\n}\n\nfunction escapeRegExp(string: string): string {\n return string.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n}\n\nexport function getScriptSrc(\n props: AnalyticsProps & { basePath?: string }\n): string {\n if (props.scriptSrc) {\n return props.scriptSrc;\n }\n if (isDevelopment()) {\n return 'https://va.vercel-scripts.com/v1/script.debug.js';\n }\n if (props.basePath) {\n return `${props.basePath}/insights/script.js`;\n }\n return '/_vercel/insights/script.js';\n}\n","import { name as packageName, version } from '../package.json';\nimport { initQueue } from './queue';\nimport type {\n AllowedPropertyValues,\n AnalyticsProps,\n FlagsDataInput,\n BeforeSend,\n BeforeSendEvent,\n} from './types';\nimport {\n isBrowser,\n parseProperties,\n setMode,\n isDevelopment,\n isProduction,\n computeRoute,\n getScriptSrc,\n} from './utils';\n\n/**\n * Injects the Vercel Web Analytics script into the page head and starts tracking page views. Read more in our [documentation](https://vercel.com/docs/concepts/analytics/package).\n * @param [props] - Analytics options.\n * @param [props.mode] - The mode to use for the analytics script. Defaults to `auto`.\n * - `auto` - Automatically detect the environment. Uses `production` if the environment cannot be determined.\n * - `production` - Always use the production script. (Sends events to the server)\n * - `development` - Always use the development script. (Logs events to the console)\n * @param [props.debug] - Whether to enable debug logging in development. Defaults to `true`.\n * @param [props.beforeSend] - A middleware function to modify events before they are sent. Should return the event object or `null` to cancel the event.\n * @param [props.dsn] - The DSN of the project to send events to. Only required when self-hosting.\n * @param [props.disableAutoTrack] - Whether the injected script should track page views from pushState events. Disable if route is updated after pushState, a manually call page pageview().\n */\nfunction inject(\n props: AnalyticsProps & {\n framework?: string;\n disableAutoTrack?: boolean;\n basePath?: string;\n } = {\n debug: true,\n }\n): void {\n if (!isBrowser()) return;\n\n setMode(props.mode);\n\n initQueue();\n\n if (props.beforeSend) {\n window.va?.('beforeSend', props.beforeSend);\n }\n\n const src = getScriptSrc(props);\n\n if (document.head.querySelector(`script[src*=\"${src}\"]`)) return;\n\n const script = document.createElement('script');\n script.src = src;\n script.defer = true;\n script.dataset.sdkn =\n packageName + (props.framework ? `/${props.framework}` : '');\n script.dataset.sdkv = version;\n\n if (props.disableAutoTrack) {\n script.dataset.disableAutoTrack = '1';\n }\n if (props.endpoint) {\n script.dataset.endpoint = props.endpoint;\n } else if (props.basePath) {\n script.dataset.endpoint = `${props.basePath}/insights`;\n }\n if (props.dsn) {\n script.dataset.dsn = props.dsn;\n }\n\n script.onerror = (): void => {\n const errorMessage = isDevelopment()\n ? 'Please check if any ad blockers are enabled and try again.'\n : 'Be sure to enable Web Analytics for your project and deploy again. See https://vercel.com/docs/analytics/quickstart for more information.';\n\n // eslint-disable-next-line no-console -- Logging to console is intentional\n console.log(\n `[Vercel Web Analytics] Failed to load script from ${src}. ${errorMessage}`\n );\n };\n\n if (isDevelopment() && props.debug === false) {\n script.dataset.debug = 'false';\n }\n\n document.head.appendChild(script);\n}\n\n/**\n * Tracks a custom event. Please refer to the [documentation](https://vercel.com/docs/concepts/analytics/custom-events) for more information on custom events.\n * @param name - The name of the event.\n * * Examples: `Purchase`, `Click Button`, or `Play Video`.\n * @param [properties] - Additional properties of the event. Nested objects are not supported. Allowed values are `string`, `number`, `boolean`, and `null`.\n */\nfunction track(\n name: string,\n properties?: Record<string, AllowedPropertyValues>,\n options?: {\n flags?: FlagsDataInput;\n }\n): void {\n if (!isBrowser()) {\n const msg =\n '[Vercel Web Analytics] Please import `track` from `@vercel/analytics/server` when using this function in a server environment';\n\n if (isProduction()) {\n // eslint-disable-next-line no-console -- Show warning in production\n console.warn(msg);\n } else {\n throw new Error(msg);\n }\n\n return;\n }\n\n if (!properties) {\n window.va?.('event', { name, options });\n return;\n }\n\n try {\n const props = parseProperties(properties, {\n strip: isProduction(),\n });\n\n window.va?.('event', {\n name,\n data: props,\n options,\n });\n } catch (err) {\n if (err instanceof Error && isDevelopment()) {\n // eslint-disable-next-line no-console -- Logging to console is intentional\n console.error(err);\n }\n }\n}\n\nfunction pageview({\n route,\n path,\n}: {\n route?: string | null;\n path?: string;\n}): void {\n window.va?.('pageview', { route, path });\n}\n\nexport { inject, track, pageview, computeRoute };\nexport type { AnalyticsProps, BeforeSend, BeforeSendEvent };\n\n// eslint-disable-next-line import/no-default-export -- Default export is intentional\nexport default {\n inject,\n track,\n computeRoute,\n};\n","export function getBasePath(): string | undefined {\n // !! important !!\n // do not access env variables using process.env[varname]\n // some bundles won't replace the value at build time.\n // eslint-disable-next-line @typescript-eslint/prefer-optional-chain -- we can't use optionnal here, it'll break if process does not exist.\n if (typeof process === 'undefined' || typeof process.env === 'undefined') {\n return undefined;\n }\n return process.env.REACT_APP_VERCEL_OBSERVABILITY_BASEPATH;\n}\n","'use client';\n/* eslint-disable @typescript-eslint/no-unnecessary-condition -- can be empty in pages router */\nimport { useParams, usePathname, useSearchParams } from 'next/navigation.js';\nimport { computeRoute } from '../utils';\n\nexport const useRoute = (): {\n route: string | null;\n path: string;\n} => {\n const params = useParams();\n const searchParams = useSearchParams();\n const path = usePathname();\n\n // Until we have route parameters, we don't compute the route\n if (!params) {\n return { route: null, path };\n }\n // in Next.js@13, useParams() could return an empty object for pages router, and we default to searchParams.\n const finalParams = Object.keys(params).length\n ? params\n : Object.fromEntries(searchParams.entries());\n return { route: computeRoute(path, finalParams), path };\n};\n\nexport function getBasePath(): string | undefined {\n // !! important !!\n // do not access env variables using process.env[varname]\n // some bundles won't replace the value at build time.\n // eslint-disable-next-line @typescript-eslint/prefer-optional-chain -- we can't use optionnal here, it'll break if process does not exist.\n if (typeof process === 'undefined' || typeof process.env === 'undefined') {\n return undefined;\n }\n return process.env.NEXT_PUBLIC_VERCEL_OBSERVABILITY_BASEPATH;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,mBAAAA;AAAA;AAAA;AACA,IAAAC,gBAAgD;;;ACAhD,mBAA0B;;;ACAxB,WAAQ;AACR,cAAW;;;ACFN,IAAM,YAAY,MAAY;AAEnC,MAAI,OAAO,GAAI;AAEf,SAAO,KAAK,SAAS,KAAK,QAAc;AACtC,KAAC,OAAO,MAAM,OAAO,OAAO,CAAC,GAAG,KAAK,MAAM;AAAA,EAC7C;AACF;;;ACLO,SAAS,YAAqB;AACnC,SAAO,OAAO,WAAW;AAC3B;AAEA,SAAS,oBAAkD;AACzD,MAAI;AACF,UAAM,MAAM,QAAQ,IAAI;AACxB,QAAI,QAAQ,iBAAiB,QAAQ,QAAQ;AAC3C,aAAO;AAAA,IACT;AAAA,EACF,SAAS,GAAG;AAAA,EAEZ;AACA,SAAO;AACT;AAEO,SAAS,QAAQ,OAAa,QAAc;AACjD,MAAI,SAAS,QAAQ;AACnB,WAAO,MAAM,kBAAkB;AAC/B;AAAA,EACF;AAEA,SAAO,MAAM;AACf;AAEO,SAAS,UAAgB;AAC9B,QAAM,OAAO,UAAU,IAAI,OAAO,MAAM,kBAAkB;AAC1D,SAAO,QAAQ;AACjB;AAMO,SAAS,gBAAyB;AACvC,SAAO,QAAQ,MAAM;AACvB;AAsCO,SAAS,aACd,UACA,YACe;AACf,MAAI,CAAC,YAAY,CAAC,YAAY;AAC5B,WAAO;AAAA,EACT;AAEA,MAAI,SAAS;AACb,MAAI;AACF,UAAM,UAAU,OAAO,QAAQ,UAAU;AAEzC,eAAW,CAAC,KAAK,KAAK,KAAK,SAAS;AAClC,UAA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AAA,EAEH,GAAG,CAAC,CAAC;AAEL,8BAAU,MAAM;AAEd,QAAI,MAAM,SAAS,MAAM,MAAM;AAC7B,eAAS,EAAE,OAAO,MAAM,OAAO,MAAM,MAAM,KAAK,CAAC;AAAA,IACnD;AAAA,EACF,GAAG,CAAC,MAAM,OAAO,MAAM,IAAI,CAAC;AAE5B,SAAO;AACT;;;AM5DA,wBAAwD;AAGjD,IAAM,WAAW,MAGnB;AACH,QAAM,aAAS,6BAAU;AACzB,QAAM,mBAAe,mCAAgB;AACrC,QAAM,WAAO,+BAAY;AAGzB,MAAI,CAAC,QAAQ;AACX,WAAO,EAAE,OAAO,MAAM,KAAK;AAAA,EAC7B;AAEA,QAAM,cAAc,OAAO,KAAK,MAAM,EAAE,SACpC,SACA,OAAO,YAAY,aAAa,QAAQ,CAAC;AAC7C,SAAO,EAAE,OAAO,aAAa,MAAM,WAAW,GAAG,KAAK;AACxD;AAEO,SAASC,eAAkC;AAKhD,MAAI,OAAO,YAAY,eAAe,OAAO,QAAQ,QAAQ,aAAa;AACxE,WAAO;AAAA,EACT;AACA,SAAO,QAAQ,IAAI;AACrB;;;APzBA,SAAS,mBAAmB,OAAyB;AACnD,QAAM,EAAE,OAAO,KAAK,IAAI,SAAS;AACjC,SACE,8BAAAC,QAAA;AAAA,IAAC;AAAA;AAAA,MACC;AAAA,MACA;AAAA,MACC,GAAG;AAAA,MACJ,UAAUC,aAAY;AAAA,MACtB,WAAU;AAAA;AAAA,EACZ;AAEJ;AAEO,SAASC,WAAU,OAAoB;AAE5C,SACE,8BAAAF,QAAA,cAAC,0BAAS,UAAU,QAClB,8BAAAA,QAAA,cAAC,sBAAoB,GAAG,OAAO,CACjC;AAEJ;","names":["Analytics","import_react","getBasePath","React","getBasePath","Analytics"]}
1
+ {"version":3,"sources":["../../src/nextjs/index.tsx","../../src/react/index.tsx","../../src/queue.ts","../../package.json","../../src/utils.ts","../../src/generic.ts","../../src/react/utils.ts","../../src/nextjs/utils.ts"],"sourcesContent":["'use client';\nimport React, { type ReactNode, Suspense } from 'react';\nimport { Analytics as AnalyticsScript } from '../react';\nimport type { AnalyticsProps, BeforeSend, BeforeSendEvent } from '../types';\nimport { getBasePath, getConfigString, useRoute } from './utils';\n\ntype Props = Omit<AnalyticsProps, 'route' | 'disableAutoTrack'>;\n\nfunction AnalyticsComponent(props: Props): ReactNode {\n const { route, path } = useRoute();\n return (\n <AnalyticsScript\n path={path}\n route={route}\n {...props}\n basePath={getBasePath()}\n configString={getConfigString()}\n framework=\"next\"\n />\n );\n}\n\nexport function Analytics(props: Props): null {\n // Because of incompatible types between ReactNode in React 19 and React 18 we return null (which is also what we render)\n return (\n <Suspense fallback={null}>\n <AnalyticsComponent {...props} />\n </Suspense>\n ) as never;\n}\n\nexport type { AnalyticsProps, BeforeSend, BeforeSendEvent };\n","'use client';\nimport { useEffect } from 'react';\nimport { inject, pageview, track } from '../generic';\nimport type { AnalyticsProps, BeforeSend, BeforeSendEvent } from '../types';\nimport { getBasePath, getConfigString } from './utils';\n\n/**\n * Injects the Vercel Web Analytics script into the page head and starts tracking page views. Read more in our [documentation](https://vercel.com/docs/concepts/analytics/package).\n * @param [props] - Analytics options.\n * @param [props.mode] - The mode to use for the analytics script. Defaults to `auto`.\n * - `auto` - Automatically detect the environment. Uses `production` if the environment cannot be determined.\n * - `production` - Always use the production script. (Sends events to the server)\n * - `development` - Always use the development script. (Logs events to the console)\n * @param [props.debug] - Whether to enable debug logging in development. Defaults to `true`.\n * @param [props.beforeSend] - A middleware function to modify events before they are sent. Should return the event object or `null` to cancel the event.\n * @example\n * ```js\n * import { Analytics } from '@vercel/analytics/react';\n *\n * export default function App() {\n * return (\n * <div>\n * <Analytics />\n * <h1>My App</h1>\n * </div>\n * );\n * }\n * ```\n */\nfunction Analytics(\n props: AnalyticsProps & {\n framework?: string;\n route?: string | null;\n path?: string | null;\n basePath?: string;\n configString?: string;\n },\n): null {\n useEffect(() => {\n if (props.beforeSend) {\n window.va?.('beforeSend', props.beforeSend);\n }\n }, [props.beforeSend]);\n\n // biome-ignore lint/correctness/useExhaustiveDependencies: only run once\n useEffect(() => {\n inject(\n {\n framework: props.framework || 'react',\n basePath: props.basePath ?? getBasePath(),\n ...(props.route !== undefined && { disableAutoTrack: true }),\n ...props,\n },\n props.configString ?? getConfigString(),\n );\n }, []);\n\n useEffect(() => {\n // explicitely track page view, since we disabled auto tracking\n if (props.route && props.path) {\n pageview({ route: props.route, path: props.path });\n }\n }, [props.route, props.path]);\n\n return null;\n}\n\nexport { track, Analytics };\nexport type { AnalyticsProps, BeforeSend, BeforeSendEvent };\n","export const initQueue = (): void => {\n // initialize va until script is loaded\n if (window.va) return;\n\n window.va = function a(...params): void {\n if (!window.vaq) window.vaq = [];\n window.vaq.push(params);\n };\n};\n","{\n \"name\": \"@vercel/analytics\",\n \"version\": \"2.0.0\",\n \"description\": \"Gain real-time traffic insights with Vercel Web Analytics\",\n \"keywords\": [\n \"analytics\",\n \"vercel\"\n ],\n \"repository\": {\n \"url\": \"github:vercel/analytics\",\n \"directory\": \"packages/web\"\n },\n \"license\": \"MIT\",\n \"exports\": {\n \"./package.json\": \"./package.json\",\n \".\": {\n \"browser\": \"./dist/index.mjs\",\n \"import\": \"./dist/index.mjs\",\n \"require\": \"./dist/index.js\"\n },\n \"./astro\": {\n \"import\": \"./dist/astro/component.ts\"\n },\n \"./next\": {\n \"browser\": \"./dist/next/index.mjs\",\n \"import\": \"./dist/next/index.mjs\",\n \"require\": \"./dist/next/index.js\"\n },\n \"./nuxt\": {\n \"import\": \"./dist/nuxt/index.mjs\",\n \"require\": \"./dist/nuxt/index.js\"\n },\n \"./nuxt/runtime\": {\n \"browser\": \"./dist/nuxt/runtime/index.mjs\",\n \"import\": \"./dist/nuxt/runtime/index.mjs\",\n \"require\": \"./dist/nuxt/runtime/index.js\"\n },\n \"./react\": {\n \"browser\": \"./dist/react/index.mjs\",\n \"import\": \"./dist/react/index.mjs\",\n \"require\": \"./dist/react/index.js\"\n },\n \"./remix\": {\n \"browser\": \"./dist/remix/index.mjs\",\n \"import\": \"./dist/remix/index.mjs\",\n \"require\": \"./dist/remix/index.js\"\n },\n \"./server\": {\n \"node\": \"./dist/server/index.mjs\",\n \"edge-light\": \"./dist/server/index.mjs\",\n \"import\": \"./dist/server/index.mjs\",\n \"require\": \"./dist/server/index.js\",\n \"default\": \"./dist/server/index.js\"\n },\n \"./sveltekit\": {\n \"svelte\": \"./dist/sveltekit/index.mjs\",\n \"types\": \"./dist/sveltekit/index.d.ts\"\n },\n \"./vue\": {\n \"browser\": \"./dist/vue/index.mjs\",\n \"import\": \"./dist/vue/index.mjs\",\n \"require\": \"./dist/vue/index.js\"\n }\n },\n \"main\": \"./dist/index.mjs\",\n \"types\": \"./dist/index.d.ts\",\n \"typesVersions\": {\n \"*\": {\n \"*\": [\n \"dist/index.d.ts\"\n ],\n \"next\": [\n \"dist/next/index.d.ts\"\n ],\n \"nuxt\": [\n \"dist/nuxt/index.d.ts\"\n ],\n \"nuxt/runtime\": [\n \"dist/nuxt/runtime/index.d.ts\"\n ],\n \"react\": [\n \"dist/react/index.d.ts\"\n ],\n \"remix\": [\n \"dist/remix/index.d.ts\"\n ],\n \"server\": [\n \"dist/server/index.d.ts\"\n ],\n \"sveltekit\": [\n \"dist/sveltekit/index.d.ts\"\n ],\n \"vue\": [\n \"dist/vue/index.d.ts\"\n ]\n }\n },\n \"scripts\": {\n \"build\": \"tsup && pnpm copy-astro\",\n \"copy-astro\": \"cp -R src/astro dist/\",\n \"dev\": \"pnpm copy-astro && tsup --watch\",\n \"test\": \"vitest\"\n },\n \"devDependencies\": {\n \"@nuxt/kit\": \"^4.2.0\",\n \"@nuxt/schema\": \"^4.2.0\",\n \"@swc/core\": \"^1.9.2\",\n \"@testing-library/jest-dom\": \"^6.9.1\",\n \"@testing-library/react\": \"^16.3.1\",\n \"@types/node\": \"^22.9.0\",\n \"@types/react\": \"^18.3.12\",\n \"server-only\": \"^0.0.1\",\n \"svelte\": \"^5.1.10\",\n \"tsup\": \"8.3.5\",\n \"vitest\": \"^4.0.16\",\n \"vue\": \"^3.5.12\",\n \"vue-router\": \"^4.4.5\"\n },\n \"peerDependencies\": {\n \"@remix-run/react\": \"^2\",\n \"@sveltejs/kit\": \"^1 || ^2\",\n \"next\": \">= 13\",\n \"nuxt\": \">= 3\",\n \"react\": \"^18 || ^19 || ^19.0.0-rc\",\n \"svelte\": \">= 4\",\n \"vue\": \"^3\",\n \"vue-router\": \"^4\"\n },\n \"peerDependenciesMeta\": {\n \"@remix-run/react\": {\n \"optional\": true\n },\n \"@sveltejs/kit\": {\n \"optional\": true\n },\n \"next\": {\n \"optional\": true\n },\n \"react\": {\n \"optional\": true\n },\n \"svelte\": {\n \"optional\": true\n },\n \"vue\": {\n \"optional\": true\n },\n \"vue-router\": {\n \"optional\": true\n }\n }\n}\n","import { name as packageName, version } from '../package.json';\nimport type {\n AllowedPropertyValues,\n AnalyticsProps,\n BeforeSend,\n InjectProps,\n Mode,\n} from './types';\n\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nfunction detectEnvironment(): 'development' | 'production' {\n try {\n const env = process.env.NODE_ENV;\n if (env === 'development' || env === 'test') {\n return 'development';\n }\n } catch {\n // do nothing, this is okay\n }\n return 'production';\n}\n\nexport function setMode(mode: Mode = 'auto'): void {\n if (mode === 'auto') {\n window.vam = detectEnvironment();\n return;\n }\n\n window.vam = mode;\n}\n\nexport function getMode(): Mode {\n const mode = isBrowser() ? window.vam : detectEnvironment();\n return mode || 'production';\n}\n\nexport function isProduction(): boolean {\n return getMode() === 'production';\n}\n\nexport function isDevelopment(): boolean {\n return getMode() === 'development';\n}\n\nfunction removeKey(\n key: string,\n { [key]: _, ...rest },\n): Record<string, unknown> {\n return rest;\n}\n\nexport function parseProperties(\n properties: Record<string, unknown> | undefined,\n options: {\n strip?: boolean;\n },\n): Error | Record<string, AllowedPropertyValues> | undefined {\n if (!properties) return undefined;\n let props = properties;\n const errorProperties: string[] = [];\n for (const [key, value] of Object.entries(properties)) {\n if (typeof value === 'object' && value !== null) {\n if (options.strip) {\n props = removeKey(key, props);\n } else {\n errorProperties.push(key);\n }\n }\n }\n\n if (errorProperties.length > 0 && !options.strip) {\n throw Error(\n `The following properties are not valid: ${errorProperties.join(\n ', ',\n )}. Only strings, numbers, booleans, and null are allowed.`,\n );\n }\n return props as Record<string, AllowedPropertyValues>;\n}\n\nexport function computeRoute(\n pathname: string | null,\n pathParams: Record<string, string | string[]> | null,\n): string | null {\n if (!pathname || !pathParams) {\n return pathname;\n }\n\n let result = pathname;\n try {\n const entries = Object.entries(pathParams);\n // simple keys must be handled first\n for (const [key, value] of entries) {\n if (!Array.isArray(value)) {\n const matcher = turnValueToRegExp(value);\n if (matcher.test(result)) {\n result = result.replace(matcher, `/[${key}]`);\n }\n }\n }\n // array values next\n for (const [key, value] of entries) {\n if (Array.isArray(value)) {\n const matcher = turnValueToRegExp(value.join('/'));\n if (matcher.test(result)) {\n result = result.replace(matcher, `/[...${key}]`);\n }\n }\n }\n return result;\n } catch {\n return pathname;\n }\n}\n\nfunction turnValueToRegExp(value: string): RegExp {\n return new RegExp(`/${escapeRegExp(value)}(?=[/?#]|$)`);\n}\n\nfunction escapeRegExp(string: string): string {\n return string.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n}\n\nfunction getScriptSrc(props: AnalyticsProps & { basePath?: string }): string {\n if (props.scriptSrc) {\n return makeAbsolute(props.scriptSrc);\n }\n if (isDevelopment()) {\n return 'https://va.vercel-scripts.com/v1/script.debug.js';\n }\n if (props.basePath) {\n return makeAbsolute(`${props.basePath}/insights/script.js`);\n }\n return '/_vercel/insights/script.js';\n}\n\nexport function loadProps(\n explicitProps: InjectProps,\n confString?: string,\n): {\n src: string;\n beforeSend?: BeforeSend;\n dataset: Record<string, string>;\n} {\n let props = explicitProps;\n if (confString) {\n try {\n props = {\n ...(JSON.parse(confString)?.analytics as InjectProps),\n ...explicitProps,\n };\n } catch {\n // do nothing\n }\n }\n setMode(props.mode);\n\n const dataset: Record<string, string> = {\n sdkn: packageName + (props.framework ? `/${props.framework}` : ''),\n sdkv: version,\n };\n if (props.disableAutoTrack) {\n dataset.disableAutoTrack = '1';\n }\n if (props.viewEndpoint) {\n dataset.viewEndpoint = makeAbsolute(props.viewEndpoint);\n }\n if (props.eventEndpoint) {\n dataset.eventEndpoint = makeAbsolute(props.eventEndpoint);\n }\n if (props.sessionEndpoint) {\n dataset.sessionEndpoint = makeAbsolute(props.sessionEndpoint);\n }\n if (isDevelopment() && props.debug === false) {\n dataset.debug = 'false';\n }\n if (props.dsn) {\n dataset.dsn = props.dsn;\n }\n // deprecated\n if (props.endpoint) {\n dataset.endpoint = props.endpoint;\n } else if (props.basePath) {\n dataset.endpoint = makeAbsolute(`${props.basePath}/insights`);\n }\n\n return {\n beforeSend: props.beforeSend,\n src: getScriptSrc(props),\n dataset,\n };\n}\n\nfunction makeAbsolute(url: string): string {\n return url.startsWith('http://') ||\n url.startsWith('https://') ||\n url.startsWith('/')\n ? url\n : `/${url}`;\n}\n","import { initQueue } from './queue';\nimport type {\n AllowedPropertyValues,\n AnalyticsProps,\n BeforeSend,\n BeforeSendEvent,\n FlagsDataInput,\n InjectProps,\n} from './types';\nimport {\n computeRoute,\n isBrowser,\n isDevelopment,\n isProduction,\n loadProps,\n parseProperties,\n} from './utils';\n\n/**\n * Injects the Vercel Web Analytics script into the page head and starts tracking page views. Read more in our [documentation](https://vercel.com/docs/concepts/analytics/package).\n * @param props - Analytics options.\n * @param [props.mode] - The mode to use for the analytics script. Defaults to `auto`.\n * - `auto` - Automatically detect the environment. Uses `production` if the environment cannot be determined.\n * - `production` - Always use the production script. (Sends events to the server)\n * - `development` - Always use the development script. (Logs events to the console)\n * @param [props.debug] - Whether to enable debug logging in development. Defaults to `true`.\n * @param [props.beforeSend] - A middleware function to modify events before they are sent. Should return the event object or `null` to cancel the event.\n * @param [props.dsn] - The DSN of the project to send events to. Only required when self-hosting.\n * @param [props.disableAutoTrack] - Whether the injected script should track page views from pushState events. Disable if route is updated after pushState, a manually call page pageview().\n * @param [confString] - an optional JSON string (InjectProps) containing the default configuration. Explicit props will take over any provided default.\n */\nfunction inject(\n props: InjectProps = {\n debug: true,\n },\n confString?: string,\n): void {\n if (!isBrowser()) return;\n\n const { beforeSend, src, dataset } = loadProps(props, confString);\n initQueue();\n\n if (beforeSend) {\n window.va?.('beforeSend', beforeSend);\n }\n if (document.head.querySelector(`script[src*=\"${src}\"]`)) return;\n\n const script = document.createElement('script');\n script.src = src;\n for (const [key, value] of Object.entries(dataset)) {\n script.dataset[key] = value;\n }\n script.defer = true;\n script.onerror = (): void => {\n const errorMessage = isDevelopment()\n ? 'Please check if any ad blockers are enabled and try again.'\n : 'Be sure to enable Web Analytics for your project and deploy again. See https://vercel.com/docs/analytics/quickstart for more information.';\n\n console.log(\n `[Vercel Web Analytics] Failed to load script from ${src}. ${errorMessage}`,\n );\n };\n\n document.head.appendChild(script);\n}\n\n/**\n * Tracks a custom event. Please refer to the [documentation](https://vercel.com/docs/concepts/analytics/custom-events) for more information on custom events.\n * @param name - The name of the event.\n * * Examples: `Purchase`, `Click Button`, or `Play Video`.\n * @param [properties] - Additional properties of the event. Nested objects are not supported. Allowed values are `string`, `number`, `boolean`, and `null`.\n */\nfunction track(\n name: string,\n properties?: Record<string, AllowedPropertyValues>,\n options?: {\n flags?: FlagsDataInput;\n },\n): void {\n if (!isBrowser()) {\n const msg =\n '[Vercel Web Analytics] Please import `track` from `@vercel/analytics/server` when using this function in a server environment';\n\n if (isProduction()) {\n console.warn(msg);\n } else {\n throw new Error(msg);\n }\n\n return;\n }\n\n if (!properties) {\n window.va?.('event', { name, options });\n return;\n }\n\n try {\n const props = parseProperties(properties, {\n strip: isProduction(),\n });\n\n window.va?.('event', {\n name,\n data: props,\n options,\n });\n } catch (err) {\n if (err instanceof Error && isDevelopment()) {\n console.error(err);\n }\n }\n}\n\nfunction pageview({\n route,\n path,\n}: {\n route?: string | null;\n path?: string;\n}): void {\n window.va?.('pageview', { route, path });\n}\n\nexport { inject, track, pageview, computeRoute };\nexport type { AnalyticsProps, BeforeSend, BeforeSendEvent };\n\nexport default {\n inject,\n track,\n computeRoute,\n};\n","// !! important !!\n// do not access env variables using process.env[varname]\n// some bundlers won't replace the value at build time.\n\nexport function getBasePath(): string | undefined {\n if (typeof process === 'undefined' || typeof process.env === 'undefined') {\n return undefined;\n }\n return process.env.REACT_APP_VERCEL_OBSERVABILITY_BASEPATH;\n}\n\nexport function getConfigString(): string | undefined {\n if (typeof process === 'undefined' || typeof process.env === 'undefined') {\n return undefined;\n }\n return process.env.REACT_APP_VERCEL_OBSERVABILITY_CLIENT_CONFIG;\n}\n","'use client';\nimport { useParams, usePathname, useSearchParams } from 'next/navigation.js';\nimport { computeRoute } from '../utils';\n\nexport const useRoute = (): {\n route: string | null;\n path: string;\n} => {\n const params = useParams();\n const searchParams = useSearchParams();\n const path = usePathname();\n\n // Until we have route parameters, we don't compute the route\n if (!params) {\n return { route: null, path };\n }\n // in Next.js@13, useParams() could return an empty object for pages router, and we default to searchParams.\n const finalParams = Object.keys(params).length\n ? params\n : Object.fromEntries(searchParams.entries());\n return { route: computeRoute(path, finalParams), path };\n};\n\n// !! important !!\n// do not access env variables using process.env[varname]\n// some bundlers won't replace the value at build time.\n\nexport function getBasePath(): string | undefined {\n if (typeof process === 'undefined' || typeof process.env === 'undefined') {\n return undefined;\n }\n return process.env.NEXT_PUBLIC_VERCEL_OBSERVABILITY_BASEPATH;\n}\n\nexport function getConfigString(): string | undefined {\n if (typeof process === 'undefined' || typeof process.env === 'undefined') {\n return undefined;\n }\n return process.env.NEXT_PUBLIC_VERCEL_OBSERVABILITY_CLIENT_CONFIG;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,mBAAAA;AAAA;AAAA;AACA,IAAAC,gBAAgD;;;ACAhD,mBAA0B;;;ACDnB,IAAM,YAAY,MAAY;AAEnC,MAAI,OAAO,GAAI;AAEf,S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OAAO,QAAQ,QAAQ,aAAa;AACxE,WAAO;AAAA,EACT;AACA,SAAO,QAAQ,IAAI;AACrB;AAEO,SAASC,mBAAsC;AACpD,MAAI,OAAO,YAAY,eAAe,OAAO,QAAQ,QAAQ,aAAa;AACxE,WAAO;AAAA,EACT;AACA,SAAO,QAAQ,IAAI;AACrB;;;AP/BA,SAAS,mBAAmB,OAAyB;AACnD,QAAM,EAAE,OAAO,KAAK,IAAI,SAAS;AACjC,SACE,8BAAAC,QAAA;AAAA,IAAC;AAAA;AAAA,MACC;AAAA,MACA;AAAA,MACC,GAAG;AAAA,MACJ,UAAUC,aAAY;AAAA,MACtB,cAAcC,iBAAgB;AAAA,MAC9B,WAAU;AAAA;AAAA,EACZ;AAEJ;AAEO,SAASC,WAAU,OAAoB;AAE5C,SACE,8BAAAH,QAAA,cAAC,0BAAS,UAAU,QAClB,8BAAAA,QAAA,cAAC,sBAAoB,GAAG,OAAO,CACjC;AAEJ;","names":["Analytics","import_react","getBasePath","getConfigString","React","getBasePath","getConfigString","Analytics"]}
@@ -6,18 +6,19 @@ import React, { Suspense } from "react";
6
6
  // src/react/index.tsx
7
7
  import { useEffect } from "react";
8
8
 
9
- // package.json
10
- var name = "@vercel/analytics";
11
- var version = "1.6.1";
12
-
13
9
  // src/queue.ts
14
10
  var initQueue = () => {
15
11
  if (window.va) return;
16
12
  window.va = function a(...params) {
17
- (window.vaq = window.vaq || []).push(params);
13
+ if (!window.vaq) window.vaq = [];
14
+ window.vaq.push(params);
18
15
  };
19
16
  };
20
17
 
18
+ // package.json
19
+ var name = "@vercel/analytics";
20
+ var version = "2.0.0";
21
+
21
22
  // src/utils.ts
22
23
  function isBrowser() {
23
24
  return typeof window !== "undefined";
@@ -28,7 +29,7 @@ function detectEnvironment() {
28
29
  if (env === "development" || env === "test") {
29
30
  return "development";
30
31
  }
31
- } catch (e) {
32
+ } catch {
32
33
  }
33
34
  return "production";
34
35
  }
@@ -70,7 +71,7 @@ function computeRoute(pathname, pathParams) {
70
71
  }
71
72
  }
72
73
  return result;
73
- } catch (e) {
74
+ } catch {
74
75
  return pathname;
75
76
  }
76
77
  }
@@ -82,55 +83,90 @@ function escapeRegExp(string) {
82
83
  }
83
84
  function getScriptSrc(props) {
84
85
  if (props.scriptSrc) {
85
- return props.scriptSrc;
86
+ return makeAbsolute(props.scriptSrc);
86
87
  }
87
88
  if (isDevelopment()) {
88
89
  return "https://va.vercel-scripts.com/v1/script.debug.js";
89
90
  }
90
91
  if (props.basePath) {
91
- return `${props.basePath}/insights/script.js`;
92
+ return makeAbsolute(`${props.basePath}/insights/script.js`);
92
93
  }
93
94
  return "/_vercel/insights/script.js";
94
95
  }
96
+ function loadProps(explicitProps, confString) {
97
+ var _a;
98
+ let props = explicitProps;
99
+ if (confString) {
100
+ try {
101
+ props = {
102
+ ...(_a = JSON.parse(confString)) == null ? void 0 : _a.analytics,
103
+ ...explicitProps
104
+ };
105
+ } catch {
106
+ }
107
+ }
108
+ setMode(props.mode);
109
+ const dataset = {
110
+ sdkn: name + (props.framework ? `/${props.framework}` : ""),
111
+ sdkv: version
112
+ };
113
+ if (props.disableAutoTrack) {
114
+ dataset.disableAutoTrack = "1";
115
+ }
116
+ if (props.viewEndpoint) {
117
+ dataset.viewEndpoint = makeAbsolute(props.viewEndpoint);
118
+ }
119
+ if (props.eventEndpoint) {
120
+ dataset.eventEndpoint = makeAbsolute(props.eventEndpoint);
121
+ }
122
+ if (props.sessionEndpoint) {
123
+ dataset.sessionEndpoint = makeAbsolute(props.sessionEndpoint);
124
+ }
125
+ if (isDevelopment() && props.debug === false) {
126
+ dataset.debug = "false";
127
+ }
128
+ if (props.dsn) {
129
+ dataset.dsn = props.dsn;
130
+ }
131
+ if (props.endpoint) {
132
+ dataset.endpoint = props.endpoint;
133
+ } else if (props.basePath) {
134
+ dataset.endpoint = makeAbsolute(`${props.basePath}/insights`);
135
+ }
136
+ return {
137
+ beforeSend: props.beforeSend,
138
+ src: getScriptSrc(props),
139
+ dataset
140
+ };
141
+ }
142
+ function makeAbsolute(url) {
143
+ return url.startsWith("http://") || url.startsWith("https://") || url.startsWith("/") ? url : `/${url}`;
144
+ }
95
145
 
96
146
  // src/generic.ts
97
147
  function inject(props = {
98
148
  debug: true
99
- }) {
149
+ }, confString) {
100
150
  var _a;
101
151
  if (!isBrowser()) return;
102
- setMode(props.mode);
152
+ const { beforeSend, src, dataset } = loadProps(props, confString);
103
153
  initQueue();
104
- if (props.beforeSend) {
105
- (_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
154
+ if (beforeSend) {
155
+ (_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", beforeSend);
106
156
  }
107
- const src = getScriptSrc(props);
108
157
  if (document.head.querySelector(`script[src*="${src}"]`)) return;
109
158
  const script = document.createElement("script");
110
159
  script.src = src;
111
- script.defer = true;
112
- script.dataset.sdkn = name + (props.framework ? `/${props.framework}` : "");
113
- script.dataset.sdkv = version;
114
- if (props.disableAutoTrack) {
115
- script.dataset.disableAutoTrack = "1";
116
- }
117
- if (props.endpoint) {
118
- script.dataset.endpoint = props.endpoint;
119
- } else if (props.basePath) {
120
- script.dataset.endpoint = `${props.basePath}/insights`;
121
- }
122
- if (props.dsn) {
123
- script.dataset.dsn = props.dsn;
160
+ for (const [key, value] of Object.entries(dataset)) {
161
+ script.dataset[key] = value;
124
162
  }
163
+ script.defer = true;
125
164
  script.onerror = () => {
126
165
  const errorMessage = isDevelopment() ? "Please check if any ad blockers are enabled and try again." : "Be sure to enable Web Analytics for your project and deploy again. See https://vercel.com/docs/analytics/quickstart for more information.";
127
166
  console.log(
128
167
  `[Vercel Web Analytics] Failed to load script from ${src}. ${errorMessage}`
129
168
  );
130
169
  };
131
- if (isDevelopment() && props.debug === false) {
132
- script.dataset.debug = "false";
133
- }
134
170
  document.head.appendChild(script);
135
171
  }
136
172
  function pageview({
@@ -148,6 +184,12 @@ function getBasePath() {
148
184
  }
149
185
  return process.env.REACT_APP_VERCEL_OBSERVABILITY_BASEPATH;
150
186
  }
187
+ function getConfigString() {
188
+ if (typeof process === "undefined" || typeof process.env === "undefined") {
189
+ return void 0;
190
+ }
191
+ return process.env.REACT_APP_VERCEL_OBSERVABILITY_CLIENT_CONFIG;
192
+ }
151
193
 
152
194
  // src/react/index.tsx
153
195
  function Analytics(props) {
@@ -158,12 +200,15 @@ function Analytics(props) {
158
200
  }
159
201
  }, [props.beforeSend]);
160
202
  useEffect(() => {
161
- inject({
162
- framework: props.framework || "react",
163
- basePath: props.basePath ?? getBasePath(),
164
- ...props.route !== void 0 && { disableAutoTrack: true },
165
- ...props
166
- });
203
+ inject(
204
+ {
205
+ framework: props.framework || "react",
206
+ basePath: props.basePath ?? getBasePath(),
207
+ ...props.route !== void 0 && { disableAutoTrack: true },
208
+ ...props
209
+ },
210
+ props.configString ?? getConfigString()
211
+ );
167
212
  }, []);
168
213
  useEffect(() => {
169
214
  if (props.route && props.path) {
@@ -191,6 +236,12 @@ function getBasePath2() {
191
236
  }
192
237
  return process.env.NEXT_PUBLIC_VERCEL_OBSERVABILITY_BASEPATH;
193
238
  }
239
+ function getConfigString2() {
240
+ if (typeof process === "undefined" || typeof process.env === "undefined") {
241
+ return void 0;
242
+ }
243
+ return process.env.NEXT_PUBLIC_VERCEL_OBSERVABILITY_CLIENT_CONFIG;
244
+ }
194
245
 
195
246
  // src/nextjs/index.tsx
196
247
  function AnalyticsComponent(props) {
@@ -202,6 +253,7 @@ function AnalyticsComponent(props) {
202
253
  route,
203
254
  ...props,
204
255
  basePath: getBasePath2(),
256
+ configString: getConfigString2(),
205
257
  framework: "next"
206
258
  }
207
259
  );