How to get all my active Facebook ads?

I am creating a toolbar for myself that helps me keep track of the Facebook ads that I run.

What I could not figure out is:

How can I get an array of ad IDs for all active ads or can it be active after any further action on my part?

In other words, I want all the ads that I have installed for Active, and which exist in active and active advertisements (and therefore, these ads right now) ... plus all the ads that in my opinion are Active, but Facebook has set another status, such as Awaiting Review (and will return to active soon).

I have the code below, but the problem is that it also randomly includes pending ads that, once they are reviewed and approved by Facebook, will be inactive and not active (because I set them that way). And I do NOT want this type of ad to be included in my report.

My report should only show those where I actively spend money or have the opportunity to spend money as soon as they are approved by FB.

I think I understand the difference between configured_statusand effective_statusin AbstractArchivableCrudObjectFields, but I don’t know that this is enough to help me, because I have a lot of ads configured to Active that are within Addresses that are inactive, and I don’t want to see those that are listed in my 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 as $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);
}
+6
1

, . 119 . php-, ( ):

    $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 a new Session and instanciate an Api object
        Api::init(self::api_key, self::secret_token, self::extended_token)->getHttpClient()->setCaBundlePath( $this->path_cert);

        // The Api object is now available trough singleton
        $api = Api::instance();
        $user = new \FacebookAds\Object\Business($business_id);
        $user->read(array(BusinessFields::ID));

        //get all ad_account from 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); 

            do {
                $adset_insights->fetchAfter();
            } while ($adset_insights->getNext());

            $adsets = $adset_insights->getArrayCopy(true); 
    }
    while ($accounts->current());    
    endif;
+1

Source: https://habr.com/ru/post/1016340/


All Articles