How to get "daily" video views Facebook API? - facebook

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?

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.

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

Related

Facebook Marketing API returns error on "breakdowns call"

I'm trying to make an API call with the folowing URL
act_XXXXXXXX/insights?fields=ad_id,clicks,unique_clicks,impressions,reach,spend,date_start,date_stop,actions,action_values,unique_actions,account_id&level=ad&breakdowns=platform_position&time_range={"since":"2019-09-07","until":"2019-09-07"}
and the response is :
{
"error": {
"message": "(#100) Current combination of data breakdown columns (action_type, platform_position) is invalid ",
"type": "OAuthException",
"code": 100,
"fbtrace_id": "XXXXXXXXXXXXXXXXXXXX"
}
}
the reason is in "breakdown" value = "platform_position"
but the problem is that I need exactly that breakdown :(
when I do an API call and change "breakdown" in URL to something else, for example to "publisher_platform":
act_XXXXXXX/insights?fields=ad_id,clicks,unique_clicks,impressions,reach,spend,date_start,date_stop,actions,action_values,unique_actions,account_id&level=ad&breakdowns=publisher_platform&time_range={"since":"2019-09-07","until":"2019-09-07"}
it is OK and responds with data.
I don't know why it happens because I'm not specifying any "action_type" breakdown but it tells that I am :(
What I've tried:
I tried to remove all the parameters from that URL one by one but it still returns an error
The main questions are: why the API call for "breakdown" value = "platform_position" fails and how to make it work.
Thanks.
PS: you can use FB API testing tool to test requests to FB api.
I'm using API version v8.0
You will need both publisher and position:
breakdowns=publisher_platform,platform_position

Facebook Places API incomplete & incorrect response

My app is having issues with getting facebook page data: "rating_count" & "overall_star_rating". The issue have been found about 20 hours ago, between 2 am UTC to 6 am UTC 12th of October, so probably it's on Facebook side.
For example, request to all graph versions older 2.8 would same response:
https://graph.facebook.com/v2.8/pageID?fields=overall_star_rating,rating_count&access_token=YourToken
{
"overall_star_rating": 0,
"rating_count": 0,
"id": "pageID"
}
Look at zero counts - blank for all the pages. It doesn't matter what token you're using - app/user/page one, response is the same.
Requesting newer API versions (v2.10 & v2.9) give similar, but worth (incomplete) response:
{
"rating_count": 0,
"id": "pageID"
}
Both counts, "overall_star_rating" & "rating_count" are valid according to documentation.
Did anyone experience the issue? How did you fix it?
BTW, if you request "ratings" along with counts, but you'll have to use Page Access Token for that, you'll get an array of reviews of the page, but zero stats. I'm thinking of counting reviews & calculating rating based on them, but this way is wrong for many reasons.

Getting active time from Google Fit Rest API

I am struggling with matching the "active time" returned by Fit REST API with reality.
As an example - on 12/14 I had two walks, about 45 minutes each. The api returns one of them as type 7 ("walking" - right!) and another one as type 0 (in vehicle - wrong!). However, Fit app shows both as "walking", so it apparently uses a different data source.
I checked some other days and on these days, the session with type 0 is indeed a valid "in vehicle" session.
I tried all aggregated data sources that return com.google.activity.segment. Most of them are empty, I've found data only in merge_activity_segments and platform_activity_segments (and they seem to be identical).
Google's docs have a caveat about delay in data sync, but they never specified how long this delay is. The data I am looking at is about 24 hours old - if their sync is that slow, then this API is more or less unusable.
I am using the following POST to https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate
{
"aggregateBy": [
{
"dataSourceId": "derived:com.google.activity.segment:com.google.android.gms:merge_activity_segments"
}
],
"endTimeMillis": "1481788800000",
"startTimeMillis": "1481702400000",
"bucketByTime": {
"period": {
"timeZoneId": "America/Los_Angeles",
"type": "day",
"value": 1
}
}
}
For reference - activity types: https://developers.google.com/fit/rest/v1/reference/activity-types
Has anyone been able to retrieve activity time from Fit's REST API that is correct? Any suggestions?
By the way, steps and calories seem to work fine - just aggregate the following datasets:derived:com.google.calories.expended:com.google.android.gms:merge_calories_expended and derived:com.google.step_count.delta:com.google.android.gms:estimated_steps
A side note - it is probably the worst documented API from a major company I have seen.

facebook graph api /me/achievements returns unexpected results (empty data arrays with low limits)

I have 5 test achievements in my app. first,second,third,first_login,recurring_login. After a specific action an achievement is unlocked. The recurring_login achievement gets deleted and re-added every subsequent login after first_login. I do this to log the players last login to be able to use this information in their friends apps.
When I use the graph_api and collect me/achievements I have 2 problems:
1: when I use the graph-api-explorer scope I can see all my achievements
when I use the app scope I can not see the recurring_login achievement (I have seen it here some time ago, but it no longer apears).
2: when I search for me/achievements in graph-api-explorer scope I getthe recurring_login achievement when using limit=1 to limit=145
I get 2 achievents when using limit=146, 3 with 147 up to 5 at 149
When using app scope I get 0 achievements up to limit=147, 1 at 148 up to 4 at 151. recurring_login is never shown.
note recurring_login is correctly owned by app_scope as checked with application_id in graph-api-explorer scope searches.
It seems that perhaps the achievement is incorrectly removed and not overridden, but I have no clue how to find this out. Can someone solve this mystery?
Edit:
If i try to delete recurring_login in app scope (cannot be done in explorer scope) I get the following message:
{
"error": {
"message": "(#3404) User does not have requested achievement",
"type": "OAuthException",
"code": 3404
}
}
When I try to post it in stead (just change delete to post with exact same achievement=url) I get:
{
"error": {
"message": "(#3501) User has already earned this achievement.",
"type": "OAuthException",
"code": 3501
}
}
Confirmed bug by facebook, given a low priority it probably wont be fixed soon.

How to set time_ranges for Ad Report Stats?

https://developers.facebook.com/docs/reference/ads-api/adreportstats/
Based on the documentation
time_ranges Array of timestamp objects {time_start, time_stop} or date objects {day, month, year}.
So in the Graph API Explorer, I am setting the time_ranges value as
time_ranges=[{"day_start":{"day":1,"month":11,"year":2013}},{"day_stop":{"day":10,"month":11,"year":2013}}]
This returns me an error
{
"error": {
"message": "(#100) Please set time_start and time_stop or day_start and day_stop.",
"type": "OAuthException",
"code": 100
}
}
I have removed other part of the above URL for brevity. I have tried with date_preset value and the report returns data.
Thanks.
EDIT: The complete URL (sans the valid account id)
https://graph.facebook.com/act_12345/reportstats?data_columns=["account_id","account_name","campaign_id","campaign_name","impressions","clicks","spend"]&time_ranges=["{'day_start':{'day':1,'month':11,'year':2013}}","{'day_stop':{'day':10,'month':11,'year':2013}}"]
I had (have!) an identical issue.
I worked around it by using time_start and time_stop instead of day_start and day_stop. Stupid, I know... but it's what worked for me.
So to get 1-day's worth of data (the 19th of Nov for example):
&time_ranges=["{'time_start': '2013-11-19','time_stop':'2013-11-20'}"]
Oddly enough, the example appears to be an array of strings that are json objects. So try changing this line:
time_ranges=[{"day_start":{"day":1,"month":11,"year":2013}},{"day_stop":{"day":10,"month":11,"year":2013}}]
to this:
time_ranges=["{'day_start':{'day':1,'month':11,'year':2013}}","{'day_stop':{'day':10,'month':11,'year':2013}}"]
I had the same problem, try using:
time_ranges=["{'day_start':{'day':1,'month':11,'year':2013}, 'day_stop':{'day':10,'month':11,'year':2013}}"]
instead of:
time_ranges=["{'day_start':{'day':1,'month':11,'year':2013}}","{'day_stop':{'day':10,'month':11,'year':2013}}"]
Complete URL:
https://graph.facebook.com/act_12345/reportstats?data_columns=["account_id","account_name","campaign_id","campaign_name","impressions","clicks","spend"]&time_ranges=["{'day_start':{'day':1,'month':11,'year':2013}, 'day_stop':{'day':10,'month':11,'year':2013}}"]
I opened a documentation issue at https://developers.facebook.com/x/bugs/1423346704577387/