Skip to content

Commit

Permalink
chore: code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mihir-4116 committed Mar 26, 2024
1 parent 1a62f6d commit 71445df
Show file tree
Hide file tree
Showing 4 changed files with 457 additions and 5 deletions.
87 changes: 86 additions & 1 deletion src/v0/destinations/ga4/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
const { validateEventName, prepareUserProperties, removeInvalidParams } = require('./utils');
const {
validateEventName,
removeInvalidParams,
prepareUserConsents,
prepareUserProperties,
} = require('./utils');

const userPropertyData = [
{
Expand Down Expand Up @@ -447,4 +452,84 @@ describe('Google Analytics 4 utils test', () => {
expect(result).toEqual(expected);
});
});

describe('prepareUserConsents function tests', () => {
it('Should return an empty object when no consents are given', () => {
const message = {};
const result = prepareUserConsents(message);
expect(result).toEqual({});
});

it('Should return an empty object when no consents are given', () => {
const message = {
integrations: {
GA4: {},
},
};
const result = prepareUserConsents(message);
expect(result).toEqual({});
});

it('Should return an empty object when no consents are given', () => {
const message = {
integrations: {
GA4: {
consents: {},
},
},
};
const result = prepareUserConsents(message);
expect(result).toEqual({});
});

it('Should return a consents object when consents are given', () => {
const message = {
integrations: {
GA4: {
consents: {
personalizationConsent: 'GRANTED',
userDataConsent: 'GRANTED',
},
},
},
};
const result = prepareUserConsents(message);
expect(result).toEqual({
ad_personalization: 'GRANTED',
ad_user_data: 'GRANTED',
});
});

it('Should return an empty object when invalid consents are given', () => {
const message = {
integrations: {
GA4: {
consents: {
personalizationConsent: 'NOT_SPECIFIED',
userDataConsent: 'NOT_SPECIFIED',
},
},
},
};
const result = prepareUserConsents(message);
expect(result).toEqual({});
});

it('Should return a valid consents values from consents object', () => {
const message = {
integrations: {
GA4: {
consents: {
personalizationConsent: 'NOT_SPECIFIED',
userDataConsent: 'DENIED',
},
},
},
};
const result = prepareUserConsents(message);
expect(result).toEqual({
ad_user_data: 'DENIED',
});
});
});
});
14 changes: 11 additions & 3 deletions src/v0/util/googleUtils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ const finaliseConsent = (consentConfigMap, eventLevelConsent = {}, destConfig =
return consentObj;
};

/**
* Populates the consent object based on the provided configuration and consent mapping.
* @param {*} consentConfigMap
* @param {*} eventLevelConsent
* @returns
*/
const finaliseAnalyticsConsents = (consentConfigMap, eventLevelConsent = {}) => {
const consentObj = {};
// Iterate through each key in consentConfigMap to set the consent
Expand All @@ -92,12 +98,14 @@ const finaliseAnalyticsConsents = (consentConfigMap, eventLevelConsent = {}) =>
// Set consent only if valid
if (
eventLevelConsent &&
eventLevelConsent.hasOwnProperty(consentKey) &&
GA4_ALLOWED_CONSENT_STATUS.includes(eventLevelConsent[consentKey])
eventLevelConsent.hasOwnProperty(configKey) &&
GA4_ALLOWED_CONSENT_STATUS.includes(eventLevelConsent[configKey])
) {
consentObj[consentKey] = eventLevelConsent[consentKey];
consentObj[consentKey] = eventLevelConsent[configKey];
}
});

return consentObj;
};

module.exports = {
Expand Down
52 changes: 51 additions & 1 deletion src/v0/util/googleUtils/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const { populateConsentFromConfig, finaliseConsent } = require('./index');
const {
finaliseConsent,
populateConsentFromConfig,
finaliseAnalyticsConsents,
} = require('./index');

describe('unit test for populateConsentFromConfig', () => {
const consentConfigMap = {
Expand Down Expand Up @@ -243,3 +247,49 @@ describe('finaliseConsent', () => {
});
});
});

describe('unit test for finaliseAnalyticsConsents', () => {
const consentConfigMap = {
personalizationConsent: 'ad_personalization',
userDataConsent: 'ad_user_data',
};
it('Should return an empty object when no valid consents are provided', () => {
const result = finaliseAnalyticsConsents(consentConfigMap, {});
expect(result).toEqual({});
});

it('Should set ad_user_data property of consent object when userDataConsent property is provided and its value is one of the allowed consent statuses', () => {
const properties = { userDataConsent: 'GRANTED' };
const result = finaliseAnalyticsConsents(consentConfigMap, properties);
expect(result).toEqual({ ad_user_data: 'GRANTED' });
});

it('Should set ad_personalization property of consent object when personalizationConsent property is provided and its value is one of the allowed consent statuses', () => {
const properties = { personalizationConsent: 'DENIED' };
const result = finaliseAnalyticsConsents(consentConfigMap, properties);
expect(result).toEqual({ ad_personalization: 'DENIED' });
});

it('Should return an empty object when properties parameter is not provided', () => {
const result = finaliseAnalyticsConsents(consentConfigMap, undefined);
expect(result).toEqual({});
});

it('Should return an empty object when properties parameter is null', () => {
const result = finaliseAnalyticsConsents(consentConfigMap, null);
expect(result).toEqual({});
});

it('Should return an empty object when properties parameter is an UNSPECIFIED object', () => {
const result = finaliseAnalyticsConsents(consentConfigMap, {});
expect(result).toEqual({});
});

it('should return empty object when properties parameter contains ad_user_data and ad_personalization with non-allowed values', () => {
const result = finaliseAnalyticsConsents(consentConfigMap, {
userDataConsent: 'RANDOM',
personalizationConsent: 'RANDOM',
});
expect(result).toEqual({});
});
});
Loading

0 comments on commit 71445df

Please sign in to comment.