Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.
TypeScript analyticsaudience-segmentationchartscohort-analysisgoogle-analyticsproduct-analyticsstatisticsuser-journeyweb-analytics
Enabling caching in tracker script.
Mike Cao7cf0927741f0
diff --git a/lib/session.js b/lib/session.jsindex f91545f34..73d02e590 100644--- a/lib/session.js+++ b/lib/session.js@@ -9,7 +9,8 @@ export async function getSession(req) {throw new Error('Invalid request');}- const { website: website_uuid, hostname, screen, language, cache } = payload;+ const { website: website_uuid, hostname, screen, language } = payload;+ const cache = req.headers['x-umami-cache'];if (cache) {const result = await parseToken(cache);diff --git a/tracker/index.js b/tracker/index.jsindex a5012b718..d7ccb86fc 100644--- a/tracker/index.js+++ b/tracker/index.js@@ -7,7 +7,6 @@ import { removeTrailingSlash } from '../lib/url';navigator: { language },location: { hostname, pathname, search },localStorage,- sessionStorage,document,history,} = window;@@ -21,14 +20,12 @@ import { removeTrailingSlash } from '../lib/url';const hostUrl = attr('data-host-url');const autoTrack = attr('data-auto-track') !== 'false';const dnt = attr('data-do-not-track');- const useCache = attr('data-cache');const cssEvents = attr('data-css-events') !== 'false';const domain = attr('data-domains') || '';const domains = domain.split(',').map(n => n.trim());const eventClass = /^umami--([a-z]+)--([\w]+[\w-]*)$/;const eventSelect = "[class*='umami--']";- const cacheKey = 'umami.cache';const trackingDisabled = () =>(localStorage && localStorage.getItem('umami.disabled')) ||@@ -42,6 +39,7 @@ import { removeTrailingSlash } from '../lib/url';const listeners = {};let currentUrl = `${pathname}${search}`;let currentRef = document.referrer;+ let cache;/* Collect metrics */@@ -49,6 +47,7 @@ import { removeTrailingSlash } from '../lib/url';const req = new XMLHttpRequest();req.open('POST', url, true);req.setRequestHeader('Content-Type', 'application/json');+ if (cache) req.setRequestHeader('x-umami-cache', cache);req.onreadystatechange = () => {if (req.readyState === 4) {@@ -64,7 +63,6 @@ import { removeTrailingSlash } from '../lib/url';hostname,screen,language,- cache: useCache && sessionStorage.getItem(cacheKey),url: currentUrl,});@@ -84,7 +82,7 @@ import { removeTrailingSlash } from '../lib/url';type,payload,},- res => useCache && sessionStorage.setItem(cacheKey, res),+ res => (cache = res),);};