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 for main #987

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
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 google consent mode option was passed as `Advanced`', () => {
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';
};
Loading