Happening now | Office Hours: Customizing Your Theme With Moeed | Ask your questions now!

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

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

ole_albrecht
Visitor
1 0 0

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

Replies 2 (2)

StoreWatchers
Trailblazer
205 30 39

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

 

StoreWatchers - Automate testing for your Shopify store in seconds


If you find my reply helpful, please hit Like and Mark as Solution
Visit us: App Store | Website | FAQs

LitExtension
Shopify Partner
4958 1009 1190

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.

LitExtension - Shopping Cart Migration Expert
Check out our Shopify migration app to migrate your online store to Shopify
Contact us:
- Email: contact@litextension.com