@vercel/analytics
Version:
Gain real-time traffic insights with Vercel Web Analytics
184 lines (177 loc) • 4.82 kB
JavaScript
// src/vue/create-component.ts
import { defineComponent, watch } from "vue";
import { useRoute } from "vue-router";
// package.json
var name = "@vercel/analytics";
var version = "1.5.0";
// src/queue.ts
var initQueue = () => {
if (window.va) return;
window.va = function a(...params) {
(window.vaq = window.vaq || []).push(params);
};
};
// 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 (e) {
}
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 isDevelopment() {
return getMode() === "development";
}
function computeRoute(pathname, pathParams) {
if (!pathname || !pathParams) {
return pathname;
}
let result = pathname;
try {
const entries = Object.entries(pathParams);
for (const [key, value] of entries) {
if (!Array.isArray(value)) {
const matcher = turnValueToRegExp(value);
if (matcher.test(result)) {
result = result.replace(matcher, `/[${key}]`);
}
}
}
for (const [key, value] of entries) {
if (Array.isArray(value)) {
const matcher = turnValueToRegExp(value.join("/"));
if (matcher.test(result)) {
result = result.replace(matcher, `/[...${key}]`);
}
}
}
return result;
} catch (e) {
return pathname;
}
}
function turnValueToRegExp(value) {
return new RegExp(`/${escapeRegExp(value)}(?=[/?#]|$)`);
}
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function getScriptSrc(props) {
if (props.scriptSrc) {
return props.scriptSrc;
}
if (isDevelopment()) {
return "https://va.vercel-scripts.com/v1/script.debug.js";
}
if (props.basePath) {
return `${props.basePath}/insights/script.js`;
}
return "/_vercel/insights/script.js";
}
// src/generic.ts
function inject(props = {
debug: true
}) {
var _a;
if (!isBrowser()) return;
setMode(props.mode);
initQueue();
if (props.beforeSend) {
(_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
}
const src = getScriptSrc(props);
if (document.head.querySelector(`script[src*="${src}"]`)) return;
const script = document.createElement("script");
script.src = src;
script.defer = true;
script.dataset.sdkn = name + (props.framework ? `/${props.framework}` : "");
script.dataset.sdkv = version;
if (props.disableAutoTrack) {
script.dataset.disableAutoTrack = "1";
}
if (props.endpoint) {
script.dataset.endpoint = props.endpoint;
} else if (props.basePath) {
script.dataset.endpoint = `${props.basePath}/insights`;
}
if (props.dsn) {
script.dataset.dsn = props.dsn;
}
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}`
);
};
if (isDevelopment() && props.debug === false) {
script.dataset.debug = "false";
}
document.head.appendChild(script);
}
function pageview({
route,
path
}) {
var _a;
(_a = window.va) == null ? void 0 : _a.call(window, "pageview", { route, path });
}
// src/vue/utils.ts
function getBasePath() {
try {
return import.meta.env.VITE_VERCEL_OBSERVABILITY_BASEPATH;
} catch {
}
}
// src/vue/create-component.ts
function createComponent(framework = "vue") {
return defineComponent({
props: ["dsn", "beforeSend", "debug", "scriptSrc", "endpoint", "mode"],
setup(props) {
const route = useRoute();
inject({
...props,
basePath: getBasePath(),
// keep auto-tracking unless we have route support (Nuxt or vue-router).
disableAutoTrack: Boolean(route),
framework
});
if (route && typeof window !== "undefined") {
const changeRoute = () => {
pageview({
route: computeRoute(route.path, route.params),
path: route.path
});
};
changeRoute();
watch(route, changeRoute);
}
},
// Vue component must have a render function, or a template.
render() {
return null;
}
});
}
// src/vue/index.ts
var Analytics = createComponent();
export {
Analytics
};
//# sourceMappingURL=index.mjs.map