DEV Community

Cover image for How We Automated Google Ads Campaigns with Custom Scripts
Marcelo M
Marcelo M

Posted on • Edited on

How We Automated Google Ads Campaigns with Custom Scripts

In this post, we share how we use Google Ads Scripts at Scalix Lab to automate complex campaign management workflows at scale. You'll find real code snippets, logic structures, and usage recommendations from budget monitoring to ROAS tracking with Slack alerts.

This article is aimed at digital marketing professionals, media buyers, and technical marketers managing high-volume Google Ads accounts.

What Are Google Ads Scripts (and Why Use Them)?

Google Ads Scripts are JavaScript programs that let you interact directly with the internal API of your Google Ads account. They offer a programmable layer of automation that goes beyond standard automated rules.

Technical advantages:

  • Programmatic access to account objects (campaigns, ad groups, ads, etc.)
  • Access to real-time and historical stats
  • Conditional logic, loops, and modular automation
  • External integrations via HTTP (Slack, Zapier, CRMs...)

Script 1: Pause Campaigns When Daily Spend Exceeds Budget

Scenario

We want all campaigns to be paused if today's spend exceeds a set limit (e.g., €200).

Code

function main() {
  var limit = 200; // Daily spend limit
  var stats = AdsApp.account().getStatsFor("TODAY");
  var spend = stats.getCost();

  if (spend > limit) {
    var campaigns = AdsApp.campaigns().get();
    while (campaigns.hasNext()) {
      campaigns.next().pause();
    }
    Logger.log("Campaigns paused. Current spend: " + spend);
  }
}
Enter fullscreen mode Exit fullscreen mode

Recommended usage: Run hourly on accounts with multiple live campaigns.

Script 2: Pause Underperforming Ads (Low CTR)

Scenario

Automatically pause ads with CTR below 1.5% over the last 7 days and more than 100 impressions.

Code

function main() {
  var threshold = 1.5;
  var ads = AdsApp.ads()
    .withCondition("Impressions > 100")
    .withCondition("Ctr < " + threshold)
    .forDateRange("LAST_7_DAYS")
    .get();

  while (ads.hasNext()) {
    var ad = ads.next();
    ad.pause();
    Logger.log("Ad paused: " + ad.getHeadline());
  }
}
Enter fullscreen mode Exit fullscreen mode

Recommended usage: Run daily. Ideal for accounts with frequent creative rotations.

Script 3: Enable Campaigns Remotely via Webhook

Scenario

Enable a specific campaign when a POST request is received from an external system (e.g., Notion button, CRM trigger, mobile app).

Code

function doPost(e) {
  var campaigns = AdsApp.campaigns()
    .withCondition("Name CONTAINS 'Lanzamiento Flash'")
    .get();

  while (campaigns.hasNext()) {
    campaigns.next().enable();
  }
  return ContentService.createTextOutput("OK");
}
Enter fullscreen mode Exit fullscreen mode

Technical note: Deploy this script as a Web App in Google Apps Script and enable anonymous access to the endpoint.

Script 4: Send Slack Alert When ROAS Drops

Scenario

Send a real-time Slack alert if any campaign had ROAS lower than 2.0 yesterday.

Code

function main() {
  var campaigns = AdsApp.campaigns().get();
  while (campaigns.hasNext()) {
    var c = campaigns.next();
    var stats = c.getStatsFor("YESTERDAY");
    var roas = stats.getConversionValue() / stats.getCost();

    if (roas < 2.0) {
      var payload = JSON.stringify({
        text: `⚠️ Low ROAS (${roas.toFixed(2)}) in: ${c.getName()}`
      });

      UrlFetchApp.fetch("https://hooks.slack.com/services/xxx/yyy/zzz", {
        method: "post",
        contentType: "application/json",
        payload: payload
      });
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Recommended usage: Run daily early in the morning for ROAS-sensitive accounts.

Technical Considerations

Use Utilities.sleep(ms) to delay between actions if needed.

Use Labels to filter or segment campaigns managed by scripts.

Combine Logger.log() with Google Sheets API for advanced debugging or reporting.

Conclusion

Google Ads Scripts allow you to go beyond the limitations of standard rules and gain full programmatic control over your campaigns. They enable faster decisions, reduce manual errors, and support scalable operations without third-party tools.

Top comments (0)