Skip to content

Commit

Permalink
OneTrust_CookieConesnt_Changes: added try/catch and minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ruchiramoitra committed Dec 22, 2021
1 parent 02cf227 commit a4847d7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 45 deletions.
2 changes: 1 addition & 1 deletion analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class Analytics {
this.clientIntegrations = this.clientIntegrations.filter((intg) => {
return (
integrations[intg.name] != undefined &&
(!this.cookieConsent ||
(!this.cookieConsent || // check if cookieconsent object is present and then do filtering
(this.cookieConsent && this.cookieConsent.isEnabled(intg.config)))
);
});
Expand Down
93 changes: 49 additions & 44 deletions cookieConsent/OneTrust/browser.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logger from "../../utils/logUtil";

/* eslint-disable class-methods-use-this */
class OneTrust {
constructor(sourceConfig) {
Expand All @@ -7,12 +9,12 @@ class OneTrust {
isEnabled(destConfig) {
// If user does not load onetrust sdk before loading rudderstack sdk
// we will not be filtering any of the destinations.
try {
if (!window.OneTrust && !window.OnetrustActiveGroups) {
return true;
}

if (!window.OneTrust && !window.OnetrustActiveGroups) {
return true;
}

/**
/**
* Structure of onetrust consent group destination config.
*
* "oneTrustConsentGroup": [
Expand All @@ -29,58 +31,61 @@ class OneTrust {
*
*/

const { oneTrustConsentGroup } = destConfig; // mapping of the destination with the consent group name
const { oneTrustConsentGroup } = destConfig; // mapping of the destination with the consent group name

// If the destination do not have this mapping events will be sent.
// If the destination do not have this mapping events will be sent.

if (!oneTrustConsentGroup) {
return true;
}
// OneTrust Cookie Compliance populates a data layer object OnetrustActiveGroups with
// the cookie categories that the user has consented to.
// Eg: ',C0001,C0003,'
// We split it and save it as an array.
if (!oneTrustConsentGroup) {
return true;
}
// OneTrust Cookie Compliance populates a data layer object OnetrustActiveGroups with
// the cookie categories that the user has consented to.
// Eg: ',C0001,C0003,'
// We split it and save it as an array.

const userSetConsentGroupIds = window.OnetrustActiveGroups.split(","); // Ids user has consented
const userSetConsentGroupIds = window.OnetrustActiveGroups.split(","); // Ids user has consented

// Get information about the cookie script - data includes, consent models, cookies in preference centre, etc.
// We get the groups(cookie categorization), user has created in one trust account.
// Get information about the cookie script - data includes, consent models, cookies in preference centre, etc.
// We get the groups(cookie categorization), user has created in one trust account.

const oneTrustAllGroupsInfo = window.OneTrust.GetDomainData().Groups;
const userSetConsentGroupNames = [];
const oneTrustAllGroupsInfo = window.OneTrust.GetDomainData().Groups;
const userSetConsentGroupNames = [];

// Get the names of the cookies consented by the user in the browser.
// Get the names of the cookies consented by the user in the browser.

oneTrustAllGroupsInfo.forEach((group) => {
const { CustomGroupId, GroupName } = group;
if (userSetConsentGroupIds.includes(CustomGroupId)) {
userSetConsentGroupNames.push(GroupName.toUpperCase().trim());
}
});
oneTrustAllGroupsInfo.forEach((group) => {
const { CustomGroupId, GroupName } = group;
if (userSetConsentGroupIds.includes(CustomGroupId)) {
userSetConsentGroupNames.push(GroupName.toUpperCase().trim());
}
});

// Change the structure of oneTrustConsentGroup as an array and filter values if empty string
// Eg:
// ["Performance Cookies", "Functional Cookies"]
// Change the structure of oneTrustConsentGroup as an array and filter values if empty string
// Eg:
// ["Performance Cookies", "Functional Cookies"]

const oneTrustConsentGroupArr = oneTrustConsentGroup
.map((c) => c.oneTrustConsentGroup)
.filter((n) => n);
let containsAllConsent = true;
const oneTrustConsentGroupArr = oneTrustConsentGroup
.map((c) => c.oneTrustConsentGroup)
.filter((n) => n);
let containsAllConsent = true;

// Check if any value is there in the destination's cookie groups.
// If not events will go anyway to the destination.
// Check if any value is there in the destination's cookie groups.
// If not events will go anyway to the destination.

if (
Array.isArray(oneTrustConsentGroupArr) &&
oneTrustConsentGroupArr.length
) {
// Check if all the destination's mapped cookie categories are consented by the user in the browser.
if (
Array.isArray(oneTrustConsentGroupArr) &&
oneTrustConsentGroupArr.length
) {
// Check if all the destination's mapped cookie categories are consented by the user in the browser.

containsAllConsent = oneTrustConsentGroupArr.every((element) =>
userSetConsentGroupNames.includes(element.toUpperCase().trim())
);
containsAllConsent = oneTrustConsentGroupArr.every((element) =>
userSetConsentGroupNames.includes(element.toUpperCase().trim())
);
}
return containsAllConsent;
} catch (e) {
logger.debug(`Error during onetrust cookie consent management ${e}`);
}
return containsAllConsent;
}
}

Expand Down

0 comments on commit a4847d7

Please sign in to comment.