UNPKG

@vercel/analytics

Version:

Gain real-time traffic insights with Vercel Web Analytics

237 lines (231 loc) 6.03 kB
// src/sveltekit/index.ts import { browser } from "$app/environment"; import { page } from "$app/stores"; // 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/sveltekit/utils.ts function getBasePath() { try { return import.meta.env.VITE_VERCEL_OBSERVABILITY_BASEPATH; } catch { } } function getConfigString() { try { return import.meta.env.VITE_VERCEL_OBSERVABILITY_CLIENT_CONFIG; } catch { } } // src/sveltekit/index.ts function injectAnalytics(props = {}) { if (browser) { inject( { ...props, basePath: getBasePath(), disableAutoTrack: true, framework: "sveltekit" }, getConfigString() ); page.subscribe(({ route, url }) => { if (route == null ? void 0 : route.id) { pageview({ route: route.id, path: url.pathname }); } }); } } export { injectAnalytics, track }; //# sourceMappingURL=index.mjs.map