I have many campaigns and I want to summarize the spends by all the states (USA states) and based on from and to dates.

I went through the Campaigns | Docs | Twitter Developer Platform but I dont see any point that can determine the spends based on states.

Which endpoint I need to use in twitter: In the FB I am using <FB_GRAPH_API_BASE_URL>/<ACCOUNT_ID>/insights?access_token=<ACCESS_TOKEN>&fields=spend&breakdowns=region%2Ccountry&level=ad&time_range=%7B%27since%27%3A%272022-05-15%27%2C%27until%27%3A%272022-05-15%27%7D&limit=100&after=OTkZD

Any help is really appreciated.

Hello,

Thanks for reaching out. You may want to use these APIs:

  1. The GET stats/accounts/:account_id/active_entities endpoint. This endpoint helps to retrieve campaigns where the analytics metrics have changed in a given time period

  2. With the entity_ids from the response, you can use the POST stats/jobs/accounts/:account_id and GET stats/jobs/accounts/:account_id endpoint, also known as the Async endpoint which helps to retrieve the relevant analytics data.

For your case, in the POST call, you can specify the metric_groups to be BILLING and the segmentation_type to be METROS/CITIES/REGIONS. For more information on metrics and segmentation, you can refer to this documentation

Note that when segmenting by METROS/CITIES/REGION, the country param needs to be specified in the POST call as well and you can retrieve the value using this endpoint: GET targeting_criteria/locations

Thank you so much @jaredtcy Would it possible to achieve the same using synchronous API call?

Hello,

Unfortunately you can’t do so. For the synchronous API call, it does not allow segmentation and you can only retrieve data that is within 7 days.

You can refer to this documentation for more information on the difference between Asynchronous and Synchronous.

There is a issue doing so, I am not able to get the exact spend totals, Any help is greatly appreciated.

In the UI, Say I want to find the ad spends on 14th June. So I Input date start as 2022-06-14 and end date as 2022-06-14, the total spend it gave me was $215.93

When the same was done by API

https://ads-api.twitter.com/{{version}}/stats/accounts/{{ACC_ID}}/active_entities?end_time=2022-06-15T00:00:00Z&entity=CAMPAIGN&start_time=2022-06-14T00:00:00Z

(In the API I cannot specify the start date same as end date, so I specify from 2022-06-14T00:00:00Z to midnight 2022-06-15T00:00:00Z)

{ "request": { "params": { "account_id": "xxxxxxxxx", "entity": "CAMPAIGN", "start_time": "2022-06-14T00:00:00Z", "end_time": "2022-06-15T00:00:00Z" } }, "data": [ { "entity_id": "huiuj", "activity_start_time": "2022-06-13T12:35:31Z", "activity_end_time": "2022-06-14T23:59:25Z", "placements": [ "ALL_ON_TWITTER" ] }, { "entity_id": "hyac2", "activity_start_time": "2022-06-13T00:25:12Z", "activity_end_time": "2022-06-13T12:18:04Z", "placements": [ "ALL_ON_TWITTER" ] } ] }

https://ads-api.twitter.com/{{version}}/stats/jobs/accounts/{{ACC_ID}}?end_time=2022-06-15T00:00:00Z&entity=CAMPAIGN&entity_ids=hyac2,huiuj,hwbna&granularity=HOUR&metric_groups=BILLING&placement=ALL_ON_TWITTER&start_time=2022-06-14T00:00:00Z&country=96683cc9126741d1&segmentation_type=REGIONS

It is giving me a response as below, Which is totalled to 267.02392700 instead of $215.93

Where I am going wrong? I want to adapt my API request to get the total as 215.93 same like the UI report.

