@vercel/analytics 1.3.2 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/astro/component.ts +9 -0
- package/dist/astro/index.astro +40 -0
- package/dist/index.d.mts +10 -5
- package/dist/index.d.ts +10 -5
- package/dist/index.js +44 -7
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +43 -7
- package/dist/index.mjs.map +1 -1
- package/dist/next/index.d.mts +4 -4
- package/dist/next/index.d.ts +4 -4
- package/dist/next/index.js +18 -20
- package/dist/next/index.js.map +1 -1
- package/dist/next/index.mjs +18 -20
- package/dist/next/index.mjs.map +1 -1
- package/dist/nuxt/index.d.mts +33 -0
- package/dist/nuxt/index.d.ts +33 -0
- package/dist/nuxt/index.js +193 -0
- package/dist/nuxt/index.js.map +1 -0
- package/dist/nuxt/index.mjs +166 -0
- package/dist/nuxt/index.mjs.map +1 -0
- package/dist/react/index.d.mts +4 -3
- package/dist/react/index.d.ts +4 -3
- package/dist/react/index.js +14 -11
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +13 -10
- package/dist/react/index.mjs.map +1 -1
- package/dist/remix/index.d.mts +33 -0
- package/dist/remix/index.d.ts +33 -0
- package/dist/remix/index.js +206 -0
- package/dist/remix/index.js.map +1 -0
- package/dist/remix/index.mjs +171 -0
- package/dist/remix/index.mjs.map +1 -0
- package/dist/server/index.d.mts +12 -0
- package/dist/server/index.d.ts +12 -0
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.mjs.map +1 -1
- package/dist/sveltekit/index.d.mts +46 -0
- package/dist/sveltekit/index.d.ts +46 -0
- package/dist/sveltekit/index.js +201 -0
- package/dist/sveltekit/index.js.map +1 -0
- package/dist/sveltekit/index.mjs +173 -0
- package/dist/sveltekit/index.mjs.map +1 -0
- package/dist/vue/index.d.mts +33 -0
- package/dist/vue/index.d.ts +33 -0
- package/dist/vue/index.js +193 -0
- package/dist/vue/index.js.map +1 -0
- package/dist/vue/index.mjs +166 -0
- package/dist/vue/index.mjs.map +1 -0
- package/jest.setup.ts +6 -3
- package/package.json +79 -26
- package/tsconfig.json +1 -1
- package/tsup.config.js +31 -5
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
interface PageViewEvent {
|
|
2
|
+
type: 'pageview';
|
|
3
|
+
url: string;
|
|
4
|
+
}
|
|
5
|
+
interface CustomEvent {
|
|
6
|
+
type: 'event';
|
|
7
|
+
url: string;
|
|
8
|
+
}
|
|
9
|
+
type BeforeSendEvent = PageViewEvent | CustomEvent;
|
|
10
|
+
type Mode = 'auto' | 'development' | 'production';
|
|
11
|
+
type AllowedPropertyValues = string | number | boolean | null;
|
|
12
|
+
type BeforeSend = (event: BeforeSendEvent) => BeforeSendEvent | null;
|
|
13
|
+
interface AnalyticsProps {
|
|
14
|
+
beforeSend?: BeforeSend;
|
|
15
|
+
debug?: boolean;
|
|
16
|
+
mode?: Mode;
|
|
17
|
+
scriptSrc?: string;
|
|
18
|
+
endpoint?: string;
|
|
19
|
+
dsn?: string;
|
|
20
|
+
}
|
|
21
|
+
declare global {
|
|
22
|
+
interface Window {
|
|
23
|
+
va?: (event: 'beforeSend' | 'event' | 'pageview', properties?: unknown) => void;
|
|
24
|
+
vaq?: [string, unknown?][];
|
|
25
|
+
vai?: boolean;
|
|
26
|
+
vam?: Mode;
|
|
27
|
+
/** used by Astro component only */
|
|
28
|
+
webAnalyticsBeforeSend?: BeforeSend;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
type PlainFlags = Record<string, unknown>;
|
|
32
|
+
type FlagsDataInput = (string | PlainFlags)[] | PlainFlags;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Tracks a custom event. Please refer to the [documentation](https://vercel.com/docs/concepts/analytics/custom-events) for more information on custom events.
|
|
36
|
+
* @param name - The name of the event.
|
|
37
|
+
* * Examples: `Purchase`, `Click Button`, or `Play Video`.
|
|
38
|
+
* @param [properties] - Additional properties of the event. Nested objects are not supported. Allowed values are `string`, `number`, `boolean`, and `null`.
|
|
39
|
+
*/
|
|
40
|
+
declare function track(name: string, properties?: Record<string, AllowedPropertyValues>, options?: {
|
|
41
|
+
flags?: FlagsDataInput;
|
|
42
|
+
}): void;
|
|
43
|
+
|
|
44
|
+
declare function injectAnalytics(props?: Omit<AnalyticsProps, 'framework'>): void;
|
|
45
|
+
|
|
46
|
+
export { AnalyticsProps, BeforeSend, BeforeSendEvent, injectAnalytics, track };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
interface PageViewEvent {
|
|
2
|
+
type: 'pageview';
|
|
3
|
+
url: string;
|
|
4
|
+
}
|
|
5
|
+
interface CustomEvent {
|
|
6
|
+
type: 'event';
|
|
7
|
+
url: string;
|
|
8
|
+
}
|
|
9
|
+
type BeforeSendEvent = PageViewEvent | CustomEvent;
|
|
10
|
+
type Mode = 'auto' | 'development' | 'production';
|
|
11
|
+
type AllowedPropertyValues = string | number | boolean | null;
|
|
12
|
+
type BeforeSend = (event: BeforeSendEvent) => BeforeSendEvent | null;
|
|
13
|
+
interface AnalyticsProps {
|
|
14
|
+
beforeSend?: BeforeSend;
|
|
15
|
+
debug?: boolean;
|
|
16
|
+
mode?: Mode;
|
|
17
|
+
scriptSrc?: string;
|
|
18
|
+
endpoint?: string;
|
|
19
|
+
dsn?: string;
|
|
20
|
+
}
|
|
21
|
+
declare global {
|
|
22
|
+
interface Window {
|
|
23
|
+
va?: (event: 'beforeSend' | 'event' | 'pageview', properties?: unknown) => void;
|
|
24
|
+
vaq?: [string, unknown?][];
|
|
25
|
+
vai?: boolean;
|
|
26
|
+
vam?: Mode;
|
|
27
|
+
/** used by Astro component only */
|
|
28
|
+
webAnalyticsBeforeSend?: BeforeSend;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
type PlainFlags = Record<string, unknown>;
|
|
32
|
+
type FlagsDataInput = (string | PlainFlags)[] | PlainFlags;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Tracks a custom event. Please refer to the [documentation](https://vercel.com/docs/concepts/analytics/custom-events) for more information on custom events.
|
|
36
|
+
* @param name - The name of the event.
|
|
37
|
+
* * Examples: `Purchase`, `Click Button`, or `Play Video`.
|
|
38
|
+
* @param [properties] - Additional properties of the event. Nested objects are not supported. Allowed values are `string`, `number`, `boolean`, and `null`.
|
|
39
|
+
*/
|
|
40
|
+
declare function track(name: string, properties?: Record<string, AllowedPropertyValues>, options?: {
|
|
41
|
+
flags?: FlagsDataInput;
|
|
42
|
+
}): void;
|
|
43
|
+
|
|
44
|
+
declare function injectAnalytics(props?: Omit<AnalyticsProps, 'framework'>): void;
|
|
45
|
+
|
|
46
|
+
export { AnalyticsProps, BeforeSend, BeforeSendEvent, injectAnalytics, track };
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name2 in all)
|
|
8
|
+
__defProp(target, name2, { get: all[name2], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/sveltekit/index.ts
|
|
21
|
+
var sveltekit_exports = {};
|
|
22
|
+
__export(sveltekit_exports, {
|
|
23
|
+
injectAnalytics: () => injectAnalytics,
|
|
24
|
+
track: () => track
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(sveltekit_exports);
|
|
27
|
+
|
|
28
|
+
// package.json
|
|
29
|
+
var name = "@vercel/analytics";
|
|
30
|
+
var version = "1.4.0";
|
|
31
|
+
|
|
32
|
+
// src/queue.ts
|
|
33
|
+
var initQueue = () => {
|
|
34
|
+
if (window.va)
|
|
35
|
+
return;
|
|
36
|
+
window.va = function a(...params) {
|
|
37
|
+
(window.vaq = window.vaq || []).push(params);
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// src/utils.ts
|
|
42
|
+
function isBrowser() {
|
|
43
|
+
return typeof window !== "undefined";
|
|
44
|
+
}
|
|
45
|
+
function detectEnvironment() {
|
|
46
|
+
try {
|
|
47
|
+
const env = process.env.NODE_ENV;
|
|
48
|
+
if (env === "development" || env === "test") {
|
|
49
|
+
return "development";
|
|
50
|
+
}
|
|
51
|
+
} catch (e) {
|
|
52
|
+
}
|
|
53
|
+
return "production";
|
|
54
|
+
}
|
|
55
|
+
function setMode(mode = "auto") {
|
|
56
|
+
if (mode === "auto") {
|
|
57
|
+
window.vam = detectEnvironment();
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
window.vam = mode;
|
|
61
|
+
}
|
|
62
|
+
function getMode() {
|
|
63
|
+
const mode = isBrowser() ? window.vam : detectEnvironment();
|
|
64
|
+
return mode || "production";
|
|
65
|
+
}
|
|
66
|
+
function isProduction() {
|
|
67
|
+
return getMode() === "production";
|
|
68
|
+
}
|
|
69
|
+
function isDevelopment() {
|
|
70
|
+
return getMode() === "development";
|
|
71
|
+
}
|
|
72
|
+
function removeKey(key, { [key]: _, ...rest }) {
|
|
73
|
+
return rest;
|
|
74
|
+
}
|
|
75
|
+
function parseProperties(properties, options) {
|
|
76
|
+
if (!properties)
|
|
77
|
+
return void 0;
|
|
78
|
+
let props = properties;
|
|
79
|
+
const errorProperties = [];
|
|
80
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
81
|
+
if (typeof value === "object" && value !== null) {
|
|
82
|
+
if (options.strip) {
|
|
83
|
+
props = removeKey(key, props);
|
|
84
|
+
} else {
|
|
85
|
+
errorProperties.push(key);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (errorProperties.length > 0 && !options.strip) {
|
|
90
|
+
throw Error(
|
|
91
|
+
`The following properties are not valid: ${errorProperties.join(
|
|
92
|
+
", "
|
|
93
|
+
)}. Only strings, numbers, booleans, and null are allowed.`
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
return props;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// src/generic.ts
|
|
100
|
+
var DEV_SCRIPT_URL = "https://va.vercel-scripts.com/v1/script.debug.js";
|
|
101
|
+
var PROD_SCRIPT_URL = "/_vercel/insights/script.js";
|
|
102
|
+
function inject(props = {
|
|
103
|
+
debug: true
|
|
104
|
+
}) {
|
|
105
|
+
var _a;
|
|
106
|
+
if (!isBrowser())
|
|
107
|
+
return;
|
|
108
|
+
setMode(props.mode);
|
|
109
|
+
initQueue();
|
|
110
|
+
if (props.beforeSend) {
|
|
111
|
+
(_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
|
|
112
|
+
}
|
|
113
|
+
const src = props.scriptSrc || (isDevelopment() ? DEV_SCRIPT_URL : PROD_SCRIPT_URL);
|
|
114
|
+
if (document.head.querySelector(`script[src*="${src}"]`))
|
|
115
|
+
return;
|
|
116
|
+
const script = document.createElement("script");
|
|
117
|
+
script.src = src;
|
|
118
|
+
script.defer = true;
|
|
119
|
+
script.dataset.sdkn = name + (props.framework ? `/${props.framework}` : "");
|
|
120
|
+
script.dataset.sdkv = version;
|
|
121
|
+
if (props.disableAutoTrack) {
|
|
122
|
+
script.dataset.disableAutoTrack = "1";
|
|
123
|
+
}
|
|
124
|
+
if (props.endpoint) {
|
|
125
|
+
script.dataset.endpoint = props.endpoint;
|
|
126
|
+
}
|
|
127
|
+
if (props.dsn) {
|
|
128
|
+
script.dataset.dsn = props.dsn;
|
|
129
|
+
}
|
|
130
|
+
script.onerror = () => {
|
|
131
|
+
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.";
|
|
132
|
+
console.log(
|
|
133
|
+
`[Vercel Web Analytics] Failed to load script from ${src}. ${errorMessage}`
|
|
134
|
+
);
|
|
135
|
+
};
|
|
136
|
+
if (isDevelopment() && props.debug === false) {
|
|
137
|
+
script.dataset.debug = "false";
|
|
138
|
+
}
|
|
139
|
+
document.head.appendChild(script);
|
|
140
|
+
}
|
|
141
|
+
function track(name2, properties, options) {
|
|
142
|
+
var _a, _b;
|
|
143
|
+
if (!isBrowser()) {
|
|
144
|
+
const msg = "[Vercel Web Analytics] Please import `track` from `@vercel/analytics/server` when using this function in a server environment";
|
|
145
|
+
if (isProduction()) {
|
|
146
|
+
console.warn(msg);
|
|
147
|
+
} else {
|
|
148
|
+
throw new Error(msg);
|
|
149
|
+
}
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (!properties) {
|
|
153
|
+
(_a = window.va) == null ? void 0 : _a.call(window, "event", { name: name2, options });
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
try {
|
|
157
|
+
const props = parseProperties(properties, {
|
|
158
|
+
strip: isProduction()
|
|
159
|
+
});
|
|
160
|
+
(_b = window.va) == null ? void 0 : _b.call(window, "event", {
|
|
161
|
+
name: name2,
|
|
162
|
+
data: props,
|
|
163
|
+
options
|
|
164
|
+
});
|
|
165
|
+
} catch (err) {
|
|
166
|
+
if (err instanceof Error && isDevelopment()) {
|
|
167
|
+
console.error(err);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
function pageview({
|
|
172
|
+
route,
|
|
173
|
+
path
|
|
174
|
+
}) {
|
|
175
|
+
var _a;
|
|
176
|
+
(_a = window.va) == null ? void 0 : _a.call(window, "pageview", { route, path });
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// src/sveltekit/index.ts
|
|
180
|
+
var import_stores = require("$app/stores");
|
|
181
|
+
var import_environment = require("$app/environment");
|
|
182
|
+
function injectAnalytics(props = {}) {
|
|
183
|
+
if (import_environment.browser) {
|
|
184
|
+
inject({
|
|
185
|
+
...props,
|
|
186
|
+
disableAutoTrack: true,
|
|
187
|
+
framework: "sveltekit"
|
|
188
|
+
});
|
|
189
|
+
import_stores.page.subscribe(({ route, url }) => {
|
|
190
|
+
if (route == null ? void 0 : route.id) {
|
|
191
|
+
pageview({ route: route.id, path: url.pathname });
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
197
|
+
0 && (module.exports = {
|
|
198
|
+
injectAnalytics,
|
|
199
|
+
track
|
|
200
|
+
});
|
|
201
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/sveltekit/index.ts","../../package.json","../../src/queue.ts","../../src/utils.ts","../../src/generic.ts"],"sourcesContent":["import { inject, pageview, track } from '../generic';\nimport type { AnalyticsProps, BeforeSend, BeforeSendEvent } from '../types';\nimport { page } from '$app/stores';\nimport { browser } from '$app/environment';\nimport type {} from '@sveltejs/kit';\n\nfunction injectAnalytics(props: Omit<AnalyticsProps, 'framework'> = {}): void {\n if (browser) {\n inject({\n ...props,\n disableAutoTrack: true,\n framework: 'sveltekit',\n });\n\n page.subscribe(({ route, url }) => {\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- route could be undefined in layout.js file\n if (route?.id) {\n pageview({ route: route.id, path: url.pathname });\n }\n });\n }\n}\n\nexport { injectAnalytics, track };\nexport type { AnalyticsProps, BeforeSend, BeforeSendEvent };\n","{\n \"name\": \"@vercel/analytics\",\n \"version\": \"1.4.0\",\n \"description\": \"Gain real-time traffic insights with Vercel Web Analytics\",\n \"keywords\": [\n \"analytics\",\n \"vercel\"\n ],\n \"repository\": {\n \"url\": \"github:vercel/analytics\",\n \"directory\": \"packages/web\"\n },\n \"license\": \"MPL-2.0\",\n \"exports\": {\n \"./package.json\": \"./package.json\",\n \".\": {\n \"browser\": \"./dist/index.mjs\",\n \"import\": \"./dist/index.mjs\",\n \"require\": \"./dist/index.js\"\n },\n \"./astro\": {\n \"import\": \"./dist/astro/component.ts\"\n },\n \"./next\": {\n \"browser\": \"./dist/next/index.mjs\",\n \"import\": \"./dist/next/index.mjs\",\n \"require\": \"./dist/next/index.js\"\n },\n \"./nuxt\": {\n \"browser\": \"./dist/nuxt/index.mjs\",\n \"import\": \"./dist/nuxt/index.mjs\",\n \"require\": \"./dist/nuxt/index.js\"\n },\n \"./react\": {\n \"browser\": \"./dist/react/index.mjs\",\n \"import\": \"./dist/react/index.mjs\",\n \"require\": \"./dist/react/index.js\"\n },\n \"./remix\": {\n \"browser\": \"./dist/remix/index.mjs\",\n \"import\": \"./dist/remix/index.mjs\",\n \"require\": \"./dist/remix/index.js\"\n },\n \"./server\": {\n \"node\": \"./dist/server/index.js\",\n \"edge-light\": \"./dist/server/index.mjs\",\n \"import\": \"./dist/server/index.mjs\",\n \"require\": \"./dist/server/index.js\",\n \"default\": \"./dist/server/index.js\"\n },\n \"./sveltekit\": {\n \"svelte\": \"./dist/sveltekit/index.mjs\",\n \"types\": \"./dist/sveltekit/index.d.ts\"\n },\n \"./vue\": {\n \"browser\": \"./dist/vue/index.mjs\",\n \"import\": \"./dist/vue/index.mjs\",\n \"require\": \"./dist/vue/index.js\"\n }\n },\n \"main\": \"./dist/index.mjs\",\n \"types\": \"./dist/index.d.ts\",\n \"typesVersions\": {\n \"*\": {\n \"*\": [\n \"dist/index.d.ts\"\n ],\n \"next\": [\n \"dist/next/index.d.ts\"\n ],\n \"nuxt\": [\n \"dist/nuxt/index.d.ts\"\n ],\n \"react\": [\n \"dist/react/index.d.ts\"\n ],\n \"remix\": [\n \"dist/remix/index.d.ts\"\n ],\n \"server\": [\n \"dist/server/index.d.ts\"\n ],\n \"sveltekit\": [\n \"dist/sveltekit/index.d.ts\"\n ],\n \"vue\": [\n \"dist/vue/index.d.ts\"\n ]\n }\n },\n \"scripts\": {\n \"build\": \"tsup && pnpm copy-astro\",\n \"copy-astro\": \"cp -R src/astro dist/\",\n \"dev\": \"pnpm copy-astro && tsup --watch\",\n \"lint\": \"eslint .\",\n \"lint-fix\": \"eslint . --fix\",\n \"test\": \"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 \"devDependencies\": {\n \"@jest/globals\": \"^29.7.0\",\n \"@swc/core\": \"^1.8.0\",\n \"@swc/jest\": \"^0.2.37\",\n \"@testing-library/jest-dom\": \"^6.6.3\",\n \"@testing-library/react\": \"^16.0.1\",\n \"@types/node\": \"^20.17.6\",\n \"@types/react\": \"^18.3.12\",\n \"@vercel/eslint-config\": \"workspace:0.0.0\",\n \"jest\": \"^29.7.0\",\n \"jest-environment-jsdom\": \"^29.7.0\",\n \"server-only\": \"^0.0.1\",\n \"svelte\": \"^5.1.10\",\n \"tsup\": \"7.1.0\",\n \"vue\": \"^3.5.12\",\n \"vue-router\": \"^4.4.5\"\n },\n \"peerDependencies\": {\n \"@remix-run/react\": \"^2\",\n \"@sveltejs/kit\": \"^1 || ^2\",\n \"next\": \">= 13\",\n \"react\": \"^18 || ^19 || ^19.0.0-rc\",\n \"svelte\": \">= 4\",\n \"vue\": \"^3\",\n \"vue-router\": \"^4\"\n },\n \"peerDependenciesMeta\": {\n \"@remix-run/react\": {\n \"optional\": true\n },\n \"@sveltejs/kit\": {\n \"optional\": true\n },\n \"next\": {\n \"optional\": true\n },\n \"react\": {\n \"optional\": true\n },\n \"svelte\": {\n \"optional\": true\n },\n \"vue\": {\n \"optional\": true\n },\n \"vue-router\": {\n \"optional\": true\n }\n }\n}\n","export const initQueue = (): void => {\n // initialize va until script is loaded\n if (window.va) return;\n\n window.va = function a(...params): void {\n (window.vaq = window.vaq || []).push(params);\n };\n};\n","import type { AllowedPropertyValues, Mode } from './types';\n\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nfunction detectEnvironment(): 'development' | 'production' {\n try {\n const env = process.env.NODE_ENV;\n if (env === 'development' || env === 'test') {\n return 'development';\n }\n } catch (e) {\n // do nothing, this is okay\n }\n return 'production';\n}\n\nexport function setMode(mode: Mode = 'auto'): void {\n if (mode === 'auto') {\n window.vam = detectEnvironment();\n return;\n }\n\n window.vam = mode;\n}\n\nexport function getMode(): Mode {\n const mode = isBrowser() ? window.vam : detectEnvironment();\n return mode || 'production';\n}\n\nexport function isProduction(): boolean {\n return getMode() === 'production';\n}\n\nexport function isDevelopment(): boolean {\n return getMode() === 'development';\n}\n\nfunction removeKey(\n key: string,\n { [key]: _, ...rest }\n): Record<string, unknown> {\n return rest;\n}\n\nexport function parseProperties(\n properties: Record<string, unknown> | undefined,\n options: {\n strip?: boolean;\n }\n): Error | Record<string, AllowedPropertyValues> | undefined {\n if (!properties) return undefined;\n let props = properties;\n const errorProperties: string[] = [];\n for (const [key, value] of Object.entries(properties)) {\n if (typeof value === 'object' && value !== null) {\n if (options.strip) {\n props = removeKey(key, props);\n } else {\n errorProperties.push(key);\n }\n }\n }\n\n if (errorProperties.length > 0 && !options.strip) {\n throw Error(\n `The following properties are not valid: ${errorProperties.join(\n ', '\n )}. Only strings, numbers, booleans, and null are allowed.`\n );\n }\n return props as Record<string, AllowedPropertyValues>;\n}\n\nexport function computeRoute(\n pathname: string | null,\n pathParams: Record<string, string | string[]> | null\n): string | null {\n if (!pathname || !pathParams) {\n return pathname;\n }\n\n let result = pathname;\n try {\n const entries = Object.entries(pathParams);\n // simple keys must be handled first\n for (const [key, value] of entries) {\n if (!Array.isArray(value)) {\n const matcher = turnValueToRegExp(value);\n if (matcher.test(result)) {\n result = result.replace(matcher, `/[${key}]`);\n }\n }\n }\n // array values next\n for (const [key, value] of entries) {\n if (Array.isArray(value)) {\n const matcher = turnValueToRegExp(value.join('/'));\n if (matcher.test(result)) {\n result = result.replace(matcher, `/[...${key}]`);\n }\n }\n }\n return result;\n } catch (e) {\n return pathname;\n }\n}\n\nfunction turnValueToRegExp(value: string): RegExp {\n return new RegExp(`/${escapeRegExp(value)}(?=[/?#]|$)`);\n}\n\nfunction escapeRegExp(string: string): string {\n return string.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n}\n","import { name as packageName, version } from '../package.json';\nimport { initQueue } from './queue';\nimport type {\n AllowedPropertyValues,\n AnalyticsProps,\n FlagsDataInput,\n BeforeSend,\n BeforeSendEvent,\n} from './types';\nimport {\n isBrowser,\n parseProperties,\n setMode,\n isDevelopment,\n isProduction,\n computeRoute,\n} 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 * @param [props.disableAutoTrack] - Whether the injected script should track page views from pushState events. Disable if route is updated after pushState, a manually call page pageview().\n */\nfunction inject(\n props: AnalyticsProps & {\n framework?: string;\n disableAutoTrack?: boolean;\n } = {\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 options?: {\n flags?: FlagsDataInput;\n }\n): void {\n if (!isBrowser()) {\n const msg =\n '[Vercel Web Analytics] Please import `track` from `@vercel/analytics/server` when using this function in a server environment';\n\n if (isProduction()) {\n // eslint-disable-next-line no-console -- Show warning in production\n console.warn(msg);\n } else {\n throw new Error(msg);\n }\n\n return;\n }\n\n if (!properties) {\n window.va?.('event', { name, options });\n return;\n }\n\n try {\n const props = parseProperties(properties, {\n strip: isProduction(),\n });\n\n window.va?.('event', {\n name,\n data: props,\n options,\n });\n } catch (err) {\n if (err instanceof Error && isDevelopment()) {\n // eslint-disable-next-line no-console -- Logging to console is intentional\n console.error(err);\n }\n }\n}\n\nfunction pageview({\n route,\n path,\n}: {\n route?: string | null;\n path?: string;\n}): void {\n window.va?.('pageview', { route, path });\n}\n\nexport { inject, track, pageview, computeRoute };\nexport type { AnalyticsProps, BeforeSend, BeforeSendEvent };\n\n// eslint-disable-next-line import/no-default-export -- Default export is intentional\nexport default {\n inject,\n track,\n computeRoute,\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCE,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;AAC3C,aAAO;AAAA,IACT;AAAA,EACF,SAAS,GAAG;AAAA,EAEZ;AACA,SAAO;AACT;AAEO,SAAS,QAAQ,OAAa,QAAc;AACjD,MAAI,SAAS,QAAQ;AACnB,WAAO,MAAM,kBAAkB;AAC/B;AAAA,EACF;AAEA,SAAO,MAAM;AACf;AAEO,SAAS,UAAgB;AAC9B,QAAM,OAAO,UAAU,IAAI,OAAO,MAAM,kBAAkB;AAC1D,SAAO,QAAQ;AACjB;AAEO,SAAS,eAAwB;AACtC,SAAO,QAAQ,MAAM;AACvB;AAEO,SAAS,gBAAyB;AACvC,SAAO,QAAQ,MAAM;AACvB;AAEA,SAAS,UACP,KACA,EAAE,CAAC,GAAG,GAAG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AAEjC,UAAI,+BAAO,IAAI;AACb,iBAAS,EAAE,OAAO,MAAM,IAAI,MAAM,IAAI,SAAS,CAAC;AAAA,MAClD;AAAA,IACF,CAAC;AAAA,EACH;AACF;","names":["name"]}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// package.json
|
|
2
|
+
var name = "@vercel/analytics";
|
|
3
|
+
var version = "1.4.0";
|
|
4
|
+
|
|
5
|
+
// src/queue.ts
|
|
6
|
+
var initQueue = () => {
|
|
7
|
+
if (window.va)
|
|
8
|
+
return;
|
|
9
|
+
window.va = function a(...params) {
|
|
10
|
+
(window.vaq = window.vaq || []).push(params);
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// src/utils.ts
|
|
15
|
+
function isBrowser() {
|
|
16
|
+
return typeof window !== "undefined";
|
|
17
|
+
}
|
|
18
|
+
function detectEnvironment() {
|
|
19
|
+
try {
|
|
20
|
+
const env = process.env.NODE_ENV;
|
|
21
|
+
if (env === "development" || env === "test") {
|
|
22
|
+
return "development";
|
|
23
|
+
}
|
|
24
|
+
} catch (e) {
|
|
25
|
+
}
|
|
26
|
+
return "production";
|
|
27
|
+
}
|
|
28
|
+
function setMode(mode = "auto") {
|
|
29
|
+
if (mode === "auto") {
|
|
30
|
+
window.vam = detectEnvironment();
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
window.vam = mode;
|
|
34
|
+
}
|
|
35
|
+
function getMode() {
|
|
36
|
+
const mode = isBrowser() ? window.vam : detectEnvironment();
|
|
37
|
+
return mode || "production";
|
|
38
|
+
}
|
|
39
|
+
function isProduction() {
|
|
40
|
+
return getMode() === "production";
|
|
41
|
+
}
|
|
42
|
+
function isDevelopment() {
|
|
43
|
+
return getMode() === "development";
|
|
44
|
+
}
|
|
45
|
+
function removeKey(key, { [key]: _, ...rest }) {
|
|
46
|
+
return rest;
|
|
47
|
+
}
|
|
48
|
+
function parseProperties(properties, options) {
|
|
49
|
+
if (!properties)
|
|
50
|
+
return void 0;
|
|
51
|
+
let props = properties;
|
|
52
|
+
const errorProperties = [];
|
|
53
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
54
|
+
if (typeof value === "object" && value !== null) {
|
|
55
|
+
if (options.strip) {
|
|
56
|
+
props = removeKey(key, props);
|
|
57
|
+
} else {
|
|
58
|
+
errorProperties.push(key);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (errorProperties.length > 0 && !options.strip) {
|
|
63
|
+
throw Error(
|
|
64
|
+
`The following properties are not valid: ${errorProperties.join(
|
|
65
|
+
", "
|
|
66
|
+
)}. Only strings, numbers, booleans, and null are allowed.`
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
return props;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// src/generic.ts
|
|
73
|
+
var DEV_SCRIPT_URL = "https://va.vercel-scripts.com/v1/script.debug.js";
|
|
74
|
+
var PROD_SCRIPT_URL = "/_vercel/insights/script.js";
|
|
75
|
+
function inject(props = {
|
|
76
|
+
debug: true
|
|
77
|
+
}) {
|
|
78
|
+
var _a;
|
|
79
|
+
if (!isBrowser())
|
|
80
|
+
return;
|
|
81
|
+
setMode(props.mode);
|
|
82
|
+
initQueue();
|
|
83
|
+
if (props.beforeSend) {
|
|
84
|
+
(_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
|
|
85
|
+
}
|
|
86
|
+
const src = props.scriptSrc || (isDevelopment() ? DEV_SCRIPT_URL : PROD_SCRIPT_URL);
|
|
87
|
+
if (document.head.querySelector(`script[src*="${src}"]`))
|
|
88
|
+
return;
|
|
89
|
+
const script = document.createElement("script");
|
|
90
|
+
script.src = src;
|
|
91
|
+
script.defer = true;
|
|
92
|
+
script.dataset.sdkn = name + (props.framework ? `/${props.framework}` : "");
|
|
93
|
+
script.dataset.sdkv = version;
|
|
94
|
+
if (props.disableAutoTrack) {
|
|
95
|
+
script.dataset.disableAutoTrack = "1";
|
|
96
|
+
}
|
|
97
|
+
if (props.endpoint) {
|
|
98
|
+
script.dataset.endpoint = props.endpoint;
|
|
99
|
+
}
|
|
100
|
+
if (props.dsn) {
|
|
101
|
+
script.dataset.dsn = props.dsn;
|
|
102
|
+
}
|
|
103
|
+
script.onerror = () => {
|
|
104
|
+
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.";
|
|
105
|
+
console.log(
|
|
106
|
+
`[Vercel Web Analytics] Failed to load script from ${src}. ${errorMessage}`
|
|
107
|
+
);
|
|
108
|
+
};
|
|
109
|
+
if (isDevelopment() && props.debug === false) {
|
|
110
|
+
script.dataset.debug = "false";
|
|
111
|
+
}
|
|
112
|
+
document.head.appendChild(script);
|
|
113
|
+
}
|
|
114
|
+
function track(name2, properties, options) {
|
|
115
|
+
var _a, _b;
|
|
116
|
+
if (!isBrowser()) {
|
|
117
|
+
const msg = "[Vercel Web Analytics] Please import `track` from `@vercel/analytics/server` when using this function in a server environment";
|
|
118
|
+
if (isProduction()) {
|
|
119
|
+
console.warn(msg);
|
|
120
|
+
} else {
|
|
121
|
+
throw new Error(msg);
|
|
122
|
+
}
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (!properties) {
|
|
126
|
+
(_a = window.va) == null ? void 0 : _a.call(window, "event", { name: name2, options });
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
try {
|
|
130
|
+
const props = parseProperties(properties, {
|
|
131
|
+
strip: isProduction()
|
|
132
|
+
});
|
|
133
|
+
(_b = window.va) == null ? void 0 : _b.call(window, "event", {
|
|
134
|
+
name: name2,
|
|
135
|
+
data: props,
|
|
136
|
+
options
|
|
137
|
+
});
|
|
138
|
+
} catch (err) {
|
|
139
|
+
if (err instanceof Error && isDevelopment()) {
|
|
140
|
+
console.error(err);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function pageview({
|
|
145
|
+
route,
|
|
146
|
+
path
|
|
147
|
+
}) {
|
|
148
|
+
var _a;
|
|
149
|
+
(_a = window.va) == null ? void 0 : _a.call(window, "pageview", { route, path });
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// src/sveltekit/index.ts
|
|
153
|
+
import { page } from "$app/stores";
|
|
154
|
+
import { browser } from "$app/environment";
|
|
155
|
+
function injectAnalytics(props = {}) {
|
|
156
|
+
if (browser) {
|
|
157
|
+
inject({
|
|
158
|
+
...props,
|
|
159
|
+
disableAutoTrack: true,
|
|
160
|
+
framework: "sveltekit"
|
|
161
|
+
});
|
|
162
|
+
page.subscribe(({ route, url }) => {
|
|
163
|
+
if (route == null ? void 0 : route.id) {
|
|
164
|
+
pageview({ route: route.id, path: url.pathname });
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
export {
|
|
170
|
+
injectAnalytics,
|
|
171
|
+
track
|
|
172
|
+
};
|
|
173
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../package.json","../../src/queue.ts","../../src/utils.ts","../../src/generic.ts","../../src/sveltekit/index.ts"],"sourcesContent":["{\n \"name\": \"@vercel/analytics\",\n \"version\": \"1.4.0\",\n \"description\": \"Gain real-time traffic insights with Vercel Web Analytics\",\n \"keywords\": [\n \"analytics\",\n \"vercel\"\n ],\n \"repository\": {\n \"url\": \"github:vercel/analytics\",\n \"directory\": \"packages/web\"\n },\n \"license\": \"MPL-2.0\",\n \"exports\": {\n \"./package.json\": \"./package.json\",\n \".\": {\n \"browser\": \"./dist/index.mjs\",\n \"import\": \"./dist/index.mjs\",\n \"require\": \"./dist/index.js\"\n },\n \"./astro\": {\n \"import\": \"./dist/astro/component.ts\"\n },\n \"./next\": {\n \"browser\": \"./dist/next/index.mjs\",\n \"import\": \"./dist/next/index.mjs\",\n \"require\": \"./dist/next/index.js\"\n },\n \"./nuxt\": {\n \"browser\": \"./dist/nuxt/index.mjs\",\n \"import\": \"./dist/nuxt/index.mjs\",\n \"require\": \"./dist/nuxt/index.js\"\n },\n \"./react\": {\n \"browser\": \"./dist/react/index.mjs\",\n \"import\": \"./dist/react/index.mjs\",\n \"require\": \"./dist/react/index.js\"\n },\n \"./remix\": {\n \"browser\": \"./dist/remix/index.mjs\",\n \"import\": \"./dist/remix/index.mjs\",\n \"require\": \"./dist/remix/index.js\"\n },\n \"./server\": {\n \"node\": \"./dist/server/index.js\",\n \"edge-light\": \"./dist/server/index.mjs\",\n \"import\": \"./dist/server/index.mjs\",\n \"require\": \"./dist/server/index.js\",\n \"default\": \"./dist/server/index.js\"\n },\n \"./sveltekit\": {\n \"svelte\": \"./dist/sveltekit/index.mjs\",\n \"types\": \"./dist/sveltekit/index.d.ts\"\n },\n \"./vue\": {\n \"browser\": \"./dist/vue/index.mjs\",\n \"import\": \"./dist/vue/index.mjs\",\n \"require\": \"./dist/vue/index.js\"\n }\n },\n \"main\": \"./dist/index.mjs\",\n \"types\": \"./dist/index.d.ts\",\n \"typesVersions\": {\n \"*\": {\n \"*\": [\n \"dist/index.d.ts\"\n ],\n \"next\": [\n \"dist/next/index.d.ts\"\n ],\n \"nuxt\": [\n \"dist/nuxt/index.d.ts\"\n ],\n \"react\": [\n \"dist/react/index.d.ts\"\n ],\n \"remix\": [\n \"dist/remix/index.d.ts\"\n ],\n \"server\": [\n \"dist/server/index.d.ts\"\n ],\n \"sveltekit\": [\n \"dist/sveltekit/index.d.ts\"\n ],\n \"vue\": [\n \"dist/vue/index.d.ts\"\n ]\n }\n },\n \"scripts\": {\n \"build\": \"tsup && pnpm copy-astro\",\n \"copy-astro\": \"cp -R src/astro dist/\",\n \"dev\": \"pnpm copy-astro && tsup --watch\",\n \"lint\": \"eslint .\",\n \"lint-fix\": \"eslint . --fix\",\n \"test\": \"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 \"devDependencies\": {\n \"@jest/globals\": \"^29.7.0\",\n \"@swc/core\": \"^1.8.0\",\n \"@swc/jest\": \"^0.2.37\",\n \"@testing-library/jest-dom\": \"^6.6.3\",\n \"@testing-library/react\": \"^16.0.1\",\n \"@types/node\": \"^20.17.6\",\n \"@types/react\": \"^18.3.12\",\n \"@vercel/eslint-config\": \"workspace:0.0.0\",\n \"jest\": \"^29.7.0\",\n \"jest-environment-jsdom\": \"^29.7.0\",\n \"server-only\": \"^0.0.1\",\n \"svelte\": \"^5.1.10\",\n \"tsup\": \"7.1.0\",\n \"vue\": \"^3.5.12\",\n \"vue-router\": \"^4.4.5\"\n },\n \"peerDependencies\": {\n \"@remix-run/react\": \"^2\",\n \"@sveltejs/kit\": \"^1 || ^2\",\n \"next\": \">= 13\",\n \"react\": \"^18 || ^19 || ^19.0.0-rc\",\n \"svelte\": \">= 4\",\n \"vue\": \"^3\",\n \"vue-router\": \"^4\"\n },\n \"peerDependenciesMeta\": {\n \"@remix-run/react\": {\n \"optional\": true\n },\n \"@sveltejs/kit\": {\n \"optional\": true\n },\n \"next\": {\n \"optional\": true\n },\n \"react\": {\n \"optional\": true\n },\n \"svelte\": {\n \"optional\": true\n },\n \"vue\": {\n \"optional\": true\n },\n \"vue-router\": {\n \"optional\": true\n }\n }\n}\n","export const initQueue = (): void => {\n // initialize va until script is loaded\n if (window.va) return;\n\n window.va = function a(...params): void {\n (window.vaq = window.vaq || []).push(params);\n };\n};\n","import type { AllowedPropertyValues, Mode } from './types';\n\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nfunction detectEnvironment(): 'development' | 'production' {\n try {\n const env = process.env.NODE_ENV;\n if (env === 'development' || env === 'test') {\n return 'development';\n }\n } catch (e) {\n // do nothing, this is okay\n }\n return 'production';\n}\n\nexport function setMode(mode: Mode = 'auto'): void {\n if (mode === 'auto') {\n window.vam = detectEnvironment();\n return;\n }\n\n window.vam = mode;\n}\n\nexport function getMode(): Mode {\n const mode = isBrowser() ? window.vam : detectEnvironment();\n return mode || 'production';\n}\n\nexport function isProduction(): boolean {\n return getMode() === 'production';\n}\n\nexport function isDevelopment(): boolean {\n return getMode() === 'development';\n}\n\nfunction removeKey(\n key: string,\n { [key]: _, ...rest }\n): Record<string, unknown> {\n return rest;\n}\n\nexport function parseProperties(\n properties: Record<string, unknown> | undefined,\n options: {\n strip?: boolean;\n }\n): Error | Record<string, AllowedPropertyValues> | undefined {\n if (!properties) return undefined;\n let props = properties;\n const errorProperties: string[] = [];\n for (const [key, value] of Object.entries(properties)) {\n if (typeof value === 'object' && value !== null) {\n if (options.strip) {\n props = removeKey(key, props);\n } else {\n errorProperties.push(key);\n }\n }\n }\n\n if (errorProperties.length > 0 && !options.strip) {\n throw Error(\n `The following properties are not valid: ${errorProperties.join(\n ', '\n )}. Only strings, numbers, booleans, and null are allowed.`\n );\n }\n return props as Record<string, AllowedPropertyValues>;\n}\n\nexport function computeRoute(\n pathname: string | null,\n pathParams: Record<string, string | string[]> | null\n): string | null {\n if (!pathname || !pathParams) {\n return pathname;\n }\n\n let result = pathname;\n try {\n const entries = Object.entries(pathParams);\n // simple keys must be handled first\n for (const [key, value] of entries) {\n if (!Array.isArray(value)) {\n const matcher = turnValueToRegExp(value);\n if (matcher.test(result)) {\n result = result.replace(matcher, `/[${key}]`);\n }\n }\n }\n // array values next\n for (const [key, value] of entries) {\n if (Array.isArray(value)) {\n const matcher = turnValueToRegExp(value.join('/'));\n if (matcher.test(result)) {\n result = result.replace(matcher, `/[...${key}]`);\n }\n }\n }\n return result;\n } catch (e) {\n return pathname;\n }\n}\n\nfunction turnValueToRegExp(value: string): RegExp {\n return new RegExp(`/${escapeRegExp(value)}(?=[/?#]|$)`);\n}\n\nfunction escapeRegExp(string: string): string {\n return string.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n}\n","import { name as packageName, version } from '../package.json';\nimport { initQueue } from './queue';\nimport type {\n AllowedPropertyValues,\n AnalyticsProps,\n FlagsDataInput,\n BeforeSend,\n BeforeSendEvent,\n} from './types';\nimport {\n isBrowser,\n parseProperties,\n setMode,\n isDevelopment,\n isProduction,\n computeRoute,\n} 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 * @param [props.disableAutoTrack] - Whether the injected script should track page views from pushState events. Disable if route is updated after pushState, a manually call page pageview().\n */\nfunction inject(\n props: AnalyticsProps & {\n framework?: string;\n disableAutoTrack?: boolean;\n } = {\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 options?: {\n flags?: FlagsDataInput;\n }\n): void {\n if (!isBrowser()) {\n const msg =\n '[Vercel Web Analytics] Please import `track` from `@vercel/analytics/server` when using this function in a server environment';\n\n if (isProduction()) {\n // eslint-disable-next-line no-console -- Show warning in production\n console.warn(msg);\n } else {\n throw new Error(msg);\n }\n\n return;\n }\n\n if (!properties) {\n window.va?.('event', { name, options });\n return;\n }\n\n try {\n const props = parseProperties(properties, {\n strip: isProduction(),\n });\n\n window.va?.('event', {\n name,\n data: props,\n options,\n });\n } catch (err) {\n if (err instanceof Error && isDevelopment()) {\n // eslint-disable-next-line no-console -- Logging to console is intentional\n console.error(err);\n }\n }\n}\n\nfunction pageview({\n route,\n path,\n}: {\n route?: string | null;\n path?: string;\n}): void {\n window.va?.('pageview', { route, path });\n}\n\nexport { inject, track, pageview, computeRoute };\nexport type { AnalyticsProps, BeforeSend, BeforeSendEvent };\n\n// eslint-disable-next-line import/no-default-export -- Default export is intentional\nexport default {\n inject,\n track,\n computeRoute,\n};\n","import { inject, pageview, track } from '../generic';\nimport type { AnalyticsProps, BeforeSend, BeforeSendEvent } from '../types';\nimport { page } from '$app/stores';\nimport { browser } from '$app/environment';\nimport type {} from '@sveltejs/kit';\n\nfunction injectAnalytics(props: Omit<AnalyticsProps, 'framework'> = {}): void {\n if (browser) {\n inject({\n ...props,\n disableAutoTrack: true,\n framework: 'sveltekit',\n });\n\n page.subscribe(({ route, url }) => {\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- route could be undefined in layout.js file\n if (route?.id) {\n pageview({ route: route.id, path: url.pathname });\n }\n });\n }\n}\n\nexport { injectAnalytics, track };\nexport type { AnalyticsProps, BeforeSend, BeforeSendEvent };\n"],"mappings":";AACE,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;AAC3C,aAAO;AAAA,IACT;AAAA,EACF,SAAS,GAAG;AAAA,EAEZ;AACA,SAAO;AACT;AAEO,SAAS,QAAQ,OAAa,QAAc;AACjD,MAAI,SAAS,QAAQ;AACnB,WAAO,MAAM,kBAAkB;AAC/B;AAAA,EACF;AAEA,SAAO,MAAM;AACf;AAEO,SAAS,UAAgB;AAC9B,QAAM,OAAO,UAAU,IAAI,OAAO,MAAM,kBAAkB;AAC1D,SAAO,QAAQ;AACjB;AAEO,SAAS,eAAwB;AACtC,SAAO,QAAQ,MAAM;AACvB;AAEO,SAAS,gBAAyB;AACvC,SAAO,QAAQ,MAAM;AACvB;AAEA,SAAS,UACP,KACA,EAAE,CAAC,GAAG,GAAG,GAAG,GAAG,KAAK,GACK;AACzB,SAAO;AACT;AAEO,SAAS,gBACd,YACA,SA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,MAAM,IAAI,SAAS,CAAC;AAAA,MAClD;AAAA,IACF,CAAC;AAAA,EACH;AACF;","names":["name"]}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
interface PageViewEvent {
|
|
2
|
+
type: 'pageview';
|
|
3
|
+
url: string;
|
|
4
|
+
}
|
|
5
|
+
interface CustomEvent {
|
|
6
|
+
type: 'event';
|
|
7
|
+
url: string;
|
|
8
|
+
}
|
|
9
|
+
type BeforeSendEvent = PageViewEvent | CustomEvent;
|
|
10
|
+
type Mode = 'auto' | 'development' | 'production';
|
|
11
|
+
type BeforeSend = (event: BeforeSendEvent) => BeforeSendEvent | null;
|
|
12
|
+
interface AnalyticsProps {
|
|
13
|
+
beforeSend?: BeforeSend;
|
|
14
|
+
debug?: boolean;
|
|
15
|
+
mode?: Mode;
|
|
16
|
+
scriptSrc?: string;
|
|
17
|
+
endpoint?: string;
|
|
18
|
+
dsn?: string;
|
|
19
|
+
}
|
|
20
|
+
declare global {
|
|
21
|
+
interface Window {
|
|
22
|
+
va?: (event: 'beforeSend' | 'event' | 'pageview', properties?: unknown) => void;
|
|
23
|
+
vaq?: [string, unknown?][];
|
|
24
|
+
vai?: boolean;
|
|
25
|
+
vam?: Mode;
|
|
26
|
+
/** used by Astro component only */
|
|
27
|
+
webAnalyticsBeforeSend?: BeforeSend;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
declare const Analytics: any;
|
|
32
|
+
|
|
33
|
+
export { Analytics, AnalyticsProps, BeforeSend, BeforeSendEvent };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
interface PageViewEvent {
|
|
2
|
+
type: 'pageview';
|
|
3
|
+
url: string;
|
|
4
|
+
}
|
|
5
|
+
interface CustomEvent {
|
|
6
|
+
type: 'event';
|
|
7
|
+
url: string;
|
|
8
|
+
}
|
|
9
|
+
type BeforeSendEvent = PageViewEvent | CustomEvent;
|
|
10
|
+
type Mode = 'auto' | 'development' | 'production';
|
|
11
|
+
type BeforeSend = (event: BeforeSendEvent) => BeforeSendEvent | null;
|
|
12
|
+
interface AnalyticsProps {
|
|
13
|
+
beforeSend?: BeforeSend;
|
|
14
|
+
debug?: boolean;
|
|
15
|
+
mode?: Mode;
|
|
16
|
+
scriptSrc?: string;
|
|
17
|
+
endpoint?: string;
|
|
18
|
+
dsn?: string;
|
|
19
|
+
}
|
|
20
|
+
declare global {
|
|
21
|
+
interface Window {
|
|
22
|
+
va?: (event: 'beforeSend' | 'event' | 'pageview', properties?: unknown) => void;
|
|
23
|
+
vaq?: [string, unknown?][];
|
|
24
|
+
vai?: boolean;
|
|
25
|
+
vam?: Mode;
|
|
26
|
+
/** used by Astro component only */
|
|
27
|
+
webAnalyticsBeforeSend?: BeforeSend;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
declare const Analytics: any;
|
|
32
|
+
|
|
33
|
+
export { Analytics, AnalyticsProps, BeforeSend, BeforeSendEvent };
|