Skip to content

Commit ec0bf79

Browse files
authored
fix: send default and update consent payloads on kit init (#61)
1 parent 8f1fc5b commit ec0bf79

File tree

5 files changed

+112
-64
lines changed

5 files changed

+112
-64
lines changed

packages/GA4Client/src/common.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,42 @@ Common.prototype.limitProductAttributes = function (attributes) {
139139
return this.mergeObjects(limitedProductAttributes, reservedAttributes);
140140
};
141141

142+
Common.prototype.getEventConsentState = function (eventConsentState) {
143+
return eventConsentState && eventConsentState.getGDPRConsentState
144+
? eventConsentState.getGDPRConsentState()
145+
: {};
146+
};
147+
148+
Common.prototype.maybeSendConsentUpdateToGoogle = function (consentState) {
149+
// If consent payload is empty,
150+
// we never sent an initial default consent state
151+
// so we shouldn't send an update.
152+
if (
153+
this.consentPayloadAsString &&
154+
this.consentMappings &&
155+
!this.isEmpty(consentState)
156+
) {
157+
var updatedConsentPayload =
158+
this.consentHandler.generateConsentStatePayloadFromMappings(
159+
consentState,
160+
this.consentMappings
161+
);
162+
163+
var eventConsentAsString = JSON.stringify(updatedConsentPayload);
164+
165+
if (eventConsentAsString !== this.consentPayloadAsString) {
166+
gtag('consent', 'update', updatedConsentPayload);
167+
this.consentPayloadAsString = eventConsentAsString;
168+
}
169+
}
170+
};
171+
172+
Common.prototype.sendDefaultConsentPayloadToGoogle = function (consentPayload) {
173+
this.consentPayloadAsString = JSON.stringify(consentPayload);
174+
175+
gtag('consent', 'default', consentPayload);
176+
};
177+
142178
Common.prototype.truncateEventName = function (eventName) {
143179
return truncateString(eventName, EVENT_NAME_MAX_LENGTH);
144180
};

packages/GA4Client/src/consent.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ ConsentHandler.prototype.getUserConsentState = function () {
4646
return userConsentState;
4747
};
4848

49-
ConsentHandler.prototype.getEventConsentState = function (eventConsentState) {
50-
return eventConsentState && eventConsentState.getGDPRConsentState
51-
? eventConsentState.getGDPRConsentState()
52-
: {};
53-
};
54-
5549
ConsentHandler.prototype.getConsentSettings = function () {
5650
var consentSettings = {};
5751

packages/GA4Client/src/event-handler.js

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,6 @@ function EventHandler(common) {
22
this.common = common || {};
33
}
44

5-
EventHandler.prototype.maybeSendConsentUpdateToGa4 = function (event) {
6-
// If consent payload is empty,
7-
// we never sent an initial default consent state
8-
// so we shouldn't send an update.
9-
if (this.common.consentPayloadAsString && this.common.consentMappings) {
10-
var eventConsentState = this.common.consentHandler.getEventConsentState(
11-
event.ConsentState
12-
);
13-
14-
if (!this.common.isEmpty(eventConsentState)) {
15-
var updatedConsentPayload =
16-
this.common.consentHandler.generateConsentStatePayloadFromMappings(
17-
eventConsentState,
18-
this.common.consentMappings
19-
);
20-
21-
var eventConsentAsString = JSON.stringify(updatedConsentPayload);
22-
23-
if (eventConsentAsString !== this.common.consentPayloadAsString) {
24-
gtag('consent', 'update', updatedConsentPayload);
25-
this.common.consentPayloadAsString = eventConsentAsString;
26-
}
27-
}
28-
}
29-
};
30-
315
// TODO: https://mparticle-eng.atlassian.net/browse/SQDSDKS-5715
326
EventHandler.prototype.sendEventToGA4 = function (eventName, eventAttributes) {
337
var standardizedEventName;
@@ -58,7 +32,10 @@ EventHandler.prototype.sendEventToGA4 = function (eventName, eventAttributes) {
5832
};
5933

6034
EventHandler.prototype.logEvent = function (event) {
61-
this.maybeSendConsentUpdateToGa4(event);
35+
var eventConsentState = this.common.getEventConsentState(
36+
event.ConsentState
37+
);
38+
this.common.maybeSendConsentUpdateToGoogle(eventConsentState);
6239
this.sendEventToGA4(event.EventName, event.EventAttributes);
6340
};
6441

@@ -109,7 +86,10 @@ EventHandler.prototype.logPageView = function (event) {
10986
event.EventAttributes
11087
);
11188

112-
this.maybeSendConsentUpdateToGa4(event);
89+
var eventConsentState = this.common.getEventConsentState(
90+
event.ConsentState
91+
);
92+
this.common.maybeSendConsentUpdateToGoogle(eventConsentState);
11393
this.sendEventToGA4('page_view', eventAttributes);
11494

11595
return true;

packages/GA4Client/src/initialization.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,29 @@ var initialization = {
105105

106106
common.consentPayloadDefaults =
107107
common.consentHandler.getConsentSettings();
108-
var initialConsentState = common.consentHandler.getUserConsentState();
109-
110-
var defaultConsentPayload =
108+
var defaultConsentPayload = common.cloneObject(
109+
common.consentPayloadDefaults
110+
);
111+
var updatedConsentState = common.consentHandler.getUserConsentState();
112+
var updatedDefaultConsentPayload =
111113
common.consentHandler.generateConsentStatePayloadFromMappings(
112-
initialConsentState,
114+
updatedConsentState,
113115
common.consentMappings
114116
);
115117

118+
// If a default consent payload exists (as selected in the mParticle UI), set it as the default
116119
if (!common.isEmpty(defaultConsentPayload)) {
117-
common.consentPayloadAsString = JSON.stringify(
118-
defaultConsentPayload
120+
common.sendDefaultConsentPayloadToGoogle(defaultConsentPayload);
121+
// If a default consent payload does not exist, but the user currently has updated their consent,
122+
// send that as the default because a default must be sent
123+
} else if (!common.isEmpty(updatedDefaultConsentPayload)) {
124+
common.sendDefaultConsentPayloadToGoogle(
125+
updatedDefaultConsentPayload
119126
);
120-
121-
gtag('consent', 'default', defaultConsentPayload);
122127
}
123128

129+
common.maybeSendConsentUpdateToGoogle(updatedConsentState);
130+
124131
return isInitialized;
125132
},
126133
};

packages/GA4Client/test/src/tests.js

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2628,7 +2628,7 @@ describe('Google Analytics 4 Event', function () {
26282628
done();
26292629
});
26302630

2631-
it('should merge Consent Setting Defaults with User Consent State to construct a Default Consent State', (done) => {
2631+
it('should construct a Default Consent State Payload from Default Settings and construct an Update Consent State Payload from Mappings', (done) => {
26322632
mParticle.forwarder.init(
26332633
{
26342634
conversionId: 'AW-123123123',
@@ -2643,9 +2643,20 @@ describe('Google Analytics 4 Event', function () {
26432643
true
26442644
);
26452645

2646-
var expectedDataLayer = [
2646+
var expectedDataLayer1 = [
26472647
'consent',
26482648
'default',
2649+
{
2650+
ad_personalization: 'granted', // From Consent Settings
2651+
ad_user_data: 'granted', // From Consent Settings
2652+
ad_storage: 'granted', // From Consent Settings
2653+
analytics_storage: 'granted', // From Consent Settings
2654+
},
2655+
];
2656+
2657+
var expectedDataLayer2 = [
2658+
'consent',
2659+
'update',
26492660
{
26502661
ad_personalization: 'denied', // From User Consent State
26512662
ad_user_data: 'denied', // From User Consent State
@@ -2656,10 +2667,13 @@ describe('Google Analytics 4 Event', function () {
26562667

26572668
// Initial elements of Data Layer are setup for gtag.
26582669
// Consent state should be on the bottom
2659-
window.dataLayer.length.should.eql(4);
2670+
window.dataLayer.length.should.eql(5);
26602671
window.dataLayer[3][0].should.equal('consent');
26612672
window.dataLayer[3][1].should.equal('default');
2662-
window.dataLayer[3][2].should.deepEqual(expectedDataLayer[2]);
2673+
window.dataLayer[3][2].should.deepEqual(expectedDataLayer1[2]);
2674+
window.dataLayer[4][0].should.equal('consent');
2675+
window.dataLayer[4][1].should.equal('update');
2676+
window.dataLayer[4][2].should.deepEqual(expectedDataLayer2[2]);
26632677

26642678
done();
26652679
});
@@ -2877,7 +2891,18 @@ describe('Google Analytics 4 Event', function () {
28772891
true
28782892
);
28792893

2880-
var expectedDataLayerBefore = [
2894+
var expectedDataLayerBefore1 = [
2895+
'consent',
2896+
'default',
2897+
{
2898+
ad_personalization: 'granted', // From Consent Settings
2899+
ad_user_data: 'granted', // From Consent Settings
2900+
ad_storage: 'granted', // From Consent Settings
2901+
analytics_storage: 'granted', // From Consent Settings
2902+
},
2903+
];
2904+
2905+
var expectedDataLayerBefore2 = [
28812906
'consent',
28822907
'update',
28832908
{
@@ -2889,11 +2914,15 @@ describe('Google Analytics 4 Event', function () {
28892914
];
28902915

28912916
// Initial elements of Data Layer are setup for gtag.
2892-
// Consent state should be on the bottom
2893-
window.dataLayer.length.should.eql(4);
2917+
// Default Consent payload from default settings should be index 3
2918+
// Update Consent payload from mappings should be on the bottom (index 4)
2919+
window.dataLayer.length.should.eql(5);
28942920
window.dataLayer[3][0].should.equal('consent');
28952921
window.dataLayer[3][1].should.equal('default');
2896-
window.dataLayer[3][2].should.deepEqual(expectedDataLayerBefore[2]);
2922+
window.dataLayer[3][2].should.deepEqual(expectedDataLayerBefore1[2]);
2923+
window.dataLayer[4][0].should.equal('consent');
2924+
window.dataLayer[4][1].should.equal('update');
2925+
window.dataLayer[4][2].should.deepEqual(expectedDataLayerBefore2[2]);
28972926

28982927
mParticle.forwarder.process({
28992928
EventName: 'Homepage',
@@ -2950,12 +2979,13 @@ describe('Google Analytics 4 Event', function () {
29502979

29512980
// Initial elements of Data Layer are setup for gtag.
29522981
// Consent Default is index 3
2953-
// Consent Update is index 4
2954-
// Event is index 5
2955-
window.dataLayer.length.should.eql(6);
2956-
window.dataLayer[4][0].should.equal('consent');
2957-
window.dataLayer[4][1].should.equal('update');
2958-
window.dataLayer[4][2].should.deepEqual(expectedDataLayerAfter[2]);
2982+
// Initial Consent Update from mappings is index 4
2983+
// Consent Update #2 is index 5
2984+
// Event is index 6
2985+
window.dataLayer.length.should.eql(7);
2986+
window.dataLayer[5][0].should.equal('consent');
2987+
window.dataLayer[5][1].should.equal('update');
2988+
window.dataLayer[5][2].should.deepEqual(expectedDataLayerAfter[2]);
29592989

29602990
mParticle.forwarder.process({
29612991
EventName: 'Homepage',
@@ -3022,14 +3052,15 @@ describe('Google Analytics 4 Event', function () {
30223052

30233053
// Initial elements of Data Layer are setup for gtag.
30243054
// Consent Default is index 3
3025-
// Consent Update is index 4
3026-
// Event is index 5
3027-
// Consent Update #2 is index 6
3028-
// Event #2 is index 7
3029-
window.dataLayer.length.should.eql(8);
3030-
window.dataLayer[6][0].should.equal('consent');
3031-
window.dataLayer[6][1].should.equal('update');
3032-
window.dataLayer[6][2].should.deepEqual(expectedDataLayerFinal[2]);
3055+
// Initial Consent Update from mappings is index 4
3056+
// Consent Update #2 is index 5
3057+
// Event is index 6
3058+
// Consent Update #3 is index 7
3059+
// Event #2 is index 8
3060+
window.dataLayer.length.should.eql(9);
3061+
window.dataLayer[7][0].should.equal('consent');
3062+
window.dataLayer[7][1].should.equal('update');
3063+
window.dataLayer[7][2].should.deepEqual(expectedDataLayerFinal[2]);
30333064
done();
30343065
});
30353066

0 commit comments

Comments
 (0)