@vercel/analytics 1.1.2 → 1.2.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,16 +14,23 @@ interface AnalyticsProps {
14
14
  beforeSend?: BeforeSend;
15
15
  debug?: boolean;
16
16
  mode?: Mode;
17
+ route?: string | null;
18
+ disableAutoTrack?: boolean;
19
+ scriptSrc?: string;
20
+ endpoint?: string;
21
+ dsn?: string;
17
22
  }
18
23
  declare global {
19
24
  interface Window {
20
- va?: (event: 'beforeSend' | 'event', properties?: unknown) => void;
25
+ va?: (event: 'beforeSend' | 'event' | 'pageview', properties?: unknown) => void;
21
26
  vaq?: [string, unknown?][];
22
27
  vai?: boolean;
23
28
  vam?: Mode;
24
29
  }
25
30
  }
26
31
 
32
+ declare const DEV_SCRIPT_URL = "https://va.vercel-scripts.com/v1/script.debug.js";
33
+ declare const PROD_SCRIPT_URL = "/_vercel/insights/script.js";
27
34
  /**
28
35
  * 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
36
  * @param [props] - Analytics options.
@@ -33,8 +40,11 @@ declare global {
33
40
  * - `development` - Always use the development script. (Logs events to the console)
34
41
  * @param [props.debug] - Whether to enable debug logging in development. Defaults to `true`.
35
42
  * @param [props.beforeSend] - A middleware function to modify events before they are sent. Should return the event object or `null` to cancel the event.
43
+ * @param [props.dsn] - The DSN of the project to send events to. Only required when self-hosting.
36
44
  */
37
- declare function inject(props?: AnalyticsProps): void;
45
+ declare function inject(props?: AnalyticsProps & {
46
+ framework?: string;
47
+ }): void;
38
48
  /**
39
49
  * Tracks a custom event. Please refer to the [documentation](https://vercel.com/docs/concepts/analytics/custom-events) for more information on custom events.
40
50
  * @param name - The name of the event.
@@ -42,10 +52,13 @@ declare function inject(props?: AnalyticsProps): void;
42
52
  * @param [properties] - Additional properties of the event. Nested objects are not supported. Allowed values are `string`, `number`, `boolean`, and `null`.
43
53
  */
44
54
  declare function track(name: string, properties?: Record<string, AllowedPropertyValues>): void;
55
+ declare function pageview({ route }: {
56
+ route?: string;
57
+ }): void;
45
58
 
46
59
  declare const _default: {
47
60
  inject: typeof inject;
48
61
  track: typeof track;
49
62
  };
50
63
 
51
- export { AnalyticsProps, _default as default, inject, track };
64
+ export { AnalyticsProps, DEV_SCRIPT_URL, PROD_SCRIPT_URL, _default as default, inject, pageview, track };
package/dist/index.d.ts CHANGED
@@ -14,16 +14,23 @@ interface AnalyticsProps {
14
14
  beforeSend?: BeforeSend;
15
15
  debug?: boolean;
16
16
  mode?: Mode;
17
+ route?: string | null;
18
+ disableAutoTrack?: boolean;
19
+ scriptSrc?: string;
20
+ endpoint?: string;
21
+ dsn?: string;
17
22
  }
18
23
  declare global {
19
24
  interface Window {
20
- va?: (event: 'beforeSend' | 'event', properties?: unknown) => void;
25
+ va?: (event: 'beforeSend' | 'event' | 'pageview', properties?: unknown) => void;
21
26
  vaq?: [string, unknown?][];
22
27
  vai?: boolean;
23
28
  vam?: Mode;
24
29
  }
25
30
  }
26
31
 
32
+ declare const DEV_SCRIPT_URL = "https://va.vercel-scripts.com/v1/script.debug.js";
33
+ declare const PROD_SCRIPT_URL = "/_vercel/insights/script.js";
27
34
  /**
28
35
  * 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
36
  * @param [props] - Analytics options.
@@ -33,8 +40,11 @@ declare global {
33
40
  * - `development` - Always use the development script. (Logs events to the console)
34
41
  * @param [props.debug] - Whether to enable debug logging in development. Defaults to `true`.
35
42
  * @param [props.beforeSend] - A middleware function to modify events before they are sent. Should return the event object or `null` to cancel the event.
43
+ * @param [props.dsn] - The DSN of the project to send events to. Only required when self-hosting.
36
44
  */
37
- declare function inject(props?: AnalyticsProps): void;
45
+ declare function inject(props?: AnalyticsProps & {
46
+ framework?: string;
47
+ }): void;
38
48
  /**
39
49
  * Tracks a custom event. Please refer to the [documentation](https://vercel.com/docs/concepts/analytics/custom-events) for more information on custom events.
40
50
  * @param name - The name of the event.
@@ -42,10 +52,13 @@ declare function inject(props?: AnalyticsProps): void;
42
52
  * @param [properties] - Additional properties of the event. Nested objects are not supported. Allowed values are `string`, `number`, `boolean`, and `null`.
43
53
  */
44
54
  declare function track(name: string, properties?: Record<string, AllowedPropertyValues>): void;
55
+ declare function pageview({ route }: {
56
+ route?: string;
57
+ }): void;
45
58
 
46
59
  declare const _default: {
47
60
  inject: typeof inject;
48
61
  track: typeof track;
49
62
  };
50
63
 
