2

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.

Cyberio
  • 65
  • 2
  • 11
  • You can not get most of the data the API provides, without proper prior authorization. If you don’t want to bother yourself with implementing login and asking for permissions right now, you can use Graph API Explorer to create tokens. (Make sure to select your own app from the dropdown first.) – CBroe Jun 23 '17 at 07:42

1 Answers1

1
  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}"
       }
}
Cyberio
  • 65
  • 2
  • 11