@vercel/analytics 0.1.6 → 0.1.7-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.
package/dist/index.cjs CHANGED
@@ -20,7 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/generic.ts
21
21
  var generic_exports = {};
22
22
  __export(generic_exports, {
23
- inject: () => inject
23
+ inject: () => inject,
24
+ track: () => track
24
25
  });
25
26
  module.exports = __toCommonJS(generic_exports);
26
27
 
@@ -37,16 +38,49 @@ var initQueue = () => {
37
38
  function isBrowser() {
38
39
  return typeof window !== "undefined";
39
40
  }
40
- function isDevelopment() {
41
+ function detectEnvironment() {
41
42
  if (typeof process === "undefined")
42
- return false;
43
- return process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test";
43
+ return "production";
44
+ if (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test")
45
+ return "development";
46
+ return "production";
44
47
  }
45
- function getMode(mode = "auto") {
48
+ function setMode(mode = "auto") {
46
49
  if (mode === "auto") {
47
- return isDevelopment() ? "development" : "production";
50
+ window.vam = detectEnvironment();
51
+ }
52
+ window.vam = mode;
53
+ }
54
+ function getMode() {
55
+ return window.vam || "production";
56
+ }
57
+ function isProduction() {
58
+ return getMode() === "production";
59
+ }
60
+ function isDevelopment() {
61
+ return getMode() === "development";
62
+ }
63
+ var removeKey = (key, { [key]: _, ...rest }) => rest;
64
+ function parseProperties(properties, options) {
65
+ let props = properties;
66
+ const errorProperties = [];
67
+ for (const [key, value] of Object.entries(properties)) {
68
+ if (typeof value === "object" && value !== null) {
69
+ if (options.strip) {
70
+ props = removeKey(key, props);
71
+ } else {
72
+ errorProperties.push(key);
73
+ }
74
+ }
75
+ }
76
+ if (errorProperties.length > 0 && !options.strip) {
77
+ throw Error(
78
+ `The following properties are not valid: ${errorProperties.join(
79
+ ", "
80
+ )}. Only strings, numbers, booleans, and null are allowed.`
81
+ );
48
82
  }
49
- return mode;
83
+ return props;
50
84
  }
51
85
 
52
86
  // src/generic.ts
@@ -56,24 +90,45 @@ var inject = (props = {
56
90
  var _a;
57
91
  if (!isBrowser())
58
92
  return;
59
- const mode = getMode(props.mode);
93
+ setMode(props.mode);
60
94
  initQueue();
61
95
  if (props.beforeSend) {
62
96
  (_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
63
97
  }
64
- const src = mode === "development" ? "https://cdn.vercel-insights.com/v1/script.debug.js" : "/_vercel/insights/script.js";
98
+ const src = isDevelopment() ? "https://cdn.vercel-insights.com/v1/script.debug.js" : "/_vercel/insights/script.js";
65
99
  if (document.head.querySelector(`script[src*="${src}"]`))
66
100
  return;
67
101
  const script = document.createElement("script");
68
102
  script.src = src;
69
103
  script.defer = true;
70
- if (mode === "development" && props.debug === false) {
104
+ if (isDevelopment() && props.debug === false) {
71
105
  script.setAttribute("data-debug", "false");
72
106
  }
73
107
  document.head.appendChild(script);
74
108
  };
109
+ var track = (name, properties) => {
110
+ var _a, _b;
111
+ if (!properties) {
112
+ (_a = window.va) == null ? void 0 : _a.call(window, "track", { name });
113
+ return;
114
+ }
115
+ try {
116
+ const props = parseProperties(properties, {
117
+ strip: isProduction()
118
+ });
119
+ (_b = window.va) == null ? void 0 : _b.call(window, "track", {
120
+ name,
121
+ data: props
122
+ });
123
+ } catch (err) {
124
+ if (err instanceof Error && isDevelopment()) {
125
+ console.error(err);
126
+ }
127
+ }
128
+ };
75
129
  // Annotate the CommonJS export names for ESM import in node:
76
130
  0 && (module.exports = {
77
- inject
131
+ inject,
132
+ track
78
133
  });
79
134
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
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, getMode } from './utils';\n\nexport const inject = (\n props: AnalyticsProps = {\n debug: true,\n },\n): void => {\n if (!isBrowser()) return;\n\n const mode = getMode(props.mode);\n\n initQueue();\n\n if (props.beforeSend) {\n window.va?.('beforeSend', props.beforeSend);\n }\n\n const src =\n mode === 'development'\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 (mode === 'development' && props.debug === false) {\n script.setAttribute('data-debug', 'false');\n }\n\n document.head.appendChild(script);\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 { Mode } from './types';\n\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nexport function isDevelopment(): boolean {\n if (typeof process === 'undefined') return false;\n return (\n process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test'\n );\n}\n\nexport function getMode(mode: Mode = 'auto'): Mode {\n if (mode === 'auto') {\n return isDevelopment() ? 'development' : 'production';\n }\n\n return mode;\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;;;ACLO,SAAS,YAAqB;AACnC,SAAO,OAAO,WAAW;AAC3B;AAEO,SAAS,gBAAyB;AACvC,MAAI,OAAO,YAAY;AAAa,WAAO;AAC3C,SACE,QAAQ,IAAI,aAAa,iBAAiB,QAAQ,IAAI,aAAa;AAEvE;AAEO,SAAS,QAAQ,OAAa,QAAc;AACjD,MAAI,SAAS,QAAQ;AACnB,WAAO,cAAc,IAAI,gBAAgB;AAAA,EAC3C;AAEA,SAAO;AACT;;;AFfO,IAAM,SAAS,CACpB,QAAwB;AAAA,EACtB,OAAO;AACT,MACS;AARX;AASE,MAAI,CAAC,UAAU;AAAG;AAElB,QAAM,OAAO,QAAQ,MAAM,IAAI;AAE/B,YAAU;AAEV,MAAI,MAAM,YAAY;AACpB,iBAAO,OAAP,gCAAY,cAAc,MAAM;AAAA,EAClC;AAEA,QAAM,MACJ,SAAS,gBACL,uDACA;AAEN,MAAI,SAAS,KAAK,cAAc,gBAAgB,OAAO;AAAG;AAE1D,QAAM,SAAS,SAAS,cAAc,QAAQ;AAC9C,SAAO,MAAM;AACb,SAAO,QAAQ;AAEf,MAAI,SAAS,iBAAiB,MAAM,UAAU,OAAO;AACnD,WAAO,aAAa,cAAc,OAAO;AAAA,EAC3C;AAEA,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 { AllowedPropertyValues, AnalyticsProps } from './types';\nimport {\n isBrowser,\n parseProperties,\n setMode,\n isDevelopment,\n isProduction,\n} from './utils';\n\nexport const 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://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() && props.debug === false) {\n script.setAttribute('data-debug', 'false');\n }\n\n document.head.appendChild(script);\n};\n\nexport const track = (\n name: string,\n properties?: Record<string, AllowedPropertyValues>,\n): void => {\n if (!properties) {\n window.va?.('track', { name });\n return;\n }\n\n try {\n const props = parseProperties(properties, {\n strip: isProduction(),\n });\n\n window.va?.('track', {\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","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 if (typeof process === 'undefined') return 'production';\n\n if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test')\n return 'development';\n\n return 'production';\n}\n\nexport function setMode(mode: Mode = 'auto'): void {\n if (mode === 'auto') {\n window.vam = detectEnvironment();\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\nconst removeKey = (key: string, { [key]: _, ...rest }): Record<string, any> =>\n rest;\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;;;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,oB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AE1D,QAAM,SAAS,SAAS,cAAc,QAAQ;AAC9C,SAAO,MAAM;AACb,SAAO,QAAQ;AAEf,MAAI,cAAc,KAAK,MAAM,UAAU,OAAO;AAC5C,WAAO,aAAa,cAAc,OAAO;AAAA,EAC3C;AAEA,WAAS,KAAK,YAAY,MAAM;AAClC;AAEO,IAAM,QAAQ,CACnB,MACA,eACS;AA7CX;AA8CE,MAAI,CAAC,YAAY;AACf,iBAAO,OAAP,gCAAY,SAAS,EAAE,KAAK;AAC5B;AAAA,EACF;AAEA,MAAI;AACF,UAAM,QAAQ,gBAAgB,YAAY;AAAA,MACxC,OAAO,aAAa;AAAA,IACtB,CAAC;AAED,iBAAO,OAAP,gCAAY,SAAS;AAAA,MACnB;AAAA,MACA,MAAM;AAAA,IACR;AAAA,EACF,SAAS,KAAP;AACA,QAAI,eAAe,SAAS,cAAc,GAAG;AAE3C,cAAQ,MAAM,GAAG;AAAA,IACnB;AAAA,EACF;AACF;","names":[]}
package/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ interface PageViewEvent {
4
4
  }
5
5
  declare type Event = PageViewEvent;
6
6
  declare type Mode = 'auto' | 'development' | 'production';
7
+ declare type AllowedPropertyValues = string | number | boolean | null;
7
8
  declare type BeforeSend = (event: Event) => Event | null;
8
9
  interface AnalyticsProps {
9
10
  beforeSend?: BeforeSend;
@@ -15,9 +16,11 @@ declare global {
15
16
  va?: (event: string, properties?: unknown) => void;
16
17
  vaq?: [string, unknown?][];
17
18
  vai?: boolean;
19
+ vam?: Mode;
18
20
  }
19
21
  }
20
22
 
21
23
  declare const inject: (props?: AnalyticsProps) => void;
24
+ declare const track: (name: string, properties?: Record<string, AllowedPropertyValues>) => void;
22
25
 
23
- export { inject };
26
+ export { inject, track };
package/dist/index.js CHANGED
@@ -11,16 +11,49 @@ var initQueue = () => {
11
11
  function isBrowser() {
12
12
  return typeof window !== "undefined";
13
13
  }
14
- function isDevelopment() {
14
+ function detectEnvironment() {
15
15
  if (typeof process === "undefined")
16
- return false;
17
- return process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test";
16
+ return "production";
17
+ if (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test")
18
+ return "development";
19
+ return "production";
18
20
  }
19
- function getMode(mode = "auto") {
21
+ function setMode(mode = "auto") {
20
22
  if (mode === "auto") {
21
- return isDevelopment() ? "development" : "production";
23
+ window.vam = detectEnvironment();
24
+ }
25
+ window.vam = mode;
26
+ }
27
+ function getMode() {
28
+ return window.vam || "production";
29
+ }
30
+ function isProduction() {
31
+ return getMode() === "production";
32
+ }
33
+ function isDevelopment() {
34
+ return getMode() === "development";
35
+ }
36
+ var removeKey = (key, { [key]: _, ...rest }) => rest;
37
+ function parseProperties(properties, options) {
38
+ let props = properties;
39
+ const errorProperties = [];
40
+ for (const [key, value] of Object.entries(properties)) {
41
+ if (typeof value === "object" && value !== null) {
42
+ if (options.strip) {
43
+ props = removeKey(key, props);
44
+ } else {
45
+ errorProperties.push(key);
46
+ }
47
+ }
48
+ }
49
+ if (errorProperties.length > 0 && !options.strip) {
50
+ throw Error(
51
+ `The following properties are not valid: ${errorProperties.join(
52
+ ", "
53
+ )}. Only strings, numbers, booleans, and null are allowed.`
54
+ );
22
55
  }
23
- return mode;
56
+ return props;
24
57
  }
25
58
 
26
59
  // src/generic.ts
@@ -30,23 +63,44 @@ var inject = (props = {
30
63
  var _a;
31
64
  if (!isBrowser())
32
65
  return;
33
- const mode = getMode(props.mode);
66
+ setMode(props.mode);
34
67
  initQueue();
35
68
  if (props.beforeSend) {
36
69
  (_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
37
70
  }
38
- const src = mode === "development" ? "https://cdn.vercel-insights.com/v1/script.debug.js" : "/_vercel/insights/script.js";
71
+ const src = isDevelopment() ? "https://cdn.vercel-insights.com/v1/script.debug.js" : "/_vercel/insights/script.js";
39
72
  if (document.head.querySelector(`script[src*="${src}"]`))
40
73
  return;
41
74
  const script = document.createElement("script");
42
75
  script.src = src;
43
76
  script.defer = true;
44
- if (mode === "development" && props.debug === false) {
77
+ if (isDevelopment() && props.debug === false) {
45
78
  script.setAttribute("data-debug", "false");
46
79
  }
47
80
  document.head.appendChild(script);
48
81
  };
82
+ var track = (name, properties) => {
83
+ var _a, _b;
84
+ if (!properties) {
85
+ (_a = window.va) == null ? void 0 : _a.call(window, "track", { name });
86
+ return;
87
+ }
88
+ try {
89
+ const props = parseProperties(properties, {
90
+ strip: isProduction()
91
+ });
92
+ (_b = window.va) == null ? void 0 : _b.call(window, "track", {
93
+ name,
94
+ data: props
95
+ });
96
+ } catch (err) {
97
+ if (err instanceof Error && isDevelopment()) {
98
+ console.error(err);
99
+ }
100
+ }
101
+ };
49
102
  export {
50
- inject
103
+ inject,
104
+ track
51
105
  };
52
106
  //# 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\nexport function isDevelopment(): boolean {\n if (typeof process === 'undefined') return false;\n return (\n process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test'\n );\n}\n\nexport function getMode(mode: Mode = 'auto'): Mode {\n if (mode === 'auto') {\n return isDevelopment() ? 'development' : 'production';\n }\n\n return mode;\n}\n","import { initQueue } from './queue';\nimport type { AnalyticsProps } from './types';\nimport { isBrowser, getMode } from './utils';\n\nexport const inject = (\n props: AnalyticsProps = {\n debug: true,\n },\n): void => {\n if (!isBrowser()) return;\n\n const mode = getMode(props.mode);\n\n initQueue();\n\n if (props.beforeSend) {\n window.va?.('beforeSend', props.beforeSend);\n }\n\n const src =\n mode === 'development'\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 (mode === 'development' && props.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;;;ACLO,SAAS,YAAqB;AACnC,SAAO,OAAO,WAAW;AAC3B;AAEO,SAAS,gBAAyB;AACvC,MAAI,OAAO,YAAY;AAAa,WAAO;AAC3C,SACE,QAAQ,IAAI,aAAa,iBAAiB,QAAQ,IAAI,aAAa;AAEvE;AAEO,SAAS,QAAQ,OAAa,QAAc;AACjD,MAAI,SAAS,QAAQ;AACnB,WAAO,cAAc,IAAI,gBAAgB;AAAA,EAC3C;AAEA,SAAO;AACT;;;ACfO,IAAM,SAAS,CACpB,QAAwB;AAAA,EACtB,OAAO;AACT,MACS;AARX;AASE,MAAI,CAAC,UAAU;AAAG;AAElB,QAAM,OAAO,QAAQ,MAAM,IAAI;AAE/B,YAAU;AAEV,MAAI,MAAM,YAAY;AACpB,iBAAO,OAAP,gCAAY,cAAc,MAAM;AAAA,EAClC;AAEA,QAAM,MACJ,SAAS,gBACL,uDACA;AAEN,MAAI,SAAS,KAAK,cAAc,gBAAgB,OAAO;AAAG;AAE1D,QAAM,SAAS,SAAS,cAAc,QAAQ;AAC9C,SAAO,MAAM;AACb,SAAO,QAAQ;AAEf,MAAI,SAAS,iBAAiB,MAAM,UAAU,OAAO;AACnD,WAAO,aAAa,cAAc,OAAO;AAAA,EAC3C;AAEA,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 // 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 if (typeof process === 'undefined') return 'production';\n\n if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test')\n return 'development';\n\n return 'production';\n}\n\nexport function setMode(mode: Mode = 'auto'): void {\n if (mode === 'auto') {\n window.vam = detectEnvironment();\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\nconst removeKey = (key: string, { [key]: _, ...rest }): Record<string, any> =>\n rest;\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 { 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 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://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() && props.debug === false) {\n script.setAttribute('data-debug', 'false');\n }\n\n document.head.appendChild(script);\n};\n\nexport const track = (\n name: string,\n properties?: Record<string, AllowedPropertyValues>,\n): void => {\n if (!properties) {\n window.va?.('track', { name });\n return;\n }\n\n try {\n const props = parseProperties(properties, {\n strip: isProduction(),\n });\n\n window.va?.('track', {\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"],"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,OAAO,YAAY;AAAa,WAAO;AAE3C,MAAI,QAAQ,IAAI,aAAa,iBAAiB,QAAQ,IAAI,aAAa;AACrE,WAAO;AAET,SAAO;AACT;AAEO,SAAS,QAAQ,OAAa,QAAc;AACjD,MAAI,SAAS,QAAQ;AACnB,WAAO,MAAM,kBAAkB;AAAA,EACjC;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,IAAM,YAAY,CAAC,KAAa,GAAG,MAAM,MAAM,KAAK,MAClD;AAEK,SAAS,gBACd,YACA,SAG2D;AAC3D,MAAI,QAAQ;AACZ,QAAM,kBAA4B,CAAC;AACnC,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,UAAU,GAAG;AACrD,QAAI,OAA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20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/react.tsx
21
21
  var react_exports = {};
22
22
  __export(react_exports, {
23
- Analytics: () => Analytics
23
+ Analytics: () => Analytics,
24
+ track: () => track
24
25
  });
25
26
  module.exports = __toCommonJS(react_exports);
26
27
  var import_react = require("react");
@@ -38,16 +39,49 @@ var initQueue = () => {
38
39
  function isBrowser() {
39
40
  return typeof window !== "undefined";
40
41
  }
41
- function isDevelopment() {
42
+ function detectEnvironment() {
42
43
  if (typeof process === "undefined")
43
- return false;
44
- return process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test";
44
+ return "production";
45
+ if (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test")
46
+ return "development";
47
+ return "production";
45
48
  }
46
- function getMode(mode = "auto") {
49
+ function setMode(mode = "auto") {
47
50
  if (mode === "auto") {
48
- return isDevelopment() ? "development" : "production";
51
+ window.vam = detectEnvironment();
52
+ }
53
+ window.vam = mode;
54
+ }
55
+ function getMode() {
56
+ return window.vam || "production";
57
+ }
58
+ function isProduction() {
59
+ return getMode() === "production";
60
+ }
61
+ function isDevelopment() {
62
+ return getMode() === "development";
63
+ }
64
+ var removeKey = (key, { [key]: _, ...rest }) => rest;
65
+ function parseProperties(properties, options) {
66
+ let props = properties;
67
+ const errorProperties = [];
68
+ for (const [key, value] of Object.entries(properties)) {
69
+ if (typeof value === "object" && value !== null) {
70
+ if (options.strip) {
71
+ props = removeKey(key, props);
72
+ } else {
73
+ errorProperties.push(key);
74
+ }
75
+ }
76
+ }
77
+ if (errorProperties.length > 0 && !options.strip) {
78
+ throw Error(
79
+ `The following properties are not valid: ${errorProperties.join(
80
+ ", "
81
+ )}. Only strings, numbers, booleans, and null are allowed.`
82
+ );
49
83
  }
50
- return mode;
84
+ return props;
51
85
  }
52
86
 
53
87
  // src/generic.ts
@@ -57,22 +91,42 @@ var inject = (props = {
57
91
  var _a;
58
92
  if (!isBrowser())
59
93
  return;
60
- const mode = getMode(props.mode);
94
+ setMode(props.mode);
61
95
  initQueue();
62
96
  if (props.beforeSend) {
63
97
  (_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
64
98
  }
65
- const src = mode === "development" ? "https://cdn.vercel-insights.com/v1/script.debug.js" : "/_vercel/insights/script.js";
99
+ const src = isDevelopment() ? "https://cdn.vercel-insights.com/v1/script.debug.js" : "/_vercel/insights/script.js";
66
100
  if (document.head.querySelector(`script[src*="${src}"]`))
67
101
  return;
68
102
  const script = document.createElement("script");
69
103
  script.src = src;
70
104
  script.defer = true;
71
- if (mode === "development" && props.debug === false) {
105
+ if (isDevelopment() && props.debug === false) {
72
106
  script.setAttribute("data-debug", "false");
73
107
  }
74
108
  document.head.appendChild(script);
75
109
  };
110
+ var track = (name, properties) => {
111
+ var _a, _b;
112
+ if (!properties) {
113
+ (_a = window.va) == null ? void 0 : _a.call(window, "track", { name });
114
+ return;
115
+ }
116
+ try {
117
+ const props = parseProperties(properties, {
118
+ strip: isProduction()
119
+ });
120
+ (_b = window.va) == null ? void 0 : _b.call(window, "track", {
121
+ name,
122
+ data: props
123
+ });
124
+ } catch (err) {
125
+ if (err instanceof Error && isDevelopment()) {
126
+ console.error(err);
127
+ }
128
+ }
129
+ };
76
130
 
77
131
  // src/react.tsx
78
132
  function Analytics({
@@ -87,6 +141,7 @@ function Analytics({
87
141
  }
88
142
  // Annotate the CommonJS export names for ESM import in node:
89
143
  0 && (module.exports = {
90
- Analytics
144
+ Analytics,
145
+ track
91
146
  });
92
147
  //# sourceMappingURL=index.cjs.map
@@ -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\nexport type { AnalyticsProps } from './types';\n\nexport function 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}\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 { Mode } from './types';\n\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nexport function isDevelopment(): boolean {\n if (typeof process === 'undefined') return false;\n return (\n process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test'\n );\n}\n\nexport function getMode(mode: Mode = 'auto'): Mode {\n if (mode === 'auto') {\n return isDevelopment() ? 'development' : 'production';\n }\n\n return mode;\n}\n","import { initQueue } from './queue';\nimport type { AnalyticsProps } from './types';\nimport { isBrowser, getMode } from './utils';\n\nexport const inject = (\n props: AnalyticsProps = {\n debug: true,\n },\n): void => {\n if (!isBrowser()) return;\n\n const mode = getMode(props.mode);\n\n initQueue();\n\n if (props.beforeSend) {\n window.va?.('beforeSend', props.beforeSend);\n }\n\n const src =\n mode === 'development'\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 (mode === 'development' && props.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;;;ACLO,SAAS,YAAqB;AACnC,SAAO,OAAO,WAAW;AAC3B;AAEO,SAAS,gBAAyB;AACvC,MAAI,OAAO,YAAY;AAAa,WAAO;AAC3C,SACE,QAAQ,IAAI,aAAa,iBAAiB,QAAQ,IAAI,aAAa;AAEvE;AAEO,SAAS,QAAQ,OAAa,QAAc;AACjD,MAAI,SAAS,QAAQ;AACnB,WAAO,cAAc,IAAI,gBAAgB;AAAA,EAC3C;AAEA,SAAO;AACT;;;ACfO,IAAM,SAAS,CACpB,QAAwB;AAAA,EACtB,OAAO;AACT,MACS;AARX;AASE,MAAI,CAAC,UAAU;AAAG;AAElB,QAAM,OAAO,QAAQ,MAAM,IAAI;AAE/B,YAAU;AAEV,MAAI,MAAM,YAAY;AACpB,iBAAO,OAAP,gCAAY,cAAc,MAAM;AAAA,EAClC;AAEA,QAAM,MACJ,SAAS,gBACL,uDACA;AAEN,MAAI,SAAS,KAAK,cAAc,gBAAgB,OAAO;AAAG;AAE1D,QAAM,SAAS,SAAS,cAAc,QAAQ;AAC9C,SAAO,MAAM;AACb,SAAO,QAAQ;AAEf,MAAI,SAAS,iBAAiB,MAAM,UAAU,OAAO;AACnD,WAAO,aAAa,cAAc,OAAO;AAAA,EAC3C;AAEA,WAAS,KAAK,YAAY,MAAM;AAClC;;;AH7BO,SAAS,UAAU;AAAA,EACxB;AAAA,EACA,QAAQ;AAAA,EACR,OAAO;AACT,GAAyB;AACvB,8BAAU,MAAM;AACd,WAAO,EAAE,YAAY,OAAO,KAAK,CAAC;AAAA,EACpC,GAAG,CAAC,YAAY,OAAO,IAAI,CAAC;AAE5B,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';\n\nexport type { AnalyticsProps } from './types';\n\nexport function 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}\n\nexport { track } from './generic';\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 if (typeof process === 'undefined') return 'production';\n\n if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test')\n return 'development';\n\n return 'production';\n}\n\nexport function setMode(mode: Mode = 'auto'): void {\n if (mode === 'auto') {\n window.vam = detectEnvironment();\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\nconst removeKey = (key: string, { [key]: _, ...rest }): Record<string, any> =>\n rest;\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 { 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 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://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() && props.debug === false) {\n script.setAttribute('data-debug', 'false');\n }\n\n document.head.appendChild(script);\n};\n\nexport const track = (\n name: string,\n properties?: Record<string, AllowedPropertyValues>,\n): void => {\n if (!properties) {\n window.va?.('track', { name });\n return;\n }\n\n try {\n const props = parseProperties(properties, {\n strip: isProduction(),\n });\n\n window.va?.('track', {\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"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;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;;;ACLO,SAAS,YAAqB;AACnC,SAAO,OAAO,WAAW;AAC3B;AAEA,SAAS,oBAAkD;AACzD,MAAI,OAAO,YAAY;AAAa,WAAO;AAE3C,MAAI,QAAQ,IAAI,aAAa,iBAAiB,QAAQ,IAAI,aAAa;AACrE,WAAO;AAET,SAAO;AACT;AAEO,SAAS,QAAQ,OAAa,QAAc;AACjD,MAAI,SAAS,QAAQ;AACnB,WAAO,MAAM,kBAAkB;AAAA,EACjC;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,IAAM,YAAY,CAAC,KAAa,GAAG,MAAM,MAAM,KAAK,MAClD;AAEK,SAAS,gBACd,YACA,SAG2D;AAC3D,MAAI,QAAQ;AACZ,QAAM,kBAA4B,CAAC;AACnC,aAAW,CAAC,KAAK,KAAK,KAAK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,IACnB;AAAA,EACF;AACF;;;AH5DO,SAAS,UAAU;AAAA,EACxB;AAAA,EACA,QAAQ;AAAA,EACR,OAAO;AACT,GAAyB;AACvB,8BAAU,MAAM;AACd,WAAO,EAAE,YAAY,OAAO,KAAK,CAAC;AAAA,EACpC,GAAG,CAAC,YAAY,OAAO,IAAI,CAAC;AAE5B,SAAO;AACT;","names":[]}
@@ -4,6 +4,7 @@ interface PageViewEvent {
4
4
  }
5
5
  declare type Event = PageViewEvent;
6
6
  declare type Mode = 'auto' | 'development' | 'production';
7
+ declare type AllowedPropertyValues = string | number | boolean | null;
7
8
  declare type BeforeSend = (event: Event) => Event | null;
8
9
  interface AnalyticsProps {
9
10
  beforeSend?: BeforeSend;
@@ -15,9 +16,12 @@ declare global {
15
16
  va?: (event: string, properties?: unknown) => void;
16
17
  vaq?: [string, unknown?][];
17
18
  vai?: boolean;
19
+ vam?: Mode;
18
20
  }
19
21
  }
20
22
 
23
+ declare const track: (name: string, properties?: Record<string, AllowedPropertyValues>) => void;
24
+
21
25
  declare function Analytics({ beforeSend, debug, mode, }: AnalyticsProps): null;
22
26
 
23
- export { Analytics, AnalyticsProps };
27
+ export { Analytics, AnalyticsProps, track };
@@ -14,16 +14,49 @@ var initQueue = () => {
14
14
  function isBrowser() {
15
15
  return typeof window !== "undefined";
16
16
  }
17
- function isDevelopment() {
17
+ function detectEnvironment() {
18
18
  if (typeof process === "undefined")
19
- return false;
20
- return process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test";
19
+ return "production";
20
+ if (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test")
21
+ return "development";
22
+ return "production";
21
23
  }
22
- function getMode(mode = "auto") {
24
+ function setMode(mode = "auto") {
23
25
  if (mode === "auto") {
24
- return isDevelopment() ? "development" : "production";
26
+ window.vam = detectEnvironment();
27
+ }
28
+ window.vam = mode;
29
+ }
30
+ function getMode() {
31
+ return window.vam || "production";
32
+ }
33
+ function isProduction() {
34
+ return getMode() === "production";
35
+ }
36
+ function isDevelopment() {
37
+ return getMode() === "development";
38
+ }
39
+ var removeKey = (key, { [key]: _, ...rest }) => rest;
40
+ function parseProperties(properties, options) {
41
+ let props = properties;
42
+ const errorProperties = [];
43
+ for (const [key, value] of Object.entries(properties)) {
44
+ if (typeof value === "object" && value !== null) {
45
+ if (options.strip) {
46
+ props = removeKey(key, props);
47
+ } else {
48
+ errorProperties.push(key);
49
+ }
50
+ }
51
+ }
52
+ if (errorProperties.length > 0 && !options.strip) {
53
+ throw Error(
54
+ `The following properties are not valid: ${errorProperties.join(
55
+ ", "
56
+ )}. Only strings, numbers, booleans, and null are allowed.`
57
+ );
25
58
  }
26
- return mode;
59
+ return props;
27
60
  }
28
61
 
29
62
  // src/generic.ts
@@ -33,22 +66,42 @@ var inject = (props = {
33
66
  var _a;
34
67
  if (!isBrowser())
35
68
  return;
36
- const mode = getMode(props.mode);
69
+ setMode(props.mode);
37
70
  initQueue();
38
71
  if (props.beforeSend) {
39
72
  (_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
40
73
  }
41
- const src = mode === "development" ? "https://cdn.vercel-insights.com/v1/script.debug.js" : "/_vercel/insights/script.js";
74
+ const src = isDevelopment() ? "https://cdn.vercel-insights.com/v1/script.debug.js" : "/_vercel/insights/script.js";
42
75
  if (document.head.querySelector(`script[src*="${src}"]`))
43
76
  return;
44
77
  const script = document.createElement("script");
45
78
  script.src = src;
46
79
  script.defer = true;
47
- if (mode === "development" && props.debug === false) {
80
+ if (isDevelopment() && props.debug === false) {
48
81
  script.setAttribute("data-debug", "false");
49
82
  }
50
83
  document.head.appendChild(script);
51
84
  };
85
+ var track = (name, properties) => {
86
+ var _a, _b;
87
+ if (!properties) {
88
+ (_a = window.va) == null ? void 0 : _a.call(window, "track", { name });
89
+ return;
90
+ }
91
+ try {
92
+ const props = parseProperties(properties, {
93
+ strip: isProduction()
94
+ });
95
+ (_b = window.va) == null ? void 0 : _b.call(window, "track", {
96
+ name,
97
+ data: props
98
+ });
99
+ } catch (err) {
100
+ if (err instanceof Error && isDevelopment()) {
101
+ console.error(err);
102
+ }
103
+ }
104
+ };
52
105
 
53
106
  // src/react.tsx
54
107
  function Analytics({
@@ -62,6 +115,7 @@ function Analytics({
62
115
  return null;
63
116
  }
64
117
  export {
65
- Analytics
118
+ Analytics,
119
+ track
66
120
  };
67
121
  //# sourceMappingURL=index.js.map
@@ -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\nexport type { AnalyticsProps } from './types';\n\nexport function 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}\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 { Mode } from './types';\n\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nexport function isDevelopment(): boolean {\n if (typeof process === 'undefined') return false;\n return (\n process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test'\n );\n}\n\nexport function getMode(mode: Mode = 'auto'): Mode {\n if (mode === 'auto') {\n return isDevelopment() ? 'development' : 'production';\n }\n\n return mode;\n}\n","import { initQueue } from './queue';\nimport type { AnalyticsProps } from './types';\nimport { isBrowser, getMode } from './utils';\n\nexport const inject = (\n props: AnalyticsProps = {\n debug: true,\n },\n): void => {\n if (!isBrowser()) return;\n\n const mode = getMode(props.mode);\n\n initQueue();\n\n if (props.beforeSend) {\n window.va?.('beforeSend', props.beforeSend);\n }\n\n const src =\n mode === 'development'\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 (mode === 'development' && props.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;;;ACLO,SAAS,YAAqB;AACnC,SAAO,OAAO,WAAW;AAC3B;AAEO,SAAS,gBAAyB;AACvC,MAAI,OAAO,YAAY;AAAa,WAAO;AAC3C,SACE,QAAQ,IAAI,aAAa,iBAAiB,QAAQ,IAAI,aAAa;AAEvE;AAEO,SAAS,QAAQ,OAAa,QAAc;AACjD,MAAI,SAAS,QAAQ;AACnB,WAAO,cAAc,IAAI,gBAAgB;AAAA,EAC3C;AAEA,SAAO;AACT;;;ACfO,IAAM,SAAS,CACpB,QAAwB;AAAA,EACtB,OAAO;AACT,MACS;AARX;AASE,MAAI,CAAC,UAAU;AAAG;AAElB,QAAM,OAAO,QAAQ,MAAM,IAAI;AAE/B,YAAU;AAEV,MAAI,MAAM,YAAY;AACpB,iBAAO,OAAP,gCAAY,cAAc,MAAM;AAAA,EAClC;AAEA,QAAM,MACJ,SAAS,gBACL,uDACA;AAEN,MAAI,SAAS,KAAK,cAAc,gBAAgB,OAAO;AAAG;AAE1D,QAAM,SAAS,SAAS,cAAc,QAAQ;AAC9C,SAAO,MAAM;AACb,SAAO,QAAQ;AAEf,MAAI,SAAS,iBAAiB,MAAM,UAAU,OAAO;AACnD,WAAO,aAAa,cAAc,OAAO;AAAA,EAC3C;AAEA,WAAS,KAAK,YAAY,MAAM;AAClC;;;AH7BO,SAAS,UAAU;AAAA,EACxB;AAAA,EACA,QAAQ;AAAA,EACR,OAAO;AACT,GAAyB;AACvB,YAAU,MAAM;AACd,WAAO,EAAE,YAAY,OAAO,KAAK,CAAC;AAAA,EACpC,GAAG,CAAC,YAAY,OAAO,IAAI,CAAC;AAE5B,SAAO;AACT;","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';\n\nexport type { AnalyticsProps } from './types';\n\nexport function 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}\n\nexport { track } from './generic';\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 if (typeof process === 'undefined') return 'production';\n\n if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test')\n return 'development';\n\n return 'production';\n}\n\nexport function setMode(mode: Mode = 'auto'): void {\n if (mode === 'auto') {\n window.vam = detectEnvironment();\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\nconst removeKey = (key: string, { [key]: _, ...rest }): Record<string, any> =>\n rest;\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 { 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 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://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() && props.debug === false) {\n script.setAttribute('data-debug', 'false');\n }\n\n document.head.appendChild(script);\n};\n\nexport const track = (\n name: string,\n properties?: Record<string, AllowedPropertyValues>,\n): void => {\n if (!properties) {\n window.va?.('track', { name });\n return;\n }\n\n try {\n const props = parseProperties(properties, {\n strip: isProduction(),\n });\n\n window.va?.('track', {\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"],"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,OAAO,YAAY;AAAa,WAAO;AAE3C,MAAI,QAAQ,IAAI,aAAa,iBAAiB,QAAQ,IAAI,aAAa;AACrE,WAAO;AAET,SAAO;AACT;AAEO,SAAS,QAAQ,OAAa,QAAc;AACjD,MAAI,SA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AK,YAAY,MAAM;AAClC;AAEO,IAAM,QAAQ,CACnB,MACA,eACS;AA7CX;AA8CE,MAAI,CAAC,YAAY;AACf,iBAAO,OAAP,gCAAY,SAAS,EAAE,KAAK;AAC5B;AAAA,EACF;AAEA,MAAI;AACF,UAAM,QAAQ,gBAAgB,YAAY;AAAA,MACxC,OAAO,aAAa;AAAA,IACtB,CAAC;AAED,iBAAO,OAAP,gCAAY,SAAS;AAAA,MACnB;AAAA,MACA,MAAM;AAAA,IACR;AAAA,EACF,SAAS,KAAP;AACA,QAAI,eAAe,SAAS,cAAc,GAAG;AAE3C,cAAQ,MAAM,GAAG;AAAA,IACnB;AAAA,EACF;AACF;;;AH5DO,SAAS,UAAU;AAAA,EACxB;AAAA,EACA,QAAQ;AAAA,EACR,OAAO;AACT,GAAyB;AACvB,YAAU,MAAM;AACd,WAAO,EAAE,YAAY,OAAO,KAAK,CAAC;AAAA,EACpC,GAAG,CAAC,YAAY,OAAO,IAAI,CAAC;AAE5B,SAAO;AACT;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vercel/analytics",
3
- "version": "0.1.6",
3
+ "version": "0.1.7-beta.1",
4
4
  "keywords": [
5
5
  "analytics",
6
6
  "vercel"