Expert recommendations for writing efficient, maintainable, and safe automation scripts that deliver consistent results.
Always use preview mode before running scripts live. This shows exactly what changes will be made.
Begin with safe thresholds and gradually optimize. It's easier to scale up than recover from mistakes.
// ✅ Good: Clear, safe, logged function main() { const TARGET_ROAS = 3.0; const MAX_BUDGET = 1000; Logger.log('Starting budget optimization...'); const campaigns = AdsApp.campaigns() .withCondition('Status = ENABLED') .withCondition('Cost > 50 DURING LAST_7_DAYS') .get(); let optimizedCount = 0; while (campaigns.hasNext()) { const campaign = campaigns.next(); const stats = campaign.getStatsFor('LAST_7_DAYS'); const roas = stats.getConversionValue() / stats.getCost(); if (roas >= TARGET_ROAS) { const currentBudget = campaign.getBudget(); const newBudget = Math.min(currentBudget * 1.2, MAX_BUDGET); campaign.setBudget(newBudget); optimizedCount++; Logger.log(`Optimized: ${campaign.getName()} - ${currentBudget} → ${newBudget}`); } } Logger.log(`Optimization complete: ${optimizedCount} campaigns updated`); }