type:
integration
supports-foxycart-version-from:
2.0
system:
pinterest
name:
Pinterest Pixel Conversion Tracking
description:
Tracking Foxy conversions with the Pinterest Pixel
tags:
conversion, tracking, analytics, pixel
date:
2021-10-06

Pinterest 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 post in our forum, but if we cannot offer assistance (due to unfamiliarity with this particular system or language) we apologize in advance.

Installation

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 %}
<!-- Pinterest Pixel Code -->
{% set pinterest_pixel_id = "###########" %}
<script>
    !function(e){if(!window.pintrk){window.pintrk=function(){window.pintrk.queue.push(
     Array.prototype.slice.call(arguments))};var
     n=window.pintrk;n.queue=[],n.version="3.0";var
     t=document.createElement("script");t.async=!0,t.src=e;var
     r=document.getElementsByTagName("script")[0];r.parentNode.insertBefore(t,r)}}("https://s.pinimg.com/ct/core.js");
    pintrk('load', '{{ pinterest_pixel_id }}');
    pintrk('page');
 
    {% if first_receipt_display %}
    if (typeof pintrk != 'undefined') {
        var line_items = [];
        for (var i = 0; i < FC.json.items.length; i++) {
            line_items.push({ 'product_name': FC.json.items[i].name, 'product_id': FC.json.items[i].code, 'product_quantity': FC.json.items[i].quantity, 'product_price': FC.json.items[i].price_each, 'product_category': FC.json.items[i].category });
        }
        pintrk('track', 'checkout', {
            'order_id': '{{ transaction_id }}',
            'value': {{ total_order }},
            'order_quantity': {{ item_count }},
            'currency': '{{ locale_info.int_curr_symbol|trim }}',
            'line_items': line_items
        });
    }
    {% endif %}
</script>
<noscript><img height="1" width="1" style="display:none;" alt="" src="https://ct.pinterest.com/v3/?tid={{ pinterest_pixel_id }}&event=init&noscript=1" /></noscript>
<!-- End Pinterest Pixel Code -->
{% endif %}

Within the script, edit the line near the start to set your Pinterest pixel ID {% set pinterest_pixel_id = “###########” %}.

The script will automatically include the Pinterest 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 the Checkout event for each completed transaction.

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 Pinterest Pixel initialisation code on your website already too):

<script>
var FC = FC || {};
FC.existingOnLoad_pinterest = (typeof FC.onLoad == "function") ? FC.onLoad : function () { };
 
FC.onLoad = function () {
    FC.existingOnLoad_pinterest();
    FC.client.on('cart-submit.done', function (params) {
        if (
            typeof pintrk != 'undefined'
            && FC.client.isActionNeeded(params)
            && (!FC.sidecart || (FC.sidecart
                && !FC.sidecart.shouldUseFullpageCart()
                && !FC.sidecart.bypassSidecart(params)
            ))
        ) {
            var item_name = decodeURIComponent(params.data.name);
            var item_price = parseFloat(params.data.price);
            var item_quantity = (params.data.hasOwnProperty('quantity') ? parseInt(params.data.quantity) : 1);
            var item_code = (params.data.hasOwnProperty('code') ? decodeURIComponent(params.data.code) : "");
            var item_category = (params.data.hasOwnProperty('category') ? decodeURIComponent(params.data.category) : "DEFAULT");
            pintrk('track', 'addtocart', {
                'order_id': FC.json.transaction_id,
                'value': item_price,
                'order_quantity': item_quantity,
                'currency': FC.json.locale_info.int_curr_symbol.trim(),
                'line_items': [{
                    'product_name': item_name,
                    'product_id': item_code,
                    'product_quantity': item_quantity,
                    'product_price': item_price,
                    'product_category': item_category
                }]
            });
        }
    });
}
</script>

Site Tools