Include Adset details Insights query - facebook-graph-api

Using the Facebook Marketing API (Graph API), I use the following (simplified for brevity) query:
act_xxxxxx/insights?level=adset&fields=date_start,date_stop,clicks,adset_name
This gives me a result like this:
{
"data": [
{
"date_start": "2019-01-01",
"date_stop": "2019-01-30",
"clicks": "999",
"adset_name": "Awesome Adset"
}
] //paging info removed, not relevant
}
For each node in data, I would like to include some properties from the adset, like end_time,start_time,lifetime_budget,daily_budget.
I have looked into expanding fields, but there does not seem to be an adset field I can expand on here.
Is there a way to include adset fields like lifetime_budget, so I can get the results in one go?
Right now, query adsets separately and join afterwards, but I think there must be a way to get it in one resultset.

You could query adsets and ask for all the fields you need adding with the parenthesis notation each kind of params you need, as example, for ask about all the adsets with budgets info and bid strategy of an adAccount with insights with specified metrics, you can use the following:
act_XXXX/adsets?fields=insights{clicks,spend,cpc},lifetime_budget,daily_budget,name,start_time,end_time
Hope this help

Related

How to run a combination of query and filter in elasticsearch?

I am experimenting using elasticsearch in a dummy project in django. I am attempting to make a search page using django-elasticsearch-dsl. The user may provide a title, summary and a score to search for. The search should match all the information given by the user, but if the user does not provide any info about something, this should be skipped.
I am running the following code to search for all the values.
client = Elasticsearch()
s = Search().using(client).query("match", title=title_value)\
.query("match", summary=summary_value)\
.filter('range', score={'gt': scorefrom_value, 'lte': scoreto_value})
When I have a value for all the fields then the search works correctly, but if for example I do not provide a value for the summary_value, although I am expecting the search to continue searching for the rest of the values, the result is that it comes up with nothing as a result.
Is there some value that the fields should have by default in case the user does not provide a value? Or how should I approach this?
UPDATE 1
I tried using the following, but it returns every time no matter the input i am giving the same results.
s = Search(using=client)
if title:
s.query("match", title=title)
if summary:
s.query("match", summary=summary)
response = s.execute()
UPDATE 2
I can print using the to_dict().
if it is like the following then s is empty
s = Search(using=client)
s.query("match", title=title)
if it is like this
s = Search(using=client).query("match", title=title)
then it works properly but still if i add s.query("match", summary=summary) it does nothing.
You need to assign back into s:
if title:
s = s.query("match", title=title)
if summary:
s = s.query("match", summary=summary)
I can see in the Search example that django-elasticsearch-dsl lets you apply aggregations after a search so...
How about "staging" your search? I can think if the following:
#first, declare the Search object
s = Search(using=client, index="my-index")
#if parameter1 exists
if parameter1:
s.filter("term", field1= parameter1)
#if parameter2 exists
if parameter2:
s.query("match", field=parameter2)
Do the same for all your parameters (with the needed method for each) so only the ones that exist will appear in your query. At the end just run
response = s.execute()
and everything should work as you want :D
I would recommend you to use the Python ES Client. It lets you manage multiple things related to your cluster: set mappings, health checks, do queries, etc.
In its method .search(), the body parameter is where you send your query as you normally would run it ({"query"...}). Check the Usage example.
Now, for your particular case, you can have a template of your query stored in a variable. You first start with, let's say, an "empty query" only with filter, just like:
query = {
"query":{
"bool":{
"filter":[
]
}
}
}
From here, you now can build your query from the parameters you have.
This is:
#This would look a little messy, but it's useful ;)
#if parameter1 is not None or emtpy
#(change the if statement for your particular case)
if parameter1:
query["query"]["bool"]["filter"].append({"term": {"field1": parameter1}})
Do the same for all your parameters (for strings, use "term", for ranges use "range" as usual) and send the query in the .search()'s body parameter and it should work as you want.
Hope this is helpful! :D

How to get Zomato restaurant ID using restaurant link?

