Hi Klaviyo team!
i was setting up the added to cart event on one of my stores and when i add the snippet code in product.liquid with my ID, the added to cart doesn’t show in my metrics!
i tried adding a product to cart but nothing happened
Best answer by DovView original
The snipped you’ve used looks OK, but I’m not sure about the ID you’ve used in the snippet. Can you please provide a screenshot of the HTML from your “add to cart” button in Shopify? Maybe you’ve used the wrong ID.
Let’s start with that and see if we’ll find a way to troubleshoot this.
here is a screenshot of my ATC button HTML
@Chakiry - are you familiar with using console logging?
One useful approach to testing or debugging things like this is adding some logging to the script to narrow down possible causes, or locate the specific portion of the code that is not working as expected. In this case, for example, it could be helpful to add a console.log statement above where you reference _learnq… This would effectively make the script say, “Listen for the click of a button with this ID and when that is observed, first log some text to the console, then make the track call to Klaviyo.”
If you add this code and then test it - and watch the dev console to see if this logs out - you’ll be able to determine if your event listener is working as expected. If the statement does log, it suggests that the event listener is properly configured. And if that’s the case, the most likely reason for the track call not going through is that you are trying to make that call, but without having a known identity to Klaviyo. In other words, the code could be perfectly fine, but if Klaviyo’s cookie doesn’t know who you are, that track call won’t get sent (by design).
If you find yourself in this scenario, I recommend using this approach to identify yourself and then retry. Specifically, you can add a param to the URL that says “utm_email=” followed by your email. That will cause you to get cookied and then I would expect your track call to go through as expected (provided that your event listener is working, as demonstrated by the console logging mentioned above).
Phew, that was long, but hopefully helpful!
Thank you for sharing this information with the Klaviyo Community.
In addition to taking into consideration the points made by
@mkprkr, you will have to add a discrete item to your cart after you have self-identified your browser to log an Added to Cart event. And, as @mkprkr pointed out, self-identifying the browser can be achieved by following the instructions here. Only after this step is done, will an Added to Cart metric appear under Analytics > Metrics in Klaviyo after you add an item to your cart.
Also, you must already have Viewed Product tracking installed in order for the Added to Cart event to appear and function . The Viewed Product snippet must be installed above the Added to Cart snippet in the code for your website. We have more details in our article on this topic here.
Thanks, and have a great day.
Your event listener will function as intended if you add this code, test it, and then keep an eye on the dev console to see if this logs out. If the statement really logs something, it can mean that the event listener is set up correctly. If so, the fact that you are attempting to make the track call without first providing Klaviyo with your known identify is the most likely cause of it failing.