duanju8431 2017-06-22 21:48
浏览 243
已采纳

用于获取Page Insights数据的Facebook Graph URL

I am trying to build an analytics dashboard and get the page views, engagements, and reach for a Facebook page. I haven't found a way to access read_insights on the graph api with our app and admin user (see this question: Facebook (#100) Tried accessing nonexisting field (read_insights) on node type (Page)), but I have been able to directly access some information using file_get_contents and running a url through it. I know that this one works:

https://graph.facebook.com/{page_id}/feed?access_token={access_token}

With that call I get arrayed data of the recent posts.

I have run this URL to get insights page_views_total:

https://graph.facebook.com/{page_id}/insights/page_views_total?access_token={access_token}

but I only get back an array

{
   "data": [

   ],
   "paging": {
      "previous": "https://graph.facebook.com/v2.9/{page_id}/insights?access_token={token_string}\{access_string}w&pretty=1&metric=page_views_total&since=1497596400&until=1497769200",
      "next": "https://graph.facebook.com/v2.9/{page_id}/insights?access_token={token_string}\{access_string}&pretty=1&metric=page_views_total&since=1497942000&until=1498114800"
   }
}

Which none of these arrays have any useful information. I've tried running the enclosed URLs in the arrays but it just gives Oauth errors.

The way I got this URL initially was following these steps and replacing feed with insights/{metric_data}

"Go to https://developers.facebook.com/tools/access_token/ . When you copy it, you just go directly https://graph.facebook.com//feed?access_token="

Side note: The Facebook Graph API is rather difficult to interpret for newbies so I have put aside the authentication and token methods in PHP and am trying to get a direct URL for my data. Probably not the best solution but I can't figure out how to get authenticated with my app and extract the page insights data.

  • 写回答

1条回答 默认 最新

  • dswmmvrg40957 2017-06-23 12:50
    关注
    1. Check you are admin of the page.
    2. Create your app.
    3. Get a non-expiring token. To do so, follow this guide found on another question: Long-lasting FB access-token for server to pull FB page info

    Once you have this token you can string together a URL to get any insights data you need. For example, to get page_views_total for the past week:

    https://graph.facebook.com/{app_id}/insights/page_views_total/week?access_token={non_expiring_token}
    

    This should return an array which if using PHP you can get the file contents on this URL and use json_decode to read it as an object:

    $requestedInsights = file_get_contents('https://graph.facebook.com/{app_id}/insights/page_views_total/week?access_token={non_expiring_token}');     
    $decodedObject = json_decode($requestedInsights);
    

    The array should look like this:

    {
       "data": [
          {
             "name": "page_views_total",
             "period": "week",
             "values": [
                {
                   "value": 4,
                   "end_time": "{time_string}"
                },
                {
                   "value": 5,
                   "end_time": "{time_string}"
                }
             ],
             "title": "Weekly Total views count per Page",
             "description": "Weekly: Total views count per Page",
             "id": "{app_id}/insights/page_views_total/week"
          }
       ],
       "paging": {
          "previous": "{paging_url_with_token}",
          "next": "{paging_url_with_token}"
       }
    }
    

    You can then get the specific value you need in the object with:

    $pageViewsInsight = $decoded->data[0]->values[0]->value;
    

    I have found that the first value matches what I have seen on the Insights dashboard. I am not sure why two different values are returned with different time codes.

    You can change week to day or days_28. I haven't found a way to use until= or set specific times other than these.

    To request multiple metrics, string them together with commas in between.

    https://graph.facebook.com/{app_id}/insights/page_views_total,page_impressions_unique,page_post_engagements/week?access_token={non_expiring_token}
    

    With this method, you cannot specify times (week, day, days_28) for each Insight. It will apply the time for all of the requested metrics.

    With the request of multiple metrics you will need to check the name of the data array to get the appropriate values.

    {
       "data": [
          {
             "name": "page_impressions_unique",
             "period": "week",
             "values": [
                {
                   "value": 38,
                   "end_time": "{time_string}"
                },
                {
                   "value": 24,
                   "end_time": "{time_string}"
                }
             ],
             "title": "Weekly Total Reach",
             "description": "Weekly: The number of people who have seen any content associated with your Page. (Unique Users)",
             "id": "{app_id}/insights/page_impressions_unique/week"
          },
          {
             "name": "page_views_total",
             "period": "week",
             "values": [
                {
                   "value": 4,
                   "end_time": "{time_string}"
                },
                {
                   "value": 5,
                   "end_time": "{time_string}"
                }
             ],
             "title": "Weekly Total views count per Page",
             "description": "Weekly: Total views count per Page",
             "id": "{app_id}/insights/page_views_total/week"
          },
          {
             "name": "page_post_engagements",
             "period": "week",
             "values": [
                {
                   "value": 7,
                   "end_time": "{time_string}"
                },
                {
                   "value": 10,
                   "end_time": "{time_string}"
                }
             ],
             "title": "Weekly Post Engagements",
             "description": "Weekly: The number of times people have engaged with your posts through like, comments and shares and more.",
             "id": "{app_id}/insights/page_post_engagements/week"
          }
       ],
       "paging": {
              "previous": "{paging_url_with_token}",
              "next": "{paging_url_with_token}"
           }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题