Skip to content

Commit 03cee49

Browse files
committed
extend custom product attribute mapping to all ecommerce events
1 parent 3770ec6 commit 03cee49

File tree

2 files changed

+154
-25
lines changed

2 files changed

+154
-25
lines changed

src/FacebookEventForwarder.js

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ var name = 'Facebook',
167167
SupportedCommerceTypes.indexOf(event.ProductAction.ProductActionType) > -1) {
168168
var eventName,
169169
totalValue,
170-
contents,
171170
params = cloneEventAttributes(event),
172171
eventID = createEventId(event),
173172
sendProductNamesAsContents = settings.sendProductNamesasContents || false;
@@ -202,6 +201,12 @@ var name = 'Facebook',
202201
params['order_id'] = event.ProductAction.TransactionId;
203202
}
204203

204+
// Build contents array
205+
var contents = buildProductContents(event.ProductAction.ProductList);
206+
if (contents && contents.length > 0) {
207+
params['contents'] = contents;
208+
}
209+
205210
if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToWishlist ||
206211
event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout) {
207212
var eventCategory = getEventCategoryString(event);
@@ -231,12 +236,6 @@ var name = 'Facebook',
231236
}
232237
else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToCart){
233238
eventName = ADD_TO_CART_EVENT_NAME;
234-
235-
// Build contents array for AddToCart events
236-
contents = buildProductContents(event.ProductAction.ProductList);
237-
if (contents && contents.length > 0) {
238-
params['contents'] = contents;
239-
}
240239
}
241240
else{
242241
eventName = VIEW_CONTENT_EVENT_NAME;
@@ -259,12 +258,6 @@ var name = 'Facebook',
259258
return sum;
260259
}, 0);
261260
params['num_items'] = num_items;
262-
263-
// Build contents array for Purchase/Checkout events
264-
contents = buildProductContents(event.ProductAction.ProductList);
265-
if (contents && contents.length > 0) {
266-
params['contents'] = contents;
267-
}
268261
}
269262
else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.RemoveFromCart) {
270263
eventName = REMOVE_FROM_CART_EVENT_NAME;
@@ -285,12 +278,6 @@ var name = 'Facebook',
285278
}
286279

287280
params['value'] = totalValue;
288-
289-
// Build contents array for RemoveFromCart events
290-
contents = buildProductContents(event.ProductAction.ProductList);
291-
if (contents && contents.length > 0) {
292-
params['contents'] = contents;
293-
}
294281

295282
fbq('trackCustom', eventName || 'customEvent', params, eventID);
296283
return true;

test/tests.js

Lines changed: 148 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,14 @@ describe('Facebook Forwarder', function () {
696696
window.fbqObj.params.should.have.property('eventAttr2', 'value2');
697697
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
698698
window.fbqObj.params.should.have.property('order_id', 123);
699-
window.fbqObj.params.should.not.have.property('contents');
699+
window.fbqObj.params.should.have.property('contents');
700+
window.fbqObj.params.contents.length.should.equal(3);
701+
window.fbqObj.params.contents[0].should.have.property('id', '12345');
702+
window.fbqObj.params.contents[0].should.have.property('quantity', 2);
703+
window.fbqObj.params.contents[1].should.have.property('id', '888');
704+
window.fbqObj.params.contents[1].should.have.property('quantity', 1);
705+
window.fbqObj.params.contents[2].should.have.property('id', '666');
706+
window.fbqObj.params.contents[2].should.have.property('quantity', 1);
700707

701708
done();
702709
});
@@ -745,7 +752,10 @@ describe('Facebook Forwarder', function () {
745752
window.fbqObj.params.should.have.property('eventAttr2', 'value2');
746753
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
747754
window.fbqObj.params.should.have.property('order_id', 123);
748-
window.fbqObj.params.should.not.have.property('contents');
755+
window.fbqObj.params.should.have.property('contents');
756+
window.fbqObj.params.contents.length.should.equal(1);
757+
window.fbqObj.params.contents[0].should.have.property('id', '12345');
758+
window.fbqObj.params.contents[0].should.have.property('quantity', 1);
749759

750760
done();
751761
});
@@ -793,7 +803,10 @@ describe('Facebook Forwarder', function () {
793803
window.fbqObj.params.should.have.property('eventAttr2', 'value2');
794804
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
795805
window.fbqObj.params.should.have.property('order_id', 123);
796-
window.fbqObj.params.should.not.have.property('contents');
806+
window.fbqObj.params.should.have.property('contents');
807+
window.fbqObj.params.contents.length.should.equal(1);
808+
window.fbqObj.params.contents[0].should.have.property('id', '145');
809+
window.fbqObj.params.contents[0].should.have.property('quantity', 1);
797810

798811
done();
799812
});
@@ -841,7 +854,10 @@ describe('Facebook Forwarder', function () {
841854
window.fbqObj.params.should.have.property('eventAttr2', 'value2');
842855
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
843856
window.fbqObj.params.should.have.property('order_id', 123);
844-
window.fbqObj.params.should.not.have.property('contents');
857+
window.fbqObj.params.should.have.property('contents');
858+
window.fbqObj.params.contents.length.should.equal(1);
859+
window.fbqObj.params.contents[0].should.have.property('id', '12345');
860+
window.fbqObj.params.contents[0].should.have.property('quantity', 1);
845861

846862
done();
847863
});
@@ -1139,6 +1155,124 @@ describe('Facebook Forwarder', function () {
11391155
done();
11401156
});
11411157

