Skip to content

Commit a691450

Browse files
committed
extend product attribute mapping to AddToCart and Checkout event
1 parent db0e1d6 commit a691450

2 files changed

Lines changed: 164 additions & 7 deletions

File tree

src/FacebookEventForwarder.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,16 @@ var name = 'Facebook',
216216
}
217217
else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToCart){
218218
eventName = ADD_TO_CART_EVENT_NAME;
219+
if (event.ProductAction.TransactionId) {
220+
params['order_id'] = event.ProductAction.TransactionId;
221+
}
222+
223+
// Build contents array for Purchase events
224+
var contents = buildProductContents(event.ProductAction.ProductList);
225+
if (contents && contents.length > 0) {
226+
params['contents'] = contents;
227+
}
228+
219229
}
220230
else{
221231
eventName = VIEW_CONTENT_EVENT_NAME;
@@ -244,11 +254,9 @@ var name = 'Facebook',
244254
}
245255

246256
// Build contents array for Purchase events
247-
if (event.ProductAction.ProductActionType == mParticle.ProductActionType.Purchase) {
248-
var contents = buildProductContents(event.ProductAction.ProductList);
249-
if (contents && contents.length > 0) {
250-
params['contents'] = contents;
251-
}
257+
var contents = buildProductContents(event.ProductAction.ProductList);
258+
if (contents && contents.length > 0) {
259+
params['contents'] = contents;
252260
}
253261
}
254262
else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.RemoveFromCart) {

test/tests.js

Lines changed: 151 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ describe('Facebook Forwarder', function () {
371371
window.fbqObj.params.should.have.property('eventAttr1', 'value1');
372372
window.fbqObj.params.should.have.property('eventAttr2', 'value2');
373373
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
374+
window.fbqObj.params.should.have.property('order_id', 123);
375+
window.fbqObj.params.should.have.property('contents');
374376

375377
done();
376378
});
@@ -439,7 +441,8 @@ describe('Facebook Forwarder', function () {
439441
window.fbqObj.params.should.have.property('checkout_step', 1);
440442
window.fbqObj.params.should.have.property('num_items', 9);
441443
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
442-
444+
window.fbqObj.params.should.have.property('order_id', 123);
445+
window.fbqObj.params.should.have.property('contents');
443446

444447
done();
445448
});
@@ -481,7 +484,8 @@ describe('Facebook Forwarder', function () {
481484
window.fbqObj.params.should.have.property('content_name', 'eCommerce - AddToCart');
482485
window.fbqObj.params.should.have.property('content_ids', ['12345']);
483486
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
484-
window.fbqObj.params.should.not.have.property('contents');
487+
window.fbqObj.params.should.have.property('contents');
488+
window.fbqObj.params.should.have.property('order_id', 123);
485489

486490
done();
487491
});
@@ -540,6 +544,8 @@ describe('Facebook Forwarder', function () {
540544
window.fbqObj.params.should.have.property('content_name', 'eCommerce - AddToCart');
541545
window.fbqObj.params.should.have.property('content_ids', ['12345', '888', '666']);
542546
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
547+
window.fbqObj.params.should.have.property('order_id', 123);
548+
window.fbqObj.params.should.have.property('contents');
543549

544550
done();
545551
});
@@ -581,6 +587,8 @@ describe('Facebook Forwarder', function () {
581587
window.fbqObj.params.should.have.property('content_name', 'eCommerce - RemoveFromCart');
582588
window.fbqObj.params.should.have.property('content_ids', ['12345']);
583589
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
590+
window.fbqObj.params.should.not.have.property('order_id');
591+
window.fbqObj.params.should.not.have.property('contents');
584592

585593
done();
586594
});
@@ -621,6 +629,8 @@ describe('Facebook Forwarder', function () {
621629
window.fbqObj.params.should.have.property('content_name', 'eCommerce - RemoveFromCart');
622630
window.fbqObj.params.should.have.property('content_ids', ['12345']);
623631
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
632+
window.fbqObj.params.should.not.have.property('order_id');
633+
window.fbqObj.params.should.not.have.property('contents');
624634

625635
done();
626636
});
@@ -685,6 +695,8 @@ describe('Facebook Forwarder', function () {
685695
window.fbqObj.params.should.have.property('eventAttr1', 'value1');
686696
window.fbqObj.params.should.have.property('eventAttr2', 'value2');
687697
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
698+
window.fbqObj.params.should.not.have.property('order_id');
699+
window.fbqObj.params.should.not.have.property('contents');
688700

689701
done();
690702
});
@@ -732,6 +744,8 @@ describe('Facebook Forwarder', function () {
732744
window.fbqObj.params.should.have.property('eventAttr1', 'value1');
733745
window.fbqObj.params.should.have.property('eventAttr2', 'value2');
734746
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
747+
window.fbqObj.params.should.not.have.property('order_id');
748+
window.fbqObj.params.should.not.have.property('contents');
735749

736750
done();
737751
});
@@ -778,6 +792,8 @@ describe('Facebook Forwarder', function () {
778792
window.fbqObj.params.should.have.property('eventAttr1', 'value1');
779793
window.fbqObj.params.should.have.property('eventAttr2', 'value2');
780794
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
795+
window.fbqObj.params.should.not.have.property('order_id');
796+
window.fbqObj.params.should.not.have.property('contents');
781797

782798
done();
783799
});
@@ -824,6 +840,8 @@ describe('Facebook Forwarder', function () {
824840
window.fbqObj.params.should.have.property('eventAttr1', 'value1');
825841
window.fbqObj.params.should.have.property('eventAttr2', 'value2');
826842
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
843+
window.fbqObj.params.should.not.have.property('order_id');
844+
window.fbqObj.params.should.not.have.property('contents');
827845

828846
done();
829847
});
@@ -924,5 +942,136 @@ describe('Facebook Forwarder', function () {
924942

925943
done();
926944
});
945+
946+
it('should build contents array with mapped attributes for AddToCart events', function (done) {
947+
// Initialize with product attribute mapping
948+
mParticle.forwarder.init({
949+
pixelCode: 'test-pixel-code',
950+
"productAttributeMapping":"[{"jsmap":"3373707","map":"Name","maptype":"ProductAttributeSelector.Name","value":"custom_name"},{"jsmap":"93997959","map":"Brand","maptype":"ProductAttributeSelector.Name","value":"custom_brand"},{"jsmap":"106934601","map":"Price","maptype":"ProductAttributeSelector.Name","value":"custom_price"},{"jsmap":"50511102","map":"Category","maptype":"ProductAttributeSelector.Name","value":"custom_category"},{"jsmap":"94842723","map":"category","maptype":"ProductAttributeSelector.Name","value":"custom_attribute_category"}]"
951+
}, reportService.cb, true);
952+
953+
mParticle.forwarder.process({
954+
EventName: 'eCommerce - AddToCart',
955+
EventDataType: MessageType.Commerce,
956+
ProductAction: {
957+
ProductActionType: ProductActionType.AddToCart,
958+
ProductList: [
959+
{
960+
Sku: 'sku-12',
961+
Name: 'iPhone',
962+
Brand: 'Apple',
963+
Category: 'electronics',
964+
Variant: 'blue',
965+
Price: 1000.99,
966+
Quantity: 1,
967+
Attributes: {
968+
category: 'phones'
969+
}
970+
},
971+
{
972+
Sku: 'sku-34',
973+
Name: 'Watch',
974+
Brand: 'Samsung',
975+
Price: 450.99,
976+
Quantity: 2
977+
}
978+
],
979+
TransactionId: 123
980+
},
981+
CurrencyCode: 'USD',
982+
SourceMessageId: SOURCE_MESSAGE_ID,
983+
});
984+
985+
checkBasicProperties('track');
986+
window.fbqObj.should.have.property('eventName', 'AddToCart');
987+
window.fbqObj.params.should.have.property('contents');
988+
window.fbqObj.params.contents.length.should.equal(2);
989+
window.fbqObj.params.should.have.property('order_id', 123);
990+
991+
var firstProduct = window.fbqObj.params.contents[0];
992+
// Standard Facebook fields
993+
firstProduct.should.have.property('id', 'sku-12');
994+
firstProduct.should.have.property('name', 'iPhone');
995+
firstProduct.should.have.property('brand', 'Apple');
996+
firstProduct.should.have.property('item_price', 1000.99);
997+
firstProduct.should.have.property('quantity', 1);
998+
999+
// Mapped standard fields
1000+
firstProduct.should.have.property('custom_name', 'iPhone');
1001+
firstProduct.should.have.property('custom_brand', 'Apple');
1002+
firstProduct.should.have.property('custom_price', 1000.99);
1003+
firstProduct.should.have.property('custom_category', 'electronics');
1004+
1005+
// Mapped custom attribute
1006+
firstProduct.should.have.property('custom_attribute_category', 'phones');
1007+
1008+
done();
1009+
});
1010+
1011+
it('should build contents array with mapped attributes for Checkout events', function (done) {
1012+
// Initialize with product attribute mapping
1013+
mParticle.forwarder.init({
1014+
pixelCode: 'test-pixel-code',
1015+
"productAttributeMapping":"[{"jsmap":"3373707","map":"Name","maptype":"ProductAttributeSelector.Name","value":"custom_name"},{"jsmap":"93997959","map":"Brand","maptype":"ProductAttributeSelector.Name","value":"custom_brand"},{"jsmap":"106934601","map":"Price","maptype":"ProductAttributeSelector.Name","value":"custom_price"},{"jsmap":"50511102","map":"Category","maptype":"ProductAttributeSelector.Name","value":"custom_category"},{"jsmap":"94842723","map":"category","maptype":"ProductAttributeSelector.Name","value":"custom_attribute_category"}]"
1016+
}, reportService.cb, true);
1017+
1018+
mParticle.forwarder.process({
1019+
EventName: 'eCommerce - Checkout',
1020+
EventDataType: MessageType.Commerce,
1021+
ProductAction: {
1022+
ProductActionType: ProductActionType.Checkout,
1023+
ProductList: [
1024+
{
1025+
Sku: 'sku-12',
1026+
Name: 'iPhone',
1027+
Brand: 'Apple',
1028+
Category: 'electronics',
1029+
Variant: 'blue',
1030+
Price: 1000.99,
1031+
Quantity: 1,
1032+
Attributes: {
1033+
category: 'phones'
1034+
}
1035+
},
1036+
{
1037+
Sku: 'sku-34',
1038+
Name: 'Watch',
1039+
Brand: 'Samsung',
1040+
Price: 450.99,
1041+
Quantity: 2
1042+
}
1043+
],
1044+
TransactionId: 123,
1045+
TotalAmount: 1902.97
1046+
},
1047+
CurrencyCode: 'USD',
1048+
SourceMessageId: SOURCE_MESSAGE_ID,
1049+
});
1050+
1051+
checkBasicProperties('track');
1052+
window.fbqObj.should.have.property('eventName', 'InitiateCheckout');
1053+
window.fbqObj.params.should.have.property('contents');
1054+
window.fbqObj.params.contents.length.should.equal(2);
1055+
window.fbqObj.params.should.have.property('order_id', 123);
1056+
1057+
var firstProduct = window.fbqObj.params.contents[0];
1058+
// Standard Facebook fields
1059+
firstProduct.should.have.property('id', 'sku-12');
1060+
firstProduct.should.have.property('name', 'iPhone');
1061+
firstProduct.should.have.property('brand', 'Apple');
1062+
firstProduct.should.have.property('item_price', 1000.99);
1063+
firstProduct.should.have.property('quantity', 1);
1064+
1065+
// Mapped standard fields
1066+
firstProduct.should.have.property('custom_name', 'iPhone');
1067+
firstProduct.should.have.property('custom_brand', 'Apple');
1068+
firstProduct.should.have.property('custom_price', 1000.99);
1069+
firstProduct.should.have.property('custom_category', 'electronics');
1070+
1071+
// Mapped custom attribute
1072+
firstProduct.should.have.property('custom_attribute_category', 'phones');
1073+
1074+
done();
1075+
});
9271076
});
9281077
});

0 commit comments

Comments
 (0)