Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/FacebookEventForwarder.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,16 @@ var name = 'Facebook',
}
else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToCart){
eventName = ADD_TO_CART_EVENT_NAME;
if (event.ProductAction.TransactionId) {
params['order_id'] = event.ProductAction.TransactionId;
}

// Build contents array for AddToCart events
var contents = buildProductContents(event.ProductAction.ProductList);
if (contents && contents.length > 0) {
params['contents'] = contents;
}

}
else{
eventName = VIEW_CONTENT_EVENT_NAME;
Expand Down Expand Up @@ -243,12 +253,10 @@ var name = 'Facebook',
params['order_id'] = event.ProductAction.TransactionId;
}

// Build contents array for Purchase events
if (event.ProductAction.ProductActionType == mParticle.ProductActionType.Purchase) {
var contents = buildProductContents(event.ProductAction.ProductList);
if (contents && contents.length > 0) {
params['contents'] = contents;
}
// Build contents array for Purchase/Checkout events
var contents = buildProductContents(event.ProductAction.ProductList);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var contents is being redeclared here. best to declar it at the top of the file rather than twice at 234/257

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved contents declaration to the top of the logCommerceEvent function (line 171)

if (contents && contents.length > 0) {
params['contents'] = contents;
}
}
else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.RemoveFromCart) {
Expand Down
153 changes: 151 additions & 2 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ describe('Facebook Forwarder', function () {
window.fbqObj.params.should.have.property('eventAttr1', 'value1');
window.fbqObj.params.should.have.property('eventAttr2', 'value2');
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
window.fbqObj.params.should.have.property('order_id', 123);
window.fbqObj.params.should.have.property('contents');

done();
});
Expand Down Expand Up @@ -439,7 +441,8 @@ describe('Facebook Forwarder', function () {
window.fbqObj.params.should.have.property('checkout_step', 1);
window.fbqObj.params.should.have.property('num_items', 9);
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);

window.fbqObj.params.should.have.property('order_id', 123);
window.fbqObj.params.should.have.property('contents');

done();
});
Expand Down Expand Up @@ -481,7 +484,8 @@ describe('Facebook Forwarder', function () {
window.fbqObj.params.should.have.property('content_name', 'eCommerce - AddToCart');
window.fbqObj.params.should.have.property('content_ids', ['12345']);
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
window.fbqObj.params.should.not.have.property('contents');
window.fbqObj.params.should.have.property('contents');
window.fbqObj.params.should.have.property('order_id', 123);

done();
});
Expand Down Expand Up @@ -540,6 +544,8 @@ describe('Facebook Forwarder', function () {
window.fbqObj.params.should.have.property('content_name', 'eCommerce - AddToCart');
window.fbqObj.params.should.have.property('content_ids', ['12345', '888', '666']);
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
window.fbqObj.params.should.have.property('order_id', 123);
window.fbqObj.params.should.have.property('contents');

done();
});
Expand Down Expand Up @@ -581,6 +587,8 @@ describe('Facebook Forwarder', function () {
window.fbqObj.params.should.have.property('content_name', 'eCommerce - RemoveFromCart');
window.fbqObj.params.should.have.property('content_ids', ['12345']);
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
window.fbqObj.params.should.not.have.property('order_id');
window.fbqObj.params.should.not.have.property('contents');

done();
});
Expand Down Expand Up @@ -621,6 +629,8 @@ describe('Facebook Forwarder', function () {
window.fbqObj.params.should.have.property('content_name', 'eCommerce - RemoveFromCart');
window.fbqObj.params.should.have.property('content_ids', ['12345']);
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
window.fbqObj.params.should.not.have.property('order_id');
window.fbqObj.params.should.not.have.property('contents');

done();
});
Expand Down Expand Up @@ -685,6 +695,8 @@ describe('Facebook Forwarder', function () {
window.fbqObj.params.should.have.property('eventAttr1', 'value1');
window.fbqObj.params.should.have.property('eventAttr2', 'value2');
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
window.fbqObj.params.should.not.have.property('order_id');
window.fbqObj.params.should.not.have.property('contents');

done();
});
Expand Down Expand Up @@ -732,6 +744,8 @@ describe('Facebook Forwarder', function () {
window.fbqObj.params.should.have.property('eventAttr1', 'value1');
window.fbqObj.params.should.have.property('eventAttr2', 'value2');
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
window.fbqObj.params.should.not.have.property('order_id');
window.fbqObj.params.should.not.have.property('contents');

done();
});
Expand Down Expand Up @@ -778,6 +792,8 @@ describe('Facebook Forwarder', function () {
window.fbqObj.params.should.have.property('eventAttr1', 'value1');
window.fbqObj.params.should.have.property('eventAttr2', 'value2');
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
window.fbqObj.params.should.not.have.property('order_id');
window.fbqObj.params.should.not.have.property('contents');

done();
});
Expand Down Expand Up @@ -824,6 +840,8 @@ describe('Facebook Forwarder', function () {
window.fbqObj.params.should.have.property('eventAttr1', 'value1');
window.fbqObj.params.should.have.property('eventAttr2', 'value2');
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID);
window.fbqObj.params.should.not.have.property('order_id');
window.fbqObj.params.should.not.have.property('contents');

done();
});
Expand Down Expand Up @@ -924,5 +942,136 @@ describe('Facebook Forwarder', function () {

done();
});

