Javascript API
CookieHub provides a flexible JavaScript API that allows you to extend its functionality and integrate it seamlessly with your website. This API includes a set of events and methods to help you customize user experiences related to cookie consent. Here's a comprehensive guide to using the CookieHub JavaScript API.
Events
CookieHub events are triggered at different points in the consent process, allowing you to take specific actions in response. Here's a list of available events:
- onInitialise(status): Fired when CookieHub has finished loading. You can use this event to execute custom code based on the user's initial consent status.
- onAllow(category): Triggered whenever a user allows a cookie category that was previously disallowed. This event helps you react to changes in cookie preferences.
- onRevoke(category): Activated when a user revokes consent for a previously allowed cookie category. Use this event to handle consent changes.
- onStatusChange(status, previousState): Fired when users modify their cookie preferences, such as by clicking the "Save Settings" button or allowing all cookies. This event helps you track changes in user consent.
The following example demonstrates how to use these events:
var cpm = {
onInitialise: function(status) {
if (this.hasConsented('analytics')) {
console.log('The analytics category is allowed');
}
},
onStatusChange: function(status, previousStatus) {
if (this.hasConsented("analytics")) {
console.log('The analytics category is allowed');
}
},
onAllow: function(category) {
if (category == 'analytics') {
console.log('The analytics category was just allowed');
}
},
onRevoke: function(category) {
if (category == 'analytics') {
console.log('The analytics category was just revoked');
}
},
};
Note: The above example is integrated with the default CookieHub JavaScript code.
Public methods
CookieHub provides several public methods that you can use to enhance its functionality:
-
load(options): This method initializes CookieHub. It should be part of the default provided code and should only be called once on each page load. Any attempt to initialize CookieHub again will be ignored.
- options (object): An object containing a list of options and events. -
hasAnswered(): Checks if the user has already made cookie choices by allowing all cookies or saving settings. This method doesn't accept any parameters.
-
hasConsented(category): Checks if the user has allowed the specified cookie category.
- category (string): The name or ID of the cookie category to check. -
openDialog(): Opens the CookieHub dialog, typically displayed on the first load.
-
closeDialog(): Closes the CookieHub dialog if it's still open.
-
openSettings(): Opens the CookieHub settings dialog. Useful if you've hidden the settings icon and want to create a custom link or button for opening the settings dialog.
-
closeSettings(): Closes the CookieHub settings dialog.
-
allowAll(): Allows all cookie categories. This method offers an alternative way for users to consent to all categories.
-
denyAll(): Denies all cookie categories. Similar to allowAll(), this method offers an alternative way for users to revoke consent for all categories.
Here's a sample usage of these methods:
if (window.cookiehub.hasAnswered()) {
console.log('User has answered');
}
if (window.cookiehub.hasConsented("analytics")) {
console.log('Analytics category is allowed');
}
if (!window.cookiehub.hasAnswered()) {
window.cookiehub.openDialog();
}
window.cookiehub.closeDialog();
window.cookiehub.openSettings();
window.cookiehub.closeSettings();
window.cookiehub.allowAll();
window.cookiehub.denyAll();
By leveraging the CookieHub JavaScript API, you can create a personalized and compliant cookie consent experience for your website visitors.