24492

Facebook Marketing API: retrieving metadata for many Ads via Python

I hope, someone has stumbled over the same issue and might guide me towards a simple solution for my problem.

I want to retrieve regularly some data regarding my Ads on Facebook. Basically, I just want to store some metadata in one of my databases for further reporting purposes. Thus, I want to get AD-ID, AD-name and corresponding ADSET-ID for all my Ads.

I have written this small function in Python:

def get_ad_stats(ad_account): """ Pull basic stats for all ads Args: 'ad_account' is the Facebook AdAccount object Returns: 'fb_ads', a list with basic values """ fb_ads = [] fb_fields = [ Ad.Field.id, Ad.Field.name, Ad.Field.adset_id, Ad.Field.created_time, ] fb_params = { 'date_preset': 'last_14_days', } for ad in ad_account.get_ads(fields = fb_fields, params = fb_params): fb_ads.append({ 'id': ad[Ad.Field.id], 'name': ad[Ad.Field.name], 'adset_id': ad[Ad.Field.adset_id], 'created_time': datetime.datetime.strptime(ad[Ad.Field.created_time], "%Y-%m-%dT%H:%M:%S+0000"), }) return (fb_ads)

Similar functions for Campaign- and AdSet-data work fine. But for Ads I am always reaching a user request limit: "(#17) User request limit reached".

I do have an API-access level of "BASIC" and we're talking here about 12,000 Ads. And, unfortunately, async-calls seem to work only for the Insights-edge.

Is there a way to avoid the user request limit, e.g. by limiting the API-request to only those Ads which have been changed/newly created after a specific date or so?

Answer1:

Ok, sacrificing the 'created_time' field, I have realized I could use the Insights-edge for that. Here is a revised code for the same function which is now using async-calls and a delay between calls:

def get_ad_stats(ad_account): """ Pull basic stats for all ads Args: 'ad_account' is the Facebook AdAccount object Returns: 'fb_ads', a list with basic values """ fb_ads = [] fb_params = { 'date_preset': 'last_14_days', 'level': 'ad', } fb_fields = [ 'ad_id', 'ad_name', 'adset_id', ] async_job = ad_account.get_insights(fields = fb_fields, params = fb_params, async=True) async_job.remote_read() while async_job['async_percent_completion'] < 100: time.sleep(1) async_job.remote_read() for ad in async_job.get_result(): fb_ads.append({ 'id': ad['ad_id'], 'name': ad['ad_name'], 'adset_id': ad['adset_id'], }) return (fb_ads)

Recommend

  • Facebook Marketing API: Potential reach of a Facebook campaign
  • Refresh activity/ImageView?
  • Would using jQuery to load elements affect SEO ranking?
  • Jackson Json List inside object
  • FineUploader: Add/change validation rules on onValidate/onValidateBatch callback
  • Google Play Games y GoogleMobileAds do not work together in Unity 5
  • Admob sending adult content, how to disable this?
  • Admob ads now showing from two weeks
  • Django - using different apps together in a project
  • How to force a tbody element into an empty table for non IE browsers
  • AdMob showing test interstitials in released application
  • UWP trial version and products purchase that make full version with expiration
  • Facebook Marketing API some field are not shown
  • Interstitials not displaying, but impressions being counted
  • Can't scroll webview in Android
  • Admob live ads not appearing in libgdx application - Failed to load ad: 0
  • Microsoft Pubcenter Ads In WPF Application
  • Close button for AdMob banner
  • Pipe in for loop breaks double quoted variables
  • Admob Interstitial Ad showing black with new Ad unit ID
  • Android - VerifyError
  • How do you create a Fuseki SPARQL server using the Apache Jena Java API?
  • jQuery: add elements until a particular height is reached
  • How to Cache Real-time Data?
  • Admob requires api-13 or later can I not deploy on old API-8 phones?
  • Spring Data JPA custom method causing PropertyReferenceException
  • Possible to stop flickering java tooltip in heavyweight mode?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • AT Commands to Send SMS not working in Windows 8.1
  • How do I rollback to a specific git commit
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How do I configure my settings file to work with unit tests?
  • Turn off referential integrity in Derby? is it possible?
  • Add sale price programmatically to product variations
  • Is it possible to post an object from jquery to bottle.py?
  • Checking variable from a different class in C#
  • Reading document lines to the user (python)
  • Unable to use reactive element in my shiny app
  • How do I use LINQ to get all the Items that have a particular SubItem?