it('should build contents array with mapped attributes for AddToCart events', function (done) {
// Initialize with product attribute mapping
mParticle.forwarder.init({
pixelCode: 'test-pixel-code',
"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"}]"
}, reportService.cb, true);

mParticle.forwarder.process({
EventName: 'eCommerce - AddToCart',
EventDataType: MessageType.Commerce,
ProductAction: {
ProductActionType: ProductActionType.AddToCart,
ProductList: [
{
Sku: 'sku-12',
Name: 'iPhone',
Brand: 'Apple',
Category: 'electronics',
Variant: 'blue',
Price: 1000.99,
Quantity: 1,
Attributes: {
category: 'phones'
}
},
{
Sku: 'sku-34',
Name: 'Watch',
Brand: 'Samsung',
Price: 450.99,
Quantity: 2
}
],
TransactionId: 123
},
CurrencyCode: 'USD',
SourceMessageId: SOURCE_MESSAGE_ID,
});

checkBasicProperties('track');
window.fbqObj.should.have.property('eventName', 'AddToCart');
window.fbqObj.params.should.have.property('contents');
window.fbqObj.params.contents.length.should.equal(2);
window.fbqObj.params.should.have.property('order_id', 123);

var firstProduct = window.fbqObj.params.contents[0];
// Standard Facebook fields
firstProduct.should.have.property('id', 'sku-12');
firstProduct.should.have.property('name', 'iPhone');
firstProduct.should.have.property('brand', 'Apple');
firstProduct.should.have.property('item_price', 1000.99);
firstProduct.should.have.property('quantity', 1);

// Mapped standard fields
firstProduct.should.have.property('custom_name', 'iPhone');
firstProduct.should.have.property('custom_brand', 'Apple');
firstProduct.should.have.property('custom_price', 1000.99);
firstProduct.should.have.property('custom_category', 'electronics');

// Mapped custom attribute
firstProduct.should.have.property('custom_attribute_category', 'phones');

done();
});

it('should build contents array with mapped attributes for Checkout events', function (done) {
// Initialize with product attribute mapping
mParticle.forwarder.init({
pixelCode: 'test-pixel-code',
"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"}]"
}, reportService.cb, true);

mParticle.forwarder.process({
EventName: 'eCommerce - Checkout',
EventDataType: MessageType.Commerce,
ProductAction: {
ProductActionType: ProductActionType.Checkout,
ProductList: [
{
Sku: 'sku-12',
Name: 'iPhone',
Brand: 'Apple',
Category: 'electronics',
Variant: 'blue',
Price: 1000.99,
Quantity: 1,
Attributes: {
category: 'phones'
}
},
{
Sku: 'sku-34',
Name: 'Watch',
Brand: 'Samsung',
Price: 450.99,
Quantity: 2
}
],
TransactionId: 123,
TotalAmount: 1902.97
},
CurrencyCode: 'USD',
SourceMessageId: SOURCE_MESSAGE_ID,
});

checkBasicProperties('track');
window.fbqObj.should.have.property('eventName', 'InitiateCheckout');
window.fbqObj.params.should.have.property('contents');
window.fbqObj.params.contents.length.should.equal(2);
window.fbqObj.params.should.have.property('order_id', 123);

var firstProduct = window.fbqObj.params.contents[0];
// Standard Facebook fields
firstProduct.should.have.property('id', 'sku-12');
firstProduct.should.have.property('name', 'iPhone');
firstProduct.should.have.property('brand', 'Apple');
firstProduct.should.have.property('item_price', 1000.99);
firstProduct.should.have.property('quantity', 1);

// Mapped standard fields
firstProduct.should.have.property('custom_name', 'iPhone');
firstProduct.should.have.property('custom_brand', 'Apple');
firstProduct.should.have.property('custom_price', 1000.99);
firstProduct.should.have.property('custom_category', 'electronics');

// Mapped custom attribute
firstProduct.should.have.property('custom_attribute_category', 'phones');

done();
});
});
});