Having trouble's installing "added to cart" metric into klaviyo for one of my clients

  • 12 January 2021
  • 8 replies

Badge +2

Hey guys, I’m having trouble installing one of the following metrics.

That being added to cart.

On my clients shopify store, I have inspected the element of the add to cart button and even applied both snippets (to testing reasons) for id and class. Obviously I’ve followed instructions to paste the snippet of code under the product.liquid file

For you information, my clients “add to cart” button has a class notation.

However, when I use the alternate code snippet and modify it, still does not show the metric in klaviyo.

I also want to point out that the add to cart button on my clients store is automated to pulse frequently switching from:

class=”ProductForm__AddToCart Button Button--secondary Button--full vtl-aa-animated-atc-button”


class=”ProductForm__AddToCart Button Button--secondary Button--full vtl-aa-animated-atc-button vtl-aa-active”

I believe this is the main reason klaviyo can’t identify the added to cart metric.

If anyone knows anyway to bypass this, or if I am installing the code wrong please message me back.

Since the add to cart button has a class notation this is the code Klaviyo help centre told me to use (yes I have tested without modifying the code also - still to no avail )

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



Best answer by retention 12 January 2021, 23:42

View original

8 replies

Userlevel 5
Badge +8

Hi there,


The snippet you’ve provided uses “add-to-cart” as the class name. Based on the button code you’ve also provided, it looks like the name of the class is actually “ProductForm__AddToCart” -- as a result, I’d suggest switching the querySelector to use ‘.ProductForm__AddToCart’ or whatever the actual class name is for your Add to Cart button.





Userlevel 7
Badge +57

@Blythe_Adrian  … Going on what @caroline suggested, try this:

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

Let us know if it worked!

Badge +1

Hey guys! Help me! My add to cart button looks like this. Nothing matches on klaviyo’s article.


Userlevel 7
Badge +61

Hi @Lee,

Thanks for sharing your question with the Klaviyo Community.

I can see that your button does not have an ID and instead uses a class notation. In this circumstance, you’ll need to paste an alternative snippet of code into Shopify. Maybe you can clarify what you mean by “nothing like this” and “nothing matches on Klaviyo’s article” because our article on Added to Cart does detail the code to use for a button using class here.

If you’re still having trouble logging the Added to Cart event, after reviewing the article, I recommend reviewing the posts from this thread. The last post on the thread summarizes the necessary steps you need to take to successfully log the Added to Cart metric.

Thanks and have a wonderful day.

Badge +2

Hey there, I am having the same problem. It looks as if i have a button id and a class notation. I have tried following the steps both, applying everything i can think of.

can anyone help?

Userlevel 7
Badge +60

Hello @ethan,

The button code you highlighted actually resembled the example found in the Alternate Code Snippet for "Add to Cart" Button with Class Notation subsection of the How to Create a Custom "Added to Cart" Event for Shopify article Klaviyo offers. Although they are not exactly the same, the same theory would apply since your button is using multiple classes. I would suggest editing and using the base code below in your product page to apply this add to cart functionality:

<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);

After customizing and installing the code, I would also suggest testing it by following the below steps which I’ve mentioned in a similar Community post:

You can cookie your own browser with your desired email by following these steps:

  • Navigate to your website/homepage in an incognito browser
  • Add the following to the end of your store URL, replacing with your email address:
    • Example URL:
  • After you reload the page your browser should be cookied with your desired email address, which would create a profile within Klaviyo
  • Search in Klaviyo for your email address

If you are still having trouble I would encourage you working with a developer or the Shopify’s theme developer use an ID that's consistent across all product pages, or add a class to all of their product pages (such as "add-to-cart"), so that the metric can be tracked off of a single identifier. You can also finding a Klaviyo partner who can also further help you with this as well.

I hope this helps!


Badge +2


Thanks for your response! I’ve tried what you have recommended but still no luck.

Could you be more specific on what my class notation exactly is?

Also I’ve looked through my other liquid files and cant find anywhere else that would require the code snippet.

I am also a little confused because when I first applied the snippets and was accepted to make the flow, it tracked myself adding to cart once, making me think it was successful but since then it hasn’t worked.

I’ve done a lot of work on this theme and don’t want to have to make a whole new website just for this metric.

Thanks, ethan

Userlevel 7
Badge +60

Hello @ethan,

The class notation, from your screenshot seems to be btn btn--full product-form__cart-submit btn--secondary-accent.

If you were able to track an add to cart event, then that would mean that the code you first used and applied did work correctly. Keep in mind that the add to cart event would only be triggered if your browser is cookied. Even if your code was installed correctly but your browser was not cookied and you added an item to the cart, the event would not trigger and track.