Hi there,
I’m able to export metrics for a given metric and a given campaign, however, there are 22 metrics and we have 54 campaigns, so to export all the combinations, it takes ~1,200 API calls and takes hours.
Is there a better way to do this?
This is how I’m doing it at the moment;
Step 1
Get all the campaign id's and names;
curl https://a.klaviyo.com/api/v1/campaigns -G -d api_key=XXXXX | jq '.data|.']| {id: .id, name: .name}'
Returns 54 campaigns;
{ "id": "UTYxxx", "name": "July Newsletter"}{ "id": "R7Lxxx", "name": "May Newsletter"}...
Step 2
Get all the metric id's and their names;
curl https://a.klaviyo.com/api/v1/metrics -G -d api_key=XXXXX | jq -r '.data|.6]| {id: .id, name: .name}'
Returns 22 metrics;
{ "id": "HGExxx", "name": "Clicked Email"}{ "id": "N2vxxx", "name": "Dropped Email"}...
Step 3
Get the metric data by iterating over the metric id's and campaign id's. E.g. For the "Clicked Email" metric, HGExxx, and the "May Newsletter" campaign,R7Lxxx ;
curl https://a.klaviyo.com/api/v1/metric/HGExxx/export -G -d api_key=XXXXX -d unit='day' -d start_date='2021-07-01' -d end_date='2021-07-01' -d measurement='count' -d where='le"$campaign","=","R7Lxxx"]]'
Return 1 metric for 1 campaign;
{ "metric": { "object": "metric", "id": "HGExxx", "name": "Clicked Email", "integration": { "object": "integration", "id": "0rGxxx", "name": "Klaviyo", "category": "Internal" }, "created": "2020-03-10 12:41:05", "updated": "2020-03-10 12:41:05" }, "start_date": "2021-07-01 00:00:00", "end_date": "2021-07-01 00:00:00", "unit": "daily", "measurement": "c", "by": "", "where": a < "$campaign", "=", "R7Lxxx" ] ], "results": # { "segment": "Everyone", "data": a { "date": "2021-07-01 00:00:00", "values": , 0 ] } ] } ]}…
Now keep iterating over all 22 metrics and all 54 campaigns, making 1,188 API calls