Skip to main content

I try to obtain the data with a breakdown by date

and I want to get the campaign ID to

for now, I succeeded received by campaign name, but this is not good enough because I need the campaign ID value that should be single-valued that I can use after that with join to other tables

this the code that I use for now

 
const metric = 'xyz'
const body = {data: {
type: 'metric-aggregate',
attributes: {
measurements: ['count'],
filter: [
`greater-or-equal(datetime,${'2024-06-01'})`,
`less-than(datetime,${'2024-06-03'})`,
],
interval: 'day',
metric_id: metric,
timezone: 'UTC',
},
},
};

 

If you add this to your request —

"by": :

    "$attributed_message"

]

— it’ll group the response data by campaign ID, assuming the metric is something like Placed Order that can be attributed to campaign messages.


Hy saulblum

how can I know which metric is attributed to a campaign?


Go to the metrics page in the UI, e.g. Placed Order, and the six-character ID will be in the URL. Specific events of a metric, e.g. a specific profile who has a Placed Order event, may be attributed to a campaign or flow, per https://help.klaviyo.com/hc/en-us/articles/1260804504250


I am upgrading my apex code in salesforce for the new version of Klaviyo Api. But when I am trying to run the code authorisation is failing for some reasons. When I use this syntax:  req.setHeader('Authorization', 'Klaviyo-API-Key “My Api Key Hardcoded”');,

I am getting the response but i dont want hardcoded value to give here. while trying this syntax i am getting the error 400 :

 req.setHeader('Authorization','klaviyo_api_key '+ defaults.ApiKey__c);.

Can someone please give me a solution to this.Its urgent.


I am upgrading my apex code in salesforce for the new version of Klaviyo Api. But when I am trying to run the code authorisation is failing for some reasons. When I use this syntax:  req.setHeader('Authorization', 'Klaviyo-API-Key “My Api Key Hardcoded”');,

I am getting the response but i dont want hardcoded value to give here. while trying this syntax i am getting the error 400 :

 req.setHeader('Authorization','klaviyo_api_key '+ defaults.ApiKey__c);.

Can someone please give me a solution to this.Its urgent.

@coder, if you’re getting a 401 error, that’d indicate you have an issue in how you’re sending the authorization/private key information. If you’re getting a 400 error that likely indicates an issue with how you’re formatting the request body. Do you mind starting a separate post with more details on your request so we can help out? Thanks! 


Hey @ronen tak, if your goal ultimately is to get stats on these campaigns for each campaign ID you might be better off using the campaign reporting API instead: https://developers.klaviyo.com/en/reference/query_campaign_values


Otherwise, if you’re specifically trying to get placed orders or other metrics aggregated / attributed to various campaigns in a timeframe, you can use the approach @saulblum described above. Using the “by: $attributed_message” group by should group by the relevant campaign ID or flow message ID. If you want to filter out flows, you might do a query like the following 

const body = {data: {
type: 'metric-aggregate',
attributes: {
measurements: u'count'],
filter:
`greater-or-equal(datetime,${'2024-06-01'})`,
`less-than(datetime,${'2024-06-03'})`,
`equals($flow,null)`
],
by: '$attributed_message',
interval: 'day',
metric_id: metric,
timezone: 'UTC',
},
},
};

When you do this, the data that comes back in the response under data.attributes will each have a dimension field that is a campaign ID. 


hello @Kim Strauch 

Is this what I request 

SftY5b = Opened Email

{

"data": {

"type": "metric-aggregate",

"attributes": {

"measurements": "
"unique",
"count"
],

"filter": "
"greater-or-equal(datetime,2024-03-01)",
"less-than(datetime,2024-03-07)",
"equals($flow,null)"
],

"by": ""$attributed_message"],
"interval": "day",
"metric_id": "SftY5b",
"page_size": 500
}
}
}

and I don’t get the campaign ID even though I added "$attributed_message"

this is what I am getting in response 

{
"data": {
"type": "metric-aggregate",
"id": "8246020471467986601",
"attributes": {
"dates": t
"2024-03-01T00:00:00+00:00",
"2024-03-02T00:00:00+00:00",
"2024-03-03T00:00:00+00:00",
"2024-03-04T00:00:00+00:00",
"2024-03-05T00:00:00+00:00",
"2024-03-06T00:00:00+00:00"
],
"data": a
{
"dimensions": o
""
],
"measurements": {
"unique": q
0.0,
0.0,
0.0,
0.0,
13.0,
6.0
],
"count": u
0.0,
0.0,
0.0,
0.0,
27.0,
15.0
]
}
}
]
},
"links": {
"self": "https://a.klaviyo.com/api/metric-aggregates/"
}
},
"links": {
"self": "https://a.klaviyo.com/api/metric-aggregates/",
"next": null,
"prev": null
}
}

What am I supposed to do to get it

 


@ronen tak  so in the case of clicked/opened/received email/sms metrics for this endpoint, you’ll want to use "by": b"$message"]

 

$attributed_message is what you’d want to use in the case of trying to get Placed Order, Active on Site, Added to Cart, events that are attributed to this campaign.

 

If you update your API call above to use $message, you should see campaign IDs under “dimensions”.

 

Given the complexity involved with this endpoint, I’d recommend the Query Campaign Values endpoint instead. To get opens, unique opens and open rate, you’d do something like

 

POST https://a.klaviyo.com/api/campaign-values-reports/

{
"data": {
"type": "campaign-values-report",
"attributes": {
"timeframe": {
"start": "2024-03-01",
"end": "2024-03-07"
},
"conversion_metric_id": "SftY5b",
"statistics":
"opens",
"opens_unique",
"open_rate"
]
}
}
}

 

When you do this, you’ll get a response back where each result is grouped by campaign and will say “campaign_id” so you can clearly see which campaign these opens are associated with.


Hi, @Kim Strauch thank you for the help,

I still have two questions:

  1. How can I identify which metrics are related to the campaign ID and which are not?
  2. This endpoint does not return data broken down by date as the aggregate endpoint does. Therefore, it does not meet my needs.

@ronen tak,
1. I’m not sure what you mean by  “How can I identify which metrics are related to the campaign ID and which are not?”. Do you mean, when should you use $message vs. $attributed_message? If so, for things like email opened/clicked/received/etc. (klaviyo-sourced metrics related to message sending), you should use $message, and for anything else you want to understand that might be attributed to the campaign (e.g. active on site, placed order, etc.) you should use $attributed_message. Hopefully that answers your question.

 
2. Can you share more details on why you want the data broken out (e.g. for clicks) on a day-by-day basis? I understand this use case for flows which are sent to users at all kinds of different times but would be curious to learn more about your use case for this for campaigns 


Regarding your question about my preferences, it's primarily for convenience. Below is an image illustrating how I envision it, with the division by days being particularly useful

 


@ronen tak, thanks for sharing this view. This is helpful!

You could create an output like this for campaigns using the reporting API, where you also call GET /api/campaigns to get the date the campaign was sent to create such a view, for example if you want to know when the campaign was sent and all the stats associated with that campaign in aggregate, vs. having multiple rows per campaign based on the dates users engaged. I suppose it’s a matter of personal preference


Reply