Turning certain google ad keywords on/off every hour

24 views
Skip to first unread message

Caroline

unread,
Sep 19, 2025, 2:26:52 AM (11 days ago) Sep 19
to Google Ads Scripts Forum

I am trying to write a script where for two specific keywords, I turn them both ON and then both OFF in alternating hours for six weeks in a specific country. All users should be able to see these ads when the keywords are on, and no users should be able to see them when they are off. So the rhythm should be:

Monday 00:00 - 00:59 Keywords ON 01:00 - 01:59 Keywords OFF 02:00 - 02:59 Keywords ON

Tuesday 00:00 - 00:59 Keywords OFF 01:00 - 01:59 Keywords ON 02:00 - 02:59 Keywords OFF

And so on. If this routine is followed, Monday on week 2 should start with keywords OFF from 00:00 - 00:59 and so on.

I only have some javascript experience and am totally new to google ads specifically, so I'm a bit afraid of just implementing my script without prior consulting someone else. 

Also happy to hear about any best practices or a way I can test out my script before actually implementing it!

It also feels needlessly complicated as of right now. Here's what I have so far:

function main() { var account = AdsApp.currentAccount();
var tz = account.getTimeZone();

// only run for austria accounts
if (tz !== "Europe/Vienna") {
Logger.log("Not Austria (" + tz + "), exiting.");
return;
}

// define start date
var startDate = new Date(2025, 0, 1); // Example: Jan 1, 2025
var today = new Date();
// get "today" in account time zone
var todayStr = Utilities.formatDate(today, tz, "yyyy-MM-dd");
var todayLocal = new Date(todayStr); // midnight local

// days since start
var msPerDay = 1000 * 60 * 60 * 24;
var daysSinceStart = Math.floor((todayLocal - startDate) / msPerDay) + 1;

// odd days start ON, even days start OFF
var startingOn = (daysSinceStart % 2 === 1);

// current hour in AT
var hour = parseInt(Utilities.formatDate(today, tz, "H"), 10);

// flip every hour
var isOn = (hour % 2 === 0) ? startingOn : !startingOn;

Logger.log("Day " + daysSinceStart + " (startingOn=" + startingOn +
"), hour " + hour + "" + (isOn ? "ON" : "OFF"));

// select keywords by label
var selector = AdsApp.keywords()
.withCondition("LabelNames CONTAINS 'EXPLabel'");

var keywords = selector.get();
while (keywords.hasNext()) {
var kw = keywords.next();
if (isOn && kw.isPaused()) {
kw.enable();
Logger.log("Enabled: " + kw.getText());
} else if (!isOn && !kw.isPaused()) {
kw.pause();
Logger.log("Paused: " + kw.getText());
}
}
}

Google Ads Scripts Forum

unread,
Sep 19, 2025, 4:47:53 AM (11 days ago) Sep 19
to Google Ads Scripts Forum

Hi,

Thank you for reaching out to the Google Ads Scripts support team.

To test a script functionality, you can preview the script so that it will log any errors without applying the changes to the account. You may refer to the Preview Mode guide for more information. Also, you can refer to the Best Practices document to execute a script effectively. Additionally, you can go through Working with Dates and Times, Keywords, and AdsApp.​Keyword documents for reference.

I would like to inform you that our team is focused on guiding users to troubleshoot specific issues within their own scripts, rather than providing hands-on implementation or creating scripts on a user's behalf.

I hope this helps! Feel free to get back to us with the following details for any further issues.

  • Google Ads account ID/CID
  • Name of the affected script
  • Screenshot of the issue (if any)
  • Shareable spreadsheet link if you are using any in the script. You may follow this guide to share a file publicly

You can share the requested details via Reply privately to the author option or a direct private reply to this email.

Thanks,
Google Ads Scripts team
Reply all
Reply to author
Forward
0 new messages