How to add the event snippet Added to cart event from Klaviyo on Dawn Theme

YordanB
Excursionist
37 1 4

Hello, please help me to add the Added to card event snipper from Klaviyo, so I can send following up emails. I tried everything from their help article but nothing works. Thank you. 

 

 

Replies 4 (4)

BeautifulWeb
Shopify Partner
12 1 6

@YordanBYou need to use following script if you have Mini Cart enabled in Dawn Theme and the product is added to the mini cart instead of redirecting the user to the Checkout Page when "Add to Cart" button is clicked.

<script type="text/javascript">
var _learnq = _learnq || [];
function addedToCart() {
  fetch(`${window.location.origin}/cart.js`)
  .then(res => res.clone().json().then(data => {
    var cart = {
      total_price: data.total_price/100,
      $value: data.total_price/100,
      total_discount: data.total_discount,
      original_total_price: data.original_total_price/100,
      items: data.items
    }
    if (item !== 'undefined') {
      cart = Object.assign(cart, item)
    }
    _learnq.push(['track', 'Added to Cart', cart])
  }))
} 
(function (ns, fetch) {
  ns.fetch = function() {
    const response = fetch.apply(this, arguments);
    response.then(res => {
      if (`${window.location.origin}/cart/add.js`
      	.includes(res.url)) {
        	addedToCart()
      }
    });
    return response
  }
}(window, window.fetch))
$(document).ajaxComplete(function(event, request, settings){
  if(settings.url == "/cart/add.js"){
      addedToCart()
  }
})</script>

You need to edit the Default Product Page Template and add a Custom Liquid block and then add above script in that.

 

BeautifulWeb_1-1677607838521.png

 

After you are done doing that... Test the event by force cooking an email id for which a profile in Klaviyo doesn't exists already by something like this domain.com?utm_email=testemail@emailhost.com (replace domain.com with your domain name and testemail@emailhost.com with your test email id). Add the URL in your browser window and enter URL > Navigate to Product Page > Add Product to Cart.

 

Look for the profile with this email id in Klaviyo and if you see that then the Event Tracking is working.

 

And, finally go back to your Flow, add Add to Cart Tigger and Complete it.

BeautifulWeb_0-1677607127428.png

featherandflame
Visitor
1 0 0

This worked for me! thank you!

BeautifulWeb
Shopify Partner
12 1 6

Great! Please accept the solution to help others!

daniel1491
New Member
10 0 0

Hi, I've been testing and this is still not working on my end. Not sure why. Cheers, Daniel