I want to get details of a restaurant in Zomato. I have it's link as the input (https://www.zomato.com/mumbai/fantasy-the-cake-shop-kalyan?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1). By browsing the documentation of Zomato APIs, I didn't found a way to get it.
I tried searching for the restaurant using search API but it returns many results.
Any help will be appreciated
It's a two-step process:
Find out restaurant's city id, in your case, Mumbai's city id through the /cities API. It's a simple query search.
Use the city id from the above API call in the /search API, like, https://developers.zomato.com/api/v2.1/search?entity_type=city&entity_id=3&q=fantasy%20the%20cake%20shop%20kalyan
This would give all the basic information about a restaurant.
View the page's source and search for window.RES_ID
I had the same issue as you described. This Zomato's API approach is at least odd. It's almost immposible to GET any information about restaurant if you don't know res_id in advance and that's not possible to parse since Zomato will deny access.
This worked for me:
Obtain user-key from Zomato API Credentials (https://developers.zomato.com/api)
Search restaurant url via API (https://developers.zomato.com/api/v2.1/search?entity_id=84&entity_type=city&q=RESTAURANT_URL&category=7%2C9). The more specific you will be, the better results you'll get (This url is specified by city to Prague (ID = 84) and categories Daily menus (ID = 7) and Lunch (ID = 9). If there is possibility do specify city, category or cuisine, it helps, but should't be necessary. Don't forget to define GET method in headers.
Loop or filter through json results and search for the wanted url. You might need to use method valueOf() to search for the same url. Be careful, you might need to add "?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1" at the end of your wanted url so it has the same format. Check that through Zomato API Documentation page.
for (i in body.restaurants) {
var url_wanted = restaurant_url + '?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1'
var url_in_json = body.restaurants[i].restaurant.url;
if (url_wanted.valueOf() == url_in_json.valueOf()) {
var restaurant_id = body.restaurants[i].restaurant.id;
}
console.log('Voala! We have res_id:' + restaurant_id);
}
There you have it. It could be easier though.
Hope it helps!
once you have the url of the rseraunt's page you can simply look for a javascript object attribute named "window.RES_ID" and further use it in the api call.

Can creationTime and other Directory meta-fields be used in a query?

I'm trying to filter the list of users returned from Directory.Users.List, and want to use the creationTime value in the filter. According to this:
Search for users
...you can use Date fields in comparisons, however when I try something like:
creationTime>=2016-06-30 (or the same value quoted)
...I get:
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Invalid Input: creationTime>=2016-06-30",
"reason" : "invalid"
} ],
"message" : "Invalid Input: creationTime>=2016-06-30"
}
The message isn't particularly helpful - is this a syntax issue with the query, or is it the case that the field isn't available for query (it isn't listed in that article, but it is part of the JSON response)?
The article specifies the format for Date field queries, however this field also includes a time component, so I tried to also replicate the exact format that shows in the JSON, with the same result.
Also, same result in the Try it! section of the Users reference page.
Also also, tried using a numeric value (Date.getTime(), in effect) - same error.
Thanks...
P.S. Interestingly, Google does document how to search on date fields for Chromebooks, and it's entirely different than they imply for users. I still can't use creationTime with this, but it does work for Chromebooks. The other part of this is that it refers to fields that aren't documented, and why I try to use the documented field (e.g. lastSync vs. sync), it fails with the same message. This whole thing seems half-baked.
View Chrome device information
Only the fields listed in the table are searchable via the users.list search.
A workaround (I don't know if this is suitable for you) may be to push the data you want to use in a search into custom user fields. These fields are searchable.

Facebook Graph API - search doesn't return a list of groups

I'm not sure if Facebook have made their API like this deliberately or if i'm missing something.
When using the Graph API explorer: https://developers.facebook.com/tools/explorer/
Why does:
search?type=user&q=coffee
return a list of users who have the word coffee in their name
but
search?type=group&q=coffee
returns nothing:
{
"data": [
]
}
Is it impossible to search for a list of groups?
This works:
search?type=group&id=JavaScript.Programming
But what exactly is the point of a search when you already know the name or id of the group!?
I want to return a list of groups that have the word 'coffee' in them.
It seems totally illogical that you can search for users like that but not for groups.
You need to get an Access token and select the permission user_group.

Artist info missing from Facebook Graph API /music.listens endpoint

I'm playing around w/ the /me/music.listens endpoint of Graph API and I have it working just fine. Except I can't seem to figure out how to get actual artist info to come back. I see the song and even the album (though that seems a little inconsistent too). But never any artist info.
Check the developer explorer here: https://developers.facebook.com/tools/explorer/?method=GET&path=me%2Fmusic.listens
No artist info. Is this just not returned? I can't see how to specify this in a fields param list. FB's documentation of the actions is spotty at best so I figured I'd try here.
Thanks!
I've figured out a work around. It doesn't look like Facebook actually returns artist info. So you have to use the Spotify Lookup Service (http://developer.spotify.com/technologies/web-api/lookup/).
To break it down a little further, you start by pulling the music.listens feed from FB and you'll get info that looks like:
(
[song] => Array
(
[id] => 381659191902248
[url] => http://open.spotify.com/track/**2Vv27Gc5k5GgGW9jgtj1CS**
[type] => music.song
[title] => Follow Through
)
)
From there, you need to grab the bolded track ID.
Lastly, fetch the song metadata with this call to Spotify: http://ws.spotify.com/lookup/1/.json?uri=spotify:track:2Vv27Gc5k5GgGW9jgtj1CS
You'll be returned the album name, artist info, etc.
If someone knows a better way, lemme know!
You can retrieve artists informations on graph API with the song id :
https://graph.facebook.com/song_id?fields=data{musician}
ex : https://graph.facebook.com/697975930266172?fields=data{musician}
Old question I know, but you can retrieve related information such as both the song title and artist title in the same request using Graph API's 'field expansion'.
For example - Last 10 songs
me?fields=music.listens.limit(10){data{song{title,id},musician{id,title}}}
To retrieve more information from either the song or musician entities just tweak the fields requested.
Note, this requires user_actions.music permission