Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Hey there,
I'm trying to add some code to my theme.liquid via a snippet called "e_commerce_events". In this snippet I want to include code that executes a script every time an e-commerce event happens, f.e.:
<!-- view_item -->
{% if template == 'product' %}
<script>
dataLayer.push({'event': 'view_item', 'itemName': '{{ product.title }}'});
</script>
{% endif %}
For the view_item event this is obviously pretty simple and straight forward. However, I am more of a digital marketer and not so much of a developer and I'm struggling with the implementation of events where I cannot use a static state (like template == 'product') as a trigger but an actual event like the add_to_cart event. I'm thinking about something like this:
<!-- add_to_cart -->
{% if product.add %}
<script>
dataLayer.push({'event': 'add_to_cart', 'addedItemName': '{{ cart.item_added.title }}'});
</script>
{% endif %}
I tried a few things here and checked the liquid cheat sheet but I can't really get to a working solution. However, I assume this must be somehow possible and it can't really be that difficult, right?
I would appreciate it very much if someone could help me out here.
Thanks a lot and kind regards!
Ole
Hello @ole_albrecht,
Greetings from the Store Watchers Support Team! Happy to help you today.
You can not listen add to cart event in liquid. You have to add code in JavaScript file where event of add to cart action is binded. The name of JavaScript file changes per theme.
If you can share your store URL we can check which JavaScript file is use for add to cart event.
Let us know if you need further assistance!
Regards,
Store Watchers Support Team
Hi @ole_albrecht,
Please send me the theme name you are using, I will help you to check it, add to cart event need to be added at JS file.
June brought summer energy to our community. Members jumped in with solutions, clicked ...
By JasonH Jun 5, 2025Learn how to build powerful custom workflows in Shopify Flow with expert guidance from ...
By Jacqui May 7, 2025Did You Know? May is named after Maia, the Roman goddess of growth and flourishing! ...
By JasonH May 2, 2025