How to add e-commerce events (add_to_cart, remove_from cart...) to an if-statement in liquid?

Topic summary

Goal: Track e-commerce events (e.g., view_item, add_to_cart) by pushing to dataLayer via a snippet included in theme.liquid. Example provided for view_item using a Liquid condition on product template.

Key issue: Seeking a Liquid-based trigger for add_to_cart (and remove_from_cart). Attempted Liquid conditions but couldn’t find a working approach.

Guidance: Add-to-cart cannot be detected in Liquid because Liquid is server-side templating and doesn’t listen to client-side actions. These events must be handled in JavaScript, bound to the theme’s add-to-cart action. The relevant JS file differs by theme.

Action requested: Share the store URL or theme name so helpers can locate the appropriate JavaScript file/hook and suggest where to push the dataLayer event.

Status: Open/ongoing. No code solution provided yet; next steps depend on providing theme/store details and moving event tracking from Liquid to JavaScript.

Summarized with AI on February 2. AI used: gpt-5.

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.:

{% if template == ‘product’ %}

{% 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:

{% if product.add %}

{% 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.