Facebook Graph API - getting daily spend per ad set
Asked Answered
I

2

8

I’m trying to get the daily spend per ad set for all our ad accounts. We’re spending lots of money with Facebook and Finance wants to see where it is going. Basically, I’m looking each day to get the spend per ad set for yesterday. The problem is, there are lots of ad sets.

I’m trying to do this in two parts. First get the ad set IDs from the Graph API. Then get the spend using the Marketing API.

1. Graph API (v.2.4)

I'm getting a list of all the ad sets we have. I cannot do this in one call, as there are to many results (over nine pages).

GET/v2.4/me/adaccounts?fields=name,adcampaign_groups{id,name,campaign_group_status,account_id,adcampaigns{id,name}}

2. Marketing API

To get the spend for yesterday, I'm making a call to the Marketing API for each ad set. This is the PHP code I’m using:

// Get spend for yesterday
$adSet = new \FacebookAds\Object\AdSet($adSetId);
$params = array(
    'date_preset' => InsightsPresets::YESTERDAY,
);
/** @var Cursor $insights */
$insights = $adSet->getInsights(array('spend'), $params);
$insights->rewind();
$spend = 0;
if ($insights->current() != false) {
    $spend = $insights->current()->getData()['spend'];
}

Since I have lots of ad sets, I’m hitting my trotting limit long before I get the data I need.

Is there any way to get this information in one call? If not, can I get the spend data from the Marketing API in less calls?

I’m happy to update to Graph API v2.5 if it helps. I’m using composer libraries:

facebook/php-sdk-v4: ~5.0
facebook/php-ads-sdk: 2.4.*

Thanks.

Interscholastic answered 13/10, 2015 at 15:26 Comment(0)
E
13

There is no need to do this on the adset level. With calls to the Ad Insights API what you can do is specify a specific aggregate for the data at a defined object level. Given that you have fetched your ad accounts from the /me/adaccounts endpoint you should then be able to do the following:

1. Graph API Request

The following request in the Graph API explorer, or via cURL (both provided) should return you the spend for the entire account:

1a. Graph URL

https://graph.facebook.com/<VERSION>/act_<ACCOUNT_ID>/insights?fields=spend&level=account&date_preset=yesterday&access_token=<ACCESS_TOKEN>

1b. cURL

curl -G \
-d 'level=account' \
-d 'fields=spend' \
-d 'date_preset=yesterday' \
-d 'access_token=<ACCESS_TOKEN>' \
https://graph.facebook.com/<VERSION>/act_<ACCOUNT_ID>/insights

2. Marketing API SDK

$account = new AdAccount('act_<Ad_ACCOUNT_ID>');

$params = array(
  'level' => 'account',
  'date_preset' => InsightsPresets::YESTERDAY,
  'fields' => 'spend',
);

$account->getInsights(array(), $params);

I believe that the above should help you. I have tested with the 1a & 1b and it works perfectly but haven't fully tested with 2.

Eulau answered 27/10, 2015 at 16:33 Comment(4)
Sadly the accounts want to see the result per ad set. After must banging my head off the wall, I'm now using the graph API to get the total spend so far, and calculating the difference per day - me/adaccounts?fields=name,campaigns{id,name,adsets{id,name,insights{spend}}}Interscholastic
Even with a date range or preset specified it returns the spend on the ad so far not within the specified date range. This is driving me nuts. Any ideas? I am using pythonNappy
@LucyTurtle, have you found a solution? i working right now in on this :/Brownedoff
@AlejandroViquez what exactly are you looking for? Take a look at my answer on this: #47835023Nappy
O
1

The parameter that you are looking for is "time_increment" equals 1. Here is the docs https://developers.facebook.com/docs/marketing-api/insights/v2.5

Outworn answered 25/11, 2015 at 7:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.