I’ve addressed the benefits of Google Ads scripts to automate aspects of account management. Producing custom scripts is now much easier with generative AI platforms.

Scripts can go beyond automation to solve problems. For example, it’s not always evident when an account stops serving ads. The payment method may fail, or Google could reject them. The “Account Down” script runs hourly and sends an email when an account hasn’t seen an impression in a designated period.

I’ve struggled with campaigns that set a higher target cost per acquisition than the budget. It’s not always obvious. A budget might be $500, but the tCPA is $600. The tCPA should be at least three times the budget in my experience.

AI for Ad Scripts

Here’s where AI comes in. Tools such as ChatGPT, Gemini, and Claude (my favorite) can generate scripts quickly.

Sample prompt in Claude for the tCPA mismatch and Claude's response.

Generative AI platforms such as Claude can produce scripts quickly.

Here’s my prompt in Claude to create a script:

Please write a Google Ads script that reviews all campaigns and sends an email alert when the tCPA is higher than the campaign budget.

Here’s Claude’s response:

Google Ads tCPA Alert Script

This script:

1. Iterates through all enabled campaigns in your Google Ads account
2. Identifies campaigns using Target CPA bidding strategy
3. Compares each campaign’s tCPA value against its daily budget
4. Creates a list of campaigns where tCPA exceeds the budget
5. Sends you an email alert with details if any campaigns meet this condition

I pasted the script into Google Ads and received an error.

3/5/2025 3:47:20 PM TypeError: campaign.bidding(...).get is not a function
at main (Code:39:40)
at Object.<anonymous> (adsapp_compiled:20555:54)

I then prompted Claude to fix the error. Here’s Claude’s response:

Google Ads tCPA Alert Script – Fixed

I’ve fixed the error by correcting how the script retrieves the target CPA value. The issue was with the line `var bidding = campaign.bidding().get();` which was using an incorrect method call.

Key changes made:

1. Replaced the problematic method chain with a direct call to `campaign.bidding().getTargetCpa()`

2. Added error handling with try/catch blocks to gracefully handle different API versions and bidding strategy configurations

3. Added support for retrieving tCPA from shared bidding strategies (which have a different access method)

4. Added validation to ensure tCPA was successfully retrieved before comparing it to the budget

The script — download it here — now runs in Google. I solved a problem through a series of prompts.

Not perfect

My next AI task was to obtain a script that identified all active ad groups in live campaigns with no impressions in 21 days. The alert would trigger me to investigate the cause.

Claude’s script worked, but it had two errors:

  • It defaulted to any period, not 21 days.
  • It included custom experiments that ended 21 or more days previously. Experiments run only for a designated period, but the script included them as normal campaigns.

After sharing these errors and receiving replacement scripts, Claude produced new errors for invalid inputs. I ended the session without securing the script I wanted.

It’s a handy reminder that AI is not always as productive as humans. A developer could likely set up this script without the extended back-and-forth.

Claude’s script for tCPA conflicts worked. The no-impressions script did not.

Monitor the unexpected

The final script I created addressed rejected ads. Advertisers should monitor not just the ads but rejected keywords and audiences, which occur occasionally.

Any script in Google Ads times out after 30 to 60 minutes. I first prompted Claude to identify disapproved ad assets (sitelinks, callouts, and more). Fearing a timeout, I opted for two separate scripts.

Dialog showing the prompt and Claude's response.

The follow-up script and response focused on “limited” statuses.

Here’s the prompt for the non-asset disapproval.

Please update this script so it only looks at disapproved ads, keywords, and audiences. Entities with “limited” status should not be included.

And Claude’s response:

I’ll update the script to only include items with “DISAPPROVED” status and exclude those with “LIMITED” or any other non-disapproved status. Here’s the modified version:

Google Ads Script for Disapproved Items Only. Click to open code.

The key changes I made to the script:

1. Changed the condition checks for ads from:

```javascript
if (ad.getPolicyApprovalStatus() != 'APPROVED') {
```

to:

```javascript
if (ad.getPolicyApprovalStatus() == 'DISAPPROVED') {

Download the script.

No limits

My example scripts above alert advertisers via email, but I could have prompted Claude to output to a spreadsheet instead. I could have included paused entities. The possibilities are seemingly limitless.

Similar Posts