Get More from Your Google LSA Text Us at (628) 228-6704
0 like 0 dislike
591 views
by Marketing Guru (8.8k points)

1 Answer

0 like 0 dislike
by Marketing Guru (9.0k points)

Yes, you can use this code to auto-pause at 5 PM and auto-enable at 9 AM.

  • What it does: Pauses selected campaigns at 17:00 and enables them at 09:00 (account time zone).

  • Safety: Only touches campaigns labeled AUTO_SCHEDULE. (Change or remove the label logic if you want it to affect all campaigns.)

  • Scheduling: Set the script to run Hourly.

/**
 * Auto-pause at 17:00 and auto-enable at 09:00 (account time zone).
 * Affects only campaigns with label "AUTO_SCHEDULE".
 * Schedule: Hourly
 */
function main() {
  var tz = AdsApp.currentAccount().getTimeZone();
  var hour = parseInt(Utilities.formatDate(new Date(), tz, 'H'), 10); // 0–23
  var labelName = 'AUTO_SCHEDULE';

  ensureLabel_(labelName);

  if (hour === 17) {
    // 5 PM — pause enabled campaigns
    var it = AdsApp.campaigns()
      .withCondition("Status = ENABLED")
      .withCondition("LabelNames CONTAINS_ANY ['" + labelName + "']")
      .get();
    var count = 0;
    while (it.hasNext()) { it.next().pause(); count++; }
    Logger.log('Paused ' + count + ' campaign(s) at 17:00.');
  } else if (hour === 9) {
    // 9 AM — enable paused campaigns
    var it2 = AdsApp.campaigns()
      .withCondition("Status = PAUSED")
      .withCondition("LabelNames CONTAINS_ANY ['" + labelName + "']")
      .get();
    var count2 = 0;
    while (it2.hasNext()) { it2.next().enable(); count2++; }
    Logger.log('Enabled ' + count2 + ' campaign(s) at 09:00.');
  } else {
    Logger.log('No action this hour (' + hour + ':00).');
  }
}

function ensureLabel_(name) {
  if (!AdsApp.labels().withCondition("Name = '" + name + "'").get().hasNext()) {
    AdsApp.createLabel(name);
  }
}

How to use

  1. In Google Ads: Tools & Settings → Bulk actions → Scripts → +

  2. Paste the code → AuthorizeSave.

  3. Add the label AUTO_SCHEDULE to any campaign you want the script to control.

  4. Schedule the script Hourly.

Join a specialized Q&A site for digital marketing experts focused on ad platforms like Google, Facebook, YouTube, LinkedIn, Bing, Quora, and more. Connect with peers to share strategies on client retention beyond acquisition. Discuss and solve technical, managerial, or general challenges to improve your ad campaigns and client relationships.

Register to unlock your digital marketing potential!
Get More from Your Google LSA
Text Us at (628) 228-6704
...