skip to Main Content

I have a little experience dealing with Facebook Graph API. I need to get daily views for the video as oppose to lifetime views.

FB API docs don’t show this to be an option (lifetime – only period param)
https://developers.facebook.com/docs/graph-api/reference/video/video_insights/

However, I’ve seen another post on SO answering this question, yet for some reasons it doesn’t work for me
(Getting a video views with Facebook API).

This is my successful API call which returns lifetime stats as expected:

/{video_id}/video_insights/total_video_views/lifetime

This is what I thought I should be doing:

/{video_id}/video_insights/total_video_views/day

… but got this error:

{
  "error": {
    "message": "(#100) Invalid parameter",
    "type": "OAuthException",
    "code": 100,
    "error_data": "Daily query is not supported for metric (total_video_views)"
  }
}

Then, as the SO post suggested, I tried different period param:

/{video_id}/video_insights/total_video_views/month

… and got this:

{
  "error": {
    "message": "(#100) Invalid parameter",
    "type": "OAuthException",
    "code": 100,
    "error_data": "Period should be either lifetime or day."
  }
}

… which tells that day should be acceptable param just like lifetime

Eventually, just for fun, I thought I’ll pass “wrong” param – Day:

/{video_id}/video_insights/total_video_views/Day

… and got this:

{
  "error": {
    "message": "(#100) For field 'video_insights': period must be one of the following values: day, week, days_28, month, lifetime",
    "type": "OAuthException",
    "code": 100
  }
}

This states that all of these values are good (day, week, days_28, month, lifetime), yet they don’t work.

I’m really confused here. I saw daily break down for a video views on FB webpage/insights and thought it should be possible to do the same through the API.

What am i doing wrong?

2

Answers


  1. Chosen as BEST ANSWER

    Here is my solution (after running this through FB support)

    basically,

    1) i get list of all posts:

    fields='object_id,type'
        url="https://graph.facebook.com/v3.2/{}?access_token={}&fields={}&limit={}".format(resource_id,access_token,fields,limit)
    while url:
            rec=requests.request(method="GET",url=url)
            content = json.loads(rec.content)
            if "next" in content["paging"]:
                url=content["paging"]["next"]
            else:
                url=False
                break 
    

    2) then, i iterate through each of them (filtering those with videos) and retrieve data for the post which contains this video rather than video itself (which turned out to be not possible anymore with daily grouping)

    insights=[]
    video_post_id=page_id+"_"+video_id
    metric="post_video_views"
    
    url_insight=f"https://graph.facebook.com/v3.2/{video_post_id}/insights?metric={metric}&since={since}&until={until}&pretty=0&access_token={access_token}"
    while url_insight:
        rec=requests.request(method="GET",url=url_insight)
        insight = json.loads(rec.content)
        if "next" in insight["paging"]:
            url_insight=insight["paging"]["next"]
        else:
            url_insight=False
            break
    

    this worked for me


  2. I encountered the same problem shortly after your post. All of my testing and research has led me to believe Facebook has removed the ability to retrieve anything other than lifetime. After reading all of the Change Log for the last few releases, I don’t see any evidence of this change being documented by Facebook. This is obviously very disappointing.

    The API responses clearly indicate “day” should be a valid period, which leads me to believe it was hastily removed.

    Unfortunately I do not see a reliable workaround. One may be able to achieve an approximation of daily by retrieving lifetime every 24 hours and calculating the difference between the two. This would only be an approximation though since there could be data reconciliation issues for a given time period.

    It is too bad this issue didn’t get any traction because it is core to the Facebook Video Insights API.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search