简体   繁体   English

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

[英]Facebook Graph URL to get Page Insights Data

I am trying to build an analytics dashboard and get the page views, engagements, and reach for a Facebook page. 我正在尝试构建分析仪表板并获取Facebook页面的页面查看,约定和覆盖面。 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. 我没有找到一种方法来访问图表api上的read_insights与我们的应用程序和管理员用户(请参阅此问题: Facebook(#100)尝试访问节点类型(页面)上的不存在的字段(read_insights) ),但我已经能够使用file_get_contents直接访问某些信息并通过它运行URL。 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: 我已运行此网址以获取洞察力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. 我已经尝试在数组中运行随附的URL,但它只是给出了Oauth错误。

The way I got this URL initially was following these steps and replacing feed with insights/{metric_data} 我最初获取此网址的方式是遵循以下步骤并使用数据洞察替换Feed / {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=" “转到https://developers.facebook.com/tools/access_token/ 。当你复制它时,你只需直接去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. 旁注:对于新手来说,Facebook Graph API很难解释,所以我在PHP中放弃了身份验证和令牌方法,并试图获取我的数据的直接URL。 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. 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 为此,请按照另一个问题上的指南: 服务器的持久FB访问令牌来提取FB页面信息

Once you have this token you can string together a URL to get any insights data you need. 获得此令牌后,您可以将URL串在一起以获取所需的任何洞察数据。 For example, to get page_views_total for the past week: 例如,要获取过去一周的page_views_total:

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: 这应该返回一个数组,如果使用PHP,你可以获取此URL上的文件内容,并使用json_decode将其作为对象读取:

$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. 我发现第一个值与我在Insights仪表板上看到的值相匹配。 I am not sure why two different values are returned with different time codes. 我不确定为什么返回两个不同的值与不同的时间码。

You can change week to day or days_28. 您可以每周或每天更改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. 使用此方法,您无法为每个Insight指定时间(周,日,天_28)。 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}"
       }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM