Solved

How to add "Add to Cart" snippet with Shopify Turbo theme

  • 25 December 2020
  • 10 replies
  • 549 views

Badge

Hello,

I have added the “add to cart” snippet and have followed the instructions: https://help.klaviyo.com/hc/en-us/articles/115001396711-Create-a-Custom-Added-to-Cart-Event-for-Shopify#alternate-code-snippet-for--add-to-cart--button-with-class-notation4

 

However "Add To Cart" matric does not appear. I use Turbo theme.

 

This is code snippet i add to shopify:

 

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

 

Please help me

 

Thank you

icon

Best answer by jlytle 4 February 2021, 03:25

I got help today on this. For Turbo theme you need to remove the . before add_to_cart

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

Now the Add to cart Trigger is functioning. Sweet

View original

10 replies

Userlevel 6
Badge +5

@younow5tr The code that you’ve shared looks good.

After checking the demo of the Turbo theme for Shopify, I can confirm that the Add to Cart button contains the “add_to_cart” class.
 


That being said, and assuming you’ve implemented the code correctly, the only thing before the “Add to Cart” metric appears in your Klaviyo’s Metrics section is the first action/event.

As in many elements of Klaviyo, for something to be recognized and start appearing, it has to have at least one event happened for it so it’s captured in Klaviyo’s database.
For example, when you set an Email in a Flow, and you want to create a Segment of people who received that Email, you can not find that Email in the selectable suggestions until at least one email is sent.

Similar to that, even if you’ve set everything correctly for the “Add to Cart” metric, Klaviyo doesn’t know what you’ve done in the Shopify’s code until at least one event happens that will push data to Klaviyo. At that point Klaviyo will recognize the new data and create the “Add to Cart” metric.

All you need to do to push that first event into Klaviyo is to add something to cart from identifiable browser. Remember that the “Add to Cart”, same as the “Viewed Product” feature works only for existing subscribers, that are browsing from a browser with which they’ve previously opened your email. Whenever some of your subscribers opens one of your emails, a cookie is created that connects that browser with the subscriber. After that, whenever your subscriber comes to your web store from the same browser, Klaviyo knows which subscriber that is.

The other way to push that first event is to wait for it to happen naturally. If you already have subscribers in your list and you’re actively sending emails to them, it’s a matter of time before someone adds something to cart and create that first event.

If this doesn’t help, please share more details and I’ll be happy to help.

Cheers!

Badge

Thank you Joseph for the very clear explanation.

 

I have been waiting for 2 days. Many subscribers have come back and hit 'add to cart' button. But “Add to Cart” metric still DO NOT appears. I  do not understand.

 

 

I had the same question in my mind but got the right answer from this thread so thanks. 

Userlevel 1
Badge

Hi there,

I use the Turbo theme as well and also placed the Add to cart trigger script as follows:

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

I too have been unable to add the Abandoned Cart Reminder Flow to my site because there is a red x that states: This flow requires setting up a special "Added to Cart" metric.

Also, I think Klaviyo should rename their other Abandon Cart Flow, because, in all truth, they are Abandoned Checkout Flows, not Abandoned Cart Flows.

I will report back if time is all it takes to wait for an existing profile to add to cart, but this script has been installed for days and my store does a lot of business per day.

Many thanks,

Userlevel 1
Badge

Correction… I added the script with .add_to_cart, not .add-to-cart:

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

 

I am wondering if this needs to be the only place this exact class exists in the code of the button other than the script.If I view the source of my product detail, I see other instances of “action_button add_to_cart” in the sorurce for the mini-cart such as:

I Agree with the Terms & Conditions</label> <a href="/pages/policies" target="_blank" class="tos_icon">[View Terms]</a></p> <button type="submit" class="action_button add_to_cart">

 

Could these be conflicting?

Userlevel 1
Badge

I got help today on this. For Turbo theme you need to remove the . before add_to_cart

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

Now the Add to cart Trigger is functioning. Sweet

Userlevel 6
Badge +5

Hi @jlytle

Thanks so much for coming back to the Community and sharing your solution for this! So glad to hear that you were able to figure it out and really appreciate you spreading the word on the Community! I know many others will find this useful too. 

Appreciate you, and thanks for being a part of the forum! 
-Cass. 

Hello ! I have the same problem, but the code don’t work…

Anyone have a solution? :D

Thanks a lot for your help,

Best,

Thomas

the posted solution does not work for me either.. would love if this is reopened or if Klaviyo can give us the correct answer. Turbo is one of the most popular shopify themes out there...

Userlevel 5
Badge +4

Hi @Gollum & @edgeecom,

Thank you both for your follow-up replies. I’ll provide as much clarity as I can here.

Ultimately, in order for the Added to Cart metric to work, it requires the id or class of the button to match the id or class of the Added to Cart snippet (called out in Klaviyo’s Added to Cart for Shopify documentation). This may not always be resolved by removing the period unless that is what is causing the discrepancy between the two snippets of code. Using the example of a button that uses a class, if you navigate to your website and inspect the page:

 

Compare your button code

AddCart2.png

With the contents of the code snippet


AddCart7.png

If these two don't match, change the code snippet to reflect the class or id information contained in the button.

For example, this code snippet has been modified to reflect the class value product-form__cart-submit 

AddCart8.png

Also important to note is that query selectors for buttons with multiple classes need to have those classes separated by a period. For example, in this code snippet, the classes 'btn', 'btn--full', and 'add-to-cart' are all separated by a period:

mceclip0.png

 

Assuming all of the above has been done correctly, also note that the Added to Cart event will only tracked for browsers that have already been cookied (self-identified) by Klaviyo. 

A browser is self-identified only after somebody has done at least one of the following: filled out a Klaviyo sign-up form, previously clicked through a Klaviyo email, self-identified using a UTM parameter. Only after one of these actions are taken and a user subsequently adds an item to their cart, will an Added to Cart metric appear under Analytics > Metrics in Klaviyo.

Finally, you must already have Viewed Product tracking installed in order for the Added to Cart event to function. This is because Added to Cart “calls upon” the Viewed Product snippet to recognize when someone has added an item to their cart. Also, the Viewed Product snippet must be installed above the Added to Cart snippet in the code for your website.

That was a lot of information - to summarize:

  1. Ensure you have a match between the id or class of the button and the id or class of the Added to Cart snippet
  2. If there are multiple classes being used, ensure all classes are separated by a period
  3. Ensure that you have Viewed Product tracking installed above the Added to Cart snippet in the code
  4. In order to track an Added to Cart event, the browser adding the item must have already been cookied or self-identified i.e. through previously filled out a Klaviyo form, clicking through a Klaviyo email or self-identified using a UTM parameter
  5. Once all of the above conditions are met, you can test the Added to Cart functionality by adding an item to your cart from a cookied browser

Thanks, and have a great day.

Reply