Connect your cookie banner
Unagi works in two modes, and your cookie banner decides which one a visitor is in.
onclick="Unagi.openConsent()"). Every choice goes into the consent log with the wording the visitor saw, and the dashboard shows your acceptance rate, counted without any identifier. It keeps out of the way if another banner tool is on the page. It does not support the IAB TCF standard or Google Consent Mode, so if you run Google Ads or AdSense in the EEA you may need a Google-certified banner instead.data-consent="auto". It reads the common cookie banners (Cookiebot, OneTrust, CookieYes, Complianz, Osano, Termly, Shopify's privacy settings and Google Consent Mode) and switches tracking on only when the visitor has accepted analytics, and off again if they withdraw. If your banner is not one of those, or you have no banner, nothing is switched on automatically: use the manual calls below.Without consent (the default)
- Only anonymous page views are sent: the page path, the time, and the referrer's hostname.
- No visitor ID and no session. Nothing is written to the visitor's browser.
- The server also refuses any other event type from a visitor without consent, even if a modified script sent it.
This gives you honest page-view counts for everyone, but no journeys, findings about carts, or visitor profiles.
With consent
When a visitor accepts analytics in your banner, call:
Unagi.consent(true);
Unagi then stores a random visitor ID in the browser (localStorage key unagi_vid) and a session ID (sessionStorage key unagi_sid), and starts recording behaviour. It also re-sends the current page as a consented view, so the visitor's journey is complete. If they decline, call:
Unagi.consent(false);
That clears any ID Unagi stored and stops recording. Unagi remembers the choice in localStorage (unagi_consent) so it is applied on later visits.
Wiring it to your banner
Every consent tool offers a callback or event when a visitor makes a choice. Check yours for its exact name, and call Unagi.consent from it. The general pattern:
// when your banner reports "analytics accepted":
window.Unagi && Unagi.consent(true);
// when it reports "declined" or consent is withdrawn:
window.Unagi && Unagi.consent(false);
The script loads asynchronously, so also apply the saved choice when the page loads, once window.Unagi exists:
window.addEventListener('load', function () {
if (yourBannerSaysAnalyticsAccepted()) Unagi.consent(true);
});
If you only output the Unagi tag after consent has been given, add data-consent="granted" to the tag instead.
Browsers that opt out
Visitors whose browser sends Global Privacy Control or Do Not Track are always treated as not having consented, whatever your banner says.