Skip to content

Commit 0d775d2

Browse files
committed
extend order_id and content_name as product_names to all commerce events
1 parent a44e5ae commit 0d775d2

File tree

2 files changed

+101
-41
lines changed

2 files changed

+101
-41
lines changed

src/FacebookEventForwarder.js

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ var name = 'Facebook',
168168
var eventName,
169169
totalValue,
170170
contents,
171-
productNames,
172171
params = cloneEventAttributes(event),
173172
eventID = createEventId(event),
174173
sendProductNamesAsContents = settings.sendProductNamesasContents || false;
@@ -190,6 +189,19 @@ var name = 'Facebook',
190189
params['content_ids'] = productSkus;
191190
}
192191

192+
// Override content_name with product names array if setting enabled
193+
if (sendProductNamesAsContents) {
194+
var productNames = buildProductNames(event.ProductAction.ProductList);
195+
if (productNames && productNames.length > 0) {
196+
params['content_name'] = productNames;
197+
}
198+
}
199+
200+
// Set order_id if TransactionId exists
201+
if (event.ProductAction.TransactionId) {
202+
params['order_id'] = event.ProductAction.TransactionId;
203+
}
204+
193205
if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToWishlist ||
194206
event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout) {
195207
var eventCategory = getEventCategoryString(event);
@@ -219,18 +231,6 @@ var name = 'Facebook',
219231
}
220232
else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToCart){
221233
eventName = ADD_TO_CART_EVENT_NAME;
222-
223-
// Set product names as content_name if enabled
224-
if (sendProductNamesAsContents) {
225-
productNames = buildProductNames(event.ProductAction.ProductList);
226-
if (productNames && productNames.length > 0) {
227-
params['content_name'] = productNames;
228-
}
229-
}
230-
231-
if (event.ProductAction.TransactionId) {
232-
params['order_id'] = event.ProductAction.TransactionId;
233-
}
234234

235235
// Build contents array for AddToCart events
236236
contents = buildProductContents(event.ProductAction.ProductList);
@@ -259,18 +259,6 @@ var name = 'Facebook',
259259
return sum;
260260
}, 0);
261261
params['num_items'] = num_items;
262-
263-
// Set product names as content_name if enabled
264-
if (sendProductNamesAsContents) {
265-
productNames = buildProductNames(event.ProductAction.ProductList);
266-
if (productNames && productNames.length > 0) {
267-
params['content_name'] = productNames;
268-
}
269-
}
270-
271-
if (event.ProductAction.TransactionId) {
272-
params['order_id'] = event.ProductAction.TransactionId;
273-
}
274262

275263
// Build contents array for Purchase/Checkout events
276264
contents = buildProductContents(event.ProductAction.ProductList);
@@ -297,18 +285,6 @@ var name = 'Facebook',
297285
}
298286

299287
params['value'] = totalValue;
300-
301-
// Set product names as content_name if enabled
302-
if (sendProductNamesAsContents) {
303-
productNames = buildProductNames(event.ProductAction.ProductList);
304-
if (productNames && productNames.length > 0) {
305-
params['content_name'] = productNames;
306-
}
307-
}
308-
309-
if (event.ProductAction.TransactionId) {
310-
params['order_id'] = event.ProductAction.TransactionId;
311-
}
312288

313289
// Build contents array for RemoveFromCart events
314290
contents = buildProductContents(event.ProductAction.ProductList);

