From 2f0522553f025aa0ef366e7476ab03dc6ecd3c05 Mon Sep 17 00:00:00 2001 From: "daniel.bento" Date: Thu, 8 Feb 2024 12:57:01 +0000 Subject: [PATCH] feat: add advanced mode gcm in --- .../src/analytics/integrations/GA4/GA4.ts | 19 +++++++++++++++++++ .../integrations/GA4/__tests__/GA4.test.ts | 17 +++++++++++++++++ .../GoogleConsentMode/GoogleConsentMode.ts | 16 +++++++++------- .../shared/GoogleConsentMode/types.ts | 1 + 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/packages/react/src/analytics/integrations/GA4/GA4.ts b/packages/react/src/analytics/integrations/GA4/GA4.ts index 2cf194e71..cce3d7b31 100644 --- a/packages/react/src/analytics/integrations/GA4/GA4.ts +++ b/packages/react/src/analytics/integrations/GA4/GA4.ts @@ -183,6 +183,25 @@ class GA4 extends integrations.Integration { return this; } + /** + * Method to check if the integration is ready to be loaded. + * + * @param consent - User consent data. + * @param options - Options passed for the GA4 integration. + * + * @returns If the integration is ready to be loaded. + */ + static override shouldLoad( + consent: ConsentData, + options: GA4IntegrationOptions, + ) { + if (get(options, `${OPTION_GOOGLE_CONSENT_CONFIG}.mode`) === 'Advanced') { + return true; + } + + return super.shouldLoad(consent, options); + } + /** * Send page events to GA4. * diff --git a/packages/react/src/analytics/integrations/GA4/__tests__/GA4.test.ts b/packages/react/src/analytics/integrations/GA4/__tests__/GA4.test.ts index e8b1b591c..d4d8142a8 100644 --- a/packages/react/src/analytics/integrations/GA4/__tests__/GA4.test.ts +++ b/packages/react/src/analytics/integrations/GA4/__tests__/GA4.test.ts @@ -107,6 +107,23 @@ describe('GA4 Integration', () => { expect(GA4.shouldLoad({ statistics: true }, {})).toBe(true); }); + it('`shouldLoad` should return true if there google consent mode was assigned in advancedMode', () => { + 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.ts b/packages/react/src/analytics/integrations/shared/GoogleConsentMode/GoogleConsentMode.ts index 1a41c7b8f..4e14b5c75 100644 --- a/packages/react/src/analytics/integrations/shared/GoogleConsentMode/GoogleConsentMode.ts +++ b/packages/react/src/analytics/integrations/shared/GoogleConsentMode/GoogleConsentMode.ts @@ -12,7 +12,7 @@ import { isEqual, omit } from 'lodash-es'; export class GoogleConsentMode { private dataLayer!: string; // Stores different data layer names private config?: GoogleConsentModeConfig; // Stores default or customized consent category mappings - private configExcludingRegionsAndWaitForUpdate!: Record< + private configExcludingModeRegionsAndWaitForUpdate!: Record< string, GoogleConsentCategoryConfig >; // exclude not consent properties from config @@ -30,9 +30,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); @@ -52,13 +53,13 @@ export class GoogleConsentMode { // Obtain default google consent registry const consentRegistry = Object.keys( - this.configExcludingRegionsAndWaitForUpdate, + this.configExcludingModeRegionsAndWaitForUpdate, ).reduce( (result, consentKey) => ({ ...result, [consentKey]: - this.configExcludingRegionsAndWaitForUpdate[consentKey]?.default || - GoogleConsentType.Denied, + this.configExcludingModeRegionsAndWaitForUpdate[consentKey] + ?.default || GoogleConsentType.Denied, }), initialValue, ); @@ -87,10 +88,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 = GoogleConsentType.Denied; - const consent = this.configExcludingRegionsAndWaitForUpdate[consentKey]; + const consent = + this.configExcludingModeRegionsAndWaitForUpdate[consentKey]; if (consent) { // has consent config key diff --git a/packages/react/src/analytics/integrations/shared/GoogleConsentMode/types.ts b/packages/react/src/analytics/integrations/shared/GoogleConsentMode/types.ts index b9e5ac1b4..adbc06f1b 100644 --- a/packages/react/src/analytics/integrations/shared/GoogleConsentMode/types.ts +++ b/packages/react/src/analytics/integrations/shared/GoogleConsentMode/types.ts @@ -26,4 +26,5 @@ export type GoogleConsentModeConfig = GoogleConsentMappingsBase & { regions?: Array; waitForUpdate?: number; + mode?: 'Basic' | 'Advanced'; };