vercel analytics script on both pages; disable project SSO protection (was 404-ing visitors)
This commit is contained in:
1 parent
cc6a660729
commit
632d053de9
68 files changed
+5662
No files matched your search
+78
@@ -0,0 +1,78 @@
|
||||
interface PageViewEvent {
|
||||
type: 'pageview';
|
||||
url: string;
|
||||
}
|
||||
interface CustomEvent {
|
||||
type: 'event';
|
||||
url: string;
|
||||
}
|
||||
type BeforeSendEvent = PageViewEvent | CustomEvent;
|
||||
type Mode = 'auto' | 'development' | 'production';
|
||||
type AllowedPropertyValues = string | number | boolean | null | undefined;
|
||||
type BeforeSend = (event: BeforeSendEvent) => BeforeSendEvent | null;
|
||||
interface AnalyticsProps {
|
||||
beforeSend?: BeforeSend;
|
||||
debug?: boolean;
|
||||
mode?: Mode;
|
||||
scriptSrc?: string;
|
||||
dsn?: string;
|
||||
eventEndpoint?: string;
|
||||
viewEndpoint?: string;
|
||||
sessionEndpoint?: string;
|
||||
endpoint?: string;
|
||||
}
|
||||
declare global {
|
||||
interface Window {
|
||||
va?: (event: 'beforeSend' | 'event' | 'pageview', properties?: unknown) => void;
|
||||
vaq?: [string, unknown?][];
|
||||
vai?: boolean;
|
||||
vam?: Mode;
|
||||
/** used by Astro component only */
|
||||
webAnalyticsBeforeSend?: BeforeSend;
|
||||
}
|
||||
}
|
||||
type PlainFlags = Record<string, unknown>;
|
||||
type FlagsDataInput = (string | PlainFlags)[] | PlainFlags;
|
||||
|
||||
/**
|
||||
* Tracks a custom event. Please refer to the [documentation](https://vercel.com/docs/concepts/analytics/custom-events) for more information on custom events.
|
||||
* @param name - The name of the event.
|
||||
* * Examples: `Purchase`, `Click Button`, or `Play Video`.
|
||||
* @param [properties] - Additional properties of the event. Nested objects are not supported. Allowed values are `string`, `number`, `boolean`, and `null`.
|
||||
*/
|
||||
declare function track(name: string, properties?: Record<string, AllowedPropertyValues>, options?: {
|
||||
flags?: FlagsDataInput;
|
||||
}): void;
|
||||
|
||||
/**
|
||||
* 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).
|
||||
* @param [props] - Analytics options.
|
||||
* @param [props.mode] - The mode to use for the analytics script. Defaults to `auto`.
|
||||
* - `auto` - Automatically detect the environment. Uses `production` if the environment cannot be determined.
|
||||
* - `production` - Always use the production script. (Sends events to the server)
|
||||
* - `development` - Always use the development script. (Logs events to the console)
|
||||
* @param [props.debug] - Whether to enable debug logging in development. Defaults to `true`.
|
||||
* @param [props.beforeSend] - A middleware function to modify events before they are sent. Should return the event object or `null` to cancel the event.
|
||||
* @example
|
||||
* ```js
|
||||
* import { Analytics } from '@vercel/analytics/react';
|
||||
*
|
||||
* export default function App() {
|
||||
* return (
|
||||
* <div>
|
||||
* <Analytics />
|
||||
* <h1>My App</h1>
|
||||
* </div>
|
||||
* );
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
declare function Analytics(props: AnalyticsProps & {
|
||||
framework?: string;
|
||||
route?: string | null;
|
||||
path?: string | null;
|
||||
basePath?: string;
|
||||
configString?: string;
|
||||
}): null;
|
||||
|
||||
export { Analytics, type AnalyticsProps, type BeforeSend, type BeforeSendEvent, track };
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
interface PageViewEvent {
|
||||
type: 'pageview';
|
||||
url: string;
|
||||
}
|
||||
interface CustomEvent {
|
||||
type: 'event';
|
||||
url: string;
|
||||
}
|
||||
type BeforeSendEvent = PageViewEvent | CustomEvent;
|
||||
type Mode = 'auto' | 'development' | 'production';
|
||||
type AllowedPropertyValues = string | number | boolean | null | undefined;
|
||||
type BeforeSend = (event: BeforeSendEvent) => BeforeSendEvent | null;
|
||||
interface AnalyticsProps {
|
||||
beforeSend?: BeforeSend;
|
||||
debug?: boolean;
|
||||
mode?: Mode;
|
||||
scriptSrc?: string;
|
||||
dsn?: string;
|
||||
eventEndpoint?: string;
|
||||
viewEndpoint?: string;
|
||||
sessionEndpoint?: string;
|
||||
endpoint?: string;
|
||||
}
|
||||
declare global {
|
||||
interface Window {
|
||||
va?: (event: 'beforeSend' | 'event' | 'pageview', properties?: unknown) => void;
|
||||
vaq?: [string, unknown?][];
|
||||
vai?: boolean;
|
||||
vam?: Mode;
|
||||
/** used by Astro component only */
|
||||
webAnalyticsBeforeSend?: BeforeSend;
|
||||
}
|
||||
}
|
||||
type PlainFlags = Record<string, unknown>;
|
||||
type FlagsDataInput = (string | PlainFlags)[] | PlainFlags;
|
||||
|
||||
/**
|
||||
* Tracks a custom event. Please refer to the [documentation](https://vercel.com/docs/concepts/analytics/custom-events) for more information on custom events.
|
||||
* @param name - The name of the event.
|
||||
* * Examples: `Purchase`, `Click Button`, or `Play Video`.
|
||||
* @param [properties] - Additional properties of the event. Nested objects are not supported. Allowed values are `string`, `number`, `boolean`, and `null`.
|
||||
*/
|
||||
declare function track(name: string, properties?: Record<string, AllowedPropertyValues>, options?: {
|
||||
flags?: FlagsDataInput;
|
||||
}): void;
|
||||
|
||||
/**
|
||||
* 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).
|
||||
* @param [props] - Analytics options.
|
||||
* @param [props.mode] - The mode to use for the analytics script. Defaults to `auto`.
|
||||
* - `auto` - Automatically detect the environment. Uses `production` if the environment cannot be determined.
|
||||
* - `production` - Always use the production script. (Sends events to the server)
|
||||
* - `development` - Always use the development script. (Logs events to the console)
|
||||
* @param [props.debug] - Whether to enable debug logging in development. Defaults to `true`.
|
||||
* @param [props.beforeSend] - A middleware function to modify events before they are sent. Should return the event object or `null` to cancel the event.
|
||||
* @example
|
||||
* ```js
|
||||
* import { Analytics } from '@vercel/analytics/react';
|
||||
*
|
||||
* export default function App() {
|
||||
* return (
|
||||
* <div>
|
||||
* <Analytics />
|
||||
* <h1>My App</h1>
|
||||
* </div>
|
||||
* );
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
declare function Analytics(props: AnalyticsProps & {
|
||||
framework?: string;
|
||||
route?: string | null;
|
||||
path?: string | null;
|
||||
basePath?: string;
|
||||
configString?: string;
|
||||
}): null;
|
||||
|
||||
export { Analytics, type AnalyticsProps, type BeforeSend, type BeforeSendEvent, track };
|
||||
+270
@@ -0,0 +1,270 @@
|
||||
"use strict";
|
||||
"use client";
|
||||
var __defProp = Object.defineProperty;
|
||||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||||
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||||
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
||||
var __export = (target, all) => {
|
||||
for (var name2 in all)
|
||||
__defProp(target, name2, { get: all[name2], enumerable: true });
|
||||
};
|
||||
var __copyProps = (to, from, except, desc) => {
|
||||
if (from && typeof from === "object" || typeof from === "function") {
|
||||
for (let key of __getOwnPropNames(from))
|
||||
if (!__hasOwnProp.call(to, key) && key !== except)
|
||||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
||||
}
|
||||
return to;
|
||||
};
|
||||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
||||
|
||||
// src/react/index.tsx
|
||||
var react_exports = {};
|
||||
__export(react_exports, {
|
||||
Analytics: () => Analytics,
|
||||
track: () => track
|
||||
});
|
||||
module.exports = __toCommonJS(react_exports);
|
||||
var import_react = require("react");
|
||||
|
||||
// src/queue.ts
|
||||
var initQueue = () => {
|
||||
if (window.va) return;
|
||||
window.va = function a(...params) {
|
||||
if (!window.vaq) window.vaq = [];
|
||||
window.vaq.push(params);
|
||||
};
|
||||
};
|
||||
|
||||
// package.json
|
||||
var name = "@vercel/analytics";
|
||||
var version = "2.0.1";
|
||||
|
||||
// src/utils.ts
|
||||
function isBrowser() {
|
||||
return typeof window !== "undefined";
|
||||
}
|
||||
function detectEnvironment() {
|
||||
try {
|
||||
const env = process.env.NODE_ENV;
|
||||
if (env === "development" || env === "test") {
|
||||
return "development";
|
||||
}
|
||||
} catch {
|
||||
}
|
||||
return "production";
|
||||
}
|
||||
function setMode(mode = "auto") {
|
||||
if (mode === "auto") {
|
||||
window.vam = detectEnvironment();
|
||||
return;
|
||||
}
|
||||
window.vam = mode;
|
||||
}
|
||||
function getMode() {
|
||||
const mode = isBrowser() ? window.vam : detectEnvironment();
|
||||
return mode || "production";
|
||||
}
|
||||
function isProduction() {
|
||||
return getMode() === "production";
|
||||
}
|
||||
function isDevelopment() {
|
||||
return getMode() === "development";
|
||||
}
|
||||
function removeKey(key, { [key]: _, ...rest }) {
|
||||
return rest;
|
||||
}
|
||||
function parseProperties(properties, options) {
|
||||
if (!properties) return void 0;
|
||||
let props = properties;
|
||||
const errorProperties = [];
|
||||
for (const [key, value] of Object.entries(properties)) {
|
||||
if (typeof value === "object" && value !== null) {
|
||||
if (options.strip) {
|
||||
props = removeKey(key, props);
|
||||
} else {
|
||||
errorProperties.push(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (errorProperties.length > 0 && !options.strip) {
|
||||
throw Error(
|
||||
`The following properties are not valid: ${errorProperties.join(
|
||||
", "
|
||||
)}. Only strings, numbers, booleans, and null are allowed.`
|
||||
);
|
||||
}
|
||||
return props;
|
||||
}
|
||||
function getScriptSrc(props) {
|
||||
if (props.scriptSrc) {
|
||||
return makeAbsolute(props.scriptSrc);
|
||||
}
|
||||
if (isDevelopment()) {
|
||||
return "https://va.vercel-scripts.com/v1/script.debug.js";
|
||||
}
|
||||
if (props.basePath) {
|
||||
return makeAbsolute(`${props.basePath}/insights/script.js`);
|
||||
}
|
||||
return "/_vercel/insights/script.js";
|
||||
}
|
||||
function loadProps(explicitProps, confString) {
|
||||
var _a;
|
||||
let props = explicitProps;
|
||||
if (confString) {
|
||||
try {
|
||||
props = {
|
||||
...(_a = JSON.parse(confString)) == null ? void 0 : _a.analytics,
|
||||
...explicitProps
|
||||
};
|
||||
} catch {
|
||||
}
|
||||
}
|
||||
setMode(props.mode);
|
||||
const dataset = {
|
||||
sdkn: name + (props.framework ? `/${props.framework}` : ""),
|
||||
sdkv: version
|
||||
};
|
||||
if (props.disableAutoTrack) {
|
||||
dataset.disableAutoTrack = "1";
|
||||
}
|
||||
if (props.viewEndpoint) {
|
||||
dataset.viewEndpoint = makeAbsolute(props.viewEndpoint);
|
||||
}
|
||||
if (props.eventEndpoint) {
|
||||
dataset.eventEndpoint = makeAbsolute(props.eventEndpoint);
|
||||
}
|
||||
if (props.sessionEndpoint) {
|
||||
dataset.sessionEndpoint = makeAbsolute(props.sessionEndpoint);
|
||||
}
|
||||
if (isDevelopment() && props.debug === false) {
|
||||
dataset.debug = "false";
|
||||
}
|
||||
if (props.dsn) {
|
||||
dataset.dsn = props.dsn;
|
||||
}
|
||||
if (props.endpoint) {
|
||||
dataset.endpoint = props.endpoint;
|
||||
} else if (props.basePath) {
|
||||
dataset.endpoint = makeAbsolute(`${props.basePath}/insights`);
|
||||
}
|
||||
return {
|
||||
beforeSend: props.beforeSend,
|
||||
src: getScriptSrc(props),
|
||||
dataset
|
||||
};
|
||||
}
|
||||
function makeAbsolute(url) {
|
||||
return url.startsWith("http://") || url.startsWith("https://") || url.startsWith("/") ? url : `/${url}`;
|
||||
}
|
||||
|
||||
// src/generic.ts
|
||||
function inject(props = {
|
||||
debug: true
|
||||
}, confString) {
|
||||
var _a;
|
||||
if (!isBrowser()) return;
|
||||
const { beforeSend, src, dataset } = loadProps(props, confString);
|
||||
initQueue();
|
||||
if (beforeSend) {
|
||||
(_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", beforeSend);
|
||||
}
|
||||
if (document.head.querySelector(`script[src*="${src}"]`)) return;
|
||||
const script = document.createElement("script");
|
||||
script.src = src;
|
||||
for (const [key, value] of Object.entries(dataset)) {
|
||||
script.dataset[key] = value;
|
||||
}
|
||||
script.defer = true;
|
||||
script.onerror = () => {
|
||||
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.";
|
||||
console.log(
|
||||
`[Vercel Web Analytics] Failed to load script from ${src}. ${errorMessage}`
|
||||
);
|
||||
};
|
||||
document.head.appendChild(script);
|
||||
}
|
||||
function track(name2, properties, options) {
|
||||
var _a, _b;
|
||||
if (!isBrowser()) {
|
||||
const msg = "[Vercel Web Analytics] Please import `track` from `@vercel/analytics/server` when using this function in a server environment";
|
||||
if (isProduction()) {
|
||||
console.warn(msg);
|
||||
} else {
|
||||
throw new Error(msg);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!properties) {
|
||||
(_a = window.va) == null ? void 0 : _a.call(window, "event", { name: name2, options });
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const props = parseProperties(properties, {
|
||||
strip: isProduction()
|
||||
});
|
||||
(_b = window.va) == null ? void 0 : _b.call(window, "event", {
|
||||
name: name2,
|
||||
data: props,
|
||||
options
|
||||
});
|
||||
} catch (err) {
|
||||
if (err instanceof Error && isDevelopment()) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
function pageview({
|
||||
route,
|
||||
path
|
||||
}) {
|
||||
var _a;
|
||||
(_a = window.va) == null ? void 0 : _a.call(window, "pageview", { route, path });
|
||||
}
|
||||
|
||||
// src/react/utils.ts
|
||||
function getBasePath() {
|
||||
if (typeof process === "undefined" || typeof process.env === "undefined") {
|
||||
return void 0;
|
||||
}
|
||||
return process.env.REACT_APP_VERCEL_OBSERVABILITY_BASEPATH;
|
||||
}
|
||||
function getConfigString() {
|
||||
if (typeof process === "undefined" || typeof process.env === "undefined") {
|
||||
return void 0;
|
||||
}
|
||||
return process.env.REACT_APP_VERCEL_OBSERVABILITY_CLIENT_CONFIG;
|
||||
}
|
||||
|
||||
// src/react/index.tsx
|
||||
function Analytics(props) {
|
||||
(0, import_react.useEffect)(() => {
|
||||
var _a;
|
||||
if (props.beforeSend) {
|
||||
(_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
|
||||
}
|
||||
}, [props.beforeSend]);
|
||||
(0, import_react.useEffect)(() => {
|
||||
inject(
|
||||
{
|
||||
framework: props.framework || "react",
|
||||
basePath: props.basePath ?? getBasePath(),
|
||||
...props.route !== void 0 && { disableAutoTrack: true },
|
||||
...props
|
||||
},
|
||||
props.configString ?? getConfigString()
|
||||
);
|
||||
}, []);
|
||||
(0, import_react.useEffect)(() => {
|
||||
if (props.route && props.path) {
|
||||
pageview({ route: props.route, path: props.path });
|
||||
}
|
||||
}, [props.route, props.path]);
|
||||
return null;
|
||||
}
|
||||
// Annotate the CommonJS export names for ESM import in node:
|
||||
0 && (module.exports = {
|
||||
Analytics,
|
||||
track
|
||||
});
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../src/react/index.tsx","../../src/queue.ts","../../package.json","../../src/utils.ts","../../src/generic.ts","../../src/react/utils.ts"],"sourcesContent":["'use client';\nimport { useEffect } from 'react';\nimport { inject, pageview, track } from '../generic';\nimport type { AnalyticsProps, BeforeSend, BeforeSendEvent } from '../types';\nimport { getBasePath, getConfigString } 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 * @example\n * ```js\n * import { Analytics } from '@vercel/analytics/react';\n *\n * export default function App() {\n * return (\n * <div>\n * <Analytics />\n * <h1>My App</h1>\n * </div>\n * );\n * }\n * ```\n */\nfunction Analytics(\n props: AnalyticsProps & {\n framework?: string;\n route?: string | null;\n path?: string | null;\n basePath?: string;\n configString?: string;\n },\n): null {\n useEffect(() => {\n if (props.beforeSend) {\n window.va?.('beforeSend', props.beforeSend);\n }\n }, [props.beforeSend]);\n\n // biome-ignore lint/correctness/useExhaustiveDependencies: only run once\n useEffect(() => {\n inject(\n {\n framework: props.framework || 'react',\n basePath: props.basePath ?? getBasePath(),\n ...(props.route !== undefined && { disableAutoTrack: true }),\n ...props,\n },\n props.configString ?? getConfigString(),\n );\n }, []);\n\n useEffect(() => {\n // explicitely track page view, since we disabled auto tracking\n if (props.route && props.path) {\n pageview({ route: props.route, path: props.path });\n }\n }, [props.route, props.path]);\n\n return null;\n}\n\nexport { track, Analytics };\nexport type { AnalyticsProps, BeforeSend, BeforeSendEvent };\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 if (!window.vaq) window.vaq = [];\n window.vaq.push(params);\n };\n};\n","{\n \"name\": \"@vercel/analytics\",\n \"version\": \"2.0.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\": \"MIT\",\n \"exports\": {\n \"./package.json\": \"./package.json\",\n \".\": {\n \"browser\": \"./dist/index.mjs\",\n \"import\": \"./dist/index.mjs\",\n \"require\": \"./dist/index.js\"\n },\n \"./astro\": {\n \"import\": \"./dist/astro/component.ts\"\n },\n \"./next\": {\n \"browser\": \"./dist/next/index.mjs\",\n \"import\": \"./dist/next/index.mjs\",\n \"require\": \"./dist/next/index.js\"\n },\n \"./nuxt\": {\n \"import\": \"./dist/nuxt/index.mjs\",\n \"require\": \"./dist/nuxt/index.js\"\n },\n \"./nuxt/runtime\": {\n \"browser\": \"./dist/nuxt/runtime/index.mjs\",\n \"import\": \"./dist/nuxt/runtime/index.mjs\",\n \"require\": \"./dist/nuxt/runtime/index.js\"\n },\n \"./react\": {\n \"browser\": \"./dist/react/index.mjs\",\n \"import\": \"./dist/react/index.mjs\",\n \"require\": \"./dist/react/index.js\"\n },\n \"./remix\": {\n \"browser\": \"./dist/remix/index.mjs\",\n \"import\": \"./dist/remix/index.mjs\",\n \"require\": \"./dist/remix/index.js\"\n },\n \"./server\": {\n \"node\": \"./dist/server/index.mjs\",\n \"edge-light\": \"./dist/server/index.mjs\",\n \"import\": \"./dist/server/index.mjs\",\n \"require\": \"./dist/server/index.js\",\n \"default\": \"./dist/server/index.js\"\n },\n \"./sveltekit\": {\n \"svelte\": \"./dist/sveltekit/index.mjs\",\n \"types\": \"./dist/sveltekit/index.d.ts\"\n },\n \"./vue\": {\n \"browser\": \"./dist/vue/index.mjs\",\n \"import\": \"./dist/vue/index.mjs\",\n \"require\": \"./dist/vue/index.js\"\n }\n },\n \"main\": \"./dist/index.mjs\",\n \"types\": \"./dist/index.d.ts\",\n \"typesVersions\": {\n \"*\": {\n \"*\": [\n \"dist/index.d.ts\"\n ],\n Line truncated
|
||||
+245
@@ -0,0 +1,245 @@
|
||||
"use client";
|
||||
|
||||
// src/react/index.tsx
|
||||
import { useEffect } from "react";
|
||||
|
||||
// src/queue.ts
|
||||
var initQueue = () => {
|
||||
if (window.va) return;
|
||||
window.va = function a(...params) {
|
||||
if (!window.vaq) window.vaq = [];
|
||||
window.vaq.push(params);
|
||||
};
|
||||
};
|
||||
|
||||
// package.json
|
||||
var name = "@vercel/analytics";
|
||||
var version = "2.0.1";
|
||||
|
||||
// src/utils.ts
|
||||
function isBrowser() {
|
||||
return typeof window !== "undefined";
|
||||
}
|
||||
function detectEnvironment() {
|
||||
try {
|
||||
const env = process.env.NODE_ENV;
|
||||
if (env === "development" || env === "test") {
|
||||
return "development";
|
||||
}
|
||||
} catch {
|
||||
}
|
||||
return "production";
|
||||
}
|
||||
function setMode(mode = "auto") {
|
||||
if (mode === "auto") {
|
||||
window.vam = detectEnvironment();
|
||||
return;
|
||||
}
|
||||
window.vam = mode;
|
||||
}
|
||||
function getMode() {
|
||||
const mode = isBrowser() ? window.vam : detectEnvironment();
|
||||
return mode || "production";
|
||||
}
|
||||
function isProduction() {
|
||||
return getMode() === "production";
|
||||
}
|
||||
function isDevelopment() {
|
||||
return getMode() === "development";
|
||||
}
|
||||
function removeKey(key, { [key]: _, ...rest }) {
|
||||
return rest;
|
||||
}
|
||||
function parseProperties(properties, options) {
|
||||
if (!properties) return void 0;
|
||||
let props = properties;
|
||||
const errorProperties = [];
|
||||
for (const [key, value] of Object.entries(properties)) {
|
||||
if (typeof value === "object" && value !== null) {
|
||||
if (options.strip) {
|
||||
props = removeKey(key, props);
|
||||
} else {
|
||||
errorProperties.push(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (errorProperties.length > 0 && !options.strip) {
|
||||
throw Error(
|
||||
`The following properties are not valid: ${errorProperties.join(
|
||||
", "
|
||||
)}. Only strings, numbers, booleans, and null are allowed.`
|
||||
);
|
||||
}
|
||||
return props;
|
||||
}
|
||||
function getScriptSrc(props) {
|
||||
if (props.scriptSrc) {
|
||||
return makeAbsolute(props.scriptSrc);
|
||||
}
|
||||
if (isDevelopment()) {
|
||||
return "https://va.vercel-scripts.com/v1/script.debug.js";
|
||||
}
|
||||
if (props.basePath) {
|
||||
return makeAbsolute(`${props.basePath}/insights/script.js`);
|
||||
}
|
||||
return "/_vercel/insights/script.js";
|
||||
}
|
||||
function loadProps(explicitProps, confString) {
|
||||
var _a;
|
||||
let props = explicitProps;
|
||||
if (confString) {
|
||||
try {
|
||||
props = {
|
||||
...(_a = JSON.parse(confString)) == null ? void 0 : _a.analytics,
|
||||
...explicitProps
|
||||
};
|
||||
} catch {
|
||||
}
|
||||
}
|
||||
setMode(props.mode);
|
||||
const dataset = {
|
||||
sdkn: name + (props.framework ? `/${props.framework}` : ""),
|
||||
sdkv: version
|
||||
};
|
||||
if (props.disableAutoTrack) {
|
||||
dataset.disableAutoTrack = "1";
|
||||
}
|
||||
if (props.viewEndpoint) {
|
||||
dataset.viewEndpoint = makeAbsolute(props.viewEndpoint);
|
||||
}
|
||||
if (props.eventEndpoint) {
|
||||
dataset.eventEndpoint = makeAbsolute(props.eventEndpoint);
|
||||
}
|
||||
if (props.sessionEndpoint) {
|
||||
dataset.sessionEndpoint = makeAbsolute(props.sessionEndpoint);
|
||||
}
|
||||
if (isDevelopment() && props.debug === false) {
|
||||
dataset.debug = "false";
|
||||
}
|
||||
if (props.dsn) {
|
||||
dataset.dsn = props.dsn;
|
||||
}
|
||||
if (props.endpoint) {
|
||||
dataset.endpoint = props.endpoint;
|
||||
} else if (props.basePath) {
|
||||
dataset.endpoint = makeAbsolute(`${props.basePath}/insights`);
|
||||
}
|
||||
return {
|
||||
beforeSend: props.beforeSend,
|
||||
src: getScriptSrc(props),
|
||||
dataset
|
||||
};
|
||||
}
|
||||
function makeAbsolute(url) {
|
||||
return url.startsWith("http://") || url.startsWith("https://") || url.startsWith("/") ? url : `/${url}`;
|
||||
}
|
||||
|
||||
// src/generic.ts
|
||||
function inject(props = {
|
||||
debug: true
|
||||
}, confString) {
|
||||
var _a;
|
||||
if (!isBrowser()) return;
|
||||
const { beforeSend, src, dataset } = loadProps(props, confString);
|
||||
initQueue();
|
||||
if (beforeSend) {
|
||||
(_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", beforeSend);
|
||||
}
|
||||
if (document.head.querySelector(`script[src*="${src}"]`)) return;
|
||||
const script = document.createElement("script");
|
||||
script.src = src;
|
||||
for (const [key, value] of Object.entries(dataset)) {
|
||||
script.dataset[key] = value;
|
||||
}
|
||||
script.defer = true;
|
||||
script.onerror = () => {
|
||||
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.";
|
||||
console.log(
|
||||
`[Vercel Web Analytics] Failed to load script from ${src}. ${errorMessage}`
|
||||
);
|
||||
};
|
||||
document.head.appendChild(script);
|
||||
}
|
||||
function track(name2, properties, options) {
|
||||
var _a, _b;
|
||||
if (!isBrowser()) {
|
||||
const msg = "[Vercel Web Analytics] Please import `track` from `@vercel/analytics/server` when using this function in a server environment";
|
||||
if (isProduction()) {
|
||||
console.warn(msg);
|
||||
} else {
|
||||
throw new Error(msg);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!properties) {
|
||||
(_a = window.va) == null ? void 0 : _a.call(window, "event", { name: name2, options });
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const props = parseProperties(properties, {
|
||||
strip: isProduction()
|
||||
});
|
||||
(_b = window.va) == null ? void 0 : _b.call(window, "event", {
|
||||
name: name2,
|
||||
data: props,
|
||||
options
|
||||
});
|
||||
} catch (err) {
|
||||
if (err instanceof Error && isDevelopment()) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
function pageview({
|
||||
route,
|
||||
path
|
||||
}) {
|
||||
var _a;
|
||||
(_a = window.va) == null ? void 0 : _a.call(window, "pageview", { route, path });
|
||||
}
|
||||
|
||||
// src/react/utils.ts
|
||||
function getBasePath() {
|
||||
if (typeof process === "undefined" || typeof process.env === "undefined") {
|
||||
return void 0;
|
||||
}
|
||||
return process.env.REACT_APP_VERCEL_OBSERVABILITY_BASEPATH;
|
||||
}
|
||||
function getConfigString() {
|
||||
if (typeof process === "undefined" || typeof process.env === "undefined") {
|
||||
return void 0;
|
||||
}
|
||||
return process.env.REACT_APP_VERCEL_OBSERVABILITY_CLIENT_CONFIG;
|
||||
}
|
||||
|
||||
// src/react/index.tsx
|
||||
function Analytics(props) {
|
||||
useEffect(() => {
|
||||
var _a;
|
||||
if (props.beforeSend) {
|
||||
(_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
|
||||
}
|
||||
}, [props.beforeSend]);
|
||||
useEffect(() => {
|
||||
inject(
|
||||
{
|
||||
framework: props.framework || "react",
|
||||
basePath: props.basePath ?? getBasePath(),
|
||||
...props.route !== void 0 && { disableAutoTrack: true },
|
||||
...props
|
||||
},
|
||||
props.configString ?? getConfigString()
|
||||
);
|
||||
}, []);
|
||||
useEffect(() => {
|
||||
if (props.route && props.path) {
|
||||
pageview({ route: props.route, path: props.path });
|
||||
}
|
||||
}, [props.route, props.path]);
|
||||
return null;
|
||||
}
|
||||
export {
|
||||
Analytics,
|
||||
track
|
||||
};
|
||||
//# sourceMappingURL=index.mjs.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../src/react/index.tsx","../../src/queue.ts","../../package.json","../../src/utils.ts","../../src/generic.ts","../../src/react/utils.ts"],"sourcesContent":["'use client';\nimport { useEffect } from 'react';\nimport { inject, pageview, track } from '../generic';\nimport type { AnalyticsProps, BeforeSend, BeforeSendEvent } from '../types';\nimport { getBasePath, getConfigString } 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 * @example\n * ```js\n * import { Analytics } from '@vercel/analytics/react';\n *\n * export default function App() {\n * return (\n * <div>\n * <Analytics />\n * <h1>My App</h1>\n * </div>\n * );\n * }\n * ```\n */\nfunction Analytics(\n props: AnalyticsProps & {\n framework?: string;\n route?: string | null;\n path?: string | null;\n basePath?: string;\n configString?: string;\n },\n): null {\n useEffect(() => {\n if (props.beforeSend) {\n window.va?.('beforeSend', props.beforeSend);\n }\n }, [props.beforeSend]);\n\n // biome-ignore lint/correctness/useExhaustiveDependencies: only run once\n useEffect(() => {\n inject(\n {\n framework: props.framework || 'react',\n basePath: props.basePath ?? getBasePath(),\n ...(props.route !== undefined && { disableAutoTrack: true }),\n ...props,\n },\n props.configString ?? getConfigString(),\n );\n }, []);\n\n useEffect(() => {\n // explicitely track page view, since we disabled auto tracking\n if (props.route && props.path) {\n pageview({ route: props.route, path: props.path });\n }\n }, [props.route, props.path]);\n\n return null;\n}\n\nexport { track, Analytics };\nexport type { AnalyticsProps, BeforeSend, BeforeSendEvent };\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 if (!window.vaq) window.vaq = [];\n window.vaq.push(params);\n };\n};\n","{\n \"name\": \"@vercel/analytics\",\n \"version\": \"2.0.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\": \"MIT\",\n \"exports\": {\n \"./package.json\": \"./package.json\",\n \".\": {\n \"browser\": \"./dist/index.mjs\",\n \"import\": \"./dist/index.mjs\",\n \"require\": \"./dist/index.js\"\n },\n \"./astro\": {\n \"import\": \"./dist/astro/component.ts\"\n },\n \"./next\": {\n \"browser\": \"./dist/next/index.mjs\",\n \"import\": \"./dist/next/index.mjs\",\n \"require\": \"./dist/next/index.js\"\n },\n \"./nuxt\": {\n \"import\": \"./dist/nuxt/index.mjs\",\n \"require\": \"./dist/nuxt/index.js\"\n },\n \"./nuxt/runtime\": {\n \"browser\": \"./dist/nuxt/runtime/index.mjs\",\n \"import\": \"./dist/nuxt/runtime/index.mjs\",\n \"require\": \"./dist/nuxt/runtime/index.js\"\n },\n \"./react\": {\n \"browser\": \"./dist/react/index.mjs\",\n \"import\": \"./dist/react/index.mjs\",\n \"require\": \"./dist/react/index.js\"\n },\n \"./remix\": {\n \"browser\": \"./dist/remix/index.mjs\",\n \"import\": \"./dist/remix/index.mjs\",\n \"require\": \"./dist/remix/index.js\"\n },\n \"./server\": {\n \"node\": \"./dist/server/index.mjs\",\n \"edge-light\": \"./dist/server/index.mjs\",\n \"import\": \"./dist/server/index.mjs\",\n \"require\": \"./dist/server/index.js\",\n \"default\": \"./dist/server/index.js\"\n },\n \"./sveltekit\": {\n \"svelte\": \"./dist/sveltekit/index.mjs\",\n \"types\": \"./dist/sveltekit/index.d.ts\"\n },\n \"./vue\": {\n \"browser\": \"./dist/vue/index.mjs\",\n \"import\": \"./dist/vue/index.mjs\",\n \"require\": \"./dist/vue/index.js\"\n }\n },\n \"main\": \"./dist/index.mjs\",\n \"types\": \"./dist/index.d.ts\",\n \"typesVersions\": {\n \"*\": {\n \"*\": [\n \"dist/index.d.ts\"\n ],\n Line truncated
|
||||
Reference in new issue
Block a user