Skip to content

Instantly share code, notes, and snippets.

@martinsoender
Created November 22, 2021 09:14
Show Gist options
  • Save martinsoender/7b5a4381648cbd31400430c88a859df9 to your computer and use it in GitHub Desktop.
Save martinsoender/7b5a4381648cbd31400430c88a859df9 to your computer and use it in GitHub Desktop.
Replace <insert_pixel_id> with the Facebook pixel id
{% if first_time_accessed %}
<!-- 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', '<insert_pixel_id>');
fbq('track', 'PageView');
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=<insert_pixel_id>&ev=PageView&noscript=1"
/></noscript>
<!-- End Facebook Pixel Code -->
<!-- Facebook Pixel Event - Purchase -->
<script>
window.addEventListener('DOMContentLoaded', () => {
console.log('Initiated Event - Purchase')
fbq('track', 'Purchase', {
content_type: 'product',
content_name: [{%- for line_item in order.line_items -%}'{{ line_item.title }}'{%- unless forloop.last -%},{%- endunless -%}{%- endfor -%}],
content_ids: [{%- for line_item in order.line_items -%}'{{ line_item.sku }}'{%- unless forloop.last -%},{%- endunless -%}{%- endfor -%}],
currency: '{{ shop.currency }}',
num_items: {{ checkout.line_items | size }},
value: parseFloat('{{ checkout.total_price | money_without_currency }}')
});
});
</script>
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment