|

|  How to Use Facebook Marketing API to Retrieve Ad Insights in PHP

How to Use Facebook Marketing API to Retrieve Ad Insights in PHP

October 31, 2024

Learn to harness the Facebook Marketing API using PHP for retrieving detailed ad insights. Enhance your advertising strategy with this comprehensive guide.

How to Use Facebook Marketing API to Retrieve Ad Insights in PHP

 

Install the Facebook Ads SDK for PHP

 

  • You can install the Facebook Ads SDK for PHP via Composer, a dependency manager for PHP.
  •  

  • Make sure you have Composer installed on your machine, then use the following command to install the SDK:

 

composer require facebook/graph-sdk

 

Initialize the Facebook API Client

 

  • Once the SDK is installed, you need to initialize it and create a new API client object, which will require your Facebook App credentials.
  •  

  • Create an instance of the Facebook object with your App ID, App Secret, and a valid Access Token (ensure the token has necessary permissions for reading ad insights).

 

require 'vendor/autoload.php';

use FacebookAds\Api;

$app_id = 'your-app-id';
$app_secret = 'your-app-secret';
$access_token = 'your-access-token';

// Initialize the API
Api::init($app_id, $app_secret, $access_token);

 

Fetch Ad Insights

 

  • Once the API is initialized, you can call the Facebook Marketing API endpoint to retrieve insights for your ads.
  •  

  • Define the fields you want to pull from the API, such as impressions, clicks, and conversions.
  •  

  • Make a request to the ad insights endpoint using the respective ad account ID you wish to analyze.

 

use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Fields\InsightsFields;
use FacebookAds\Object\Values\InsightsPresets;

$ad_account_id = 'act_your-ad-account-id';

$ad_account = new AdAccount($ad_account_id);

$params = array(
  'level' => 'ad',
  'date_preset' => InsightsPresets::LAST_7_DAYS,
);

$fields = array(
  InsightsFields::IMPRESSIONS,
  InsightsFields::CLICKS,
  InsightsFields::SPEND,
  InsightsFields::CONVERSIONS,
);

$insights = $ad_account->getInsights($fields, $params);

foreach ($insights as $insight) {
  echo 'Ad ID: ' . $insight->ad_id . "\n";
  echo 'Impressions: ' . $insight->impressions . "\n";
  echo 'Clicks: ' . $insight->clicks . "\n";
  echo 'Spend: ' . $insight->spend . "\n";
  echo 'Conversions: ' . $insight->conversions . "\n";
}

 

Error Handling and Debugging

 

  • It's important to implement error handling while making API requests to handle any issues that arise, such as permissions errors or network issues.
  •  

  • Leverage PHP's try-catch blocks to handle potential exceptions and log error messages for troubleshooting.

 

try {
  $insights = $ad_account->getInsights($fields, $params);
} catch (\Exception $e) {
  echo 'Error: ' . $e->getMessage() . "\n";
}

 

Optimize API Usage

 

  • Facebook's API usage is subject to rate limits, so it's critical to manage your requests efficiently to avoid running into these limits.
  •  

  • Plan your API calls to batch requests together or use pagination effectively to minimize the number of requests.