@vercel/analytics 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -33,23 +33,32 @@ var initQueue = () => {
33
33
  };
34
34
  };
35
35
 
36
+ // src/utils.ts
37
+ function isBrowser() {
38
+ return typeof window !== "undefined";
39
+ }
40
+ function isDevelopment() {
41
+ return typeof process !== "undefined" && process.env.NODE_ENV !== "production";
42
+ }
43
+
36
44
  // src/generic.ts
37
- var isBrowser = typeof window !== "undefined";
38
- var inject = ({
39
- beforeSend
40
- } = {}) => {
45
+ var inject = ({ beforeSend, debug } = { debug: isDevelopment() }) => {
41
46
  var _a;
42
- if (!isBrowser)
47
+ if (!isBrowser())
43
48
  return;
44
49
  initQueue();
45
50
  if (beforeSend) {
46
51
  (_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", beforeSend);
47
52
  }
48
- if (document.head.querySelector('script[src="/va/script.js"]'))
53
+ const src = isDevelopment() ? "https://cdn.vercel-insights.com/v1/script.debug.js" : "/_vercel/insights/script.js";
54
+ if (document.head.querySelector(`script[src*="${src}"]`))
49
55
  return;
50
56
  const script = document.createElement("script");
51
- script.src = "/va/script.js";
57
+ script.src = src;
52
58
  script.defer = true;
59
+ if (isDevelopment() && debug === false) {
60
+ script.setAttribute("data-debug", "false");
61
+ }
53
62
  document.head.appendChild(script);
54
63
  };
55
64
  // Annotate the CommonJS export names for ESM import in node:
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/generic.ts","../src/queue.ts"],"sourcesContent":["import { initQueue } from './queue';\nimport type { BeforeSend } from './types';\n\nconst isBrowser = typeof window !== 'undefined';\n\nexport const inject = ({\n beforeSend,\n}: { beforeSend?: BeforeSend } = {}): void => {\n if (!isBrowser) return;\n initQueue();\n\n if (beforeSend) {\n window.va?.('beforeSend', beforeSend);\n }\n\n if (document.head.querySelector('script[src=\"/va/script.js\"]')) return;\n\n const script = document.createElement('script');\n script.src = '/va/script.js';\n script.defer = true;\n\n document.head.appendChild(script);\n};\n","export const initQueue = (): void => {\n // initialise 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"],"mappings":";;;;;;;;;;;;;;;;;;;;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;;;ADJA,IAAM,YAAY,OAAO,WAAW;AAE7B,IAAM,SAAS,CAAC;AAAA,EACrB;AACF,IAAiC,CAAC,MAAY;AAP9C;AAQE,MAAI,CAAC;AAAW;AAChB,YAAU;AAEV,MAAI,YAAY;AACd,iBAAO,OAAP,gCAAY,cAAc;AAAA,EAC5B;AAEA,MAAI,SAAS,KAAK,cAAc,6BAA6B;AAAG;AAEhE,QAAM,SAAS,SAAS,cAAc,QAAQ;AAC9C,SAAO,MAAM;AACb,SAAO,QAAQ;AAEf,WAAS,KAAK,YAAY,MAAM;AAClC;","names":[]}
1
+ {"version":3,"sources":["../src/generic.ts","../src/queue.ts","../src/utils.ts"],"sourcesContent":["import { initQueue } from './queue';\nimport type { AnalyticsProps } from './types';\nimport { isBrowser, isDevelopment } from './utils';\n\nexport const inject = (\n { beforeSend, debug }: AnalyticsProps = { debug: isDevelopment() },\n): void => {\n if (!isBrowser()) return;\n initQueue();\n\n if (beforeSend) {\n window.va?.('beforeSend', beforeSend);\n }\n const src = isDevelopment()\n ? 'https://cdn.vercel-insights.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\n if (isDevelopment() && debug === false) {\n script.setAttribute('data-debug', 'false');\n }\n\n document.head.appendChild(script);\n};\n","export const initQueue = (): void => {\n // initialise 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","export function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nexport function isDevelopment(): boolean {\n return (\n typeof process !== 'undefined' && process.env.NODE_ENV !== 'production'\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;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;;;ACPO,SAAS,YAAqB;AACnC,SAAO,OAAO,WAAW;AAC3B;AAEO,SAAS,gBAAyB;AACvC,SACE,OAAO,YAAY,eAAe,QAAQ,IAAI,aAAa;AAE/D;;;AFJO,IAAM,SAAS,CACpB,EAAE,YAAY,MAAM,IAAoB,EAAE,OAAO,cAAc,EAAE,MACxD;AANX;AAOE,MAAI,CAAC,UAAU;AAAG;AAClB,YAAU;AAEV,MAAI,YAAY;AACd,iBAAO,OAAP,gCAAY,cAAc;AAAA,EAC5B;AACA,QAAM,MAAM,cAAc,IACtB,uDACA;AAEJ,MAAI,SAAS,KAAK,cAAc,gBAAgB,OAAO;AAAG;AAE1D,QAAM,SAAS,SAAS,cAAc,QAAQ;AAC9C,SAAO,MAAM;AACb,SAAO,QAAQ;AAEf,MAAI,cAAc,KAAK,UAAU,OAAO;AACtC,WAAO,aAAa,cAAc,OAAO;AAAA,EAC3C;AAEA,WAAS,KAAK,YAAY,MAAM;AAClC;","names":[]}
package/dist/index.d.ts CHANGED
@@ -4,6 +4,10 @@ interface PageViewEvent {
4
4
  }
5
5
  declare type IEvent = PageViewEvent;
6
6
  declare type BeforeSend = (event: IEvent) => IEvent | null;
7
+ interface AnalyticsProps {
8
+ beforeSend?: BeforeSend;
9
+ debug?: boolean;
10
+ }
7
11
  declare global {
8
12
  interface Window {
9
13
  va?: (event: string, properties?: unknown) => void;
@@ -12,8 +16,6 @@ declare global {
12
16
  }
13
17
  }
14
18
 
15
- declare const inject: ({ beforeSend, }?: {
16
- beforeSend?: BeforeSend | undefined;
17
- }) => void;
19
+ declare const inject: ({ beforeSend, debug }?: AnalyticsProps) => void;
18
20
 
19
21
  export { inject };
package/dist/index.js CHANGED
@@ -7,23 +7,32 @@ var initQueue = () => {
7
7
  };
8
8
  };
9
9
 
10
+ // src/utils.ts
11
+ function isBrowser() {
12
+ return typeof window !== "undefined";
13
+ }
14
+ function isDevelopment() {
15
+ return typeof process !== "undefined" && process.env.NODE_ENV !== "production";
16
+ }
17
+
10
18
  // src/generic.ts
11
- var isBrowser = typeof window !== "undefined";
12
- var inject = ({
13
- beforeSend
14
- } = {}) => {
19
+ var inject = ({ beforeSend, debug } = { debug: isDevelopment() }) => {
15
20
  var _a;
16
- if (!isBrowser)
21
+ if (!isBrowser())
17
22
  return;
18
23
  initQueue();
19
24
  if (beforeSend) {
20
25
  (_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", beforeSend);
21
26
  }
22
- if (document.head.querySelector('script[src="/va/script.js"]'))
27
+ const src = isDevelopment() ? "https://cdn.vercel-insights.com/v1/script.debug.js" : "/_vercel/insights/script.js";
28
+ if (document.head.querySelector(`script[src*="${src}"]`))
23
29
  return;
24
30
  const script = document.createElement("script");
25
- script.src = "/va/script.js";
31
+ script.src = src;
26
32
  script.defer = true;
33
+ if (isDevelopment() && debug === false) {
34
+ script.setAttribute("data-debug", "false");
35
+ }
27
36
  document.head.appendChild(script);
28
37
  };
29
38
  export {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/queue.ts","../src/generic.ts"],"sourcesContent":["export const initQueue = (): void => {\n // initialise 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 { initQueue } from './queue';\nimport type { BeforeSend } from './types';\n\nconst isBrowser = typeof window !== 'undefined';\n\nexport const inject = ({\n beforeSend,\n}: { beforeSend?: BeforeSend } = {}): void => {\n if (!isBrowser) return;\n initQueue();\n\n if (beforeSend) {\n window.va?.('beforeSend', beforeSend);\n }\n\n if (document.head.querySelector('script[src=\"/va/script.js\"]')) return;\n\n const script = document.createElement('script');\n script.src = '/va/script.js';\n script.defer = true;\n\n document.head.appendChild(script);\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;;;ACJA,IAAM,YAAY,OAAO,WAAW;AAE7B,IAAM,SAAS,CAAC;AAAA,EACrB;AACF,IAAiC,CAAC,MAAY;AAP9C;AAQE,MAAI,CAAC;AAAW;AAChB,YAAU;AAEV,MAAI,YAAY;AACd,iBAAO,OAAP,gCAAY,cAAc;AAAA,EAC5B;AAEA,MAAI,SAAS,KAAK,cAAc,6BAA6B;AAAG;AAEhE,QAAM,SAAS,SAAS,cAAc,QAAQ;AAC9C,SAAO,MAAM;AACb,SAAO,QAAQ;AAEf,WAAS,KAAK,YAAY,MAAM;AAClC;","names":[]}
1
+ {"version":3,"sources":["../src/queue.ts","../src/utils.ts","../src/generic.ts"],"sourcesContent":["export const initQueue = (): void => {\n // initialise 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","export function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nexport function isDevelopment(): boolean {\n return (\n typeof process !== 'undefined' && process.env.NODE_ENV !== 'production'\n );\n}\n","import { initQueue } from './queue';\nimport type { AnalyticsProps } from './types';\nimport { isBrowser, isDevelopment } from './utils';\n\nexport const inject = (\n { beforeSend, debug }: AnalyticsProps = { debug: isDevelopment() },\n): void => {\n if (!isBrowser()) return;\n initQueue();\n\n if (beforeSend) {\n window.va?.('beforeSend', beforeSend);\n }\n const src = isDevelopment()\n ? 'https://cdn.vercel-insights.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\n if (isDevelopment() && debug === false) {\n script.setAttribute('data-debug', 'false');\n }\n\n document.head.appendChild(script);\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;;;ACPO,SAAS,YAAqB;AACnC,SAAO,OAAO,WAAW;AAC3B;AAEO,SAAS,gBAAyB;AACvC,SACE,OAAO,YAAY,eAAe,QAAQ,IAAI,aAAa;AAE/D;;;ACJO,IAAM,SAAS,CACpB,EAAE,YAAY,MAAM,IAAoB,EAAE,OAAO,cAAc,EAAE,MACxD;AANX;AAOE,MAAI,CAAC,UAAU;AAAG;AAClB,YAAU;AAEV,MAAI,YAAY;AACd,iBAAO,OAAP,gCAAY,cAAc;AAAA,EAC5B;AACA,QAAM,MAAM,cAAc,IACtB,uDACA;AAEJ,MAAI,SAAS,KAAK,cAAc,gBAAgB,OAAO;AAAG;AAE1D,QAAM,SAAS,SAAS,cAAc,QAAQ;AAC9C,SAAO,MAAM;AACb,SAAO,QAAQ;AAEf,MAAI,cAAc,KAAK,UAAU,OAAO;AACtC,WAAO,aAAa,cAAc,OAAO;AAAA,EAC3C;AAEA,WAAS,KAAK,YAAY,MAAM;AAClC;","names":[]}
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,10 +15,6 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
- mod
23
- ));
24
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
19
 
26
20
  // src/react.tsx
@@ -29,7 +23,7 @@ __export(react_exports, {
29
23
  Analytics: () => Analytics
30
24
  });
31
25
  module.exports = __toCommonJS(react_exports);
32
- var import_react = __toESM(require("react"), 1);
26
+ var import_react = require("react");
33
27
 
34
28
  // src/queue.ts
35
29
  var initQueue = () => {
@@ -40,47 +34,43 @@ var initQueue = () => {
40
34
  };
41
35
  };
42
36
 
37
+ // src/utils.ts
38
+ function isBrowser() {
39
+ return typeof window !== "undefined";
40
+ }
41
+ function isDevelopment() {
42
+ return typeof process !== "undefined" && process.env.NODE_ENV !== "production";
43
+ }
44
+
43
45
  // src/generic.ts
44
- var isBrowser = typeof window !== "undefined";
45
- var inject = ({
46
- beforeSend
47
- } = {}) => {
46
+ var inject = ({ beforeSend, debug } = { debug: isDevelopment() }) => {
48
47
  var _a;
49
- if (!isBrowser)
48
+ if (!isBrowser())
50
49
  return;
51
50
  initQueue();
52
51
  if (beforeSend) {
53
52
  (_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", beforeSend);
54
53
  }
55
- if (document.head.querySelector('script[src="/va/script.js"]'))
54
+ const src = isDevelopment() ? "https://cdn.vercel-insights.com/v1/script.debug.js" : "/_vercel/insights/script.js";
55
+ if (document.head.querySelector(`script[src*="${src}"]`))
56
56
  return;
57
57
  const script = document.createElement("script");
58
- script.src = "/va/script.js";
58
+ script.src = src;
59
59
  script.defer = true;
60
+ if (isDevelopment() && debug === false) {
61
+ script.setAttribute("data-debug", "false");
62
+ }
60
63
  document.head.appendChild(script);
61
64
  };
62
65
 
63
66
  // src/react.tsx
64
- function Analytics(props) {
65
- if (process.env.NODE_ENV !== "production") {
66
- return /* @__PURE__ */ import_react.default.createElement(NoopAnalytics, null);
67
- }
68
- return /* @__PURE__ */ import_react.default.createElement(EnabledAnalytics, {
69
- ...props
70
- });
71
- }
72
- function EnabledAnalytics({ beforeSend }) {
73
- (0, import_react.useEffect)(() => {
74
- inject({ beforeSend });
75
- }, [beforeSend]);
76
- return null;
77
- }
78
- function NoopAnalytics() {
67
+ function Analytics({
68
+ beforeSend,
69
+ debug = isDevelopment()
70
+ }) {
79
71
  (0, import_react.useEffect)(() => {
80
- console.warn(
81
- "Vercel Analytics is set up, but detected a non-production environment.\n\nPlease note that no analytics events will be sent."
82
- );
83
- }, []);
72
+ inject({ beforeSend, debug });
73
+ }, [beforeSend, debug]);
84
74
  return null;
85
75
  }
86
76
  // Annotate the CommonJS export names for ESM import in node:
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/react.tsx","../../src/queue.ts","../../src/generic.ts"],"sourcesContent":["import React, { useEffect } from 'react';\nimport { inject } from './generic';\nimport type { BeforeSend } from './types';\n\ninterface AnalyticsProps {\n beforeSend?: BeforeSend;\n}\n\nexport function Analytics(props: AnalyticsProps): JSX.Element {\n if (process.env.NODE_ENV !== 'production') {\n return <NoopAnalytics />;\n }\n\n return <EnabledAnalytics {...props} />;\n}\n\nfunction EnabledAnalytics({ beforeSend }: AnalyticsProps): null {\n useEffect(() => {\n inject({ beforeSend });\n }, [beforeSend]);\n\n return null;\n}\n\nfunction NoopAnalytics(): null {\n useEffect(() => {\n // eslint-disable-next-line no-console\n console.warn(\n 'Vercel Analytics is set up, but detected a non-production environment.\\n\\nPlease note that no analytics events will be sent.',\n );\n }, []);\n\n return null;\n}\n","export const initQueue = (): void => {\n // initialise 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 { initQueue } from './queue';\nimport type { BeforeSend } from './types';\n\nconst isBrowser = typeof window !== 'undefined';\n\nexport const inject = ({\n beforeSend,\n}: { beforeSend?: BeforeSend } = {}): void => {\n if (!isBrowser) return;\n initQueue();\n\n if (beforeSend) {\n window.va?.('beforeSend', beforeSend);\n }\n\n if (document.head.querySelector('script[src=\"/va/script.js\"]')) return;\n\n const script = document.createElement('script');\n script.src = '/va/script.js';\n script.defer = true;\n\n document.head.appendChild(script);\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAiC;;;ACA1B,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;;;ACJA,IAAM,YAAY,OAAO,WAAW;AAE7B,IAAM,SAAS,CAAC;AAAA,EACrB;AACF,IAAiC,CAAC,MAAY;AAP9C;AAQE,MAAI,CAAC;AAAW;AAChB,YAAU;AAEV,MAAI,YAAY;AACd,iBAAO,OAAP,gCAAY,cAAc;AAAA,EAC5B;AAEA,MAAI,SAAS,KAAK,cAAc,6BAA6B;AAAG;AAEhE,QAAM,SAAS,SAAS,cAAc,QAAQ;AAC9C,SAAO,MAAM;AACb,SAAO,QAAQ;AAEf,WAAS,KAAK,YAAY,MAAM;AAClC;;;AFdO,SAAS,UAAU,OAAoC;AAC5D,MAAI,QAAQ,IAAI,aAAa,cAAc;AACzC,WAAO,6BAAAA,QAAA,cAAC,mBAAc;AAAA,EACxB;AAEA,SAAO,6BAAAA,QAAA,cAAC;AAAA,IAAkB,GAAG;AAAA,GAAO;AACtC;AAEA,SAAS,iBAAiB,EAAE,WAAW,GAAyB;AAC9D,8BAAU,MAAM;AACd,WAAO,EAAE,WAAW,CAAC;AAAA,EACvB,GAAG,CAAC,UAAU,CAAC;AAEf,SAAO;AACT;AAEA,SAAS,gBAAsB;AAC7B,8BAAU,MAAM;AAEd,YAAQ;AAAA,MACN;AAAA,IACF;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,SAAO;AACT;","names":["React"]}
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';\nimport { isDevelopment } from './utils';\n\nexport function Analytics({\n beforeSend,\n debug = isDevelopment(),\n}: AnalyticsProps): null {\n useEffect(() => {\n inject({ beforeSend, debug });\n }, [beforeSend, debug]);\n\n return null;\n}\n","export const initQueue = (): void => {\n // initialise 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","export function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nexport function isDevelopment(): boolean {\n return (\n typeof process !== 'undefined' && process.env.NODE_ENV !== 'production'\n );\n}\n","import { initQueue } from './queue';\nimport type { AnalyticsProps } from './types';\nimport { isBrowser, isDevelopment } from './utils';\n\nexport const inject = (\n { beforeSend, debug }: AnalyticsProps = { debug: isDevelopment() },\n): void => {\n if (!isBrowser()) return;\n initQueue();\n\n if (beforeSend) {\n window.va?.('beforeSend', beforeSend);\n }\n const src = isDevelopment()\n ? 'https://cdn.vercel-insights.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\n if (isDevelopment() && debug === false) {\n script.setAttribute('data-debug', 'false');\n }\n\n document.head.appendChild(script);\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA0B;;;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;;;ACPO,SAAS,YAAqB;AACnC,SAAO,OAAO,WAAW;AAC3B;AAEO,SAAS,gBAAyB;AACvC,SACE,OAAO,YAAY,eAAe,QAAQ,IAAI,aAAa;AAE/D;;;ACJO,IAAM,SAAS,CACpB,EAAE,YAAY,MAAM,IAAoB,EAAE,OAAO,cAAc,EAAE,MACxD;AANX;AAOE,MAAI,CAAC,UAAU;AAAG;AAClB,YAAU;AAEV,MAAI,YAAY;AACd,iBAAO,OAAP,gCAAY,cAAc;AAAA,EAC5B;AACA,QAAM,MAAM,cAAc,IACtB,uDACA;AAEJ,MAAI,SAAS,KAAK,cAAc,gBAAgB,OAAO;AAAG;AAE1D,QAAM,SAAS,SAAS,cAAc,QAAQ;AAC9C,SAAO,MAAM;AACb,SAAO,QAAQ;AAEf,MAAI,cAAc,KAAK,UAAU,OAAO;AACtC,WAAO,aAAa,cAAc,OAAO;AAAA,EAC3C;AAEA,WAAS,KAAK,YAAY,MAAM;AAClC;;;AHvBO,SAAS,UAAU;AAAA,EACxB;AAAA,EACA,QAAQ,cAAc;AACxB,GAAyB;AACvB,8BAAU,MAAM;AACd,WAAO,EAAE,YAAY,MAAM,CAAC;AAAA,EAC9B,GAAG,CAAC,YAAY,KAAK,CAAC;AAEtB,SAAO;AACT;","names":[]}
@@ -4,6 +4,10 @@ interface PageViewEvent {
4
4
  }
5
5
  declare type IEvent = PageViewEvent;
6
6
  declare type BeforeSend = (event: IEvent) => IEvent | null;
7
+ interface AnalyticsProps {
8
+ beforeSend?: BeforeSend;
9
+ debug?: boolean;
10
+ }
7
11
  declare global {
8
12
  interface Window {
9
13
  va?: (event: string, properties?: unknown) => void;
@@ -12,9 +16,6 @@ declare global {
12
16
  }
13
17
  }
14
18
 
15
- interface AnalyticsProps {
16
- beforeSend?: BeforeSend;
17
- }
18
- declare function Analytics(props: AnalyticsProps): JSX.Element;
19
+ declare function Analytics({ beforeSend, debug, }: AnalyticsProps): null;
19
20
 
20
21
  export { Analytics };
@@ -1,5 +1,5 @@
1
1
  // src/react.tsx
2
- import React, { useEffect } from "react";
2
+ import { useEffect } from "react";
3
3
 
4
4
  // src/queue.ts
5
5
  var initQueue = () => {
@@ -10,47 +10,43 @@ var initQueue = () => {
10
10
  };
11
11
  };
12
12
 
13
+ // src/utils.ts
14
+ function isBrowser() {
15
+ return typeof window !== "undefined";
16
+ }
17
+ function isDevelopment() {
18
+ return typeof process !== "undefined" && process.env.NODE_ENV !== "production";
19
+ }
20
+
13
21
  // src/generic.ts
14
- var isBrowser = typeof window !== "undefined";
15
- var inject = ({
16
- beforeSend
17
- } = {}) => {
22
+ var inject = ({ beforeSend, debug } = { debug: isDevelopment() }) => {
18
23
  var _a;
19
- if (!isBrowser)
24
+ if (!isBrowser())
20
25
  return;
21
26
  initQueue();
22
27
  if (beforeSend) {
23
28
  (_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", beforeSend);
24
29
  }
25
- if (document.head.querySelector('script[src="/va/script.js"]'))
30
+ const src = isDevelopment() ? "https://cdn.vercel-insights.com/v1/script.debug.js" : "/_vercel/insights/script.js";
31
+ if (document.head.querySelector(`script[src*="${src}"]`))
26
32
  return;
27
33
  const script = document.createElement("script");
28
- script.src = "/va/script.js";
34
+ script.src = src;
29
35
  script.defer = true;
36
+ if (isDevelopment() && debug === false) {
37
+ script.setAttribute("data-debug", "false");
38
+ }
30
39
  document.head.appendChild(script);
31
40
  };
32
41
 
33
42
  // src/react.tsx
34
- function Analytics(props) {
35
- if (process.env.NODE_ENV !== "production") {
36
- return /* @__PURE__ */ React.createElement(NoopAnalytics, null);
37
- }
38
- return /* @__PURE__ */ React.createElement(EnabledAnalytics, {
39
- ...props
40
- });
41
- }
42
- function EnabledAnalytics({ beforeSend }) {
43
- useEffect(() => {
44
- inject({ beforeSend });
45
- }, [beforeSend]);
46
- return null;
47
- }
48
- function NoopAnalytics() {
43
+ function Analytics({
44
+ beforeSend,
45
+ debug = isDevelopment()
46
+ }) {
49
47
  useEffect(() => {
50
- console.warn(
51
- "Vercel Analytics is set up, but detected a non-production environment.\n\nPlease note that no analytics events will be sent."
52
- );
53
- }, []);
48
+ inject({ beforeSend, debug });
49
+ }, [beforeSend, debug]);
54
50
  return null;
55
51
  }
56
52
  export {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/react.tsx","../../src/queue.ts","../../src/generic.ts"],"sourcesContent":["import React, { useEffect } from 'react';\nimport { inject } from './generic';\nimport type { BeforeSend } from './types';\n\ninterface AnalyticsProps {\n beforeSend?: BeforeSend;\n}\n\nexport function Analytics(props: AnalyticsProps): JSX.Element {\n if (process.env.NODE_ENV !== 'production') {\n return <NoopAnalytics />;\n }\n\n return <EnabledAnalytics {...props} />;\n}\n\nfunction EnabledAnalytics({ beforeSend }: AnalyticsProps): null {\n useEffect(() => {\n inject({ beforeSend });\n }, [beforeSend]);\n\n return null;\n}\n\nfunction NoopAnalytics(): null {\n useEffect(() => {\n // eslint-disable-next-line no-console\n console.warn(\n 'Vercel Analytics is set up, but detected a non-production environment.\\n\\nPlease note that no analytics events will be sent.',\n );\n }, []);\n\n return null;\n}\n","export const initQueue = (): void => {\n // initialise 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 { initQueue } from './queue';\nimport type { BeforeSend } from './types';\n\nconst isBrowser = typeof window !== 'undefined';\n\nexport const inject = ({\n beforeSend,\n}: { beforeSend?: BeforeSend } = {}): void => {\n if (!isBrowser) return;\n initQueue();\n\n if (beforeSend) {\n window.va?.('beforeSend', beforeSend);\n }\n\n if (document.head.querySelector('script[src=\"/va/script.js\"]')) return;\n\n const script = document.createElement('script');\n script.src = '/va/script.js';\n script.defer = true;\n\n document.head.appendChild(script);\n};\n"],"mappings":";AAAA,OAAO,SAAS,iBAAiB;;;ACA1B,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;;;ACJA,IAAM,YAAY,OAAO,WAAW;AAE7B,IAAM,SAAS,CAAC;AAAA,EACrB;AACF,IAAiC,CAAC,MAAY;AAP9C;AAQE,MAAI,CAAC;AAAW;AAChB,YAAU;AAEV,MAAI,YAAY;AACd,iBAAO,OAAP,gCAAY,cAAc;AAAA,EAC5B;AAEA,MAAI,SAAS,KAAK,cAAc,6BAA6B;AAAG;AAEhE,QAAM,SAAS,SAAS,cAAc,QAAQ;AAC9C,SAAO,MAAM;AACb,SAAO,QAAQ;AAEf,WAAS,KAAK,YAAY,MAAM;AAClC;;;AFdO,SAAS,UAAU,OAAoC;AAC5D,MAAI,QAAQ,IAAI,aAAa,cAAc;AACzC,WAAO,oCAAC,mBAAc;AAAA,EACxB;AAEA,SAAO,oCAAC;AAAA,IAAkB,GAAG;AAAA,GAAO;AACtC;AAEA,SAAS,iBAAiB,EAAE,WAAW,GAAyB;AAC9D,YAAU,MAAM;AACd,WAAO,EAAE,WAAW,CAAC;AAAA,EACvB,GAAG,CAAC,UAAU,CAAC;AAEf,SAAO;AACT;AAEA,SAAS,gBAAsB;AAC7B,YAAU,MAAM;AAEd,YAAQ;AAAA,MACN;AAAA,IACF;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,SAAO;AACT;","names":[]}
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';\nimport { isDevelopment } from './utils';\n\nexport function Analytics({\n beforeSend,\n debug = isDevelopment(),\n}: AnalyticsProps): null {\n useEffect(() => {\n inject({ beforeSend, debug });\n }, [beforeSend, debug]);\n\n return null;\n}\n","export const initQueue = (): void => {\n // initialise 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","export function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nexport function isDevelopment(): boolean {\n return (\n typeof process !== 'undefined' && process.env.NODE_ENV !== 'production'\n );\n}\n","import { initQueue } from './queue';\nimport type { AnalyticsProps } from './types';\nimport { isBrowser, isDevelopment } from './utils';\n\nexport const inject = (\n { beforeSend, debug }: AnalyticsProps = { debug: isDevelopment() },\n): void => {\n if (!isBrowser()) return;\n initQueue();\n\n if (beforeSend) {\n window.va?.('beforeSend', beforeSend);\n }\n const src = isDevelopment()\n ? 'https://cdn.vercel-insights.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\n if (isDevelopment() && debug === false) {\n script.setAttribute('data-debug', 'false');\n }\n\n document.head.appendChild(script);\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;;;ACPO,SAAS,YAAqB;AACnC,SAAO,OAAO,WAAW;AAC3B;AAEO,SAAS,gBAAyB;AACvC,SACE,OAAO,YAAY,eAAe,QAAQ,IAAI,aAAa;AAE/D;;;ACJO,IAAM,SAAS,CACpB,EAAE,YAAY,MAAM,IAAoB,EAAE,OAAO,cAAc,EAAE,MACxD;AANX;AAOE,MAAI,CAAC,UAAU;AAAG;AAClB,YAAU;AAEV,MAAI,YAAY;AACd,iBAAO,OAAP,gCAAY,cAAc;AAAA,EAC5B;AACA,QAAM,MAAM,cAAc,IACtB,uDACA;AAEJ,MAAI,SAAS,KAAK,cAAc,gBAAgB,OAAO;AAAG;AAE1D,QAAM,SAAS,SAAS,cAAc,QAAQ;AAC9C,SAAO,MAAM;AACb,SAAO,QAAQ;AAEf,MAAI,cAAc,KAAK,UAAU,OAAO;AACtC,WAAO,aAAa,cAAc,OAAO;AAAA,EAC3C;AAEA,WAAS,KAAK,YAAY,MAAM;AAClC;;;AHvBO,SAAS,UAAU;AAAA,EACxB;AAAA,EACA,QAAQ,cAAc;AACxB,GAAyB;AACvB,YAAU,MAAM;AACd,WAAO,EAAE,YAAY,MAAM,CAAC;AAAA,EAC9B,GAAG,CAAC,YAAY,KAAK,CAAC;AAEtB,SAAO;AACT;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vercel/analytics",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "keywords": [
5
5
  "analytics",
6
6
  "vercel"
@@ -46,6 +46,7 @@
46
46
  },
47
47
  "scripts": {
48
48
  "build": "tsup",
49
+ "dev": "tsup --watch",
49
50
  "lint": "eslint .",
50
51
  "lint-fix": "eslint . --fix",
51
52
  "type-check": "tsc --noEmit"