Skip to main content
Solved

klaviyo api - query metric aggregates by list / segment


Forum|alt.badge.img+1

Question: is it possible to pull aggregated metric (e.g. “Opened Email”, etc) data by list / segment from the https://a.klaviyo.com/api/metric-aggregates/ endpoint? If not, what are my options? One option I can think of is to pull the raw events and associate them with the lists but that seems quite a lot of work.

I am asking because I was able to do that segmentation in the reporting interface.

 

Context:

I was trying to pull aggregated metric data from the https://a.klaviyo.com/api/metric-aggregates/
 endpoint.

With the following payload,

{  
    "data": {  
         "type": "metric-aggregate",  
         "attributes": {  
              "measurements": [  
                   "count"  
              ],  
              "filter": [
                  "greater-or-equal(datetime,2022-06-06 12:29:59+00:00)",  
                  "less-than(datetime,2023-06-05 18:46:29+00:00)",
                  'any($message,["MESSAGE_ID1","MESSAGE_ID2"])'
              ],
              "by": ["$message"],
              "interval": "month",  
              "timezone": "US/Eastern",  
              "metric_id": "METRIC_ID"
         }  
    }  
}

I was able to get a response for the “Opened Email” metric.

{'data': {'type': 'metric-aggregate',
  'id': 'xxx',
  'attributes': {'dates': ['2022-06-01T04:00:00+00:00',
    '2022-07-01T04:00:00+00:00',
    '2022-08-01T04:00:00+00:00',
    '2022-09-01T04:00:00+00:00',
    '2022-10-01T04:00:00+00:00',
    '2022-11-01T04:00:00+00:00',
    '2022-12-01T05:00:00+00:00',
    '2023-01-01T05:00:00+00:00',
    '2023-02-01T05:00:00+00:00',
    '2023-03-01T05:00:00+00:00',
    '2023-04-01T04:00:00+00:00',
    '2023-05-01T04:00:00+00:00',
    '2023-06-01T04:00:00+00:00'],
   'data': [{'dimensions': ['MESSAGE_ID1'],
     'measurements': {'count': [0.0,
       0.0,
       0.0,
       0.0,
       0.0,
       0.0,
       0.0,
       0.0,
       0.0,
       2378.0,
       347.0,
       21.0,
       0.0]}},
    {'dimensions': ['MESSAGE_ID2'],
     'measurements': {'count': [0.0,
       0.0,
       0.0,
       0.0,
       0.0,
       0.0,
       0.0,
       0.0,
       0.0,
       0.0,
       599.0,
       31.0,
       0.0]}}]},
  'links': {'self': 'https://a.klaviyo.com/api/metric-aggregates/'}},
 'links': {'self': 'https://a.klaviyo.com/api/metric-aggregates/',
  'next': None,
  'prev': None}}

However, when I try to further segment the response by list / segment with the following payload (adding the additional “by” field parameter), I was not able to get the intended result. No further segmentation is given.

{  
    "data": {  
         "type": "metric-aggregate",  
         "attributes": {  
              "measurements": [  
                   "count"  
              ],  
              "filter": [
                  "greater-or-equal(datetime,2022-06-06 12:29:59+00:00)",  
                  "less-than(datetime,2023-06-05 18:46:29+00:00)",
                  'any($message,["MESSAGE_ID1","MESSAGE_ID2"])'
              ],
              "by": ["$message", "List"],
              "interval": "month",  
              "timezone": "US/Eastern",  
              "metric_id": "METRIC_ID"
         }  
    }  
}

Best answer by Brian Turcotte

Hi @william_arctic!
 

Per our documentation on this endpoint, only 1 filter can be used per query. In the call you supplied, there are two: $message and List:

 

Also, as of now you can only filter by List, not segment. Here is a list of the supported metrics and attributes by which this endpoint can be filtered:

 

Finally, if you haven’t seen it, here’s the complete, expanded guide on how to use this endpoint effectively:

 

I hope this helps, and thanks for using the Community!

- Brian

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

2 replies

Brian Turcotte
Forum|alt.badge.img+37

Hi @william_arctic!

 

I’m going to check on this with Engineering for you, and then I will update the thread as soon as possible.

 

Thanks, 
Brian
 


Brian Turcotte
Forum|alt.badge.img+37

Hi @william_arctic!
 

Per our documentation on this endpoint, only 1 filter can be used per query. In the call you supplied, there are two: $message and List:

 

Also, as of now you can only filter by List, not segment. Here is a list of the supported metrics and attributes by which this endpoint can be filtered:

 

Finally, if you haven’t seen it, here’s the complete, expanded guide on how to use this endpoint effectively:

 

I hope this helps, and thanks for using the Community!

- Brian