The CookieHub Javascript API provides a flexible set of public methods and events that enable Front-End Developers to interact with and control the CMP widget directly on the website. This API is essential for ensuring robust compliance by allowing you to manually verify a user's consent status before executing any third-party marketing, analytics, or functional scripts.
All functionality is accessed via the global window.cookiehub object, which becomes available once the CookieHub tag is initialized.
Using the API ensures your website is compliant, dynamic, and offers a smooth user experience.
Conditional Script Loading
Use the hasConsented() method to load third-party tags (e.g., Google Analytics, Facebook Pixel) only if the user has provided the required consent category.
Real-Time Integration
Utilize events like cookiehub_onStatusChange to trigger scripts or update your data layer the moment a user changes their preferences, without requiring a page refresh.
Custom User Experience
Create custom buttons or links that programmatically open the consent dialog (openDialog()) or settings panel (openSettings()) from anywhere on your site.
Developer Flexibility
Provides simple methods like allowAll() or denyAll() for advanced integration scenarios where you need manual control over consent states.
The Javascript API functionality is separated into two main groups: Public Methods (for querying status and control) and Events (for reacting to changes).
To ensure the API object is available, the standard CookieHub tag must be injected into your website's <head>:
Installation Guide: Information on the script to inject - CookieHub Tag
These methods allow you to actively check the user's consent status or control the appearance of the dialog.
hasConsented(category)
Purpose: The most critical method. Checks if the user has allowed the specified cookie category (e.g., "analytics", "marketing").
Example: if (window.cookiehub.hasConsented("analytics")) { loadAnalytics(); }
hasAnswered()
Purpose: Returns true if the user has previously made a choice by allowing all or saving settings.
openSettings(tab)
Purpose: Opens the settings dialog. You can optionally specify a tab (e.g., "declaration").
Example: window.cookiehub.openSettings('declaration');
openDialog()
Purpose: Programmatically opens the main consent dialog.
allowAll() / denyAll()
Purpose: Programmatically grants or revokes consent for all categories. Useful for implementing custom controls outside the main dialog.
Events are crucial for running code after the user interacts with the CMP. Listen for these events using the standard document.addEventListener.
cookiehub_onInitialise
Trigger: Fired when CookieHub has loaded and the user's existing consent status is known.
Key Use Case: Executing initial page logic that depends on prior consent.
cookiehub_onStatusChange
Trigger: Fired whenever the user modifies their cookie preferences (e.g., clicks "Save Settings" or "Allow All").
Key Use Case: The most reliable event for re-firing GTM or scripts after consent is updated.
cookiehub_onAllow
Trigger: Fired when a user specifically allows a category that was previously disallowed.
Key Use Case: Loading a single, category-dependent script at the moment of consent.
cookiehub_onRevoke
Trigger: Fired when a user specifically revokes consent for a previously allowed category.
Key Use Case: Stopping execution of a specific tracking script.
All public methods and properties are available under the global object, window.cookiehub, once the main CookieHub tag has loaded.
Yes, absolutely. If you disable the default floating icon in the CookieHub dashboard, you can create a custom link or button anywhere on your site (e.g., in the footer) that triggers the openDialog() public method.
Yes. The full cookie declaration can be embedded directly onto any page (such as your dedicated Cookie Policy page) using a simple HTML snippet. This method renders the declaration as an unstyled table which inherits your website's CSS, ensuring visual consistency. How to Embed: Insert the following HTML snippet exactly where you want the declaration to appear: <div class="cookiehub-declaration"></div> Note: If you want users to view the declaration inside the CMP dialog overlay, you can still use the window.cookiehub.openSettings('declaration'); method.
Use a Public Method (like hasConsented()) when your code needs to query a status or control the dialog (an active operation). Use an Event (like cookiehub_onStatusChange) when your code needs to react to a user action (a passive operation) or an initialization moment.
No. The API methods like allowAll() or denyAll() are designed to provide alternative ways for the user to express consent (e.g., via a custom button). They should not be used to bypass the user's explicit choice or the CMP workflow.
The cookiehub_onStatusChange event is the most reliable event for GTM integration. It fires whenever the user modifies their cookie preferences, allowing you to update your GTM Data Layer immediately to reflect the new consent state.
©2018-2025 CookieHub ehf.
CookieHub CMP offers tools and services for managing cookies and online privacy.

