Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Graph API - getting daily spend per ad set

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.

like image 379
Aine Avatar asked Oct 13 '15 15:10

Aine


2 Answers

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.

like image 78
The1Fitz Avatar answered Nov 10 '22 17:11

The1Fitz


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

like image 29
ale500 Avatar answered Nov 10 '22 17:11

ale500