Simple script that checks certain URL and pauses all campaigns

512 views
Skip to first unread message

Martin B

unread,
May 12, 2015, 12:13:47 PM5/12/15
to adwords...@googlegroups.com
Hello,

I'm looking for a simple script that only checks a certain URL that I specified in the script (not all URL's in my account) for a 404 or 500 error.

The script should pause all active campaigns if that certain URL responds with one of the error codes. Also it should send an email notification with a simple message if that happens.


I would be glad if you could help me.

Thanks,
Martin

Anthony Madrigal

unread,
May 13, 2015, 10:10:52 AM5/13/15
to adwords...@googlegroups.com
Hello Martin,

Here is a script to help you get started. You will need to fill in the conditions and URLs based on your use case:

function main(){
 
var URL = 'YOUR_URL_HERE';
 
var adGroupIterator = AdWordsApp.adGroups().get();
 
 
if (adGroupIterator.hasNext()) {
   
var adGroup = adGroupIterator.next();
   
var adsIterator = adGroup.ads().withCondition('Type=TEXT_AD').get();
   
while (adsIterator.hasNext()) {
     
var ad = adsIterator.next();
     
var campaign = ad.getCampaign();
     
var response = UrlFetchApp.fetch(URL, { muteHttpExceptions: true});
     
if ((response.getResponseCode() == 404 || response.getResponseCode() == 500) && ad.urls().getFinalUrl()){ //or whichever URL it corresponds to
        campaign
.pause();
     
}
   
}
 
}  
}


There is a full solution of something similar to what you are trying to do on the AdWords Scripts page that will also assist you with writing the script.

Cheers,
Anthony
AdWords Scripts Team

Martin B

unread,
May 14, 2015, 1:14:19 PM5/14/15
to adwords...@googlegroups.com
Hi Anthony,

Thanks a lot for your reply.

To test the script I tried it with: var URL = 'http://www.google.com/123abc.html';
That URL has a 404 error, but when I run the script it finishes without any changes. But shouldn't it pause all campaigns?

Thanks,
Martin

Anthony Madrigal

unread,
May 14, 2015, 2:17:55 PM5/14/15
to adwords...@googlegroups.com
Hey Martin,

If you want it to pause all active campaigns if your link is 404 or 500 then just remove && ad.urls().getFinalUrl().

However, if you want it to pause the campaigns based on if the URL you provided has a 404 or 500 error AND matches one of your Ad URLS e.g., DestinationUrl, FinalUrl, then leave ad.urls().getFinalUrl() or change urls().getFinalUrl() to match the corresponding URL type.

Regards,
Anthony
AdWords Scripts Team

Martin B

unread,
May 16, 2015, 5:04:27 AM5/16/15
to adwords...@googlegroups.com
Hi Anthony,

Thank you. Yes, I would like to pause all active campaigns if the link is 404 or 500.

But when I run the script only 1 campaign will be paused, not all. Is there anything else that I need to change?

I removed && ad.urls().getFinalUrl() as you mentioned and the script now looks like this:

function main(){
 var adGroupIterator = AdWordsApp.adGroups().get();
  
  if (adGroupIterator.hasNext()) {
    var adGroup = adGroupIterator.next();
    var adsIterator = adGroup.ads().withCondition('Type=TEXT_AD').get();
    while (adsIterator.hasNext()) {
      var ad = adsIterator.next();
      var campaign = ad.getCampaign();
      var response = UrlFetchApp.fetch(URL, { muteHttpExceptions: true});
      if ((response.getResponseCode() == 404 || response.getResponseCode() == 500)){
        campaign.pause();
      }
    }
  }  
}


Thank you,
Martin

Anthony Madrigal:

Anthony Madrigal

unread,
May 18, 2015, 9:24:11 AM5/18/15
to adwords...@googlegroups.com
Hi Martin,

I simplified the code a bit. This should work now:
function main(){
 
var URL = 'http://www.google.com/123abc.html';

 
var campaignIterator = AdWordsApp.campaigns().get();
 
while (campaignIterator.hasNext()) {
   
var campaign = campaignIterator.next();

   
var response = UrlFetchApp.fetch(URL, { muteHttpExceptions: true});

   
if (response.getResponseCode() == 404 || response.getResponseCode() == 500){
        campaign
.pause();    
   
}
 
}
}

Martin B

unread,
May 19, 2015, 10:40:52 AM5/19/15
to adwords...@googlegroups.com
Hi Anthony,

Great, that works! Thank you very much for your help.

I tried to extend the script myself by adding a function to send an email notification if the scripts pauses the campaigns because of the error. Unfortunately that sends me an email for each campaign that had been paused. What do I have to change to let the script send only one email if it pauses my campaigns?
function main(){
  var campaignIterator = AdWordsApp.campaigns().get();
  while (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    var response = UrlFetchApp.fetch(URL, { muteHttpExceptions: true});
    if (response.getResponseCode() == 404 || response.getResponseCode() == 500){
        campaign.pause();
      
      var recipient = "my email address";
      var subject = "AdWords Script: Campaigns paused";
      var body = "AdWords script execution: All campaigns paused because of 404 or 500 error.";
      MailApp.sendEmail(recipient, subject, body);
      
    }
  }
}

Thanks,
Martin

Anthony Madrigal:

Anthony Madrigal

unread,
May 19, 2015, 11:05:24 AM5/19/15
to adwords...@googlegroups.com
Hi Martin,

If you move the email part of code outside of the while loop and put it inside an if statement that is exactly like the other one, you should only get one email.

Martin B

unread,
May 21, 2015, 5:33:10 AM5/21/15
to adwords...@googlegroups.com
Hi Anthony,

That works. Thank you. 

Could you please let me know what happens, when the URL specified in var URL = '...'; is a tracking URL that redirects to another URL? Will the script check the tracking redirect URL or the final URL for the 404 or 500 error?

Thanks,
Martin

Anthony Madrigal:

Anthony Madrigal

unread,
May 21, 2015, 1:45:50 PM5/21/15
to adwords...@googlegroups.com
Hey Martin,

When using the fetch(url,params) method, there is an advanced parameter called the followRedirects which if set to false will use the original HTTP request (the tracking URL). It is set to true by default, which means that the script will check the final URL if there is a 404 or 500 error.
Reply all
Reply to author
Forward
0 new messages