Google Ads

Creating Google Ads Scripts with ChatGPT

0 · by Dennis Moons · Updated on 8 November 2023

Google Ads scripts can help you to level up as an advertiser.

They can help you get access to reports and automation that aren’t available in the standard interface.

But they involve code, which is a barrier for many advertisers.

My own experience with Google Ads scripts is limited to using existing scripts and modifying them slightly. That’s an improvement. 

But as you get deeper into scripts, it becomes clear that there is so much more you could do. If only you could create your own scripts.

And that’s where ChatpGPT comes in!

In this article, we’ll break down how to use ChatGPT to create working Google Ads scripts.

Common Roadblocks

With ChatGPT, you have a coding wizard at your disposal.

But while it can produce code faster than you can talk, it doesn’t get the actual goal right.

If you jump straight into entering prompts, you’ll probably hit one or more of the following roadblocks:

  • Output gets messy after a while
  • ChatGPT renames variables when generating new responses
  • The regenerated version sometimes forgets prior fixes
  • Doesn’t always get the documentation right

It doesn’t matter whether you’re using GPT 3.5 (free version) or GPT 4 (paid version), these mistakes happen with both versions.

These errors can be frustrating. Especially when you’re creating a more complex script, it can be tough to throw in the towel after messing around for a few hours.

But if you’re a little bit more deliberate, you can increase your hit rate.

I’ve built quite a few Google Ads scripts, and in this post, I’d like to share my system to get better and more consistent results from my efforts.

1 – Translate the objective

So, before jumping to creating code, let’s think about how we can best use the strengths of ChatGPT.

As I mentioned before, ChatGPT is great at producing code, but it doesn’t always understand the actual thing we’re trying to do.

So if you don’t understand the code it’s putting out, you won’t really get what it’s trying to do, or whether that’s the best way forward.

Don’t get me wrong, the whole point of using ChatGPT is to leverage its coding abilities.

It just means that you have to make up for it by having a much deeper understanding of what you’re actually trying to accomplish.

  • Don’t understand code + don’t understand problem = mess
  • Don’t understand code + understand problem = guided chaos
  • Understand code + understand problem = perfect man+machine combo

Example: handling out-of-stock products

Let me show you an example to make this more practical.

Sometimes bestselling products go out of stock. Google will automatically stop these products from showing up as Shopping Ads. But I might not always know that this is happening. So it could be a few days after I notice one campaign really tanking because the product is no longer available, and Google trying to make up for that revenue with other products.

So I want to build a script that does the following: I want to receive an email when a bestseller goes out of stock so that I can adapt my approach.

While this objective makes sense at a high level, in order to build code, we need to translate it into something much more specific.

A bestseller goes out of stock.

 Bestseller = We can define this as a product with a specific product id, or a product with a certain amount of clicks, conversions, or revenue.

Goes out of stock = the product status changes from Ready to serve to Inactive OR if the product goes out of stock, it will stop showing as an ad, meaning impressions and clicks will fall dramatically.

Notice how much more precise we’ve gotten about what we’re looking to accomplish. In both cases, we’ve identified 2 different ways to get to the same data.

Some data will be easy to get, and some data will be harder or require extra integration. So the more different ways you have to accomplish this, the more options you have. 

(If you only have a single way to get the data, that’s also perfectly fine)

2 – Getting clear on the building blocks

Now that we’re clear on the objective, let’s look at the different parts we need.

One of the challenges with writing Google Ads scripts with ChatGPT is that things tend to become complex. In one iteration, part A might be working perfectly, but then with another iteration, part B might work but part A is broken again.

To avoid this, we’re going to figure out the building blocks first, and then build them separately.

Examples of building blocks:

  • Run a report on what’s happened in the account
  • Make changes to an existing campaign
  • Send information to a spreadsheet
  • Send an email
  • Check product status
  • Etc.

Example: handling out-of-stock products

Continuing our example from the previous section, here are the building blocks:

  1. Compare the performance of products day to day
  2. Find the products that match our criteria (bestselling product, big change)
  3. Send an email when #2 finds a product

3 – Build each block separately

Now it’s time to start building, I open up ChatGPT and the script section of Google Ads, that way I can immediately copy/paste the output to check.

Leveraging the log

Before jumping in, I want to mention another great tool, and that is your log:

google scripts using the log

This log is a way for you to see what’s happening as a result of running the code. It gives you a look inside. This lets you spot any problems and troubleshoot your script.

In the image above for example, the log tells us that it’s starting a new analysis, and it’s found “12 this execution”. 

By default a script won’t add anything to the log, so it’s something we’ll explicitly tell ChatGPT.

Preload ChatGPT

Good prompts are an important part of getting high-quality results.

While there are many ways to get the same results, the prompt below does the job for me.

You’re a Google Ads expert tasked with creating Google Ads scripts to help with account management.

It’s important to keep the code as simple as possible and to stay consistent with variable names between different iterations.

If there are multiple options or alternative paths, please ask follow-up questions before continuing.

For each part of the code, please add an output to the logger that describes exactly what’s going on and what the operation has an effect on.

Then you would add what script you’d like to build, which we’ll cover below.

Example: Building the first block

Instead of trying to build everything at once, we’ll build our Google Ads script piece by piece.

Let’s tackle this part first: Compare the performance of products day to day

chatgpt google scripts prompt

I’ll spare you the details. 

But over 13 iterations going back and forward with fixes and new ideas, I got this first part to work.

If you want you can see the full chat thread here.

Here is the approach I use:

  1. Add prompt
  2. Copy ChatGPT output
  3. Paste script in Google Ads
  4. Run Preview
  5. Check log
    1. If error: feed back into ChatGPT
    2. Otherwise: see if the script works and readjust
  6. Repeat

Along the way, the biggest obstacles you’ll run into are errors and inconsistencies of available data.

Handling errors

Handling errors is pretty simple. Copy the error you find in the log:

google scripts error

Add it to ChatGPT. You can skip adding extra context because it will understand the error.

fixing google scripts errors with chatgpt

It will then apologize and fix the script. Repeat.

Tip: sometimes it will only regenerate the parts of the script that were inaccurate. Instead, ask it to regenerate the whole script. That way you don’t accidentally copy errors into newer versions.

Problems with scope

If you have problems with incorrect syntax, these are pretty easy to fix.

But some problems go a bit deeper and you can get stuck if you don’t think things through.

One example is that Standard Shopping and Performance Max use different account structures, so if you want to pull a report that spans both, you’ll need to adhere to both exact cases and later combine them again.

You wouldn’t know about most of these issues until you run into them, but hey, that’s part of the fun 🙂

Reiterate other building blocks

When I had a working version of the first building block, I started working on the next part.

Here you have two options. You either expand the script you’ve been working on, or start from scratch.

The best approach depends on how much complexity you’re adding.

If you’re just doing the output of the previous building block, sending an email or adding stuff to a spreadsheet, you can modify the existing script.

That’s because these things are pretty straightforward.

If you want to do more complex things, I suggest starting fresh and later trying to integrate them.

For example, let’s say that when we identify a certain product that has stopped showing, we want to check if there are variations of the product that are still live.

This again isn’t super straightforward.

4 – Finishing your script

When you’ve combined all the different parts, your script is finally ready.

Depending on how it’s going to be used, you might want to clean it up or add additional instructions on how to use it.

You can do this manually, or simply use ChatGPT:

chatgpt google scripts final prompt

This resulted in a much more user-friendly script:

chatgpt google scripts final output

Advanced versions with Apps scripts.

You might recall that when we were defining the objective, we came up with different ways to pull the data.

One was to check product status changes, the other was to check the impressions day to day.

I chose the latter because it is much simpler.

Tracking changes to the product is possible via the Content API for Google Merchant Center.

The tricky thing is that this doesn’t integrate with Google Ads scripts. That’s why I chose an alternative that avoids this entirely.

You can build “apps” like this, Google calls these “App scripts. But they are a whole other can of worms.

And it’s probably a topic for a different day and a different article 🙂

Dennis Moons

Dennis Moons is the founder and lead instructor at Store Growers.

He's a Google Ads expert with over 12 years of experience in running Google Ads campaigns.

During this time he has managed more than $5 million in ad spend and worked with clients ranging from small businesses to global brands. His goal is to provide advice that allows you to compete effectively in Google Ads.

Follow him on Twitter or LinkedIn.


Leave a Reply

Your email address will not be published. Required fields are marked *