Search code examples
facebookfacebook-graph-apifacebook-insights

How to get "daily" video views Facebook API?


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?


Solution

  • 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