Solved

How to create "Added to Cart" Event with 2 different button class notations

  • 29 November 2021
  • 2 replies
  • 212 views

Badge +2

Hi!

I’m trying to add the “Added to Cart” Snippet to a Shopify store that has a different button class for the best-selling product (screenshot 1) and the other products (screenshot 2).

Screenshot 1

 

Screenshot 2

 

 

This is the snippet that I’ve added to the different product.liquid files and it does not seem to be working (probably the code is wrong):

<script type="text/javascript">
var _learnq = _learnq || [];
var classname = document.getElementsByClassName("btn-theme addToCartButtonMain" or "action_button add_to_cart");
var addToCart = function() {
_learnq.push(['track', 'Added to Cart', item]);
}; for (var i = 0; i < classname.length; i++) {
classname[i].addEventListener('click', addToCart, false);
}
</script>

 

I’ve made sure to add the “Viewed Product” snippet and check that the metric is working. Could anyone help me with this please? The theme is Turbo / Chicago.

 

Thanks a lot!

icon

Best answer by stephen.trumble 3 December 2021, 18:03

View original

2 replies

Userlevel 7
Badge +60

Hey @maitran 

I am asking some of our experts for some advice on this and will update you as soon as i have their answer as to how to solve for this. Thank you for your patience.

Userlevel 7
Badge +60

Hey @maitran 

The best practice would be to actually have your developer or the theme developer either add a targetable parent class to the add to cart buttons or figure out a way to better consolidate all of those buttons to be targeted by one snippet. You should be able to add 2 separate add to cart codes each pointing to the different class,  but the drawback to having the multiple add to cart codes is that it may cause some onsite issues depending on how you have your page built and it could potentially interact with other codes.

Reply