diff --git a/src/FacebookEventForwarder.js b/src/FacebookEventForwarder.js index 7a4bd60..359d5ef 100755 --- a/src/FacebookEventForwarder.js +++ b/src/FacebookEventForwarder.js @@ -165,11 +165,12 @@ var name = 'Facebook', event.ProductAction.ProductList && event.ProductAction.ProductActionType && SupportedCommerceTypes.indexOf(event.ProductAction.ProductActionType) > -1) { - var eventName, totalValue, params = cloneEventAttributes(event), - eventID = createEventId(event); + eventID = createEventId(event), + sendProductNamesAsContents = settings.sendProductNamesasContents || false; + params['currency'] = event.CurrencyCode || 'USD'; if (event.EventName) { @@ -187,6 +188,25 @@ var name = 'Facebook', params['content_ids'] = productSkus; } + // Override content_name with product names array if setting enabled + if (sendProductNamesAsContents) { + var productNames = buildProductNames(event.ProductAction.ProductList); + if (productNames && productNames.length > 0) { + params['content_name'] = productNames; + } + } + + // Set order_id if TransactionId exists + if (event.ProductAction.TransactionId) { + params['order_id'] = event.ProductAction.TransactionId; + } + + // Build contents array + var contents = buildProductContents(event.ProductAction.ProductList); + if (contents && contents.length > 0) { + params['contents'] = contents; + } + if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToWishlist || event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout) { var eventCategory = getEventCategoryString(event); @@ -238,18 +258,6 @@ var name = 'Facebook', return sum; }, 0); params['num_items'] = num_items; - - if (event.ProductAction.TransactionId) { - 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; - } - } } else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.RemoveFromCart) { eventName = REMOVE_FROM_CART_EVENT_NAME; @@ -407,6 +415,25 @@ var name = 'Facebook', } } + /** + * Builds array of product names from product list + * @param {Array} productList - Array of products + * @returns {Array} Array of product names + */ + function buildProductNames(productList) { + if (!productList || productList.length === 0) { + return []; + } + + return productList + .filter(function(product) { + return product && product.Name; + }) + .map(function(product) { + return product.Name; + }); + } + this.init = initForwarder; this.process = processEvent; }; diff --git a/test/tests.js b/test/tests.js index b0e0763..a8b9bf5 100755 --- a/test/tests.js +++ b/test/tests.js @@ -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(); }); @@ -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(); }); @@ -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(); }); @@ -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(); }); @@ -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.have.property('order_id', 123); + window.fbqObj.params.should.have.property('contents'); done(); }); @@ -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.have.property('order_id', 123); + window.fbqObj.params.should.have.property('contents'); done(); }); @@ -685,6 +695,15 @@ 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'); + window.fbqObj.params.contents.length.should.equal(3); + window.fbqObj.params.contents[0].should.have.property('id', '12345'); + window.fbqObj.params.contents[0].should.have.property('quantity', 2); + window.fbqObj.params.contents[1].should.have.property('id', '888'); + window.fbqObj.params.contents[1].should.have.property('quantity', 1); + window.fbqObj.params.contents[2].should.have.property('id', '666'); + window.fbqObj.params.contents[2].should.have.property('quantity', 1); done(); }); @@ -732,6 +751,11 @@ 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'); + window.fbqObj.params.contents.length.should.equal(1); + window.fbqObj.params.contents[0].should.have.property('id', '12345'); + window.fbqObj.params.contents[0].should.have.property('quantity', 1); done(); }); @@ -778,6 +802,11 @@ 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'); + window.fbqObj.params.contents.length.should.equal(1); + window.fbqObj.params.contents[0].should.have.property('id', '145'); + window.fbqObj.params.contents[0].should.have.property('quantity', 1); done(); }); @@ -824,6 +853,11 @@ 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'); + window.fbqObj.params.contents.length.should.equal(1); + window.fbqObj.params.contents[0].should.have.property('id', '12345'); + window.fbqObj.params.contents[0].should.have.property('quantity', 1); done(); }); @@ -924,5 +958,632 @@ 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(); + }); + + it('should build contents array with mapped attributes for RemoveFromCart 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 - RemoveFromCart', + EventCategory: CommerceEventType.ProductRemoveFromCart, + EventDataType: MessageType.Commerce, + ProductAction: { + ProductActionType: ProductActionType.RemoveFromCart, + ProductList: [ + { + Sku: '12345', + Name: 'iPhone 6', + Category: 'Phones', + Brand: 'iPhone', + Variant: '6', + Price: 200, + CouponCode: null, + Quantity: 1, + TotalAmount: 200, + Attributes: { + category: 'phones' + } + } + ], + TransactionId: 123, + Affiliation: 'my-affiliation', + TaxAmount: 40, + ShippingAmount: 10, + TotalAmount: 205 + }, + CurrencyCode: 'USD', + SourceMessageId: SOURCE_MESSAGE_ID, + }); + + checkBasicProperties('trackCustom'); + window.fbqObj.should.have.property('eventName', 'RemoveFromCart'); + window.fbqObj.params.should.have.property('contents'); + window.fbqObj.params.contents.length.should.equal(1); + window.fbqObj.params.should.have.property('order_id', 123); + + var firstProduct = window.fbqObj.params.contents[0]; + // Standard Facebook fields + firstProduct.should.have.property('id', '12345'); + firstProduct.should.have.property('name', 'iPhone 6'); + firstProduct.should.have.property('brand', 'iPhone'); + firstProduct.should.have.property('item_price', 200); + firstProduct.should.have.property('quantity', 1); + + // Mapped standard fields + firstProduct.should.have.property('custom_name', 'iPhone 6'); + firstProduct.should.have.property('custom_brand', 'iPhone'); + firstProduct.should.have.property('custom_price', 200); + firstProduct.should.have.property('custom_category', 'Phones'); + + // Mapped custom attribute + firstProduct.should.have.property('custom_attribute_category', 'phones'); + + done(); + }); + + it('should build contents array with mapped attributes for AddToWishlist 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 - AddToWishlist', + EventCategory: CommerceEventType.ProductAddToWishlist, + EventDataType: MessageType.Commerce, + ProductAction: { + ProductActionType: ProductActionType.AddToWishlist, + ProductList: [ + { + Sku: 'wishlist-sku', + Name: 'Designer Bag', + Category: 'Accessories', + Brand: 'Gucci', + Variant: 'Black', + Price: 2500, + Quantity: 1, + Attributes: { + category: 'luxury' + } + } + ], + TransactionId: 789 + }, + CurrencyCode: 'USD', + SourceMessageId: SOURCE_MESSAGE_ID, + }); + + checkBasicProperties('track'); + window.fbqObj.should.have.property('eventName', 'AddToWishlist'); + window.fbqObj.params.should.have.property('contents'); + window.fbqObj.params.contents.length.should.equal(1); + window.fbqObj.params.should.have.property('order_id', 789); + + var firstProduct = window.fbqObj.params.contents[0]; + // Standard Facebook fields + firstProduct.should.have.property('id', 'wishlist-sku'); + firstProduct.should.have.property('name', 'Designer Bag'); + firstProduct.should.have.property('brand', 'Gucci'); + firstProduct.should.have.property('item_price', 2500); + firstProduct.should.have.property('quantity', 1); + + // Mapped standard fields + firstProduct.should.have.property('custom_name', 'Designer Bag'); + firstProduct.should.have.property('custom_brand', 'Gucci'); + firstProduct.should.have.property('custom_price', 2500); + firstProduct.should.have.property('custom_category', 'Accessories'); + + // Mapped custom attribute + firstProduct.should.have.property('custom_attribute_category', 'luxury'); + + done(); + }); + + it('should build contents array with mapped attributes for ViewDetail 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 - ViewDetail', + EventCategory: CommerceEventType.ProductViewDetail, + EventDataType: MessageType.Commerce, + ProductAction: { + ProductActionType: ProductActionType.ViewDetail, + ProductList: [ + { + Sku: 'view-sku-999', + Name: 'Laptop Pro', + Category: 'Electronics', + Brand: 'Apple', + Variant: '16-inch', + Price: 3000, + Quantity: 1, + Attributes: { + category: 'computers' + } + } + ], + TransactionId: 999 + }, + CurrencyCode: 'USD', + SourceMessageId: SOURCE_MESSAGE_ID, + }); + + checkBasicProperties('track'); + window.fbqObj.should.have.property('eventName', 'ViewContent'); + window.fbqObj.params.should.have.property('contents'); + window.fbqObj.params.contents.length.should.equal(1); + window.fbqObj.params.should.have.property('order_id', 999); + + var firstProduct = window.fbqObj.params.contents[0]; + // Standard Facebook fields + firstProduct.should.have.property('id', 'view-sku-999'); + firstProduct.should.have.property('name', 'Laptop Pro'); + firstProduct.should.have.property('brand', 'Apple'); + firstProduct.should.have.property('item_price', 3000); + firstProduct.should.have.property('quantity', 1); + + // Mapped standard fields + firstProduct.should.have.property('custom_name', 'Laptop Pro'); + firstProduct.should.have.property('custom_brand', 'Apple'); + firstProduct.should.have.property('custom_price', 3000); + firstProduct.should.have.property('custom_category', 'Electronics'); + + // Mapped custom attribute + firstProduct.should.have.property('custom_attribute_category', 'computers'); + + done(); + }); + + it('should log Purchase event with product names as contents', function (done) { + mParticle.forwarder.init({ + pixelCode: 'test-pixel-code', + sendProductNamesasContents: true + }, reportService.cb, true); + + mParticle.forwarder.process({ + EventName: 'eCommerce - Purchase', + EventDataType: MessageType.Commerce, + ProductAction: { + ProductActionType: ProductActionType.Purchase, + ProductList: [ + { + Sku: 'sku-12', + Name: 'iPhone', + Brand: 'Apple', + Category: 'electronics', + Variant: 'blue', + Price: 1000.99, + Quantity: 1, + Attributes: { + category: 'phones' + } + }, + { + Sku: 'sku-no-name', + // Name missing to test filter + Brand: 'Generic', + Price: 99.99, + Quantity: 1 + }, + { + Sku: 'sku-34', + Name: 'Watch', + Brand: 'Samsung', + Price: 450.99, + Quantity: 2 + } + ], + TransactionId: 'txn-1234', + TotalAmount: 1551.97 + }, + CurrencyCode: 'USD', + SourceMessageId: SOURCE_MESSAGE_ID, + }); + + checkBasicProperties('track'); + window.fbqObj.should.have.property('eventName', 'Purchase'); + window.fbqObj.params.should.have.property('contents'); + window.fbqObj.params.contents.length.should.equal(3); + window.fbqObj.params.contents[0].should.have.property('name', 'iPhone'); + window.fbqObj.params.contents[2].should.have.property('name', 'Watch'); + // content_name should only include products with names (filters out product without Name) + window.fbqObj.params.should.have.property('content_name', ['iPhone', 'Watch']); + window.fbqObj.params.should.have.property('order_id', 'txn-1234'); + window.fbqObj.params.should.have.property('value', 1551.97); + window.fbqObj.params.should.have.property('currency', 'USD'); + window.fbqObj.params.should.have.property('num_items', 4); + window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID); + + done(); + }); + + it('should log AddToCart event with product names as contents', function (done) { + mParticle.forwarder.init({ + pixelCode: 'test-pixel-code', + sendProductNamesasContents: true + }, reportService.cb, true); + + mParticle.forwarder.process({ + EventName: 'eCommerce - AddToCart', + EventCategory: CommerceEventType.ProductAddToCart, + EventDataType: MessageType.Commerce, + ProductAction: { + ProductActionType: ProductActionType.AddToCart, + ProductList: [ + { + Sku: '12345', + Name: 'iPhone 6', + Category: 'Phones', + Brand: 'iPhone', + Variant: '6', + Price: 400, + CouponCode: null, + Quantity: 1 + }, + { + Sku: '1234', + Name: 'iPhone 11', + Category: 'Phones', + Brand: 'iPhone', + Variant: '6', + Price: 500, + CouponCode: null, + Quantity: 2 + } + ], + TransactionId: 123, + Affiliation: 'my-affiliation', + TaxAmount: 40, + ShippingAmount: 10 + }, + CurrencyCode: 'USD', + SourceMessageId: SOURCE_MESSAGE_ID, + + }); + + checkBasicProperties('track'); + window.fbqObj.should.have.property('eventName', 'AddToCart'); + window.fbqObj.params.should.have.property('content_name', ['iPhone 6', 'iPhone 11']); + window.fbqObj.params.should.have.property('content_ids', ['12345', '1234']); + window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID); + done(); + }); + + it('should log RemoveFromCart event with product names as content_name', function (done) { + mParticle.forwarder.init({ + pixelCode: 'test-pixel-code', + sendProductNamesasContents: true + }, reportService.cb, true); + + mParticle.forwarder.process({ + EventName: 'eCommerce - RemoveFromCart', + EventCategory: CommerceEventType.ProductRemoveFromCart, + EventDataType: MessageType.Commerce, + ProductAction: { + ProductActionType: ProductActionType.RemoveFromCart, + ProductList: [ + { + Sku: '12345', + Name: 'iPhone 6', + Category: 'Phones', + Brand: 'iPhone', + Variant: '6', + Price: 200, + CouponCode: null, + Quantity: 1, + TotalAmount: 200 + } + ], + TransactionId: 123, + Affiliation: 'my-affiliation', + TaxAmount: 40, + ShippingAmount: 10, + TotalAmount: 205 + }, + CurrencyCode: 'USD', + SourceMessageId: SOURCE_MESSAGE_ID, + }); + + checkBasicProperties('trackCustom'); + window.fbqObj.should.have.property('eventName', 'RemoveFromCart'); + window.fbqObj.params.should.have.property('content_name', ['iPhone 6']); + window.fbqObj.params.should.have.property('content_ids', ['12345']); + window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID); + done(); + }); + + it('should log Checkout event with product names as contents', function (done) { + mParticle.forwarder.init({ + pixelCode: 'test-pixel-code', + sendProductNamesasContents: true + }, reportService.cb, true); + + mParticle.forwarder.process({ + EventName: 'eCommerce - Checkout', + EventCategory: CommerceEventType.ProductCheckout, + EventDataType: MessageType.Commerce, + ProductAction: { + ProductActionType: ProductActionType.Checkout, + ProductList: [ + { + Sku: 'sku-12', + Name: 'iPhone', + Brand: 'Apple', + Category: 'electronics', + Variant: 'blue', + Price: 1000.99, + Quantity: 1 + }, + { + Sku: 'sku-no-name', + // Name missing to test filter + Brand: 'Generic', + Price: 75.50, + Quantity: 1 + }, + { + Sku: 'sku-34', + Name: 'Watch', + Brand: 'Samsung', + Price: 450.99, + Quantity: 2 + } + ], + TransactionId: 'txn-5678', + TotalAmount: 1978.47, + CheckoutStep: 1 + }, + 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(3); + window.fbqObj.params.contents[0].should.have.property('name', 'iPhone'); + window.fbqObj.params.contents[2].should.have.property('name', 'Watch'); + // content_name should only include products with names (filters out product without Name) + window.fbqObj.params.should.have.property('content_name', ['iPhone', 'Watch']); + window.fbqObj.params.should.have.property('order_id', 'txn-5678'); + window.fbqObj.params.should.have.property('value', 1978.47); + window.fbqObj.params.should.have.property('currency', 'USD'); + window.fbqObj.params.should.have.property('num_items', 4); + window.fbqObj.params.should.have.property('checkout_step', 1); + window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID); + + done(); + }); + + it('should log AddToWishlist event with product names as contents', function (done) { + mParticle.forwarder.init({ + pixelCode: 'test-pixel-code', + sendProductNamesasContents: true + }, reportService.cb, true); + + mParticle.forwarder.process({ + EventName: 'eCommerce - AddToWishlist', + EventCategory: CommerceEventType.ProductAddToWishlist, + EventDataType: MessageType.Commerce, + ProductAction: { + ProductActionType: ProductActionType.AddToWishlist, + ProductList: [ + { + Sku: 'sku-111', + Name: 'Laptop', + Category: 'Electronics', + Brand: 'Dell', + Price: 1200, + Quantity: 1 + }, + { + Sku: 'sku-222', + Name: 'Mouse', + Category: 'Accessories', + Brand: 'Logitech', + Price: 50, + Quantity: 1 + } + ], + TransactionId: 456 + }, + CurrencyCode: 'USD', + SourceMessageId: SOURCE_MESSAGE_ID, + }); + + checkBasicProperties('track'); + window.fbqObj.should.have.property('eventName', 'AddToWishlist'); + window.fbqObj.params.should.have.property('content_name', ['Laptop', 'Mouse']); + window.fbqObj.params.should.have.property('order_id', 456); + window.fbqObj.params.should.have.property('content_ids', ['sku-111', 'sku-222']); + window.fbqObj.params.should.have.property('contents'); + window.fbqObj.params.contents.length.should.equal(2); + window.fbqObj.params.contents[0].should.have.property('id', 'sku-111'); + window.fbqObj.params.contents[0].should.have.property('quantity', 1); + window.fbqObj.params.contents[1].should.have.property('id', 'sku-222'); + window.fbqObj.params.contents[1].should.have.property('quantity', 1); + window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID); + done(); + }); + + it('should log ViewDetail event with product names as contents', function (done) { + mParticle.forwarder.init({ + pixelCode: 'test-pixel-code', + sendProductNamesasContents: true + }, reportService.cb, true); + + mParticle.forwarder.process({ + EventName: 'eCommerce - ViewDetail', + EventCategory: CommerceEventType.ProductViewDetail, + EventDataType: MessageType.Commerce, + ProductAction: { + ProductActionType: ProductActionType.ViewDetail, + ProductList: [ + { + Sku: 'sku-999', + Name: 'Tablet', + Category: 'Electronics', + Brand: 'Samsung', + Price: 800, + Quantity: 1 + } + ], + TransactionId: 789 + }, + CurrencyCode: 'USD', + SourceMessageId: SOURCE_MESSAGE_ID, + }); + + checkBasicProperties('track'); + window.fbqObj.should.have.property('eventName', 'ViewContent'); + window.fbqObj.params.should.have.property('content_name', ['Tablet']); + window.fbqObj.params.should.have.property('order_id', 789); + window.fbqObj.params.should.have.property('content_ids', ['sku-999']); + window.fbqObj.params.should.have.property('contents'); + window.fbqObj.params.contents.length.should.equal(1); + window.fbqObj.params.contents[0].should.have.property('id', 'sku-999'); + window.fbqObj.params.contents[0].should.have.property('quantity', 1); + window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID); + done(); + }); }); });