Skip to content

Commit

Permalink
fix: send default and update consent payloads on kit init (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmustafa-tse authored Oct 8, 2024
1 parent a576f99 commit 0cd8f36
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 59 deletions.
36 changes: 36 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,42 @@ Common.prototype.sendGtagConsent = function (type, payload) {
gtag('consent', type, payload);
};

Common.prototype.getEventConsentState = function (eventConsentState) {
return eventConsentState && eventConsentState.getGDPRConsentState
? eventConsentState.getGDPRConsentState()
: {};
};

Common.prototype.maybeSendConsentUpdateToGoogle = function (consentState) {
// If consent payload is empty,
// we never sent an initial default consent state
// so we shouldn't send an update.
if (
this.consentPayloadAsString &&
this.consentMappings &&
!this.isEmpty(consentState)
) {
var updatedConsentPayload =
this.consentHandler.generateConsentStatePayloadFromMappings(
consentState,
this.consentMappings
);

var eventConsentAsString = JSON.stringify(updatedConsentPayload);

if (eventConsentAsString !== this.consentPayloadAsString) {
this.sendGtagConsent('update', updatedConsentPayload);
this.consentPayloadAsString = eventConsentAsString;
}
}
};

Common.prototype.sendDefaultConsentPayloadToGoogle = function (consentPayload) {
this.consentPayloadAsString = JSON.stringify(consentPayload);

this.sendGtagConsent('default', consentPayload);
};

Common.prototype.cloneObject = function (obj) {
return JSON.parse(JSON.stringify(obj));
};
Expand Down
6 changes: 0 additions & 6 deletions src/consent.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ ConsentHandler.prototype.getUserConsentState = function () {
return userConsentState;
};

ConsentHandler.prototype.getEventConsentState = function (eventConsentState) {
return eventConsentState && eventConsentState.getGDPRConsentState
? eventConsentState.getGDPRConsentState()
: {};
};

ConsentHandler.prototype.getConsentSettings = function () {
var consentSettings = {};

Expand Down
31 changes: 4 additions & 27 deletions src/event-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,11 @@ function EventHandler(common) {
this.common = common || {};
}

EventHandler.prototype.maybeSendConsentUpdateToGoogle = function (event) {
// If consent payload is empty,
// we never sent an initial default consent state
// so we shouldn't send an update.
if (this.common.consentPayloadAsString && this.common.consentMappings) {
var eventConsentState = this.common.consentHandler.getEventConsentState(
event.ConsentState
);

if (!this.common.isEmpty(eventConsentState)) {
var updatedConsentPayload =
this.common.consentHandler.generateConsentStatePayloadFromMappings(
eventConsentState,
this.common.consentMappings
);

var eventConsentAsString = JSON.stringify(updatedConsentPayload);

if (eventConsentAsString !== this.common.consentPayloadAsString) {
this.common.sendGtagConsent('update', updatedConsentPayload);
this.common.consentPayloadAsString = eventConsentAsString;
}
}
}
};

EventHandler.prototype.logEvent = function (event) {
this.maybeSendConsentUpdateToGoogle(event);
var eventConsentState = this.common.getEventConsentState(
event.ConsentState
);
this.common.maybeSendConsentUpdateToGoogle(eventConsentState);

var gtagProperties = {};
this.common.setCustomVariables(event, gtagProperties);
Expand Down
24 changes: 16 additions & 8 deletions src/initialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,28 @@ var initialization = {

common.consentPayloadDefaults =
common.consentHandler.getConsentSettings();
var initialConsentState = common.consentHandler.getUserConsentState();
var defaultConsentPayload =
var defaultConsentPayload = common.cloneObject(
common.consentPayloadDefaults
);
var updatedConsentState = common.consentHandler.getUserConsentState();
var updatedDefaultConsentPayload =
common.consentHandler.generateConsentStatePayloadFromMappings(
initialConsentState,
updatedConsentState,
common.consentMappings
);


// If a default consent payload exists (as selected in the mParticle UI), set it as the default
if (!common.isEmpty(defaultConsentPayload)) {
common.consentPayloadAsString = JSON.stringify(
defaultConsentPayload
common.sendDefaultConsentPayloadToGoogle(defaultConsentPayload);
// If a default consent payload does not exist, but the user currently has updated their consent,
// send that as the default because a default must be sent
} else if (!common.isEmpty(updatedDefaultConsentPayload)) {
common.sendDefaultConsentPayloadToGoogle(
updatedDefaultConsentPayload
);

common.sendGtagConsent('default', defaultConsentPayload);
}

common.maybeSendConsentUpdateToGoogle(updatedConsentState);
},
};

Expand Down
67 changes: 49 additions & 18 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ describe('DoubleClick', function () {
done();
});

it('should merge Consent Setting Defaults with User Consent State to construct a Default Consent State', (done) => {
it('should construct a Default Consent State Payload from Default Settings and construct an Update Consent State Payload from Mappings', (done) => {
mParticle.forwarder.init(
{
conversionId: 'AW-123123123',
Expand All @@ -584,9 +584,20 @@ describe('DoubleClick', 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 @@ -597,10 +608,13 @@ describe('DoubleClick', function () {

// Initial elements of Data Layer are setup for gtag.
// Consent state should be on the bottom
window.dataLayer.length.should.eql(4);
window.dataLayer.length.should.eql(5);
window.dataLayer[3][0].should.equal('consent');
window.dataLayer[3][1].should.equal('default');
window.dataLayer[3][2].should.deepEqual(expectedDataLayer[2]);
window.dataLayer[3][2].should.deepEqual(expectedDataLayer1[2]);
window.dataLayer[4][0].should.equal('consent');
window.dataLayer[4][1].should.equal('update');
window.dataLayer[4][2].should.deepEqual(expectedDataLayer2[2]);

done();
});
Expand Down Expand Up @@ -820,7 +834,18 @@ describe('DoubleClick', 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 @@ -832,11 +857,15 @@ describe('DoubleClick', function () {
];

// Initial elements of Data Layer are setup for gtag.
// Consent state should be on the bottom
window.dataLayer.length.should.eql(4);
// Default Consent payload from default settings should be index 3
// Update Consent payload from mappings should be on the bottom (index 4)
window.dataLayer.length.should.eql(5);
window.dataLayer[3][0].should.equal('consent');
window.dataLayer[3][1].should.equal('default');
window.dataLayer[3][2].should.deepEqual(expectedDataLayerBefore[2]);
window.dataLayer[3][2].should.deepEqual(expectedDataLayerBefore1[2]);
window.dataLayer[4][0].should.equal('consent');
window.dataLayer[4][1].should.equal('update');
window.dataLayer[4][2].should.deepEqual(expectedDataLayerBefore2[2]);

mParticle.forwarder.process({
EventName: 'Test Event',
Expand Down Expand Up @@ -889,11 +918,12 @@ describe('DoubleClick', function () {

// Initial elements of Data Layer are setup for gtag.
// Consent Default is index 3
// Consent Update is index 4
window.dataLayer.length.should.eql(5);
window.dataLayer[4][0].should.equal('consent');
window.dataLayer[4][1].should.equal('update');
window.dataLayer[4][2].should.deepEqual(expectedDataLayerAfter[2]);
// Initial Consent Update from mappings is index 4
// Consent Update #2 is index 5
window.dataLayer.length.should.eql(6);
window.dataLayer[5][0].should.equal('consent');
window.dataLayer[5][1].should.equal('update');
window.dataLayer[5][2].should.deepEqual(expectedDataLayerAfter[2]);

mParticle.forwarder.process({
EventName: 'Test Event',
Expand Down Expand Up @@ -962,12 +992,13 @@ describe('DoubleClick', function () {
];
// Initial elements of Data Layer are setup for gtag.
// Consent Default is index 3
// Consent Update is index 4
// Initial Consent Update from mappings is index 4
// Consent Update #2 is index 5
window.dataLayer.length.should.eql(6);
window.dataLayer[5][0].should.equal('consent');
window.dataLayer[5][1].should.equal('update');
window.dataLayer[5][2].should.deepEqual(expectedDataLayerFinal[2]);
// Consent Update #3 is index 6
window.dataLayer.length.should.eql(7);
window.dataLayer[6][0].should.equal('consent');
window.dataLayer[6][1].should.equal('update');
window.dataLayer[6][2].should.deepEqual(expectedDataLayerFinal[2]);
done();
});

Expand Down

0 comments on commit 0cd8f36

Please sign in to comment.