Skip to main content
Solved

New tracking for _learnq not showing up


IanHogers
Contributor IV
Forum|alt.badge.img+2

Hi, 

We recently added Wishlist tracking to our website with the help of @elisegaines 

Now we’re trying to a similar thing for a different metric, but the metric is not showing up in klaviyo.

Our code is as follows:
(viewed_items_data is referencing an object with properties: $value and Items )

_learnq.push(['track', 'Viewed Multiple Products', viewed_items_data]);

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:

_learnq.push(['track', 'Viewed Multiple Products', viewed_items_construction]);

 

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:

  1. Moved the declaration of _learnq outside of the $document.ready function to the top of the script above the line var viewed_items_list = []
  2. 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!

View original
Did this topic or the replies in the thread help you find an answer to your question?

10 replies

caroline
Forum|alt.badge.img+8
  • Klaviyo Alum
  • 215 replies
  • March 16, 2021

Hi @IanHogers,

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.

Best,

Caroline


IanHogers
Contributor IV
Forum|alt.badge.img+2
  • Author
  • Contributor IV
  • 9 replies
  • March 18, 2021

Hi @caroline , 

Thanks for your reply, We’ve got this already in there.
Please see our snippet:

 


let viewed_items_list = [];
var viewed_item = {
  Name: {{ product.title|json }},
  ProductID: {{ product.id|json }},
  Categories: {{ product.collections|map:'title'|json }},
  ImageURL: "https:{{ product.featured_image.src|img_url:'grande' }}",
  URL: "{{ shop.secure_url }}{{ product.url }}",
  Price: {{ product.price|money_without_trailing_zeros|strip_html|json }}
};

$(document).ready(function(){

    var _learnq = _learnq || [];


    pushViewedItems();


    function pushViewedItems() {
      /**
       * Some functionality here for gathering data about the viewed product
       * ... code
       */
      let viewed_items_construction = {
        "$value": viewed_list_value, // Total value of all the items in the stored_viewed_items array
        "Items": stored_viewed_items // stored_viewed_items is an array with [viewed_item] values
      }


      // Doesn't work
      _learnq.push(['track', 'Viewed Multiple Products', viewed_items_construction]);

      // Works
      _learnq.push(['track', 'Viewed Product', viewed_item]);

    }

  })

So the “Viewed Product” track push does work but the “Viewed Multiple Products” does not

Kind Regards,
Ian Hogers


cbarley
Klaviyo Employee
Forum|alt.badge.img+5
  • Klaviyo Employee
  • 18 replies
  • March 25, 2021

Hey @IanHogers ,

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:

  1. somebody fills out a Klaviyo form
  2. somebody clicks on a Klaviyo email that goes to your site with the klaviyo.js tracking code on it
  3. 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)


IanHogers
Contributor IV
Forum|alt.badge.img+2
  • Author
  • Contributor IV
  • 9 replies
  • March 26, 2021

Hi Connor,

Thanks for taking the time to reply,

I’m definitely Identified.

Please see the following URL: https://canr46qfozzw1157-1828937.shopifypreview.com/collections/knitted-ties/products/dark-green-pointed-knitted-tie

This is our theme preview URL.

Please see the code snippet in the element inspector, search for

pushViewedItems()


You can see at the end of the snippet: “Viewed Product” and “Viewed Multiple Products”

Thanks for helping out.

Kind Regards,
Ian Hogers



 


cbarley
Klaviyo Employee
Forum|alt.badge.img+5
  • Klaviyo Employee
  • 18 replies
  • March 26, 2021

Hi Ian,

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:

The snippet appears to be working, you likely just weren’t identified by the browser when you were testing:

 

To check if you’re identified you can type:

_learnq.identify()

into the console when you’re on the site and if you don’t see $email returned, you’ll know that you’re not identified. 


IanHogers
Contributor IV
Forum|alt.badge.img+2
  • Author
  • Contributor IV
  • 9 replies
  • March 29, 2021

Hi Connor, 

Thanks again for the reply,

The issue is not happening with the add to wishlist but with “Viewed Multiple Products”.

It’s just not showing up in klaviyo, “Viewed Product” works, but “Viewed Multiple Products” doesn’t.

Kind Regards,
Ian


cbarley
Klaviyo Employee
Forum|alt.badge.img+5
  • Klaviyo Employee
  • 18 replies
  • Answer
  • March 29, 2021

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:

_learnq.push(['track', 'Viewed Multiple Products', viewed_items_construction]);

 

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:

  1. Moved the declaration of _learnq outside of the $document.ready function to the top of the script above the line var viewed_items_list = []
  2. 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!


IanHogers
Contributor IV
Forum|alt.badge.img+2
  • Author
  • Contributor IV
  • 9 replies
  • March 29, 2021

Hey @cbarley ! That’s it!

silly HTML and JavaScript!

Thanks heaps. Can you mark that last reply as the solution instead? thanks!

 


cbarley
Klaviyo Employee
Forum|alt.badge.img+5
  • Klaviyo Employee
  • 18 replies
  • March 29, 2021

@IanHogers will do! and no problem, happy to help :relaxed:


Forum|alt.badge.img+1
  • Contributor I
  • 1 reply
  • March 7, 2023

My personal experience as of 3/7/2023 is that using

_learnq.identify()

after using ?utm_email=(email address) did not return a field $email and tracking still worked for me. 

Just thought I’d share my experience here in case someone else experienced the same thing