Skip to content

Commit

Permalink
feat: add advanced mode gcm in
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbento92 committed Feb 9, 2024
1 parent 24ca467 commit 8f8c7ad
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
19 changes: 19 additions & 0 deletions packages/react/src/analytics/integrations/GA4/GA4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,25 @@ class GA4 extends integrations.Integration<GA4IntegrationOptions> {
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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ function getWindowGa4Spy() {
}

describe('GA4 Integration', () => {
const validOptions = {
measurementId: 'GA-123456-12',
};

beforeEach(() => {
jest.clearAllMocks();
});
Expand All @@ -99,21 +103,39 @@ describe('GA4 Integration', () => {
});

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

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

it('`shouldLoad` should return true if there google consent mode was assigned in advancedMode', () => {
expect(
GA4.shouldLoad(
{},
{
...validOptions,
googleConsentConfig: {
ad_personalization: {},
ad_storage: {},
ad_user_data: {},
analytics_storage: {},
mode: 'Advanced',
},
},
),
).toBe(true);
});

describe('GA4 instance', () => {
let ga4Instance;

const validOptions = {
measurementId: 'GA-123456-12',
};

const loadData: LoadIntegrationEventData = {
...loadIntegrationData,
user: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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,
);
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ export type GoogleConsentModeConfig =
GoogleConsentMappingsBase<GoogleConsentCategoryConfig> & {
regions?: Array<GoogleConsentRegionConfig>;
waitForUpdate?: number;
mode?: 'Basic' | 'Advanced';
};

0 comments on commit 8f8c7ad

Please sign in to comment.