Hey there everyone I have a tricky question which Idk why with this implementation I created is not the tracking events and not being submitted even I'm identified
- This is a shopify implementation
- My shopify store is integrated with my Klaviyo account, which means that, Klaviyo Object Script should be already initialized
So let'scheck the structure code, taking in mind there would 2 scenaries one with the script already initializated and the other from scratch.
first we Init
<script type="text/javascript" async src="https://static.klaviyo.com/onsite/js/klaviyo.js?company_id=MyKey123"></script>
second we Init Object
<script>
!function(){if(!window.klaviyo){window._klOnsite=window._klOnsite|||];try{window.klaviyo=new Proxy({},{get:function(n,i){return"push"===i?function(){var n;(n=window._klOnsite).push.apply(n,arguments)}:function(){for(var n=arguments.length,o=new Array(n),w=0;w<n;w++)o)w]=argumentstw];var t="function"==typeof o o.length-1]?o.pop():void 0,e=new Promise((function(n){window._klOnsite.push(hi].concat(o,ofunction(i){t&&t(i),n(i)}]))}));return e}}})}catch(n){window.klaviyo=window.klaviyo|||],window.klaviyo.push=function(){var n;(n=window._klOnsite).push.apply(n,arguments)}}}}();
</script>
Third custom code:
<script>
window.addEventListener('load', async function() {
let klaviyo = window.klaviyo || ];
function identifyUser() {
let email = {{ customer.email | json }};
console.log('Identifying user with email:', email);
if (email) {
const response = klaviyo.identify({ email: email });
console.log('Response:', response);
console.log('User identified');
} else {
console.log('No email found for identification');
}
}
async function addedToCart() {
fetch(`${window.location.origin}/cart.js`)
.then(res => res.json())
.then(data => {
let 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
};
console.log('Cart data:', cart);
const response = klaviyo.track('Added to Cart', cart);
console.log('Added to Cart event tracked');
})
.catch(error => console.error('Error fetching cart data:', error));
}
(function(ns, fetch) {
ns.fetch = function() {
const response = fetch.apply(this, arguments);
response.then(res => {
console.log('Fetch response URL:', res.url);
if (res.url.includes('/cart/add.js')) {
console.log('Cart add detected');
addedToCart();
}
});
return response;
};
})(window, window.fetch);
let atcButtons = document.querySelectorAll("formfaction*='/cart/add'] buttonttype='submit']");
for (let i = 0; i < atcButtons.length; i++) {
atcButtonsti].addEventListener("click", function() {
console.log('Add to Cart button clicked');
identifyUser();
addedToCart();
});
}
let classname = document.getElementsByClassName("add-to-cart__button");
for (let i = 0; i < classname.length; i++) {
classnameni].addEventListener('click', function(){
console.log('Add to Cart button clicked (class)');
identifyUser();
addedToCart();
}, false);
}
console.log('Identifying user on page load');
identifyUser();
});
</script>
- basically what it does is identify if the user is logged and make Klaviyo.indetify
- then if the user submit or press on add to cart ( to handle cases where adding to cart from other places it) wil catch the add.js and retrieve its data and submit it as properties on Klaviyo.track event
So the code works as expected except that is not sending data to Klaviyo
https://freeimage.host/i/JpegTVs
Just to confirm that my account works, I create a private key and use postman to make a post to Klaviyo event endpoint which is showing there called “Added to Cart”
My tries:
I had tried with
- using. “?utm_email=testing.email@gmail.com”
- removed load new object (just script and custom code)
- removed script and load object (just custom code)
- using _learnq object previous Klaviyo object
- all of the above with _learnq
- tried with examples of :
- tested on local shopify env and also on a Production Theme Preview
None of them works…
the code is based on Added to Cart Klaviyo Example which is also using _learnq
https://help.klaviyo.com/hc/en-us/articles/115001396711