Solved

Add to Cart metric is not working !!

  • 21 April 2022
  • 4 replies
  • 354 views

Badge +3

Before asking here  I've read all the query related to this topic like all of  the community post but i just didn’t got my problem resolved 

that’s why the last option for me is asking here 

I've read  the whole guide of Guide to Creating an "Added to Cart" Event for Shopify for  freaki’n 10 times so my shopify theme is turbo and it has class notation instead of a button id 

<script type="text/javascript">
var _learnq = _learnq || [];
var classname = document.getElementsByClassName("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>

the above code is  needed to be  pasted in the  bottom of product.liquid  file  and I have to modify the above code with the class name 

This Is the class notation of my site’s add to cart button 

Below screen shot is the modified code pasted in  the bottom of my product.liquid file 

still my added to cart event is not being tracked and wait !!! i  also cookie myself with adding this ?utm_email=example@gmail.com in the end of my store’s url and also replacing example@gmail.com with my email id  and you must be thinking may be his onsite web tracking is not enabled but the truth is my active on site metric & viewed product metric is  tracking absolutely alright and enabled 

@mkprkr @retention @Dov @Taylor Tarpley @caroline 

icon

Best answer by alex.hong 25 April 2022, 23:34

View original

4 replies

Userlevel 7
Badge +58

Hi there @abubakaranees,

Welcome to the Community.

Thank you for sharing your question here and providing screenshots. 

I will say the first step to troubleshooting is making sure your syntax and variables are all accurate and there is no error within the code itself. For example, make sure the metric snippet looks like

<script type="text/javascript"> var _learnq = _learnq || [];
document.querySelector('.action_button.add_to_cart').addEventListener('click',function (){
_learnq.push(['track', 'Added to Cart', item]); }); </script>

I will investigate the matter further and report back.

Alex 

Badge +3

Hey Alex ,

appreciate you reaching out to me , 

the above code that you have pasted is different than what present in Alternate Snippet for "Add to Cart" Button without a Button ID

now I'm a bit confused ?????

Userlevel 7
Badge +58

Hi @abubakaranees,

Apologies. I see the code does not apply to your situation. I would first recommend you check out 

as it provides a similar situation and good solution for your case.

I’d also recommend you take a look at a few other buttons on your site and inspect them to better understand what class you should be targeting. Different buttons can have different classes, and the easiest solution is to find the common class between them all. This is why I said in my initial message to check for syntax and proper variable usage. 

 

Hope that helped,

Alex

Badge +4

I struggled for this issue a quite bit but I found the problem is you can only call the following line after the product is successfull added so you need to listen for add to cart event maybe one of these so “added to cart” metrics will show up in the klaviyo

 

Shopify.onItemAdded = function(line_item) {    	console.log('line item added');  	  };Shopify.onCartUpdate = function(cart) {  alert('There are now ' + cart.item_count + ' items in the cart.');}; 

 

learnq.push(['track', 'Added to Cart', item]); });

Reply