I have created a custom event and created a datalayer push into the template.liquid-file in my Shopify. I can get my custom event to fire in Tag Manager when I just view a product, so I know that the push works, but I need to trigger the push when a product is added to my Shopify cart, and I'm not sure it's even possible the way I'm trying to do it.
This is the code I currently have for the push:
{% if cart.item_count > 0 %}
<script type="text/javascript" defer>
window.dataLayer = window.dataLayer || [];
dataLayer.push({
'event': 'addedToCart',
'ecommerce': {
'currencyCode': '{{ shop.currency }}',
'add': {
'products': [{
'name': '{{ product.title }}',
'id': {{ product.id }},
'price': {{ product.price | divided_by: 100 }},
'category': '{{ product.type }}',
'variant': '{{ product.selected_or_first_available_variant }}',
'quantity': '{{ product.quantity }}'
}]
}
}
});
cart_data = {{ cart | json}};
console.log(cart_data);
</script>
{% endif %}
I'm trying to avoid triggering on button clicks, since there are many different buttons on the store, but if that's the only way around this, I will do it