How to get Facebook Ads UTM - facebook

I see that when creating an ad, I can define UTM Tags with the endpoint URL_Tags
=> https://developers.facebook.com/docs/marketing-api/reference/ad-creative/
But if I want to track the performance I am not able to get back the UTM
=> https://developers.facebook.com/docs/marketing-api/insights/parameters
I know that super-metrics are able to get the URL Tags... there is no reason that they can and not the US.
=> https://supermetrics.com/api/getFields?ds=FA&fieldType=dim
If I am reading well, facebook API insight is not able to get me the URL Tags. But is there a workaround? Maybe playing with Facebook ads create the function... a read after write method or something like that?

Try this,
1).The UTM tags + Facebook ads article explains clearly on Standardizing a UTM protocol
2). For more on Track, FB Ads refer this article
3). Get the UTM tags with Facebook Marketing API refer

from facebook_business.adobjects.adaccount import AdAccount
from facebook_business.adobjects.adcreative import AdCreative
from facebook_business.api import FacebookAdsApi
access_token = '<ACCESS_TOKEN>'
app_secret = '<APP_SECRET>'
app_id = '<APP_ID>'
id = '<ID>'
FacebookAdsApi.init(access_token=access_token)
fields = [
'name',
]
params = {
}
print AdAccount(id).get_ad_creatives(
fields=fields
)
Just replace your relevant details and add url_tags as a field under fields. You will be able to access all UTM params for every creative under your account.

Related

How to get Instagram post URL from Facebook Graph media_id

I'm using Instagram Mentions API and was able to obtain FB media_id for couple Instagram posts.
I need media_id to get post content via /user/mentioned_media. Unfortunately that endpoint does not provide ig_id or shortcode or permalink which I need.
Is there a way to get Instagram Post URL with FB Graph API media_id?
It seems like you can get media metadata (and all comments/replies) when someone #-mentions you in a non-owned-media comment by using a query like below.
You would use your own instagram business account id for the first number (the path component), and you'd use the comment_id you received from the webhook comment-mention notification as the second number:
/12345678901234567890?fields=mentioned_comment.comment_id(9876543210987654321){id,text,username,timestamp,media{id,media_type,media_url,permalink,username,like_count,comments_count,comments{id,text,username,user{id,username},like_count,timestamp,replies{id,text,username,user{id,username},like_count,timestamp}}}}
Yes, there is a way you can get Instagram post URL associated with a media if you know the media id.
You can also get more information (all its fields and edges) on media. See the docs here
You can get the Instagram post URL by making a GET request to /{ig-media-id}
Sample Request:
GET https://graph.facebook.com/v15.0/{{media_id}}?fields=permalink, shortcode&access_token={{ACCESS_TOKEN}}
Sample Response:
{
"permalink": "https://www.instagram.com/p/Clxsi5Pol2_/",
"shortcode": "Clxsi5Pol2_",
"id": "17xxx59557977xxx"
}
The value of the permalink is the valid public URL for the post.
You can also use the shortcode returned to construct the URL by concatenating the shortcode to https://www.instagram.com/p/.

How to get the Ad final url with the Facebook insights API

I'm trying to get an ad final url using the python sdk.
I tried all the fields but I can't manage to find one which returns the ad url. At the moment my fields are:
params = {'access_token':creds.refresh_token,
'fields':'action_values, actions, ad_name, adset_name, objective, outbound_clicks, outbound_clicks_ctr, call_to_action_clicks, campaign_name, cost_per_unique_click, cpc, cpm, spend, website_ctr, date_start, date_stop',
'action_breakdowns':['action_link_click_destination'],
'date_presets':'last_year',
# 'time_range':" since:2017-06-01, until:2017-01-03}"
}
ad_insights = requests.get("https://graph.facebook.com/v2.10/{}/insights".format(ad['id']), params=params)
I understand that some ads won't have a final url, like boosting posts, but many ads are only pointing to a site, how can I get the "call to action url" ?
You have to pull in the adCreative for each ad that AdInsights reports on. The adCreative will contain the outbound link.
See here https://developers.facebook.com/docs/marketing-api/reference/ad-creative
The following fields may be of interest to you
object_url
url_tags

Identifying a Facebook user id from a url?

I've been playing around with the Facebook graphs api for a while, and to the best of my knowledge you shouldn't be able to get a Facebook user's id from their username.
I then came accross stalkscan.com and note that they do just that. You provide a Facebook url and the resulting queries contain that usernames profile id.
How can I acheive a similar result with or without the graphs api?
You can get a facebook user id by using the following endpoint:
URL /?id={url}
{url} represents an external URL as it relates to the Facebook social graph - shares and comments from the URL on Facebook, and any Open Graph objects associated with the URL. (e.g. a user, a page...).
With Python, you can use something like this:
import requests
USER_URL = '' # your link
ACCESS_TOKEN = '' # your credentials
params= {'id': USER_URL,'access_token': ACCESS_TOKEN}
fb_graph = "https://graph.facebook.com/v2.7/"
r = requests.get(fb_graph, params=params)
fb_id = r.json().get('id')
print('Facebook User ID: %s' % fb_id)
Full documentation of the graph API is available here.

Facebook Graph API - Access token for page likes, name and url

I'm trying to update some old PHP code which is supposed to get the name, likes and a link to the given Facebook page. This is the current old code:
$fbsite1 = json_decode(file_get_contents('http://graph.facebook.com/page1'));
$fbsite2 = json_decode(file_get_contents('http://graph.facebook.com/page2'));
$fbsite3 = json_decode(file_get_contents('https://graph.facebook.com/page3'));
for($j=1; $j<=3; $j++){
$data[] = array(
'name' => ${'fbsite'.$j}->name,
'likes' => ${'fbsite'.$j}->likes,
'link' => ${'fbsite'.$j}->link
);
}
The problem is that this method requires an access token, which I'm not quite sure how to get. I've looked at the Facebook API reference, but there seems to be a few different access tokens (different permission etc.). Which one do I need to accomplish this? How do I get it?
If the pages are public, then you only need an app access token.
So create an app in the FB dev section (if you have not done so already), and then include your app access token in those request URLs:
http://graph.facebook.com/page1?access_token=…
FYI, you should consider requesting all that data in one go, instead of making multiple API requests. You can do that using this syntax (for up to 50 ids in one request),
http://graph.facebook.com/?ids=page1,page2,page3&access_token=…
And you will have to explicitly ask for the name, link and likes fields now (otherwise you will get name and id only):
http://graph.facebook.com/?ids=page1,page2,page3&fields=name,likes,link&access_token=…

How to get all facebook pages id associated with my account?

I want to get the statistics of all the pages associated with my account on facebook, i have the code to get the statistics of a page, i just want to get the page id of all the pages associated with my account, how can i do this?
Please help.
Thanks.
You'll need to request the manage_pages permission (more details here), once you have the aquired the permission, all you have to do is make a request to :
https://graph.facebook.com/YOUR_FACEBOOK_ID/accounts.
The return value will be a JSON with all the pages/applications that are associated with your account.
You can also use this great tool that facebook provides - The Graph API Explorer (/me/accounts) to explore what other information you can retrieve without having to write a single line of code :P very useful.
FB.api('/me/accounts', function(response) {
for (id in response.data)
{
page = data[id];
page.id;
page.name;
}
});
You can use Javascript API to do this, read more :http://developers.facebook.com/docs/reference/javascript/