skip to Main Content

I have problems retrieving Lead Ads.

I have the Ad-ID and the Page-ID. I haven’t created them, but was added as a developer.

I was trying to use the PHP SDK and this https://developers.facebook.com/docs/marketing-api/guides/lead-ads/v2.9

Nothing is working. I cannot find a nice tutorial about that.
I just want to retrieve the leading Ads!

Anyone?

2

Answers


  1. Assuming you have already installed FB API SDK and configured your FB app, you can use this to get all results from all LeadAds of your $page_id

    use FacebookAdsApi;
    use FacebookAdsObjectPage;
    use FacebookAdsObjectAd;
    
    $access_token = 'YOUR TOKEN';
    $app_id = 'YOUR APP ID';
    $app_secret = 'YOUR APP SECRET';
    
    $page_id = 'YOUR PAGE ID';
    
    Api::init($app_id, $app_secret, $access_token);
    
    $ads = getAllLeadsAds($page_id);
    
    $result = array();
    
    foreach ($ads->data as $item) {
    
        $leads = getLeadAdInfo($item->id);
        $i = 0;
        foreach ($leads->data as $value) {
            $result[$i]['ad_id'] = $item->id;
            $result[$i]['lead_id'] = $value->id;
            $result[$i]['form'] = $value->field_data;
            $i++;
        }
    }
    
    print_r($result);
    
    function getAllLeadsAds($page)
    {
        $page = new Page($page);
        return $page->getLeadgenForms()->getResponse()->getBody();
    }
    
    function getLeadAdInfo($ad)
    {
        $ad = new Ad($ad);
        return $ad->getLeads()->getResponse()->getBody();
    }
    

    Login or Signup to reply.
  2. Its possible that you not calling all ads inside adset.

    You can use facebook cursor.

    $cursor->fetchAfter();

    use FacebookAdsObjectAdAccount;
    use FacebookAdsObjectValuesArchivableCrudObjectEffectiveStatuses;
    use FacebookAdsObjectFieldsCampaignFields;
    use FacebookAdsObjectAdCampaign;
    use FacebookAdsObjectFieldsAdSetFields;
    use FacebookAdsObjectAdSet;
    use FacebookAdsObjectFieldsAdFields;
    use FacebookAdsObjectAd;
    use FacebookAdsObjectCampaign;
    
    function getadsetname($adset_id,$acc_id){
    
      $adset = new AdSet($adset_id, $acc_id);
      $adset->read(array(
        AdSetFields::NAME,
      ));
    
      return $adset->name; // Outputs name of adset.
    }
    function get_campaignname($campaign_id){
    
      $campaign = new Campaign($campaign_id);
      $campaign->read(array(
        CampaignFields::NAME,
      ));
      return $campaign->name;
    }
    
    $account = new AdAccount($account_id);
    
    echo $adset_name = getadsetname($adset_id,$account_id);
    echo $campaign_name = get_campaignname($campaign_id);
    echo "<hr>";
    
    $adcampaign = new AdAccount($campaign_id);  
    
    $adset = new AdSet($adset_id);
    $ads = $adset->getAds(array(
        AdFields::NAME,
        AdFields::CONFIGURED_STATUS,
        AdFields::EFFECTIVE_STATUS,
        AdFields::CREATIVE,
       ));
    
    $ads->fetchAfter();
    
    foreach ($ads as $ad) {
    
        $ad_id = $ad->id;
        $ad_name = $ad->name;
    
        if($ad->configured_status=="ACTIVE"){
    
            $ad1 = new Ad($ad_id);
            $leads = $ad1->getLeads();  
            $leads->fetchAfter();
    
            foreach ($leads as $lead) {
    
                $fname = $lead->field_data;
                //var_dump($fname);
    
                $data = array();
                $data['lead_id']        = $lead->id;
                $data['created_time']   = $lead->created_time;
                $data['ad_id']          = $ad_id;
                $data['ad_name']        = $ad_name;
                $data['adset_id']       = $adset_id;
                $data['adset_name']     = $adset_name;//$adset_name;
                $data['campaign_id']    = $campaign_id;
                $data['campaign_name']  = $campaign_name;
                $data['form_id']        = $lead->form_id;//$lead->id;
                $data['is_organic']     = $lead->is_organic;//$lead->id;
    
    
            }
    
        }
    }
    

    $ads->fetchAfter(); will get the next list of ads and
    $leads->fetchAfter(); will get the next list of leads

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search