Analytics: queued events, once-per-order revenue, heatmap on product pages
build / build (pull_request) Successful in 1m10s
build / build (pull_request) Successful in 1m10s
trackEvent queues events until the deferred Umami script exists (they were dropped when fired in onMounted) and drops them silently after 10 s. The result page sends purchase-completed with revenue/currency once per order (localStorage guard, set only after sending). The plugin honours Do Not Track and loads recorder.js only on /details/* for Umami heatmaps. bun test utils/trackEvent.test.ts: 4 pass. Netcup #115.
This commit is contained in:
4 files changed
+145
-14
No files matched your search
+31
-11
@@ -1,15 +1,35 @@
|
||||
export default defineNuxtPlugin(() => {
|
||||
const config = useRuntimeConfig();
|
||||
const scriptUrl = config.public.umamiScriptUrl as string;
|
||||
const websiteId = config.public.umamiWebsiteId as string;
|
||||
if (!scriptUrl || !websiteId) return;
|
||||
|
||||
if (config.public.umamiScriptUrl && config.public.umamiWebsiteId) {
|
||||
useHead({
|
||||
script: [
|
||||
{
|
||||
src: config.public.umamiScriptUrl,
|
||||
defer: true,
|
||||
"data-website-id": config.public.umamiWebsiteId
|
||||
}
|
||||
]
|
||||
});
|
||||
}
|
||||
useHead({
|
||||
script: [
|
||||
{
|
||||
src: scriptUrl,
|
||||
defer: true,
|
||||
"data-website-id": websiteId,
|
||||
"data-do-not-track": "true"
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
// Heatmap recorder only on product pages (no personal data there). It needs
|
||||
// heatmapEnabled on the Umami website, set via umami/analytics/reports.json.
|
||||
const recorderUrl = scriptUrl.replace(/script\.js(\?.*)?$/, "recorder.js");
|
||||
let recorderLoaded = false;
|
||||
const loadRecorder = (path: string) => {
|
||||
if (recorderLoaded || !path.startsWith("/details/")) return;
|
||||
if (navigator.doNotTrack === "1") return;
|
||||
recorderLoaded = true;
|
||||
const el = document.createElement("script");
|
||||
el.src = recorderUrl;
|
||||
el.defer = true;
|
||||
el.dataset.websiteId = websiteId;
|
||||
document.head.appendChild(el);
|
||||
};
|
||||
const router = useRouter();
|
||||
loadRecorder(router.currentRoute.value.path);
|
||||
router.afterEach((to) => loadRecorder(to.path));
|
||||
});
|
||||
Reference in new issue
Block a user