@vercel/analytics 0.1.11 → 1.0.1-beta.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 +6 -6
- package/dist/index.cjs +83 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +32 -5
- 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 +46 -4
- package/dist/react/index.js +82 -14
- package/dist/react/index.js.map +1 -1
- package/package.json +9 -2
package/README.md
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
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">
|
|
7
7
|
<a href="https://vercel.com/analytics">Website</a>
|
|
8
8
|
<span> · </span>
|
|
9
|
-
<a href="https://vercel.com/docs/concepts/analytics/
|
|
9
|
+
<a href="https://vercel.com/docs/concepts/analytics/package">Documentation</a>
|
|
10
10
|
<span> · </span>
|
|
11
11
|
<a href="https://twitter.com/vercel">Twitter</a>
|
|
12
12
|
</div>
|
|
13
13
|
|
|
14
14
|
## Overview
|
|
15
15
|
|
|
16
|
-
`@vercel/analytics` allows you to track page views in your Next.js app or any other website that is deployed to Vercel.
|
|
16
|
+
`@vercel/analytics` allows you to track page views and custom events in your Next.js app or any other website that is deployed to Vercel.
|
|
17
17
|
|
|
18
18
|
All page views are automatically tracked in your app.
|
|
19
19
|
|
|
@@ -21,16 +21,16 @@ 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/
|
|
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/quickstart).
|
|
31
31
|
|
|
32
32
|
4. Deploy your app to Vercel and see data flowing in.
|
|
33
33
|
|
|
34
34
|
## Documentation
|
|
35
35
|
|
|
36
|
-
Find more details about this package in our [documentation](https://vercel.com/docs/concepts/analytics/
|
|
36
|
+
Find more details about this package in our [documentation](https://vercel.com/docs/concepts/analytics/quickstart).
|
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.1.
|
|
31
|
+
var version = "1.0.1-beta.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\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 */\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\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 */\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;AAEO,SAAS,gBAAyB;AACvC,SAAO,QAAQ,MAAM;AACvB;AAEA,SAAS,UACP,KACA,GAAG,MAAM,MAAM,KAAK,GACK;AAC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C5B;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
|
@@ -2,9 +2,14 @@ interface PageViewEvent {
|
|
|
2
2
|
type: 'pageview';
|
|
3
3
|
url: string;
|
|
4
4
|
}
|
|
5
|
-
|
|
5
|
+
interface CustomEvent {
|
|
6
|
+
type: 'event';
|
|
7
|
+
url: string;
|
|
8
|
+
}
|
|
9
|
+
declare type BeforeSendEvent = PageViewEvent | CustomEvent;
|
|
6
10
|
declare type Mode = 'auto' | 'development' | 'production';
|
|
7
|
-
declare type
|
|
11
|
+
declare type AllowedPropertyValues = string | number | boolean | null;
|
|
12
|
+
declare type BeforeSend = (event: BeforeSendEvent) => BeforeSendEvent | null;
|
|
8
13
|
interface AnalyticsProps {
|
|
9
14
|
beforeSend?: BeforeSend;
|
|
10
15
|
debug?: boolean;
|
|
@@ -12,12 +17,34 @@ interface AnalyticsProps {
|
|
|
12
17
|
}
|
|
13
18
|
declare global {
|
|
14
19
|
interface Window {
|
|
15
|
-
va?: (event:
|
|
20
|
+
va?: (event: 'beforeSend' | 'event', properties?: unknown) => void;
|
|
16
21
|
vaq?: [string, unknown?][];
|
|
17
22
|
vai?: boolean;
|
|
23
|
+
vam?: Mode;
|
|
18
24
|
}
|
|
19
25
|
}
|
|
20
26
|
|
|
21
|
-
|
|
27
|
+
/**
|
|
28
|
+
* 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).
|
|
29
|
+
* @param [props] - Analytics options.
|
|
30
|
+
* @param [props.mode] - The mode to use for the analytics script. Defaults to `auto`.
|
|
31
|
+
* - `auto` - Automatically detect the environment. Uses `production` if the environment cannot be determined.
|
|
32
|
+
* - `production` - Always use the production script. (Sends events to the server)
|
|
33
|
+
* - `development` - Always use the development script. (Logs events to the console)
|
|
34
|
+
* @param [props.debug] - Whether to enable debug logging in development. Defaults to `true`.
|
|
35
|
+
* @param [props.beforeSend] - A middleware function to modify events before they are sent. Should return the event object or `null` to cancel the event.
|
|
36
|
+
*/
|
|
37
|
+
declare function inject(props?: AnalyticsProps): void;
|
|
38
|
+
/**
|
|
39
|
+
* Tracks a custom event. Please refer to the [documentation](https://vercel.com/docs/concepts/analytics/custom-events) for more information on custom events.
|
|
40
|
+
* @param name - The name of the event.
|
|
41
|
+
* * Examples: `Purchase`, `Click Button`, or `Play Video`.
|
|
42
|
+
* @param [properties] - Additional properties of the event. Nested objects are not supported. Allowed values are `string`, `number`, `boolean`, and `null`.
|
|
43
|
+
*/
|
|
44
|
+
declare function track(name: string, properties?: Record<string, AllowedPropertyValues>): void;
|
|
45
|
+
declare const _default: {
|
|
46
|
+
inject: typeof inject;
|
|
47
|
+
track: typeof track;
|
|
48
|
+
};
|
|
22
49
|
|
|
23
|
-
export { inject };
|
|
50
|
+
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.1.
|
|
3
|
+
var version = "1.0.1-beta.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\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 */\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\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 */\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;AA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AEA,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/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.1.
|
|
33
|
+
var version = "1.0.1-beta.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\n/**\n * Injects the Vercel Web Analytics script into the page head and starts tracking page views. Read more in our [documentation](https://vercel.com/docs/concepts/analytics/package).\n * @param [props] - Analytics options.\n * @param [props.mode] - The mode to use for the analytics script. Defaults to `auto`.\n * - `auto` - Automatically detect the environment. Uses `production` if the environment cannot be determined.\n * - `production` - Always use the production script. (Sends events to the server)\n * - `development` - Always use the development script. (Logs events to the console)\n * @param [props.debug] - Whether to enable debug logging in development. Defaults to `true`.\n * @param [props.beforeSend] - A middleware function to modify events before they are sent. Should return the event object or `null` to cancel the event.\n * @example\n * ```js\n * import { Analytics } from '@vercel/analytics/react';\n *\n * export default function App() {\n * return (\n * <div>\n * <Analytics />\n * <h1>My App</h1>\n * </div>\n * );\n * }\n * ```\n */\nfunction Analytics({\n 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\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 */\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\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 */\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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,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AAO;AACT;AAKA,IAAO,gBAAQ;AAAA,EACb;AAAA,EACA;AACF;","names":["name"]}
|
package/dist/react/index.d.ts
CHANGED
|
@@ -2,9 +2,14 @@ interface PageViewEvent {
|
|
|
2
2
|
type: 'pageview';
|
|
3
3
|
url: string;
|
|
4
4
|
}
|
|
5
|
-
|
|
5
|
+
interface CustomEvent {
|
|
6
|
+
type: 'event';
|
|
7
|
+
url: string;
|
|
8
|
+
}
|
|
9
|
+
declare type BeforeSendEvent = PageViewEvent | CustomEvent;
|
|
6
10
|
declare type Mode = 'auto' | 'development' | 'production';
|
|
7
|
-
declare type
|
|
11
|
+
declare type AllowedPropertyValues = string | number | boolean | null;
|
|
12
|
+
declare type BeforeSend = (event: BeforeSendEvent) => BeforeSendEvent | null;
|
|
8
13
|
interface AnalyticsProps {
|
|
9
14
|
beforeSend?: BeforeSend;
|
|
10
15
|
debug?: boolean;
|
|
@@ -12,12 +17,49 @@ interface AnalyticsProps {
|
|
|
12
17
|
}
|
|
13
18
|
declare global {
|
|
14
19
|
interface Window {
|
|
15
|
-
va?: (event:
|
|
20
|
+
va?: (event: 'beforeSend' | 'event', properties?: unknown) => void;
|
|
16
21
|
vaq?: [string, unknown?][];
|
|
17
22
|
vai?: boolean;
|
|
23
|
+
vam?: Mode;
|
|
18
24
|
}
|
|
19
25
|
}
|
|
20
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Tracks a custom event. Please refer to the [documentation](https://vercel.com/docs/concepts/analytics/custom-events) for more information on custom events.
|
|
29
|
+
* @param name - The name of the event.
|
|
30
|
+
* * Examples: `Purchase`, `Click Button`, or `Play Video`.
|
|
31
|
+
* @param [properties] - Additional properties of the event. Nested objects are not supported. Allowed values are `string`, `number`, `boolean`, and `null`.
|
|
32
|
+
*/
|
|
33
|
+
declare function track(name: string, properties?: Record<string, AllowedPropertyValues>): void;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 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).
|
|
37
|
+
* @param [props] - Analytics options.
|
|
38
|
+
* @param [props.mode] - The mode to use for the analytics script. Defaults to `auto`.
|
|
39
|
+
* - `auto` - Automatically detect the environment. Uses `production` if the environment cannot be determined.
|
|
40
|
+
* - `production` - Always use the production script. (Sends events to the server)
|
|
41
|
+
* - `development` - Always use the development script. (Logs events to the console)
|
|
42
|
+
* @param [props.debug] - Whether to enable debug logging in development. Defaults to `true`.
|
|
43
|
+
* @param [props.beforeSend] - A middleware function to modify events before they are sent. Should return the event object or `null` to cancel the event.
|
|
44
|
+
* @example
|
|
45
|
+
* ```js
|
|
46
|
+
* import { Analytics } from '@vercel/analytics/react';
|
|
47
|
+
*
|
|
48
|
+
* export default function App() {
|
|
49
|
+
* return (
|
|
50
|
+
* <div>
|
|
51
|
+
* <Analytics />
|
|
52
|
+
* <h1>My App</h1>
|
|
53
|
+
* </div>
|
|
54
|
+
* );
|
|
55
|
+
* }
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
21
58
|
declare function Analytics({ beforeSend, debug, mode, }: AnalyticsProps): null;
|
|
22
59
|
|
|
23
|
-
|
|
60
|
+
declare const _default: {
|
|
61
|
+
Analytics: typeof Analytics;
|
|
62
|
+
track: typeof track;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
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.1.
|
|
8
|
+
var version = "1.0.1-beta.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\n/**\n * Injects the Vercel Web Analytics script into the page head and starts tracking page views. Read more in our [documentation](https://vercel.com/docs/concepts/analytics/package).\n * @param [props] - Analytics options.\n * @param [props.mode] - The mode to use for the analytics script. Defaults to `auto`.\n * - `auto` - Automatically detect the environment. Uses `production` if the environment cannot be determined.\n * - `production` - Always use the production script. (Sends events to the server)\n * - `development` - Always use the development script. (Logs events to the console)\n * @param [props.debug] - Whether to enable debug logging in development. Defaults to `true`.\n * @param [props.beforeSend] - A middleware function to modify events before they are sent. Should return the event object or `null` to cancel the event.\n * @example\n * ```js\n * import { Analytics } from '@vercel/analytics/react';\n *\n * export default function App() {\n * return (\n * <div>\n * <Analytics />\n * <h1>My App</h1>\n * </div>\n * );\n * }\n * ```\n */\nfunction Analytics({\n 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\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 */\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\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 */\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;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,OAAO,QAAQ,UAAU,GAAG;AACrD,QAAI,OAAO,UAAU,YAAY,UAAU,MAAM;AAC/C,UAAI,QAAQ,OAAO;AACjB,gBAAQ,UAAU,KAAK,KAAK;AAAA,MAC9B,OAAO;AACL,wBAAgB,KAAK,GAAG;AAAA,MAC1B;AAAA,IACF;AAAA,EACF;AA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,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,10 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vercel/analytics",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "1.0.1-beta.0",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"analytics",
|
|
6
6
|
"vercel"
|
|
7
7
|
],
|
|
8
|
+
"repository": {
|
|
9
|
+
"url": "github:vercel/analytics",
|
|
10
|
+
"directory": "packages/web"
|
|
11
|
+
},
|
|
8
12
|
"license": "MPL-2.0",
|
|
9
13
|
"type": "module",
|
|
10
14
|
"exports": {
|
|
@@ -36,6 +40,9 @@
|
|
|
36
40
|
"extends": [
|
|
37
41
|
"@vercel/eslint-config"
|
|
38
42
|
],
|
|
43
|
+
"rules": {
|
|
44
|
+
"tsdoc/syntax": "off"
|
|
45
|
+
},
|
|
39
46
|
"ignorePatterns": [
|
|
40
47
|
"jest.setup.ts"
|
|
41
48
|
]
|
|
@@ -54,7 +61,7 @@
|
|
|
54
61
|
"jest-environment-jsdom": "^29.3.1",
|
|
55
62
|
"react": "^18.2.0",
|
|
56
63
|
"react-dom": "^18.2.0",
|
|
57
|
-
"tsup": "
|
|
64
|
+
"tsup": "6.5.0"
|
|
58
65
|
},
|
|
59
66
|
"peerDependencies": {
|
|
60
67
|
"react": "^16.8||^17||^18"
|