facebook / facebook-nodejs-business-sdk

Node.js SDK for Meta Marketing APIs
https://developers.facebook.com/docs/business-sdk
Other
497 stars 227 forks source link

Omitting 'fields' Query Parameter in FacebookAdsApiBatch via APIRequest with facebook-nodejs-business-sdk #289

Open gharesagar opened 4 months ago

gharesagar commented 4 months ago

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 { 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": "" } ....... ....... } }

3. 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. Thank you in advance.

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