Script to Pause / Enable Campaign by Day/Date

1,106 views
Skip to first unread message

Richard

unread,
Jan 15, 2015, 10:38:04 AM1/15/15
to adwords...@googlegroups.com
Afternnon all, 

I'm sure this is possible but I'm struggling to find anything regarding day/date functions..

I'm after a script that will allow me to pause  / enable a campaign (selected by label) by day or date array


Basically, to spread costs, id like to be able to pause certain campaigns every other day (ie Tuesday, Thursday, Saturday.. even Sunday)

then i need the same script to reverse the paused campaigns and make them enabled on the other days (ie, Monday, Wednesday, Friday.. or Sunday)


please can someone help.

 

Ryan Gallup

unread,
Jan 15, 2015, 10:51:58 AM1/15/15
to adwords...@googlegroups.com
I'm not sure if it would help for your situation, but I would try to just adjust the campaign schedules to the days/hours desired, this would however keep it consistent each week instead of having the variance that the every other day would have done.

Richard

unread,
Jan 15, 2015, 11:01:41 AM1/15/15
to adwords...@googlegroups.com
Thanks Ryan, 

Unfortunately i don't want to be adjusting schedules as there are over 500 campaigns each with a very specific schedule setup..

as this is going to be used on the odd occasion id prefer to go down the route of a script that i can start / stop when required.

Rich.

Anash Oommen

unread,
Jan 16, 2015, 7:34:46 AM1/16/15
to adwords...@googlegroups.com
Hi Richard,

Something like this should work - apply a label to desired campaigns, then pause / enable them based on the day of the week:

function main() {
 
var label = AdWordsApp.labels()
     
.withCondition('Name = "START_MONDAY_PAUSE_FRIDAY"')
     
.get().next();
 
var campaigns = label.campaigns().get();
 
var today = new Date().getDays();

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

   
// Monday
   
if (today == 1) {
      campaign
.enable();
   
}

   
// Friday
   
if (today == 6) {
      campaign
.pause();
   
}
 
}
}


Cheers,
Anash P. Oommen,
AdWords Scripts Team.

Richard

unread,
Jan 19, 2015, 8:13:36 AM1/19/15
to adwords...@googlegroups.com
Hi Anash, 

Thank you very much for your reply, i have edited the script to reflect my requirements, but i am getting an error on line 6..

 var today = new Date().getDays();

TypeError: Cannot find function getDays in object Mon Jan 19 2015 05:06:45 GMT-0800 (PST). (line 6)


Do you know how i can fix this? 


Also, does "today" run Monday - Sunday (Monday being 1, Sunday being 7) and how can I get this to match GMT timezone.?

Kind Regards,

Richard

unread,
Jan 19, 2015, 8:37:13 AM1/19/15
to adwords...@googlegroups.com
Ok, I've figured out the error.. 

.getDays(); should be .getDay();

I would still like to know, does "today" run Monday - Sunday (Monday being 1, Sunday being 7) and how can I get this to match GMT timezone.?

Also this only picks up "Search" campaigns, how can i select all "Search and Shopping" campaigns which have the same label.

Kind Regards,

Anash Oommen

unread,
Jan 19, 2015, 11:30:07 PM1/19/15
to adwords...@googlegroups.com
Hi Richard,

1. getDay() runs from 0 to 6. See http://www.w3schools.com/jsref/jsref_getday.asp

Cheers,
Anash P. Oommen,
AdWords Scripts Team.

Richard

unread,
Jan 20, 2015, 4:26:29 AM1/20/15
to adwords...@googlegroups.com
Thanks Anash, thats really helpful.. :)

can you advise on why its only picking up "Search" campaigns? i need it to select both Search and Shopping Campaigns.

Kind Regards,
Richard.

Matt Greenland

unread,
Jan 20, 2015, 5:36:11 PM1/20/15
to adwords...@googlegroups.com
Hi Richard,

We intentionally keep search and shopping campaigns separate in our API, because aside from the basics like pausing/enabling, they have a very different set of features.

It doesn't look like we (yet) have a "label.shoppingCampaigns()" function to go along with the "label.campaigns()" function, but it should still be possible to get the campaigns and shopping campaigns associated with a label:


  var campaign = AdWordsApp.campaigns()
      .withCondition('LabelNames CONTAINS_ALL ["START_MONDAY_PAUSE_FRIDAY"]')
      .get();
  var shoppingCampaigns = AdWordsApp.shoppingCampaigns()
      .withCondition('LabelNames CONTAINS_ALL ["START_MONDAY_PAUSE_FRIDAY"]')
      .get();

Richard

unread,
Jan 22, 2015, 5:01:23 AM1/22/15
to adwords...@googlegroups.com
Hi Matt, 

That worked after a little adjustment to the rest of the script.. 

