Skip to content

Commit

Permalink
Feat: Submit consent to API #CCM-39
Browse files Browse the repository at this point in the history
  • Loading branch information
OndraM committed Nov 24, 2021
1 parent 52e2656 commit 85c9c02
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 7 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,14 @@ initLmcCookieConsentManager( // when loaded as a module, these options are passe
## Configuration options
| Option | Type | Default value | Description |
|---------------|-------------|--------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------|
| `defaultLang` | string | 'cs' | Default language. One of `cs`, `de`, `en`, `hu`, `pl`, `ru`, `sk`, `uk`. This language will be used when autodetect is disabled or when it fails. |
| `autodetectLang`| boolean | true | Autodetect language from the browser. If autodetect fails or if unsupported language is detected, fallback to `defaultLang`.<br>When disabled, force language to `defaultLang`. |
| `companyNames`| array | ['LMC'] | Array of strings with company names. Adjust only when the consent needs to be given to multiple companies. [See example][examples-configuration]. |
| `config` | Object | {} | Override default config of the underlying library. For all parameters see [original library](https://github.com/orestbida/cookieconsent#all-available-options). |
| `on*` callbacks| function | (cookie, cookieConsent) => {} | See below for configurable callbacks. |
| Option | Type | Default value | Description |
|-----------------|-------------|---------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------|
| `defaultLang` | string | 'cs' | Default language. One of `cs`, `de`, `en`, `hu`, `pl`, `ru`, `sk`, `uk`. This language will be used when autodetect is disabled or when it fails. |
| `autodetectLang`| boolean | true | Autodetect language from the browser. If autodetect fails or if unsupported language is detected, fallback to `defaultLang`.<br>When disabled, force language to `defaultLang`. |
| `companyNames` | array | ['LMC'] | Array of strings with company names. Adjust only when the consent needs to be given to multiple companies. [See example][examples-configuration]. |
| `consentCollectorApiUrl`| ?string | 'https://ccm.lmc.cz/(...)' | URL of the API where user consent information is sent. Null to disable sending data to the API. |
| `config` | Object | {} | Override default config of the underlying library. For all parameters see [original library](https://github.com/orestbida/cookieconsent#all-available-options). |
| `on*` callbacks | function | (cookie, cookieConsent) => {} | See below for configurable callbacks. |
### Supported languages
Expand Down
1 change: 1 addition & 0 deletions examples/callbacks.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ <h3 class="mt-4">Callbacks</h3>
{
autodetectLang: false, // do not detect language from the browser
defaultLang: 'en', // use 'en' language by default
consentCollectorApiUrl: 'https://ccm.lmc.cz/local-data-acceptation-data-entries?Spot=(public,demo)', // override default URL for demo purposes; do not set custom consentCollectorApiUrl unless you have been explicitly guided to do so!
onAccept: (cookie, cookieConsent) => { // any type of consent detected
markCallbackCalled('onAccept');
},
Expand Down
1 change: 1 addition & 0 deletions examples/configuration.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ <h3 class="mt-4">Use cookieConsent instance</h3>
autodetectLang: false, // do not detect language from the browser
defaultLang: 'en', // use 'en' language by default
companyNames: ['LMC', 'Some Other Company', 'ACME'], // define custom company names to be shown in the consent text
consentCollectorApiUrl: 'https://ccm.lmc.cz/local-data-acceptation-data-entries?Spot=(public,demo)', // override default URL for demo purposes; do not set custom consentCollectorApiUrl unless you have been explicitly guided to do so!
config: { // override default config of the underlying library, see https://github.com/orestbida/cookieconsent#all-available-options
delay: 500, // show cookie consent banner after 500ms
page_scripts: false, // disable third-party script management, see https://github.com/lmc-eu/cookie-consent-manager#third-party-scripts-loaded-via-script
Expand Down
1 change: 1 addition & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ <h3 class="mt-4">Allowed consent categories</h3>
{
autodetectLang: false, // do not detect language from the browser
defaultLang: 'en', // use 'en' language by default
consentCollectorApiUrl: 'https://ccm.lmc.cz/local-data-acceptation-data-entries?Spot=(public,demo)', // override default URL for demo purposes; do not set custom consentCollectorApiUrl unless you have been explicitly guided to do so!
onAccept: (cookie, cookieConsent) => { // any type of consent (including "only necessary") detected
document.getElementById('onaccept-content').innerHTML = JSON.stringify(cookie)

Expand Down
1 change: 1 addition & 0 deletions examples/theming.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ <h3 class="mt-4">TODO</h3>
{
autodetectLang: false, // do not detect language from the browser
defaultLang: 'en', // use 'en' language by default
consentCollectorApiUrl: 'https://ccm.lmc.cz/local-data-acceptation-data-entries?Spot=(public,demo)', // override default URL for demo purposes; do not set custom consentCollectorApiUrl unless you have been explicitly guided to do so!
}
);
});
Expand Down
8 changes: 8 additions & 0 deletions src/LmcCookieConsentManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import { config as configPl } from './languages/pl';
import { config as configRu } from './languages/ru';
import { config as configSk } from './languages/sk';
import { config as configUk } from './languages/uk';
import submitConsent from './consentCollector';

const defaultOptions = {
defaultLang: 'cs',
autodetectLang: true,
consentCollectorApiUrl: 'https://ccm.lmc.cz/local-data-acceptation-data-entries',
onFirstAccept: (cookie, cookieConsent) => {},
onFirstAcceptOnlyNecessary: (cookie, cookieConsent) => {},
onFirstAcceptAll: (cookie, cookieConsent) => {},
Expand All @@ -28,6 +30,7 @@ const defaultOptions = {
* @param {Object} [args] - Options for cookie consent manager
* @param {string} [args.defaultLang] - Default language. Must be one of predefined languages.
* @param {boolean} [args.autodetectLang] - Autodetect language from the browser
* @param {?string} [args.consentCollectorApiUrl] - URL of the API where user consent information should be sent. Null to disable.
* @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
Expand All @@ -47,6 +50,7 @@ const LmcCookieConsentManager = (serviceName, args) => {
const {
defaultLang,
autodetectLang,
consentCollectorApiUrl,
onFirstAccept,
onFirstAcceptOnlyNecessary,
onFirstAcceptAll,
Expand Down Expand Up @@ -114,6 +118,10 @@ const LmcCookieConsentManager = (serviceName, args) => {
});
}

if (consentCollectorApiUrl !== null) {
submitConsent(consentCollectorApiUrl, cookieConsent, acceptedOnlyNecessary);
}

onFirstAccept(cookie, cookieConsent);
acceptedOnlyNecessary
? onFirstAcceptOnlyNecessary(cookie, cookieConsent)
Expand Down
61 changes: 61 additions & 0 deletions src/consentCollector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Submit information about consent level given by the user.
*
* @param {string} consentCollectorApiUrl
* @param {Object} cookieConsent
* @param {boolean} acceptedOnlyNecessary
*/
export const submitConsent = (consentCollectorApiUrl, cookieConsent, acceptedOnlyNecessary) => {
const payload = buildPayload(cookieConsent, acceptedOnlyNecessary);

postDataToApi(consentCollectorApiUrl, payload);
};

/**
* @param {Object} cookieConsent
* @param {boolean} acceptedOnlyNecessary
* @returns {Object}
*/
const buildPayload = (cookieConsent, acceptedOnlyNecessary) => {
const cookieData = cookieConsent.get('data');
const acceptedCategories = cookieConsent.get('level');
// TODO: read actual categories once following is implemented in vanilla-cookieconsent:
// https://github.com/orestbida/cookieconsent/discussions/90#discussioncomment-1466886
const rejectedCategories = acceptedOnlyNecessary
? ['ad', 'analytics', 'functionality', 'personalization']
: [];

return {
data: {
type: 'localDataAcceptationDataEntries',
attributes: {
acceptation_id: cookieData.uid,
accept_type: acceptedOnlyNecessary ? 'accept_necessary' : 'accept_all',
accepted_categories: acceptedCategories,
rejected_categories: rejectedCategories,
revision: cookieConsent.get('revision'),
source: cookieData.serviceName,
},
},
};
};

/**
* @param {string} apiUrl
* @param {Object} payload
* @return {Promise<any>}
*/
const postDataToApi = async (apiUrl, payload) => {
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/vnd.api+json',
Accept: 'application/vnd.api+json',
},
body: JSON.stringify(payload),
});

return response.json();
};

export default submitConsent;

0 comments on commit 85c9c02

Please sign in to comment.