Skip to content

Instantly share code, notes, and snippets.

@philcon93
Last active March 8, 2018 04:16
Show Gist options
  • Save philcon93/7a9c67990ed9ebcab2352c04e7b40e1a to your computer and use it in GitHub Desktop.
Save philcon93/7a9c67990ed9ebcab2352c04e7b40e1a to your computer and use it in GitHub Desktop.
FB Pixel addtocart functions

FB Pixel addtocart event

Suppose you have an e-commerce website and your "Add to Cart" button does not navigate to a new page. You may want to activate an event when the button is clicked.

In this example, we will activate a ViewContent standard event on page load.

When someone clicks "Add to Cart" button, we will activate an AddToCart standard event.

[%if [@config:current_page_type@] eq 'product'%]
[%set [@facebook_track@]%]ViewContent[%/set%]
[%set [@facebook_extra@]%], {
content_name: '[%url_encode%][@name@][%/url_encode%]',
content_category: '[%CONTENT_PATH id:'[@inventory_id@]' type:'category' limit:'1'%][%PARAM *body%][%url_encode%][@content_name@][%/url_encode%][%END PARAM%][%END CONTENT_PATH%]',
content_ids: ['[@sku@]'],
content_type: 'product',
value: [@price@],
currency: '[@CONFIG:DEFAULTCURRENCY@]'
}[%/set%]
[%elseif [@config:current_page_type@] eq 'category' and [@form:rf@] eq 'kw'%]
[%set [@facebook_track@]%]Search[%/set%]
[%set [@facebook_extra@]%], {
search_string: '[%url_encode%][@form:kw@][%/url_encode%]'
}[%/set%]
[%elseif [@config:current_page_type@] eq 'category'%] [%set [@facebook_track@]%]ViewContent[%/set%]
[%set [@facebook_extra@]%], {
content_name: '[%url_encode%][@content_name@][%/url_encode%]',
content_type: 'product',
content_ids: [''[%site_value id:'facebook-pixel' type:'load'/%]]
}[%/set%]
[%elseif [@config:current_page_type@] eq 'checkout' and [@CONFIG:GA_FUNNEL_VALUE@] eq '/purchase/shopping_cart.html'%]
[%set [@facebook_track@]%]InitiateCheckout[%/set%]
[%set [@facebook_extra@]%][%/set%]
[%elseif [@config:current_page_type@] eq 'checkout' and [@CONFIG:GA_FUNNEL_VALUE@] eq '/purchase/confirmation.html'%]
[%set [@facebook_track@]%]AddPaymentInfo[%/set%]
[%set [@facebook_extra@]%][%/set%]
[%else%]
[%set [@facebook_track@]%][%/set%]
[%set [@facebook_extra@]%][%/set%]
[%/if%]
<!-- Facebook Pixel Code -->
<script>
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window, document,'script',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '[@REFERRAL_KEY1@]');
fbq('track', 'PageView');
[%if [@facebook_track@] ne ''%]
fbq('track', '[@facebook_track@]'[@facebook_extra@]);
[%/if%]
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=[@REFERRAL_KEY1@]&ev=PageView&noscript=1"
/></noscript>
<!-- End Facebook Pixel Code -->
<!-- Handle Neto data for FB Pixel -->
<script>
function cartLoaded(){
console.log('hey cart has loaded! lets send some tracking stuff')
}
// Sets up data for fb pixel
function fbProductSetup(product) {
return {
'content_name': product.name,
"content_category": product.category_fullname ? product.category_fullname : "",
'content_ids': product.SKU ? product.SKU : product.parent_sku,
'content_type': 'product',
'value': product.price,
'currency': '[@CONFIG:DEFAULTCURRENCY@]'
}
}
function fbPixelAddToCart(){
var nProduct = $.getLastItemAdded();
var fbProduct = fbProductSetup(nProduct);
console.log('Sent to fbPixel');
fbq('track', 'AddToCart', fbProduct);
}
function fbPixelAddMultiToCart(){
var nProducts = $.getLastItemsAdded();
for (var i = 0; i < nProducts.length; i++) {
var fbProduct = fbProductSetup(nProducts[i]);
console.log('Sent multi add fbPixel');
fbq('track', 'AddToCart', fbProduct);
}
}
nAddItemCallbacks.push(fbPixelAddToCart);
nAddMultiItemsCallbacks.push(fbPixelAddMultiToCart);
nCartInitCallbacks.push(cartLoaded)
</script>
<!-- End Handle Neto data for FB Pixel -->
<!-- Facebook Pixel Code -->
<script>
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
document,'script','//connect.facebook.net/en_US/fbevents.js');
fbq('init', '[@REFERRAL_KEY1@]');
fbq('track', 'PageView');
fbq('track', "Purchase", {
content_type: 'product',
content_ids: [''[%show_order id:'[@order_id@]'%][%param *body%],'[@sku@]'[%/param%][%/show_order%]],
value: [@grand_total@],
currency: '[@CONFIG:DEFAULTCURRENCY@]'
});
</script>
<noscript>
<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=[@REFERRAL_KEY1@]&ev=PageView&noscript=1" />
</noscript>
<!-- End Facebook Pixel Code -->
[%site_value id:'facebook-pixel'%],'[@sku@]'[%/site_value%]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment