diff --git a/integrations/facebook-pixel/HISTORY.md b/integrations/facebook-pixel/HISTORY.md index 3a2aff314..707cc5e49 100644 --- a/integrations/facebook-pixel/HISTORY.md +++ b/integrations/facebook-pixel/HISTORY.md @@ -1,3 +1,8 @@ +2.5.4/ 2018-09-13 +================== + + * Adds Search and InitiateCheckout event support + 2.5.2/ 2018-09-13 ================== diff --git a/integrations/facebook-pixel/lib/index.js b/integrations/facebook-pixel/lib/index.js index 2f4acd0c3..bf84bdcae 100644 --- a/integrations/facebook-pixel/lib/index.js +++ b/integrations/facebook-pixel/lib/index.js @@ -351,6 +351,60 @@ FacebookPixel.prototype.orderCompleted = function(track) { }, this.legacyEvents(track.event())); }; +FacebookPixel.prototype.productsSearched = function(track) { + window.fbq('track', 'Search', { + search_string: track.proxy('properties.query') + }); + + + // fall through for mapped legacy conversions + each(function(event) { + window.fbq('track', event, { + currency: track.currency(), + value: formatRevenue(track.revenue()) + }); + }, this.legacyEvents(track.event())); +} + +FacebookPixel.prototype.checkoutStarted = function(track) { + var products = track.products() || []; + var contentIds = []; + var contents = []; + var contentCategory = track.category(); + + each(function(product) { + var track = new Track({ properties: product }); + contentIds.push(track.productId() || track.id() || track.sku()) + contents.push({ + id: track.productId() || track.id() || track.sku(), + quantity: track.quantity(), + item_price: track.price(), + }) + }, products); + + // If no top-level category was defined use that of the first product. @gabriel + if (!contentCategory && products[0] && products[0].category) { + contentCategory = products[0].category + } + + window.fbq('track', 'InitiateCheckout', { + content_category: contentCategory, + content_ids: contentIds, + contents: contents, + currency: track.currency(), + num_items: contentIds.length, + value: formatRevenue(track.revenue()) + }); + + // fall through for mapped legacy conversions + each(function(event) { + window.fbq('track', event, { + currency: track.currency(), + value: formatRevenue(track.revenue()) + }); + }, this.legacyEvents(track.event())); +} + /** * mappedContentTypesOrDefault returns an array of mapped content types for * the category - or returns the defaul value. diff --git a/integrations/facebook-pixel/package.json b/integrations/facebook-pixel/package.json index aa57b38f3..a60b6ade1 100644 --- a/integrations/facebook-pixel/package.json +++ b/integrations/facebook-pixel/package.json @@ -1,7 +1,7 @@ { "name": "@segment/analytics.js-integration-facebook-pixel", "description": "The Facebook Pixel analytics.js integration.", - "version": "2.5.2", + "version": "2.5.4", "keywords": [ "analytics.js", "analytics.js-integration", diff --git a/integrations/facebook-pixel/test/index.test.js b/integrations/facebook-pixel/test/index.test.js index c8d548776..bec996a22 100644 --- a/integrations/facebook-pixel/test/index.test.js +++ b/integrations/facebook-pixel/test/index.test.js @@ -588,7 +588,103 @@ describe("Facebook Pixel", function() { currency: "USD", value: "0.50" }); - }); + }) + + describe('.productsSearched()', function() { + it('should send pixel the search string', function() { + analytics.track('Products Searched', { query: 'yo' }) + analytics.called(window.fbq, 'track', 'Search', { + search_string: 'yo' + }) + }) + }) + + describe('.checkoutStarted()', function() { + it('should call InitiateCheckout with the top-level category', function() { + analytics.track('Checkout Started', { + category: 'NotGames', + order_id: '50314b8e9bcf000000000000', + affiliation: 'Google Store', + value: 30, + revenue: 25, + shipping: 3, + tax: 2, + discount: 2.5, + coupon: 'hasbros', + currency: 'USD', + products: [ + { + product_id: '507f1f77bcf86cd799439011', + sku: '45790-32', + name: 'Monopoly: 3rd Edition', + price: 19, + quantity: 1, + category: 'Games', + url: 'https://www.example.com/product/path', + image_url: 'https://www.example.com/product/path.jpg' + }, + { + product_id: '505bd76785ebb509fc183733', + sku: '46493-32', + name: 'Uno Card Game', + price: 3, + quantity: 2, + category: 'Games' + } + ] + }) + analytics.called(window.fbq, 'track', 'InitiateCheckout', { + content_ids: ["507f1f77bcf86cd799439011", "505bd76785ebb509fc183733"], + value: "25.00", + contents: [{ "id": "507f1f77bcf86cd799439011", "quantity": 1, "item_price": 19 }, { "id": "505bd76785ebb509fc183733", "quantity": 2, "item_price": 3 }], + num_items: 2, + currency: 'USD', + content_category: 'NotGames', + }) + }) + + it('should call InitiateCheckout with the first product category', function() { + analytics.track('Checkout Started', { + order_id: '50314b8e9bcf000000000000', + affiliation: 'Google Store', + value: 30, + revenue: 25, + shipping: 3, + tax: 2, + discount: 2.5, + coupon: 'hasbros', + currency: 'USD', + products: [ + { + product_id: '507f1f77bcf86cd799439011', + sku: '45790-32', + name: 'Monopoly: 3rd Edition', + price: 19, + quantity: 1, + category: 'Games', + url: 'https://www.example.com/product/path', + image_url: 'https://www.example.com/product/path.jpg' + }, + { + product_id: '505bd76785ebb509fc183733', + sku: '46493-32', + name: 'Uno Card Game', + price: 3, + quantity: 2, + category: 'Games' + } + ] + }) + analytics.called(window.fbq, 'track', 'InitiateCheckout', { + content_ids: ["507f1f77bcf86cd799439011", "505bd76785ebb509fc183733"], + value: "25.00", + contents: [{ "id": "507f1f77bcf86cd799439011", "quantity": 1, "item_price": 19 }, { "id": "505bd76785ebb509fc183733", "quantity": 2, "item_price": 3 }], + num_items: 2, + currency: 'USD', + content_category: 'Games', + }) + }) + }) }); }); });