# Facebook tracking pixel

Create a snippet called "facebook-pixel.liquid" and paste the following code:

snippets/facebook-pixel.liquid

{% assign facebookPixel = shop.apps.facebook_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', '{{ facebookPixel }}');
        fbq('track', 'PageView');

    {% if request.path contains 'products/' %}
        fbq('track', 'ViewContent', {value: '{{ product.price | divided_by: 100 }}', currency: '{{ shop.currency }}', content_name: '{{ product.title }}', content_type: 'product', content_ids: [{% for variant in product.variants %}{{ variant.id }}{% unless forloop.last %},{% endunless %}{% endfor %}]});
    {% endif %}

    {% if request.path contains 'checkout' %}
    {% unless request.path contains 'checkout/thanks/' %}
        fbq('track', 'InitiateCheckout', {value: {{ cart.total_price | divided_by: 100 }}, currency: '{{ shop.currency }}', num_items: {{ cart.item_count }}, content_ids: [{% for row in cart.items %}{{ row.variant.id }}{% unless forloop.last %},{% endunless %}{% endfor %}]});
    {% endunless %}
    {% endif %}

    {% if request.path contains 'checkout/thanks/' and first_time_accessed %}
        fbq('track', 'Purchase', {value: '{{ order.total_price | divided_by: 100 }}', currency: '{{ shop.currency }}', content_type: 'product', content_ids: [{% for row in order.line_items %}{{ row.variant.id }}{% unless forloop.last %},{% endunless %}{% endfor %}]});
    {% endif %}

    {% if cart.just_added_item %}
        fbq('track', 'AddToCart', {
            value: '{{ cart.just_added_item.variant.price | divided_by: 100 }}',
            currency: '{{ shop.currency }}',
            content_ids: ['{{ cart.just_added_item.variant.id }}'],
            content_type: 'product'
        });
    {% endif %}

</script>
<noscript>
    <img height="1" width="1" src="https://www.facebook.com/tr?id={{ facebookPixel }}&ev=PageView&noscript=1"/>
</noscript>

Include it in your layout/theme.liquid just before the closing of your </head> tag.

<!doctype html>
<html lang="en">
<head>
    ...
    {% include 'facebook-pixel' %}
</head>

TIP

If you have a setup with child/parent shops the scripts are typically only inserted in the the parent shop's theme. However, feel free to overwrite and modify the scripts for each child shop you have.

Now you can enter your Facebook Pixel ID within Settings ->Setup -> Analytics -> Facebook Pixel ID and your snippet will use that pixel id.

This is a basic setup of the the snapchat tracking script. We include as a snippet in Codefort so you can customize it however you want.