How a Google Ads script is structured: main(), settings and helpers
The parts of a well-organized Google Ads script and the conventions chiliad expects.
Google Ads scripts are JavaScript with one rule: there must be a function called main. Everything else is convention, but good conventions make a script easier to reuse across accounts.
A typical layout
// 1. Settings
var MIN_CLICKS = 50;
var MAX_CPA = 40;
var ALERT_EMAIL = "you@example.com";
// 2. Entry point
function main() {
var rows = findExpensiveKeywords();
if (rows.length > 0) sendReport(rows);
}
// 3. Helpers
function findExpensiveKeywords() {
// query and return rows
}
function sendReport(rows) {
// format and send
}Settings at the top
Put everything a user might want to change in top-level var declarations with UPPER_SNAKE_CASE names. Do not bury thresholds inside functions. chiliad detects these declarations and lets you turn them into variables that can be set per account.
One entry point
main() should read like a summary of what the script does. Keep the details in helper functions.
What to leave out when hosting in chiliad
Write the script for a single account and let the platform do the rest:
- No manager-account loop. Do not wrap the logic in
AdsManagerAppcode; the loader runs the script in each assigned account. - No reporting code. The loader reports each run's result and duration by itself.
- No "dry run" switch. Use
AdsApp.getExecutionInfo().isPreview()to skip side effects in preview.
Small habits that pay off
- Log what the script decided and why with
Logger.log; the logs are the first place you look when something is off. - Fail loudly: throw an error for a missing setting rather than carrying on with an empty value.
- Prefer one
AdsApp.search()query over loops that fetch statistics entity by entity.