@vercel/analytics 1.1.2 → 1.2.0-beta.2
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.d.cts → index.d.mts} +16 -3
- package/dist/index.d.ts +16 -3
- package/dist/index.js +59 -8
- package/dist/index.js.map +1 -1
- package/dist/{index.cjs → index.mjs} +29 -37
- package/dist/index.mjs.map +1 -0
- package/dist/next/index.d.mts +36 -0
- package/dist/next/index.d.ts +36 -0
- package/dist/next/index.js +201 -0
- package/dist/next/index.js.map +1 -0
- package/dist/next/index.mjs +167 -0
- package/dist/next/index.mjs.map +1 -0
- package/dist/react/{index.d.cts → index.d.mts} +10 -2
- package/dist/react/index.d.ts +10 -2
- package/dist/react/index.js +65 -16
- package/dist/react/index.js.map +1 -1
- package/dist/react/{index.cjs → index.mjs} +41 -42
- package/dist/react/index.mjs.map +1 -0
- package/dist/server/{index.d.cts → index.d.mts} +1 -1
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.js +29 -2
- package/dist/server/index.js.map +1 -1
- package/dist/server/{index.cjs → index.mjs} +3 -30
- package/dist/server/index.mjs.map +1 -0
- package/jest.config.js +1 -1
- package/package.json +23 -4
- package/tsup.config.js +14 -0
- package/dist/index.cjs.map +0 -1
- package/dist/react/index.cjs.map +0 -1
- package/dist/server/index.cjs.map +0 -1
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name2 in all)
|
|
11
|
+
__defProp(target, name2, { get: all[name2], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// src/nextjs/index.tsx
|
|
32
|
+
var nextjs_exports = {};
|
|
33
|
+
__export(nextjs_exports, {
|
|
34
|
+
Analytics: () => Analytics2
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(nextjs_exports);
|
|
37
|
+
var import_react2 = __toESM(require("react"));
|
|
38
|
+
|
|
39
|
+
// src/react.tsx
|
|
40
|
+
var import_react = require("react");
|
|
41
|
+
|
|
42
|
+
// package.json
|
|
43
|
+
var name = "@vercel/analytics";
|
|
44
|
+
var version = "1.2.0-beta.2";
|
|
45
|
+
|
|
46
|
+
// src/queue.ts
|
|
47
|
+
var initQueue = () => {
|
|
48
|
+
if (window.va)
|
|
49
|
+
return;
|
|
50
|
+
window.va = function a(...params) {
|
|
51
|
+
(window.vaq = window.vaq || []).push(params);
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// src/utils.ts
|
|
56
|
+
function isBrowser() {
|
|
57
|
+
return typeof window !== "undefined";
|
|
58
|
+
}
|
|
59
|
+
function detectEnvironment() {
|
|
60
|
+
try {
|
|
61
|
+
const env = process.env.NODE_ENV;
|
|
62
|
+
if (env === "development" || env === "test") {
|
|
63
|
+
return "development";
|
|
64
|
+
}
|
|
65
|
+
} catch (e) {
|
|
66
|
+
}
|
|
67
|
+
return "production";
|
|
68
|
+
}
|
|
69
|
+
function setMode(mode = "auto") {
|
|
70
|
+
if (mode === "auto") {
|
|
71
|
+
window.vam = detectEnvironment();
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
window.vam = mode;
|
|
75
|
+
}
|
|
76
|
+
function getMode() {
|
|
77
|
+
const mode = isBrowser() ? window.vam : detectEnvironment();
|
|
78
|
+
return mode || "production";
|
|
79
|
+
}
|
|
80
|
+
function isDevelopment() {
|
|
81
|
+
return getMode() === "development";
|
|
82
|
+
}
|
|
83
|
+
function computeRoute(pathname, pathParams) {
|
|
84
|
+
if (!pathname || !pathParams) {
|
|
85
|
+
return pathname;
|
|
86
|
+
}
|
|
87
|
+
let result = pathname;
|
|
88
|
+
try {
|
|
89
|
+
for (const [key, valueOrArray] of Object.entries(pathParams)) {
|
|
90
|
+
const isValueArray = Array.isArray(valueOrArray);
|
|
91
|
+
const value = isValueArray ? valueOrArray.join("/") : valueOrArray;
|
|
92
|
+
const expr = isValueArray ? `...${key}` : key;
|
|
93
|
+
const matcher = new RegExp(`/${escapeRegExp(value)}(?=[/?#]|$)`);
|
|
94
|
+
if (matcher.test(result)) {
|
|
95
|
+
result = result.replace(matcher, `/[${expr}]`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return result;
|
|
99
|
+
} catch (e) {
|
|
100
|
+
return pathname;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function escapeRegExp(string) {
|
|
104
|
+
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// src/generic.ts
|
|
108
|
+
var DEV_SCRIPT_URL = "https://va.vercel-scripts.com/v1/script.debug.js";
|
|
109
|
+
var PROD_SCRIPT_URL = "/_vercel/insights/script.js";
|
|
110
|
+
function inject(props = {
|
|
111
|
+
debug: true
|
|
112
|
+
}) {
|
|
113
|
+
var _a;
|
|
114
|
+
if (!isBrowser())
|
|
115
|
+
return;
|
|
116
|
+
setMode(props.mode);
|
|
117
|
+
initQueue();
|
|
118
|
+
if (props.beforeSend) {
|
|
119
|
+
(_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
|
|
120
|
+
}
|
|
121
|
+
const src = props.scriptSrc || (isDevelopment() ? DEV_SCRIPT_URL : PROD_SCRIPT_URL);
|
|
122
|
+
if (document.head.querySelector(`script[src*="${src}"]`))
|
|
123
|
+
return;
|
|
124
|
+
const script = document.createElement("script");
|
|
125
|
+
script.src = src;
|
|
126
|
+
script.defer = true;
|
|
127
|
+
script.dataset.sdkn = name + (props.framework ? `/${props.framework}` : "");
|
|
128
|
+
script.dataset.sdkv = version;
|
|
129
|
+
if (props.disableAutoTrack) {
|
|
130
|
+
script.dataset.disableAutoTrack = "1";
|
|
131
|
+
}
|
|
132
|
+
if (props.endpoint) {
|
|
133
|
+
script.dataset.endpoint = props.endpoint;
|
|
134
|
+
}
|
|
135
|
+
if (props.dsn) {
|
|
136
|
+
script.dataset.dsn = props.dsn;
|
|
137
|
+
}
|
|
138
|
+
script.onerror = () => {
|
|
139
|
+
const errorMessage = isDevelopment() ? "Please check if any ad blockers are enabled and try again." : "Be sure to enable Web Analytics for your project and deploy again. See https://vercel.com/docs/analytics/quickstart for more information.";
|
|
140
|
+
console.log(
|
|
141
|
+
`[Vercel Web Analytics] Failed to load script from ${src}. ${errorMessage}`
|
|
142
|
+
);
|
|
143
|
+
};
|
|
144
|
+
if (isDevelopment() && props.debug === false) {
|
|
145
|
+
script.dataset.debug = "false";
|
|
146
|
+
}
|
|
147
|
+
document.head.appendChild(script);
|
|
148
|
+
}
|
|
149
|
+
function pageview({ route }) {
|
|
150
|
+
var _a;
|
|
151
|
+
(_a = window.va) == null ? void 0 : _a.call(window, "pageview", {
|
|
152
|
+
route
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// src/react.tsx
|
|
157
|
+
function Analytics(props) {
|
|
158
|
+
(0, import_react.useEffect)(() => {
|
|
159
|
+
inject({
|
|
160
|
+
framework: props.framework || "react",
|
|
161
|
+
...props.route && { disableAutoTrack: true },
|
|
162
|
+
...props
|
|
163
|
+
});
|
|
164
|
+
}, [props]);
|
|
165
|
+
(0, import_react.useEffect)(() => {
|
|
166
|
+
if (props.route) {
|
|
167
|
+
pageview({
|
|
168
|
+
route: props.route
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
}, [props.route, props.path]);
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// src/nextjs/utils.ts
|
|
176
|
+
var import_navigation = require("next/navigation.js");
|
|
177
|
+
var useRoute = () => {
|
|
178
|
+
const params = (0, import_navigation.useParams)();
|
|
179
|
+
const searchParams = (0, import_navigation.useSearchParams)();
|
|
180
|
+
const path = (0, import_navigation.usePathname)();
|
|
181
|
+
const finalParams = {
|
|
182
|
+
...Object.fromEntries(searchParams.entries()),
|
|
183
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- can be empty in pages router
|
|
184
|
+
...params || {}
|
|
185
|
+
};
|
|
186
|
+
return { route: computeRoute(path, finalParams), path };
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
// src/nextjs/index.tsx
|
|
190
|
+
function AnalyticsComponent(props) {
|
|
191
|
+
const { route, path } = useRoute();
|
|
192
|
+
return /* @__PURE__ */ import_react2.default.createElement(Analytics, { path, route, ...props, framework: "next" });
|
|
193
|
+
}
|
|
194
|
+
function Analytics2(props) {
|
|
195
|
+
return /* @__PURE__ */ import_react2.default.createElement(import_react2.Suspense, { fallback: null }, /* @__PURE__ */ import_react2.default.createElement(AnalyticsComponent, { ...props }));
|
|
196
|
+
}
|
|
197
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
198
|
+
0 && (module.exports = {
|
|
199
|
+
Analytics
|
|
200
|
+
});
|
|
201
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/nextjs/index.tsx","../../src/react.tsx","../../package.json","../../src/queue.ts","../../src/utils.ts","../../src/generic.ts","../../src/nextjs/utils.ts"],"sourcesContent":["import React, { Suspense } from 'react';\nimport { Analytics as AnalyticsScript } from '../react';\nimport type { AnalyticsProps } from '../types';\nimport { useRoute } from './utils';\n\ntype Props = Omit<AnalyticsProps, 'route'>;\n\nfunction AnalyticsComponent(props: Props): React.ReactElement {\n const { route, path } = useRoute();\n\n return (\n <AnalyticsScript path={path} route={route} {...props} framework=\"next\" />\n );\n}\n\nexport function Analytics(props: Props): React.ReactElement {\n return (\n <Suspense fallback={null}>\n <AnalyticsComponent {...props} />\n </Suspense>\n );\n}\n\nexport type { AnalyticsProps };\n","import { useEffect } from 'react';\nimport { inject, track, pageview } from './generic';\nimport type { AnalyticsProps } from './types';\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 path?: string;\n }\n): null {\n useEffect(() => {\n inject({\n framework: props.framework || 'react',\n ...(props.route && { disableAutoTrack: true }),\n ...props,\n });\n }, [props]);\n\n useEffect(() => {\n if (props.route) {\n pageview({\n route: props.route,\n });\n }\n }, [props.route, props.path]);\n\n return null;\n}\n\nexport { track, Analytics };\nexport type { AnalyticsProps };\n","{\n \"name\": \"@vercel/analytics\",\n \"version\": \"1.2.0-beta.2\",\n \"description\": \"Gain real-time traffic insights with Vercel Web Analytics\",\n \"keywords\": [\n \"analytics\",\n \"vercel\"\n ],\n \"repository\": {\n \"url\": \"github:vercel/analytics\",\n \"directory\": \"packages/web\"\n },\n \"license\": \"MPL-2.0\",\n \"exports\": {\n \"./package.json\": \"./package.json\",\n \".\": {\n \"browser\": \"./dist/index.js\",\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"./react\": {\n \"browser\": \"./dist/react/index.js\",\n \"import\": \"./dist/react/index.js\",\n \"require\": \"./dist/react/index.cjs\"\n },\n \"./next\": {\n \"browser\": \"./dist/next/index.mjs\",\n \"import\": \"./dist/next/index.mjs\",\n \"require\": \"./dist/next/index.js\"\n },\n \"./server\": {\n \"node\": \"./dist/server/index.js\",\n \"edge-light\": \"./dist/server/index.js\",\n \"import\": \"./dist/server/index.js\",\n \"require\": \"./dist/server/index.cjs\",\n \"default\": \"./dist/server/index.cjs\"\n }\n },\n \"main\": \"./dist/index.js\",\n \"types\": \"./dist/index.d.ts\",\n \"typesVersions\": {\n \"*\": {\n \"*\": [\n \"dist/index.d.ts\"\n ],\n \"react\": [\n \"dist/react/index.d.ts\"\n ],\n \"server\": [\n \"dist/server/index.d.ts\"\n ],\n \"next\": [\n \"dist/next/index.d.ts\"\n ]\n }\n },\n \"scripts\": {\n \"build\": \"tsup\",\n \"dev\": \"tsup --watch\",\n \"lint\": \"eslint .\",\n \"lint-fix\": \"eslint . --fix\",\n \"test\": \"jest\",\n \"type-check\": \"tsc --noEmit\"\n },\n \"eslintConfig\": {\n \"extends\": [\n \"@vercel/eslint-config\"\n ],\n \"rules\": {\n \"tsdoc/syntax\": \"off\"\n },\n \"ignorePatterns\": [\n \"jest.setup.ts\"\n ]\n },\n \"dependencies\": {\n \"server-only\": \"^0.0.1\"\n },\n \"devDependencies\": {\n \"@swc/core\": \"^1.3.66\",\n \"@swc/jest\": \"^0.2.26\",\n \"@testing-library/jest-dom\": \"^5.16.5\",\n \"@testing-library/react\": \"^14.0.0\",\n \"@types/jest\": \"^29.5.2\",\n \"@types/node\": \"^20.3.1\",\n \"@types/react\": \"^18.2.14\",\n \"@types/testing-library__jest-dom\": \"^5.14.6\",\n \"@vercel/eslint-config\": \"workspace:0.0.0\",\n \"jest\": \"^29.5.0\",\n \"jest-environment-jsdom\": \"^29.5.0\",\n \"react\": \"^18.2.0\",\n \"react-dom\": \"^18.2.0\",\n \"tsup\": \"7.1.0\"\n },\n \"peerDependencies\": {\n \"next\": \">= 13\",\n \"react\": \"^18 || ^19\"\n },\n \"peerDependenciesMeta\": {\n \"next\": {\n \"optional\": true\n },\n \"react\": {\n \"optional\": true\n }\n }\n}\n","export const initQueue = (): void => {\n // initialize va until script is loaded\n if (window.va) return;\n\n window.va = function a(...params): void {\n (window.vaq = window.vaq || []).push(params);\n };\n};\n","import type { AllowedPropertyValues, Mode } from './types';\n\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nfunction detectEnvironment(): 'development' | 'production' {\n try {\n const env = process.env.NODE_ENV;\n if (env === 'development' || env === 'test') {\n return 'development';\n }\n } catch (e) {\n // do nothing, this is okay\n }\n return 'production';\n}\n\nexport function setMode(mode: Mode = 'auto'): void {\n if (mode === 'auto') {\n window.vam = detectEnvironment();\n return;\n }\n\n window.vam = mode;\n}\n\nexport function getMode(): Mode {\n const mode = isBrowser() ? window.vam : detectEnvironment();\n return mode || 'production';\n}\n\nexport function isProduction(): boolean {\n return getMode() === 'production';\n}\n\nexport function isDevelopment(): boolean {\n return getMode() === 'development';\n}\n\nfunction removeKey(\n key: string,\n { [key]: _, ...rest }\n): Record<string, unknown> {\n return rest;\n}\n\nexport function parseProperties(\n properties: Record<string, unknown> | undefined,\n options: {\n strip?: boolean;\n }\n): Error | Record<string, AllowedPropertyValues> | undefined {\n if (!properties) return undefined;\n let props = properties;\n const errorProperties: string[] = [];\n for (const [key, value] of Object.entries(properties)) {\n if (typeof value === 'object' && value !== null) {\n if (options.strip) {\n props = removeKey(key, props);\n } else {\n errorProperties.push(key);\n }\n }\n }\n\n if (errorProperties.length > 0 && !options.strip) {\n throw Error(\n `The following properties are not valid: ${errorProperties.join(\n ', '\n )}. Only strings, numbers, booleans, and null are allowed.`\n );\n }\n return props as Record<string, AllowedPropertyValues>;\n}\n\nexport function computeRoute(\n pathname: string | null,\n pathParams: Record<string, string | string[]> | null\n): string | null {\n if (!pathname || !pathParams) {\n return pathname;\n }\n\n let result = pathname;\n\n try {\n for (const [key, valueOrArray] of Object.entries(pathParams)) {\n const isValueArray = Array.isArray(valueOrArray);\n const value = isValueArray ? valueOrArray.join('/') : valueOrArray;\n const expr = isValueArray ? `...${key}` : key;\n\n const matcher = new RegExp(`/${escapeRegExp(value)}(?=[/?#]|$)`);\n if (matcher.test(result)) {\n result = result.replace(matcher, `/[${expr}]`);\n }\n }\n\n return result;\n } catch (e) {\n return pathname;\n }\n}\n\nfunction escapeRegExp(string: string): string {\n return string.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n}\n","import { name as packageName, version } from '../package.json';\nimport { initQueue } from './queue';\nimport type { AllowedPropertyValues, AnalyticsProps } from './types';\nimport {\n isBrowser,\n parseProperties,\n setMode,\n isDevelopment,\n isProduction,\n} from './utils';\n\nexport const DEV_SCRIPT_URL =\n 'https://va.vercel-scripts.com/v1/script.debug.js';\nexport const PROD_SCRIPT_URL = '/_vercel/insights/script.js';\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 */\nfunction inject(\n props: AnalyticsProps & {\n framework?: string;\n } = {\n debug: true,\n }\n): void {\n if (!isBrowser()) return;\n\n setMode(props.mode);\n\n initQueue();\n\n if (props.beforeSend) {\n window.va?.('beforeSend', props.beforeSend);\n }\n\n const src =\n props.scriptSrc || (isDevelopment() ? DEV_SCRIPT_URL : PROD_SCRIPT_URL);\n\n if (document.head.querySelector(`script[src*=\"${src}\"]`)) return;\n\n const script = document.createElement('script');\n script.src = src;\n script.defer = true;\n script.dataset.sdkn =\n packageName + (props.framework ? `/${props.framework}` : '');\n script.dataset.sdkv = version;\n\n if (props.disableAutoTrack) {\n script.dataset.disableAutoTrack = '1';\n }\n if (props.endpoint) {\n script.dataset.endpoint = props.endpoint;\n }\n if (props.dsn) {\n script.dataset.dsn = props.dsn;\n }\n\n script.onerror = (): void => {\n const errorMessage = isDevelopment()\n ? 'Please check if any ad blockers are enabled and try again.'\n : 'Be sure to enable Web Analytics for your project and deploy again. See https://vercel.com/docs/analytics/quickstart for more information.';\n\n // eslint-disable-next-line no-console -- Logging to console is intentional\n console.log(\n `[Vercel Web Analytics] Failed to load script from ${src}. ${errorMessage}`\n );\n };\n\n if (isDevelopment() && props.debug === false) {\n script.dataset.debug = 'false';\n }\n\n document.head.appendChild(script);\n}\n\n/**\n * Tracks a custom event. Please refer to the [documentation](https://vercel.com/docs/concepts/analytics/custom-events) for more information on custom events.\n * @param name - The name of the event.\n * * Examples: `Purchase`, `Click Button`, or `Play Video`.\n * @param [properties] - Additional properties of the event. Nested objects are not supported. Allowed values are `string`, `number`, `boolean`, and `null`.\n */\nfunction track(\n name: string,\n properties?: Record<string, AllowedPropertyValues>\n): void {\n if (!isBrowser()) {\n const msg =\n '[Vercel Web Analytics] Please import `track` from `@vercel/analytics/server` when using this function in a server environment';\n\n if (isProduction()) {\n // eslint-disable-next-line no-console -- Show warning in production\n console.warn(msg);\n } else {\n throw new Error(msg);\n }\n\n return;\n }\n\n if (!properties) {\n window.va?.('event', { name });\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 });\n } catch (err) {\n if (err instanceof Error && isDevelopment()) {\n // eslint-disable-next-line no-console -- Logging to console is intentional\n console.error(err);\n }\n }\n}\n\nfunction pageview({ route }: { route?: string }): void {\n window.va?.('pageview', {\n route,\n });\n}\n\nexport { inject, track, pageview };\nexport type { AnalyticsProps };\n\n// eslint-disable-next-line import/no-default-export -- Default export is intentional\nexport default {\n inject,\n track,\n};\n","'use client';\nimport { useParams, usePathname, useSearchParams } from 'next/navigation.js';\nimport { computeRoute } from '../utils';\n\nexport const useRoute = (): {\n route: string | null;\n path: string;\n} => {\n const params = useParams();\n const searchParams = useSearchParams();\n const path = usePathname();\n\n const finalParams = {\n ...Object.fromEntries(searchParams.entries()),\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- can be empty in pages router\n ...(params || {}),\n };\n\n return { route: computeRoute(path, finalParams), path };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,mBAAAA;AAAA;AAAA;AAAA,IAAAC,gBAAgC;;;ACAhC,mBAA0B;;;ACCxB,WAAQ;AACR,cAAW;;;ACFN,IAAM,YAAY,MAAY;AAEnC,MAAI,OAAO;AAAI;AAEf,SAAO,KAAK,SAAS,KAAK,QAAc;AACtC,KAAC,OAAO,MAAM,OAAO,OAAO,CAAC,GAAG,K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QAAI,MAAM,OAAO;AACf,eAAS;AAAA,QACP,OAAO,MAAM;AAAA,MACf,CAAC;AAAA,IACH;AAAA,EACF,GAAG,CAAC,MAAM,OAAO,MAAM,IAAI,CAAC;AAE5B,SAAO;AACT;;;AKjDA,wBAAwD;AAGjD,IAAM,WAAW,MAGnB;AACH,QAAM,aAAS,6BAAU;AACzB,QAAM,mBAAe,mCAAgB;AACrC,QAAM,WAAO,+BAAY;AAEzB,QAAM,cAAc;AAAA,IAClB,GAAG,OAAO,YAAY,aAAa,QAAQ,CAAC;AAAA;AAAA,IAE5C,GAAI,UAAU,CAAC;AAAA,EACjB;AAEA,SAAO,EAAE,OAAO,aAAa,MAAM,WAAW,GAAG,KAAK;AACxD;;;ANZA,SAAS,mBAAmB,OAAkC;AAC5D,QAAM,EAAE,OAAO,KAAK,IAAI,SAAS;AAEjC,SACE,8BAAAC,QAAA,cAAC,aAAgB,MAAY,OAAe,GAAG,OAAO,WAAU,QAAO;AAE3E;AAEO,SAASC,WAAU,OAAkC;AAC1D,SACE,8BAAAD,QAAA,cAAC,0BAAS,UAAU,QAClB,8BAAAA,QAAA,cAAC,sBAAoB,GAAG,OAAO,CACjC;AAEJ;","names":["Analytics","import_react","React","Analytics"]}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/nextjs/index.tsx
|
|
4
|
+
import React, { Suspense } from "react";
|
|
5
|
+
|
|
6
|
+
// src/react.tsx
|
|
7
|
+
import { useEffect } from "react";
|
|
8
|
+
|
|
9
|
+
// package.json
|
|
10
|
+
var name = "@vercel/analytics";
|
|
11
|
+
var version = "1.2.0-beta.2";
|
|
12
|
+
|
|
13
|
+
// src/queue.ts
|
|
14
|
+
var initQueue = () => {
|
|
15
|
+
if (window.va)
|
|
16
|
+
return;
|
|
17
|
+
window.va = function a(...params) {
|
|
18
|
+
(window.vaq = window.vaq || []).push(params);
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/utils.ts
|
|
23
|
+
function isBrowser() {
|
|
24
|
+
return typeof window !== "undefined";
|
|
25
|
+
}
|
|
26
|
+
function detectEnvironment() {
|
|
27
|
+
try {
|
|
28
|
+
const env = process.env.NODE_ENV;
|
|
29
|
+
if (env === "development" || env === "test") {
|
|
30
|
+
return "development";
|
|
31
|
+
}
|
|
32
|
+
} catch (e) {
|
|
33
|
+
}
|
|
34
|
+
return "production";
|
|
35
|
+
}
|
|
36
|
+
function setMode(mode = "auto") {
|
|
37
|
+
if (mode === "auto") {
|
|
38
|
+
window.vam = detectEnvironment();
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
window.vam = mode;
|
|
42
|
+
}
|
|
43
|
+
function getMode() {
|
|
44
|
+
const mode = isBrowser() ? window.vam : detectEnvironment();
|
|
45
|
+
return mode || "production";
|
|
46
|
+
}
|
|
47
|
+
function isDevelopment() {
|
|
48
|
+
return getMode() === "development";
|
|
49
|
+
}
|
|
50
|
+
function computeRoute(pathname, pathParams) {
|
|
51
|
+
if (!pathname || !pathParams) {
|
|
52
|
+
return pathname;
|
|
53
|
+
}
|
|
54
|
+
let result = pathname;
|
|
55
|
+
try {
|
|
56
|
+
for (const [key, valueOrArray] of Object.entries(pathParams)) {
|
|
57
|
+
const isValueArray = Array.isArray(valueOrArray);
|
|
58
|
+
const value = isValueArray ? valueOrArray.join("/") : valueOrArray;
|
|
59
|
+
const expr = isValueArray ? `...${key}` : key;
|
|
60
|
+
const matcher = new RegExp(`/${escapeRegExp(value)}(?=[/?#]|$)`);
|
|
61
|
+
if (matcher.test(result)) {
|
|
62
|
+
result = result.replace(matcher, `/[${expr}]`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return result;
|
|
66
|
+
} catch (e) {
|
|
67
|
+
return pathname;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function escapeRegExp(string) {
|
|
71
|
+
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/generic.ts
|
|
75
|
+
var DEV_SCRIPT_URL = "https://va.vercel-scripts.com/v1/script.debug.js";
|
|
76
|
+
var PROD_SCRIPT_URL = "/_vercel/insights/script.js";
|
|
77
|
+
function inject(props = {
|
|
78
|
+
debug: true
|
|
79
|
+
}) {
|
|
80
|
+
var _a;
|
|
81
|
+
if (!isBrowser())
|
|
82
|
+
return;
|
|
83
|
+
setMode(props.mode);
|
|
84
|
+
initQueue();
|
|
85
|
+
if (props.beforeSend) {
|
|
86
|
+
(_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
|
|
87
|
+
}
|
|
88
|
+
const src = props.scriptSrc || (isDevelopment() ? DEV_SCRIPT_URL : PROD_SCRIPT_URL);
|
|
89
|
+
if (document.head.querySelector(`script[src*="${src}"]`))
|
|
90
|
+
return;
|
|
91
|
+
const script = document.createElement("script");
|
|
92
|
+
script.src = src;
|
|
93
|
+
script.defer = true;
|
|
94
|
+
script.dataset.sdkn = name + (props.framework ? `/${props.framework}` : "");
|
|
95
|
+
script.dataset.sdkv = version;
|
|
96
|
+
if (props.disableAutoTrack) {
|
|
97
|
+
script.dataset.disableAutoTrack = "1";
|
|
98
|
+
}
|
|
99
|
+
if (props.endpoint) {
|
|
100
|
+
script.dataset.endpoint = props.endpoint;
|
|
101
|
+
}
|
|
102
|
+
if (props.dsn) {
|
|
103
|
+
script.dataset.dsn = props.dsn;
|
|
104
|
+
}
|
|
105
|
+
script.onerror = () => {
|
|
106
|
+
const errorMessage = isDevelopment() ? "Please check if any ad blockers are enabled and try again." : "Be sure to enable Web Analytics for your project and deploy again. See https://vercel.com/docs/analytics/quickstart for more information.";
|
|
107
|
+
console.log(
|
|
108
|
+
`[Vercel Web Analytics] Failed to load script from ${src}. ${errorMessage}`
|
|
109
|
+
);
|
|
110
|
+
};
|
|
111
|
+
if (isDevelopment() && props.debug === false) {
|
|
112
|
+
script.dataset.debug = "false";
|
|
113
|
+
}
|
|
114
|
+
document.head.appendChild(script);
|
|
115
|
+
}
|
|
116
|
+
function pageview({ route }) {
|
|
117
|
+
var _a;
|
|
118
|
+
(_a = window.va) == null ? void 0 : _a.call(window, "pageview", {
|
|
119
|
+
route
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// src/react.tsx
|
|
124
|
+
function Analytics(props) {
|
|
125
|
+
useEffect(() => {
|
|
126
|
+
inject({
|
|
127
|
+
framework: props.framework || "react",
|
|
128
|
+
...props.route && { disableAutoTrack: true },
|
|
129
|
+
...props
|
|
130
|
+
});
|
|
131
|
+
}, [props]);
|
|
132
|
+
useEffect(() => {
|
|
133
|
+
if (props.route) {
|
|
134
|
+
pageview({
|
|
135
|
+
route: props.route
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}, [props.route, props.path]);
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// src/nextjs/utils.ts
|
|
143
|
+
import { useParams, usePathname, useSearchParams } from "next/navigation.js";
|
|
144
|
+
var useRoute = () => {
|
|
145
|
+
const params = useParams();
|
|
146
|
+
const searchParams = useSearchParams();
|
|
147
|
+
const path = usePathname();
|
|
148
|
+
const finalParams = {
|
|
149
|
+
...Object.fromEntries(searchParams.entries()),
|
|
150
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- can be empty in pages router
|
|
151
|
+
...params || {}
|
|
152
|
+
};
|
|
153
|
+
return { route: computeRoute(path, finalParams), path };
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
// src/nextjs/index.tsx
|
|
157
|
+
function AnalyticsComponent(props) {
|
|
158
|
+
const { route, path } = useRoute();
|
|
159
|
+
return /* @__PURE__ */ React.createElement(Analytics, { path, route, ...props, framework: "next" });
|
|
160
|
+
}
|
|
161
|
+
function Analytics2(props) {
|
|
162
|
+
return /* @__PURE__ */ React.createElement(Suspense, { fallback: null }, /* @__PURE__ */ React.createElement(AnalyticsComponent, { ...props }));
|
|
163
|
+
}
|
|
164
|
+
export {
|
|
165
|
+
Analytics2 as Analytics
|
|
166
|
+
};
|
|
167
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/nextjs/index.tsx","../../src/react.tsx","../../package.json","../../src/queue.ts","../../src/utils.ts","../../src/generic.ts","../../src/nextjs/utils.ts"],"sourcesContent":["import React, { Suspense } from 'react';\nimport { Analytics as AnalyticsScript } from '../react';\nimport type { AnalyticsProps } from '../types';\nimport { useRoute } from './utils';\n\ntype Props = Omit<AnalyticsProps, 'route'>;\n\nfunction AnalyticsComponent(props: Props): React.ReactElement {\n const { route, path } = useRoute();\n\n return (\n <AnalyticsScript path={path} route={route} {...props} framework=\"next\" />\n );\n}\n\nexport function Analytics(props: Props): React.ReactElement {\n return (\n <Suspense fallback={null}>\n <AnalyticsComponent {...props} />\n </Suspense>\n );\n}\n\nexport type { AnalyticsProps };\n","import { useEffect } from 'react';\nimport { inject, track, pageview } from './generic';\nimport type { AnalyticsProps } from './types';\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 path?: string;\n }\n): null {\n useEffect(() => {\n inject({\n framework: props.framework || 'react',\n ...(props.route && { disableAutoTrack: true }),\n ...props,\n });\n }, [props]);\n\n useEffect(() => {\n if (props.route) {\n pageview({\n route: props.route,\n });\n }\n }, [props.route, props.path]);\n\n return null;\n}\n\nexport { track, Analytics };\nexport type { AnalyticsProps };\n","{\n \"name\": \"@vercel/analytics\",\n \"version\": \"1.2.0-beta.2\",\n \"description\": \"Gain real-time traffic insights with Vercel Web Analytics\",\n \"keywords\": [\n \"analytics\",\n \"vercel\"\n ],\n \"repository\": {\n \"url\": \"github:vercel/analytics\",\n \"directory\": \"packages/web\"\n },\n \"license\": \"MPL-2.0\",\n \"exports\": {\n \"./package.json\": \"./package.json\",\n \".\": {\n \"browser\": \"./dist/index.js\",\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"./react\": {\n \"browser\": \"./dist/react/index.js\",\n \"import\": \"./dist/react/index.js\",\n \"require\": \"./dist/react/index.cjs\"\n },\n \"./next\": {\n \"browser\": \"./dist/next/index.mjs\",\n \"import\": \"./dist/next/index.mjs\",\n \"require\": \"./dist/next/index.js\"\n },\n \"./server\": {\n \"node\": \"./dist/server/index.js\",\n \"edge-light\": \"./dist/server/index.js\",\n \"import\": \"./dist/server/index.js\",\n \"require\": \"./dist/server/index.cjs\",\n \"default\": \"./dist/server/index.cjs\"\n }\n },\n \"main\": \"./dist/index.js\",\n \"types\": \"./dist/index.d.ts\",\n \"typesVersions\": {\n \"*\": {\n \"*\": [\n \"dist/index.d.ts\"\n ],\n \"react\": [\n \"dist/react/index.d.ts\"\n ],\n \"server\": [\n \"dist/server/index.d.ts\"\n ],\n \"next\": [\n \"dist/next/index.d.ts\"\n ]\n }\n },\n \"scripts\": {\n \"build\": \"tsup\",\n \"dev\": \"tsup --watch\",\n \"lint\": \"eslint .\",\n \"lint-fix\": \"eslint . --fix\",\n \"test\": \"jest\",\n \"type-check\": \"tsc --noEmit\"\n },\n \"eslintConfig\": {\n \"extends\": [\n \"@vercel/eslint-config\"\n ],\n \"rules\": {\n \"tsdoc/syntax\": \"off\"\n },\n \"ignorePatterns\": [\n \"jest.setup.ts\"\n ]\n },\n \"dependencies\": {\n \"server-only\": \"^0.0.1\"\n },\n \"devDependencies\": {\n \"@swc/core\": \"^1.3.66\",\n \"@swc/jest\": \"^0.2.26\",\n \"@testing-library/jest-dom\": \"^5.16.5\",\n \"@testing-library/react\": \"^14.0.0\",\n \"@types/jest\": \"^29.5.2\",\n \"@types/node\": \"^20.3.1\",\n \"@types/react\": \"^18.2.14\",\n \"@types/testing-library__jest-dom\": \"^5.14.6\",\n \"@vercel/eslint-config\": \"workspace:0.0.0\",\n \"jest\": \"^29.5.0\",\n \"jest-environment-jsdom\": \"^29.5.0\",\n \"react\": \"^18.2.0\",\n \"react-dom\": \"^18.2.0\",\n \"tsup\": \"7.1.0\"\n },\n \"peerDependencies\": {\n \"next\": \">= 13\",\n \"react\": \"^18 || ^19\"\n },\n \"peerDependenciesMeta\": {\n \"next\": {\n \"optional\": true\n },\n \"react\": {\n \"optional\": true\n }\n }\n}\n","export const initQueue = (): void => {\n // initialize va until script is loaded\n if (window.va) return;\n\n window.va = function a(...params): void {\n (window.vaq = window.vaq || []).push(params);\n };\n};\n","import type { AllowedPropertyValues, Mode } from './types';\n\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nfunction detectEnvironment(): 'development' | 'production' {\n try {\n const env = process.env.NODE_ENV;\n if (env === 'development' || env === 'test') {\n return 'development';\n }\n } catch (e) {\n // do nothing, this is okay\n }\n return 'production';\n}\n\nexport function setMode(mode: Mode = 'auto'): void {\n if (mode === 'auto') {\n window.vam = detectEnvironment();\n return;\n }\n\n window.vam = mode;\n}\n\nexport function getMode(): Mode {\n const mode = isBrowser() ? window.vam : detectEnvironment();\n return mode || 'production';\n}\n\nexport function isProduction(): boolean {\n return getMode() === 'production';\n}\n\nexport function isDevelopment(): boolean {\n return getMode() === 'development';\n}\n\nfunction removeKey(\n key: string,\n { [key]: _, ...rest }\n): Record<string, unknown> {\n return rest;\n}\n\nexport function parseProperties(\n properties: Record<string, unknown> | undefined,\n options: {\n strip?: boolean;\n }\n): Error | Record<string, AllowedPropertyValues> | undefined {\n if (!properties) return undefined;\n let props = properties;\n const errorProperties: string[] = [];\n for (const [key, value] of Object.entries(properties)) {\n if (typeof value === 'object' && value !== null) {\n if (options.strip) {\n props = removeKey(key, props);\n } else {\n errorProperties.push(key);\n }\n }\n }\n\n if (errorProperties.length > 0 && !options.strip) {\n throw Error(\n `The following properties are not valid: ${errorProperties.join(\n ', '\n )}. Only strings, numbers, booleans, and null are allowed.`\n );\n }\n return props as Record<string, AllowedPropertyValues>;\n}\n\nexport function computeRoute(\n pathname: string | null,\n pathParams: Record<string, string | string[]> | null\n): string | null {\n if (!pathname || !pathParams) {\n return pathname;\n }\n\n let result = pathname;\n\n try {\n for (const [key, valueOrArray] of Object.entries(pathParams)) {\n const isValueArray = Array.isArray(valueOrArray);\n const value = isValueArray ? valueOrArray.join('/') : valueOrArray;\n const expr = isValueArray ? `...${key}` : key;\n\n const matcher = new RegExp(`/${escapeRegExp(value)}(?=[/?#]|$)`);\n if (matcher.test(result)) {\n result = result.replace(matcher, `/[${expr}]`);\n }\n }\n\n return result;\n } catch (e) {\n return pathname;\n }\n}\n\nfunction escapeRegExp(string: string): string {\n return string.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n}\n","import { name as packageName, version } from '../package.json';\nimport { initQueue } from './queue';\nimport type { AllowedPropertyValues, AnalyticsProps } from './types';\nimport {\n isBrowser,\n parseProperties,\n setMode,\n isDevelopment,\n isProduction,\n} from './utils';\n\nexport const DEV_SCRIPT_URL =\n 'https://va.vercel-scripts.com/v1/script.debug.js';\nexport const PROD_SCRIPT_URL = '/_vercel/insights/script.js';\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 */\nfunction inject(\n props: AnalyticsProps & {\n framework?: string;\n } = {\n debug: true,\n }\n): void {\n if (!isBrowser()) return;\n\n setMode(props.mode);\n\n initQueue();\n\n if (props.beforeSend) {\n window.va?.('beforeSend', props.beforeSend);\n }\n\n const src =\n props.scriptSrc || (isDevelopment() ? DEV_SCRIPT_URL : PROD_SCRIPT_URL);\n\n if (document.head.querySelector(`script[src*=\"${src}\"]`)) return;\n\n const script = document.createElement('script');\n script.src = src;\n script.defer = true;\n script.dataset.sdkn =\n packageName + (props.framework ? `/${props.framework}` : '');\n script.dataset.sdkv = version;\n\n if (props.disableAutoTrack) {\n script.dataset.disableAutoTrack = '1';\n }\n if (props.endpoint) {\n script.dataset.endpoint = props.endpoint;\n }\n if (props.dsn) {\n script.dataset.dsn = props.dsn;\n }\n\n script.onerror = (): void => {\n const errorMessage = isDevelopment()\n ? 'Please check if any ad blockers are enabled and try again.'\n : 'Be sure to enable Web Analytics for your project and deploy again. See https://vercel.com/docs/analytics/quickstart for more information.';\n\n // eslint-disable-next-line no-console -- Logging to console is intentional\n console.log(\n `[Vercel Web Analytics] Failed to load script from ${src}. ${errorMessage}`\n );\n };\n\n if (isDevelopment() && props.debug === false) {\n script.dataset.debug = 'false';\n }\n\n document.head.appendChild(script);\n}\n\n/**\n * Tracks a custom event. Please refer to the [documentation](https://vercel.com/docs/concepts/analytics/custom-events) for more information on custom events.\n * @param name - The name of the event.\n * * Examples: `Purchase`, `Click Button`, or `Play Video`.\n * @param [properties] - Additional properties of the event. Nested objects are not supported. Allowed values are `string`, `number`, `boolean`, and `null`.\n */\nfunction track(\n name: string,\n properties?: Record<string, AllowedPropertyValues>\n): void {\n if (!isBrowser()) {\n const msg =\n '[Vercel Web Analytics] Please import `track` from `@vercel/analytics/server` when using this function in a server environment';\n\n if (isProduction()) {\n // eslint-disable-next-line no-console -- Show warning in production\n console.warn(msg);\n } else {\n throw new Error(msg);\n }\n\n return;\n }\n\n if (!properties) {\n window.va?.('event', { name });\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 });\n } catch (err) {\n if (err instanceof Error && isDevelopment()) {\n // eslint-disable-next-line no-console -- Logging to console is intentional\n console.error(err);\n }\n }\n}\n\nfunction pageview({ route }: { route?: string }): void {\n window.va?.('pageview', {\n route,\n });\n}\n\nexport { inject, track, pageview };\nexport type { AnalyticsProps };\n\n// eslint-disable-next-line import/no-default-export -- Default export is intentional\nexport default {\n inject,\n track,\n};\n","'use client';\nimport { useParams, usePathname, useSearchParams } from 'next/navigation.js';\nimport { computeRoute } from '../utils';\n\nexport const useRoute = (): {\n route: string | null;\n path: string;\n} => {\n const params = useParams();\n const searchParams = useSearchParams();\n const path = usePathname();\n\n const finalParams = {\n ...Object.fromEntries(searchParams.entries()),\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- can be empty in pages router\n ...(params || {}),\n };\n\n return { route: computeRoute(path, finalParams), path };\n};\n"],"mappings":";;;AAAA,OAAO,SAAS,gBAAgB;;;ACAhC,SAAS,iBAAiB;;;ACCxB,WAAQ;AACR,cAAW;;;ACFN,IAAM,YAAY,MAAY;AAEnC,MAAI,OAAO;AAAI;AAEf,SAAO,KAAK,SAAS,KAAK,QAAc;AACtC,KAAC,OAAO,MAAM,OAAO,OAAO,CAAC,GAAG,KAAK,MAAM;AAAA,EAC7C;AACF;;;ACLO,SAAS,YAAqB;AACnC,SAAO,OAAO,WAAW;AAC3B;AAEA,SAAS,oBAAkD;AACzD,MAAI;AACF,UAAM,MAAM,QAAQ,IAAI;AACxB,QAAI,QAAQ,iBAAiB,QAAQ,QAAQ;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GjD,IAAM,WAAW,MAGnB;AACH,QAAM,SAAS,UAAU;AACzB,QAAM,eAAe,gBAAgB;AACrC,QAAM,OAAO,YAAY;AAEzB,QAAM,cAAc;AAAA,IAClB,GAAG,OAAO,YAAY,aAAa,QAAQ,CAAC;AAAA;AAAA,IAE5C,GAAI,UAAU,CAAC;AAAA,EACjB;AAEA,SAAO,EAAE,OAAO,aAAa,MAAM,WAAW,GAAG,KAAK;AACxD;;;ANZA,SAAS,mBAAmB,OAAkC;AAC5D,QAAM,EAAE,OAAO,KAAK,IAAI,SAAS;AAEjC,SACE,oCAAC,aAAgB,MAAY,OAAe,GAAG,OAAO,WAAU,QAAO;AAE3E;AAEO,SAASA,WAAU,OAAkC;AAC1D,SACE,oCAAC,YAAS,UAAU,QAClB,oCAAC,sBAAoB,GAAG,OAAO,CACjC;AAEJ;","names":["Analytics"]}
|
|
@@ -14,10 +14,15 @@ interface AnalyticsProps {
|
|
|
14
14
|
beforeSend?: BeforeSend;
|
|
15
15
|
debug?: boolean;
|
|
16
16
|
mode?: Mode;
|
|
17
|
+
route?: string | null;
|
|
18
|
+
disableAutoTrack?: boolean;
|
|
19
|
+
scriptSrc?: string;
|
|
20
|
+
endpoint?: string;
|
|
21
|
+
dsn?: string;
|
|
17
22
|
}
|
|
18
23
|
declare global {
|
|
19
24
|
interface Window {
|
|
20
|
-
va?: (event: 'beforeSend' | 'event', properties?: unknown) => void;
|
|
25
|
+
va?: (event: 'beforeSend' | 'event' | 'pageview', properties?: unknown) => void;
|
|
21
26
|
vaq?: [string, unknown?][];
|
|
22
27
|
vai?: boolean;
|
|
23
28
|
vam?: Mode;
|
|
@@ -55,6 +60,9 @@ declare function track(name: string, properties?: Record<string, AllowedProperty
|
|
|
55
60
|
* }
|
|
56
61
|
* ```
|
|
57
62
|
*/
|
|
58
|
-
declare function Analytics(
|
|
63
|
+
declare function Analytics(props: AnalyticsProps & {
|
|
64
|
+
framework?: string;
|
|
65
|
+
path?: string;
|
|
66
|
+
}): null;
|
|
59
67
|
|
|
60
68
|
export { Analytics, AnalyticsProps, track };
|
package/dist/react/index.d.ts
CHANGED
|
@@ -14,10 +14,15 @@ interface AnalyticsProps {
|
|
|
14
14
|
beforeSend?: BeforeSend;
|
|
15
15
|
debug?: boolean;
|
|
16
16
|
mode?: Mode;
|
|
17
|
+
route?: string | null;
|
|
18
|
+
disableAutoTrack?: boolean;
|
|
19
|
+
scriptSrc?: string;
|
|
20
|
+
endpoint?: string;
|
|
21
|
+
dsn?: string;
|
|
17
22
|
}
|
|
18
23
|
declare global {
|
|
19
24
|
interface Window {
|
|
20
|
-
va?: (event: 'beforeSend' | 'event', properties?: unknown) => void;
|
|
25
|
+
va?: (event: 'beforeSend' | 'event' | 'pageview', properties?: unknown) => void;
|
|
21
26
|
vaq?: [string, unknown?][];
|
|
22
27
|
vai?: boolean;
|
|
23
28
|
vam?: Mode;
|
|
@@ -55,6 +60,9 @@ declare function track(name: string, properties?: Record<string, AllowedProperty
|
|
|
55
60
|
* }
|
|
56
61
|
* ```
|
|
57
62
|
*/
|
|
58
|
-
declare function Analytics(
|
|
63
|
+
declare function Analytics(props: AnalyticsProps & {
|
|
64
|
+
framework?: string;
|
|
65
|
+
path?: string;
|
|
66
|
+
}): null;
|
|
59
67
|
|
|
60
68
|
export { Analytics, AnalyticsProps, track };
|
package/dist/react/index.js
CHANGED
|
@@ -1,11 +1,35 @@
|
|
|
1
1
|
"use client";
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name2 in all)
|
|
9
|
+
__defProp(target, name2, { get: all[name2], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
2
20
|
|
|
3
21
|
// src/react.tsx
|
|
4
|
-
|
|
22
|
+
var react_exports = {};
|
|
23
|
+
__export(react_exports, {
|
|
24
|
+
Analytics: () => Analytics,
|
|
25
|
+
track: () => track
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(react_exports);
|
|
28
|
+
var import_react = require("react");
|
|
5
29
|
|
|
6
30
|
// package.json
|
|
7
31
|
var name = "@vercel/analytics";
|
|
8
|
-
var version = "1.
|
|
32
|
+
var version = "1.2.0-beta.2";
|
|
9
33
|
|
|
10
34
|
// src/queue.ts
|
|
11
35
|
var initQueue = () => {
|
|
@@ -75,6 +99,8 @@ function parseProperties(properties, options) {
|
|
|
75
99
|
}
|
|
76
100
|
|
|
77
101
|
// src/generic.ts
|
|
102
|
+
var DEV_SCRIPT_URL = "https://va.vercel-scripts.com/v1/script.debug.js";
|
|
103
|
+
var PROD_SCRIPT_URL = "/_vercel/insights/script.js";
|
|
78
104
|
function inject(props = {
|
|
79
105
|
debug: true
|
|
80
106
|
}) {
|
|
@@ -86,14 +112,23 @@ function inject(props = {
|
|
|
86
112
|
if (props.beforeSend) {
|
|
87
113
|
(_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
|
|
88
114
|
}
|
|
89
|
-
const src = isDevelopment() ?
|
|
115
|
+
const src = props.scriptSrc || (isDevelopment() ? DEV_SCRIPT_URL : PROD_SCRIPT_URL);
|
|
90
116
|
if (document.head.querySelector(`script[src*="${src}"]`))
|
|
91
117
|
return;
|
|
92
118
|
const script = document.createElement("script");
|
|
93
119
|
script.src = src;
|
|
94
120
|
script.defer = true;
|
|
95
|
-
script.
|
|
96
|
-
script.
|
|
121
|
+
script.dataset.sdkn = name + (props.framework ? `/${props.framework}` : "");
|
|
122
|
+
script.dataset.sdkv = version;
|
|
123
|
+
if (props.disableAutoTrack) {
|
|
124
|
+
script.dataset.disableAutoTrack = "1";
|
|
125
|
+
}
|
|
126
|
+
if (props.endpoint) {
|
|
127
|
+
script.dataset.endpoint = props.endpoint;
|
|
128
|
+
}
|
|
129
|
+
if (props.dsn) {
|
|
130
|
+
script.dataset.dsn = props.dsn;
|
|
131
|
+
}
|
|
97
132
|
script.onerror = () => {
|
|
98
133
|
const errorMessage = isDevelopment() ? "Please check if any ad blockers are enabled and try again." : "Be sure to enable Web Analytics for your project and deploy again. See https://vercel.com/docs/analytics/quickstart for more information.";
|
|
99
134
|
console.log(
|
|
@@ -101,7 +136,7 @@ function inject(props = {
|
|
|
101
136
|
);
|
|
102
137
|
};
|
|
103
138
|
if (isDevelopment() && props.debug === false) {
|
|
104
|
-
script.
|
|
139
|
+
script.dataset.debug = "false";
|
|
105
140
|
}
|
|
106
141
|
document.head.appendChild(script);
|
|
107
142
|
}
|
|
@@ -134,20 +169,34 @@ function track(name2, properties) {
|
|
|
134
169
|
}
|
|
135
170
|
}
|
|
136
171
|
}
|
|
172
|
+
function pageview({ route }) {
|
|
173
|
+
var _a;
|
|
174
|
+
(_a = window.va) == null ? void 0 : _a.call(window, "pageview", {
|
|
175
|
+
route
|
|
176
|
+
});
|
|
177
|
+
}
|
|
137
178
|
|
|
138
179
|
// src/react.tsx
|
|
139
|
-
function Analytics({
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
}, [
|
|
180
|
+
function Analytics(props) {
|
|
181
|
+
(0, import_react.useEffect)(() => {
|
|
182
|
+
inject({
|
|
183
|
+
framework: props.framework || "react",
|
|
184
|
+
...props.route && { disableAutoTrack: true },
|
|
185
|
+
...props
|
|
186
|
+
});
|
|
187
|
+
}, [props]);
|
|
188
|
+
(0, import_react.useEffect)(() => {
|
|
189
|
+
if (props.route) {
|
|
190
|
+
pageview({
|
|
191
|
+
route: props.route
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
}, [props.route, props.path]);
|
|
147
195
|
return null;
|
|
148
196
|
}
|
|
149
|
-
export
|
|
197
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
198
|
+
0 && (module.exports = {
|
|
150
199
|
Analytics,
|
|
151
200
|
track
|
|
152
|
-
};
|
|
201
|
+
});
|
|
153
202
|
//# sourceMappingURL=index.js.map
|