Skip to content

Commit

Permalink
fix:send default and update consent payloads on kit init (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmustafa-tse authored Oct 1, 2024
1 parent b119f9f commit 97e9a2e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 43 deletions.
66 changes: 38 additions & 28 deletions src/GoogleAdWordsEventForwarder.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,8 @@
// If consent payload is empty,
// we never sent an initial default consent state
// so we shouldn't send an update.
if (consentPayloadAsString && forwarderSettings.consentMappingWeb) {
var eventConsentState = getEventConsentState(event.ConsentState);

if (!isEmpty(eventConsentState)) {
var updatedConsentPayload = generateConsentStatePayloadFromMappings(
eventConsentState,
consentMappings,
);

var eventConsentAsString =
JSON.stringify(updatedConsentPayload);

if (eventConsentAsString !== consentPayloadAsString) {
sendGtagConsentUpdate(updatedConsentPayload);
consentPayloadAsString = eventConsentAsString;
}
}
}
var eventConsentState = getEventConsentState(event.ConsentState);
maybeSendConsentUpdateToGoogle(eventConsentState)

if (
forwarderSettings.enableEnhancedConversions ===
Expand Down Expand Up @@ -360,8 +344,33 @@
return true;
}

function maybeSendConsentUpdateToGoogle(consentState) {
if (
consentPayloadAsString &&
forwarderSettings.consentMappingWeb &&
!isEmpty(consentState)
) {
var updatedConsentPayload = generateConsentStatePayloadFromMappings(
consentState,
consentMappings,
);

var eventConsentAsString =
JSON.stringify(updatedConsentPayload);

if (eventConsentAsString !== consentPayloadAsString) {
sendGtagConsentUpdate(updatedConsentPayload);
consentPayloadAsString = eventConsentAsString;
}
}
}

function sendGtagConsentDefaults(payload) {
// https://go.mparticle.com/work/SQDSDKS-6165
consentPayloadAsString = JSON.stringify(
payload
);

try {
gtag('consent', 'default', payload);
} catch (e) {
Expand Down Expand Up @@ -558,21 +567,22 @@
}

consentPayloadDefaults = getConsentSettings();
var initialConsentState = getUserConsentState();

var defaultConsentPayload =
var defaultConsentPayload = cloneObject(consentPayloadDefaults);
var updatedConsentState = getUserConsentState();
var updatedDefaultConsentPayload =
generateConsentStatePayloadFromMappings(
initialConsentState,
updatedConsentState,
consentMappings
);

if(!isEmpty(defaultConsentPayload)) {
consentPayloadAsString = JSON.stringify(
defaultConsentPayload
);

sendGtagConsentDefaults(defaultConsentPayload);
if (!isEmpty(defaultConsentPayload)) {
sendGtagConsentDefaults(defaultConsentPayload)
} else if (!isEmpty(updatedDefaultConsentPayload)) {
sendGtagConsentDefaults(updatedDefaultConsentPayload)
}

maybeSendConsentUpdateToGoogle(updatedConsentState)

}

forwarderSettings.remarketingOnly = forwarderSettings.remarketingOnly == 'True';
Expand Down
60 changes: 45 additions & 15 deletions test/src/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,7 @@ describe('Adwords forwarder', function () {
done();
});

it('should merge Consent Setting Defaults with User Consent State to construct a Default Consent State', function (done) {
it('should construct a Default Consent State Payload from Default Settings and construct an Update Consent State Payload from Mappings', function (done) {
mParticle.forwarder.init(
{
conversionId: 'AW-123123123',
Expand All @@ -1242,9 +1242,20 @@ describe('Adwords forwarder', function () {
true
);

var expectedDataLayer = [
var expectedDataLayer1 = [
'consent',
'default',
{
ad_personalization: 'granted', // From Consent Settings
ad_user_data: 'granted', // From Consent Settings
ad_storage: 'granted', // From Consent Settings
analytics_storage: 'granted', // From Consent Settings
},
];

var expectedDataLayer2 = [
'consent',
'update',
{
ad_personalization: 'denied', // From User Consent State
ad_user_data: 'denied', // From User Consent State
Expand All @@ -1253,10 +1264,13 @@ describe('Adwords forwarder', function () {
},
];

window.dataLayer.length.should.eql(1);
window.dataLayer.length.should.eql(2);
window.dataLayer[0][0].should.equal('consent');
window.dataLayer[0][1].should.equal('default');
window.dataLayer[0][2].should.deepEqual(expectedDataLayer[2]);
window.dataLayer[0][2].should.deepEqual(expectedDataLayer1[2]);
window.dataLayer[1][0].should.equal('consent');
window.dataLayer[1][1].should.equal('update');
window.dataLayer[1][2].should.deepEqual(expectedDataLayer2[2]);

done();
});
Expand Down Expand Up @@ -1465,7 +1479,18 @@ describe('Adwords forwarder', function () {
true
);

var expectedDataLayerBefore = [
var expectedDataLayerBefore1 = [
'consent',
'default',
{
ad_personalization: 'granted', // From Consent Settings
ad_user_data: 'granted', // From Consent Settings
ad_storage: 'granted', // From Consent Settings
analytics_storage: 'granted', // From Consent Settings
},
];

var expectedDataLayerBefore2 = [
'consent',
'update',
{
Expand All @@ -1476,11 +1501,16 @@ describe('Adwords forwarder', function () {
},
];

window.dataLayer.length.should.eql(1);
window.dataLayer.length.should.eql(2);
window.dataLayer[0][0].should.equal('consent');
window.dataLayer[0][1].should.equal('default');
window.dataLayer[0][2].should.deepEqual(
expectedDataLayerBefore[2]
expectedDataLayerBefore1[2]
);
window.dataLayer[1][0].should.equal('consent');
window.dataLayer[1][1].should.equal('update');
window.dataLayer[1][2].should.deepEqual(
expectedDataLayerBefore2[2]
);

mParticle.forwarder.process({
Expand Down Expand Up @@ -1536,10 +1566,10 @@ describe('Adwords forwarder', function () {
},
];

window.dataLayer.length.should.eql(2);
window.dataLayer[1][0].should.equal('consent');
window.dataLayer[1][1].should.equal('update');
window.dataLayer[1][2].should.deepEqual(
window.dataLayer.length.should.eql(3);
window.dataLayer[2][0].should.equal('consent');
window.dataLayer[2][1].should.equal('update');
window.dataLayer[2][2].should.deepEqual(
expectedDataLayerAfter[2]
);

Expand Down Expand Up @@ -1606,10 +1636,10 @@ describe('Adwords forwarder', function () {
},
];

window.dataLayer.length.should.eql(3);
window.dataLayer[2][0].should.equal('consent');
window.dataLayer[2][1].should.equal('update');
window.dataLayer[2][2].should.deepEqual(
window.dataLayer.length.should.eql(4);
window.dataLayer[3][0].should.equal('consent');
window.dataLayer[3][1].should.equal('update');
window.dataLayer[3][2].should.deepEqual(
expectedDataLayerFinal[2]
);
done();
Expand Down

0 comments on commit 97e9a2e

Please sign in to comment.