How to get Facebook post ID from ad using API?


Ryan

I'm very happy with the Facebook Ads reporting tool I built myself.

It lists all campaigns in an ad account, and all ad sets in each campaign, and all ads in each ad set.

It shows the metrics I care about.

But I haven't been able to figure out how to retrieve the post ID from the ad.

It seems that this field is not mentioned in Insights edge ( not available in Insights InsightsFields), so maybe I have to "join" on another API call.

My main code for getting insights is:

public function getInsights($levelType, $id, $aggLevel, $start, $end) {
    if ($levelType) {
        if ($id == null) {
            abort(500, 'You must provide the ID for the object you want to retrieve.');
        }
    } else {
        $levelType = \AdAccount::class;
        $id = ACT_PREPEND . $this->fbConfig['account_id'];
        $aggLevel = AdsInsightsLevelValues::CAMPAIGN;
    }
    $variableClassWithNamespace = '\FacebookAds\Object\\' . $levelType;
    $level = new $variableClassWithNamespace($id);
    $fields = [
        InsightsFields::CAMPAIGN_ID,
        InsightsFields::CAMPAIGN_NAME,
        InsightsFields::ADSET_ID,
        InsightsFields::ADSET_NAME,
        InsightsFields::AD_ID,
        InsightsFields::AD_NAME,
        InsightsFields::SPEND,
        InsightsFields::UNIQUE_IMPRESSIONS,
        InsightsFields::INLINE_LINK_CLICKS,
        InsightsFields::INLINE_LINK_CLICK_CTR,
        InsightsFields::COST_PER_INLINE_LINK_CLICK,
        InsightsFields::ACTIONS,
        InsightsFields::COST_PER_ACTION_TYPE,
        InsightsFields::CPM,
    ];
    $params = [
        AdReportRunFields::LEVEL => $aggLevel,
    ];
    if ($start) {
        $params[AdReportRunFields::TIME_RANGE]['since'] = $start;
        if (!$end) {
            $params[AdReportRunFields::TIME_RANGE]['until'] = (new \DateTime("+2 year"))->format('Y-m-d');
        }
    }
    if ($end) {
        $params[AdReportRunFields::TIME_RANGE]['until'] = $end;
        if (!$start) {
            $params[AdReportRunFields::TIME_RANGE]['since'] = (new \DateTime("-1 year"))->format('Y-m-d');
        }
    }
    if (!$start && !$end) {
        $params[AdReportRunFields::DATE_PRESET] = InsightsPresets::LIFETIME;
    }
    $insights = $level->getInsights($fields, $params);
    return $insights->getResponse()->getBody();
}

So I'm at least willing to answer this simple question: Given an ad ID, how do I retrieve the ID of the "post" to show in that ad, assuming I have all the required API permissions (which I certainly do)?

Ideally, what I would most like to answer is: How do I retrieve that Post ID in one API query when retrieving all the insights I need (as shown above)?

Thanks!

David

The point is, the post ID is the basic ad parameter, not the daily value you get from insights. That's why it can't be found anywhere on the insights endpoint.

Assuming you know all ad IDs, you can query them all at once (there may be some undocumented limit on the number in a single request) like this:

/v2.8/?ids=6068303632680,6074066638080&fields=creative{effective_object_story_id}

In fact, this is a shortcut to execute these two queries

/v2.8/6068303632680?fields=creative{effective_object_story_id}
/v2.8/6074066638080?fields=creative{effective_object_story_id}

You can find more information on advanced API usage in the " Multiple ID Read Requests" section of the FB documentation .

Related


How to get Facebook post ID from ad using API?

Ryan I'm very happy with the Facebook Ads reporting tool I built myself. It lists all campaigns in an ad account, and all ad sets in each campaign, and all ads in each ad set. It shows the metrics I care about. But I haven't been able to figure out how to retr

How to get Facebook post ID from ad using API?

Ryan I'm very happy with the Facebook Ads reporting tool I built myself. It lists all campaigns in an ad account, and all ad sets in each campaign, and all ads in each ad set. It shows the metrics I care about. But I haven't been able to figure out how to retr

How to get Facebook post ID from ad using API?

Ryan I'm very happy with the Facebook Ads reporting tool I built myself. It lists all campaigns in an ad account, and all ad sets in each campaign, and all ads in each ad set. It shows the metrics I care about. But I haven't been able to figure out how to retr

How to get ad final url using facebook insights api

