diff --git a/src/common.js b/src/common.js index e7f2dd8..380f40c 100644 --- a/src/common.js +++ b/src/common.js @@ -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) { @@ -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]; diff --git a/src/event-handler.js b/src/event-handler.js index cf7058e..4c9249a 100644 --- a/src/event-handler.js +++ b/src/event-handler.js @@ -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) { diff --git a/src/initialization.js b/src/initialization.js index 2f322b1..b4df68b 100644 --- a/src/initialization.js +++ b/src/initialization.js @@ -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); diff --git a/test/tests.js b/test/tests.js index 1d4a510..677ecae 100644 --- a/test/tests.js +++ b/test/tests.js @@ -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 @@ -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 = [ { @@ -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 @@ -579,6 +609,7 @@ describe('DoubleClick', function () { defaultAnalyticsStorageConsentWeb: 'Granted', eventMapping: '[]', customVariables: '[]', + customParams: '[]', }, reportService.cb, true @@ -631,6 +662,7 @@ describe('DoubleClick', function () { defaultAnalyticsStorageConsentWeb: 'Unspecified', eventMapping: '[]', customVariables: '[]', + customParams: '[]', }, reportService.cb, true @@ -663,6 +695,7 @@ describe('DoubleClick', function () { consentMappingWeb: JSON.stringify(consentMap), eventMapping: '[]', customVariables: '[]', + customParams: '[]', }, reportService.cb, true @@ -829,6 +862,7 @@ describe('DoubleClick', function () { defaultAnalyticsStorageConsentWeb: 'Granted', eventMapping: '[]', customVariables: '[]', + customParams: '[]', }, reportService.cb, true @@ -1010,6 +1044,7 @@ describe('DoubleClick', function () { consentMappingWeb: JSON.stringify(consentMap), eventMapping: '[]', customVariables: '[]', + customParams: '[]', }, reportService.cb, true @@ -1074,6 +1109,7 @@ describe('DoubleClick', function () { enableGtag: 'True', eventMapping: '[]', customVariables: '[]', + customParams: '[]', }, reportService.cb, true @@ -1153,6 +1189,7 @@ describe('DoubleClick', function () { defaultAnalyticsStorageConsentWeb: 'Denied', eventMapping: '[]', customVariables: '[]', + customParams: '[]', }, reportService.cb, true