My client has been downloading Klaviyo reports through the UI by hand. She wants to automate this by exporting the data to Google Sheets through the API. I am a dev new to Klaviyo who got was task. The metrics needed are Revenue, Open Rate, Click Rate.
I have been at this for over 5 days. I got through the Get Started Steps. Then I had to select which API to use, etc. etc.
I found a Help Center article How to Export Conversion Data. It seems to cover the subject that I need. However, it gives v1 APIs which are no longer current.
Per this article, my first step was to export all of the campaigns. As the list of campaigns turned out to be multi-page, I had to take care of fetching all the pages. I got through that. I am sharing a Google Apps Script function I ended up with, in case it might help anyone else or be improved.
I also fetched all the metrics available in the account through the /metrics endpoint. I also found a trick, not prominently displayed: "you can find the metric_id in your Klaviyo account by navigating to the page for a given metric. Once on the page for a given metric, you'll find the metric_id in the URL (e.g.,https://www.klaviyo.com/metric/METRIC_ID/metricname)." Maybe this datum would help other readers of "How to export conversion data" article; at least, I was baffled about this at first.
Now I have the following metrics and their IDs:
Opened Email
Opened Push
Clicked Email
Clicked SMS
Received Email
Received Push
Placed Order
Where do I go from here? I need to end up with a Google Sheet that lists these metrics for each campaign. And then I need to be able to update this Google Sheet periodically. And do the same for the flows as well; it's only for simplicity that I am concentrating on campaigns now.
Which API should I use to fetch this data?
If I understand the docs correctly, the new Query Metric Aggregates endpoint would give me the aggregated data over a period. But I need the data aggregated based on a campaign or flow, not based on time. Can it do this?
The description in "How to Export Conversion Data" seems to mean that I have to make an API request to API/metric endpoint for each metric for each campaign. If I have 7 metrics and 100 campaigns that would be 700 requests. This has to be minimized.
Can I supply an array of campaigns in my API request for a metric and receive an array of metric data corresponding to the campaigns? If so, how?
Or vice versa, can I fetch 7 metrics related to one campaign or flow in one API call?
In fetching metrics, can I specify the "measurement"? The "How to Export Conversion Data" article says that the API calls should have measurement='count' or measurement='unique' or measurement='value'. Does this work in the current version of API? I don't see "measurement" in query params.
I can keep on trying to invent a wheel in navigating the docs, but if anyone can share a simple explanation, it would help me a lot, and probably help the future users as well.