Skip to content

Commit b39224d

Browse files
danielbento92boliveira
authored andcommitted
feat: add advanced mode to gcm
1 parent 0fc45f7 commit b39224d

File tree

5 files changed

+51
-19
lines changed

5 files changed

+51
-19
lines changed

packages/react/src/analytics/integrations/AnalyticsApi/AnalyticsApi.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ import isArray from 'lodash/isArray';
99
import omit from 'lodash/omit';
1010

1111
export default class AnalyticsAPI extends integrations.Integration {
12-
static [utils.CONSENT_CATEGORIES_PROPERTY] =
13-
utils.DefaultConsentKeys.MARKETING;
14-
1512
/**
1613
* Creates an instance of Analytics Api integration.
1714
*
@@ -59,6 +56,15 @@ export default class AnalyticsAPI extends integrations.Integration {
5956
: undefined;
6057
}
6158

59+
/**
60+
* Returns true due to being a required integration - No need to check for consent.
61+
*
62+
* @returns {boolean} A value indicating if the integration should load.
63+
*/
64+
static shouldLoad() {
65+
return true;
66+
}
67+
6268
/**
6369
* Creates and returns the instance for the Analytics API integration.
6470
*

packages/react/src/analytics/integrations/GA4/GA4.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class GA4 extends integrations.Integration {
139139
* Sets the consent object.
140140
* This method is called by analytics whenever the consent changes, so there's no need to validate if it has changed or not.
141141
*
142-
* @param {object} consent - Object to be written on the dataLayer.
142+
* @param {object} consent - User consent data.
143143
*
144144
* @returns {GA4} This allows chaining of class methods.
145145
*/
@@ -149,6 +149,22 @@ class GA4 extends integrations.Integration {
149149
return this;
150150
}
151151

152+
/**
153+
* Method to check if the integration is ready to be loaded.
154+
*
155+
* @param {object} consent - User consent data.
156+
* @param {object} options - Options passed for the GA4 integration.
157+
*
158+
* @returns {boolean} If the integration is ready to be loaded.
159+
*/
160+
static shouldLoad(consent, options) {
161+
if (get(options, `${OPTION_GOOGLE_CONSENT_CONFIG}.mode`) === 'Advanced') {
162+
return true;
163+
}
164+
165+
return super.shouldLoad(consent, options);
166+
}
167+
152168
/**
153169
* Returns the data layer name.
154170
*

packages/react/src/analytics/integrations/__tests__/AnalyticsApi.test.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,6 @@ describe('AnalyticsApi Integration', () => {
4343
expect(AnalyticsApi.prototype).toBeInstanceOf(integrations.Integration);
4444
});
4545

46-
it('`shouldLoad` should return false if there is no user consent', () => {
47-
expect(AnalyticsApi.shouldLoad({ marketing: false }, {})).toBe(false);
48-
expect(AnalyticsApi.shouldLoad({}, {})).toBe(false);
49-
});
50-
51-
it('`shouldLoad` should return true if there is user consent', () => {
52-
expect(AnalyticsApi.shouldLoad({ marketing: true }, {})).toBe(true);
53-
});
54-
5546
describe('AnalyticsApi Instance', () => {
5647
let analyticsApiInstance;
5748

packages/react/src/analytics/integrations/__tests__/GA4.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,23 @@ describe('GA4 Integration', () => {
7373
expect(GA4.shouldLoad({ statistics: true })).toBe(true);
7474
});
7575

76+
it('`shouldLoad` should return true if google consent mode option was passed as `Advanced`', () => {
77+
expect(
78+
GA4.shouldLoad(
79+
{},
80+
{
81+
googleConsentConfig: {
82+
ad_personalization: {},
83+
ad_storage: {},
84+
ad_user_data: {},
85+
analytics_storage: {},
86+
mode: 'Advanced',
87+
},
88+
},
89+
),
90+
).toBe(true);
91+
});
92+
7693
describe('GA4 instance', () => {
7794
let ga4Instance;
7895

packages/react/src/analytics/integrations/shared/GoogleConsentMode/GoogleConsentMode.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ export class GoogleConsentMode {
2121
this.config = config;
2222

2323
// select only the Google Consent Elements
24-
this.configExcludingRegionsAndWaitForUpdate = omit(this.config || {}, [
24+
this.configExcludingModeRegionsAndWaitForUpdate = omit(this.config || {}, [
2525
'waitForUpdate',
2626
'regions',
27+
'mode',
2728
]);
2829

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

4546
// Obtain default google consent registry
4647
const consentRegistry = Object.keys(
47-
this.configExcludingRegionsAndWaitForUpdate,
48+
this.configExcludingModeRegionsAndWaitForUpdate,
4849
).reduce((result, consentKey) => {
4950
return {
5051
...result,
5152
[consentKey]:
52-
this.configExcludingRegionsAndWaitForUpdate[consentKey]?.default ||
53-
googleConsentTypes.DENIED,
53+
this.configExcludingModeRegionsAndWaitForUpdate[consentKey]
54+
?.default || googleConsentTypes.DENIED,
5455
};
5556
}, initialValue);
5657

@@ -82,10 +83,11 @@ export class GoogleConsentMode {
8283

8384
// Fill consent value into consent element, using analytics consent categories
8485
const consentRegistry = Object.keys(
85-
this.configExcludingRegionsAndWaitForUpdate,
86+
this.configExcludingModeRegionsAndWaitForUpdate,
8687
).reduce((result, consentKey) => {
8788
let consentValue = googleConsentTypes.DENIED;
88-
const consent = this.configExcludingRegionsAndWaitForUpdate[consentKey];
89+
const consent =
90+
this.configExcludingModeRegionsAndWaitForUpdate[consentKey];
8991

9092
if (consent) {
9193
// has consent config key

0 commit comments

Comments
 (0)