Hi,
I need to figure out a way to get the count of emails opened, I am able to get the Campaigns data with the following code,
import requests
url = "https://a.klaviyo.com/api/campaigns/?filter=equals(messages.channel,'email')"
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)
Now, I want to get the following items against each Campaign,
Opened Email Count
Unique Opens
Click Rates
I understand that I need to use Query End Parameter for this purpose. My understanding is that if we want to group the data by campaign_id, then we use by = [‘$attributed_message’]. So I used the following query end parameter for my purpose,
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()
However, it returned me nothing except the blank dimension (which I was not interested in)
So, then, I used the same code above but changed the by attribute to by = [‘$message’] and it returned me results.
In the dimension, I had “message_id” and in results, I had each month record. However, now I want to relate this information with my initial Campaigns Data. I can’t figure out a way to relate these message IDs with my campaign IDs. Can anyone help me out in this please? My ultimate goal is to find the number of total opens, unique opens and clicks against each campaign.
Thanks.