Skip to content

Commit

Permalink
Merge pull request #408 from rudderlabs/production-staging
Browse files Browse the repository at this point in the history
production-staging -> production
  • Loading branch information
saikumarrs authored Jan 4, 2022
2 parents b98a0b7 + 94d66dc commit 399191b
Show file tree
Hide file tree
Showing 11 changed files with 435 additions and 221 deletions.
44 changes: 22 additions & 22 deletions analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import logger from "./utils/logUtil";
import { addDomEventHandlers } from "./utils/autotrack.js";
import ScriptLoader from "./integrations/ScriptLoader";
import parseLinker from "./utils/linker";
import CookieConsentFactory from "./cookieConsent/CookieConsentFactory";

const queryDefaults = {
trait: "ajs_trait_",
Expand Down Expand Up @@ -97,6 +98,7 @@ class Analytics {
};
this.loaded = false;
this.loadIntegration = true;
this.cookieConsentOptions = {};
}

/**
Expand Down Expand Up @@ -180,12 +182,26 @@ class Analytics {
this.loadOnlyIntegrations,
this.clientIntegrations
);
let cookieConsent = undefined;
// Check if cookie consent manager is being set through load options
if (Object.keys(this.cookieConsentOptions).length) {
// Call the cookie consent factory to initialise and return the type of cookie
// consent being set. For now we only support OneTrust.
cookieConsent = CookieConsentFactory.initialize(
response,
this.cookieConsentOptions
);
}

// remove from the list which don't have support yet in SDK
// If cookie consent object is return we filter according to consents given by user
// else we do not consider any filtering for cookie consent.
this.clientIntegrations = this.clientIntegrations.filter((intg) => {
return integrations[intg.name] != undefined;
return (
integrations[intg.name] != undefined &&
(!cookieConsent || // check if cookieconsent object is present and then do filtering
(cookieConsent && cookieConsent.isEnabled(intg.config)))
);
});

this.init(this.clientIntegrations);
} catch (error) {
handleError(error);
Expand Down Expand Up @@ -213,7 +229,6 @@ class Analytics {
const self = this;
logger.debug("supported intgs ", integrations);
// this.clientIntegrationObjects = [];

if (!intgArray || intgArray.length == 0) {
if (this.readyCallback) {
this.readyCallback();
Expand All @@ -232,9 +247,7 @@ class Analytics {
const destConfig = intg.config;
intgInstance = new intgClass(destConfig, self);
intgInstance.init();

logger.debug("initializing destination: ", intg);

this.isInitialized(intgInstance).then(this.replayEvents);
} catch (e) {
logger.error(
Expand Down Expand Up @@ -918,6 +931,8 @@ class Analytics {
*/
load(writeKey, serverUrl, options) {
logger.debug("inside load ");
if (options && options.cookieConsentManager)
this.cookieConsentOptions = cloneDeep(options.cookieConsentManager);
if (this.loaded) return;
let configUrl = CONFIG_URL;
if (!this.isValidWriteKey(writeKey) || !this.isValidServerUrl(serverUrl)) {
Expand Down Expand Up @@ -978,25 +993,11 @@ class Analytics {
this.registerCallbacks(true);
}

if (
options &&
options.queueOptions &&
options.queueOptions != null &&
typeof options.queueOptions == "object"
) {
this.eventRepository.startQueue(options.queueOptions);
} else {
this.eventRepository.startQueue({});
}

if (options && options.loadIntegration != undefined) {
this.loadIntegration = !!options.loadIntegration;
}

this.eventRepository.writeKey = writeKey;
if (serverUrl) {
this.eventRepository.url = serverUrl;
}
this.eventRepository.initialize(writeKey, serverUrl, options);
this.initializeUser();
this.setInitialPageProperties();
this.loaded = true;
Expand Down Expand Up @@ -1025,7 +1026,6 @@ class Analytics {
addDomEventHandlers(this);
}
}

if (options && options.getSourceConfig) {
if (typeof options.getSourceConfig !== "function") {
handleError('option "getSourceConfig" must be a function');
Expand Down
30 changes: 30 additions & 0 deletions cookieConsent/CookieConsentFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { OneTrust } from "./OneTrust";

class CookieConsentFactory {
static initialize(sourceConfig, cookieConsentOptions) {
/**
*
* check which type of cookie consent manager needs to be called if enabled
* for now we have only OneTrust.
* But if new cookie consent manager options are implemented,
* we need to make sure only one of them is enabled by the user in the
* load options
*
*/

if (cookieConsentOptions?.oneTrust?.enabled) {
// This is P1. When we have an ui in source side to turn on/off of cookie consent
// if (sourceConfig &&
// sourceConfig.cookieConsentManager &&
// sourceConfig.cookieConsentManager.oneTrust &&
// sourceConfig.cookieConsentManager.oneTrustenabled) {

return new OneTrust();

// }
}
return null;
}
}

export default CookieConsentFactory;
87 changes: 87 additions & 0 deletions cookieConsent/OneTrust/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import logger from "../../utils/logUtil";

/* eslint-disable class-methods-use-this */
class OneTrust {
constructor(sourceConfig) {
this.sourceConfig = sourceConfig;
// If user does not load onetrust sdk before loading rudderstack sdk
// we will not be filtering any of the destinations.
if (!window.OneTrust || !window.OnetrustActiveGroups) {
logger.debug(
`Onetrust window objects not retrieved. Thus events are sent.`
);
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

// 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;
this.userSetConsentGroupNames = [];

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

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

isEnabled(destConfig) {
try {
/**
* Structure of onetrust consent group destination config.
*
* "oneTrustConsentGroup": [
{
"oneTrustConsentGroup": "Performance Cookies"
},
{
"oneTrustConsentGroup": "Functional Cookies"
},
{
"oneTrustConsentGroup": ""
}
]
*
*/

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

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

if (!oneTrustCookieCategories) {
return true;
}

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

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

// Check if all the destination's mapped cookie categories are consented by the user in the browser.
containsAllConsent = oneTrustConsentGroupArr.every((element) =>
this.userSetConsentGroupNames.includes(element.toUpperCase().trim())
);

return containsAllConsent;
} catch (e) {
logger.error(`Error during onetrust cookie consent management ${e}`);
return true;
}
}
}

export default OneTrust;
3 changes: 3 additions & 0 deletions cookieConsent/OneTrust/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import OneTrust from "./browser";

export { OneTrust };
2 changes: 1 addition & 1 deletion dist/rudder-sdk-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rudder-sdk-js",
"version": "1.2.14",
"version": "1.2.13",
"description": "RudderStack Javascript SDK",
"main": "index.js",
"types": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rudder-analytics",
"version": "1.2.14",
"version": "1.2.13",
"description": "",
"main": "./dist/browser.min.js",
"size-limit": [
Expand Down
Loading

0 comments on commit 399191b

Please sign in to comment.