JavaScript API
the following methods are available:
cc.
show (<optional_ delay>, <create_ modal>) cc.
hide () cc.
show Settings (<optional_ delay>) cc.
hide Settings () cc.
accept (<accepted_ categories>, <optional_ rejected_ categories>) -
accepted_
: string or string[]categories rejected_
: string[] - optionalcategories
Note: **all categories marked as readonly
will ALWAYS be enabled/accepted regardless of the categories provided inside the .accept
API call.
cc.accept('all'); // accept all categories
cc.accept([]); // accept none (reject all)
cc.accept('analytics'); // accept only analytics category
cc.accept(['cat_1', 'cat_2']); // accept only these 2 categories
cc.accept(); // accept all currently selected categories inside modal
cc.accept('all', ['analytics']); // accept all except "analytics" category
cc.accept('all', ['cat_1', 'cat_2']); // accept all except these 2 categories
How to later reject a specific category (cookieconsent already accepted)? Same as above:
cc.accept('all', ['targeting']); // opt out of targeting category
-
cc.
allowed Category (<category_ name>) Note: there are no default cookie categories, you create them!
A cookie category corresponds to the string of the
value
property inside thetoggle
object:// ... toggle: { value: 'analytics', // cookie category enabled: false, // default status readonly: false // allow to enable/disable // reload: 'on_disable', // allows to reload page when the current cookie category is deselected } // ...
Copied!Example:
// Check if user accepts cookie consent with analytics category enabled if (cc.allowedCategory('analytics')) { // yoo, you might want to load analytics.js ... };
Copied! -
cc.
valid Cookie (<cookie_ name>) If cookie exists and has non-empty ('') value => return true, otherwise false.
// Example: check if '_gid' cookie is set if (!cc.validCookie('_gid')) { // yoo, _gid cookie is not set, do something ... };
Copied! -
cc.
erase Cookies (<cookie_ names>, <optional_ path>, <optional_ domains>) - cookie_names: string[]
- path: string - optional
- domains: string[] - optional
Examples:
cc.eraseCookies(['cc_cookies']); // erase "cc_cookie" if it exists cc.eraseCookies(['cookie1', 'cookie2']); // erase these 2 cookies cc.eraseCookies(['cc_cookie'], "/demo"); cc.eraseCookies(['cc_cookie'], "/demo", [location.hostname]);
Copied! -
cc.
load Script (<path>, <callback_ function>, <optional_ custom_ attributes>) Basic example:
cc.loadScript('https://www.google-analytics.com/analytics.js', function(){ // Script loaded, do something });
Copied!How to load scripts with custom attributes:
cc.loadScript('https://www.google-analytics.com/analytics.js', function(){ // Script loaded, do something }, [ {name: 'id', value: 'ga_id'}, {name: 'another-attribute', value: 'value'} ]);
Copied! -
cc.
set (<field>, <object>) The
.set
method allows you to set the following values: - data (used to save custom data inside the plugin's cookie) - revision() How to save custom data:
// Set cookie's "data" field to whatever the value of the `value` prop. is cc.set('data', {value: {id: 21, country: "italy"}}); // Only add/update the specified props. cc.set('data', {value: {id: 22, new_prop: 'new prop value'}, mode: 'update'});
Copied! -
cc.
get (<field>) The
.get
method allows you to retrieve any of the fields inside the plugin's cookie:() cc.get('level'); // retrieve all accepted categories (if cookie exists) cc.get('data'); // retrieve custom data (if cookie exists) cc.get('revision'); // retrieve revision number (if cookie exists)
Copied! -
cc.
get Config (<field>) The
.get
method allows you to read configuration options from the current instance:Config () cc.getConfig('current_lang'); // get currently used language cc.getConfig('cookie_expiration'); // get configured cookie expiration // ...
Copied! -
cc.
get User Preferences () The
.get
returns the following object (for analytics/logging purposes):User Preferences () { accept_type: string, // 'all', 'necessary', 'custom' accepted_categories: string[], // e.g. ['necessary', 'analytics'] rejected_categories: string[] // e.g. ['ads'] }
Copied! -
cc.
update Scripts () This method allows the plugin to manage dynamically added/injected scripts that have been loaded after the plugin's execution.
E.g. dynamic content generated by server-side languages like php, node, ruby ...