But when we check Klaviyo it never shows up. In the past when we created new tracks like this it worked and would just start populating data but for some reason it doesn’t this time around.
Kind Regards, Ian Hogers
Best answer by cbarley
Hey @IanHogers , my bad! I misread your original post.
From what it looks like, this is an issue with the _learnq function fully loading after the code to push the Viewed Items to Klaviyo gets called. Oddly enough, the next line is able to run, but this is definitely some weird DOM timing issue. Here’s what I get when I put a debugger on the line after you call:
This just means that _learnq is being initialized as an empty array and that klaviyo.js hasn’t loaded fully when this function is called.
All I did to fix the issue was:
Moved the declaration of _learnq outside of the $document.ready function to the top of the script above the line var viewed_items_list = []
swapped out your $(document).ready(function(){ }) with window.onload = function(){}
These two things seemed to fix the _learnq timing issue as with those changes above, we get the following in the console:
You should be able to see my profile with the Viewed Multiple Products event on it now (connor+kltest@gmail.com). Once you make the two changes I mentioned above, you should be good to go!
Before you use _learnq.push, you need to define _learnq as follows:
var _learnq = _learnq || [];
If you add the above and the user has been cookied or identified, the event should show up in your Klaviyo account. You can learn more about Klaviyo’s APIs here.
Do you happen to have a URL link to share that I could take a look at to debug?
One other thing to note is that Klaviyo doesn’t track anonymous behavior, so if you are not identified on the site, the activity can’t be tracked back to a profile. Here is a good article about who Klaviyo tracks. We basically need one of 3 things to happen in order to properly identify a user on the frontend to track data to a profile:
somebody fills out a Klaviyo form
somebody clicks on a Klaviyo email that goes to your site with the klaviyo.js tracking code on it
javascript written to execute and identify somebody via our javascript API fires to track somebody’s email address
You can always check to see if you are currently identified by popping open the console and typing:
_learnq.identify()
if $email shows up, then you’re successfully identified. If just referrer info shows up, you’re not. A quick way to identify yourself would then be to simply add a URL parameter to the page you’re on (as long as it has klaviyo.js on the page somewhere) and add in https://website.com?utm_email=youremail@domain.com and hit Return to refresh.
Hopefully this helps. If the steps to identify yourself don’t work, please feel free to shoot over the link of the page you’re working on so I can take a look (via DM or on here, wherever you’re comfortable)
Yep, so my initial hunch was correct, you have to be identified by the browser in order to have this event show up on a profile’s timeline in Klaviyo. Here’s what I did to test this:
Hey @IanHogers , my bad! I misread your original post.
From what it looks like, this is an issue with the _learnq function fully loading after the code to push the Viewed Items to Klaviyo gets called. Oddly enough, the next line is able to run, but this is definitely some weird DOM timing issue. Here’s what I get when I put a debugger on the line after you call:
This just means that _learnq is being initialized as an empty array and that klaviyo.js hasn’t loaded fully when this function is called.
All I did to fix the issue was:
Moved the declaration of _learnq outside of the $document.ready function to the top of the script above the line var viewed_items_list = []
swapped out your $(document).ready(function(){ }) with window.onload = function(){}
These two things seemed to fix the _learnq timing issue as with those changes above, we get the following in the console:
You should be able to see my profile with the Viewed Multiple Products event on it now (connor+kltest@gmail.com). Once you make the two changes I mentioned above, you should be good to go!