php - How to retrieve all my active Facebook ads? -


i'm creating dashboard myself helps me keep track of facebook ads i'm running.

what i've not been able figure out is:

how can retrieve array of ad ids ads active or active after no further action on part?

in other words, want ads i've set active , exist within adsets , campaigns active (and therefore these ads live right now)... plus ads my perspective active facebook has set status such pending review (and set active).

i have code below, problem also accidentally includes pending ads that--once reviewed , approved facebook--will inactive rather active (because i've set them way). , not want type of ad included in report.

my report should show me ones i'm actively spending money or have potential spend money fb approves them.

i think understand difference between configured_status , effective_status in abstractarchivablecrudobjectfields, don't know it's enough me because have lots of ads set active within adsets inactive, , don't want see listed in report.

any recommendations?

public function getactiveadids() {     $key = 'activeadids';     $adidsjson = cache::get($key);     if ($adidsjson) {         $adids = json_decode($adidsjson);     } else {         $adsresponse = $this->getadsbystatus([archivablecrudobjecteffectivestatuses::active, archivablecrudobjecteffectivestatuses::pending_review]);         $ads = $adsresponse->data;         $adids = [];         foreach ($ads $ad) {             $adids[] = $ad->id;         }         $adidsjson = json_encode($adids);         cache::put($key, $adidsjson, 1);     }     return $adids; }  public function getadsbystatus($statuses) {     $params = [\facebookads\object\fields\abstractarchivablecrudobjectfields::effective_status => $statuses];     $adaccount = new adaccount(self::act_prepend . $this->fbconfig['account_id']);     $cursor = $adaccount->getads([], $params);     $response = $cursor->getresponse();     $jsonstring = $response->getbody();     return json_decode($jsonstring); } 

i stats based on assets active campaigns. have 119 ad accounts. php code used purpose (any suggestion improve appreciated):

    $fields = array(adsinsightsfields::account_name,adsinsightsfields::campaign_id,                     adsinsightsfields::campaign_name, adsinsightsfields::adset_id,                     adsinsightsfields::adset_name,adsinsightsfields::date_start,                     adsinsightsfields::date_stop,adsinsightsfields::reach,                     adsinsightsfields::spend, adsinsightsfields::impressions,                     adsinsightsfields::clicks, adsinsightsfields::website_clicks,                     adsinsightsfields::call_to_action_clicks,adsinsightsfields::actions,                     adsinsightsfields::total_actions,adsinsightsfields::cpc,                     adsinsightsfields::cpm,adsinsightsfields::cpp,                     adsinsightsfields::ctr,adsinsightsfields::objective,);     $params_c['date_preset'] = addatepresetvalues::yesterday;     $params_c['time_increment'] = 1;     $params_c['action_attribution_windows'] = array('1d_view', '28d_click');     $params_c['effective_status'] = adstatusvalues::active;     $params_c['level'] = adsinsightslevelvalues::adset;     $params_c['filtering'] = [array("field"=>"campaign.delivery_info",                                     "operator"=>"in",                                     "value"=>array("active"))];     $params_c['fields']= $fields;          try{          // initialize new session , instanciate api object         api::init(self::api_key, self::secret_token, self::extended_token)->gethttpclient()->setcabundlepath( $this->path_cert);          // api object available trough singleton         $api = api::instance();         $user = new \facebookads\object\business($business_id);         $user->read(array(businessfields::id));          //get ad_account business         $accounts = $user->getassignedadaccounts(                       array(                     adaccountfields::id,                  ),                   array('limit'=>1000,)                 );                                          } catch (facebookads\exception\exception $ex) {         return $ex->getmessage();     }         if(isset($accounts) && ($accounts->count() > 0)):                    do{              $ad_account = $accounts->current();                         $adset_insights = $ad_account->getinsights($fields,$params_c);               {                 $adset_insights->fetchafter();             } while ($adset_insights->getnext());              $adsets = $adset_insights->getarraycopy(true);      }     while ($accounts->current());         endif; 

Comments