Skip to main content
Solved

trackViewedItem throws error Cannot read properties of null (reading 'LastViewedDate')

  • September 3, 2024
  • 3 replies
  • 57 views

Forum|alt.badge.img+2

I’m in the process of upgrading to the new API from the old version, which we’ve been using for years.

After each “Viewed Product” event, we’d been calling trackViewedItem, with the product information structured slightly differently, so that it would register in the user’s recently viewed items list.

With the new API, calls to trackViewedItem are throwing an error.  I’m using syntax similar to the example on https://developers.klaviyo.com/en/docs/guide_to_integrating_a_platform_without_a_pre_built_klaviyo_integration#viewed-product-tracking-snippet

I’ve isolated the failing call from my app and am invoking it directly in the browser console. Here’s an example:

 

var item = {
 Title: "SampleItem",  
 ItemId: 47,  
 Categories: [ "Things" ],  
 ImageUrl: "https://XXXXXXXXXXXXX.net/img/foo.gif",       
 Url: "https://XXXXXXXXXXXXX.net/sample",  
 Metadata: { Brand: "ACME", Price: 99.99 }  
}

klaviyo.trackViewedItem(item);

The error:

fender_analytics.992c5dd9d8732b969bc5.js?cb=1:1 Uncaught (in promise) TypeError: Cannot read properties of null (reading 'LastViewedDate')
    at fender_analytics.992c5dd9d8732b969bc5.js?cb=1:1:22628
    at Array.reduce (<anonymous>)
    at Y.trackViewedItem (fender_analytics.992c5dd9d8732b969bc5.js?cb=1:1:22608)
    at Array.r [as push] (fender_analytics.992c5dd9d8732b969bc5.js?cb=1:1:30334)
    at Object.trackViewedItem (sharedUtils.32fecf14b565ff0981cb.js?cb=1:1:1208)
    at Array.o [as push] (sharedUtils.32fecf14b565ff0981cb.js?cb=1:1:2357)
    at sharedUtils.32fecf14b565ff0981cb.js?cb=1:1:2072
    at new Promise (<anonymous>)
    at Proxy.<anonymous> (sharedUtils.32fecf14b565ff0981cb.js?cb=1:1:2038)
    at <anonymous>:1:9

 

k.push([“trackViewedItem”, item]) gives exactly the same error.

 

In the Klaviyo console, on the profile of the user I’m testing with, I see the “Viewed Product” events just fine, but the “Recently viewed products” section is empty. We were successfully using this feature before, with the old API, as I can see by browsing accounts recently active on the production server.

Best answer by saulblum

Hi! Could you try adding a LastViewedDate property to the item object? Set it as a UNIX time stamp. It’s possible our guide’s example is wrong.

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

3 replies

Forum|alt.badge.img+7
  • Klaviyo Employee
  • 169 replies
  • Answer
  • September 4, 2024

Hi! Could you try adding a LastViewedDate property to the item object? Set it as a UNIX time stamp. It’s possible our guide’s example is wrong.


Forum|alt.badge.img+2
  • Author
  • Contributor I
  • 1 reply
  • September 4, 2024

I tried adding that property yesterday, with the value being a javascript Date object. The result was the same.

I examined the Klaviyo source code where that error occurred. It’s not reading that property from the item object I’m passing in, it’s reading it from something in localStorage (the key name is also obfuscated in your source, so I don’t know what it’s storing there).  Maybe my earliest attempts to use this call resulted in something malformed being written to localStorage… 

I cleared localStorage completely. After that, the trackViewedItem API call works - both with my sample product above, and later with actual products from my catalog, once I enabled that feature and started browsing again - they show up in my user profile with pictures, product name and URL, all the good stuff.

So, I have a workaround for now. If I see this happen again I’ll ask for an investigation. 


Forum|alt.badge.img+7
  • Klaviyo Employee
  • 169 replies
  • September 4, 2024
aagmatt wrote:

So, I have a workaround for now. If I see this happen again I’ll ask for an investigation. 

Glad it’s working. I know we use local storage for anonymous backfill, but that shouldn’t be conflicting with trackViewedItem.