Skip to content

Commit

Permalink
Merge pull request #1 from MetamorfosiLab/feature/cookie-groups
Browse files Browse the repository at this point in the history
Feature/cookie groups
  • Loading branch information
hrynevychroman committed Jan 31, 2024
2 parents e3aa42a + ffbd6d0 commit cac36e1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 19 deletions.
25 changes: 20 additions & 5 deletions src/examples/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,26 @@ const cookiesConsent = new CookiesConsent({
</p>
`,
},
cc_ga: {
name: 'cc_ga',
title: 'Google Analytics',
description: '<p>GA Cookies 🍪.</p>',
code: 'G-XXXXXXXXXX',
// cc_ga: {
// name: 'cc_ga',
// title: 'Google Analytics',
// description: '<p>GA Cookies 🍪.</p>',
// code: 'G-XXXXXXXXXX',
// },
analytics: {
name: 'analytics',
title: 'Analytics',
description: '<p>Analytics Cookies 🍪.</p>',
cookies: [
{
name: 'cc_ga',
code: 'G-XXXXXXXXXX',
},
{
name: 'cc_gtm',
code: 'GTM-XXXXXXX',
},
],
},
},
// #endregion cookies
Expand Down
49 changes: 37 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
import { manageGoogleAnalytics } from './modules/cc-ga'
import { manageGoogleTagManager } from './modules/cc-gtm'
import { contentDefault } from './shared/content-default'
import type { Cookie } from './types/cookie.types'

export class CookiesConsent {
private params: CookiesConsentParams
Expand Down Expand Up @@ -50,16 +51,35 @@ export class CookiesConsent {
checkCookies() {
document.cookie.split(' ').forEach((cookie) => {
const [name] = cookie.split('=')
this.#cookies_status[name] = true
this.#cookies_status[name].status = true
})
}

#isCookiesGroup(cookie: Cookie) {
return 'cookies' in cookie
}

checkParameters() {
this.params = this.params || {}
this.params.cookies = this.params.cookies || {}

for (const cookie in this.params.cookies)
this.#cookies_status[this.params.cookies[cookie]?.name] = false
for (const cookie in this.params.cookies) {
if (!this.#isCookiesGroup(this.params.cookies[cookie])) {
this.#cookies_status[this.params.cookies[cookie]?.name] = {
status: false,
...this.params.cookies[cookie],
}
}
else {
this.params.cookies[cookie].cookies?.forEach((childCookie) => {
this.#cookies_status[childCookie.name] = {
status: false,
parent: this.params.cookies?.[cookie],
...childCookie,
}
})
}
}

this.params.mainWindowSettings ??= false
this.params.position ??= 'bottom-left'
Expand Down Expand Up @@ -456,7 +476,7 @@ export class CookiesConsent {
setCookie(this.#cookie_name, value)

Object.entries(this.#cookies_status).forEach(([key, value]) => {
value && setCookie(key, btoa(`${key}:${Date.now()}`))
value.status && setCookie(key, btoa(`${key}:${Date.now()}`))
})

this.#answered = true
Expand All @@ -466,21 +486,26 @@ export class CookiesConsent {
if (this.#cookies_status) {
if ($type === 'accept_all') {
for (const cookie in this.#cookies_status)
this.#cookies_status[cookie] = true
this.#cookies_status[cookie].status = true
}
else if ($type === 'reject_all') {
for (const cookie in this.#cookies_status)
this.#cookies_status[cookie] = false
this.#cookies_status[cookie].status = false
}
else if ($type === 'selection') {
const chk_cookie: NodeListOf<HTMLInputElement> = document.querySelectorAll('.cc-window-settings-cookie-value .cc-cookie-checkbox')

for (let i = 0; i < chk_cookie.length; i++) {
const key = chk_cookie[i].dataset.name
chk_cookie.forEach((checkbox) => {
const cookieName = checkbox.dataset.name

if (key)
this.#cookies_status[key] = chk_cookie[i].checked
}
Object.entries(this.#cookies_status).forEach(([key, value]) => {
if (cookieName && value?.name === cookieName)
this.#cookies_status[cookieName].status = checkbox.checked

if (cookieName && value.parent?.name === cookieName)
this.#cookies_status[key].status = checkbox.checked
})
})
}
}
}
Expand All @@ -499,7 +524,7 @@ export class CookiesConsent {
}

const invokeAnalyticsCallback = (cookieType: AnalyticsType) => {
const status = Boolean(this.#cookies_status?.[cookieType] === true)
const status = Boolean(this.#cookies_status?.[cookieType].status === true)
const manageFunction = cookieType === 'cc_ga' ? manageGoogleAnalytics : manageGoogleTagManager

if (this.params.cookies?.[cookieType]) {
Expand Down
2 changes: 2 additions & 0 deletions src/types/cookie.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ export interface Cookie {
* Google Analytics code for global site tag (gtag.js).
*/
code?: string

cookies?: Array<Omit<Cookie, 'title' | 'description'>>
}
7 changes: 5 additions & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface Cookies {
* Custom cookies can be added using their respective names as keys.
* The values are instances of the `Cookie` type.
*/
[key: string]: Cookie
[cookieName: string]: Cookie
}
// #endregion CookiesInterface

Expand All @@ -31,7 +31,10 @@ export interface CookiesStatus {
* Status of each custom cookie, identified by their respective names.
* The values are boolean flags indicating whether the cookie is accepted or rejected.
*/
[key: keyof Cookies]: boolean | undefined
[key: keyof Cookies]: {
status: boolean
parent?: Cookie
} & Cookie
}
// #endregion CookiesStatusInterface

Expand Down

0 comments on commit cac36e1

Please sign in to comment.