I'm trying to get all ads from a Facebook ad account that have metrics with 'spend' set to 0 and 'reach' set to 1 using GRAPH API SDK v.19. However, if 'reach' does not exist, this filter skips ads that have 'spend' at 0. On the other hand, if in my filter I put 'reach' at 0 and 'spend' at 0, this brings me all the ads of the ad account (+5000) even if they do not have metrics on the specified date. How can I fix this problem and get only the ads that have metrics with 'spend' greater than or equal to 0 excluding those without.

I tried using Facebook's Graph Explorer to get all the ads in an ad account that have metrics. I used the 'filtering' field in my query to filter the results based on these criteria of spend greater than or equal to 0 and reach greater than or equal to 1. However, if 'reach' does not exist, this filter ignores ads that have 'spend' at 0. On the other hand, if in my filter I put 'reach' at 0 and 'spend' at 0, this brings me all the ads in the ad account even if they do not have metrics on the specified date. I was hoping to get only the ads that have metrics.

I share graph explorer URL: act_384943998894065/ads?fields=id,name, effective_status,created_time,insights.time_range({'since':'2024-06-19','until':'2024-06-19'}){ad_id,reach,spend,objective,actions,cost_per_action_type}&filtering=[{"field":"spend","operator":"GREATER_THAN_OR_EQUAL","value":0},{"field":"reach","operator":"GREATER_THAN_OR_EQUAL","value":0}]&time_range={'since':'2024-06-19','until':'2024-06-19'}

attached image of graph explorer results:

{       "id": "120208793679360133",       "name": "CE1705202404",       "effective_status": "ACTIVE",       "created_time": "2024-05-17T14:59:20-0500"     },     {       "id": "120208458805660133",       "name": "CE202404301910",       "effective_status": "ACTIVE",       "created_time": "2024-05-09T12:23:30-0500",       "insights": {         "data": [           {             "ad_id": "120208458805660133",             "spend": "0",             "objective": "OUTCOME_ENGAGEMENT",             "actions": [               {                 "action_type": "onsite_conversion.messaging_welcome_message_view",                 "value": "1"               }             ],             "cost_per_action_type": [               {                 "action_type": "onsite_conversion.messaging_welcome_message_view",                 "value": "0"               }             ],             "date_start": "2024-06-19",             "date_stop": "2024-06-19"           }         ],         "paging": {           "cursors": {             "before": "MAZDZD",             "after": "MAZDZD"           }         }       }     }, 

I attach my PHP code:

public function getAdsByAccountId($date){         try {             $date_range = "{'since':'$date','until':'$date'}";             $time_range = "&time_range=$date_range";             $fields = [                 "id", "campaign_id", "adset_id", "adset{promoted_object}", "creative{id,image_url,video_id,object_story_spec}", "name", "created_time", "status", "effective_status", "configured_status",                 "campaign{id,account_id, name, objective, effective_status, status, configured_status, created_time}",                 "insights.time_range($date_range){ad_id,reach,spend,objective,actions,cost_per_action_type}",             ];             $string_fields = $this->buildFields($fields);             $filtering = "&filtering=[{'field':'spend','operator':'GREATER_THAN_OR_EQUAL','value':0},{'field':'reach','operator':'GREATER_THAN_OR_EQUAL','value':1}]";             $limit = "&limit=200";             $url = "/" . $this->BUSINESS_ACCOUNT_ID . "/ads" . $string_fields . $filtering . $time_range . $limit;             $response = $this->FB_SDK->get($url, $this->ACCESS_TOKEN);             $response = json_decode($response->getBody());             $ads = $response->data;             $data = $this->getAdsFromNextPages($response, $ads);             $ads = $data->ads;             $response = $data->response;             return (object) [                 "ads" => $ads,                 "response" => $response,             ];         } catch (\Facebook\Exceptions\FacebookResponseException $e) {             Log::error('FacebookAdsService::getAdsByAccountId Graph returned an error: ' . $e->getMessage());             throw $e;         } catch (\Facebook\Exceptions\FacebookSDKException $e) {             Log::error('FacebookAdsService::getAdsByAccountId Facebook SDK returned an error: ' . $e->getMessage());             throw $e;         }     } 

Tag:php, facebook-graph-api

Add a new comment.