Advanced techniques to make your Google Ads scripts run faster, consume fewer resources, and operate within platform limits.
// Bad: Gets ALL campaigns first
const campaigns = AdsApp.campaigns().get();
while (campaigns.hasNext()) {
const campaign = campaigns.next();
const stats = campaign.getStatsFor('LAST_7_DAYS');
// Filter AFTER getting data
if (stats.getCost() > 100 &&
campaign.isEnabled()) {
// Process campaign
}
}// Good: Filter BEFORE getting data
const campaigns = AdsApp.campaigns()
.withCondition('Status = ENABLED')
.withCondition('Cost > 100 DURING LAST_7_DAYS')
.get();
while (campaigns.hasNext()) {
const campaign = campaigns.next();
// Only process relevant campaigns
processCampaign(campaign);
}// Efficient batch processing for large accounts
function processCampaignsInBatches() {
const BATCH_SIZE = 500;
const MAX_OPERATIONS = 200000; // Stay under 250k limit
let operationCount = 0;
const campaigns = AdsApp.campaigns()
.withCondition('Status = ENABLED')
.orderBy('Cost DESC')
.get();
const campaignBatch = [];
while (campaigns.hasNext() && operationCount < MAX_OPERATIONS) {
campaignBatch.push(campaigns.next());
// Process batch when full or at end
if (campaignBatch.length >= BATCH_SIZE || !campaigns.hasNext()) {
operationCount += processBatch(campaignBatch);
campaignBatch.length = 0; // Clear batch
// Log progress
Logger.log(`Processed batch. Operations used: ${operationCount}`);
}
}
Logger.log(`Total operations: ${operationCount}`);
}
function processBatch(campaigns) {
let operations = 0;
campaigns.forEach(campaign => {
const stats = campaign.getStatsFor('LAST_7_DAYS');
operations += 2; // One for campaign, one for stats
// Your optimization logic here
if (stats.getCost() > 0) {
// Process campaign
operations += 1; // Additional operation for changes
}
});
return operations;
}// Performance monitoring and limits checking
function main() {
const startTime = new Date();
const MAX_RUNTIME_MINUTES = 25; // Stay under 30min limit
let operationCount = 0;
Logger.log('Script started at: ' + startTime.toLocaleString());
// Your main script logic here
const campaigns = AdsApp.campaigns()
.withCondition('Status = ENABLED')
.get();
while (campaigns.hasNext()) {
// Check time limit
const elapsed = (new Date() - startTime) / (1000 * 60);
if (elapsed > MAX_RUNTIME_MINUTES) {
Logger.log(`Time limit reached: ${elapsed.toFixed(1)} minutes`);
break;
}
// Check operation limit
if (operationCount > 200000) {
Logger.log(`Operation limit reached: ${operationCount}`);
break;
}
const campaign = campaigns.next();
operationCount += processCampaign(campaign);
// Log progress every 100 campaigns
if (operationCount % 100 === 0) {
Logger.log(`Progress: ${operationCount} operations, ${elapsed.toFixed(1)}min elapsed`);
}
}
const totalTime = (new Date() - startTime) / 1000;
Logger.log(`Script completed in ${totalTime.toFixed(1)} seconds using ${operationCount} operations`);
}
function processCampaign(campaign) {
// Return the number of operations used
return 2; // Example: getting campaign + stats
}Always monitor execution time and operation count to prevent script timeouts and ensure reliable performance.