Skip to content

Commit

Permalink
feat: add advanced mode to gcm
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbento92 committed Feb 8, 2024
1 parent 16d950d commit 009387b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import isArray from 'lodash/isArray';
import omit from 'lodash/omit';

export default class AnalyticsAPI extends integrations.Integration {
static [utils.CONSENT_CATEGORIES_PROPERTY] =
utils.DefaultConsentKeys.MARKETING;

/**
* Creates an instance of Analytics Api integration.
*
Expand Down Expand Up @@ -59,6 +56,15 @@ export default class AnalyticsAPI extends integrations.Integration {
: undefined;
}

/**
* Returns true due to being a required integration - No need to check for consent.
*
* @returns {boolean} A value indicating if the integration should load.
*/
static shouldLoad() {
return true;
}

/**
* Creates and returns the instance for the Analytics API integration.
*
Expand Down
18 changes: 17 additions & 1 deletion packages/react/src/analytics/integrations/GA4/GA4.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class GA4 extends integrations.Integration {
* Sets the consent object.
* This method is called by analytics whenever the consent changes, so there's no need to validate if it has changed or not.
*
* @param {object} consent - Object to be written on the dataLayer.
* @param {object} consent - User consent data.
*
* @returns {GA4} This allows chaining of class methods.
*/
Expand All @@ -149,6 +149,22 @@ class GA4 extends integrations.Integration {
return this;
}

/**
* Method to check if the integration is ready to be loaded.
*
* @param {object} consent - User consent data.
* @param {object} options - Options passed for the GA4 integration.
*
* @returns {boolean} If the integration is ready to be loaded.
*/
static shouldLoad(consent, options) {
if (get(options, `${OPTION_GOOGLE_CONSENT_CONFIG}.mode`) === 'Advanced') {
return true;
}

return super.shouldLoad(consent, options);
}

/**
* Returns the data layer name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,6 @@ describe('AnalyticsApi Integration', () => {
expect(AnalyticsApi.prototype).toBeInstanceOf(integrations.Integration);
});

it('`shouldLoad` should return false if there is no user consent', () => {
expect(AnalyticsApi.shouldLoad({ marketing: false }, {})).toBe(false);
expect(AnalyticsApi.shouldLoad({}, {})).toBe(false);
});

it('`shouldLoad` should return true if there is user consent', () => {
expect(AnalyticsApi.shouldLoad({ marketing: true }, {})).toBe(true);
});

describe('AnalyticsApi Instance', () => {
let analyticsApiInstance;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ export class GoogleConsentMode {
this.config = config;

// select only the Google Consent Elements
this.configExcludingRegionsAndWaitForUpdate = omit(this.config || {}, [
this.configExcludingModeRegionsAndWaitForUpdate = omit(this.config || {}, [
'waitForUpdate',
'regions',
'mode',
]);

this.loadDefaults(initConsent);
Expand All @@ -44,13 +45,13 @@ export class GoogleConsentMode {

// Obtain default google consent registry
const consentRegistry = Object.keys(
this.configExcludingRegionsAndWaitForUpdate,
this.configExcludingModeRegionsAndWaitForUpdate,
).reduce((result, consentKey) => {
return {
...result,
[consentKey]:
this.configExcludingRegionsAndWaitForUpdate[consentKey]?.default ||
googleConsentTypes.DENIED,
this.configExcludingModeRegionsAndWaitForUpdate[consentKey]
?.default || googleConsentTypes.DENIED,
};
}, initialValue);

Expand Down Expand Up @@ -82,10 +83,11 @@ export class GoogleConsentMode {

// Fill consent value into consent element, using analytics consent categories
const consentRegistry = Object.keys(
this.configExcludingRegionsAndWaitForUpdate,
this.configExcludingModeRegionsAndWaitForUpdate,
).reduce((result, consentKey) => {
let consentValue = googleConsentTypes.DENIED;
const consent = this.configExcludingRegionsAndWaitForUpdate[consentKey];
const consent =
this.configExcludingModeRegionsAndWaitForUpdate[consentKey];

if (consent) {
// has consent config key
Expand Down

0 comments on commit 009387b

Please sign in to comment.