1.0.0 • Published 8 months ago
@oslokommune/cookie-consent v1.0.0
Cookie Consent
Install
Run npm install @oslokommune/cookie-consent
Methods
import { CookieConsent } from '@oslokommune/cookie-consent'
const googleAnalyticsId = 123;
const hotjarId = 456;
const cookieConsentConfig = {
cookies: {
domain: 'localhost',
secure: true,
expiryDays: 90
},
services: {
serviceIds: {
statistics: googleAnalyticsId,
survey: hotjarId
}
},
logging: {
adapters: [
{
adapter: 'console' // for debug only
},
{
adapter: 'http',
config: {
store: {
endpoint: 'url_to_store_logging_endpoint'
}
}
}
]
}
};
const cookieConsent = new CookieConsent(cookieConsentConfig);
// Set a consentCookie
try {
await cookieConsent.setConsentCookie([
{
"name": "survey",
"consent": true
},
{
"name": "statistics",
"constent": true
},
{
"name": "functional",
"consent": true
}
]);
// Toggle services on/off based on consents
cookieConsent.toggleServices(cookieConsent.getConsentCookie());
} catch (error) {
// Handle error
}
// Get a consentCookie
const consent = cookieConsent.getConsentCookie();
console.log(consent)
// items: ConsentItem[]
// checksum: string
// uuid: string
// Delete a consentCookie
cookieConsent.deleteConsentCookie();
// Validate consentCookie, toggle services or init cookie banner for user
try {
const validation = await cookieConsent.validateConsentCookie();
if (validation) {
cookieConsent.toggleServices(cookieConsent.getConsentCookie());
} else {
// Init cookie consent banner
}
} catch (error) {
// Handle error
}
cookieConsent.toggleServices(consentObject: ConsentObject);
cookieConsent.enableAllServices(consentObject: ConsentObject);
cookieConsent.disableAllServices(consentObject: ConsentObject);Events
All events are emitted through the CookieEvents bus from CookieManager.
import { CookieEvents } from 'cookie-manager'
function toogleMyFunctionalCookies(payload) {
if (payload.consent) {
//
}
}
CookieEvents.on('CookieConsent.functionalCookies', toggleMyFunctionalCookies)CookieConsent.set=>ConsentObjectCookieConsent.get=>ConsentObjectCookieConsent.delete=>voidCookieConsent.validate=>boolCookieConsent.functionalCookies=>{ consent: bool }
Services
Any ConsentItem in ConsentObject whos name matches the servicekeys below will trigger the corresponding adapter.
statisticsmapped to adapterGoogleAnalyticssurveymapped to adapterHotJarfunctionalmapped to adapterFunctional
Adapters
Google analytics
// Pass google analytics id in as configuration to CookieConsent, if null or not set google analytics adapter will be disabled
const config = {
services: {
serviceIds: {
statistics: googleAnalyticsId,
...HotJar
// Pass hotjar id in as configuration to CookieConsent, if null or not set hotjar adapter will be disabled
const config = {
services: {
serviceIds: {
survey: hotjarId,
...Functional
import { CookieEvents } from 'cookie-manager'
function toogleMyFunctionalCookies(payload) {
if (payload.consent) {
// Enable functional cookies here
} else {
// Disable functional cookies here
}
}
// Emits event on enable / disable
CookieEvents.on('CookieConsent.functionalCookies', toggleMyFunctionalCookies)