@vercel/analytics 2.0.0-canary.1 → 2.0.1
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.
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/next/index.js +1 -1
- package/dist/next/index.js.map +1 -1
- package/dist/next/index.mjs +1 -1
- package/dist/next/index.mjs.map +1 -1
- package/dist/nuxt/runtime/index.js +1 -1
- package/dist/nuxt/runtime/index.js.map +1 -1
- package/dist/nuxt/runtime/index.mjs +1 -1
- package/dist/nuxt/runtime/index.mjs.map +1 -1
- package/dist/react/index.js +1 -1
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +1 -1
- package/dist/react/index.mjs.map +1 -1
- package/dist/remix/index.js +1 -1
- package/dist/remix/index.js.map +1 -1
- package/dist/remix/index.mjs +1 -1
- package/dist/remix/index.mjs.map +1 -1
- package/dist/server/index.js +1 -1
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.mjs +1 -1
- package/dist/server/index.mjs.map +1 -1
- package/dist/sveltekit/index.js +1 -1
- package/dist/sveltekit/index.js.map +1 -1
- package/dist/sveltekit/index.mjs +1 -1
- package/dist/sveltekit/index.mjs.map +1 -1
- package/dist/vue/index.js +1 -1
- package/dist/vue/index.js.map +1 -1
- package/dist/vue/index.mjs +1 -1
- package/dist/vue/index.mjs.map +1 -1
- package/package.json +4 -1
package/dist/remix/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/remix/index.tsx","../../src/react/index.tsx","../../src/queue.ts","../../package.json","../../src/utils.ts","../../src/generic.ts","../../src/react/utils.ts","../../src/remix/utils.ts"],"sourcesContent":["import React from 'react';\nimport { Analytics as AnalyticsScript } from '../react';\nimport type { AnalyticsProps, BeforeSend, BeforeSendEvent } from '../types';\nimport { getBasePath, getConfigString, useRoute } from './utils';\n\nexport function Analytics(props: Omit<AnalyticsProps, 'route'>): JSX.Element {\n return (\n <AnalyticsScript\n {...useRoute()}\n {...props}\n basePath={getBasePath()}\n configString={getConfigString()}\n framework=\"remix\"\n />\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-canary.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\": \"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","import { useLocation, useParams } from '@remix-run/react';\nimport { computeRoute } from '../utils';\n\nexport const useRoute = (): { route: string | null; path: string } => {\n const params = useParams();\n const { pathname: path } = useLocation();\n return { route: computeRoute(path, params as never), path };\n};\n\n// !! important !!\n// do not access env variables using import.meta.env[varname]\n// some bundlers won't replace the value at build time.\n\nexport function getBasePath(): string | undefined {\n try {\n return import.meta.env.VITE_VERCEL_OBSERVABILITY_BASEPATH as\n | string\n | undefined;\n } catch {\n // do nothing\n }\n}\n\nexport function getConfigString(): string | undefined {\n try {\n return import.meta.env.VITE_VERCEL_OBSERVABILITY_CLIENT_CONFIG as\n | string\n | undefined;\n } catch {\n // do nothing\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,mBAAAA;AAAA;AAAA;AAAA,IAAAC,gBAAkB;;;ACClB,mBAA0B;;;ACDnB,IAAM,YAAY,MAAY;AAEnC,MAAI,OAAO,GAAI;AAEf,SAAO,KAAK,SAAS,KAAK,QAAc;AACtC,QAAI,CAAC,OAAO,IAAK,QAAO,MAAM,CAAC;AAC/B,WAAO,IAAI,KAAK,MAAM;AAAA,EACxB;AACF;;;ACPE,WAAQ;AACR,cAAW;;;ACON,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,QAAQ;AAAA,EAER;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,OA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G;AAAA,MACL;AAAA,MACA,MAAM,gBAAgB,gBAAgB;AAAA,IACxC;AAAA,EACF,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;;;AMjEA,IAAAC,gBAAuC;AAAvC;AAGO,IAAM,WAAW,MAA8C;AACpE,QAAM,aAAS,yBAAU;AACzB,QAAM,EAAE,UAAU,KAAK,QAAI,2BAAY;AACvC,SAAO,EAAE,OAAO,aAAa,MAAM,MAAe,GAAG,KAAK;AAC5D;AAMO,SAASC,eAAkC;AAChD,MAAI;AACF,WAAO,YAAY,IAAI;AAAA,EAGzB,QAAQ;AAAA,EAER;AACF;AAEO,SAASC,mBAAsC;AACpD,MAAI;AACF,WAAO,YAAY,IAAI;AAAA,EAGzB,QAAQ;AAAA,EAER;AACF;;;AP1BO,SAASC,WAAU,OAAmD;AAC3E,SACE,8BAAAC,QAAA;AAAA,IAAC;AAAA;AAAA,MACE,GAAG,SAAS;AAAA,MACZ,GAAG;AAAA,MACJ,UAAUC,aAAY;AAAA,MACtB,cAAcC,iBAAgB;AAAA,MAC9B,WAAU;AAAA;AAAA,EACZ;AAEJ;","names":["Analytics","import_react","import_react","getBasePath","getConfigString","Analytics","React","getBasePath","getConfigString"]}
|
|
1
|
+
{"version":3,"sources":["../../src/remix/index.tsx","../../src/react/index.tsx","../../src/queue.ts","../../package.json","../../src/utils.ts","../../src/generic.ts","../../src/react/utils.ts","../../src/remix/utils.ts"],"sourcesContent":["import React from 'react';\nimport { Analytics as AnalyticsScript } from '../react';\nimport type { AnalyticsProps, BeforeSend, BeforeSendEvent } from '../types';\nimport { getBasePath, getConfigString, useRoute } from './utils';\n\nexport function Analytics(props: Omit<AnalyticsProps, 'route'>): JSX.Element {\n return (\n <AnalyticsScript\n {...useRoute()}\n {...props}\n basePath={getBasePath()}\n configString={getConfigString()}\n framework=\"remix\"\n />\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.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\": \"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 \"nuxt\": {\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","import { useLocation, useParams } from '@remix-run/react';\nimport { computeRoute } from '../utils';\n\nexport const useRoute = (): { route: string | null; path: string } => {\n const params = useParams();\n const { pathname: path } = useLocation();\n return { route: computeRoute(path, params as never), path };\n};\n\n// !! important !!\n// do not access env variables using import.meta.env[varname]\n// some bundlers won't replace the value at build time.\n\nexport function getBasePath(): string | undefined {\n try {\n return import.meta.env.VITE_VERCEL_OBSERVABILITY_BASEPATH as\n | string\n | undefined;\n } catch {\n // do nothing\n }\n}\n\nexport function getConfigString(): string | undefined {\n try {\n return import.meta.env.VITE_VERCEL_OBSERVABILITY_CLIENT_CONFIG as\n | string\n | undefined;\n } catch {\n // do nothing\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,mBAAAA;AAAA;AAAA;AAAA,IAAAC,gBAAkB;;;ACClB,mBAA0B;;;ACDnB,IAAM,YAAY,MAAY;AAEnC,MAAI,OAAO,GAAI;AAEf,SAAO,KAAK,SAAS,KAAK,QAAc;AACtC,QAAI,CAAC,OAAO,IAAK,QAAO,MAAM,CAAC;AAC/B,WAAO,IAAI,KAAK,MAAM;AAAA,EACxB;AACF;;;ACPE,WAAQ;AACR,cAAW;;;ACON,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,QAAQ;AAAA,EAER;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,MAA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,MACL;AAAA,MACA,MAAM,gBAAgB,gBAAgB;AAAA,IACxC;AAAA,EACF,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;;;AMjEA,IAAAC,gBAAuC;AAAvC;AAGO,IAAM,WAAW,MAA8C;AACpE,QAAM,aAAS,yBAAU;AACzB,QAAM,EAAE,UAAU,KAAK,QAAI,2BAAY;AACvC,SAAO,EAAE,OAAO,aAAa,MAAM,MAAe,GAAG,KAAK;AAC5D;AAMO,SAASC,eAAkC;AAChD,MAAI;AACF,WAAO,YAAY,IAAI;AAAA,EAGzB,QAAQ;AAAA,EAER;AACF;AAEO,SAASC,mBAAsC;AACpD,MAAI;AACF,WAAO,YAAY,IAAI;AAAA,EAGzB,QAAQ;AAAA,EAER;AACF;;;AP1BO,SAASC,WAAU,OAAmD;AAC3E,SACE,8BAAAC,QAAA;AAAA,IAAC;AAAA;AAAA,MACE,GAAG,SAAS;AAAA,MACZ,GAAG;AAAA,MACJ,UAAUC,aAAY;AAAA,MACtB,cAAcC,iBAAgB;AAAA,MAC9B,WAAU;AAAA;AAAA,EACZ;AAEJ;","names":["Analytics","import_react","import_react","getBasePath","getConfigString","Analytics","React","getBasePath","getConfigString"]}
|
package/dist/remix/index.mjs
CHANGED
package/dist/remix/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/remix/index.tsx","../../src/react/index.tsx","../../src/queue.ts","../../package.json","../../src/utils.ts","../../src/generic.ts","../../src/react/utils.ts","../../src/remix/utils.ts"],"sourcesContent":["import React from 'react';\nimport { Analytics as AnalyticsScript } from '../react';\nimport type { AnalyticsProps, BeforeSend, BeforeSendEvent } from '../types';\nimport { getBasePath, getConfigString, useRoute } from './utils';\n\nexport function Analytics(props: Omit<AnalyticsProps, 'route'>): JSX.Element {\n return (\n <AnalyticsScript\n {...useRoute()}\n {...props}\n basePath={getBasePath()}\n configString={getConfigString()}\n framework=\"remix\"\n />\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-canary.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\": \"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","import { useLocation, useParams } from '@remix-run/react';\nimport { computeRoute } from '../utils';\n\nexport const useRoute = (): { route: string | null; path: string } => {\n const params = useParams();\n const { pathname: path } = useLocation();\n return { route: computeRoute(path, params as never), path };\n};\n\n// !! important !!\n// do not access env variables using import.meta.env[varname]\n// some bundlers won't replace the value at build time.\n\nexport function getBasePath(): string | undefined {\n try {\n return import.meta.env.VITE_VERCEL_OBSERVABILITY_BASEPATH as\n | string\n | undefined;\n } catch {\n // do nothing\n }\n}\n\nexport function getConfigString(): string | undefined {\n try {\n return import.meta.env.VITE_VERCEL_OBSERVABILITY_CLIENT_CONFIG as\n | string\n | undefined;\n } catch {\n // do nothing\n }\n}\n"],"mappings":";AAAA,OAAO,WAAW;;;ACClB,SAAS,iBAAiB;;;ACDnB,IAAM,YAAY,MAAY;AAEnC,MAAI,OAAO,GAAI;AAEf,SAAO,KAAK,SAAS,KAAK,QAAc;AACtC,QAAI,CAAC,OAAO,IAAK,QAAO,MAAM,CAAC;AAC/B,WAAO,IAAI,KAAK,MAAM;AAAA,EACxB;AACF;;;ACPE,WAAQ;AACR,cAAW;;;ACON,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,QAAQ;AAAA,EAER;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,QAA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,gBAAgB;AAAA,IACxC;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,YAAU,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;;;AMjEA,SAAS,aAAa,iBAAiB;AAGhC,IAAM,WAAW,MAA8C;AACpE,QAAM,SAAS,UAAU;AACzB,QAAM,EAAE,UAAU,KAAK,IAAI,YAAY;AACvC,SAAO,EAAE,OAAO,aAAa,MAAM,MAAe,GAAG,KAAK;AAC5D;AAMO,SAASA,eAAkC;AAChD,MAAI;AACF,WAAO,YAAY,IAAI;AAAA,EAGzB,QAAQ;AAAA,EAER;AACF;AAEO,SAASC,mBAAsC;AACpD,MAAI;AACF,WAAO,YAAY,IAAI;AAAA,EAGzB,QAAQ;AAAA,EAER;AACF;;;AP1BO,SAASC,WAAU,OAAmD;AAC3E,SACE;AAAA,IAAC;AAAA;AAAA,MACE,GAAG,SAAS;AAAA,MACZ,GAAG;AAAA,MACJ,UAAUC,aAAY;AAAA,MACtB,cAAcC,iBAAgB;AAAA,MAC9B,WAAU;AAAA;AAAA,EACZ;AAEJ;","names":["getBasePath","getConfigString","Analytics","getBasePath","getConfigString"]}
|
|
1
|
+
{"version":3,"sources":["../../src/remix/index.tsx","../../src/react/index.tsx","../../src/queue.ts","../../package.json","../../src/utils.ts","../../src/generic.ts","../../src/react/utils.ts","../../src/remix/utils.ts"],"sourcesContent":["import React from 'react';\nimport { Analytics as AnalyticsScript } from '../react';\nimport type { AnalyticsProps, BeforeSend, BeforeSendEvent } from '../types';\nimport { getBasePath, getConfigString, useRoute } from './utils';\n\nexport function Analytics(props: Omit<AnalyticsProps, 'route'>): JSX.Element {\n return (\n <AnalyticsScript\n {...useRoute()}\n {...props}\n basePath={getBasePath()}\n configString={getConfigString()}\n framework=\"remix\"\n />\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.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\": \"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 \"nuxt\": {\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","import { useLocation, useParams } from '@remix-run/react';\nimport { computeRoute } from '../utils';\n\nexport const useRoute = (): { route: string | null; path: string } => {\n const params = useParams();\n const { pathname: path } = useLocation();\n return { route: computeRoute(path, params as never), path };\n};\n\n// !! important !!\n// do not access env variables using import.meta.env[varname]\n// some bundlers won't replace the value at build time.\n\nexport function getBasePath(): string | undefined {\n try {\n return import.meta.env.VITE_VERCEL_OBSERVABILITY_BASEPATH as\n | string\n | undefined;\n } catch {\n // do nothing\n }\n}\n\nexport function getConfigString(): string | undefined {\n try {\n return import.meta.env.VITE_VERCEL_OBSERVABILITY_CLIENT_CONFIG as\n | string\n | undefined;\n } catch {\n // do nothing\n }\n}\n"],"mappings":";AAAA,OAAO,WAAW;;;ACClB,SAAS,iBAAiB;;;ACDnB,IAAM,YAAY,MAAY;AAEnC,MAAI,OAAO,GAAI;AAEf,SAAO,KAAK,SAAS,KAAK,QAAc;AACtC,QAAI,CAAC,OAAO,IAAK,QAAO,MAAM,CAAC;AAC/B,WAAO,IAAI,KAAK,MAAM;AAAA,EACxB;AACF;;;ACPE,WAAQ;AACR,cAAW;;;ACON,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,QAAQ;AAAA,EAER;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,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EAAE,OAAO,MAAM,OAAO,MAAM,MAAM,KAAK,CAAC;AAAA,IACnD;AAAA,EACF,GAAG,CAAC,MAAM,OAAO,MAAM,IAAI,CAAC;AAE5B,SAAO;AACT;;;AMjEA,SAAS,aAAa,iBAAiB;AAGhC,IAAM,WAAW,MAA8C;AACpE,QAAM,SAAS,UAAU;AACzB,QAAM,EAAE,UAAU,KAAK,IAAI,YAAY;AACvC,SAAO,EAAE,OAAO,aAAa,MAAM,MAAe,GAAG,KAAK;AAC5D;AAMO,SAASA,eAAkC;AAChD,MAAI;AACF,WAAO,YAAY,IAAI;AAAA,EAGzB,QAAQ;AAAA,EAER;AACF;AAEO,SAASC,mBAAsC;AACpD,MAAI;AACF,WAAO,YAAY,IAAI;AAAA,EAGzB,QAAQ;AAAA,EAER;AACF;;;AP1BO,SAASC,WAAU,OAAmD;AAC3E,SACE;AAAA,IAAC;AAAA;AAAA,MACE,GAAG,SAAS;AAAA,MACZ,GAAG;AAAA,MACJ,UAAUC,aAAY;AAAA,MACtB,cAAcC,iBAAgB;AAAA,MAC9B,WAAU;AAAA;AAAA,EACZ;AAEJ;","names":["getBasePath","getConfigString","Analytics","getBasePath","getConfigString"]}
|
package/dist/server/index.js
CHANGED
package/dist/server/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/server/index.ts","../../package.json","../../src/utils.ts"],"sourcesContent":["import { name as packageName, version } from '../../package.json';\nimport type {\n AllowedPropertyValues,\n FlagsDataInput,\n PlainFlags,\n} from '../types';\nimport { isProduction, parseProperties } from '../utils';\n\ntype HeadersObject = Record<string, string | string[] | undefined>;\ntype AllowedHeaders = Headers | HeadersObject;\n\nfunction isHeaders(headers?: AllowedHeaders): headers is Headers {\n if (!headers) return false;\n return typeof (headers as HeadersObject).entries === 'function';\n}\n\ninterface Options {\n flags?: FlagsDataInput;\n headers?: AllowedHeaders;\n request?: { headers: AllowedHeaders };\n}\n\ninterface RequestContext {\n get: () => {\n headers: Record<string, string | undefined>;\n url: string;\n waitUntil?: (promise: Promise<unknown>) => void;\n flags?: {\n getValues: () => PlainFlags;\n reportValue: (key: string, value: unknown) => void;\n };\n };\n}\n\nconst symbol = Symbol.for('@vercel/request-context');\nconst logPrefix = '[Vercel Web Analytics]';\n\nexport async function track(\n eventName: string,\n properties?: Record<string, AllowedPropertyValues>,\n options?: Options,\n): Promise<void> {\n const ENDPOINT =\n process.env.VERCEL_WEB_ANALYTICS_ENDPOINT || process.env.VERCEL_URL;\n const DISABLE_LOGS = Boolean(process.env.VERCEL_WEB_ANALYTICS_DISABLE_LOGS);\n const BYPASS_SECRET = process.env.VERCEL_AUTOMATION_BYPASS_SECRET;\n\n if (typeof window !== 'undefined') {\n if (!isProduction()) {\n throw new Error(\n `${logPrefix} It seems like you imported the \\`track\\` function from \\`@vercel/web-analytics/server\\` in a browser environment. This function is only meant to be used in a server environment.`,\n );\n }\n\n return;\n }\n\n const props = parseProperties(properties, {\n strip: isProduction(),\n });\n\n if (!ENDPOINT) {\n if (isProduction()) {\n console.log(\n `${logPrefix} Can't find VERCEL_URL in environment variables.`,\n );\n } else if (!DISABLE_LOGS) {\n console.log(\n `${logPrefix} Track \"${eventName}\" ${\n props ? `with data ${JSON.stringify(props)}` : ''\n }`,\n );\n }\n return;\n }\n try {\n const requestContext = (\n (globalThis as never)[symbol] as RequestContext | undefined\n )?.get();\n\n let headers: AllowedHeaders | undefined;\n\n if (options && 'headers' in options) {\n headers = options.headers;\n } else if (options?.request) {\n headers = options.request.headers;\n } else if (requestContext?.headers) {\n // not explicitly passed in context, so take it from async storage\n headers = requestContext.headers;\n }\n\n let tmp: HeadersObject = {};\n if (headers && isHeaders(headers)) {\n headers.forEach((value, key) => {\n tmp[key] = value;\n });\n } else if (headers) {\n tmp = headers;\n }\n\n const url = ENDPOINT.startsWith('http')\n ? ENDPOINT\n : new URL('/_vercel/insights/event', `https://${ENDPOINT}`).toString();\n\n const body = {\n o: requestContext?.url || (tmp.referer as string) || new URL(url).origin,\n ts: Date.now(),\n sdkn: `${packageName}/server`,\n sdkv: version,\n r: '',\n en: eventName,\n ed: props,\n f: safeGetFlags(options?.flags, requestContext),\n };\n\n const hasHeaders = Boolean(headers);\n\n if (!hasHeaders) {\n throw new Error(\n 'No session context found. Pass `request` or `headers` to the `track` function.',\n );\n }\n\n const promise = fetch(url, {\n headers: {\n 'content-type': 'application/json',\n ...(hasHeaders\n ? {\n 'user-agent': tmp['user-agent'] as string,\n 'x-vercel-ip': tmp['x-forwarded-for'] as string,\n 'x-va-server': '1',\n cookie: tmp.cookie as string,\n }\n : {\n 'x-va-server': '2',\n }),\n ...(BYPASS_SECRET\n ? { 'x-vercel-protection-bypass': BYPASS_SECRET }\n : {}),\n },\n body: JSON.stringify(body),\n method: 'POST',\n })\n // We want to always consume the body; some cloud providers track fetch concurrency\n // and may not release the connection until the body is consumed.\n .then((response) => response.text())\n .catch((err: unknown) => {\n if (err instanceof Error && 'response' in err) {\n console.error(err.response);\n } else {\n console.error(err);\n }\n });\n\n if (requestContext?.waitUntil) {\n requestContext.waitUntil(promise);\n } else {\n await promise;\n }\n\n return void 0;\n } catch (err) {\n console.error(err);\n }\n}\n\nfunction safeGetFlags(\n flags: Options['flags'],\n requestContext?: ReturnType<RequestContext['get']>,\n):\n | {\n p: PlainFlags;\n }\n | undefined {\n try {\n // In the case plain flags are passed, just return them\n if (flags && !Array.isArray(flags)) {\n return { p: flags };\n }\n\n if (!requestContext || !flags) return;\n const plainFlags: Record<string, unknown> = {};\n // returns all available plain flags\n const resolvedPlainFlags = requestContext.flags?.getValues() ?? {};\n\n for (const flag of flags) {\n if (typeof flag === 'string') {\n // only picks the desired flags\n plainFlags[flag] = resolvedPlainFlags[flag];\n } else {\n // merge user-provided values with resolved values\n Object.assign(plainFlags, flag);\n }\n }\n\n return { p: plainFlags };\n } catch {\n /* empty */\n }\n}\n","{\n \"name\": \"@vercel/analytics\",\n \"version\": \"2.0.0-canary.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\": \"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"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCE,WAAQ;AACR,cAAW;;;ACON,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,QAAQ;AAAA,EAER;AACA,SAAO;AACT;AAWO,SAAS,UAAgB;AAC9B,QAAM,OAAO,UAAU,IAAI,OAAO,MAAM,kBAAkB;AAC1D,SAAO,QAAQ;AACjB;AAEO,SAAS,eAAwB;AACtC,SAAO,QAAQ,MAAM;AACvB;AAMA,SAAS,UACP,KACA,EAAE,CAAC,GAAG,GAAG,GAAG,GAAG,KAAK,GACK;AACzB,SAAO;AACT;AAEO,SAAS,gBACd,YACA,SAG2D;AAC3D,MAAI,CAAC,WAAY,QAAO;AACxB,MAAI,QAAQ;AACZ,QAAM,kBAA4B,CAAC;AACnC,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,UAAU,GAAG;AACrD,QAAI,OAAO,UAAU,YAAY,UAAU,MAAM;AAC/C,UAAI,QAAQ,OAAO;AACjB,gBAAQ,UAAU,KAAK,KAAK;AAAA,MAC9B,OAAO;AACL,wBAAgB,KAAK,GAAG;AAAA,MAC1B;AAAA,IACF;AAAA,EACF;AAEA,MAAI,gBAAgB,SAAS,KAAK,CAAC,QAAQ,OAAO;AAChD,UAAM;AAAA,MACJ,2CAA2C,gBAAgB;AAAA,QACzD;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACA,SAAO;AACT;;;AFtEA,SAAS,UAAU,SAA8C;AAC/D,MAAI,CAAC,QAAS,QAAO;AACrB,SAAO,OAAQ,QAA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I,SAAS,CAAC,MAAM,QAAQ,KAAK,GAAG;AAClC,aAAO,EAAE,GAAG,MAAM;AAAA,IACpB;AAEA,QAAI,CAAC,kBAAkB,CAAC,MAAO;AAC/B,UAAM,aAAsC,CAAC;AAE7C,UAAM,uBAAqB,oBAAe,UAAf,mBAAsB,gBAAe,CAAC;AAEjE,eAAW,QAAQ,OAAO;AACxB,UAAI,OAAO,SAAS,UAAU;AAE5B,mBAAW,IAAI,IAAI,mBAAmB,IAAI;AAAA,MAC5C,OAAO;AAEL,eAAO,OAAO,YAAY,IAAI;AAAA,MAChC;AAAA,IACF;AAEA,WAAO,EAAE,GAAG,WAAW;AAAA,EACzB,QAAQ;AAAA,EAER;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/server/index.ts","../../package.json","../../src/utils.ts"],"sourcesContent":["import { name as packageName, version } from '../../package.json';\nimport type {\n AllowedPropertyValues,\n FlagsDataInput,\n PlainFlags,\n} from '../types';\nimport { isProduction, parseProperties } from '../utils';\n\ntype HeadersObject = Record<string, string | string[] | undefined>;\ntype AllowedHeaders = Headers | HeadersObject;\n\nfunction isHeaders(headers?: AllowedHeaders): headers is Headers {\n if (!headers) return false;\n return typeof (headers as HeadersObject).entries === 'function';\n}\n\ninterface Options {\n flags?: FlagsDataInput;\n headers?: AllowedHeaders;\n request?: { headers: AllowedHeaders };\n}\n\ninterface RequestContext {\n get: () => {\n headers: Record<string, string | undefined>;\n url: string;\n waitUntil?: (promise: Promise<unknown>) => void;\n flags?: {\n getValues: () => PlainFlags;\n reportValue: (key: string, value: unknown) => void;\n };\n };\n}\n\nconst symbol = Symbol.for('@vercel/request-context');\nconst logPrefix = '[Vercel Web Analytics]';\n\nexport async function track(\n eventName: string,\n properties?: Record<string, AllowedPropertyValues>,\n options?: Options,\n): Promise<void> {\n const ENDPOINT =\n process.env.VERCEL_WEB_ANALYTICS_ENDPOINT || process.env.VERCEL_URL;\n const DISABLE_LOGS = Boolean(process.env.VERCEL_WEB_ANALYTICS_DISABLE_LOGS);\n const BYPASS_SECRET = process.env.VERCEL_AUTOMATION_BYPASS_SECRET;\n\n if (typeof window !== 'undefined') {\n if (!isProduction()) {\n throw new Error(\n `${logPrefix} It seems like you imported the \\`track\\` function from \\`@vercel/web-analytics/server\\` in a browser environment. This function is only meant to be used in a server environment.`,\n );\n }\n\n return;\n }\n\n const props = parseProperties(properties, {\n strip: isProduction(),\n });\n\n if (!ENDPOINT) {\n if (isProduction()) {\n console.log(\n `${logPrefix} Can't find VERCEL_URL in environment variables.`,\n );\n } else if (!DISABLE_LOGS) {\n console.log(\n `${logPrefix} Track \"${eventName}\" ${\n props ? `with data ${JSON.stringify(props)}` : ''\n }`,\n );\n }\n return;\n }\n try {\n const requestContext = (\n (globalThis as never)[symbol] as RequestContext | undefined\n )?.get();\n\n let headers: AllowedHeaders | undefined;\n\n if (options && 'headers' in options) {\n headers = options.headers;\n } else if (options?.request) {\n headers = options.request.headers;\n } else if (requestContext?.headers) {\n // not explicitly passed in context, so take it from async storage\n headers = requestContext.headers;\n }\n\n let tmp: HeadersObject = {};\n if (headers && isHeaders(headers)) {\n headers.forEach((value, key) => {\n tmp[key] = value;\n });\n } else if (headers) {\n tmp = headers;\n }\n\n const url = ENDPOINT.startsWith('http')\n ? ENDPOINT\n : new URL('/_vercel/insights/event', `https://${ENDPOINT}`).toString();\n\n const body = {\n o: requestContext?.url || (tmp.referer as string) || new URL(url).origin,\n ts: Date.now(),\n sdkn: `${packageName}/server`,\n sdkv: version,\n r: '',\n en: eventName,\n ed: props,\n f: safeGetFlags(options?.flags, requestContext),\n };\n\n const hasHeaders = Boolean(headers);\n\n if (!hasHeaders) {\n throw new Error(\n 'No session context found. Pass `request` or `headers` to the `track` function.',\n );\n }\n\n const promise = fetch(url, {\n headers: {\n 'content-type': 'application/json',\n ...(hasHeaders\n ? {\n 'user-agent': tmp['user-agent'] as string,\n 'x-vercel-ip': tmp['x-forwarded-for'] as string,\n 'x-va-server': '1',\n cookie: tmp.cookie as string,\n }\n : {\n 'x-va-server': '2',\n }),\n ...(BYPASS_SECRET\n ? { 'x-vercel-protection-bypass': BYPASS_SECRET }\n : {}),\n },\n body: JSON.stringify(body),\n method: 'POST',\n })\n // We want to always consume the body; some cloud providers track fetch concurrency\n // and may not release the connection until the body is consumed.\n .then((response) => response.text())\n .catch((err: unknown) => {\n if (err instanceof Error && 'response' in err) {\n console.error(err.response);\n } else {\n console.error(err);\n }\n });\n\n if (requestContext?.waitUntil) {\n requestContext.waitUntil(promise);\n } else {\n await promise;\n }\n\n return void 0;\n } catch (err) {\n console.error(err);\n }\n}\n\nfunction safeGetFlags(\n flags: Options['flags'],\n requestContext?: ReturnType<RequestContext['get']>,\n):\n | {\n p: PlainFlags;\n }\n | undefined {\n try {\n // In the case plain flags are passed, just return them\n if (flags && !Array.isArray(flags)) {\n return { p: flags };\n }\n\n if (!requestContext || !flags) return;\n const plainFlags: Record<string, unknown> = {};\n // returns all available plain flags\n const resolvedPlainFlags = requestContext.flags?.getValues() ?? {};\n\n for (const flag of flags) {\n if (typeof flag === 'string') {\n // only picks the desired flags\n plainFlags[flag] = resolvedPlainFlags[flag];\n } else {\n // merge user-provided values with resolved values\n Object.assign(plainFlags, flag);\n }\n }\n\n return { p: plainFlags };\n } catch {\n /* empty */\n }\n}\n","{\n \"name\": \"@vercel/analytics\",\n \"version\": \"2.0.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\": \"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 \"nuxt\": {\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"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCE,WAAQ;AACR,cAAW;;;ACON,SAAS,YAAqB;AACnC,SAAO,OAAO,WAAW;AAC3B;AAEA,SAAS,oBAAkD;AACzD,MAAI;AACF,UAAM,MAAM,QAAQ,IAAI;AACxB,QAAI,QAAQ,iBAAiB,QAAQ,QA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AO,YAAY,IAAI;AAAA,MAChC;AAAA,IACF;AAEA,WAAO,EAAE,GAAG,WAAW;AAAA,EACzB,QAAQ;AAAA,EAER;AACF;","names":[]}
|
package/dist/server/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../package.json","../../src/utils.ts","../../src/server/index.ts"],"sourcesContent":["{\n \"name\": \"@vercel/analytics\",\n \"version\": \"2.0.0-canary.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\": \"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 { name as packageName, version } from '../../package.json';\nimport type {\n AllowedPropertyValues,\n FlagsDataInput,\n PlainFlags,\n} from '../types';\nimport { isProduction, parseProperties } from '../utils';\n\ntype HeadersObject = Record<string, string | string[] | undefined>;\ntype AllowedHeaders = Headers | HeadersObject;\n\nfunction isHeaders(headers?: AllowedHeaders): headers is Headers {\n if (!headers) return false;\n return typeof (headers as HeadersObject).entries === 'function';\n}\n\ninterface Options {\n flags?: FlagsDataInput;\n headers?: AllowedHeaders;\n request?: { headers: AllowedHeaders };\n}\n\ninterface RequestContext {\n get: () => {\n headers: Record<string, string | undefined>;\n url: string;\n waitUntil?: (promise: Promise<unknown>) => void;\n flags?: {\n getValues: () => PlainFlags;\n reportValue: (key: string, value: unknown) => void;\n };\n };\n}\n\nconst symbol = Symbol.for('@vercel/request-context');\nconst logPrefix = '[Vercel Web Analytics]';\n\nexport async function track(\n eventName: string,\n properties?: Record<string, AllowedPropertyValues>,\n options?: Options,\n): Promise<void> {\n const ENDPOINT =\n process.env.VERCEL_WEB_ANALYTICS_ENDPOINT || process.env.VERCEL_URL;\n const DISABLE_LOGS = Boolean(process.env.VERCEL_WEB_ANALYTICS_DISABLE_LOGS);\n const BYPASS_SECRET = process.env.VERCEL_AUTOMATION_BYPASS_SECRET;\n\n if (typeof window !== 'undefined') {\n if (!isProduction()) {\n throw new Error(\n `${logPrefix} It seems like you imported the \\`track\\` function from \\`@vercel/web-analytics/server\\` in a browser environment. This function is only meant to be used in a server environment.`,\n );\n }\n\n return;\n }\n\n const props = parseProperties(properties, {\n strip: isProduction(),\n });\n\n if (!ENDPOINT) {\n if (isProduction()) {\n console.log(\n `${logPrefix} Can't find VERCEL_URL in environment variables.`,\n );\n } else if (!DISABLE_LOGS) {\n console.log(\n `${logPrefix} Track \"${eventName}\" ${\n props ? `with data ${JSON.stringify(props)}` : ''\n }`,\n );\n }\n return;\n }\n try {\n const requestContext = (\n (globalThis as never)[symbol] as RequestContext | undefined\n )?.get();\n\n let headers: AllowedHeaders | undefined;\n\n if (options && 'headers' in options) {\n headers = options.headers;\n } else if (options?.request) {\n headers = options.request.headers;\n } else if (requestContext?.headers) {\n // not explicitly passed in context, so take it from async storage\n headers = requestContext.headers;\n }\n\n let tmp: HeadersObject = {};\n if (headers && isHeaders(headers)) {\n headers.forEach((value, key) => {\n tmp[key] = value;\n });\n } else if (headers) {\n tmp = headers;\n }\n\n const url = ENDPOINT.startsWith('http')\n ? ENDPOINT\n : new URL('/_vercel/insights/event', `https://${ENDPOINT}`).toString();\n\n const body = {\n o: requestContext?.url || (tmp.referer as string) || new URL(url).origin,\n ts: Date.now(),\n sdkn: `${packageName}/server`,\n sdkv: version,\n r: '',\n en: eventName,\n ed: props,\n f: safeGetFlags(options?.flags, requestContext),\n };\n\n const hasHeaders = Boolean(headers);\n\n if (!hasHeaders) {\n throw new Error(\n 'No session context found. Pass `request` or `headers` to the `track` function.',\n );\n }\n\n const promise = fetch(url, {\n headers: {\n 'content-type': 'application/json',\n ...(hasHeaders\n ? {\n 'user-agent': tmp['user-agent'] as string,\n 'x-vercel-ip': tmp['x-forwarded-for'] as string,\n 'x-va-server': '1',\n cookie: tmp.cookie as string,\n }\n : {\n 'x-va-server': '2',\n }),\n ...(BYPASS_SECRET\n ? { 'x-vercel-protection-bypass': BYPASS_SECRET }\n : {}),\n },\n body: JSON.stringify(body),\n method: 'POST',\n })\n // We want to always consume the body; some cloud providers track fetch concurrency\n // and may not release the connection until the body is consumed.\n .then((response) => response.text())\n .catch((err: unknown) => {\n if (err instanceof Error && 'response' in err) {\n console.error(err.response);\n } else {\n console.error(err);\n }\n });\n\n if (requestContext?.waitUntil) {\n requestContext.waitUntil(promise);\n } else {\n await promise;\n }\n\n return void 0;\n } catch (err) {\n console.error(err);\n }\n}\n\nfunction safeGetFlags(\n flags: Options['flags'],\n requestContext?: ReturnType<RequestContext['get']>,\n):\n | {\n p: PlainFlags;\n }\n | undefined {\n try {\n // In the case plain flags are passed, just return them\n if (flags && !Array.isArray(flags)) {\n return { p: flags };\n }\n\n if (!requestContext || !flags) return;\n const plainFlags: Record<string, unknown> = {};\n // returns all available plain flags\n const resolvedPlainFlags = requestContext.flags?.getValues() ?? {};\n\n for (const flag of flags) {\n if (typeof flag === 'string') {\n // only picks the desired flags\n plainFlags[flag] = resolvedPlainFlags[flag];\n } else {\n // merge user-provided values with resolved values\n Object.assign(plainFlags, flag);\n }\n }\n\n return { p: plainFlags };\n } catch {\n /* empty */\n }\n}\n"],"mappings":";AACE,WAAQ;AACR,cAAW;;;ACON,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,QAAQ;AAAA,EAER;AACA,SAAO;AACT;AAWO,SAAS,UAAgB;AAC9B,QAAM,OAAO,UAAU,IAAI,OAAO,MAAM,kBAAkB;AAC1D,SAAO,QAAQ;AACjB;AAEO,SAAS,eAAwB;AACtC,SAAO,QAAQ,MAAM;AACvB;AAMA,SAAS,UACP,KACA,EAAE,CAAC,GAAG,GAAG,GAAG,GAAG,KAAK,GACK;AACzB,SAAO;AACT;AAEO,SAAS,gBACd,YACA,SAG2D;AAC3D,MAAI,CAAC,WAAY,QAAO;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CAAC;AAAA,MACP;AAAA,MACA,MAAM,KAAK,UAAU,IAAI;AAAA,MACzB,QAAQ;AAAA,IACV,CAAC,EAGE,KAAK,CAAC,aAAa,SAAS,KAAK,CAAC,EAClC,MAAM,CAAC,QAAiB;AACvB,UAAI,eAAe,SAAS,cAAc,KAAK;AAC7C,gBAAQ,MAAM,IAAI,QAAQ;AAAA,MAC5B,OAAO;AACL,gBAAQ,MAAM,GAAG;AAAA,MACnB;AAAA,IACF,CAAC;AAEH,QAAI,iDAAgB,WAAW;AAC7B,qBAAe,UAAU,OAAO;AAAA,IAClC,OAAO;AACL,YAAM;AAAA,IACR;AAEA,WAAO;AAAA,EACT,SAAS,KAAK;AACZ,YAAQ,MAAM,GAAG;AAAA,EACnB;AACF;AAEA,SAAS,aACP,OACA,gBAKY;AA7Kd;AA8KE,MAAI;AAEF,QAAI,SAAS,CAAC,MAAM,QAAQ,KAAK,GAAG;AAClC,aAAO,EAAE,GAAG,MAAM;AAAA,IACpB;AAEA,QAAI,CAAC,kBAAkB,CAAC,MAAO;AAC/B,UAAM,aAAsC,CAAC;AAE7C,UAAM,uBAAqB,oBAAe,UAAf,mBAAsB,gBAAe,CAAC;AAEjE,eAAW,QAAQ,OAAO;AACxB,UAAI,OAAO,SAAS,UAAU;AAE5B,mBAAW,IAAI,IAAI,mBAAmB,IAAI;AAAA,MAC5C,OAAO;AAEL,eAAO,OAAO,YAAY,IAAI;AAAA,MAChC;AAAA,IACF;AAEA,WAAO,EAAE,GAAG,WAAW;AAAA,EACzB,QAAQ;AAAA,EAER;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../package.json","../../src/utils.ts","../../src/server/index.ts"],"sourcesContent":["{\n \"name\": \"@vercel/analytics\",\n \"version\": \"2.0.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\": \"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 \"nuxt\": {\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 { name as packageName, version } from '../../package.json';\nimport type {\n AllowedPropertyValues,\n FlagsDataInput,\n PlainFlags,\n} from '../types';\nimport { isProduction, parseProperties } from '../utils';\n\ntype HeadersObject = Record<string, string | string[] | undefined>;\ntype AllowedHeaders = Headers | HeadersObject;\n\nfunction isHeaders(headers?: AllowedHeaders): headers is Headers {\n if (!headers) return false;\n return typeof (headers as HeadersObject).entries === 'function';\n}\n\ninterface Options {\n flags?: FlagsDataInput;\n headers?: AllowedHeaders;\n request?: { headers: AllowedHeaders };\n}\n\ninterface RequestContext {\n get: () => {\n headers: Record<string, string | undefined>;\n url: string;\n waitUntil?: (promise: Promise<unknown>) => void;\n flags?: {\n getValues: () => PlainFlags;\n reportValue: (key: string, value: unknown) => void;\n };\n };\n}\n\nconst symbol = Symbol.for('@vercel/request-context');\nconst logPrefix = '[Vercel Web Analytics]';\n\nexport async function track(\n eventName: string,\n properties?: Record<string, AllowedPropertyValues>,\n options?: Options,\n): Promise<void> {\n const ENDPOINT =\n process.env.VERCEL_WEB_ANALYTICS_ENDPOINT || process.env.VERCEL_URL;\n const DISABLE_LOGS = Boolean(process.env.VERCEL_WEB_ANALYTICS_DISABLE_LOGS);\n const BYPASS_SECRET = process.env.VERCEL_AUTOMATION_BYPASS_SECRET;\n\n if (typeof window !== 'undefined') {\n if (!isProduction()) {\n throw new Error(\n `${logPrefix} It seems like you imported the \\`track\\` function from \\`@vercel/web-analytics/server\\` in a browser environment. This function is only meant to be used in a server environment.`,\n );\n }\n\n return;\n }\n\n const props = parseProperties(properties, {\n strip: isProduction(),\n });\n\n if (!ENDPOINT) {\n if (isProduction()) {\n console.log(\n `${logPrefix} Can't find VERCEL_URL in environment variables.`,\n );\n } else if (!DISABLE_LOGS) {\n console.log(\n `${logPrefix} Track \"${eventName}\" ${\n props ? `with data ${JSON.stringify(props)}` : ''\n }`,\n );\n }\n return;\n }\n try {\n const requestContext = (\n (globalThis as never)[symbol] as RequestContext | undefined\n )?.get();\n\n let headers: AllowedHeaders | undefined;\n\n if (options && 'headers' in options) {\n headers = options.headers;\n } else if (options?.request) {\n headers = options.request.headers;\n } else if (requestContext?.headers) {\n // not explicitly passed in context, so take it from async storage\n headers = requestContext.headers;\n }\n\n let tmp: HeadersObject = {};\n if (headers && isHeaders(headers)) {\n headers.forEach((value, key) => {\n tmp[key] = value;\n });\n } else if (headers) {\n tmp = headers;\n }\n\n const url = ENDPOINT.startsWith('http')\n ? ENDPOINT\n : new URL('/_vercel/insights/event', `https://${ENDPOINT}`).toString();\n\n const body = {\n o: requestContext?.url || (tmp.referer as string) || new URL(url).origin,\n ts: Date.now(),\n sdkn: `${packageName}/server`,\n sdkv: version,\n r: '',\n en: eventName,\n ed: props,\n f: safeGetFlags(options?.flags, requestContext),\n };\n\n const hasHeaders = Boolean(headers);\n\n if (!hasHeaders) {\n throw new Error(\n 'No session context found. Pass `request` or `headers` to the `track` function.',\n );\n }\n\n const promise = fetch(url, {\n headers: {\n 'content-type': 'application/json',\n ...(hasHeaders\n ? {\n 'user-agent': tmp['user-agent'] as string,\n 'x-vercel-ip': tmp['x-forwarded-for'] as string,\n 'x-va-server': '1',\n cookie: tmp.cookie as string,\n }\n : {\n 'x-va-server': '2',\n }),\n ...(BYPASS_SECRET\n ? { 'x-vercel-protection-bypass': BYPASS_SECRET }\n : {}),\n },\n body: JSON.stringify(body),\n method: 'POST',\n })\n // We want to always consume the body; some cloud providers track fetch concurrency\n // and may not release the connection until the body is consumed.\n .then((response) => response.text())\n .catch((err: unknown) => {\n if (err instanceof Error && 'response' in err) {\n console.error(err.response);\n } else {\n console.error(err);\n }\n });\n\n if (requestContext?.waitUntil) {\n requestContext.waitUntil(promise);\n } else {\n await promise;\n }\n\n return void 0;\n } catch (err) {\n console.error(err);\n }\n}\n\nfunction safeGetFlags(\n flags: Options['flags'],\n requestContext?: ReturnType<RequestContext['get']>,\n):\n | {\n p: PlainFlags;\n }\n | undefined {\n try {\n // In the case plain flags are passed, just return them\n if (flags && !Array.isArray(flags)) {\n return { p: flags };\n }\n\n if (!requestContext || !flags) return;\n const plainFlags: Record<string, unknown> = {};\n // returns all available plain flags\n const resolvedPlainFlags = requestContext.flags?.getValues() ?? {};\n\n for (const flag of flags) {\n if (typeof flag === 'string') {\n // only picks the desired flags\n plainFlags[flag] = resolvedPlainFlags[flag];\n } else {\n // merge user-provided values with resolved values\n Object.assign(plainFlags, flag);\n }\n }\n\n return { p: plainFlags };\n } catch {\n /* empty */\n }\n}\n"],"mappings":";AACE,WAAQ;AACR,cAAW;;;ACON,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,QAAQ;AAAA,EAER;AACA,SAAO;AACT;AAWO,SAAS,UAAgB;AAC9B,QAAM,OAAO,UAAU,IAAI,OAAO,MAAM,kBAAkB;AAC1D,SAAO,QAAQ;AACjB;AAEO,SAAS,eAAwB;AACtC,SAAO,QAAQ,MAAM;AACvB;AAMA,SAAS,UACP,KACA,EAAE,CAAC,GAAG,GAAG,GAAG,GAAG,KAAK,GACK;AACzB,SAAO;AACT;AAEO,SAAS,gBACd,YACA,SAG2D;AAC3D,MAAI,CAAC,WAAY,QAAO;AACxB,MAAI,QAAQ;AACZ,QAAM,kBAA4B,CAAC;AACnC,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,UAAU,GAAG;AACrD,QAAI,OAAO,UAAU,YAAY,UAAU,MAAM;AAC/C,UAAI,QAAQ,OAAO;AACjB,gBAAQ,UAAU,KAAK,KAAK;AAAA,MAC9B,OAAO;AACL,wBAAgB,KAAK,GAAG;AAAA,MAC1B;AAAA,IACF;AAAA,EACF;AAEA,MAAI,gBAAgB,SAAS,KAAK,CAAC,QAAQ,OAAO;AAChD,UAAM;AAAA,MACJ,2CAA2C,gBAAgB;AAAA,QACzD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