From b39224d449b220089028f3b4b9f84f06459d500e Mon Sep 17 00:00:00 2001 From: "daniel.bento" Date: Thu, 8 Feb 2024 12:38:15 +0000 Subject: [PATCH] feat: add advanced mode to gcm --- .../integrations/AnalyticsApi/AnalyticsApi.js | 12 +++++++++--- .../src/analytics/integrations/GA4/GA4.js | 18 +++++++++++++++++- .../__tests__/AnalyticsApi.test.js | 9 --------- .../integrations/__tests__/GA4.test.js | 17 +++++++++++++++++ .../GoogleConsentMode/GoogleConsentMode.js | 14 ++++++++------ 5 files changed, 51 insertions(+), 19 deletions(-) diff --git a/packages/react/src/analytics/integrations/AnalyticsApi/AnalyticsApi.js b/packages/react/src/analytics/integrations/AnalyticsApi/AnalyticsApi.js index b0f388ef6..28b06b121 100644 --- a/packages/react/src/analytics/integrations/AnalyticsApi/AnalyticsApi.js +++ b/packages/react/src/analytics/integrations/AnalyticsApi/AnalyticsApi.js @@ -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. * @@ -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. * diff --git a/packages/react/src/analytics/integrations/GA4/GA4.js b/packages/react/src/analytics/integrations/GA4/GA4.js index 95564a94d..38431ea47 100644 --- a/packages/react/src/analytics/integrations/GA4/GA4.js +++ b/packages/react/src/analytics/integrations/GA4/GA4.js @@ -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. */ @@ -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. * diff --git a/packages/react/src/analytics/integrations/__tests__/AnalyticsApi.test.js b/packages/react/src/analytics/integrations/__tests__/AnalyticsApi.test.js index 1938cb118..247ebf2b1 100644 --- a/packages/react/src/analytics/integrations/__tests__/AnalyticsApi.test.js +++ b/packages/react/src/analytics/integrations/__tests__/AnalyticsApi.test.js @@ -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; diff --git a/packages/react/src/analytics/integrations/__tests__/GA4.test.js b/packages/react/src/analytics/integrations/__tests__/GA4.test.js index 4d7c2e082..5ca58603b 100644 --- a/packages/react/src/analytics/integrations/__tests__/GA4.test.js +++ b/packages/react/src/analytics/integrations/__tests__/GA4.test.js @@ -73,6 +73,23 @@ describe('GA4 Integration', () => { expect(GA4.shouldLoad({ statistics: true })).toBe(true); }); + it('`shouldLoad` should return true if google consent mode option was passed as `Advanced`', () => { + expect( + GA4.shouldLoad( + {}, + { + googleConsentConfig: { + ad_personalization: {}, + ad_storage: {}, + ad_user_data: {}, + analytics_storage: {}, + mode: 'Advanced', + }, + }, + ), + ).toBe(true); + }); + describe('GA4 instance', () => { let ga4Instance; diff --git a/packages/react/src/analytics/integrations/shared/GoogleConsentMode/GoogleConsentMode.js b/packages/react/src/analytics/integrations/shared/GoogleConsentMode/GoogleConsentMode.js index ad7e9eaac..ef2b12ec4 100644 --- a/packages/react/src/analytics/integrations/shared/GoogleConsentMode/GoogleConsentMode.js +++ b/packages/react/src/analytics/integrations/shared/GoogleConsentMode/GoogleConsentMode.js @@ -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); @@ -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); @@ -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