Solved

I can get Opened Email Count by Messages but how do I get the same for Campaigns

  • 6 February 2024
  • 7 replies
  • 91 views

Badge

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.

icon

Best answer by saulblum 6 February 2024, 20:36

View original

7 replies

Userlevel 4
Badge +7

Hi, there will be a reporting API released this month that will let you query the same analytics via API that you can see in the UI (and export to a CSV). Subscribe to the developer newsletter at https://developers.klaviyo.com/en and you’ll be notified when the new reporting API is available.

Badge

Hi, there will be a reporting API released this month that will let you query the same analytics via API that you can see in the UI (and export to a CSV). Subscribe to the developer newsletter at https://developers.klaviyo.com/en and you’ll be notified when the new reporting API is available.

Hi @saulblum, thanks for the update. I know this new API will be releasing on 15th of this month. However, that is a bit late as I have a deadline fast approaching and I need to finish this up before that.

 

Is there no way to achieve what I am trying to achieve with the current set of APIs? 

Userlevel 4
Badge +7

If you’re trying to map messages back to their campaigns, does this call work?

Badge

If you’re trying to map messages back to their campaigns, does this call work?

This is my understanding too but it’s not working for me unfortunately. 

 

I am using this call to get the Messages count through Query Metric Aggregates Endpoint, 

 

import requests
import json

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": ["$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()

 

My understanding is, it returns the Message_ID in the dimensions. So, I pick any random message_id from the dimension and calls the given API using the following code (a random message_id is picked from the results of the above query), 

 


url = "https://a.klaviyo.com/api/campaign-messages/01HFNS0NEMQNGN7JWFVETGYNV4/"

headers = {
"accept": "application/json",
"revision": "2023-12-15",
"Authorization": f"Klaviyo-API-Key {API_KEY}"
}

response = requests.get(url, headers=headers)

print(response.json())

 

 

But it returns me this, 

 

{'errors': [{'id': 'ff8f86e3-a388-4d5d-bbbe-bde2de2573b3', 'status': 404, 'code': 'not_found', 'title': 'Not found.', 'detail': "A message with id '01HFNS0NEMQNGN7JWFVETGYNV4' does not exist.", 'source': {'pointer': '/data/'}}]}

 

I tried the same for other message_ids too but got the same result. I don’t know why this is not working. 

Userlevel 4
Badge +7

26-char IDs like 01HFNS0NEMQNGN7JWFVETGYNV4 are campaign IDs. Message IDs are usually six characters, e.g. UGKct2, and are generally tied to messages sent from flows.

Badge

26-char IDs like 01HFNS0NEMQNGN7JWFVETGYNV4 are campaign IDs. Message IDs are usually six characters, e.g. UGKct2, and are generally tied to messages sent from flows.

I am using the query above as given. 

 

If I am to get the Total Clicks, Total times the campaign email was opened and unique opens, what should I do? Any ideas on that? 
I have the campaign_id with me and I also know the metric ID for these. 

 

If I use $attributed_message in “by”, it returns me nothing. 

Userlevel 4
Badge +7

If you use "by": ["$message", "Campaign Name"] in the request body, the response will include both the campaign/flow message ID and name, e.g.

"dimensions": [
    "01GNKHCWXZ9Z5TBJK8H484QA1H",
    "Daily Newsletter: 2022-12-31"
]

"dimensions": [
    "01GNRP6AZXHG15PD155AJ964PF",
    "Daily Newsletter: 2023-01-02"
]                    

Is that what you’re looking for? If you make the call for each metric (Opened Email, Clicked Email, etc.) you’ll get the count for each metric for each campaign and flow message in the time window.

Reply