Skip to content

Commit

Permalink
Feat: Add callbacks for all onAccept scenarios #CCM-24
Browse files Browse the repository at this point in the history
  • Loading branch information
OndraM committed Nov 1, 2021
1 parent 66947d6 commit 23314b5
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions src/LmcCookieConsentManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,50 @@ import { config as configSk } from './languages/sk';
const defaultOptions = {
currentLang: 'cs',
themeCss: '',
onFirstAccept: (cookie) => {},
onFirstAcceptOnlyNecessary: (cookie) => {},
onFirstAcceptAll: (cookie) => {},
onAccept: (cookie) => {},
onAcceptOnlyNecessary: (cookie) => {},
onAcceptAll: (cookie) => {},
config: {},
};

/**
* @param {Object} args - Options for cookie consent manager
* @param {string} args.currentLang - Specify one of the languages you have defined
* @param {string} args.themeCss - Specify file to the .css file
* @param {function} args.onAccept - Callback to be executed after any consent is detected (either given now or already saved previously)
* @param {function} args.onFirstAccept - Callback to be executed right after any consent is just accepted
* @param {function} args.onFirstAcceptOnlyNecessary - Callback to be executed right after only necessary cookies are accepted
* @param {function} args.onFirstAcceptAll - Callback to be executed right after all cookies are accepted
* @param {function} args.onAccept - Callback to be executed when any consent is detected (either given right now or already saved previously)
* @param {function} args.onAcceptOnlyNecessary - Callback to be executed when consent with only necessary cookies is detected (either given right now or already saved previously)
* @param {function} args.onAcceptAll - Callback to be executed when consent with all cookies is detected (either given right now or already saved previously)
* @param {Object} args.config - Override default config. See https://github.com/orestbida/cookieconsent/blob/master/Readme.md#all-available-options
*/
const LmcCookieConsentManager = (args) => {
const options = { ...defaultOptions, ...args };
const { currentLang, themeCss, onAccept, config } = options;
const {
currentLang,
themeCss,
onFirstAccept,
onFirstAcceptOnlyNecessary,
onFirstAcceptAll,
onAccept,
onAcceptOnlyNecessary,
onAcceptAll,
config,
} = options;
const cookieName = 'lmc_ccm';

const cookieconsent = window.initCookieConsent();
const isFirstTimeAccept = !cookieconsent.validCookie(cookieName);

cookieconsent.run({
current_lang: currentLang,
auto_language: true,
theme_css: themeCss,
cookie_name: 'lmc_ccm',
cookie_name: cookieName,
cookie_expiration: 365,
use_rfc_cookie: true,
autorun: true,
Expand All @@ -44,6 +66,10 @@ const LmcCookieConsentManager = (args) => {
},
},
onAccept: (cookie) => {
const givenLevels = cookieconsent.get('level');
const acceptedOnlyNecessary =
givenLevels.length === 1 && givenLevels[0] === 'necessary';

window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'CookieConsent-update',
Expand All @@ -56,6 +82,15 @@ const LmcCookieConsentManager = (args) => {
});

onAccept(cookie);

if (isFirstTimeAccept) {
onFirstAccept(cookie);
acceptedOnlyNecessary
? onFirstAcceptOnlyNecessary(cookie)
: onFirstAcceptAll(cookie);
}

acceptedOnlyNecessary ? onAcceptOnlyNecessary(cookie) : onAcceptAll(cookie);
},
languages: {
cs: configCs,
Expand Down

0 comments on commit 23314b5

Please sign in to comment.