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

fix:send default and update consent payloads on kit init #58

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
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]);
Comment on lines 1268 to +1270
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had similar feedback for alex here - you can probably refactor these 2 checks for the [0] and [1] index to be something like:

window.dataLayer[0].should.deepEqual(expectedDataLayer1)
window.dataLayer[1].should.deepEqual(expectedDataLayer2)

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]
Comment on lines +1504 to +1513
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment about doing deepEqual to the full object

);

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]
Comment on lines +1570 to 1573
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto to above

);

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]
Comment on lines +1639 to 1643
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tritto to above

);
done();
Expand Down
Loading