Custom Website
Advanced Consentio implementation for developers
Basic Implementation
For the basic implementation, paste one line of code right after the opening <head> tag, before any Google tag:
<script src="https://consentio.cz/widget/YOUR_SCRIPT_KEY/consentio.min.js" async></script>
The widget automatically initialises and displays the cookie bar on the first page load.
You can find the code with your unique SCRIPT_KEY in the dashboard under My Websites → your website → Installation.
lang attribute on the <html>
element, within the languages you have enabled (see Languages).
JavaScript API
Once the widget has loaded, the global window.Consentio object is available for programmatic control.
The widget loads asynchronously, so code that runs while the page is loading cannot count on the object being there yet. The examples below show how to wait for it.
Available Methods
| Method | Description |
|---|---|
show() |
Displays the cookie banner |
hide() |
Hides the cookie banner |
showPreferences() |
Displays the preferences settings |
getConsent() |
Returns the visitor's choice, or null until the visitor has chosen |
updateConsent(updates) |
Changes a choice the visitor has already made. Does nothing before the first choice; withdrawing a category reloads the page |
onConsentChange(callback) |
Calls the callback after every choice and once on each page load when a choice is already stored. A callback registered later is called once with the current choice |
getBlockedScripts() |
Returns an object with the number of scripts still held back per category (marked ones and those held automatically), e.g. { marketing: 2 } |
getBlockedIframes() |
Returns the same kind of object for marked iframes |
Displaying the Banner
// Call these once the widget has loaded, for example from a click handler.
// window.Consentio?. skips the call when the widget is not there yet.
// Show the cookie banner
window.Consentio?.show()
// Hide the cookie banner
window.Consentio?.hide()
// Show the preferences settings
window.Consentio?.showPreferences()
Getting Consent State
// The visitor's choice, or null until they have chosen
const consent = window.Consentio?.getConsent()
// { necessary: true, functionality: false, analytics: false, marketing: false }
// Always allow for null before reading a category
if (consent?.analytics) {
// Analytics is allowed
}
if (consent?.marketing) {
// Marketing is allowed
}
// To load a script when consent arrives, use onConsentChange (below)
Updating Consent
// Changes a choice the visitor has already made; before the first choice it does nothing.
// Call it only from the visitor's own action (for example your own switch):
// it is recorded as their choice.
window.Consentio?.updateConsent({
analytics: true,
marketing: false
})
// Switching off a category that was on reloads the page
Event Listeners
Both options fire after every choice and once on each page load when a choice is already stored. They can fire more than once on a page, so let a loader run only once, and never reload the page from them.
// Option 1: onConsentChange. Works from any code, including code that runs late
// (a bundle, a framework hook): a callback registered late is called once
// with the current choice. The widget loads asynchronously, so wait for it:
function onConsent(callback) {
if (window.Consentio) {
Consentio.onConsentChange(callback)
} else {
window.addEventListener('consentio:consent', (event) => callback(event.detail))
}
}
let analyticsLoaded = false
onConsent((consent) => {
if (consent.analytics && !analyticsLoaded) {
analyticsLoaded = true
loadGoogleAnalytics()
}
})
// Option 2: window event. It is not replayed, so register the listener in an
// inline script in the page HTML, before the page finishes loading.
window.addEventListener('consentio:consent', (event) => {
const consent = event.detail
console.log('Consent:', consent)
})
Google Consent Mode v2
Consentio sends the Google Consent Mode v2 signals for you. The one condition is that its code sits in the page before Google Analytics and other Google tags: its first part sets everything to denied before Google reads it.
Basic or Advanced?
Consentio sends Google the consent signals on every plan. Which mode your Google scripts run in is decided by how you embed them:
- Advanced (standard installation, example below) — gtag.js loads immediately and, before consent, sends Google anonymous cookieless pings that GA4 uses to model conversions. Under ePrivacy this is a grey zone: data leaves the browser before the visitor agreed.
- Basic (blocked script) — embed gtag.js as
<script type="text/plain" data-category="analytics">. It loads only after consent and Google receives nothing before that. The safest option; you only lose modelled data.
We recommend Basic. Choose Advanced deliberately, if you genuinely need modelled data in GA4 and Google Ads.
<!-- 1. Consentio code first, above every Google tag (no async) -->
<script src="https://consentio.cz/widget/YOUR_SCRIPT_KEY/consentio.min.js"></script>
<!-- 2. Then the Google tag -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXX');
</script>
Category to Consent Mode Mapping
| Consentio Category | Google Consent Mode |
|---|---|
| necessary | none (security_storage is always granted) |
| analytics | analytics_storage |
| marketing | ad_storage, ad_user_data, ad_personalization |
| functionality | functionality_storage, personalization_storage |
Automatic blocking of known services
Consentio recognises the scripts of known tracking and advertising services by their address and holds them by itself until the visitor allows their category. You do not have to change anything in your site code. This also covers scripts that a tag manager or other code adds to the page later.
- Analytics: Hotjar, Microsoft Clarity, Smartlook, Mouseflow, FullStory, Mixpanel, Amplitude, Heap, Segment, Crazy Egg, Lucky Orange, Yandex Metrica
- Marketing: Meta Pixel, TikTok Pixel, LinkedIn Insight Tag, Microsoft Advertising (UET), Pinterest Tag, X (Twitter) Pixel, Snapchat Pixel, Reddit Pixel, Quora Pixel, Criteo, Taboola, Outbrain, AdRoll, Adform, RTB House, HubSpot, Klaviyo, Mailchimp, Sklik, Heureka (conversion tracking), Glami Pixel, Leady
Google tags (Google Analytics, Google Ads, Tag Manager) are not blocked: they stay loaded and follow the Consent Mode signals. Consentio does not hold chat widgets, embedded videos or maps by itself, because that would change what the visitor sees on the page; mark those by hand as described below.
async as the first script in <head>.
Exception for one service: add data-category="necessary" to its tag and Consentio lets it run.
Switching it off for the whole site: in the dashboard, Designer, Behavior section, the switch “Block known tracking scripts automatically”.
Script Blocking
To block scripts before consent is granted, use the type="text/plain" attribute
with the data-category data attribute:
<!-- Script runs only after analytics consent is granted -->
<script type="text/plain" data-category="analytics">
// Google Analytics code
gtag('config', 'G-XXXXXXX');
</script>
<!-- Script runs only after marketing consent is granted -->
<script type="text/plain" data-category="marketing">
// Facebook Pixel code
fbq('init', 'XXXXXXX');
</script>
<!-- Script runs only after functionality consent is granted -->
<script type="text/plain" data-category="functionality">
// Personalisation code
initPersonalization();
</script>
Available Categories
| Category | Description |
|---|---|
necessary |
Necessary cookies (always permitted) |
analytics |
Analytics cookies |
marketing |
Marketing cookies |
functionality |
Functional cookies for personalisation |
Button to Open Preferences
The floating button for changing the choice is always shown. You can also add your own link or button that opens the preferences:
<!-- In the website footer -->
<a href="#" onclick="Consentio.showPreferences(); return false;">
Cookie Settings
</a>
<!-- Or as a button -->
<button onclick="Consentio.showPreferences()">
Manage Cookie Preferences
</button>
Single Page Applications (SPA)
The widget also works in React, Vue, Angular and other SPA frameworks. It detects URL changes, sends the consent state to Google again and runs newly added marked scripts whose category is allowed.
// Example: Checking consent state on route change (Vue Router)
router.afterEach(() => {
// null until the visitor has chosen
const consent = window.Consentio?.getConsent()
if (consent?.analytics) {
// Analytics scripts are permitted
}
})