Skip to content

Commit e30b968

Browse files
gh-mmurphrmi22186
authored andcommitted
feat: Add custom field mapping (#49)
1 parent 502622d commit e30b968

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

src/common.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ function Common() {
99

1010
Common.prototype.eventMapping = {};
1111
Common.prototype.customVariablesMappings = {};
12+
Common.prototype.customFieldMappings = {};
1213
Common.prototype.settings = {};
1314
Common.prototype.setCustomVariables = function(event, gtagProperties) {
1415
for (var attribute in event.EventAttributes) {
@@ -18,6 +19,20 @@ Common.prototype.setCustomVariables = function(event, gtagProperties) {
1819
}
1920
}
2021
};
22+
Common.prototype.setCustomFields = function(event, gtagProperties) {
23+
var dc_custom_params = {};
24+
var hasMappings = false;
25+
for (var attribute in event.EventAttributes) {
26+
if (this.customFieldMappings[attribute]) {
27+
dc_custom_params[this.customFieldMappings[attribute]] =
28+
event.EventAttributes[attribute];
29+
hasMappings = true;
30+
}
31+
}
32+
if (hasMappings) {
33+
gtagProperties["dc_custom_params"] = dc_custom_params;
34+
}
35+
};
2136
Common.prototype.setSendTo = function(mapping, customFlags, gtagProperties) {
2237
var tags = mapping.value.split(';');
2338
var groupTag = tags[0];

src/event-handler.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ EventHandler.prototype.logEvent = function (event) {
1818

1919
var gtagProperties = {};
2020
this.common.setCustomVariables(event, gtagProperties);
21+
this.common.setCustomFields(event, gtagProperties);
2122
var eventMapping = this.common.getEventMapping(event);
2223

2324
if (!eventMapping) {

src/initialization.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ function initializeGoogleDFP(common, settings, isInitialized) {
7979
a[b.map] = b.value;
8080
return a;
8181
}, {});
82+
common.customFieldMappings = parseSettingsString(
83+
settings.customParams
84+
).reduce(function (a, b) {
85+
a[b.map] = b.value;
86+
return a;
87+
}, {});
8288
common.sendGtag('js', new Date(), true);
8389
common.sendGtag('allow_custom_scripts', true, true);
8490
common.sendGtag('config', settings.advertiserId, true);

test/tests.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ describe('DoubleClick', function () {
161161
var sdkSettings = {
162162
advertiserId: '123456',
163163
customVariables: '[{"jsmap":null,"map":"Total Amount","maptype":"EventAttributeClass.Name","value":"u1"},{"jsmap":null,"map":"color","maptype":"EventAttributeClass.Name","value":"u2"}]',
164+
customParams: '[{"jsmap":null,"map":"product_id","maptype":"EventAttributeClass.Name","value":"dc_product_id"},{"jsmap":null,"map":"category","maptype":"EventAttributeClass.Name","value":"dc_category"}]',
164165
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"}]'
165166
};
166167
// You may require userAttributes or userIdentities to be passed into initialization
@@ -504,6 +505,34 @@ describe('DoubleClick', function () {
504505
done();
505506
});
506507

508+
it('should log event with custom field mappings', function(done) {
509+
window.dataLayer = [];
510+
mParticle.forwarder.process({
511+
EventDataType: MessageTypes.PageEvent,
512+
EventCategory: mParticle.EventType.Unknown,
513+
EventName: 'Test Event',
514+
EventAttributes: {
515+
'product_id': '12345',
516+
'category': 'electronics',
517+
'Total Amount': 123,
518+
'color': 'blue'
519+
},
520+
CustomFlags: {
521+
'DoubleClick.Counter': 'standard'
522+
}
523+
});
524+
window.dataLayer[0][0].should.equal('event');
525+
window.dataLayer[0][1].should.equal('conversion');
526+
window.dataLayer[0][2].should.have.property('u1', 123);
527+
window.dataLayer[0][2].should.have.property('u2', 'blue');
528+
window.dataLayer[0][2].should.have.property('dc_custom_params');
529+
window.dataLayer[0][2].dc_custom_params.should.have.property('dc_product_id', '12345');
530+
window.dataLayer[0][2].dc_custom_params.should.have.property('dc_category', 'electronics');
531+
window.dataLayer[0][2].should.have.property('send_to', 'DC-123456/group tag2/activity tag2+standard');
532+
533+
done();
534+
});
535+
507536
describe('Consent State', function () {
508537
var consentMap = [
509538
{
@@ -544,6 +573,7 @@ describe('DoubleClick', function () {
544573
'[{"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"}]',
545574
eventMapping: '[]',
546575
customVariables: '[]',
576+
customParams: '[]',
547577
},
548578
reportService.cb,
549579
true
@@ -579,6 +609,7 @@ describe('DoubleClick', function () {
579609
defaultAnalyticsStorageConsentWeb: 'Granted',
580610
eventMapping: '[]',
581611
customVariables: '[]',
612+
customParams: '[]',
582613
},
583614
reportService.cb,
584615
true
@@ -631,6 +662,7 @@ describe('DoubleClick', function () {
631662
defaultAnalyticsStorageConsentWeb: 'Unspecified',
632663
eventMapping: '[]',
633664
customVariables: '[]',
665+
customParams: '[]',
634666
},
635667
reportService.cb,
636668
true
@@ -663,6 +695,7 @@ describe('DoubleClick', function () {
663695
consentMappingWeb: JSON.stringify(consentMap),
664696
eventMapping: '[]',
665697
customVariables: '[]',
698+
customParams: '[]',
666699
},
667700
reportService.cb,
668701
true
@@ -829,6 +862,7 @@ describe('DoubleClick', function () {
829862
defaultAnalyticsStorageConsentWeb: 'Granted',
830863
eventMapping: '[]',
831864
customVariables: '[]',
865+
customParams: '[]',
832866
},
833867
reportService.cb,
834868
true
@@ -1010,6 +1044,7 @@ describe('DoubleClick', function () {
10101044
consentMappingWeb: JSON.stringify(consentMap),
10111045
eventMapping: '[]',
10121046
customVariables: '[]',
1047+
customParams: '[]',
10131048
},
10141049
reportService.cb,
10151050
true
@@ -1074,6 +1109,7 @@ describe('DoubleClick', function () {
10741109
enableGtag: 'True',
10751110
eventMapping: '[]',
10761111
customVariables: '[]',
1112+
customParams: '[]',
10771113
},
10781114
reportService.cb,
10791115
true
@@ -1153,6 +1189,7 @@ describe('DoubleClick', function () {
11531189
defaultAnalyticsStorageConsentWeb: 'Denied',
11541190
eventMapping: '[]',
11551191
customVariables: '[]',
1192+
customParams: '[]',
11561193
},
11571194
reportService.cb,
11581195
true

0 commit comments

Comments
 (0)