type:
integration
supports-foxycart-version-from:
2.0
system:
facebook
name:
Facebook Pixel Conversion Tracking
description:
Tracking Foxy conversions with the Facebook Pixel
tags:
conversion, tracking, analytics, pixel
date:
2017-07-24

Facebook Pixel Conversion Tracking

Please note: The code on this page is submitted by members of the FoxyCart community, and may not verified by FoxyCart.com LLC in any way, shape, or form. Please double check the code before installing. If you need help with it please reach out to us, but if we cannot offer assistance (due to unfamiliarity with this particular system or language) we apologize in advance.

Installation

As Facebook now requires that you verify all domains that you have the tracking pixel on, you will need to make use of a custom subdomain for your store for the Facebook tracking pixel to work. Configuring a custom SSL subdomain for your store is free, it just needs to be requested, and once configured, add some DNS records to your domain. You can see more details on that here.

Within your store's FoxyCart administration, go to the “configuration” section and enable the “Add custom header and footer code to your templates” option. Within the text boxes that appear, paste the following code into the “footer” textarea:

{% if context != "cart" or cart_is_fullpage %}
<!-- Facebook Pixel Code -->
{% set fb_pixel_id = "###########" %}
<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', '{{ fb_pixel_id }}');
	fbq('track', "PageView");
 
	if (typeof fbq != 'undefined') {
		{% if context == "cart" %}
			fbq('track', 'AddToCart');
		{% elseif context == "checkout" %}
			fbq('track', 'InitiateCheckout');
		{% elseif first_receipt_display %}
  			var item_contents = [];
  			for (var i = 0; i < FC.json.items.length; i++) {
  				item_contents.push({"id": FC.json.items[i].code, "quantity": FC.json.items[i].quantity, "item_price": FC.json.items[i].price_each});
  			}
			fbq('track', 'Purchase', {value: '{{ total_order }}', currency: '{{ locale_info.int_curr_symbol|trim }}', contents: item_contents, content_type: 'product'});
		{% endif %}
	}
</script>
<noscript><img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id={{ fb_pixel_id }}&ev=PageView&noscript=1" /></noscript>
<!-- End Facebook Pixel Code -->
{% endif %}

Within the script, edit the first line to set your Facebook pixel ID {% set fb_pixel_id = “###########” %}.

The script will automatically include the Facebook Pixel initialisation code on the full page cart, checkout and receipt, but will rely on you including it on your own website for the Sidecart view. It will also track page views on the full page cart, checkout and receipt, as well as tracking AddToCart, InitiateCheckout and Purchase events.

On your own website, in order to track the AddToCart event for Sidecart, add the following code to the footer of your website (this assumes you've placed the Facebook Pixel initialisation code on your website already too):

<script>
var FC = FC || {};
FC.existingOnLoad_pixel = (typeof FC.onLoad == "function") ? FC.onLoad : function() {};
 
FC.onLoad = function () {
    FC.existingOnLoad_pixel();
    FC.client.on('cart-submit.done', function(params) {
        if (
            typeof fbq != 'undefined'
            && params.hasOwnProperty('data')
            && params.data.hasOwnProperty('name')
            && FC.client.isActionNeeded(params)
            && (!FC.sidecart || (FC.sidecart
                && !FC.sidecart.shouldUseFullpageCart()
                && !FC.sidecart.bypassSidecart(params)
            ))
        ) {
            var item_quantity = (params.data.hasOwnProperty('quantity') ? parseInt(params.data.quantity) : 1);
            var item_price = parseFloat(params.data.price);
            fbq('track', 'AddToCart', { 'content_name': decodeURIComponent(params.data.name), 'value': (item_price * item_quantity), 'currency': FC.json.locale_info.int_curr_symbol.trim(), 'contents':  [{ 'id': params.data.code, 'quantity': item_quantity, 'item_price': item_price }], 'content_type': 'product' });
        }
    });
}
</script>

Site Tools