首页 文章

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

提问于
浏览
0

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

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

为了获取特定帐户的广告关键字统计信息,我是否需要循环浏览所有帐户,广告系列和广告,还是有直接方式可以通过广告帐户并获取所有广告关键字统计信息?

请帮我 .

谢谢!

1 回答

  • 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>';
            } 
    
    
    ?>
    

相关问题