UrlFetchApp.fetch returns "Timeout: http://www.example.com"

1,366 views
Skip to first unread message

Martin Manchev

unread,
Jun 5, 2017, 6:47:26 AM6/5/17
to AdWords Scripts Forum
Hi all,

I've made a link checker that works fine for all of the accounts in our MCC, except for 2. 
In this two account the link checker returns this error Timeout: http://www.example.com" Every time it occurs at different ad...

Here is my code. It's simplified to work only over one account and does not return any results in spreadsheets or so. Just want to demonstrate the error that I'm receiving.

function main(){
  
   var campaignIterator = AdWordsApp.campaigns().withCondition("Status != DELETED").get();
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
var campaignName = campaign.getName();
      
        
//-----------------------------------------------------------
              
        var adGroupIterator = campaign.adGroups().withCondition("Status != DELETED").get();    
           while (adGroupIterator.hasNext()){
             var adGroup = adGroupIterator.next();
             
      
//-----------------------------------------------------------               
             var adsIterator = adGroup.ads().get();      
             while (adsIterator.hasNext()) {
               var ads = adsIterator.next();
               Logger.log(ads.getId());
   
//-----------------------------------------------------------
               
               if (ads.urls().getFinalUrl()){
                 var finalUrls = (ads.urls().getFinalUrl()) ;
                 var urlText = ads.urls().getFinalUrl();
                 var response = UrlFetchApp.fetch(finalUrls.split('?')[0], {followRedirects: false, muteHttpExceptions: true});
                 var rescheck = response.getResponseCode();
                 var together = rescheck + ' - ' + urlText ;
                 var errorUrls = [];

                 if (rescheck == 200 || rescheck == 301 ) {
                   continue;}     
                 else {
                   errorUrls.push(together);
                   Logger.log(campaignName + ' - ' + adGroup.getName() + " - " /*+ ads.getHeadline() */+ " - " + errorUrls);
     
                 } 
               }
             }        
           }
    }      
}
   

Is there a way in which I can setup a timeout in ms for a particular link. Like if the url does not return a response in 1000ms -> then continue with the next url?

Thanks in advance!

Anthony Madrigal

unread,
Jun 5, 2017, 9:31:45 AM6/5/17
to AdWords Scripts Forum
Hi Martin,

As a best practice to optimize your code and avoid timeouts, I recommend not traversing the campaign hierarchy and going directly to ads as shown here.

As for your use case, you can use the following condition
var adIterator = AdWordsApp.ads().withCondition("CampaignStatus != REMOVED").withCondition("AdGroupStatus != REMOVED").get();

Please let me know if you face any other issues.

Regards,
Anthony
AdWords Scripts Team

Martin Manchev

unread,
Jun 7, 2017, 6:18:49 AM6/7/17
to AdWords Scripts Forum
Thanks Anthony,

I changed the code so it now iterates through the ads directly on a account level. And it seems like the issue with the timeout is gone. 

Martin Manchev

unread,
Jun 8, 2017, 8:55:44 AM6/8/17
to AdWords Scripts Forum
And just one more question,

is there a way in which I can check for a 301 redirects only in the cases when he domain is different. 
For ex: not when the final url is example.com and it redirects to www.example.com

Maybe I should use the Location from the header and compare it with the final url?

Thank You!

Anthony Madrigal

unread,
Jun 8, 2017, 9:35:35 AM6/8/17
to AdWords Scripts Forum
Hi Martin,

You could reach out to the Apps Script Team since they will know better on how to use the UrlFetchApp to do this.

Regards,
Anthony
AdWords Scripts Team
Reply all
Reply to author
Forward
0 new messages