-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2a9992
commit 1492d07
Showing
13 changed files
with
243 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# CookieScript | ||
|
||
Websie: [cookiescript.com](https://cookiescript.com) | ||
|
||
|
||
## Configuration | ||
|
||
```typescript | ||
export default defineNuxtConfig({ | ||
cookieConsent: { | ||
provider: 'cookiescript', | ||
id: '00000000000000000000000000000000', // Replace with you own id | ||
} | ||
}) | ||
``` | ||
|
||
## Consent mode | ||
By default CookieScript enables Consent Mode (opt-out). | ||
|
||
[Read more about Google Consent Mode in CookieScript →](https://help.cookie-script.com/en/google-analytics/google-consent-mode-implementation-instructions) | ||
|
||
## Type Declaration | ||
<<< @/../src/runtime/types/types.d.ts#moduleGeneralOptionsCookieScript{typescript} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/runtime/components/ConsentPolicy.cookiescript.client.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { defineComponent, h, Fragment } from 'vue' | ||
import { useRuntimeConfig } from '#imports' | ||
|
||
export default defineComponent( | ||
() => { | ||
return () => { | ||
const { cookieConsent: config } = useRuntimeConfig().public | ||
|
||
// @ts-ignore | ||
const id = config.id | ||
|
||
if (!id) { | ||
return h(Fragment, { key: 'CookiePolicy' }) | ||
} | ||
|
||
return h(Fragment, { key: 'CookiePolicy' }, [ | ||
h('script', { | ||
id: 'CookiePolicy', | ||
src: `https://report.cookie-script.com/r/${id}.js`, | ||
type: 'text/javascript', | ||
'data-cookiescriptreport': 'report', | ||
charset: 'UTF-8', | ||
}), | ||
]) | ||
} | ||
}, | ||
{ | ||
props: [], | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { | ||
defineNuxtPlugin, | ||
useCookie, | ||
useCookieConsent, | ||
useHead, | ||
useRuntimeConfig, | ||
} from '#imports' | ||
import consola from 'consola' | ||
import type { CookieScriptPayload } from '../types/types' | ||
import { loadScripts } from '../utils/script.loader' | ||
import { updateState } from '../utils/update.state' | ||
|
||
export default defineNuxtPlugin(() => { | ||
const { cookieConsent: config } = useRuntimeConfig().public | ||
|
||
if (!config.init || (!config.dev && process.dev)) { | ||
return | ||
} | ||
|
||
if (config.init) { | ||
// @ts-ignore | ||
const id = config.id | ||
|
||
if (!id) { | ||
consola.warn( | ||
'Unable to initialize CookieScript script: No CookieScript id provided. Please set the `id` option in "cookieConsent" in `nuxt.config.js`', | ||
) | ||
} else { | ||
useHead({ | ||
script: [ | ||
{ | ||
id: 'CookieScript', | ||
src: `https://cdn.cookie-script.com/s/${id}.js`, | ||
type: 'text/javascript', | ||
tagPriority: 5, | ||
}, | ||
], | ||
}) | ||
} | ||
} | ||
|
||
const cookie = useCookie<CookieScriptPayload | null>('CookieScriptConsent', { | ||
decode: (value: string) => | ||
JSON.parse( | ||
decodeURIComponent(value || '{ "categories": [] }'), | ||
) as CookieScriptPayload | null, | ||
}) | ||
|
||
const { state } = useCookieConsent() | ||
|
||
if (cookie.value) { | ||
updateState(state, { | ||
necessary: true, | ||
functional: | ||
cookie.value.categories?.includes('functionality') || | ||
(cookie.value.categories == undefined && | ||
cookie.value.action === 'accept') || | ||
false, | ||
statistic: | ||
cookie.value.categories?.includes('performance') || | ||
(cookie.value.categories == undefined && | ||
cookie.value.action === 'accept') || | ||
false, | ||
marketing: | ||
cookie.value.categories?.includes('targeting') || | ||
(cookie.value.categories == undefined && | ||
cookie.value.action === 'accept') || | ||
false, | ||
unclassified: | ||
cookie.value.categories?.includes('unclassified') || | ||
(cookie.value.categories == undefined && | ||
cookie.value.action === 'accept') || | ||
false, | ||
}) | ||
} | ||
|
||
if (import.meta.client) { | ||
window.addEventListener('CookieScriptAccept', (e: any) => { | ||
const categories = e.detail?.categories || [] | ||
|
||
updateState(state, { | ||
necessary: true, | ||
functional: categories.includes('functionality') || false, | ||
statistic: categories.includes('performance') || false, | ||
marketing: categories.includes('targeting') || false, | ||
unclassified: categories.includes('unclassified') || false, | ||
}) | ||
}) | ||
|
||
window.addEventListener('CookieScriptReject', () => { | ||
updateState(state, { | ||
necessary: true, | ||
functional: false, | ||
statistic: false, | ||
marketing: false, | ||
unclassified: false, | ||
}) | ||
}) | ||
|
||
window.addEventListener('CookieScriptAcceptAll', () => { | ||
updateState(state, { | ||
necessary: true, | ||
functional: true, | ||
statistic: true, | ||
marketing: true, | ||
unclassified: true, | ||
}) | ||
}) | ||
} | ||
|
||
loadScripts(config) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters