@nuxtjs/web-vitals
Version:
Web Vitals for Nuxt.js
39 lines (38 loc) • 819 B
JavaScript
export const KEY = "ga:user";
export const UID = _getUID();
function _getUID() {
let uid;
try {
uid = localStorage[KEY];
} catch {
}
if (!uid) {
uid = Math.random() + "." + Math.random();
try {
localStorage[KEY] = uid;
} catch {
}
}
return uid;
}
export function logError(err) {
console.error("[nuxt vitals]", err);
}
export function logDebug(label, ...args) {
console.log(label, ...args);
}
export function getConnectionSpeed() {
return typeof navigator !== "undefined" && navigator.connection && navigator.connection.effectiveType || "";
}
export function send(url, body) {
if (navigator.sendBeacon) {
navigator.sendBeacon(url, body);
return;
}
fetch(url, {
body,
method: "POST",
credentials: "omit",
keepalive: true
}).catch(logError);
}