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 8, 2024
1 parent 24ca467 commit 2f05225
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 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 @@ -107,6 +107,23 @@ describe('GA4 Integration', () => {
expect(GA4.shouldLoad({ statistics: true }, {})).toBe(true);

Check failure on line 107 in packages/react/src/analytics/integrations/GA4/__tests__/GA4.test.ts

View workflow job for this annotation

GitHub Actions / CI

Argument of type '{}' is not assignable to parameter of type 'GA4IntegrationOptions'.
});

it('`shouldLoad` should return true if there google consent mode was assigned in advancedMode', () => {
expect(
GA4.shouldLoad(
{},
{

Check failure on line 114 in packages/react/src/analytics/integrations/GA4/__tests__/GA4.test.ts

View workflow job for this annotation

GitHub Actions / CI

Argument of type '{ googleConsentConfig: { ad_personalization: {}; ad_storage: {}; ad_user_data: {}; analytics_storage: {}; mode: "Advanced"; }; }' is not assignable to parameter of type 'GA4IntegrationOptions'.
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 @@ -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 2f05225

Please sign in to comment.