API Reference
run
Configures the plugin with the provided config. object.
Type
function(config: object): Promise<void>Details
The
configargument is required and must contain — at least — thecategoriesandlanguageproperties.Example ```javascript CookieConsent.run({ categories: { // categories here }, language: { default: ‘en’,
translations: { en: { // modal translations here } } }}); ```
show
Shows the consent modal.
Type
function(createModal?: boolean): voidDetails
If consent was previously expressed, the consent modal will not be generated; you’ll have to pass the argument
trueto generate it on the fly.Example ```javascript CookieConsent.show();
// show modal (if it doesn’t exist, create it) CookieConsent.show(true); ```
hide
Hides the consent modal.
Type
function(): voidExample
javascript CookieConsent.hide();
showPreferences
Shows the preferences modal.
Type
function(): voidExample
javascript CookieConsent.showPreferences();
hidePreferences
Hides the preferences modal.
Type
function(): voidExample
javascript CookieConsent.hidePreferences();
acceptCategory
Accepts or rejects categories.
Type
function( categoriesToAccept?: string | string[], exclusions?: string[] ): voidDetails
The first argument accepts either a
stringor anarrayof strings. Special values:'all': accept all[]: empty array, accept none (reject all): empty argument, accept only the categories selected in the preferences modal
Examples ```javascript CookieConsent.acceptCategory(‘all’); // accept all categories CookieConsent.acceptCategory([]); // reject all (accept only categories marked as readOnly/necessary) CookieConsent.acceptCategory(); // accept currently selected categories inside the preferences modal
CookieConsent.acceptCategory(‘analytics’); // accept only the “analytics” category CookieConsent.acceptCategory([‘cat_1’, ‘cat_2’]); // accept only these 2 categories
CookieConsent.acceptCategory(‘all’, [‘analytics’]); // accept all categories except the “analytics” category CookieConsent.acceptCategory(‘all’, [‘cat_1’, ‘cat_2’]); // accept all categories except these 2 ```
acceptedCategory
Returns true if the specified category was accepted, otherwise false.
Type
function(categoryName: string): booleanExamples ```javascript if(CookieConsent.acceptedCategory(‘analytics’)){ // great }
if(!CookieConsent.acceptedCategory(‘ads’)){ // not so great } ```
acceptService
Accepts or rejects services.
Type
function( services: string | string[], category: string ): voidDetails
Special values for the
servicesargument:'all': accept all services[]: empty array, accept none (reject all)
Examples ```javascript CookieConsent.acceptService(‘all’, ‘analytics’); // accept all services (in the ‘analytics’ category) CookieConsent.acceptService([], ‘analytics’); // reject all services
CookieConsent.acceptService(‘service1’, ‘analytics’); // accept only this specific service (reject all the others) CookieConsent.acceptService([‘service1’, ‘service2’], ‘analytics’); // accept only these 2 services (reject all the others) ```
acceptedService
Returns true if the service inside the category is accepted, otherwise false.
Type
function( serviceName: string, categoryName: string ): booleanExamples
javascript if(CookieConsent.acceptedService('Google Analytics', 'analytics')){ // great }else{ // not so great }
validConsent
Returns true if consent is valid.
Type
javascript function(): booleanDetails
Consent is not valid when at least one of following situations occurs:
- consent is missing (e.g. user has not yet made a choice)
- revision numbers don’t match
- the plugin’s cookie does not exist/has expired
- the plugin’s cookie is structurally not valid (e.g. empty)
Example
javascript if(CookieConsent.validConsent()){ // consent is valid }else{ // consent is not valid }
loadScript
Loads script files (.js).
Type
function( path: string, attributes?: {[key: string]: string} ): Promise<boolean>Examples
// basic usage CookieConsent.loadScript('path-to-script.js'); // check if script is loaded successfully const loaded = await CookieConsent.loadScript('path-to-script.js'); if(!loaded){ console.log('Script failed to load!'); }You may also concatenate multiple
.loadScriptmethods:javascript CookieConsent.loadScript('path-to-script1.js') .then(() => CookieConsent.loadScript('path-to-script2.js')) .then(() => CookieConsent.loadScript('path-to-script3.js'));Load script with attributes:
javascript CookieConsent.loadScript('path-to-script.js', { 'id': 'ga-script', 'another-attribute': 'another-value' });
getConfig
Returns the configuration object or one of its fields.
Type
javascript function(field?: string): anyExample ```javascript // Get the entire config const config = CookieConsent.getConfig();
// Get only the language’ prop. const language = CookieConsent.getConfig(‘language’); ```
getUserPreferences
Returns user’s preferences, such as accepted/rejected categories and services.
Type
function(): { acceptType: string, acceptedCategories: string[], rejectedCategories: string[], acceptedServices: {[category: string]: string[]} rejectedServices: {[category: string]: string[]} }Details
Possible
acceptTypevalues:'all''custom''necessary'
Example
const preferences = CookieConsent.getUserPreferences(); if(preferences.acceptType === 'all'){ console.log("Awesome!"); } if(preferences.acceptedCategories.includes('analytics')){ console.log("The analytics category was accepted!"); }
setLanguage
Changes the modal’s language. Returns a Promise<boolean> which evaluates to true if the language was changed successfully.
Type
javascript function( language: string, force?: boolean ): Promise<boolean>Examples
// Simple usage CookieConsent.setLanguage('it'); // Get return value const success = await CookieConsent.setLanguage('en');Forcefully refresh modals (re-generates the html content):
javascript CookieConsent.setLanguage('en', true);
reset
Reset CookieConsent.
Type:
javascript function(eraseCookie?: boolean): voidDetails:
Resets all internal pointers and config. settings. You need to call again the
.run()method with a valid config. object.You can pass the argument
trueto delete the plugin’s cookie. The user will be prompted once again to express their consent.::: warning Once this method is called, the plugin won’t be able to detect already executed
scripttags with adata-categoryattribute. :::Example:
javascript CookieConsent.reset(true);