Facebook Marketing API和Ads Insight Access

时间:2017-06-26 17:27:14

标签: facebook facebook-ads-api facebook-marketing-api

我正在构建一个与各种API协同工作的小程序,以自动化我们为一些客户提供的报告服务。

我们报告的一个问题是Facebook广告效果。

我一直在使用Facebook Marketing API& amp ;; Ads Insight APi https://developers.facebook.com/docs/marketing-apis

我已经完成了设置Facebook“应用”和获取相关应用令牌的整个过程。

但是,由于开发访问只允许使用5个广告帐户,因此我遇到了障碍。

要申请基本访问权限,营销API会要求开发平台以及与我的程序无关的一大堆其他内容。我不打算建立一个传统意义上的公共发布应用程序;相反,我只想在基本级别上集成这些API以自动执行一些内部任务。

令我难以置信的是,我无法做到这样的事情,但这正是Facebook文档似乎在暗示的内容。

如果有其他人熟悉这类问题,我很想知道是否有解决方法。

2 个答案:

答案 0 :(得分:2)

Derks,我目前正在建立超过40个客户的仪表板,我能够以基本的开发水平展示他们的每个人以及他们的见解数据等。我有代码,如果你想看看我现在想要完成的事情就是制作一个日期范围选择器,但是,你非常欢迎看看我有什么只是为了得到一个大概。

以下是Facebook中的使用对象

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


?>

我会尽可能多地帮助@Derks而且我相信你唯一需要做的就是找出方案或者你的建筑知道谁是谁。

答案 1 :(得分:2)

提交您的应用以供审核,以获得超过10个广告帐户。使用&#34;平台&#34;网站。选择原生或桌面应用; app secret未嵌入客户端。其余部分可能会被忽略,但您需要包含应用程序的一些屏幕截图以及它的工作原理和工作原理。一个真实的人会审查它,你可以在这里获得Facebook开发者小组的帮助: https://www.facebook.com/groups/fbdevelopers

您只会获得通过/失败...没有评论。如果他们第一次拒绝它,不要感到惊讶。不要重新提交并希望第二次更好的回应 - 他们最终会把你锁定几天。向小组发布帮助问题。

相关问题