Skip to content

Adds Search/InitiateCheckout events #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 17, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions integrations/facebook-pixel/HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2.5.4/ 2018-09-13
==================

* Adds Search and InitiateCheckout event support

2.5.2/ 2018-09-13
==================

Expand Down
54 changes: 54 additions & 0 deletions integrations/facebook-pixel/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion integrations/facebook-pixel/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
98 changes: 97 additions & 1 deletion integrations/facebook-pixel/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
})
})
})
});
});
});
Expand Down