CookieHub Logo

Javascript API: Frontend Control and Compliant Script Integration

Extend CookieHub's functionality, conditionally load third-party scripts based on user consent, and manage the dialog experience directly from your client-side code.

Understanding the Javascript API

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.

Why Use the Javascript API?

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.

How It Works

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

Start Integrating Your CMP

The Javascript API is the definitive way to achieve compliant, granular control over your website's third-party integrations.

Frequently Asked Questions

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.