{"data_type":"stats","time_series_length":24,"data":[{"id":"huiuj","id_data":[{"segment":{"segment_name":"Delaware, US","segment_value":"3f5897b87d2bf56c"},"metrics":{"billed_engagements":[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0],"billed_charge_local_micro":[0,0,0,0,0,0,0,0,0,0,0,0,2900000,0,0,2500000,0,0,0,0,0,0,0,0]}},{"segment":{"segment_name":"California, US","segment_value":"fbd6d2f5a4e4a15e"},"metrics":{"billed_engagements":[0,0,0,0,0,0,0,0,0,0,1,1,0,2,2,0,1,0,0,0,0,0,0,1],"billed_charge_local_micro":[0,0,0,0,0,0,0,0,0,0,3200000,2900000,0,5800000,5400000,0,3000000,0,0,0,0,0,0,3000000]}},{"segment":{"segment_name":"Connecticut, US","segment_value":"e86b380cfefcced5"},"metrics":{"billed_engagements":[0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0],"billed_charge_local_micro":[0,0,0,0,0,0,0,0,0,0,3000000,0,0,3000000,0,0,0,0,0,0,0,0,0,0]}},{"segment":{"segment_name":"New Jersey, US","segment_value":"65b4760a2b411e11"},"metrics":{"billed_engagements":[0,0,0,0,1,0,0,0,0,0,0,0,1,2,2,0,1,0,0,0,0,1,0,0],"billed_charge_local_micro":[0,0,0,0,3000000,0,0,0,0,0,0,0,1900,6000000,5900000,0,3200000,0,0,0,0,3200000,0,0]}},{"segment":{"segment_name":"Pennsylvania, US","segment_value":"dd9c503d6c35364b"},"metrics":{"billed_engagements":[1,0,0,0,0,0,0,0,0,0,1,2,3,1,0,0,0,0,0,0,0,0,0,0],"billed_charge_local_micro":[3000000,0,0,0,0,0,0,0,0,0,2867094,4543568,9100000,3200000,0,0,0,0,0,0,0,0,0,0]}},{"segment":{"segment_name":"New York, US","segment_value":"94965b2c45386f87"},"metrics":{"billed_engagements":[1,0,0,0,0,0,0,0,0,1,1,3,3,3,1,0,0,0,0,1,0,0,0,0],"billed_charge_local_micro":[3200000,0,0,0,0,0,0,0,0,3000000,3000000,10751704,10637634,8400000,3000000,0,0,0,0,3200000,0,0,0,0]}},{"segment":{"segment_name":"Nevada, US","segment_value":"d374fb61a20fb74f"},"metrics":{"billed_engagements":[0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"billed_charge_local_micro":[0,0,0,0,0,3000000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}},{"segment":{"segment_name":"Texas, US","segment_value":"e0060cda70f5f341"},"metrics":{"billed_engagements":[1,0,0,0,2,0,0,0,0,0,4,1,1,2,1,0,0,1,0,0,0,1,0,0],"billed_charge_local_micro":[3000000,0,0,0,6400000,0,0,0,0,0,10330789,3200000,2900000,5900000,2900000,0,0,2900000,0,0,0,3000000,0,0]}},{"segment":{"segment_name":"Florida, US","segment_value":"4ec01c9dbc693497"},"metrics":{"billed_engagements":[0,0,0,0,0,0,0,0,0,1,1,2,2,5,0,0,0,1,0,0,0,0,0,0],"billed_charge_local_micro":[0,0,0,0,0,0,0,0,0,3200000,3000000,5900000,6400000,14800000,0,0,0,2900000,0,0,0,0,0,0]}},{"segment":{"segment_name":"Unknown","segment_value":"0000000000000000"},"metrics":{"billed_engagements":[1,0,0,0,1,0,0,0,0,1,2,3,4,5,2,0,1,0,0,1,0,0,0,0],"billed_charge_local_micro":[3200000,0,0,0,3000000,0,0,0,0,3000000,6000000,10751704,10639534,14400000,6000000,0,3200000,0,0,3200000,0,0,0,0]}}]},{"id":"hwbna","id_data":[]},{"id":"hyac2","id_data":[]}],"request":{"params":{"start_time":"2022-06-14T00:00:00Z","segmentation_type":"REGIONS","entity_ids":["hyac2","huiuj","hwbna"],"end_time":"2022-06-15T00:00:00Z","country":"-6997c336ed98be2f","placement":"ALL_ON_TWITTER","granularity":"HOUR","entity":"CAMPAIGN","platform":null,"metric_groups":["BILLING"]}}}

Hello,

May I know what is the entity_id for the campaign that has spend $215.93 in the UI? Also, I would need your account ID to reproduce your issue. This information isn’t considered sensitive and cannot be used by unauthorized users in any way.

I am very sorry, I dont know how to get the entity IDs in the UI. Could you help me with a screenshot to find that please.

Thanks a lot for your help.

Hello,

Maybe you can share with me the url of the UI screenshot that you have attached.

Thanks @jaredtcy

Here is the URL: https://ads.twitter.com/ads_manager/18ce55fgrfu/accounts/GEO/-7608764736147602991/3/?columnSet=1504191726772174848&columns=39.8.9.70.82.27.19.108.189&endDate=2022-06-14&startDate=2022-06-14

Hello,

Thank you for sharing the url. After checking, the amount $215.93 should be from the campaign_id: huiuj which I see that you have specified in your request.

FYI, this is how you can find your entity_id:

  1. On your ads admin dashboard, locate the campaigns which contributes to the amount (In your case: $215.93)

  2. Click on the campaign

  1. On the URL, you should see something like /campaigns/29977579/. The value 29977579 is a base 10 value. You will need to use a 3rd party tool to convert 29977579 into base 36 and this will give you the entity_id.

Example: 29977579 (base 10)= HUIUJ (base 36)

For your case, could you try this POST API call:

twurl -H ads-api.twitter.com  "/11/stats/jobs/accounts/18ce55fgrfu?entity=CAMPAIGN&entity_ids=HUIUJ&start_time=2022-06-14&end_time=2022-06-15&granularity=TOTAL&metric_groups=BILLING&placement=ALL_ON_TWITTER&segmentation_type=REGIONS&country=96683cc9126741d1" -X POST | jq .

This should be the response you see:

{
  "data_type": "stats",
  "time_series_length": 1,
  "data": [
    {
      "id": "huiuj",
      "id_data": [
        {
          "segment": {
            "segment_name": "Delaware, US",
            "segment_value": "3f5897b87d2bf56c"
          },
          "metrics": {
            "billed_engagements": [
              2
            ],
            "billed_charge_local_micro": [
              5400000
            ]
          }
        },
        {
          "segment": {
            "segment_name": "California, US",
            "segment_value": "fbd6d2f5a4e4a15e"
          },
          "metrics": {
            "billed_engagements": [
              8
            ],
            "billed_charge_local_micro": [
              23300000
            ]
          }
        },
        {
          "segment": {
            "segment_name": "Connecticut, US",
            "segment_value": "e86b380cfefcced5"
          },
          "metrics": {
            "billed_engagements": [
              2
            ],
            "billed_charge_local_micro": [
              6000000
            ]
          }
        },
        {
          "segment": {
            "segment_name": "New Jersey, US",
            "segment_value": "65b4760a2b411e11"
          },
          "metrics": {
            "billed_engagements": [
              10
            ],
            "billed_charge_local_micro": [
              27701900
            ]
          }
        },
        {
          "segment": {
            "segment_name": "Pennsylvania, US",
            "segment_value": "dd9c503d6c35364b"
          },
          "metrics": {
            "billed_engagements": [
              7
            ],
            "billed_charge_local_micro": [
              19710662
            ]
          }
        },
        {
          "segment": {
            "segment_name": "New York, US",
            "segment_value": "94965b2c45386f87"
          },
          "metrics": {
            "billed_engagements": [
              15
            ],
            "billed_charge_local_micro": [
              47889338
            ]
          }
        },
        {
          "segment": {
            "segment_name": "Nevada, US",
            "segment_value": "d374fb61a20fb74f"
          },
          "metrics": {
            "billed_engagements": [
              1
            ],
            "billed_charge_local_micro": [
              3000000
            ]
          }
        },
        {
          "segment": {
            "segment_name": "Texas, US",
            "segment_value": "e0060cda70f5f341"
          },
          "metrics": {
            "billed_engagements": [
              13
            ],
            "billed_charge_local_micro": [
              37530789
            ]
          }
        },
        {
          "segment": {
            "segment_name": "Florida, US",
            "segment_value": "4ec01c9dbc693497"
          },
          "metrics": {
            "billed_engagements": [
              14
            ],
            "billed_charge_local_micro": [
              42400000
            ]
          }
        },
        {
          "segment": {
            "segment_name": "Unknown",
            "segment_value": "0000000000000000"
          },
          "metrics": {
            "billed_engagements": [
              24
            ],
            "billed_charge_local_micro": [
              72491238
            ]
          }
        }
      ]
    }
  ],
  "request": {
    "params": {
      "start_time": "2022-06-14T04:00:00Z",
      "segmentation_type": "REGIONS",
      "entity_ids": [
        "huiuj"
      ],
      "end_time": "2022-06-15T04:00:00Z",
      "country": "-6997c336ed98be2f",
      "placement": "ALL_ON_TWITTER",
      "granularity": "TOTAL",
      "entity": "CAMPAIGN",
      "platform": null,
      "metric_groups": [
        "BILLING"
      ]
    }
  }
}

Please ignore the Unknown section for now. But if you count, there are 9 regions returned and these 9 regions spend corresponds to what is seen on the UI

To add, you may realized that when adding the 9 values, it amounts to $212.93 whereas on the UI, it shows $215.93. Note that this is expected behavior due to the small attribution loss we experience when breaking out billing and targeting data. Please note that every ad impression has in fact served to your targeting specifications, but there is a 1-3% loss in the segmented spend data we display in the dashboard.

We recommend to not look at the performance based on the sum of the line items as they may include duplication or attribution loss. Instead, metrics should be pulled and analyzed at the desired level of aggregation (i.e. Campaign, Ad Group, country).