Skip to content

Commit 7e0f638

Browse files
authored
Adds Search/InitiateCheckout events (#64)
* Adds Search and InitiateCheckout event mappings * Adds tests * Bumps version * Updates HISTORY
1 parent c99468a commit 7e0f638

File tree

4 files changed

+157
-2
lines changed

4 files changed

+157
-2
lines changed

integrations/facebook-pixel/HISTORY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2.5.4/ 2018-09-13
2+
==================
3+
4+
* Adds Search and InitiateCheckout event support
5+
16
2.5.2/ 2018-09-13
27
==================
38

integrations/facebook-pixel/lib/index.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,60 @@ FacebookPixel.prototype.orderCompleted = function(track) {
351351
}, this.legacyEvents(track.event()));
352352
};
353353

354+
FacebookPixel.prototype.productsSearched = function(track) {
355+
window.fbq('track', 'Search', {
356+
search_string: track.proxy('properties.query')
357+
});
358+
359+
360+
// fall through for mapped legacy conversions
361+
each(function(event) {
362+
window.fbq('track', event, {
363+
currency: track.currency(),
364+
value: formatRevenue(track.revenue())
365+
});
366+
}, this.legacyEvents(track.event()));
367+
}
368+
369+
FacebookPixel.prototype.checkoutStarted = function(track) {
370+
var products = track.products() || [];
371+
var contentIds = [];
372+
var contents = [];
373+
var contentCategory = track.category();
374+
375+
each(function(product) {
376+
var track = new Track({ properties: product });
377+
contentIds.push(track.productId() || track.id() || track.sku())
378+
contents.push({
379+
id: track.productId() || track.id() || track.sku(),
380+
quantity: track.quantity(),
381+
item_price: track.price(),
382+
})
383+
}, products);
384+
385+
// If no top-level category was defined use that of the first product. @gabriel
386+
if (!contentCategory && products[0] && products[0].category) {
387+
contentCategory = products[0].category
388+
}
389+
390+
window.fbq('track', 'InitiateCheckout', {
391+
content_category: contentCategory,
392+
content_ids: contentIds,
393+
contents: contents,
394+
currency: track.currency(),
395+
num_items: contentIds.length,
396+
value: formatRevenue(track.revenue())
397+
});
398+
399+
// fall through for mapped legacy conversions
400+
each(function(event) {
401+
window.fbq('track', event, {
402+
currency: track.currency(),
403+
value: formatRevenue(track.revenue())
404+
});
405+
}, this.legacyEvents(track.event()));
406+
}
407+
354408
/**
355409
* mappedContentTypesOrDefault returns an array of mapped content types for
356410
* the category - or returns the defaul value.

integrations/facebook-pixel/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@segment/analytics.js-integration-facebook-pixel",
33
"description": "The Facebook Pixel analytics.js integration.",
4-
"version": "2.5.2",
4+
"version": "2.5.4",
55
"keywords": [
66
"analytics.js",
77
"analytics.js-integration",

integrations/facebook-pixel/test/index.test.js

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,103 @@ describe("Facebook Pixel", function() {
588588
currency: "USD",
589589
value: "0.50"
590590
});
591-
});
591+
})
592+
593+
describe('.productsSearched()', function() {
594+
it('should send pixel the search string', function() {
595+
analytics.track('Products Searched', { query: 'yo' })
596+
analytics.called(window.fbq, 'track', 'Search', {
597+
search_string: 'yo'
598+
})
599+
})
600+
})
601+
602+
describe('.checkoutStarted()', function() {
603+
it('should call InitiateCheckout with the top-level category', function() {
604+
analytics.track('Checkout Started', {
605+
category: 'NotGames',
606+
order_id: '50314b8e9bcf000000000000',
607+
affiliation: 'Google Store',
608+
value: 30,
609+
revenue: 25,
610+
shipping: 3,
611+
tax: 2,
612+
discount: 2.5,
613+
coupon: 'hasbros',
614+
currency: 'USD',
615+
products: [
616+
{
617+
product_id: '507f1f77bcf86cd799439011',
618+
sku: '45790-32',
619+
name: 'Monopoly: 3rd Edition',
620+
price: 19,
621+
quantity: 1,
622+
category: 'Games',
623+
url: 'https://www.example.com/product/path',
624+
image_url: 'https://www.example.com/product/path.jpg'
625+
},
626+
{
627+
product_id: '505bd76785ebb509fc183733',
628+
sku: '46493-32',
629+
name: 'Uno Card Game',
630+
price: 3,
631+
quantity: 2,
632+
category: 'Games'
633+
}
634+
]
635+
})
636+
analytics.called(window.fbq, 'track', 'InitiateCheckout', {
637+
content_ids: ["507f1f77bcf86cd799439011", "505bd76785ebb509fc183733"],
638+
value: "25.00",
639+
contents: [{ "id": "507f1f77bcf86cd799439011", "quantity": 1, "item_price": 19 }, { "id": "505bd76785ebb509fc183733", "quantity": 2, "item_price": 3 }],
640+
num_items: 2,
641+
currency: 'USD',
642+
content_category: 'NotGames',
643+
})
644+
})
645+
646+
it('should call InitiateCheckout with the first product category', function() {
647+
analytics.track('Checkout Started', {
648+
order_id: '50314b8e9bcf000000000000',
649+
affiliation: 'Google Store',
650+
value: 30,
651+
revenue: 25,
652+
shipping: 3,
653+
tax: 2,
654+
discount: 2.5,
655+
coupon: 'hasbros',
656+
currency: 'USD',
657+
products: [
658+
{
659+
product_id: '507f1f77bcf86cd799439011',
660+
sku: '45790-32',
661+
name: 'Monopoly: 3rd Edition',
662+
price: 19,
663+
quantity: 1,
664+
category: 'Games',
665+
url: 'https://www.example.com/product/path',
666+
image_url: 'https://www.example.com/product/path.jpg'
667+
},
668+
{
669+
product_id: '505bd76785ebb509fc183733',
670+
sku: '46493-32',
671+
name: 'Uno Card Game',
672+
price: 3,
673+
quantity: 2,
674+
category: 'Games'
675+
}
676+
]
677+
})
678+
analytics.called(window.fbq, 'track', 'InitiateCheckout', {
679+
content_ids: ["507f1f77bcf86cd799439011", "505bd76785ebb509fc183733"],
680+
value: "25.00",
681+
contents: [{ "id": "507f1f77bcf86cd799439011", "quantity": 1, "item_price": 19 }, { "id": "505bd76785ebb509fc183733", "quantity": 2, "item_price": 3 }],
682+
num_items: 2,
683+
currency: 'USD',
684+
content_category: 'Games',
685+
})
686+
})
687+
})
592688
});
593689
});
594690
});

0 commit comments

Comments
 (0)