Solved

How to obtain Total Clicks, Total Opens, Unique Opens against the Campaigns using Klaviyo API

  • 7 February 2024
  • 2 replies
  • 93 views

Badge

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? 

icon

Best answer by saulblum 7 February 2024, 16:07

View original

2 replies

Userlevel 4
Badge +7

Hi! There will be a new reporting API in the 2024-02-15 API revision that will let you get all the campaign and flow analytics you can see in the UI (and export to CSV). So if you can wait a week, it’ll make your coding much easier.

As for the above, $attributed_message is generally for events like Placed Order, i.e. to which email/campaign was this order attributed. Events like Opened Email wouldn’t have an attributed message.

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"
]                    

Badge

Hi! There will be a new reporting API in the 2024-02-15 API revision that will let you get all the campaign and flow analytics you can see in the UI (and export to CSV). So if you can wait a week, it’ll make your coding much easier.

As for the above, $attributed_message is generally for events like Placed Order, i.e. to which email/campaign was this order attributed. Events like Opened Email wouldn’t have an attributed message.

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"
]                    

 

Unfortunately, it is not possible for me to wait until 15th of Feb at the moment. 

 

And yes, I have tried the exact payload you mentiond but that didn’t work for me. I was getting Campaign Names but these Campaign names were not the same as the ones I am getting from the Get Campaigns Endpoint. 

Similarly, I was not able to relate the Message ID returned with any of the campaign IDs. I am not sure what am I doing wrong or what’s wrong with the endpoint. Is there anything additional I can share that might help in this? I am happy to provide as much information as possible. 

 

 

Reply