HI, I am trying to get the Total Opens, Unique Opens and Total Clicks against my campaigns.
I am able to get the Campaign Information using the following code,
import requests
import pandas as pd
url = "https://a.klaviyo.com/api/campaigns/?filter=equals%28messages.channel%2C%27sms%27%29"
headers = {
"accept": "application/json",
"revision": "2023-12-15",
"Authorization": f"Klaviyo-API-Key {API_KEY}"
}
response = requests.get(url, headers=headers)
campaigns = response.json()
data = campaigns['data']
transformed_data = [{
'id': item['id'],
'Name': item['attributes']['name'],
'Status': item['attributes']['status'],
'Send Date': item['attributes']['send_time']
} for item in data]
campaigns_df = pd.DataFrame(transformed_data)
What I need now is the Total Clicks, Unique Opens and Total Opens against these campaigns over a certain period of time. I have been trying to use query metric aggregates endpoint but its not helping me at all.
What I have tried so far is the following,
url = "https://a.klaviyo.com/api/metric-aggregates/"
metric_id = "UeDJsq"
payload = {
"data": {
"type": "metric-aggregate",
"attributes": {
"measurements": [
"count"
],
"filter": [
"greater-or-equal(datetime,2023-01-01)",
"less-than(datetime,2023-12-31)"
],
"by": ["$attributed_message"],
"interval": "month",
"timezone": "US/Eastern",
"metric_id": metric_id
}
}
}
headers = {
"accept": "application/json",
"revision": "2023-12-15",
"content-type": "application/json",
"Authorization": f"Klaviyo-API-Key {API_KEY}"
}
response = requests.post(url, json=payload, headers=headers)
data = response.json()
The above endpoint returns me nothing.
I also have tried setting the “by” to,
"by": ["Campaign Name"],
But it then returns me either no results against this or campaign names which do not match with the campaigns data I have created above using Get Campaigns End point.
I then tried setting it to $message,
"by": ["$message"],
This then returns me the results but the IDs are of messages, which I can not relate with my Campaigns through any endpoint. So, again, this doesn’t return me the required result
How do I get the given metrics against each Campaign?