Personalized checkout and custom promotions with Shopify Scripts
I'm working on adding a custom pixel script to my Shopify store that pushes purchase event data, including detailed line items, to Google Tag Manager. I've written the following Liquid code to achieve this:
{% assign items = '' %}
{% for line_item in order.line_items %}
{% assign variant = '' %}
{% if line_item.title contains 'Mini' %}
{% assign variant = 'Mini' %}
{% elsif line_item.title contains 'Small' %}
{% assign variant = 'Small' %}
{% elsif line_item.title contains 'Standard' %}
{% assign variant = 'Standard' %}
{% endif %}
{% capture current_item %}
{
"item_id": "{{ line_item.variant.sku }}",
"item_name": "{{ line_item.title }}",
"variant": "{{ variant }}",
"coupon": "{{ order.discount_codes.first.code }}",
"discount": {{ line_item.total_discount | money_without_currency }},
"item_brand": "Kori Krill Oil",
"item_category": "{{ line_item.product.type }}",
"price": {{ line_item.price | money_without_currency }},
"quantity": {{ line_item.quantity }}
}
{% endcapture %}
{% if forloop.first %}
{% assign items = current_item %}
{% else %}
{% assign items = items | append: ',' | append: current_item %}
{% endif %}
{% endfor %}
var items = [{% raw %}{{ items }}{% endraw %}];
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'purchase',
event_category: 'purchase',
event_action: 'purchase',
currency: "{{ order.currency }}",
transaction_id: "{{ order.order_number }}",
value: {{ order.total_price | money_without_currency }},
tax: {{ order.tax_total | money_without_currency }},
shipping: {{ order.shipping_price | money_without_currency }},
coupon: "{{ order.discount_codes.first.code }}",
items: items
});
Same error we are facing with installing other pixels.
What is this
By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024Thanks to everyone who participated in our AMA with 2H Media: Marketing Your Shopify St...
By Jacqui Sep 6, 2024