Search code examples
phpfacebook-graph-apifacebook-php-sdk

Facebook InsightsPresets not found


Example from facebook dev site:

`use FacebookAds\Object\AdCampaign;
use FacebookAds\Object\Values\InsightsPresets;`

`$campaign = new AdCampaign('<AD_CAMPAIGN_ID>');
$params = array(
  'date_preset' => InsightsPresets::LAST_7_DAYS,
);
$insights = $campaign->getInsights(null, $params);
print_r($insights);`

But use FacebookAds\Object\Values\InsightsPresets; does not exist in "facebook/php-business-sdk": "^3.2".

So I've tried to use:

use FacebookAds\Object\Campaign;
use FacebookAds\Object\Values\InsightsResultDatePresetValues;`
`$campaign = new Campaign('AD_CAMPAIGN_ID');
$params = array(
  'date_preset' => InsightsResultDatePresetValues::LAST_90D,
);
$insights = $campaign->getInsights(null, $params);
print_r($insights);

But it gives an error:

Argument 1 passed to FacebookAds\Object\Campaign::getInsights() must be of the type array, null given,

So what and how I need to use instead use FacebookAds\Object\Values\InsightsPresets;

I need to get Insights from Campaigns.


Solution

  • The first params should be an array of requested fields. If you pass an empty array the endpoint return standard/default metrics. As example:

    $fields = array();
    
    $params = array(
          'date_preset' => InsightsResultDatePresetValues::LAST_90D,
        );
        $insights = $campaign->getInsights($fields, $params);
        print_r($insights);
    

    Or:

    $fields = array('clicks','cpc','cpm');
    
    $params = array(
          'date_preset' => InsightsResultDatePresetValues::LAST_90D,
        );
        $insights = $campaign->getInsights($fields, $params);
        print_r($insights);
    

    More info about field metrics here in the doc