Show Facebook stats on a page using the Facebook Ads API in PHP

Does anyone know how to get facebook statistics stats and display them on a webpage using Facebook Ads Api-PHP SDK . I am using this API and I am getting campaign data like campaign name, id, status. but not able to get impressions, clicks spent.

What I do, let me share with you: 1) I get access token

by user authorization 2) After getting access token I use below code

$account = new AdAccount('act_XXXXXXXXXXXXXXX');
$account->read();

$fields = array(
  AdCampaignFields::ID,
  AdCampaignFields::NAME,
  AdCampaignFields::OBJECTIVE,
);
$params = array(AdCampaignFields::STATUS => array(AdCampaign::STATUS_ACTIVE,AdCampaign::STATUS_PAUSED,),);
$campaigns = $account->getAdCampaigns($fields, $params);

/* Added By Jigar */     
$campaign = new AdCampaign('XXXXXXXXXXXXXXXX');
$compainDetails = $campaign->read($fields);

      

3) then print the array

echo "<pre>";
print_r($compainDetails); 
exit;

      

If anyone knows of any suggestions in the above code please share. All the code is in PHP. Dose anyone have a tutorial that extracts all of the above required data and then shares it.

+3


source to share


1 answer


You can try using the facebook insights api instead of $ campaign-> read. Here's an example:

https://developers.facebook.com/docs/marketing-api/insights/v2.5#create-async-jobs

What you need to do to get impressions, click and spend is add these fields to the $ fields parameter. In your case, the complete code should look like this:

    use FacebookAds \ Object \ Campaign;
    use FacebookAds \ Object \ Values ​​\ InsightsLevels;
    use FacebookAds \ Object \ Values ​​\ InsightsFields;

    $ campaign = new Campaign ();

    $ fields = array (
      InsightsFields :: IMPRESSIONS,
      InsightsFields :: UNIQUE_CLICKS,
      InsightsFields :: CALL_TO_ACTION_CLICKS,
      InsightsFields :: INLINE_LINK_CLICKS,
      InsightsFields :: SOCIAL_CLICKS,
      InsightsFields :: UNIQUE_SOCIAL_CLICKS,
      InsightsFields :: SPEND,
    );

    $ params = array (
      'level' => InsightsLevels :: CAMPAIGN,
    );

    $ async_job = $ campaign-> getInsightsAsync ($ fields, $ params);

    $ async_job-> read ();



I don't know what exactly the "click" parameter means to you, but if you look at all these click parameters, I'm sure you will find it or you will know how to calculate it. A complete list of fields available for insights objects can be viewed at: https://github.com/facebook/facebook-php-ads-sdk/blob/master/src/FacebookAds/Object/Fields/InsightsFields.php

Hope it helps.

Best regards, Benjamin

+1


source







All Articles