How to Pause / Enable a campaign on specific Date and Time?

175 views
Skip to first unread message

Chandan Kumar

unread,
Feb 7, 2023, 5:59:53 AM2/7/23
to Google Ads Scripts Forum
What Would be the Google Ad Scripts to Pause / Enable a campaign on specific Date and Time?

Sigurd Fabrin

unread,
Feb 7, 2023, 8:05:11 AM2/7/23
to Google Ads Scripts Forum
Should be fairly simple to achieve, smth like this:
if (date == 'date1') {campaign.enable()}
else if (date == 'date2') {campaign.pause()}


sigurd

Sigurd Fabrin

unread,
Feb 7, 2023, 8:26:39 AM2/7/23
to Google Ads Scripts Forum
..forgot to throw in a link to how you can find the campaign you want to enable/pause

sigurd

Chandan Kumar

unread,
Feb 8, 2023, 3:03:42 AM2/8/23
to Google Ads Scripts Forum

This is the script I have tried and its not working

function main() {
 
  var campaignsList = [];
  var campaignIterator = AdsApp.campaigns()
      .get();
  while (campaignIterator.hasNext())
  {
    var campaign = campaignIterator.next();
    campaignsList.push(campaign);
  }
   
  var DATE = new Date().getDate();
  if (DATE == '08-02-2023')
  {
      for(var i = 0; i < campaignsList.length; i++)
      {
        var campaign = campaignsList[i];
        Logger.log(campaign.getName());
        campaign.pause();
      }
  }
}

Sigurd Fabrin

unread,
Feb 8, 2023, 4:50:02 AM2/8/23
to Google Ads Scripts Forum
Your IF statement will never be true:
var DATE = new Date().getDate();
if (DATE == '08-02-2023')

Try printing out the date and you will see why. Like this: console.log(DATE)


I'd use an approach like the example below.
It will pause all campaigns in the account on pauseDate and enable them on enableDate

let settings = {
  pauseDate:'2023-03-01', // yyyy-MM-dd
  enableDate:'2023-03-08',
}

function main() {
  let today = Utilities.formatDate(new Date(),AdsApp.currentAccount().getTimeZone(),'yyyy-MM-dd');
  console.log('Today\'s date: '+today);
  let condition;
  let enable = false;
  if (today == settings.enableDate) {
    condition = 'campaign.status = PAUSED';
    enable = true;
    console.log('Today is enable day\n*\n');
  }
  else if (today == settings.pauseDate) {
    condition = 'campaign.status = ENABLED';
    console.log('Today is pausing day\n*\n');
  }
  else {
    console.log('Won\'t do anything today');
    return
  }
  let selectors = [
    AdsApp.campaigns(),
    AdsApp.shoppingCampaigns(),
    AdsApp.videoCampaigns(),
    AdsApp.performanceMaxCampaigns()
  ];
  for (i in selectors) {
    let iter = selectors[i]
    .withCondition(condition)
    .get();
    while (iter.hasNext()) {
      let campaign = iter.next();
      if (enable == true) {
        campaign.enable();
        console.log('Enabled: "'+campaign.getName()+'"');
      }
      else {
        campaign.pause();
        console.log('Paused: "'+campaign.getName()+'"');
      }
    }
  }
}



Sigurd

Chandan Kumar

unread,
Feb 8, 2023, 6:56:03 AM2/8/23
to Google Ads Scripts Forum
Do you know script for Enabling or Pausing campaigns with Time?

Sigurd Fabrin

unread,
Feb 8, 2023, 7:16:40 AM2/8/23
to Google Ads Scripts Forum
"Do you know script for Enabling or Pausing campaigns with Time?"
That is essentially the same. So, instead of checking that dates matches, check for hours.


Sigurd

Google Ads Scripts Forum Advisor

unread,
Feb 8, 2023, 8:13:11 AM2/8/23
to adwords...@googlegroups.com

Hi All,

 

Thank you for reaching out to us. This is Yasmin from the Google Ads Scripts Team. Please excuse us for only getting back now as your message failed to be routed to our support queue.

 

@Sigurd - Thanks for providing your insights here.

 

@Chandan - I've created a script which can fit your use-case; kindly find it in the attachments. I would recommend to test this in Preview Mode. Once satisfied with the output, you can start the live execution of the script or schedule it.

 

Let us know if you have any further clarifications.

 

Best regards,

 

Google Logo
Yasmin Gabrielle
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2iXHlp:ref
Pause campaign given a date.txt

Chandan Kumar

unread,
Mar 14, 2023, 5:43:58 AM3/14/23
to Google Ads Scripts Forum
I'm unable to do it for for Time, can you please help me?

I have tried below script but it didn't work.

function main() {
  var campaignIterator = AdsApp.campaigns().get();

  while (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    var timezone = campaign.getTimeZone();
    var now = new Date();
    now.setTime(now.getTime() + (timezone.getOffset(now) * 60 * 1000));
    var hour = now.getHours();
    if (hour >= 20 || hour < 6) {
      campaign.pause();

Google Ads Scripts Forum Advisor

unread,
Mar 14, 2023, 9:40:44 AM3/14/23
to adwords...@googlegroups.com

Hi Chandan,

 

I'm afraid that it's not recommended to set a time that's minute specific as scripts is on an hourly basis. You can implement an if-else statement to check if a date (such as today) is greater than or equal to the date and time you're aiming to set the script to pause the campaign. I would recommend creating a new date object of today, and compare it with a specific date object of when you'd like the script to pause the campaign. It's also helpful logging these objects in order to get a grasp of how they are considered.

 

Kindly do note that, when creating a date object using a string that does not provide a timezone offset, the timezone is assumed to be America/Los_Angeles (Pacific time), regardless of the timezone associated with the Google Ads account. That being said, you can refer to our documentation on Working with Dates and Times for more information on important concepts, common pitfalls, and recommended approaches when working with dates and times in Google Ads scripts.

 

Let us know if you have any further clarifications.

 

Best regards,

 

Google Logo Google Ads Scripts Team


ref:_00D1U1174p._5004Q2jZP5Z:ref
Reply all
Reply to author
Forward
0 new messages