0

I write the below code for retrieve single campaign stats.

$fields = array(
      'start_time','actions','spent','clicks','impressions','end_time',
    );
$params = array();
$campaign = new AdCampaign(123456);
$stats = $campaign->getStats($fields, $params);

here I can able to access the stats. But when use this function loop then I got issue like

Calling : $campaign = new AdCampaign($campaign_id); Error : "An access token is required to request this resource"

But using graph API I can access the multiple campaigns stats at a time

https://graph.facebook.com/stats?ids=123,456,789&fields=start_time,actions,spent,clicks,impressions,end_time&access_token=...

I need it using Ads API .... Please solve it for me..

Sudipta Chatterjee
  • 4,592
  • 1
  • 27
  • 30

1 Answers1

0

I think this is a missing feature from the SDK at the moment as you can only access stats relative to an object.

However, calling stats in a loop should not be a problem, assuming you instantiated the API class correctly.

use FacebookAds\Api;
use FacebookAds\Object\AdCampaign;

Api::init($app_id, $app_secret, $access_token);

$campaign_ids = array(...);

$fields = array(
  'start_time',
  'actions',
  'spent',
  'clicks',
  'impressions',
  'end_time',
);    
$params = array();
$stats = array();
foreach($campaign_ids as $id) {
  $campaign = new AdCampaign($id);
  $stats[$id] = $campaign->getStats($fields, $params);
}

You could also just get all stats for all campaigns by using the getAdCampaignStats on AdAccount.

Paul Bain
  • 4,364
  • 1
  • 16
  • 30