Facebook营销API - 获取广告关键字统计

问题描述:

我想使用Facebook营销API获取广告关键字统计信息,我在Facebook开发人员部分&中发现了一些相同的文档,这里是相同的链接。Facebook营销API - 获取广告关键字统计

https://developers.facebook.com/docs/marketing-api/reference/ad-keyword-stats

为了让广告关键字统计数据特定帐户,我需要通过所有帐户环路和广告活动&广告或有任何直接的方式,我可以通过广告帐户&得到所有广告关键字统计数据?

请帮帮我。

谢谢!

以下是我与之合作的代码,并最终解决了所有问题,但它吸引了诸如CPC,展示等方面的见解,并且基于您使用的参数可以从帐户,广告系列,广告甚至广告级别获取。所以让我知道这是否能帮助你。

<?php 

     require_once __DIR__ . '/vendor/autoload.php'; 
     use FacebookAds\Api; 
     use FacebookAds\Object\AdUser; 
     use Facebook\Facebook; 
     use Facebook\Exceptions\FacebookResponseException; 
     use Facebook\Exceptions\FacebookSDKException; 
     use FacebookAds\Object\Campaign; 
     use FacebookAds\Object\Fields\AdsInsightsFields; 
     use FacebookAds\Object\Ad; 
     use FacebookAds\Object\Fields\AdSetFields; 
     use FacebookAds\Object\AdCampaign; 
     use FacebookAds\Object\Fields\AdFields; 
     use FacebookAds\Object\Fields; 
     use FacebookAds\Object\Fields\AdImageFields; 
     use FacebookAds\Object\AdAccount; 
     use FacebookAds\Object\AdSet; 
     use FacebookAds\Object\AdCreative; 
     use FacebookAds\Object\Fields\AdCreativeFields; 
     use FacebookAds\Object\Fields\AdCreativePhotoDataFields; 
     use FacebookAds\Object\AdCreativeLinkData; 
     use FacebookAds\Object\Fields\AdCreativeLinkDataFields; 
     use FacebookAds\Object\Fields\CampaignFields; 
     use FacebookAds\Object\Page; 
     use FacebookAds\Object\Fields\AdPreviewFields; 
     use FacebookAds\Object\Values\AdPreviewAdFormatValues; 
     use FacebookAds\Object\AdVideo; 
     ?> 

Here is the general code I am trying 

<?php 
     // Init PHP Sessions 
     session_start(); 

     $fb = new Facebook([ 
      'app_id' => 'xxxxxxxxx', 
      'app_secret' => 'xxxxxxxxxxx', 
     ]); 

     $helper = $fb->getRedirectLoginHelper(); 

     if (!isset($_SESSION['enter api key here'])) { 
      $_SESSION['enter api key here'] = null; 
     } 

     if (!$_SESSION['enter api key here']) { 
      $helper = $fb->getRedirectLoginHelper(); 
      try { 
      $_SESSION['enter api key here'] = (string) $helper->getAccessToken(); 
      } catch(FacebookResponseException $e) { 
      // When Graph returns an error 
      echo 'Graph returned an error: ' . $e->getMessage(); 
      exit; 
      } catch(FacebookSDKException $e) { 
      // When validation fails or other local issues 
      echo 'Facebook SDK returned an error: ' . $e->getMessage(); 
      exit; 
      } 
     } 

     if ($_SESSION['enter api key here']) { 
      //echo "You are logged in!"; 

     // Initialize a new Session and instantiate an API object 
     Api::init(
      'xxxxxxxxx', // App ID 
      'xxxxxxxxx', //app_secret 
      $_SESSION['enter api key here'] // Your user access token 
     ); 

    ?> 
<div id="fbdata"></div> <?php 

    $account = new AdAccount('act_xxxxxxxxxx'); 


     $params = array(

     'date_preset'=> 'last_28d', 


      'thumbnail_width' => 200, 
      'thumbnail_height' => 150, 
      'level' => 'campaign', 
      'limit' => '15' 

     ); 

$fields = array(
    AdsInsightsFields::CAMPAIGN_NAME, 
    AdsInsightsFields::CAMPAIGN_ID, 
    AdsInsightsFields::IMPRESSIONS, 
    AdsInsightsFields::CLICKS, 
    AdsInsightsFields::REACH, 
    AdsInsightsFields::SPEND, 
    AdsInsightsFields::CPM, 
    AdsInsightsFields::CPC, 
    AdsInsightsFields::ACTIONS, 
); 

$field = array(
    AdCreativeFields::TITLE, 
    AdCreativeFields::THUMBNAIL_URL, 
    AdCreativeFields::BODY, 
); 

      $params1 = array(
      'time_range' => array(
      'since' => (new \DateTime($beginDate))->format('Y-m-d'), 
      'until' => (new \DateTime($lastDate))->format('Y-m-d'), 
     ), 
      'thumbnail_width' => 200, 
      'thumbnail_height' => 150, 
      'level' => 'ad', 
      'limit' => '5' 
     );  

$adcreatives = $account->getAdCreatives($field, $params1); 
?>  
     <table class="fbtable"> 
      <tr> 
       <th>Title</th> 
       <th>Ad Image</th> 
       <th>Ad Body</th> 
      </tr> 
      <?php 
foreach($adcreatives as $t2){ 

     echo"<tr> 
     <td>$t2->title</td> 
     <td><img src='$t2->thumbnail_url'/></td> 
     <td>$t2->body</td> 
    </tr>"; 
} 

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

     <table class="fbtable"> 
      <tr> 
       <th>Campaign ID</th> 
       <th>Campaign Name</th> 
       <th>Impressions</th> 
       <th>Clicks</th> 
       <th>Reach</th> 
       <th>Spend</th> 
       <th>Total Actions</th> 
       <th>CPM</th> 
       <th>CPC</th> 
      </tr> 

      <?php 

foreach($insights as $i) { 
    $impress = number_format((float)$i->impressions); 
    $reach = number_format((float)$i->reach); 
    $totalAction = number_format((float)$i->actions); 
    $cpc = number_format($i->cpc, 2, '.', ''); 
    $cpm = number_format($i->cpm, 2, '.', ''); 
    echo"<tr class='fbtable'> 
     <td>$i->campaign_id</td> 
     <td>$i->campaign_name</td> 
     <td>$impress</td> 
     <td>$i->clicks</td> 
     <td>$reach</td> 
     <td>$$i->spend</td> 
     <td>$totalAction</td> 
     <td>$$cpm</td> 
     <td>$$cpc</td> 
    </tr>"; 
} 
     }else { 
      $permissions = ['ads_management']; 
      $loginUrl = $helper->getLoginUrl('http://where you want login to be.com', $permissions); 
      echo '<a href="' . $loginUrl . '">Log in with Facebook</a>'; 
     } 


?>