Skip to content
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
15 changes: 15 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function Common() {

Common.prototype.eventMapping = {};
Common.prototype.customVariablesMappings = {};
Common.prototype.customFieldMappings = {};
Common.prototype.settings = {};
Common.prototype.setCustomVariables = function(event, gtagProperties) {
for (var attribute in event.EventAttributes) {
Expand All @@ -18,6 +19,20 @@ Common.prototype.setCustomVariables = function(event, gtagProperties) {
}
}
};
Common.prototype.setCustomFields = function(event, gtagProperties) {
var dc_custom_params = {};
var hasMappings = false;
for (var attribute in event.EventAttributes) {
if (this.customFieldMappings[attribute]) {
dc_custom_params[this.customFieldMappings[attribute]] =
event.EventAttributes[attribute];
hasMappings = true;
}
}
if (hasMappings) {
gtagProperties["dc_custom_params"] = dc_custom_params;
}
};
Common.prototype.setSendTo = function(mapping, customFlags, gtagProperties) {
var tags = mapping.value.split(';');
var groupTag = tags[0];
Expand Down
1 change: 1 addition & 0 deletions src/event-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ EventHandler.prototype.logEvent = function (event) {

var gtagProperties = {};
this.common.setCustomVariables(event, gtagProperties);
this.common.setCustomFields(event, gtagProperties);
var eventMapping = this.common.getEventMapping(event);

if (!eventMapping) {
Expand Down
6 changes: 6 additions & 0 deletions src/initialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ function initializeGoogleDFP(common, settings, isInitialized) {
a[b.map] = b.value;
return a;
}, {});
common.customFieldMappings = parseSettingsString(
settings.customParams
).reduce(function (a, b) {
a[b.map] = b.value;
return a;
}, {});
common.sendGtag('js', new Date(), true);
common.sendGtag('allow_custom_scripts', true, true);
common.sendGtag('config', settings.advertiserId, true);
Expand Down
37 changes: 37 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ describe('DoubleClick', function () {
var sdkSettings = {
advertiserId: '123456',
customVariables: '[{"jsmap":null,"map":"Total Amount","maptype":"EventAttributeClass.Name","value":"u1"},{"jsmap":null,"map":"color","maptype":"EventAttributeClass.Name","value":"u2"}]',
customParams: '[{"jsmap":null,"map":"product_id","maptype":"EventAttributeClass.Name","value":"dc_product_id"},{"jsmap":null,"map":"category","maptype":"EventAttributeClass.Name","value":"dc_category"}]',
eventMapping: '[{"jsmap":"-1978027768","map":"-1711833867978608722","maptype":"EventClass.Id","value":"group tag2;activity tag2"},{"jsmap":"-1107730368","map":"-3234618101041058100","maptype":"EventClass.Id","value":"group tag3;activity tag3"},{"jsmap":"-1592184962","map":"-4153695833896571372","maptype":"EventClassDetails.Id","value":"group tag4;activity tag4"}]'
};
// You may require userAttributes or userIdentities to be passed into initialization
Expand Down Expand Up @@ -504,6 +505,34 @@ describe('DoubleClick', function () {
done();
});

it('should log event with custom field mappings', function(done) {
window.dataLayer = [];
mParticle.forwarder.process({
EventDataType: MessageTypes.PageEvent,
EventCategory: mParticle.EventType.Unknown,
EventName: 'Test Event',
EventAttributes: {
'product_id': '12345',
'category': 'electronics',
'Total Amount': 123,
'color': 'blue'
},
CustomFlags: {
'DoubleClick.Counter': 'standard'
}
});
window.dataLayer[0][0].should.equal('event');
window.dataLayer[0][1].should.equal('conversion');
window.dataLayer[0][2].should.have.property('u1', 123);
window.dataLayer[0][2].should.have.property('u2', 'blue');
window.dataLayer[0][2].should.have.property('dc_custom_params');
window.dataLayer[0][2].dc_custom_params.should.have.property('dc_product_id', '12345');
window.dataLayer[0][2].dc_custom_params.should.have.property('dc_category', 'electronics');
window.dataLayer[0][2].should.have.property('send_to', 'DC-123456/group tag2/activity tag2+standard');

done();
});

describe('Consent State', function () {
var consentMap = [
{
Expand Down Expand Up @@ -544,6 +573,7 @@ describe('DoubleClick', function () {
'[{"jsmap":null,"map":"Some_consent","maptype":"ConsentPurposes","value":"ad_user_data"},{"jsmap":null,"map":"Storage_consent","maptype":"ConsentPurposes","value":"analytics_storage"},{"jsmap":null,"map":"Other_test_consent","maptype":"ConsentPurposes","value":"ad_storage"},{"jsmap":null,"map":"Test_consent","maptype":"ConsentPurposes","value":"ad_personalization"}]',
eventMapping: '[]',
customVariables: '[]',
customParams: '[]',
},
reportService.cb,
true
Expand Down Expand Up @@ -579,6 +609,7 @@ describe('DoubleClick', function () {
defaultAnalyticsStorageConsentWeb: 'Granted',
eventMapping: '[]',
customVariables: '[]',
customParams: '[]',
},
reportService.cb,
true
Expand Down Expand Up @@ -631,6 +662,7 @@ describe('DoubleClick', function () {
defaultAnalyticsStorageConsentWeb: 'Unspecified',
eventMapping: '[]',
customVariables: '[]',
customParams: '[]',
},
reportService.cb,
true
Expand Down Expand Up @@ -663,6 +695,7 @@ describe('DoubleClick', function () {
consentMappingWeb: JSON.stringify(consentMap),
eventMapping: '[]',
customVariables: '[]',
customParams: '[]',
},
reportService.cb,
true
Expand Down Expand Up @@ -829,6 +862,7 @@ describe('DoubleClick', function () {
defaultAnalyticsStorageConsentWeb: 'Granted',
eventMapping: '[]',
customVariables: '[]',
customParams: '[]',
},
reportService.cb,
true
Expand Down Expand Up @@ -1010,6 +1044,7 @@ describe('DoubleClick', function () {
consentMappingWeb: JSON.stringify(consentMap),
eventMapping: '[]',
customVariables: '[]',
customParams: '[]',
},
reportService.cb,
true
Expand Down Expand Up @@ -1074,6 +1109,7 @@ describe('DoubleClick', function () {
enableGtag: 'True',
eventMapping: '[]',
customVariables: '[]',
customParams: '[]',
},
reportService.cb,
true
Expand Down Expand Up @@ -1153,6 +1189,7 @@ describe('DoubleClick', function () {
defaultAnalyticsStorageConsentWeb: 'Denied',
eventMapping: '[]',
customVariables: '[]',
customParams: '[]',
},
reportService.cb,
true
Expand Down