Here is what i have.. however, it doesn't seem to be pulling all the campaigns with the label (eg; we have 78 campaigns and it only changes 23)

is there anything im doing wrong? and is there any way of logging it?

function main() {

  var campaign = AdWordsApp.campaigns()
      .withCondition('LabelNames CONTAINS_ANY ["CAMPAIGN_TO_PAUSE_OR_ENABLE"]')
      .withCondition('LabelNames CONTAINS_NONE ["CAMPAIGNS_TO_IGNORE"]')
      .get();
  var shoppingCampaigns = AdWordsApp.shoppingCampaigns()
      .withCondition('LabelNames CONTAINS_ANY ["CAMPAIGN_TO_PAUSE_OR_ENABLE"]')
      .withCondition('LabelNames CONTAINS_NONE ["CAMPAIGNS_TO_IGNORE"]')
      .get();
  
   var today = new Date().getUTCDay();
   
   while (campaign.hasNext()) {
    var SearchCampaign = campaign.next();
    var ShoppingCampaign = shoppingCampaigns.next();
 
    // Sunday
    if (today == 0) {
      SearchCampaign.pause();
      ShoppingCampaign.pause();
    }
    
    // Monday
    if (today == 1) {
      SearchCampaign.enable();
      ShoppingCampaign.enable();
    }
    
    // Tuesday
    if (today == 2) {
      SearchCampaign.pause();
      ShoppingCampaign.pause();
    }
    
    // Wednesday
    if (today == 3) {
      SearchCampaign.enable();
      ShoppingCampaign.enable();
    }
    
    // Thursday
    if (today == 4) {
      SearchCampaign.pause();
      ShoppingCampaign.pause();
    }

    // Friday
    if (today == 5) {
      SearchCampaign.enable();
      ShoppingCampaign.enable();
    }
    
    // Saturday
    if (today == 6) {
      SearchCampaign.enable();
      ShoppingCampaign.enable();
    }
  }
}

Kind Regards,

Alexander Wang

unread,
Jan 22, 2015, 6:07:13 PM1/22/15
to adwords...@googlegroups.com
Hi Richard,

I think the biggest issue is that you are iterating over your shopping and search campaigns at the same time. If you have more shopping campaigns than search/display campaigns, your script will miss some of your shopping campaigns. You should iterate over them separately. I've rewritten your code to avoid this issue.

function main() {
 
// First, let's grab campaigns with the correct labels.
 
var campaigns = AdWordsApp.campaigns()

     
.withCondition('LabelNames CONTAINS_ANY ["CAMPAIGN_TO_PAUSE_OR_ENABLE"]')
     
.withCondition('LabelNames CONTAINS_NONE ["CAMPAIGNS_TO_IGNORE"]')
     
.get();
 
var shoppingCampaigns = AdWordsApp.shoppingCampaigns()
     
.withCondition('LabelNames CONTAINS_ANY ["CAMPAIGN_TO_PAUSE_OR_ENABLE"]')
     
.withCondition('LabelNames CONTAINS_NONE ["CAMPAIGNS_TO_IGNORE"]')
     
.get();


 
// Next, let's pause/enable regular search/display campaigns as needed.

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

    processCampaign
(campaign);
 
}

 
// Next, let's pause/enable shopping campaigns as needed.
 
while (shoppingCampaigns.hasNext()) {
   
var shoppingCampaign = shoppingCampaigns.next();
    processCampaign
(campaign);
 
}
}

var TODAY = new Date().getUTCDay();

// This does the same thing as your code, except it doesn't have as many if statements.
// If you prefer if statements, you can use them instead.
function processCampaign(campaign) {
 
switch (TODAY) {
   
// Sunday, Tuesday, Thursday
   
case 0:
   
case 2:
   
case 4:
      campaign
.pause();
     
break;
   
// Monday, Wednesday, Friday, Saturday
   
case 1:
   
case 3:
   
case 5:
   
case 6:
      campaign
.enable();
     
break;
 
}
}


There are a few other reasons the script might not affect all of your campaigns.
  1. Any campaigns that have the label "CAMPAIGN_TO_PAUSE_OR_ENABLE" AND the label "CAMPAIGNS_TO_IGNORE" will be skipped. All conditions are AND-ed together.
  2. Any "removed" campaigns are skipped by default. You can specifically request all campaigns by doing something like:
    1. AdWordsApp.campaigns().withCondition("Status IN [ENABLED, PAUSED, DELETED]") // You may need to wrap ENABLED in quotes.
  3. Any campaigns that aren't regular AdWords campaigns will be skipped. For example, any video campaigns your account may have would be skipped.
Let us know if this helps or if you're still having any issues.

Cheers,
Alex
Reply all
Reply to author
Forward
0 new messages