Skip to main content
Solved

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

  • November 29, 2021
  • 2 replies
  • 285 views

Forum|alt.badge.img+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!

Best answer by stephen.trumble

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.

View original
Did this topic or the replies in the thread help you find an answer to your question?

2 replies

stephen.trumble
Community Manager
Forum|alt.badge.img+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.


stephen.trumble
Community Manager
Forum|alt.badge.img+60
  • Community Manager
  • 1518 replies
  • Answer
  • December 3, 2021

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.