Klaviyo 'Added to Cart' Event - Snippet Help

simcmahon
New Member
1 0 0

Hi All,

 

I have tried to add the 'added to cart' snippet to my site code to track add to cart events but it does not seem to be working - my code does not have an id, but a class, so I tried to use the secondary snippet in the following article:

 

https://help.klaviyo.com/hc/en-us/articles/115001396711-Create-a-Custom-Added-to-Cart-Event-for-Shop...

 

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

 

My code for the add to cart button the shopify prestige theme is:

 

<button type="submit" class="ProductForm__AddToCart Button Button--primary Button--full" data-action="add-to-cart"><span>Add to cart</span></button>

 

I'm wondering what change i need to make to get the snippet to work? I've tried using various bits from the class field in the code above but I can not get it working.

 

Can anyone help please?

 

Thanks

Si

0 Likes

This should work, assuming you already have the Viewed Product tracking set up correctly.

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