Queue events and send them after identify #4050
Replies: 1 comment
-
|
Umami doesn't have a built-in event queue, but you can work around this by deferring tracking initialization until after your user API resolves. Approach 1: Disable auto-track and manually send events Add <script src="https://your-umami.com/script.js" data-website-id="xxx" data-auto-track="false" async></script>async function trackWithIdentity() {
const user = await fetchUser();
// Identify first
umami.identify({ userId: user.id });
// Then send the pageview
umami.track();
}
trackWithIdentity();Approach 2: Delay the tracker script loading entirely Don't include the Umami script tag in your HTML. Instead, inject it dynamically after identify is ready: async function initUmami() {
// Wait for your user API
const user = await fetchUser();
// Load the Umami script dynamically
const script = document.createElement("script");
script.src = "https://your-umami.com/script.js";
script.dataset.websiteId = "your-website-id";
script.async = true;
// Once loaded, immediately identify
script.onload = () => {
if (window.umami) {
umami.identify({ userId: user.id, email: user.email });
}
};
document.head.appendChild(script);
}
initUmami();This way the initial pageview fires only after the script loads, which is after your user data is available. Approach 1 is probably cleaner since it gives you full control over the order of operations without needing to dynamically load scripts. The key is |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
When the Umami script runs, it triggers a view event on the first page, but my user API is still pending, and I haven't called the identify event for Umami yet. As a result, some events are being wasted.
Do we have a queue function that can hold the events and send them after the identify event has been called?
Beta Was this translation helpful? Give feedback.
All reactions