Skip to main content
Solved

API - Trying to retrieve all the Events using the metric timeline


Forum|alt.badge.img+2

Im trying to paginate through the results from the  API call 

https://a.klaviyo.com/api/v1/metric/******/timeline?api_key=******

This call works fine and I take the “next” value at the end of the results above and run it, but it does not continue on in a descending manner.  A majority of the values are repeated even if I continue to use the Next value that is generated?

https://a.klaviyo.com/api/v1/metric/*****/timeline?next=1654608790-*****&api_key=****** 

The “next” result becomes 1654608813 which is a unix timestamp that is higher then the previous next.  

Am I misunderstanding the process?

Best answer by JeffV.klaviyo

Hi all, Jeff here with Klaviyo Success Engineering. I was the individual who Alex connected with to resolve this issue. Wanted to provide the resolution here for visibility purposes in case anyone else hits this issue.

In our API endpoint to get events for a specific metric (https://developers.klaviyo.com/en/reference/metric-timeline), we allow for users to set a batch size that determines the numbers of events to return in a given batch (max is 100). If an API call has more events than your batch size, we provide you with a pagination token obtained from the ‘next’ attribute of a prior API call. This pagination token can be used as the value for the ‘since’ key/value pair in the parameters of the call and allows you to pick up where you left off in the previous API call.

You can learn more about this by reading the description of the ‘since’ parameter in the documentation for this endpoint here: https://developers.klaviyo.com/en/reference/metric-timeline

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

4 replies

alex.hong
Forum|alt.badge.img+58
  • Klaviyo Alum
  • 1552 replies
  • June 7, 2022

Hi there @dramlakh,

Welcome to the Community. Thanks for sharing a question regarding API and hope I can help.

To start, one potential solution to this issue that I've seen in the past is to include the 'sort=asc' parameter to all of the requests.  Including the explicit 'sort' parameter may resolve the issue by itself.  The optional parameters are detailed in our API documentation.

The issue might also have to do with the timestamp/event id that these events were submitted to Klaviyo with on your end with the API. I have seen from other community members in the past that after making a request with metric A and then searching the results for a value, they found that just about every timestamp and "$event_id" for each event is the same value. Since all these events were submitted to Klaviyo with the same timestamp and event_id, the API isn't able to handle the events properly and so we're seeing this loop happen.

One potential solution is to use the uuid of the first (or last) event object, instead of the 'next' property defined in the response. Making this request could return  the next batch of events with a new 'next' attribute listed.  I believe that you can use this process to avoid the loop that we were seeing before.  You may want to use the uuid of the last event listed instead of the first event, it depends on whether you're using sort=asc or sort=desc.

 

Let me know if that helped,

Alex


Forum|alt.badge.img+2
  • Author
  • Contributor I
  • 1 reply
  • June 7, 2022

Hi Alex,

So ran the following:

https://a.klaviyo.com/api/v1/metric/*****/timeline?since=1650340800&count=20&sort=asc&api_key*****

from the results from this above call I used the last uuid in two different attempts

  1. https://a.klaviyo.com/api/v1/metric/SRSBLw/timeline?uuid=**********&count=20&sort=asc&api_key=*********
  2. https://a.klaviyo.com/api/v1/metric/SRSBLw/timeline?next=**********&count=20&sort=asc&api_key=*********

It has to be something with the pagination, as its the same results when I use 10 or 20 for the count, you would thing that if by chance their was only 100 results I would be able to search through the 100 results in smaller batches. 

Thanks,
Darren 


alex.hong
Forum|alt.badge.img+58
  • Klaviyo Alum
  • 1552 replies
  • June 7, 2022

Hi @dramlakh,

Going to discuss with our developers and see what might be going on.

 

Thanks,

Alex


JeffV.klaviyo
Klaviyo Employee
Forum|alt.badge.img+4
  • Klaviyo Employee
  • 6 replies
  • Answer
  • June 8, 2022

Hi all, Jeff here with Klaviyo Success Engineering. I was the individual who Alex connected with to resolve this issue. Wanted to provide the resolution here for visibility purposes in case anyone else hits this issue.

In our API endpoint to get events for a specific metric (https://developers.klaviyo.com/en/reference/metric-timeline), we allow for users to set a batch size that determines the numbers of events to return in a given batch (max is 100). If an API call has more events than your batch size, we provide you with a pagination token obtained from the ‘next’ attribute of a prior API call. This pagination token can be used as the value for the ‘since’ key/value pair in the parameters of the call and allows you to pick up where you left off in the previous API call.

You can learn more about this by reading the description of the ‘since’ parameter in the documentation for this endpoint here: https://developers.klaviyo.com/en/reference/metric-timeline