51
- export { AnalyticsProps, _default as default, inject, track };
64
+ export { AnalyticsProps, DEV_SCRIPT_URL, PROD_SCRIPT_URL, _default as default, inject, pageview, track };
package/dist/index.js CHANGED
@@ -1,6 +1,37 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name2 in all)
8
+ __defProp(target, name2, { get: all[name2], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/generic.ts
21
+ var generic_exports = {};
22
+ __export(generic_exports, {
23
+ DEV_SCRIPT_URL: () => DEV_SCRIPT_URL,
24
+ PROD_SCRIPT_URL: () => PROD_SCRIPT_URL,
25
+ default: () => generic_default,
26
+ inject: () => inject,
27
+ pageview: () => pageview,
28
+ track: () => track
29
+ });
30
+ module.exports = __toCommonJS(generic_exports);
31
+
1
32
  // package.json
2
33
  var name = "@vercel/analytics";
3
- var version = "1.1.2";
34
+ var version = "1.2.0-beta.1";
4
35
 
5
36
  // src/queue.ts
6
37
  var initQueue = () => {
@@ -70,6 +101,8 @@ function parseProperties(properties, options) {
70
101
  }
71
102
 
72
103
  // src/generic.ts
104
+ var DEV_SCRIPT_URL = "https://va.vercel-scripts.com/v1/script.debug.js";
105
+ var PROD_SCRIPT_URL = "/_vercel/insights/script.js";
73
106
  function inject(props = {
74
107
  debug: true
75
108
  }) {
@@ -81,14 +114,23 @@ function inject(props = {
81
114
  if (props.beforeSend) {
82
115
  (_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
83
116
  }
84
- const src = isDevelopment() ? "https://va.vercel-scripts.com/v1/script.debug.js" : "/_vercel/insights/script.js";
117
+ const src = props.scriptSrc || (isDevelopment() ? DEV_SCRIPT_URL : PROD_SCRIPT_URL);
85
118
  if (document.head.querySelector(`script[src*="${src}"]`))
86
119
  return;
87
120
  const script = document.createElement("script");
88
121
  script.src = src;
89
122
  script.defer = true;
90
- script.setAttribute("data-sdkn", name);
91
- script.setAttribute("data-sdkv", version);
123
+ script.dataset.sdkn = name + (props.framework ? `/${props.framework}` : "");
124
+ script.dataset.sdkv = version;
125
+ if (props.disableAutoTrack) {
126
+ script.dataset.disableAutoTrack = "1";
127
+ }
128
+ if (props.endpoint) {
129
+ script.dataset.endpoint = props.endpoint;
130
+ }
131
+ if (props.dsn) {
132
+ script.dataset.dsn = props.dsn;
133
+ }
92
134
  script.onerror = () => {
93
135
  const errorMessage = isDevelopment() ? "Please check if any ad blockers are enabled and try again." : "Be sure to enable Web Analytics for your project and deploy again. See https://vercel.com/docs/analytics/quickstart for more information.";
94
136
  console.log(
@@ -96,7 +138,7 @@ function inject(props = {
96
138
  );
97
139
  };
98
140
  if (isDevelopment() && props.debug === false) {
99
- script.setAttribute("data-debug", "false");
141
+ script.dataset.debug = "false";
100
142
  }
101
143
  document.head.appendChild(script);
102
144
  }
@@ -129,13 +171,22 @@ function track(name2, properties) {
129
171
  }
130
172
  }
131
173
  }
174
+ function pageview({ route }) {
175
+ var _a;
176
+ (_a = window.va) == null ? void 0 : _a.call(window, "pageview", {
177
+ route
178
+ });
179
+ }
132
180
  var generic_default = {
133
181
  inject,
134
182
  track
135
183
  };
136
- export {
137
- generic_default as default,
184
+ // Annotate the CommonJS export names for ESM import in node:
185
+ 0 && (module.exports = {
186
+ DEV_SCRIPT_URL,
187
+ PROD_SCRIPT_URL,
138
188
  inject,
189
+ pageview,
139
190
  track
140
- };
191
+ });
141
192
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../package.json","../src/queue.ts","../src/utils.ts","../src/generic.ts"],"sourcesContent":["{\n \"name\": \"@vercel/analytics\",\n \"version\": \"1.1.2\",\n \"description\": \"Gain real-time traffic insights with Vercel Web Analytics\",\n \"keywords\": [\n \"analytics\",\n \"vercel\"\n ],\n \"repository\": {\n \"url\": \"github:vercel/analytics\",\n \"directory\": \"packages/web\"\n },\n \"license\": \"MPL-2.0\",\n \"type\": \"module\",\n \"exports\": {\n \"./package.json\": \"./package.json\",\n \".\": {\n \"browser\": \"./dist/index.js\",\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"./react\": {\n \"browser\": \"./dist/react/index.js\",\n \"import\": \"./dist/react/index.js\",\n \"require\": \"./dist/react/index.cjs\"\n },\n \"./server\": {\n \"node\": \"./dist/server/index.js\",\n \"edge-light\": \"./dist/server/index.js\",\n \"import\": \"./dist/server/index.js\",\n \"require\": \"./dist/server/index.cjs\",\n \"default\": \"./dist/server/index.cjs\"\n }\n },\n \"main\": \"dist/index.js\",\n \"types\": \"dist/index.d.ts\",\n \"typesVersions\": {\n \"*\": {\n \"*\": [\n \"dist/index.d.ts\"\n ],\n \"react\": [\n \"dist/react/index.d.ts\"\n ],\n \"server\": [\n \"dist/server/index.d.ts\"\n ]\n }\n },\n \"scripts\": {\n \"build\": \"tsup\",\n \"dev\": \"tsup --watch\",\n \"lint\": \"eslint .\",\n \"lint-fix\": \"eslint . --fix\",\n \"test\": \"jest\",\n \"type-check\": \"tsc --noEmit\"\n },\n \"eslintConfig\": {\n \"extends\": [\n \"@vercel/eslint-config\"\n ],\n \"rules\": {\n \"tsdoc/syntax\": \"off\"\n },\n \"ignorePatterns\": [\n \"jest.setup.ts\"\n ]\n },\n \"dependencies\": {\n \"server-only\": \"^0.0.1\"\n },\n \"devDependencies\": {\n \"@swc/core\": \"^1.3.66\",\n \"@swc/jest\": \"^0.2.26\",\n \"@testing-library/jest-dom\": \"^5.16.5\",\n \"@testing-library/react\": \"^14.0.0\",\n \"@types/jest\": \"^29.5.2\",\n \"@types/node\": \"^20.3.1\",\n \"@types/react\": \"^18.2.14\",\n \"@types/testing-library__jest-dom\": \"^5.14.6\",\n \"@vercel/eslint-config\": \"workspace:0.0.0\",\n \"jest\": \"^29.5.0\",\n \"jest-environment-jsdom\": \"^29.5.0\",\n \"react\": \"^18.2.0\",\n \"react-dom\": \"^18.2.0\",\n \"tsup\": \"7.1.0\"\n }\n}\n","export const initQueue = (): void => {\n // initialize va until script is loaded\n if (window.va) return;\n\n window.va = function a(...params): void {\n (window.vaq = window.vaq || []).push(params);\n };\n};\n","import type { AllowedPropertyValues, Mode } from './types';\n\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nfunction detectEnvironment(): 'development' | 'production' {\n try {\n const env = process.env.NODE_ENV;\n if (env === 'development' || env === 'test') {\n return 'development';\n }\n } catch (e) {\n // do nothing, this is okay\n }\n return 'production';\n}\n\nexport function setMode(mode: Mode = 'auto'): void {\n if (mode === 'auto') {\n window.vam = detectEnvironment();\n return;\n }\n\n window.vam = mode;\n}\n\nexport function getMode(): Mode {\n const mode = isBrowser() ? window.vam : detectEnvironment();\n return mode || 'production';\n}\n\nexport function isProduction(): boolean {\n return getMode() === 'production';\n}\n\nexport function isDevelopment(): boolean {\n return getMode() === 'development';\n}\n\nfunction removeKey(\n key: string,\n { [key]: _, ...rest }\n): Record<string, unknown> {\n return rest;\n}\n\nexport function parseProperties(\n properties: Record<string, unknown> | undefined,\n options: {\n strip?: boolean;\n }\n): Error | Record<string, AllowedPropertyValues> | undefined {\n if (!properties) return undefined;\n let props = properties;\n const errorProperties: string[] = [];\n for (const [key, value] of Object.entries(properties)) {\n if (typeof value === 'object' && value !== null) {\n if (options.strip) {\n props = removeKey(key, props);\n } else {\n errorProperties.push(key);\n }\n }\n }\n\n if (errorProperties.length > 0 && !options.strip) {\n throw Error(\n `The following properties are not valid: ${errorProperties.join(\n ', '\n )}. Only strings, numbers, booleans, and null are allowed.`\n );\n }\n return props as Record<string, AllowedPropertyValues>;\n}\n","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 */\nfunction 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 script.onerror = (): void => {\n const errorMessage = isDevelopment()\n ? 'Please check if any ad blockers are enabled and try again.'\n : 'Be sure to enable Web Analytics for your project and deploy again. See https://vercel.com/docs/analytics/quickstart for more information.';\n\n // eslint-disable-next-line no-console -- Logging to console is intentional\n console.log(\n `[Vercel Web Analytics] Failed to load script from ${src}. ${errorMessage}`\n );\n };\n\n if (isDevelopment() && props.debug === false) {\n script.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 */\nfunction track(\n name: string,\n properties?: Record<string, AllowedPropertyValues>\n): void {\n if (!isBrowser()) {\n const msg =\n '[Vercel Web Analytics] Please import `track` from `@vercel/analytics/server` when using this function in a server environment';\n\n if (isProduction()) {\n // eslint-disable-next-line no-console -- Show warning in production\n console.warn(msg);\n } else {\n throw new Error(msg);\n }\n\n return;\n }\n\n if (!properties) {\n window.va?.('event', { name });\n return;\n }\n\n try {\n const props = parseProperties(properties, {\n strip: isProduction(),\n });\n\n window.va?.('event', {\n name,\n data: props,\n });\n } catch (err) {\n if (err instanceof Error && isDevelopment()) {\n // eslint-disable-next-line no-console -- Logging to console is intentional\n console.error(err);\n }\n }\n}\n\nexport { inject, track };\nexport type { AnalyticsProps };\n\n// eslint-disable-next-line import/no-default-export -- Default export is intentional\nexport default {\n inject,\n track,\n};\n"],"mappings":";AACE,WAAQ;AACR,cAAW;;;ACFN,IAAM,YAAY,MAAY;AAEnC,MAAI,OAAO;AAAI;AAEf,SAAO,KAAK,SAAS,KAAK,QAAc;AACtC,KAAC,OAAO,MAAM,OAAO,OAAO,CAAC,GAAG,KAAK,MAAM;AAAA,EAC7C;AACF;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A;AACF;","names":["name"]}
1
+ {"version":3,"sources":["../src/generic.ts","../package.json","../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 const DEV_SCRIPT_URL =\n 'https://va.vercel-scripts.com/v1/script.debug.js';\nexport const PROD_SCRIPT_URL = '/_vercel/insights/script.js';\n\n/**\n * Injects the Vercel Web Analytics script into the page head and starts tracking page views. Read more in our [documentation](https://vercel.com/docs/concepts/analytics/package).\n * @param [props] - Analytics options.\n * @param [props.mode] - The mode to use for the analytics script. Defaults to `auto`.\n * - `auto` - Automatically detect the environment. Uses `production` if the environment cannot be determined.\n * - `production` - Always use the production script. (Sends events to the server)\n * - `development` - Always use the development script. (Logs events to the console)\n * @param [props.debug] - Whether to enable debug logging in development. Defaults to `true`.\n * @param [props.beforeSend] - A middleware function to modify events before they are sent. Should return the event object or `null` to cancel the event.\n * @param [props.dsn] - The DSN of the project to send events to. Only required when self-hosting.\n */\nfunction inject(\n props: AnalyticsProps & {\n framework?: string;\n } = {\n debug: true,\n }\n): void {\n if (!isBrowser()) return;\n\n setMode(props.mode);\n\n initQueue();\n\n if (props.beforeSend) {\n window.va?.('beforeSend', props.beforeSend);\n }\n\n const src =\n props.scriptSrc || (isDevelopment() ? DEV_SCRIPT_URL : PROD_SCRIPT_URL);\n\n if (document.head.querySelector(`script[src*=\"${src}\"]`)) return;\n\n const script = document.createElement('script');\n script.src = src;\n script.defer = true;\n script.dataset.sdkn =\n packageName + (props.framework ? `/${props.framework}` : '');\n script.dataset.sdkv = version;\n\n if (props.disableAutoTrack) {\n script.dataset.disableAutoTrack = '1';\n }\n if (props.endpoint) {\n script.dataset.endpoint = props.endpoint;\n }\n if (props.dsn) {\n script.dataset.dsn = props.dsn;\n }\n\n script.onerror = (): void => {\n const errorMessage = isDevelopment()\n ? 'Please check if any ad blockers are enabled and try again.'\n : 'Be sure to enable Web Analytics for your project and deploy again. See https://vercel.com/docs/analytics/quickstart for more information.';\n\n // eslint-disable-next-line no-console -- Logging to console is intentional\n console.log(\n `[Vercel Web Analytics] Failed to load script from ${src}. ${errorMessage}`\n );\n };\n\n if (isDevelopment() && props.debug === false) {\n script.dataset.debug = 'false';\n }\n\n document.head.appendChild(script);\n}\n\n/**\n * Tracks a custom event. Please refer to the [documentation](https://vercel.com/docs/concepts/analytics/custom-events) for more information on custom events.\n * @param name - The name of the event.\n * * Examples: `Purchase`, `Click Button`, or `Play Video`.\n * @param [properties] - Additional properties of the event. Nested objects are not supported. Allowed values are `string`, `number`, `boolean`, and `null`.\n */\nfunction track(\n name: string,\n properties?: Record<string, AllowedPropertyValues>\n): void {\n if (!isBrowser()) {\n const msg =\n '[Vercel Web Analytics] Please import `track` from `@vercel/analytics/server` when using this function in a server environment';\n\n if (isProduction()) {\n // eslint-disable-next-line no-console -- Show warning in production\n console.warn(msg);\n } else {\n throw new Error(msg);\n }\n\n return;\n }\n\n if (!properties) {\n window.va?.('event', { name });\n return;\n }\n\n try {\n const props = parseProperties(properties, {\n strip: isProduction(),\n });\n\n window.va?.('event', {\n name,\n data: props,\n });\n } catch (err) {\n if (err instanceof Error && isDevelopment()) {\n // eslint-disable-next-line no-console -- Logging to console is intentional\n console.error(err);\n }\n }\n}\n\nfunction pageview({ route }: { route?: string }): void {\n window.va?.('pageview', {\n route,\n });\n}\n\nexport { inject, track, pageview };\nexport type { AnalyticsProps };\n\n// eslint-disable-next-line import/no-default-export -- Default export is intentional\nexport default {\n inject,\n track,\n};\n","{\n \"name\": \"@vercel/analytics\",\n \"version\": \"1.2.0-beta.1\",\n \"description\": \"Gain real-time traffic insights with Vercel Web Analytics\",\n \"keywords\": [\n \"analytics\",\n \"vercel\"\n ],\n \"repository\": {\n \"url\": \"github:vercel/analytics\",\n \"directory\": \"packages/web\"\n },\n \"license\": \"MPL-2.0\",\n \"exports\": {\n \"./package.json\": \"./package.json\",\n \".\": {\n \"browser\": \"./dist/index.js\",\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"./react\": {\n \"browser\": \"./dist/react/index.js\",\n \"import\": \"./dist/react/index.js\",\n \"require\": \"./dist/react/index.cjs\"\n },\n \"./next\": {\n \"browser\": \"./dist/next/index.mjs\",\n \"import\": \"./dist/next/index.mjs\",\n \"require\": \"./dist/next/index.js\"\n },\n \"./server\": {\n \"node\": \"./dist/server/index.js\",\n \"edge-light\": \"./dist/server/index.js\",\n \"import\": \"./dist/server/index.js\",\n \"require\": \"./dist/server/index.cjs\",\n \"default\": \"./dist/server/index.cjs\"\n }\n },\n \"main\": \"./dist/index.js\",\n \"types\": \"./dist/index.d.ts\",\n \"typesVersions\": {\n \"*\": {\n \"*\": [\n \"dist/index.d.ts\"\n ],\n \"react\": [\n \"dist/react/index.d.ts\"\n ],\n \"server\": [\n \"dist/server/index.d.ts\"\n ],\n \"next\": [\n \"dist/next/index.d.ts\"\n ]\n }\n },\n \"scripts\": {\n \"build\": \"tsup\",\n \"dev\": \"tsup --watch\",\n \"lint\": \"eslint .\",\n \"lint-fix\": \"eslint . --fix\",\n \"test\": \"jest\",\n \"type-check\": \"tsc --noEmit\"\n },\n \"eslintConfig\": {\n \"extends\": [\n \"@vercel/eslint-config\"\n ],\n \"rules\": {\n \"tsdoc/syntax\": \"off\"\n },\n \"ignorePatterns\": [\n \"jest.setup.ts\"\n ]\n },\n \"dependencies\": {\n \"server-only\": \"^0.0.1\"\n },\n \"devDependencies\": {\n \"@swc/core\": \"^1.3.66\",\n \"@swc/jest\": \"^0.2.26\",\n \"@testing-library/jest-dom\": \"^5.16.5\",\n \"@testing-library/react\": \"^14.0.0\",\n \"@types/jest\": \"^29.5.2\",\n \"@types/node\": \"^20.3.1\",\n \"@types/react\": \"^18.2.14\",\n \"@types/testing-library__jest-dom\": \"^5.14.6\",\n \"@vercel/eslint-config\": \"workspace:0.0.0\",\n \"jest\": \"^29.5.0\",\n \"jest-environment-jsdom\": \"^29.5.0\",\n \"react\": \"^18.2.0\",\n \"react-dom\": \"^18.2.0\",\n \"tsup\": \"7.1.0\"\n },\n \"peerDependencies\": {\n \"next\": \">= 13\",\n \"react\": \"^18 || ^19\"\n },\n \"peerDependenciesMeta\": {\n \"next\": {\n \"optional\": true\n },\n \"react\": {\n \"optional\": true\n }\n }\n}\n","export const initQueue = (): void => {\n // initialize va until script is loaded\n if (window.va) return;\n\n window.va = function a(...params): void {\n (window.vaq = window.vaq || []).push(params);\n };\n};\n","import type { AllowedPropertyValues, Mode } from './types';\n\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nfunction detectEnvironment(): 'development' | 'production' {\n try {\n const env = process.env.NODE_ENV;\n if (env === 'development' || env === 'test') {\n return 'development';\n }\n } catch (e) {\n // do nothing, this is okay\n }\n return 'production';\n}\n\nexport function setMode(mode: Mode = 'auto'): void {\n if (mode === 'auto') {\n window.vam = detectEnvironment();\n return;\n }\n\n window.vam = mode;\n}\n\nexport function getMode(): Mode {\n const mode = isBrowser() ? window.vam : detectEnvironment();\n return mode || 'production';\n}\n\nexport function isProduction(): boolean {\n return getMode() === 'production';\n}\n\nexport function isDevelopment(): boolean {\n return getMode() === 'development';\n}\n\nfunction removeKey(\n key: string,\n { [key]: _, ...rest }\n): Record<string, unknown> {\n return rest;\n}\n\nexport function parseProperties(\n properties: Record<string, unknown> | undefined,\n options: {\n strip?: boolean;\n }\n): Error | Record<string, AllowedPropertyValues> | undefined {\n if (!properties) return undefined;\n let props = properties;\n const errorProperties: string[] = [];\n for (const [key, value] of Object.entries(properties)) {\n if (typeof value === 'object' && value !== null) {\n if (options.strip) {\n props = removeKey(key, props);\n } else {\n errorProperties.push(key);\n }\n }\n }\n\n if (errorProperties.length > 0 && !options.strip) {\n throw Error(\n `The following properties are not valid: ${errorProperties.join(\n ', '\n )}. Only strings, numbers, booleans, and null are allowed.`\n );\n }\n return props as Record<string, AllowedPropertyValues>;\n}\n\nexport function computeRoute(\n pathname: string | null,\n pathParams: Record<string, string | string[]> | null\n): string | null {\n if (!pathname || !pathParams) {\n return pathname;\n }\n\n let result = pathname;\n\n try {\n for (const [key, valueOrArray] of Object.entries(pathParams)) {\n const isValueArray = Array.isArray(valueOrArray);\n const value = isValueArray ? valueOrArray.join('/') : valueOrArray;\n const expr = isValueArray ? `...${key}` : key;\n\n const matcher = new RegExp(`/${escapeRegExp(value)}(?=[/?#]|$)`);\n if (matcher.test(result)) {\n result = result.replace(matcher, `/[${expr}]`);\n }\n }\n\n return result;\n } catch (e) {\n return pathname;\n }\n}\n\nfunction escapeRegExp(string: string): string {\n return string.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCE,WAAQ;AACR,cAAW;;;ACFN,IAAM,YAAY,MAAY;AAEnC,MAAI,OAAO;AAAI;AAEf,SAAO,KAAK,SAAS,KAAK,QAAc;AACtC,KAAC,OAAO,MAAM,OAAO,OAAO,CAAC,GAAG,KAAK,MAAM;AAAA,EAC7C;AACF;;;ACLO,SAAS,YAAqB;AACnC,SAAO,OAAO,WAAW;AAC3B;AAEA,SAAS,oBAAkD;AACzD,MAAI;AACF,UAAM,MAAM,QAAQ,IAAI;AACxB,QAAI,QAAQ,iBAAiB,QAAQ,QAAQ;AAC3C,aAAO;AAAA,IACT;AAAA,EACF,SAAS,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,QAAM,OAAO,UAAU,IAAI,OAAO,MAAM,kBAAkB;AAC1D,SAAO,QAAQ;AACjB;AAEO,SAAS,eAAwB;A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object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name2 in all)
8
- __defProp(target, name2, { get: all[name2], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/generic.ts
21
- var generic_exports = {};
22
- __export(generic_exports, {
23
- default: () => generic_default,
24
- inject: () => inject,
25
- track: () => track
26
- });
27
- module.exports = __toCommonJS(generic_exports);
28
-
29
1
  // package.json
30
2
  var name = "@vercel/analytics";
31
- var version = "1.1.2";
3
+ var version = "1.2.0-beta.1";
32
4
 
33
5
  // src/queue.ts
34
6
  var initQueue = () => {
@@ -98,6 +70,8 @@ function parseProperties(properties, options) {
98
70
  }
99
71
 
100
72
  // src/generic.ts
73
+ var DEV_SCRIPT_URL = "https://va.vercel-scripts.com/v1/script.debug.js";
74
+ var PROD_SCRIPT_URL = "/_vercel/insights/script.js";
101
75
  function inject(props = {
102
76
  debug: true
103
77
  }) {
@@ -109,14 +83,23 @@ function inject(props = {
109
83
  if (props.beforeSend) {
110
84
  (_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
111
85
  }
112
- const src = isDevelopment() ? "https://va.vercel-scripts.com/v1/script.debug.js" : "/_vercel/insights/script.js";
86
+ const src = props.scriptSrc || (isDevelopment() ? DEV_SCRIPT_URL : PROD_SCRIPT_URL);
113
87
  if (document.head.querySelector(`script[src*="${src}"]`))
114
88
  return;
115
89
  const script = document.createElement("script");
116
90
  script.src = src;
117
91
  script.defer = true;
118
- script.setAttribute("data-sdkn", name);
119
- script.setAttribute("data-sdkv", version);
92
+ script.dataset.sdkn = name + (props.framework ? `/${props.framework}` : "");
93
+ script.dataset.sdkv = version;
94
+ if (props.disableAutoTrack) {
95
+ script.dataset.disableAutoTrack = "1";
96
+ }
97
+ if (props.endpoint) {
98
+ script.dataset.endpoint = props.endpoint;
99
+ }
100
+ if (props.dsn) {
101
+ script.dataset.dsn = props.dsn;
102
+ }
120
103
  script.onerror = () => {
121
104
  const errorMessage = isDevelopment() ? "Please check if any ad blockers are enabled and try again." : "Be sure to enable Web Analytics for your project and deploy again. See https://vercel.com/docs/analytics/quickstart for more information.";
122
105
  console.log(
@@ -124,7 +107,7 @@ function inject(props = {
124
107
  );
125
108
  };
126
109
  if (isDevelopment() && props.debug === false) {
127
- script.setAttribute("data-debug", "false");
110
+ script.dataset.debug = "false";
128
111
  }
129
112
  document.head.appendChild(script);
130
113
  }
@@ -157,13 +140,22 @@ function track(name2, properties) {
157
140
  }
158
141
  }
159
142
  }
143
+ function pageview({ route }) {
144
+ var _a;
145
+ (_a = window.va) == null ? void 0 : _a.call(window, "pageview", {
146
+ route
147
+ });
148
+ }
160
149
  var generic_default = {
161
150
  inject,
162
151
  track
163
152
  };
164
- // Annotate the CommonJS export names for ESM import in node:
165
- 0 && (module.exports = {
153
+ export {
154
+ DEV_SCRIPT_URL,
155
+ PROD_SCRIPT_URL,
156
+ generic_default as default,
166
157
  inject,
158
+ pageview,
167
159
  track
168
- });
169
- //# sourceMappingURL=index.cjs.map
160
+ };
161
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../package.json","../src/queue.ts","../src/utils.ts","../src/generic.ts"],"sourcesContent":["{\n \"name\": \"@vercel/analytics\",\n \"version\": \"1.2.0-beta.1\",\n \"description\": \"Gain real-time traffic insights with Vercel Web Analytics\",\n \"keywords\": [\n \"analytics\",\n \"vercel\"\n ],\n \"repository\": {\n \"url\": \"github:vercel/analytics\",\n \"directory\": \"packages/web\"\n },\n \"license\": \"MPL-2.0\",\n \"exports\": {\n \"./package.json\": \"./package.json\",\n \".\": {\n \"browser\": \"./dist/index.js\",\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"./react\": {\n \"browser\": \"./dist/react/index.js\",\n \"import\": \"./dist/react/index.js\",\n \"require\": \"./dist/react/index.cjs\"\n },\n \"./next\": {\n \"browser\": \"./dist/next/index.mjs\",\n \"import\": \"./dist/next/index.mjs\",\n \"require\": \"./dist/next/index.js\"\n },\n \"./server\": {\n \"node\": \"./dist/server/index.js\",\n \"edge-light\": \"./dist/server/index.js\",\n \"import\": \"./dist/server/index.js\",\n \"require\": \"./dist/server/index.cjs\",\n \"default\": \"./dist/server/index.cjs\"\n }\n },\n \"main\": \"./dist/index.js\",\n \"types\": \"./dist/index.d.ts\",\n \"typesVersions\": {\n \"*\": {\n \"*\": [\n \"dist/index.d.ts\"\n ],\n \"react\": [\n \"dist/react/index.d.ts\"\n ],\n \"server\": [\n \"dist/server/index.d.ts\"\n ],\n \"next\": [\n \"dist/next/index.d.ts\"\n ]\n }\n },\n \"scripts\": {\n \"build\": \"tsup\",\n \"dev\": \"tsup --watch\",\n \"lint\": \"eslint .\",\n \"lint-fix\": \"eslint . --fix\",\n \"test\": \"jest\",\n \"type-check\": \"tsc --noEmit\"\n },\n \"eslintConfig\": {\n \"extends\": [\n \"@vercel/eslint-config\"\n ],\n \"rules\": {\n \"tsdoc/syntax\": \"off\"\n },\n \"ignorePatterns\": [\n \"jest.setup.ts\"\n ]\n },\n \"dependencies\": {\n \"server-only\": \"^0.0.1\"\n },\n \"devDependencies\": {\n \"@swc/core\": \"^1.3.66\",\n \"@swc/jest\": \"^0.2.26\",\n \"@testing-library/jest-dom\": \"^5.16.5\",\n \"@testing-library/react\": \"^14.0.0\",\n \"@types/jest\": \"^29.5.2\",\n \"@types/node\": \"^20.3.1\",\n \"@types/react\": \"^18.2.14\",\n \"@types/testing-library__jest-dom\": \"^5.14.6\",\n \"@vercel/eslint-config\": \"workspace:0.0.0\",\n \"jest\": \"^29.5.0\",\n \"jest-environment-jsdom\": \"^29.5.0\",\n \"react\": \"^18.2.0\",\n \"react-dom\": \"^18.2.0\",\n \"tsup\": \"7.1.0\"\n },\n \"peerDependencies\": {\n \"next\": \">= 13\",\n \"react\": \"^18 || ^19\"\n },\n \"peerDependenciesMeta\": {\n \"next\": {\n \"optional\": true\n },\n \"react\": {\n \"optional\": true\n }\n }\n}\n","export const initQueue = (): void => {\n // initialize va until script is loaded\n if (window.va) return;\n\n window.va = function a(...params): void {\n (window.vaq = window.vaq || []).push(params);\n };\n};\n","import type { AllowedPropertyValues, Mode } from './types';\n\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nfunction detectEnvironment(): 'development' | 'production' {\n try {\n const env = process.env.NODE_ENV;\n if (env === 'development' || env === 'test') {\n return 'development';\n }\n } catch (e) {\n // do nothing, this is okay\n }\n return 'production';\n}\n\nexport function setMode(mode: Mode = 'auto'): void {\n if (mode === 'auto') {\n window.vam = detectEnvironment();\n return;\n }\n\n window.vam = mode;\n}\n\nexport function getMode(): Mode {\n const mode = isBrowser() ? window.vam : detectEnvironment();\n return mode || 'production';\n}\n\nexport function isProduction(): boolean {\n return getMode() === 'production';\n}\n\nexport function isDevelopment(): boolean {\n return getMode() === 'development';\n}\n\nfunction removeKey(\n key: string,\n { [key]: _, ...rest }\n): Record<string, unknown> {\n return rest;\n}\n\nexport function parseProperties(\n properties: Record<string, unknown> | undefined,\n options: {\n strip?: boolean;\n }\n): Error | Record<string, AllowedPropertyValues> | undefined {\n if (!properties) return undefined;\n let props = properties;\n const errorProperties: string[] = [];\n for (const [key, value] of Object.entries(properties)) {\n if (typeof value === 'object' && value !== null) {\n if (options.strip) {\n props = removeKey(key, props);\n } else {\n errorProperties.push(key);\n }\n }\n }\n\n if (errorProperties.length > 0 && !options.strip) {\n throw Error(\n `The following properties are not valid: ${errorProperties.join(\n ', '\n )}. Only strings, numbers, booleans, and null are allowed.`\n );\n }\n return props as Record<string, AllowedPropertyValues>;\n}\n\nexport function computeRoute(\n pathname: string | null,\n pathParams: Record<string, string | string[]> | null\n): string | null {\n if (!pathname || !pathParams) {\n return pathname;\n }\n\n let result = pathname;\n\n try {\n for (const [key, valueOrArray] of Object.entries(pathParams)) {\n const isValueArray = Array.isArray(valueOrArray);\n const value = isValueArray ? valueOrArray.join('/') : valueOrArray;\n const expr = isValueArray ? `...${key}` : key;\n\n const matcher = new RegExp(`/${escapeRegExp(value)}(?=[/?#]|$)`);\n if (matcher.test(result)) {\n result = result.replace(matcher, `/[${expr}]`);\n }\n }\n\n return result;\n } catch (e) {\n return pathname;\n }\n}\n\nfunction escapeRegExp(string: string): string {\n return string.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n}\n","import { name as packageName, version } from '../package.json';\nimport { initQueue } from './queue';\nimport type { AllowedPropertyValues, AnalyticsProps } from './types';\nimport {\n isBrowser,\n parseProperties,\n setMode,\n isDevelopment,\n isProduction,\n} from './utils';\n\nexport const DEV_SCRIPT_URL =\n 'https://va.vercel-scripts.com/v1/script.debug.js';\nexport const PROD_SCRIPT_URL = '/_vercel/insights/script.js';\n\n/**\n * Injects the Vercel Web Analytics script into the page head and starts tracking page views. Read more in our [documentation](https://vercel.com/docs/concepts/analytics/package).\n * @param [props] - Analytics options.\n * @param [props.mode] - The mode to use for the analytics script. Defaults to `auto`.\n * - `auto` - Automatically detect the environment. Uses `production` if the environment cannot be determined.\n * - `production` - Always use the production script. (Sends events to the server)\n * - `development` - Always use the development script. (Logs events to the console)\n * @param [props.debug] - Whether to enable debug logging in development. Defaults to `true`.\n * @param [props.beforeSend] - A middleware function to modify events before they are sent. Should return the event object or `null` to cancel the event.\n * @param [props.dsn] - The DSN of the project to send events to. Only required when self-hosting.\n */\nfunction inject(\n props: AnalyticsProps & {\n framework?: string;\n } = {\n debug: true,\n }\n): void {\n if (!isBrowser()) return;\n\n setMode(props.mode);\n\n initQueue();\n\n if (props.beforeSend) {\n window.va?.('beforeSend', props.beforeSend);\n }\n\n const src =\n props.scriptSrc || (isDevelopment() ? DEV_SCRIPT_URL : PROD_SCRIPT_URL);\n\n if (document.head.querySelector(`script[src*=\"${src}\"]`)) return;\n\n const script = document.createElement('script');\n script.src = src;\n script.defer = true;\n script.dataset.sdkn =\n packageName + (props.framework ? `/${props.framework}` : '');\n script.dataset.sdkv = version;\n\n if (props.disableAutoTrack) {\n script.dataset.disableAutoTrack = '1';\n }\n if (props.endpoint) {\n script.dataset.endpoint = props.endpoint;\n }\n if (props.dsn) {\n script.dataset.dsn = props.dsn;\n }\n\n script.onerror = (): void => {\n const errorMessage = isDevelopment()\n ? 'Please check if any ad blockers are enabled and try again.'\n : 'Be sure to enable Web Analytics for your project and deploy again. See https://vercel.com/docs/analytics/quickstart for more information.';\n\n // eslint-disable-next-line no-console -- Logging to console is intentional\n console.log(\n `[Vercel Web Analytics] Failed to load script from ${src}. ${errorMessage}`\n );\n };\n\n if (isDevelopment() && props.debug === false) {\n script.dataset.debug = 'false';\n }\n\n document.head.appendChild(script);\n}\n\n/**\n * Tracks a custom event. Please refer to the [documentation](https://vercel.com/docs/concepts/analytics/custom-events) for more information on custom events.\n * @param name - The name of the event.\n * * Examples: `Purchase`, `Click Button`, or `Play Video`.\n * @param [properties] - Additional properties of the event. Nested objects are not supported. Allowed values are `string`, `number`, `boolean`, and `null`.\n */\nfunction track(\n name: string,\n properties?: Record<string, AllowedPropertyValues>\n): void {\n if (!isBrowser()) {\n const msg =\n '[Vercel Web Analytics] Please import `track` from `@vercel/analytics/server` when using this function in a server environment';\n\n if (isProduction()) {\n // eslint-disable-next-line no-console -- Show warning in production\n console.warn(msg);\n } else {\n throw new Error(msg);\n }\n\n return;\n }\n\n if (!properties) {\n window.va?.('event', { name });\n return;\n }\n\n try {\n const props = parseProperties(properties, {\n strip: isProduction(),\n });\n\n window.va?.('event', {\n name,\n data: props,\n });\n } catch (err) {\n if (err instanceof Error && isDevelopment()) {\n // eslint-disable-next-line no-console -- Logging to console is intentional\n console.error(err);\n }\n }\n}\n\nfunction pageview({ route }: { route?: string }): void {\n window.va?.('pageview', {\n route,\n });\n}\n\nexport { inject, track, pageview };\nexport type { AnalyticsProps };\n\n// eslint-disable-next-line import/no-default-export -- Default export is intentional\nexport default {\n inject,\n track,\n};\n"],"mappings":";AACE,WAAQ;AACR,cAAW;;;ACFN,IAAM,YAAY,MAAY;AAEnC,MAAI,OAAO;AAAI;AAEf,SAAO,KAAK,SAAS,KAAK,QAAc;AACtC,KAAC,OAAO,MAAM,OAAO,OAAO,CAAC,GAAG,KAAK,MAAM;AAAA,EAC7C;AACF;;;ACLO,SAAS,YAAqB;AACnC,SAAO,OAAO,WAAW;AAC3B;AAEA,SAAS,oBAAkD;AACzD,MAAI;AACF,UAAM,MAAM,QAAQ,IAAI;AACxB,QAAI,QAAQ,iBAAiB,QAAQ,QAAQ;AAC3C,aAAO;AAAA,IACT;AAAA,EACF,SAAS,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,QAAM,OAAO,UAAU,IAAI,OAAO,MAAM,kBAAkB;AAC1D,SAAO,QAAQ;AACjB;AAEO,SAAS,eAAwB;AACtC,SAAO,QAAQ,MAAM;AACvB;AAEO,SAAS,gBAAyB;AACvC,SAAO,QAAQ,MAAM;AACvB;AAEA,SAAS,UACP,KACA,EAAE,CAAC,GAAG,GAAG,GAAG,GAAG,KAAK,GACK;AACzB,SAAO;AACT;AAEO,SAAS,gBACd,YACA,SAG2D;AAC3D,MAAI,CAAC;AAAY,WAAO;AACxB,MAAI,QAAQ;AACZ,QAAM,kBAA4B,CAAC;AACnC,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,UAAU,GAAG;AACrD,QAAI,OAA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AO,QAAQ,QAAQ;AAAA,EACzB;AAEA,WAAS,KAAK,YAAY,MAAM;AAClC;AAQA,SAAS,MACPA,OACA,YACM;AA5FR;AA6FE,MAAI,CAAC,UAAU,GAAG;AAChB,UAAM,MACJ;AAEF,QAAI,aAAa,GAAG;AAElB,cAAQ,KAAK,GAAG;AAAA,IAClB,OAAO;AACL,YAAM,IAAI,MAAM,GAAG;AAAA,IACrB;AAEA;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;AAEA,SAAS,SAAS,EAAE,MAAM,GAA6B;AAjIvD;AAkIE,eAAO,OAAP,gCAAY,YAAY;AAAA,IACtB;AAAA,EACF;AACF;AAMA,IAAO,kBAAQ;AAAA,EACb;AAAA,EACA;AACF;","names":["name"]}
@@ -0,0 +1,36 @@
1
+ import React from 'react';
2
+
3
+ interface PageViewEvent {
4
+ type: 'pageview';
5
+ url: string;
6
+ }
7
+ interface CustomEvent {
8
+ type: 'event';
9
+ url: string;
10
+ }
11
+ type BeforeSendEvent = PageViewEvent | CustomEvent;
12
+ type Mode = 'auto' | 'development' | 'production';
13
+ type BeforeSend = (event: BeforeSendEvent) => BeforeSendEvent | null;
14
+ interface AnalyticsProps {
15
+ beforeSend?: BeforeSend;
16
+ debug?: boolean;
17
+ mode?: Mode;
18
+ route?: string | null;
19
+ disableAutoTrack?: boolean;
20
+ scriptSrc?: string;
21
+ endpoint?: string;
22
+ dsn?: string;
23
+ }
24
+ declare global {
25
+ interface Window {
26
+ va?: (event: 'beforeSend' | 'event' | 'pageview', properties?: unknown) => void;
27
+ vaq?: [string, unknown?][];
28
+ vai?: boolean;
29
+ vam?: Mode;
30
+ }
31
+ }
32
+
33
+ type Props = Omit<AnalyticsProps, 'route'>;
34
+ declare function Analytics(props: Props): React.ReactElement;
35
+
36
+ export { Analytics, AnalyticsProps };
@@ -0,0 +1,36 @@
1
+ import React from 'react';
2
+
3
+ interface PageViewEvent {
4
+ type: 'pageview';
5
+ url: string;
6
+ }
7
+ interface CustomEvent {
8
+ type: 'event';
9
+ url: string;
10
+ }
11
+ type BeforeSendEvent = PageViewEvent | CustomEvent;
12
+ type Mode = 'auto' | 'development' | 'production';
13
+ type BeforeSend = (event: BeforeSendEvent) => BeforeSendEvent | null;
14
+ interface AnalyticsProps {
15
+ beforeSend?: BeforeSend;
16
+ debug?: boolean;
17
+ mode?: Mode;
18
+ route?: string | null;
19
+ disableAutoTrack?: boolean;
20
+ scriptSrc?: string;
21
+ endpoint?: string;
22
+ dsn?: string;
23
+ }
24
+ declare global {
25
+ interface Window {
26
+ va?: (event: 'beforeSend' | 'event' | 'pageview', properties?: unknown) => void;
27
+ vaq?: [string, unknown?][];
28
+ vai?: boolean;
29
+ vam?: Mode;
30
+ }
31
+ }
32
+
33
+ type Props = Omit<AnalyticsProps, 'route'>;
34
+ declare function Analytics(props: Props): React.ReactElement;
35
+
36
+ export { Analytics, AnalyticsProps };