Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat add advanced mode gcm on v1 #986

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
17 changes: 17 additions & 0 deletions packages/react/src/analytics/integrations/__tests__/GA4.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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
Loading