Skip to main content
Problem Solver I
June 3, 2024
Solved

how i can retrive campaign id by using aggregate

  • June 3, 2024
  • 12 replies
  • 308 views

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',
},
},
};

 

    This topic has been closed for replies.
    Best answer by saulblum

    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

    12 replies

    Klaviyo Employee
    June 3, 2024

    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.

    ronen takAuthor
    Problem Solver I
    June 5, 2024

    Hy saulblum

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

    saulblumAnswer
    Klaviyo Employee
    June 5, 2024

    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

    Contributor I
    June 20, 2024

    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.

    Kim Strauch
    Klaviyo Employee
    Klaviyo Employee
    June 21, 2024

    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! 

    Kim Strauch
    Klaviyo Employee
    Klaviyo Employee
    June 21, 2024

    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: ['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. 

    ronen takAuthor
    Problem Solver I
    June 24, 2024

    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": [
    "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": [
    {
    "dimensions": [
    ""
    ],
    "measurements": {
    "unique": [
    0.0,
    0.0,
    0.0,
    0.0,
    13.0,
    6.0
    ],
    "count": [
    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

     

    Kim Strauch
    Klaviyo Employee
    Klaviyo Employee
    June 24, 2024

    @ronen tak  so in the case of clicked/opened/received email/sms metrics for this endpoint, you’ll want to use "by": ["$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.

    ronen takAuthor
    Problem Solver I
    June 25, 2024

    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.
    Kim Strauch
    Klaviyo Employee
    Klaviyo Employee
    June 26, 2024

    @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