test/tests.js

Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ describe('Facebook Forwarder', function () {
695695
window.fbqObj.params.should.have.property('eventAttr1', 'value1');
696696
window.fbqObj.params.should.have.property('eventAttr2', 'value2');
697697
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
698-
window.fbqObj.params.should.not.have.property('order_id');
698+
window.fbqObj.params.should.have.property('order_id', 123);
699699
window.fbqObj.params.should.not.have.property('contents');
700700

701701
done();
@@ -744,7 +744,7 @@ describe('Facebook Forwarder', function () {
744744
window.fbqObj.params.should.have.property('eventAttr1', 'value1');
745745
window.fbqObj.params.should.have.property('eventAttr2', 'value2');
746746
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
747-
window.fbqObj.params.should.not.have.property('order_id');
747+
window.fbqObj.params.should.have.property('order_id', 123);
748748
window.fbqObj.params.should.not.have.property('contents');
749749

750750
done();
@@ -792,7 +792,7 @@ describe('Facebook Forwarder', function () {
792792
window.fbqObj.params.should.have.property('eventAttr1', 'value1');
793793
window.fbqObj.params.should.have.property('eventAttr2', 'value2');
794794
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
795-
window.fbqObj.params.should.not.have.property('order_id');
795+
window.fbqObj.params.should.have.property('order_id', 123);
796796
window.fbqObj.params.should.not.have.property('contents');
797797

798798
done();
@@ -840,7 +840,7 @@ describe('Facebook Forwarder', function () {
840840
window.fbqObj.params.should.have.property('eventAttr1', 'value1');
841841
window.fbqObj.params.should.have.property('eventAttr2', 'value2');
842842
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
843-
window.fbqObj.params.should.not.have.property('order_id');
843+
window.fbqObj.params.should.have.property('order_id', 123);
844844
window.fbqObj.params.should.not.have.property('contents');
845845

846846
done();
@@ -1357,6 +1357,90 @@ describe('Facebook Forwarder', function () {
13571357
window.fbqObj.params.should.have.property('checkout_step', 1);
13581358
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
13591359

1360+
done();
1361+
});
1362+
1363+
it('should log AddToWishlist event with product names as contents', function (done) {
1364+
mParticle.forwarder.init({
1365+
pixelCode: 'test-pixel-code',
1366+
sendProductNamesasContents: true
1367+
}, reportService.cb, true);
1368+
1369+
mParticle.forwarder.process({
1370+
EventName: 'eCommerce - AddToWishlist',
1371+
EventCategory: CommerceEventType.ProductAddToWishlist,
1372+
EventDataType: MessageType.Commerce,
1373+
ProductAction: {
1374+
ProductActionType: ProductActionType.AddToWishlist,
1375+
ProductList: [
1376+
{
1377+
Sku: 'sku-111',
1378+
Name: 'Laptop',
1379+
Category: 'Electronics',
1380+
Brand: 'Dell',
1381+
Price: 1200,
1382+
Quantity: 1
1383+
},
1384+
{
1385+
Sku: 'sku-222',
1386+
Name: 'Mouse',
1387+
Category: 'Accessories',
1388+
Brand: 'Logitech',
1389+
Price: 50,
1390+
Quantity: 1
1391+
}
1392+
],
1393+
TransactionId: 456
1394+
},
1395+
CurrencyCode: 'USD',
1396+
SourceMessageId: SOURCE_MESSAGE_ID,
1397+
});
1398+
1399+
checkBasicProperties('track');
1400+
window.fbqObj.should.have.property('eventName', 'AddToWishlist');
1401+
window.fbqObj.params.should.have.property('content_name', ['Laptop', 'Mouse']);
1402+
window.fbqObj.params.should.have.property('order_id', 456);
1403+
window.fbqObj.params.should.have.property('content_ids', ['sku-111', 'sku-222']);
1404+
window.fbqObj.params.should.not.have.property('contents');
1405+
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
1406+
done();
1407+
});
1408+
1409+
it('should log ViewDetail event with product names as contents', function (done) {
1410+
mParticle.forwarder.init({
1411+
pixelCode: 'test-pixel-code',
1412+
sendProductNamesasContents: true
1413+
}, reportService.cb, true);
1414+
1415+
mParticle.forwarder.process({
1416+
EventName: 'eCommerce - ViewDetail',
1417+
EventCategory: CommerceEventType.ProductViewDetail,
1418+
EventDataType: MessageType.Commerce,
1419+
ProductAction: {
1420+
ProductActionType: ProductActionType.ViewDetail,
1421+
ProductList: [
1422+
{
1423+
Sku: 'sku-999',
1424+
Name: 'Tablet',
1425+
Category: 'Electronics',
1426+
Brand: 'Samsung',
1427+
Price: 800,
1428+
Quantity: 1
1429+
}
1430+
],
1431+
TransactionId: 789
1432+
},
1433+
CurrencyCode: 'USD',
1434+
SourceMessageId: SOURCE_MESSAGE_ID,
1435+
});
1436+
1437+
checkBasicProperties('track');
1438+
window.fbqObj.should.have.property('eventName', 'ViewContent');
1439+
window.fbqObj.params.should.have.property('content_name', ['Tablet']);
1440+
window.fbqObj.params.should.have.property('order_id', 789);
1441+
window.fbqObj.params.should.have.property('content_ids', ['sku-999']);
1442+
window.fbqObj.params.should.not.have.property('contents');
1443+
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
13601444
done();
13611445
});
13621446
});

0 commit comments

Comments
 (0)