@nuxtjs/web-vitals
Version:
Web Vitals for Nuxt.js
26 lines (25 loc) • 714 B
JavaScript
import { logError, logDebug, getConnectionSpeed } from "../utils.mjs";
export function sendToAnalytics({ fullPath, href }, metric, options) {
if (!window.dataLayer) {
logError("window.dataLayer is undefined, probably GTM is not installed");
return;
}
const event = {
event: "webVitals",
webVitalsMeasurement: {
page: fullPath,
href,
name: metric.name,
id: metric.id,
value: metric.value,
delta: metric.delta,
valueRounded: metric.valueRounded,
deltaRounded: metric.deltaRounded,
speed: getConnectionSpeed()
}
};
if (options.debug) {
logDebug(metric.name, JSON.stringify(event, null, 2));
}
window.dataLayer.push(event);
}