Posts tagged with facebook-ads-api

I create a webhook that send the lead to our CRM. Everything works fine: I can retrieve the lead, the form and the page data. Now I need to retrieve which business account create the ads for the leads. The ADS are managed by two different agencies and I need to understand each campaign origin.

I think is something that I can retrieve from the form_id but I don't know how. Could someone help me out on this one? Thank you

Using cURL, I am trying to get list of AdSets/Ad Campaigns that are having errors like "creative fatigue" with the help of Facebook Graph API.

I checked Graph API documentation, but couldn't find it. I also tried checking out API Explorer tool, but couldn't find a way to get this information.

Even ChatGPT and other AIs couldn't be of help as the references they give do not work now, current version is 20, but AIs give v13, v16 and so on.

Following are a couple of API endpoints from the many I have tried:

  1. https://graph.facebook.com/v12.0/<adset_id>?fields=id,name,creative{status}&access_token=<access_token>

  2. https://graph.facebook.com/v13.0/act_{ad_account_id}/adsets?fields=name,creative_status

I am expecting an endpoint that will help me fetch all the adsets or adcampaigns which are having the error of "creative fatigue" and other similar errors.

I am currently using facebook-nodejs-business-sdk:"^18.0.0" graph api for Insights.

I have below piece of code in map which returns array of Api requests. Using the the instance of FacebookAdsApiBatch i am putting each request into it which should return multiple results as it batch

return new APIRequest(    `http://graph.facebook.com/v18.0/${levelId}`    'GET',    'insights' )  .addParam('access_token', XX_X_X)  .addParam('fields', 'impression, clicks')  .addParam('level', levelType) 

Above Array of APIRequest passed in below fucntion

async getBatchRequestResult(requests: APIRequest[]): Promise<any> {   const batchApi = new FacebookAdsApiBatch(      getFbApiInstance(),      (res) =>{         console.log('batch instance success ', res)      },      (error) => {         console.log('batch instance error', error)      }   )   reqests.forEach((request) => {      batchApi.addRequest(         request,          (data) => {           console.log(' Success addRequest: ${JSON.stringify(data)}')          },         (error) => {           console.log('error', error)         }      )   })   return new Promise((resolve, reject) => {       batchApi         .execute()         .then((responses) => {            console.log('***RESPONSES****', responses);            resolve(responses)         })         .catch((error) => {            console.log('***ERROR****', error);            reject(error)         })         }) } 

Current in lambda logs I am seeing 3 types of logs in order.

  1. batch instance success: { kind of same json as below }
  2. Muliple "Success addRequest" logs. One of mentioned below Success addRequest:
{  "_body": {     "https://graph.facebook.com/v18.0/<CAMPAIGN_ID>/insights"   }  "status": 200,  "_headers": [     long list of objects   ],  "_call": {     "method": "GET",     "relative_url": "https://graph.facebook.com/v18.0/<CAMPAIGN_ID>/insights?                         access_token=XX_X_X                        &**fields**=&level=campaign"     "attachedFiles": ""      }     .......     .......   } } 
  1. RESPONSES = Null

Question is, If we pay attention on relative_url the fields value is empty despite the fact I am passing string 'impression, clicks'. I tried with array ["impression", "clicks"] and addParams({ key:Val }) but it still not passing that value. Really not sure why it is ommitting. All other parameters are fine. I doubt that this is the why I am facing issue of Null as final result. fields is important for me because I have to fetch tons of other measures as well.

Can someone please help me to figure out what is the issue? If anyone need further detail I am ready to give as much I can.

Currently in codebase we have "new Campaign" instance which has getInsights(null, query) fun which working fine. I am replacing this and trying batch call to avoid timeout error from FB for all kind of insights (campaign, adsets, ad) and therir breakdowns and mesures. By achiveing above batch call, the number of API calling to FB would be significantly reduced.

Reference: https://www.npmjs.com/package/facebook-nodejs-business-sdk?activeTab=code ^ Go under BatchExample.js

I am running this command on my terminal :

C:\Ad-Library-API-Script-Repository-main\python> py fb_ads_library_api_cli.py -t (token) -f "id,page_id,page_name,ad_creative_bodies,spend,demographic_distribution,bylines,ad_delivery_start_time" -c "CA" -s "climate" --after-date "2024-05-01" save_to_csv file.csv 

And I get the following error :

Traceback (most recent call last): File "C:\Ad-Library-API-Script-Repository-main\python\fb_ads_library_api_cli.py", line 147, in main() File "C:\Ad-Library-API-Script-Repository-main\python\fb_ads_library_api_cli.py", line 134, in main save_to_csv( File "C:\Ad-Library-API-Script-Repository-main\python\fb_ads_library_api_operators.py", line 96, in save_to_csv with open(output_file, "w") as csvfile: ^^^^^^^^^^^^^^^^^^^^^^ PermissionError: [Errno 13] Permission denied. 

Does anyone have a solution ? i'm not sure I understand how the Permission system works in the Graph Api.

I have tried adding "encoding="utf-8" in the fb_ads_library_api_operators file to fix the encoding issue, but it didn't work (i am able to get one dataset then i get the same error all over again)

I'm trying to update my Lead Values using the API. I only use server side to send first party data, the thing is that I need to readjust the values of these leads the first days / weeks, and there isn't any mechanism that I see to do so.

The only most logical way would be to send same events with same event_id and Facebook deduplicates or updates the event to the most recent one. But the documentation states the following:

"Does not deduplicate events when only using one event source, that is browser-only or server-only. If you send us two consecutive browser events with the same information, we do not discard either. If you send us two consecutive server events with the same information, we do not discard either."

Would be there any way to delete / update events with more fresh data? Don't want to send my Leads with a lot of delay of the time event.

I'm sending events with the first value I have on my BD, but it is not 100% accurate over time.