How To Get All Active Campaigns, Adsets, and Ads Starting Today Facebook Marketing/Graph API - facebook

I am trying to query Facebook for all campaign, asset, and ad id's with their name and current spend, and whose start date is today. My current GET request inside of Graph API looks like this:
act_xxxxxxx/ads?fields=name,campaign_id,adset_id,insights{spend}&filtering=[{'field':'adset.start_time','operator':'GREATER_THAN','value':'1667448000'},{'field':'adset.start_time','operator':'LESS_THAN','value':'1667534400'}]&limit=1000&date_preset=today
Which which facebook returns
{"data": [ { "name": "Ad Name","campaign_id": "xxxxxx", "adset_id": "xxxxxxx", "id": "xxxxxxx"}...
I can confirm that the number of ads returned is equal to the number of active ads I have starting in that account today.
However, if you notice, there are no insights included, despite the fact all campaigns have spent hundreds today already.
In Graph API Explorer, I see this:
As you can see insights are greyed out, meaning the field is empty or disallowed.
I'm positive I have the proper permission on the ad account because when I remove the filtering within the query it returns spend values from prior campaigns (I can even set the start time minimum to yesterday rather than this morning at midnight and it will return yesterday's campaigns with spend).
So the issue here seems to be filtering, but I'm obviously not 100% sure, any and all help appreciated. Thanks!

Related

Facebook Marketing API : Get results and cost per result of a Campaign

I'm creating an application (using Facebook Business Java SDK) that can get all infomation of campaigns own by an Ad Account. Almost of my campaigns have same objective is APP_INSTALLS. I want to know how many time my app was installed, so I tried to request infomation like "Results" column and "Cost per Result" column in Adsmanager board :
Adsmanager colums
But when I added "results" and "cost_per_result" to APIRequestGetInsights's request fields, I got a FailRequestException :
"{"error":{"message":"(#100) results, cost_per_result are not valid for fields param. please check https://developers.facebook.com/docs/marketing-api/reference/ads-insights/ for all valid values","type":"OAuthException","code":100,"fbtrace_id":"DnPCKyIGIqs"}}"
I used an user access token with permissions below :
- read_insights
- manage_pages
- pages_show_list
- publish_pages
- ads_management
- ads_read
- business_management
I searched on google many times but nothing is useful. So someone here please tell me how to get "Results" and "Cost per Result" info of a campaign by Facebook api?
As said in the comment, this information used to be available in the field cost_per_action_type, you could retrieve the value in the item where ad['action_type'] == 'mobile_app_install'.
Unfortunately, this is no longer available in 3.2, but you can calculate this value yourself:
Make the query including the actions and spend fields.
You'll find the total number of installs (in the item with the action_type == 'mobile_app_install'), then you can calculate the cost per install with cost_per_install = spend / install_count.

Facebook graph api me/events returns blank

I'm using the Graph API Explorer and have given permissions for user_posts, user_likes & user_events.
me/posts and me/likes return non-empty data, but me/events returns this:
{
"data": [
]
}
I do have ongoing and upcoming events (interested) and they are visible on my Facebook profile.
Any idea what's going wrong? Or is it a Facebook bug?
https://developers.facebook.com/blog/post/2018/04/04/facebook-api-platform-product-changes
Testing of our more robust process starts today and the new process
should resume in a few weeks, but apps currently accessing Events and
Groups APIs will lose access today.
This may also be interesting for you: https://developers.facebook.com/docs/graph-api/changelog/breaking-changes#events-4-4

Getting Facebook Page Posts by Date period using Graph API

I want to get page posts by filtering date using Facebook Graph API, but it lets me filter only one week. For example, if today is 2017-05-01, I can get page posts between 2017-04-23 -> 2017-05-01 but I can get page posts between 2017-01-01 to 2017-04-01. Moreover,I've tried using :
me?fields=feed.since(2017-01-01).until(2017-04-01)
and the result I've got is
{
"id": "XXXXXXXXXXX"
}
Could you please show me how to get past page posts by using date period? Thanks!
Use a User Token, and grants the 'user_posts' permission.
You must change the call to add explicitly the fields that you want to retrieve. Ex. "me/feed?fields=message,description...."

Retrieve Facebook Insights older than 3 months with Graph API

I am using Facebook Graph API to retrive page insights information. My issue is that I can't get the ones older than 3 months. For example if I use /insights/page_fan_adds/day?since=2013-01-20&until=2013-04-23 is all working fine but if I use /insights/page_fan_adds/day?since=2013-01-19&until=2013-04-23 (one day before the previous since) I receive the following error:
{
"error": {
"message": "Unsupported operation",
"type": "FacebookApiException",
"code": 100
}
}
This means is not possible to get older information from the facebook page insights or I have to do something else like for example use some permissions or use other syntax?
Facebook will not allow more than 89 days, but you can adjust the since/until fields to get data older than 89 days.
Facebook will return up to 93 days of insights data. You can verify this for yourself by experimenting with the since and until fields.
If you need more than 93 days, as suggested in another answer, you'll have to modify the since and until fields to get different "windows" of data. Note that in the data returned by Facebook, paging URLs are included for your convenience. You can use them instead of reconstructing the URLs if you so desire.

Facebook graph API 'friends' request now only returning 25 friends per page? What's going on?

My application(game) has been running on Facebook for some time. I start by requesting a friends list via the graph API call: my-uid/friends
asking for user name and profile pic.
Normally I get back a list of all my friends up to a few thousand, until it starts putting friends on to the next page. Which is nice as most of my users get friends in 1 call.
Suddenly however, and with no changes to the app. about 40 minutes ago (18:40 Tuesday (PDT) - 2nd May 2012) I started getting responses with only 25 friends per 'page'!
I can still get my whole friends list using multiple calls, but the game is not currently set up to do that properly. Can anyone tell me why the sudden change? Anyone else seen similar problems and how do I get the list to give me up to 5000 friends per page like it used to.
Reproducible using the Graph API Explorer
I don't know what else to tell you; perhaps the default number returned has changed, but when I try, a call to /me/friends?limit=5000 returns the full list for me (but my friends list is >500 and < 1000 , so maybe it cuts off somewhere along the way)
(Side note: the average number of friends has been found to be ~190 so presumably most users will have less than 500 anyway, and having to page above 500 would be an edge case
In SDK 4.7 you need to pass in a bundle with the number of friends you want to return, I have set it to 5000 as this is the maximum number of friends you can have on Facebook.
You also need to set up your app as a game in the facebook dev console and use invitable friends to get a full friends list
Create your bundle
Bundle bundle = new Bundle();
Add params to your bundle
bundle.putInt("limit", 5000);
Then pass it in to your GraphRequest
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/invitable_friends",
bundle,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
//Do something with the response
}
}
).executeAsync();
It seems that facebook changed its limit to 25 results in other api calls too (feed, posts, friends, etc), if you request friends without parameters the JSON response shows the following:
"paging": {
"next": "https://graph.facebook.com/USER_ID/friends?format=json&limit=25&offset=25&__after_id=LAST_ID"
}
Anyway you could/should always set limit & offset parameters to prevent this kind of things, limit = 0 will return all your friends list.
https://graph.facebook.com/USER_ID/friends?limit=0
If you are only requesting friends from a normal user the maximum number allowed is 5,000 so the limit should could be either 0 or 5,000 if you are requesting info from a facebook page or other kind of api calls like posts or feed this limit could increase or decrease.
(Update) Facebook fixed this bug so setting limit to 0 returns 0 friends, you should set a positive limit, thanks Dinuz
I think the best thing you can do is to add limit=5000 parameter as Igy says.
However I posted a bug report since this change wasn't noticed or described in the document.
The number of results returned from the /v2.2/me/friends endpoint now defaults to 25.
Friend list now only returns friends who also use your app: The list of friends returned via the /me/friends endpoint is now limited to the list of friends that have authorized your app.
See Facebook changes
Facebook API change log
If you are using GraphRequest() (e.g. in React Native), you can put it directly in the string field, like so :
new GraphRequest(
'/me',
{
accessToken,
parameters: {
fields: {
string: 'id,email,first_name,last_name,friends.limit(5000)'
}
}
...