@vercel/analytics 0.1.11 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/index.cjs +83 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +10 -3
- package/dist/index.js +81 -13
- package/dist/index.js.map +1 -1
- package/dist/react/index.cjs +84 -15
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.d.ts +11 -2
- package/dist/react/index.js +82 -14
- package/dist/react/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|

|
|
2
2
|
|
|
3
|
-
<div align="center"><strong>Vercel Analytics
|
|
3
|
+
<div align="center"><strong>Vercel Web Analytics</strong></div>
|
|
4
4
|
<div align="center">Privacy-friendly, real-time traffic insights</div>
|
|
5
5
|
<br />
|
|
6
6
|
<div align="center">
|
|
@@ -21,13 +21,13 @@ This package does **not** track data in development mode.
|
|
|
21
21
|
|
|
22
22
|
## Quickstart
|
|
23
23
|
|
|
24
|
-
1. Enable Vercel Analytics for a project in the [Vercel Dashboard](https://vercel.com/dashboard).
|
|
24
|
+
1. Enable Vercel Web Analytics for a project in the [Vercel Dashboard](https://vercel.com/dashboard).
|
|
25
25
|
2. Add the `@vercel/analytics` package to your project
|
|
26
26
|
3. Inject the Analytics script to your app
|
|
27
27
|
|
|
28
28
|
- If you are using **Next.js** or **React**, you can use the `<Analytics />` component to inject the script into your app.
|
|
29
29
|
- For other frameworks, you can use the `inject` function add the tracking script to your app.
|
|
30
|
-
- If you want to use Vercel Analytics on a static site without npm, follow the instructions in the [documentation](https://vercel.com/docs/concepts/analytics/audiences/quickstart).
|
|
30
|
+
- If you want to use Vercel Web Analytics on a static site without npm, follow the instructions in the [documentation](https://vercel.com/docs/concepts/analytics/audiences/quickstart).
|
|
31
31
|
|
|
32
32
|
4. Deploy your app to Vercel and see data flowing in.
|
|
33
33
|
|
package/dist/index.cjs
CHANGED
|
@@ -20,13 +20,15 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/generic.ts
|
|
21
21
|
var generic_exports = {};
|
|
22
22
|
__export(generic_exports, {
|
|
23
|
-
|
|
23
|
+
default: () => generic_default,
|
|
24
|
+
inject: () => inject,
|
|
25
|
+
track: () => track
|
|
24
26
|
});
|
|
25
27
|
module.exports = __toCommonJS(generic_exports);
|
|
26
28
|
|
|
27
29
|
// package.json
|
|
28
30
|
var name = "@vercel/analytics";
|
|
29
|
-
var version = "0.
|
|
31
|
+
var version = "1.0.0";
|
|
30
32
|
|
|
31
33
|
// src/queue.ts
|
|
32
34
|
var initQueue = () => {
|
|
@@ -41,34 +43,70 @@ var initQueue = () => {
|
|
|
41
43
|
function isBrowser() {
|
|
42
44
|
return typeof window !== "undefined";
|
|
43
45
|
}
|
|
44
|
-
function
|
|
46
|
+
function detectEnvironment() {
|
|
45
47
|
try {
|
|
46
48
|
const env = process.env.NODE_ENV;
|
|
47
|
-
|
|
49
|
+
if (env === "development" || env === "test") {
|
|
50
|
+
return "development";
|
|
51
|
+
}
|
|
48
52
|
} catch (e) {
|
|
49
|
-
return false;
|
|
50
53
|
}
|
|
54
|
+
return "production";
|
|
51
55
|
}
|
|
52
|
-
function
|
|
56
|
+
function setMode(mode = "auto") {
|
|
53
57
|
if (mode === "auto") {
|
|
54
|
-
|
|
58
|
+
window.vam = detectEnvironment();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
window.vam = mode;
|
|
62
|
+
}
|
|
63
|
+
function getMode() {
|
|
64
|
+
return window.vam || "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
|
+
let props = properties;
|
|
77
|
+
const errorProperties = [];
|
|
78
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
79
|
+
if (typeof value === "object" && value !== null) {
|
|
80
|
+
if (options.strip) {
|
|
81
|
+
props = removeKey(key, props);
|
|
82
|
+
} else {
|
|
83
|
+
errorProperties.push(key);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
55
86
|
}
|
|
56
|
-
|
|
87
|
+
if (errorProperties.length > 0 && !options.strip) {
|
|
88
|
+
throw Error(
|
|
89
|
+
`The following properties are not valid: ${errorProperties.join(
|
|
90
|
+
", "
|
|
91
|
+
)}. Only strings, numbers, booleans, and null are allowed.`
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
return props;
|
|
57
95
|
}
|
|
58
96
|
|
|
59
97
|
// src/generic.ts
|
|
60
|
-
|
|
98
|
+
function inject(props = {
|
|
61
99
|
debug: true
|
|
62
|
-
})
|
|
100
|
+
}) {
|
|
63
101
|
var _a;
|
|
64
102
|
if (!isBrowser())
|
|
65
103
|
return;
|
|
66
|
-
|
|
104
|
+
setMode(props.mode);
|
|
67
105
|
initQueue();
|
|
68
106
|
if (props.beforeSend) {
|
|
69
107
|
(_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
|
|
70
108
|
}
|
|
71
|
-
const src =
|
|
109
|
+
const src = isDevelopment() ? "https://va.vercel-scripts.com/v1/script.debug.js" : "/_vercel/insights/script.js";
|
|
72
110
|
if (document.head.querySelector(`script[src*="${src}"]`))
|
|
73
111
|
return;
|
|
74
112
|
const script = document.createElement("script");
|
|
@@ -76,13 +114,44 @@ var inject = (props = {
|
|
|
76
114
|
script.defer = true;
|
|
77
115
|
script.setAttribute("data-sdkn", name);
|
|
78
116
|
script.setAttribute("data-sdkv", version);
|
|
79
|
-
if (
|
|
117
|
+
if (isDevelopment() && props.debug === false) {
|
|
80
118
|
script.setAttribute("data-debug", "false");
|
|
81
119
|
}
|
|
82
120
|
document.head.appendChild(script);
|
|
121
|
+
}
|
|
122
|
+
function track(name2, properties) {
|
|
123
|
+
var _a, _b;
|
|
124
|
+
if (!isBrowser()) {
|
|
125
|
+
console.warn(
|
|
126
|
+
"[Vercel Web Analytics] Server-side execution of `track()` is currently not supported."
|
|
127
|
+
);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if (!properties) {
|
|
131
|
+
(_a = window.va) == null ? void 0 : _a.call(window, "event", { name: name2 });
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
try {
|
|
135
|
+
const props = parseProperties(properties, {
|
|
136
|
+
strip: isProduction()
|
|
137
|
+
});
|
|
138
|
+
(_b = window.va) == null ? void 0 : _b.call(window, "event", {
|
|
139
|
+
name: name2,
|
|
140
|
+
data: props
|
|
141
|
+
});
|
|
142
|
+
} catch (err) {
|
|
143
|
+
if (err instanceof Error && isDevelopment()) {
|
|
144
|
+
console.error(err);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
var generic_default = {
|
|
149
|
+
inject,
|
|
150
|
+
track
|
|
83
151
|
};
|
|
84
152
|
// Annotate the CommonJS export names for ESM import in node:
|
|
85
153
|
0 && (module.exports = {
|
|
86
|
-
inject
|
|
154
|
+
inject,
|
|
155
|
+
track
|
|
87
156
|
});
|
|
88
157
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/generic.ts","../src/queue.ts","../src/utils.ts"],"sourcesContent":["import { name, version } from '../package.json';\nimport { initQueue } from './queue';\nimport type { AnalyticsProps } from './types';\nimport {
|
|
1
|
+
{"version":3,"sources":["../src/generic.ts","../src/queue.ts","../src/utils.ts"],"sourcesContent":["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 function inject(\n props: AnalyticsProps = {\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 = isDevelopment()\n ? 'https://va.vercel-scripts.com/v1/script.debug.js'\n : '/_vercel/insights/script.js';\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.setAttribute('data-sdkn', packageName);\n script.setAttribute('data-sdkv', version);\n\n if (isDevelopment() && props.debug === false) {\n script.setAttribute('data-debug', 'false');\n }\n\n document.head.appendChild(script);\n}\n\nexport function track(\n name: string,\n properties?: Record<string, AllowedPropertyValues>,\n): void {\n if (!isBrowser()) {\n // eslint-disable-next-line no-console\n console.warn(\n '[Vercel Web Analytics] Server-side execution of `track()` is currently not supported.',\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\n console.error(err);\n }\n }\n}\n\n// eslint-disable-next-line import/no-default-export\nexport default {\n inject,\n track,\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 return window.vam || '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>,\n options: {\n strip?: boolean;\n },\n): Error | Record<string, AllowedPropertyValues> | 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"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;;;;;ACAO,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,GAAP;AAAA,EAEF;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,SAAO,OAAO,OAAO;AACvB;AAEO,SAAS,eAAwB;AACtC,SAAO,QAAQ,MAAM;AACvB;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;AAAA,IACF;AACA;AAAA,EACF;AAEA,MAAI,CAAC,YAAY;AACf,iBAAO,OAAP,gCAAY,SAAS,EAAE,MAAAA,MAAK;AAC5B;AAAA,EACF;AAEA,MAAI;AACF,UAAM,QAAQ,gBAAgB,YAAY;AAAA,MACxC,OAAO,aAAa;AAAA,IACtB,CAAC;AAED,iBAAO,OAAP,gCAAY,SAAS;AAAA,MACnB,MAAAA;AAAA,MACA,MAAM;AAAA,IACR;AAAA,EACF,SAAS,KAAP;AACA,QAAI,eAAe,SAAS,cAAc,GAAG;AAE3C,cAAQ,MAAM,GAAG;AAAA,IACnB;AAAA,EACF;AACF;AAGA,IAAO,kBAAQ;AAAA,EACb;AAAA,EACA;AACF;","names":["name"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ interface PageViewEvent {
|
|
|
4
4
|
}
|
|
5
5
|
declare type Event = PageViewEvent;
|
|
6
6
|
declare type Mode = 'auto' | 'development' | 'production';
|
|
7
|
+
declare type AllowedPropertyValues = string | number | boolean | null;
|
|
7
8
|
declare type BeforeSend = (event: Event) => Event | null;
|
|
8
9
|
interface AnalyticsProps {
|
|
9
10
|
beforeSend?: BeforeSend;
|
|
@@ -12,12 +13,18 @@ interface AnalyticsProps {
|
|
|
12
13
|
}
|
|
13
14
|
declare global {
|
|
14
15
|
interface Window {
|
|
15
|
-
va?: (event:
|
|
16
|
+
va?: (event: 'beforeSend' | 'event', properties?: unknown) => void;
|
|
16
17
|
vaq?: [string, unknown?][];
|
|
17
18
|
vai?: boolean;
|
|
19
|
+
vam?: Mode;
|
|
18
20
|
}
|
|
19
21
|
}
|
|
20
22
|
|
|
21
|
-
declare
|
|
23
|
+
declare function inject(props?: AnalyticsProps): void;
|
|
24
|
+
declare function track(name: string, properties?: Record<string, AllowedPropertyValues>): void;
|
|
25
|
+
declare const _default: {
|
|
26
|
+
inject: typeof inject;
|
|
27
|
+
track: typeof track;
|
|
28
|
+
};
|
|
22
29
|
|
|
23
|
-
export { inject };
|
|
30
|
+
export { _default as default, inject, track };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// package.json
|
|
2
2
|
var name = "@vercel/analytics";
|
|
3
|
-
var version = "0.
|
|
3
|
+
var version = "1.0.0";
|
|
4
4
|
|
|
5
5
|
// src/queue.ts
|
|
6
6
|
var initQueue = () => {
|
|
@@ -15,34 +15,70 @@ var initQueue = () => {
|
|
|
15
15
|
function isBrowser() {
|
|
16
16
|
return typeof window !== "undefined";
|
|
17
17
|
}
|
|
18
|
-
function
|
|
18
|
+
function detectEnvironment() {
|
|
19
19
|
try {
|
|
20
20
|
const env = process.env.NODE_ENV;
|
|
21
|
-
|
|
21
|
+
if (env === "development" || env === "test") {
|
|
22
|
+
return "development";
|
|
23
|
+
}
|
|
22
24
|
} catch (e) {
|
|
23
|
-
return false;
|
|
24
25
|
}
|
|
26
|
+
return "production";
|
|
25
27
|
}
|
|
26
|
-
function
|
|
28
|
+
function setMode(mode = "auto") {
|
|
27
29
|
if (mode === "auto") {
|
|
28
|
-
|
|
30
|
+
window.vam = detectEnvironment();
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
window.vam = mode;
|
|
34
|
+
}
|
|
35
|
+
function getMode() {
|
|
36
|
+
return window.vam || "production";
|
|
37
|
+
}
|
|
38
|
+
function isProduction() {
|
|
39
|
+
return getMode() === "production";
|
|
40
|
+
}
|
|
41
|
+
function isDevelopment() {
|
|
42
|
+
return getMode() === "development";
|
|
43
|
+
}
|
|
44
|
+
function removeKey(key, { [key]: _, ...rest }) {
|
|
45
|
+
return rest;
|
|
46
|
+
}
|
|
47
|
+
function parseProperties(properties, options) {
|
|
48
|
+
let props = properties;
|
|
49
|
+
const errorProperties = [];
|
|
50
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
51
|
+
if (typeof value === "object" && value !== null) {
|
|
52
|
+
if (options.strip) {
|
|
53
|
+
props = removeKey(key, props);
|
|
54
|
+
} else {
|
|
55
|
+
errorProperties.push(key);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
29
58
|
}
|
|
30
|
-
|
|
59
|
+
if (errorProperties.length > 0 && !options.strip) {
|
|
60
|
+
throw Error(
|
|
61
|
+
`The following properties are not valid: ${errorProperties.join(
|
|
62
|
+
", "
|
|
63
|
+
)}. Only strings, numbers, booleans, and null are allowed.`
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
return props;
|
|
31
67
|
}
|
|
32
68
|
|
|
33
69
|
// src/generic.ts
|
|
34
|
-
|
|
70
|
+
function inject(props = {
|
|
35
71
|
debug: true
|
|
36
|
-
})
|
|
72
|
+
}) {
|
|
37
73
|
var _a;
|
|
38
74
|
if (!isBrowser())
|
|
39
75
|
return;
|
|
40
|
-
|
|
76
|
+
setMode(props.mode);
|
|
41
77
|
initQueue();
|
|
42
78
|
if (props.beforeSend) {
|
|
43
79
|
(_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
|
|
44
80
|
}
|
|
45
|
-
const src =
|
|
81
|
+
const src = isDevelopment() ? "https://va.vercel-scripts.com/v1/script.debug.js" : "/_vercel/insights/script.js";
|
|
46
82
|
if (document.head.querySelector(`script[src*="${src}"]`))
|
|
47
83
|
return;
|
|
48
84
|
const script = document.createElement("script");
|
|
@@ -50,12 +86,44 @@ var inject = (props = {
|
|
|
50
86
|
script.defer = true;
|
|
51
87
|
script.setAttribute("data-sdkn", name);
|
|
52
88
|
script.setAttribute("data-sdkv", version);
|
|
53
|
-
if (
|
|
89
|
+
if (isDevelopment() && props.debug === false) {
|
|
54
90
|
script.setAttribute("data-debug", "false");
|
|
55
91
|
}
|
|
56
92
|
document.head.appendChild(script);
|
|
93
|
+
}
|
|
94
|
+
function track(name2, properties) {
|
|
95
|
+
var _a, _b;
|
|
96
|
+
if (!isBrowser()) {
|
|
97
|
+
console.warn(
|
|
98
|
+
"[Vercel Web Analytics] Server-side execution of `track()` is currently not supported."
|
|
99
|
+
);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
if (!properties) {
|
|
103
|
+
(_a = window.va) == null ? void 0 : _a.call(window, "event", { name: name2 });
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
try {
|
|
107
|
+
const props = parseProperties(properties, {
|
|
108
|
+
strip: isProduction()
|
|
109
|
+
});
|
|
110
|
+
(_b = window.va) == null ? void 0 : _b.call(window, "event", {
|
|
111
|
+
name: name2,
|
|
112
|
+
data: props
|
|
113
|
+
});
|
|
114
|
+
} catch (err) {
|
|
115
|
+
if (err instanceof Error && isDevelopment()) {
|
|
116
|
+
console.error(err);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
var generic_default = {
|
|
121
|
+
inject,
|
|
122
|
+
track
|
|
57
123
|
};
|
|
58
124
|
export {
|
|
59
|
-
|
|
125
|
+
generic_default as default,
|
|
126
|
+
inject,
|
|
127
|
+
track
|
|
60
128
|
};
|
|
61
129
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/queue.ts","../src/utils.ts","../src/generic.ts"],"sourcesContent":["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 { Mode } from './types';\n\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\
|
|
1
|
+
{"version":3,"sources":["../src/queue.ts","../src/utils.ts","../src/generic.ts"],"sourcesContent":["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 return window.vam || '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>,\n options: {\n strip?: boolean;\n },\n): Error | Record<string, AllowedPropertyValues> | 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","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 function inject(\n props: AnalyticsProps = {\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 = isDevelopment()\n ? 'https://va.vercel-scripts.com/v1/script.debug.js'\n : '/_vercel/insights/script.js';\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.setAttribute('data-sdkn', packageName);\n script.setAttribute('data-sdkv', version);\n\n if (isDevelopment() && props.debug === false) {\n script.setAttribute('data-debug', 'false');\n }\n\n document.head.appendChild(script);\n}\n\nexport function track(\n name: string,\n properties?: Record<string, AllowedPropertyValues>,\n): void {\n if (!isBrowser()) {\n // eslint-disable-next-line no-console\n console.warn(\n '[Vercel Web Analytics] Server-side execution of `track()` is currently not supported.',\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\n console.error(err);\n }\n }\n}\n\n// eslint-disable-next-line import/no-default-export\nexport default {\n inject,\n track,\n};\n"],"mappings":";;;;;AAAO,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,GAAP;AAAA,EAEF;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,SAAO,OAAO,OAAO;AACvB;AAEO,SAAS,eAAwB;AACtC,SAAO,QAAQ,MAAM;AACvB;AAEO,SAAS,gBAAyB;AACvC,SAAO,QAAQ,MAAM;AACvB;AAEA,SAAS,UACP,KACA,GAAG,MAAM,MAAM,KAAK,GACK;AACzB,SAAO;AACT;AAEO,SAAS,gBACd,YACA,SAG2D;AAC3D,MAAI,QAAQ;AACZ,QAAM,kBAA4B,CAAC;AACnC,aAAW,CAAC,KAAK,KAAK,KAAK,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SAAS;AAAA,MACnB,MAAAA;AAAA,MACA,MAAM;AAAA,IACR;AAAA,EACF,SAAS,KAAP;AACA,QAAI,eAAe,SAAS,cAAc,GAAG;AAE3C,cAAQ,MAAM,GAAG;AAAA,IACnB;AAAA,EACF;AACF;AAGA,IAAO,kBAAQ;AAAA,EACb;AAAA,EACA;AACF;","names":["name"]}
|
package/dist/react/index.cjs
CHANGED
|
@@ -21,14 +21,16 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
// src/react.tsx
|
|
22
22
|
var react_exports = {};
|
|
23
23
|
__export(react_exports, {
|
|
24
|
-
Analytics: () => Analytics
|
|
24
|
+
Analytics: () => Analytics,
|
|
25
|
+
default: () => react_default,
|
|
26
|
+
track: () => track
|
|
25
27
|
});
|
|
26
28
|
module.exports = __toCommonJS(react_exports);
|
|
27
29
|
var import_react = require("react");
|
|
28
30
|
|
|
29
31
|
// package.json
|
|
30
32
|
var name = "@vercel/analytics";
|
|
31
|
-
var version = "0.
|
|
33
|
+
var version = "1.0.0";
|
|
32
34
|
|
|
33
35
|
// src/queue.ts
|
|
34
36
|
var initQueue = () => {
|
|
@@ -43,34 +45,70 @@ var initQueue = () => {
|
|
|
43
45
|
function isBrowser() {
|
|
44
46
|
return typeof window !== "undefined";
|
|
45
47
|
}
|
|
46
|
-
function
|
|
48
|
+
function detectEnvironment() {
|
|
47
49
|
try {
|
|
48
50
|
const env = process.env.NODE_ENV;
|
|
49
|
-
|
|
51
|
+
if (env === "development" || env === "test") {
|
|
52
|
+
return "development";
|
|
53
|
+
}
|
|
50
54
|
} catch (e) {
|
|
51
|
-
return false;
|
|
52
55
|
}
|
|
56
|
+
return "production";
|
|
53
57
|
}
|
|
54
|
-
function
|
|
58
|
+
function setMode(mode = "auto") {
|
|
55
59
|
if (mode === "auto") {
|
|
56
|
-
|
|
60
|
+
window.vam = detectEnvironment();
|
|
61
|
+
return;
|
|
57
62
|
}
|
|
58
|
-
|
|
63
|
+
window.vam = mode;
|
|
64
|
+
}
|
|
65
|
+
function getMode() {
|
|
66
|
+
return window.vam || "production";
|
|
67
|
+
}
|
|
68
|
+
function isProduction() {
|
|
69
|
+
return getMode() === "production";
|
|
70
|
+
}
|
|
71
|
+
function isDevelopment() {
|
|
72
|
+
return getMode() === "development";
|
|
73
|
+
}
|
|
74
|
+
function removeKey(key, { [key]: _, ...rest }) {
|
|
75
|
+
return rest;
|
|
76
|
+
}
|
|
77
|
+
function parseProperties(properties, options) {
|
|
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;
|
|
59
97
|
}
|
|
60
98
|
|
|
61
99
|
// src/generic.ts
|
|
62
|
-
|
|
100
|
+
function inject(props = {
|
|
63
101
|
debug: true
|
|
64
|
-
})
|
|
102
|
+
}) {
|
|
65
103
|
var _a;
|
|
66
104
|
if (!isBrowser())
|
|
67
105
|
return;
|
|
68
|
-
|
|
106
|
+
setMode(props.mode);
|
|
69
107
|
initQueue();
|
|
70
108
|
if (props.beforeSend) {
|
|
71
109
|
(_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
|
|
72
110
|
}
|
|
73
|
-
const src =
|
|
111
|
+
const src = isDevelopment() ? "https://va.vercel-scripts.com/v1/script.debug.js" : "/_vercel/insights/script.js";
|
|
74
112
|
if (document.head.querySelector(`script[src*="${src}"]`))
|
|
75
113
|
return;
|
|
76
114
|
const script = document.createElement("script");
|
|
@@ -78,11 +116,37 @@ var inject = (props = {
|
|
|
78
116
|
script.defer = true;
|
|
79
117
|
script.setAttribute("data-sdkn", name);
|
|
80
118
|
script.setAttribute("data-sdkv", version);
|
|
81
|
-
if (
|
|
119
|
+
if (isDevelopment() && props.debug === false) {
|
|
82
120
|
script.setAttribute("data-debug", "false");
|
|
83
121
|
}
|
|
84
122
|
document.head.appendChild(script);
|
|
85
|
-
}
|
|
123
|
+
}
|
|
124
|
+
function track(name2, properties) {
|
|
125
|
+
var _a, _b;
|
|
126
|
+
if (!isBrowser()) {
|
|
127
|
+
console.warn(
|
|
128
|
+
"[Vercel Web Analytics] Server-side execution of `track()` is currently not supported."
|
|
129
|
+
);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
if (!properties) {
|
|
133
|
+
(_a = window.va) == null ? void 0 : _a.call(window, "event", { name: name2 });
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
const props = parseProperties(properties, {
|
|
138
|
+
strip: isProduction()
|
|
139
|
+
});
|
|
140
|
+
(_b = window.va) == null ? void 0 : _b.call(window, "event", {
|
|
141
|
+
name: name2,
|
|
142
|
+
data: props
|
|
143
|
+
});
|
|
144
|
+
} catch (err) {
|
|
145
|
+
if (err instanceof Error && isDevelopment()) {
|
|
146
|
+
console.error(err);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
86
150
|
|
|
87
151
|
// src/react.tsx
|
|
88
152
|
function Analytics({
|
|
@@ -95,8 +159,13 @@ function Analytics({
|
|
|
95
159
|
}, [beforeSend, debug, mode]);
|
|
96
160
|
return null;
|
|
97
161
|
}
|
|
162
|
+
var react_default = {
|
|
163
|
+
Analytics,
|
|
164
|
+
track
|
|
165
|
+
};
|
|
98
166
|
// Annotate the CommonJS export names for ESM import in node:
|
|
99
167
|
0 && (module.exports = {
|
|
100
|
-
Analytics
|
|
168
|
+
Analytics,
|
|
169
|
+
track
|
|
101
170
|
});
|
|
102
171
|
//# sourceMappingURL=index.cjs.map
|
package/dist/react/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/react.tsx","../../src/queue.ts","../../src/utils.ts","../../src/generic.ts"],"sourcesContent":["import { useEffect } from 'react';\nimport { inject } from './generic';\nimport type { AnalyticsProps } from './types';\n\
|
|
1
|
+
{"version":3,"sources":["../../src/react.tsx","../../src/queue.ts","../../src/utils.ts","../../src/generic.ts"],"sourcesContent":["import { useEffect } from 'react';\nimport { inject, track } from './generic';\nimport type { AnalyticsProps } from './types';\n\nfunction Analytics({\n beforeSend,\n debug = true,\n mode = 'auto',\n}: AnalyticsProps): null {\n useEffect(() => {\n inject({ beforeSend, debug, mode });\n }, [beforeSend, debug, mode]);\n\n return null;\n}\nexport { track, Analytics };\nexport type { AnalyticsProps };\n\n// eslint-disable-next-line import/no-default-export\nexport default {\n Analytics,\n track,\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 return window.vam || '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>,\n options: {\n strip?: boolean;\n },\n): Error | Record<string, AllowedPropertyValues> | 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","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 function inject(\n props: AnalyticsProps = {\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 = isDevelopment()\n ? 'https://va.vercel-scripts.com/v1/script.debug.js'\n : '/_vercel/insights/script.js';\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.setAttribute('data-sdkn', packageName);\n script.setAttribute('data-sdkv', version);\n\n if (isDevelopment() && props.debug === false) {\n script.setAttribute('data-debug', 'false');\n }\n\n document.head.appendChild(script);\n}\n\nexport function track(\n name: string,\n properties?: Record<string, AllowedPropertyValues>,\n): void {\n if (!isBrowser()) {\n // eslint-disable-next-line no-console\n console.warn(\n '[Vercel Web Analytics] Server-side execution of `track()` is currently not supported.',\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\n console.error(err);\n }\n }\n}\n\n// eslint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AAyB;AACvB,8BAAU,MAAM;AACd,WAAO,EAAE,YAAY,OAAO,KAAK,CAAC;AAAA,EACpC,GAAG,CAAC,YAAY,OAAO,IAAI,CAAC;AAE5B,SAAO;AACT;AAKA,IAAO,gBAAQ;AAAA,EACb;AAAA,EACA;AACF;","names":["name"]}
|
package/dist/react/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ interface PageViewEvent {
|
|
|
4
4
|
}
|
|
5
5
|
declare type Event = PageViewEvent;
|
|
6
6
|
declare type Mode = 'auto' | 'development' | 'production';
|
|
7
|
+
declare type AllowedPropertyValues = string | number | boolean | null;
|
|
7
8
|
declare type BeforeSend = (event: Event) => Event | null;
|
|
8
9
|
interface AnalyticsProps {
|
|
9
10
|
beforeSend?: BeforeSend;
|
|
@@ -12,12 +13,20 @@ interface AnalyticsProps {
|
|
|
12
13
|
}
|
|
13
14
|
declare global {
|
|
14
15
|
interface Window {
|
|
15
|
-
va?: (event:
|
|
16
|
+
va?: (event: 'beforeSend' | 'event', properties?: unknown) => void;
|
|
16
17
|
vaq?: [string, unknown?][];
|
|
17
18
|
vai?: boolean;
|
|
19
|
+
vam?: Mode;
|
|
18
20
|
}
|
|
19
21
|
}
|
|
20
22
|
|
|
23
|
+
declare function track(name: string, properties?: Record<string, AllowedPropertyValues>): void;
|
|
24
|
+
|
|
21
25
|
declare function Analytics({ beforeSend, debug, mode, }: AnalyticsProps): null;
|
|
22
26
|
|
|
23
|
-
|
|
27
|
+
declare const _default: {
|
|
28
|
+
Analytics: typeof Analytics;
|
|
29
|
+
track: typeof track;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export { Analytics, AnalyticsProps, _default as default, track };
|
package/dist/react/index.js
CHANGED
|
@@ -5,7 +5,7 @@ import { useEffect } from "react";
|
|
|
5
5
|
|
|
6
6
|
// package.json
|
|
7
7
|
var name = "@vercel/analytics";
|
|
8
|
-
var version = "0.
|
|
8
|
+
var version = "1.0.0";
|
|
9
9
|
|
|
10
10
|
// src/queue.ts
|
|
11
11
|
var initQueue = () => {
|
|
@@ -20,34 +20,70 @@ var initQueue = () => {
|
|
|
20
20
|
function isBrowser() {
|
|
21
21
|
return typeof window !== "undefined";
|
|
22
22
|
}
|
|
23
|
-
function
|
|
23
|
+
function detectEnvironment() {
|
|
24
24
|
try {
|
|
25
25
|
const env = process.env.NODE_ENV;
|
|
26
|
-
|
|
26
|
+
if (env === "development" || env === "test") {
|
|
27
|
+
return "development";
|
|
28
|
+
}
|
|
27
29
|
} catch (e) {
|
|
28
|
-
return false;
|
|
29
30
|
}
|
|
31
|
+
return "production";
|
|
30
32
|
}
|
|
31
|
-
function
|
|
33
|
+
function setMode(mode = "auto") {
|
|
32
34
|
if (mode === "auto") {
|
|
33
|
-
|
|
35
|
+
window.vam = detectEnvironment();
|
|
36
|
+
return;
|
|
34
37
|
}
|
|
35
|
-
|
|
38
|
+
window.vam = mode;
|
|
39
|
+
}
|
|
40
|
+
function getMode() {
|
|
41
|
+
return window.vam || "production";
|
|
42
|
+
}
|
|
43
|
+
function isProduction() {
|
|
44
|
+
return getMode() === "production";
|
|
45
|
+
}
|
|
46
|
+
function isDevelopment() {
|
|
47
|
+
return getMode() === "development";
|
|
48
|
+
}
|
|
49
|
+
function removeKey(key, { [key]: _, ...rest }) {
|
|
50
|
+
return rest;
|
|
51
|
+
}
|
|
52
|
+
function parseProperties(properties, options) {
|
|
53
|
+
let props = properties;
|
|
54
|
+
const errorProperties = [];
|
|
55
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
56
|
+
if (typeof value === "object" && value !== null) {
|
|
57
|
+
if (options.strip) {
|
|
58
|
+
props = removeKey(key, props);
|
|
59
|
+
} else {
|
|
60
|
+
errorProperties.push(key);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (errorProperties.length > 0 && !options.strip) {
|
|
65
|
+
throw Error(
|
|
66
|
+
`The following properties are not valid: ${errorProperties.join(
|
|
67
|
+
", "
|
|
68
|
+
)}. Only strings, numbers, booleans, and null are allowed.`
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
return props;
|
|
36
72
|
}
|
|
37
73
|
|
|
38
74
|
// src/generic.ts
|
|
39
|
-
|
|
75
|
+
function inject(props = {
|
|
40
76
|
debug: true
|
|
41
|
-
})
|
|
77
|
+
}) {
|
|
42
78
|
var _a;
|
|
43
79
|
if (!isBrowser())
|
|
44
80
|
return;
|
|
45
|
-
|
|
81
|
+
setMode(props.mode);
|
|
46
82
|
initQueue();
|
|
47
83
|
if (props.beforeSend) {
|
|
48
84
|
(_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
|
|
49
85
|
}
|
|
50
|
-
const src =
|
|
86
|
+
const src = isDevelopment() ? "https://va.vercel-scripts.com/v1/script.debug.js" : "/_vercel/insights/script.js";
|
|
51
87
|
if (document.head.querySelector(`script[src*="${src}"]`))
|
|
52
88
|
return;
|
|
53
89
|
const script = document.createElement("script");
|
|
@@ -55,11 +91,37 @@ var inject = (props = {
|
|
|
55
91
|
script.defer = true;
|
|
56
92
|
script.setAttribute("data-sdkn", name);
|
|
57
93
|
script.setAttribute("data-sdkv", version);
|
|
58
|
-
if (
|
|
94
|
+
if (isDevelopment() && props.debug === false) {
|
|
59
95
|
script.setAttribute("data-debug", "false");
|
|
60
96
|
}
|
|
61
97
|
document.head.appendChild(script);
|
|
62
|
-
}
|
|
98
|
+
}
|
|
99
|
+
function track(name2, properties) {
|
|
100
|
+
var _a, _b;
|
|
101
|
+
if (!isBrowser()) {
|
|
102
|
+
console.warn(
|
|
103
|
+
"[Vercel Web Analytics] Server-side execution of `track()` is currently not supported."
|
|
104
|
+
);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (!properties) {
|
|
108
|
+
(_a = window.va) == null ? void 0 : _a.call(window, "event", { name: name2 });
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
const props = parseProperties(properties, {
|
|
113
|
+
strip: isProduction()
|
|
114
|
+
});
|
|
115
|
+
(_b = window.va) == null ? void 0 : _b.call(window, "event", {
|
|
116
|
+
name: name2,
|
|
117
|
+
data: props
|
|
118
|
+
});
|
|
119
|
+
} catch (err) {
|
|
120
|
+
if (err instanceof Error && isDevelopment()) {
|
|
121
|
+
console.error(err);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
63
125
|
|
|
64
126
|
// src/react.tsx
|
|
65
127
|
function Analytics({
|
|
@@ -72,7 +134,13 @@ function Analytics({
|
|
|
72
134
|
}, [beforeSend, debug, mode]);
|
|
73
135
|
return null;
|
|
74
136
|
}
|
|
137
|
+
var react_default = {
|
|
138
|
+
Analytics,
|
|
139
|
+
track
|
|
140
|
+
};
|
|
75
141
|
export {
|
|
76
|
-
Analytics
|
|
142
|
+
Analytics,
|
|
143
|
+
react_default as default,
|
|
144
|
+
track
|
|
77
145
|
};
|
|
78
146
|
//# sourceMappingURL=index.js.map
|
package/dist/react/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/react.tsx","../../src/queue.ts","../../src/utils.ts","../../src/generic.ts"],"sourcesContent":["import { useEffect } from 'react';\nimport { inject } from './generic';\nimport type { AnalyticsProps } from './types';\n\
|
|
1
|
+
{"version":3,"sources":["../../src/react.tsx","../../src/queue.ts","../../src/utils.ts","../../src/generic.ts"],"sourcesContent":["import { useEffect } from 'react';\nimport { inject, track } from './generic';\nimport type { AnalyticsProps } from './types';\n\nfunction Analytics({\n beforeSend,\n debug = true,\n mode = 'auto',\n}: AnalyticsProps): null {\n useEffect(() => {\n inject({ beforeSend, debug, mode });\n }, [beforeSend, debug, mode]);\n\n return null;\n}\nexport { track, Analytics };\nexport type { AnalyticsProps };\n\n// eslint-disable-next-line import/no-default-export\nexport default {\n Analytics,\n track,\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 return window.vam || '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>,\n options: {\n strip?: boolean;\n },\n): Error | Record<string, AllowedPropertyValues> | 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","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 function inject(\n props: AnalyticsProps = {\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 = isDevelopment()\n ? 'https://va.vercel-scripts.com/v1/script.debug.js'\n : '/_vercel/insights/script.js';\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.setAttribute('data-sdkn', packageName);\n script.setAttribute('data-sdkv', version);\n\n if (isDevelopment() && props.debug === false) {\n script.setAttribute('data-debug', 'false');\n }\n\n document.head.appendChild(script);\n}\n\nexport function track(\n name: string,\n properties?: Record<string, AllowedPropertyValues>,\n): void {\n if (!isBrowser()) {\n // eslint-disable-next-line no-console\n console.warn(\n '[Vercel Web Analytics] Server-side execution of `track()` is currently not supported.',\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\n console.error(err);\n }\n }\n}\n\n// eslint-disable-next-line import/no-default-export\nexport default {\n inject,\n track,\n};\n"],"mappings":";;;AAAA,SAAS,iBAAiB;;;;;;;ACAnB,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,GAAP;AAAA,EAEF;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,SAAO,OAAO,OAAO;AACvB;AAEO,SAAS,eAAwB;AACtC,SAAO,QAAQ,MAAM;AACvB;AAEO,SAAS,gBAAyB;A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,EACF;AAEA,MAAI,CAAC,YAAY;AACf,iBAAO,OAAP,gCAAY,SAAS,EAAE,MAAAA,MAAK;AAC5B;AAAA,EACF;AAEA,MAAI;AACF,UAAM,QAAQ,gBAAgB,YAAY;AAAA,MACxC,OAAO,aAAa;AAAA,IACtB,CAAC;AAED,iBAAO,OAAP,gCAAY,SAAS;AAAA,MACnB,MAAAA;AAAA,MACA,MAAM;AAAA,IACR;AAAA,EACF,SAAS,KAAP;AACA,QAAI,eAAe,SAAS,cAAc,GAAG;AAE3C,cAAQ,MAAM,GAAG;AAAA,IACnB;AAAA,EACF;AACF;;;AHzEA,SAAS,UAAU;AAAA,EACjB;AAAA,EACA,QAAQ;AAAA,EACR,OAAO;AACT,GAAyB;AACvB,YAAU,MAAM;AACd,WAAO,EAAE,YAAY,OAAO,KAAK,CAAC;AAAA,EACpC,GAAG,CAAC,YAAY,OAAO,IAAI,CAAC;AAE5B,SAAO;AACT;AAKA,IAAO,gBAAQ;AAAA,EACb;AAAA,EACA;AACF;","names":["name"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vercel/analytics",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"analytics",
|
|
6
6
|
"vercel"
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"jest-environment-jsdom": "^29.3.1",
|
|
55
55
|
"react": "^18.2.0",
|
|
56
56
|
"react-dom": "^18.2.0",
|
|
57
|
-
"tsup": "
|
|
57
|
+
"tsup": "6.5.0"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|
|
60
60
|
"react": "^16.8||^17||^18"
|