Control consent from your frontend code with the JavaScript API.
CookieHub’s browser-side JavaScript API lets you read a visitor’s consent state, respond the moment it changes and open or close the CookieHub consent UI from your own code — all through window.cookiehub.
- Consent state
- Consent events
- Programmatic UI control
Consent your frontend code can read and react to.
Once the CookieHub tag has initialized on a page, the JavaScript API is available on window.cookiehub. Your own scripts can use it to check what a visitor has agreed to before anything runs, and to respond as soon as that choice changes — without a page reload.
The API has two parts: public methods, which you call to query consent or control the consent UI, and events, which CookieHub fires when it has loaded and whenever a visitor allows, revokes or updates their choice.
Inspect consent state
Check whether a visitor has made a choice and which categories they allowed, with hasAnswered() and hasConsented(category).
Run scripts conditionally
Start analytics, marketing or other non-essential code only once the visitor has allowed the category it belongs to.
React to consent changes
Use onAllow, onRevoke and onStatusChange to start or stop services as soon as a visitor updates their choice.
Control the consent UI
Open the consent dialog or the preference center from your own buttons and links, and close them again from code.
Respond when consent is loaded, given or changed.
CookieHub fires an event at each point in the consent process. Add callbacks to the cpm object in the default CookieHub code or, from version 2.8.13, listen for the same events on document with addEventListener:
- onInitialise (cookiehub_onInitialise) fires when CookieHub has finished loading, with the visitor’s initial consent status
- onAllow (cookiehub_onAllow) fires when a visitor allows a category that was previously not allowed
- onRevoke (cookiehub_onRevoke) fires when a visitor revokes a category they had allowed
- onStatusChange (cookiehub_onStatusChange) fires when a visitor changes their preferences, for example by saving settings or allowing all
Callbacks receive the category or status as arguments. Browser events carry the same data in event.detail: detail.category, or detail.status and detail.previousStatus.
Read the events referenceQuery consent and control the consent UI from code.
Public methods on window.cookiehub let your code ask about consent and drive the CookieHub interface. The ones you’ll use most:
- isReady(), hasAnswered() and hasConsented(category) check whether CookieHub has initialized, whether the visitor has made a choice and whether a category is allowed
- openDialog() and closeDialog() open and close the consent dialog
- openSettings() and closeSettings() open and close the preference center; openSettings(tab) opens it on a tab such as declaration or vendors
- allowAll() and denyAll() record a choice across all categories, while allow(category) and deny(category) apply it to a single category
allowAll(), denyAll(), allow() and deny() are there for consent controls you build yourself, such as your own buttons: they carry out a choice the visitor makes, not a choice made on their behalf. The API reference lists every method, including openPersonalData() and isSellingAllowed(), and the CookieHub version each one needs.
View all methodsCommon ways to use the JavaScript API.
Short examples for the scenarios that come up most often. Each uses only the documented methods and events, so you can adapt it to your own code.
Load analytics only after consent
Add callbacks to the cpm object in your CookieHub code: onInitialise covers visitors who have already allowed analytics, and onAllow covers visitors who allow it later. Consent events can fire more than once on a page, for example when a visitor revokes analytics and then allows it again, so have loadAnalytics() check whether the script is already loaded to avoid adding it twice.
analytics.jsExamplevar cpm = {onInitialise: function (status) {if (this.hasConsented('analytics')) {loadAnalytics();}},onAllow: function (category) {if (category === 'analytics') {loadAnalytics();}},};React immediately when preferences change
Listen for the browser event from any script on the page. When a visitor revokes marketing, run your own clean-up for the services that depend on it.
consent-events.jsExampledocument.addEventListener('cookiehub_onRevoke', function (event) {if (event.detail.category === 'marketing') {stopMarketingTags();}});Add a cookie settings link to your footer
Give visitors a permanent way back to their choices by opening the preference center from a link in your own footer — useful if you have hidden CookieHub’s settings icon.
footer.jsExamplevar link = document.getElementById('cookie-settings');link.addEventListener('click', function (event) {event.preventDefault();window.cookiehub.openSettings();});Keep your app’s UI in sync with consent
Update your interface when CookieHub loads and whenever consent changes, for example by toggling a class that shows or hides features that depend on preference consent.
consent-ui.jsExamplefunction syncConsentUi() {var allowed = window.cookiehub.hasConsented('preferences');document.body.classList.toggle('preferences-allowed', allowed);}document.addEventListener('cookiehub_onInitialise', syncConsentUi);document.addEventListener('cookiehub_onStatusChange', syncConsentUi);
Use the API with your existing CookieHub code.
There’s no separate package to install. The JavaScript API is part of the standard CookieHub code: add CookieHub to your site as usual, and the API is exposed on window.cookiehub once it has initialized.
- 01
Add the CookieHub code to your site
In the CookieHub dashboard, open your domain’s Implementation tab and add the code at the top of the <head> on every page.
- 02
Add callbacks to the cpm object
The default code passes a cpm object to window.cookiehub.load(). Add onInitialise, onAllow, onRevoke or onStatusChange to it. load() runs once per page; later calls are ignored.
window.cookiehub.load(cpm) - 03
Or listen for browser events
From version 2.8.13, CookieHub also emits each event on document, so scripts that load separately can listen with addEventListener.
cookiehub_onStatusChange - 04
Call methods once CookieHub is ready
Use isReady() to check whether CookieHub has finished initializing, or run your code from onInitialise so it starts once the visitor’s consent status is known.
window.cookiehub.isReady()
JavaScript API questions, answered.
Through the global window.cookiehub object. It becomes available once the CookieHub code has loaded on the page and initialized; isReady() tells you whether initialization has finished.
Call window.cookiehub.hasConsented(category) with the category’s name or ID, such as 'analytics' or 'marketing'. It returns whether the visitor has allowed that category. hasAnswered() tells you whether they have made a choice at all.
Use the events. onAllow and onRevoke fire when a single category is allowed or revoked, and onStatusChange fires when the visitor changes their preferences, for example by saving settings or allowing all. Add them as callbacks to the cpm object, or listen for cookiehub_onAllow, cookiehub_onRevoke and cookiehub_onStatusChange on document (version 2.8.13 and later).
Yes. Call window.cookiehub.openSettings() to open the preference center, or openDialog() to open the consent dialog. openSettings(tab) opens a specific tab, such as declaration for the cookie declaration. This is useful if you hide CookieHub’s settings icon and link to the settings from your footer instead.
Yes. allow(category) and deny(category) allow or deny a single category or purpose — preferences, analytics, marketing, uncategorized or sale-and-share — and allowAll() and denyAll() cover every category at once. They exist so you can build your own consent controls: use them to carry out a choice the visitor makes, not to override one. allow() and deny() need CookieHub version 2.8.10 or later.
Use a method when your code needs to ask or do something now: check consent with hasConsented(), or open the preference center with openSettings(). Use an event when your code needs to wait for something to happen: CookieHub finishing loading, or a visitor allowing, revoking or updating their consent.
Yes. Listen for cookiehub_onStatusChange, which fires whenever a visitor changes their preferences, and push the new consent state to the data layer so your tags can respond straight away. If you install CookieHub through Google Tag Manager with the CookieHub CMP template, the template also pushes data layer events you can use as triggers.
Yes. Add <div class="cookiehub-declaration"></div> where you want it, for example on your cookie policy page, and CookieHub renders the declaration there as an unstyled table that takes on your site’s CSS. To show it inside the preference center instead, call window.cookiehub.openSettings('declaration').
No. The JavaScript API runs in the visitor’s browser and works with consent on the page. The Management API is a REST API for backend configuration and automation, such as working with domains and fetching consent log data, and is available to Enterprise customers.
Build consent into your frontend.
Add CookieHub to your site, then use the JavaScript API to read consent, respond to changes and give visitors your own way back to their settings.