1158+
it('should build contents array with mapped attributes for AddToWishlist events', function (done) {
1159+
// Initialize with product attribute mapping
1160+
mParticle.forwarder.init({
1161+
pixelCode: 'test-pixel-code',
1162+
"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"}]"
1163+
}, reportService.cb, true);
1164+
1165+
mParticle.forwarder.process({
1166+
EventName: 'eCommerce - AddToWishlist',
1167+
EventCategory: CommerceEventType.ProductAddToWishlist,
1168+
EventDataType: MessageType.Commerce,
1169+
ProductAction: {
1170+
ProductActionType: ProductActionType.AddToWishlist,
1171+
ProductList: [
1172+
{
1173+
Sku: 'wishlist-sku',
1174+
Name: 'Designer Bag',
1175+
Category: 'Accessories',
1176+
Brand: 'Gucci',
1177+
Variant: 'Black',
1178+
Price: 2500,
1179+
Quantity: 1,
1180+
Attributes: {
1181+
category: 'luxury'
1182+
}
1183+
}
1184+
],
1185+
TransactionId: 789
1186+
},
1187+
CurrencyCode: 'USD',
1188+
SourceMessageId: SOURCE_MESSAGE_ID,
1189+
});
1190+
1191+
checkBasicProperties('track');
1192+
window.fbqObj.should.have.property('eventName', 'AddToWishlist');
1193+
window.fbqObj.params.should.have.property('contents');
1194+
window.fbqObj.params.contents.length.should.equal(1);
1195+
window.fbqObj.params.should.have.property('order_id', 789);
1196+
1197+
var firstProduct = window.fbqObj.params.contents[0];
1198+
// Standard Facebook fields
1199+
firstProduct.should.have.property('id', 'wishlist-sku');
1200+
firstProduct.should.have.property('name', 'Designer Bag');
1201+
firstProduct.should.have.property('brand', 'Gucci');
1202+
firstProduct.should.have.property('item_price', 2500);
1203+
firstProduct.should.have.property('quantity', 1);
1204+
1205+
// Mapped standard fields
1206+
firstProduct.should.have.property('custom_name', 'Designer Bag');
1207+
firstProduct.should.have.property('custom_brand', 'Gucci');
1208+
firstProduct.should.have.property('custom_price', 2500);
1209+
firstProduct.should.have.property('custom_category', 'Accessories');
1210+
1211+
// Mapped custom attribute
1212+
firstProduct.should.have.property('custom_attribute_category', 'luxury');
1213+
1214+
done();
1215+
});
1216+
1217+
it('should build contents array with mapped attributes for ViewDetail events', function (done) {
1218+
// Initialize with product attribute mapping
1219+
mParticle.forwarder.init({
1220+
pixelCode: 'test-pixel-code',
1221+
"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"}]"
1222+
}, reportService.cb, true);
1223+
1224+
mParticle.forwarder.process({
1225+
EventName: 'eCommerce - ViewDetail',
1226+
EventCategory: CommerceEventType.ProductViewDetail,
1227+
EventDataType: MessageType.Commerce,
1228+
ProductAction: {
1229+
ProductActionType: ProductActionType.ViewDetail,
1230+
ProductList: [
1231+
{
1232+
Sku: 'view-sku-999',
1233+
Name: 'Laptop Pro',
1234+
Category: 'Electronics',
1235+
Brand: 'Apple',
1236+
Variant: '16-inch',
1237+
Price: 3000,
1238+
Quantity: 1,
1239+
Attributes: {
1240+
category: 'computers'
1241+
}
1242+
}
1243+
],
1244+
TransactionId: 999
1245+
},
1246+
CurrencyCode: 'USD',
1247+
SourceMessageId: SOURCE_MESSAGE_ID,
1248+
});
1249+
1250+
checkBasicProperties('track');
1251+
window.fbqObj.should.have.property('eventName', 'ViewContent');
1252+
window.fbqObj.params.should.have.property('contents');
1253+
window.fbqObj.params.contents.length.should.equal(1);
1254+
window.fbqObj.params.should.have.property('order_id', 999);
1255+
1256+
var firstProduct = window.fbqObj.params.contents[0];
1257+
// Standard Facebook fields
1258+
firstProduct.should.have.property('id', 'view-sku-999');
1259+
firstProduct.should.have.property('name', 'Laptop Pro');
1260+
firstProduct.should.have.property('brand', 'Apple');
1261+
firstProduct.should.have.property('item_price', 3000);
1262+
firstProduct.should.have.property('quantity', 1);
1263+
1264+
// Mapped standard fields
1265+
firstProduct.should.have.property('custom_name', 'Laptop Pro');
1266+
firstProduct.should.have.property('custom_brand', 'Apple');
1267+
firstProduct.should.have.property('custom_price', 3000);
1268+
firstProduct.should.have.property('custom_category', 'Electronics');
1269+
1270+
// Mapped custom attribute
1271+
firstProduct.should.have.property('custom_attribute_category', 'computers');
1272+
1273+
done();
1274+
});
1275+
11421276
it('should log Purchase event with product names as contents', function (done) {
11431277
mParticle.forwarder.init({
11441278
pixelCode: 'test-pixel-code',
@@ -1401,7 +1535,12 @@ describe('Facebook Forwarder', function () {
14011535
window.fbqObj.params.should.have.property('content_name', ['Laptop', 'Mouse']);
14021536
window.fbqObj.params.should.have.property('order_id', 456);
14031537
window.fbqObj.params.should.have.property('content_ids', ['sku-111', 'sku-222']);
1404-
window.fbqObj.params.should.not.have.property('contents');
1538+
window.fbqObj.params.should.have.property('contents');
1539+
window.fbqObj.params.contents.length.should.equal(2);
1540+
window.fbqObj.params.contents[0].should.have.property('id', 'sku-111');
1541+
window.fbqObj.params.contents[0].should.have.property('quantity', 1);
1542+
window.fbqObj.params.contents[1].should.have.property('id', 'sku-222');
1543+
window.fbqObj.params.contents[1].should.have.property('quantity', 1);
14051544
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
14061545
done();
14071546
});
@@ -1439,7 +1578,10 @@ describe('Facebook Forwarder', function () {
14391578
window.fbqObj.params.should.have.property('content_name', ['Tablet']);
14401579
window.fbqObj.params.should.have.property('order_id', 789);
14411580
window.fbqObj.params.should.have.property('content_ids', ['sku-999']);
1442-
window.fbqObj.params.should.not.have.property('contents');
1581+
window.fbqObj.params.should.have.property('contents');
1582+
window.fbqObj.params.contents.length.should.equal(1);
1583+
window.fbqObj.params.contents[0].should.have.property('id', 'sku-999');
1584+
window.fbqObj.params.contents[0].should.have.property('quantity', 1);
14431585
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
14441586
done();
14451587
});

0 commit comments

Comments
 (0)