classmethodsfacebook-php-sdk

how to get budget spent per advertising campaign


I am implementing the use of the Facebook Business SDK for PHP API (Meta Business SDK v18.0.2) to retrieve information from advertising campaigns (the name and budget spent) over the last 30 days.

I created an application at Facebook Developers. I got the app id with the secret key. Then I created a page token.

And I use this php code

Api::init($app_id, $app_secret, $access_token);
$api = Api::instance();// The Api object is now available through singleton
$api->setLogger(new CurlLogger());


$fields = array(
  CampaignFields::NAME,
  CampaignFields::LIFETIME_BUDGET,
//  CampaignFields::SPEND_CAP,
  CampaignFields::OBJECTIVE,
//  CampaignFields::AMOUNT_SPENT,
  CampaignFields::START_TIME,
  CampaignFields::DAILY_BUDGET,
  CampaignFields::STATUS,
  CampaignFields::BUDGET_REMAINING,
);

$params = array(
  CampaignFields::EFFECTIVE_STATUS => array(CampaignEffectiveStatusValues::ACTIVE),
  AdReportRunFields::DATE_PRESET => CampaignDatePresetValues::LAST_3D,// LAST_30D,
);



$account = new AdAccount($account_id);
$response = $account->getCampaigns($fields, $params);


//$response->fetchAfter();

$oCampaigns = $response->getResponse()->getContent();


echo "<br /><br />——————————————————<br />";

echo json_encode($oCampaigns, JSON_PRETTY_PRINT);


$fieldsAds = array(
 // 'currency',
 // 'spend'
  CampaignFields::NAME,
  CampaignFields::LIFETIME_BUDGET,
//  CampaignFields::SPEND_CAP,
  CampaignFields::OBJECTIVE,
//  CampaignFields::AMOUNT_SPENT,
  CampaignFields::START_TIME,
  CampaignFields::STATUS,
  CampaignFields::BUDGET_REMAINING
);

$paramsAds = array(
);


foreach ($oCampaigns['data'] as $value) {
  $iBudgetJour =  $value['daily_budget'] / 100;
  
  echo "<br />fields id : {$value['id']} - nom de la campagne : {$value['name']} - budget quotidien : {$iBudgetJour} euros<br /><br />";


  $oCampaignAds =  new Campaign($value['id']);
  //$responseAds = $oCampaignAds->getAds($fieldsAds,$paramsAds);
  $responseAds = $oCampaignAds->getAdSets($fieldsAds,$paramsAds);
  $oAds = $responseAds->getResponse()->getContent();
  
  echo json_encode($oAds, JSON_PRETTY_PRINT);
  echo "<br />——————————————————<br />";
}

But I can't recover the total amount spent per advertising campaign. Filter over the last 30 days don't workd.

Can anyone help me?

I haven't managed to find clear documentation with methods and classes on the Facebook Business API.

Thank you.

I tried several filters and parameters for the getCampaigns() method (AdAccount object) but it does not return the data I am looking for.


Solution

  • I find the solution.

    Here the code for help other people.

        /**
         * Step 1 Instantiate an API
         *
         * @link https://developers.facebook.com/docs/marketing-api/sdks#init-sdk
         */
        try {
            Api::init(APP_ID, APP_SECRET, ACCESS_TOKEN);
    
        }
        catch (\Exception $e) {
            var_dump('Api::init Exception with message : ' . $e->getMessage());
            var_dump($e->getResponse());
            return $e;
        }
    
        $api = Api::instance();// The Api object is now available through singleton
        $api->setLogger(new CurlLogger());
    
    
        /**
         * Step 2 Query to access campaign
         * @var $campaign
         * @link https://developers.facebook.com/docs/marketing-api/reference/ad-campaign-group
         *
         */
        try {
            $account = new AdAccount(ACCOUNT_ID);
    
        }
        catch (\Exception $e) {
            var_dump('AdAccount Exception with message : ' . $e->getMessage());
            var_dump($e->getResponse());
            return $e;
        }
    
    
        /**
         * Step 3 Query to get all campaigns informations
         * @var $campaign
         * @link https://developers.facebook.com/docs/marketing-api/reference/ad-campaign-stats/?locale=fr_FR
         *
         */
        try {
            $fields = array();
    
            $params = array(
              'level' => 'campaign',
              CampaignFields::EFFECTIVE_STATUS => array(Campaign::STATUS_ACTIVE, Campaign::STATUS_PAUSED),
              AdReportRunFields::DATE_PRESET => CampaignDatePresetValues::LAST_30D,//
              'fields' => 'spend, impressions, date_start, date_stop, campaign_name, campaign_id',
            );
    
            $response = $account->getInsights($fields, $params);
            $oCampaignsInsights = $response->getResponse()->getContent();
    
            //var_dump( json_encode($oCampaignsInsights, JSON_PRETTY_PRINT) );
    
            /*foreach ($oCampaignsInsights['data'] as $value) {
                var_dump( "<br />fields id : {$value['campaign_id']} - name : {$value['campaign_name']} - Spend : {$value['spend']}<br /><br />");
            }*/
    
        }
        catch (\Exception $e) {
            var_dump('Campaign Exception with message : ' . $e->getMessage());
            var_dump($e->getResponse());
            return $e;
        }