2017-06-17 64 views
0

我想使用Facebook營銷API獲取廣告關鍵字統計信息,我在Facebook開發人員部分&中發現了一些相同的文檔,這裏是相同的鏈接。Facebook營銷API - 獲取廣告關鍵字統計

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

爲了讓廣告關鍵字統計數據特定帳戶,我需要通過所有帳戶環路和廣告活動&廣告或有任何直接的方式,我可以通過廣告帳戶&得到所有廣告關鍵字統計數據?

請幫幫我。

謝謝!

回答

0

以下是我與之合作的代碼,並最終解決了所有問題,但它吸引了諸如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>'; 
     } 


?> 
相關問題