Costantin I am trying to get ad final url using python sdk. I've tried all the fields but can't find one that returns the ad URL. Currently, my fields are: params = {'access_token':creds.refresh_token, 'fields':'action_values, actions, ad_name, adset

How to get ad final url using facebook insights api

Costantin I am trying to get ad final url using python sdk. I've tried all the fields but can't find one that returns the ad URL. Currently, my fields are: params = {'access_token':creds.refresh_token, 'fields':'action_values, actions, ad_name, adset

How to get ad final url using facebook insights api

Costantin I am trying to get ad final url using python sdk. I've tried all the fields but can't find one that returns the ad URL. Currently, my fields are: params = {'access_token':creds.refresh_token, 'fields':'action_values, actions, ad_name, adset

How to get ad final url using facebook insights api

Costantin I am trying to get ad final url using python sdk. I've tried all the fields but can't find one that returns the ad URL. Currently, my fields are: params = {'access_token':creds.refresh_token, 'fields':'action_values, actions, ad_name, adset

How to get ad final url using facebook insights api

Costantin I am trying to get ad final url using python sdk. I've tried all the fields but can't find one that returns the ad URL. Currently, my fields are: params = {'access_token':creds.refresh_token, 'fields':'action_values, actions, ad_name, adset

How to delete a post in Facebook by ID using PHP, Graph API

Hamachi Can someone tell me how to delete my own posts in facebook by id using php , i have searched in google for graph API , but i found nothing :( Thanks in advance Lucien https://developers.facebook.com/docs/graph-api/reference/v2.2/post#deleting important

How to delete a post in Facebook by ID using PHP, Graph API

Hamachi Can someone tell me how to delete my own posts in facebook by id using PHP and the graph API I've searched in google but I can't find anything :( Thanks in advance Lucien https://developers.facebook.com/docs/graph-api/reference/v2.2/post#deleting impor

How to delete a post in Facebook by ID using PHP, Graph API

Hamachi Can someone tell me how to use PHP , I've searched in google for graph API to delete my own posts in facebook by id , but I can't find anything :( Thanks in advance Lucien https://developers.facebook.com/docs/graph-api/reference/v2.2/post#deleting impo

How to delete a post in Facebook by ID using PHP, Graph API

Hamachi Can someone tell me how to delete my own posts in facebook by id using PHP and the graph API I've searched in google but I can't find anything :( Thanks in advance Lucien https://developers.facebook.com/docs/graph-api/reference/v2.2/post#deleting impor

How to delete a post in Facebook by ID using PHP, Graph API

Hamachi Can someone tell me how to use PHP , I've searched in google for graph API to delete my own posts in facebook by id , but I can't find anything :( Thanks in advance Lucien https://developers.facebook.com/docs/graph-api/reference/v2.2/post#deleting impo

How to get the unique ID of an ad from the Google Analytics API

sjovijayan I'm new to google analytics api , is there any way to extract AdId from this api , google adwords api provides per ad's.unique id I've been able to extract ga:adwordsCampaignIDit ga:adwordsAdGroupIDfrom the Google Analytics API. I can't find the opt

How to get the unique ID of an ad from the Google Analytics API

sjovijayan I'm new to google analytics api , is there any way to extract AdId from this api , google adwords api provides per ad's.unique id I've been able to extract ga:adwordsCampaignIDit ga:adwordsAdGroupIDfrom the Google Analytics API. I can't find the opt

How to get the unique ID of an ad from the Google Analytics API

sjovijayan I'm new to google analytics api , is there any way to extract AdId from this api , google adwords api provides per ad's.unique id I've been able to extract ga:adwordsCampaignIDit ga:adwordsAdGroupIDfrom the Google Analytics API. I can't find the opt

Facebook API to get recent post id

Jack Evans I'm currently working on a small PHP "script" that will automatically get the post ID from the latest post on a Facebook page. So far I get: $status = $facebook->api("/645017715510822/feed?fields=id&limit=1&access_token=".$token, 'GET'); When I run

Facebook API to get recent post id

Jack Evans I'm currently working on a small PHP "script" that will automatically get the post ID from the latest post on a Facebook page. So far I get: $status = $facebook->api("/645017715510822/feed?fields=id&limit=1&access_token=".$token, 'GET'); When I run

How to get post id of Wordpress post using Rest API with SwiftUI

Kevin Brown I'm trying to include some blog posts from my WordPress site into my application. I've tested the API with a separate post id for each post and I've got it to load data in my view. However, I'm now trying to get an array and it doesn't seem to get

How to get image with specific id from facebook graph api iOS?

Ashutos In my app, I'm trying to get a specific image from a Facebook page's photo/album. I can get all the images in the NSArray, but how should I query for the specific image I want. Here is my url: @" https://graph.facebook.com/v2.0/albumid/photos " which g

How to get image with specific id from facebook graph api iOS?

Ashutos In my app, I'm trying to get a specific image from a Facebook page's photo/album. I can get all the images in the NSArray, but how should I query for the specific image I want. Here is my url: @" https://graph.facebook.com/v2.0/albumid/photos " which g

How to get image with specific id from facebook graph api iOS?

Ashutos In my app, I'm trying to get a specific image from a Facebook page's photo/album. I can get all the images in the NSArray, but how should I query for the specific image I want. Here is my url: @" https://graph.facebook.com/v2.0/albumid/photos " which g

How do i get the facebook post id?

a7madx7 How to extract post id after posting post using Facebook method Post() or PostTaskAsync() ? I use JSON.Netsomething like this var postObject = await fb.PostTaskAsync("/me/feed", postArgs); dynamic result = JsonConvert.DeserializeObject((

How do i get the facebook post id?

a7madx7 How to extract post id after posting post using Facebook method Post() or PostTaskAsync() ? I use JSON.Netsomething like this var postObject = await fb.PostTaskAsync("/me/feed", postArgs); dynamic result = JsonConvert.DeserializeObject((