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.
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}}
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.
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:
The following request in the Graph API explorer, or via cURL (both provided) should return you the spend for the entire account:
https://graph.facebook.com/<VERSION>/act_<ACCOUNT_ID>/insights?fields=spend&level=account&date_preset=yesterday&access_token=<ACCESS_TOKEN>
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
$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.