How to Use Google Ads Scripts – Free Sample Script

how to use google adwords scripts

Numbers on a screen.. spooky

How to Use Google Ads Scripts.

A detailed walkthrough of creating a custom script to alert you when sitelinks get disapproved.

Google Ads scripts are an extremely powerful and customizable tool that you can use with Google Ads. And while the idea of writing code is an immediate turn-off for some, it’s actually much easier than it sounds. With just a very basic understanding of how javascript works and an ability to use Google Search, you should be able to do some pretty powerful stuff. Of course, if you have a developer handy, that’s going to help a lot. At the very least, it’s going to save you time.

Let me just say up front that I am not a developer, nor do I have any desire to become one. My code is certainly ugly to actual developers and I have no doubt that there are more efficient ways of writing it. That said, it works. If you want to share ideas or better ways to code this particular script, feel free to share. You won’t hurt my feelings. I taught myself enough javascript to work smarter. Most of my scripts start with something that someone has already created, such as the script examples Google offers or the wealth of examples available at http://www.freeadwordsscripts.com/, and then I absolutely gut it out and add all kinds of complex behavior that suits my specific needs. If you’re not a developer like me, it’s helpful to do it that way. It lets you make small changes to something you know works and progressively make it more bespoke. That way, if it breaks at any given step, you know why. Plus, it’s a good way to learn quickly.

So, what is Google Ads Scripts exactly and how does it work? Google Ads Scripts is a built-in development platform for Google Ads. It’s pre-installed on every Google Ads account. It lives in the Tools & Settings section in Google Ads.

It works by using javascript to tell Google Ads what you want it to do. Let’s walk-through an example of how to write a script that will alert you anytime a sitelink extension gets disapproved. This script allows you to take a very mundane task and automate it. It’s a small task, but if you do this for a large number of clients, it adds up each time you do it. A script like this can check every account you manage every hour or day and will take no time to manage once your script is written.

Go to “Tool & Settings” in the upper right hand part of Google Ads and select Scripts in the Bulk Actions section.

Hit the blue circle to create a new script. You’ll see something like this. Name your script. Click Authorize. Select your account. Click Allow. Click Save on the page where the script lives.

create new script
create new script

The basic format of a Google Ads Script looks like this:

function main() {
} 

This part is required in any Google Ads Script. It’s special in that it has a required function called “main.” No matter what, you need a function called “main” in a Google Ads Script. Most coding languages work by having “functions.” Do you remember in Algebra class where you had “f(x) = …” in all of your problems? That’s all this is. “f” stands for “function” and the “x” lets you define what you want the function to work on. If the () is blank, you’re just saying that the function can apply to anything, which is exactly the same as “f(x)”. If you put something in the brackets ex: (someVariable) then the function is being applied to that variable. This script is so simple that we won’t even have to do that.

Let’s just start. It’ll make sense. Our script is going to look at all of our sitelink extensions, see which ones are disapproved and email us the number of sitelinks that are disapproved.

The first thing we want to do is write a sub-function that we will call in the main function. You could write everything in the main function, but separating the function will be good practice when you’re working on more complex scripts.

Let’s call our sub-function “getSitelinkExtensions.” Now tell your main function to run this function by adding “getSitelinkExtensions ();” to your main function. Now anytime you run this script, it will run this one function and that’s it.

Now we define the sub-function. The first thing it needs is a variable that will grab all of the sitelink extensions that are disapproved. You do this by defining the variable with “var (someName) = (the code to grab the sitelinks) For us, we’ll write it like this:

var sitelinkIterator = AdsApp.extensions()
.sitelinks()
.withCondition("DisapprovalShortNames != ''")
.get(); 

Let’s break that down. The variable is called “sitelinkIterator.” Now we have to use special Google Ads code to grab what we want. You can read all about the Google Ads API reference to see all of the special API commands that are available. This starts with the AdsApp(“AdWordsApp” also works but is being deprecated) part. It’s like a mini app. You use it to filter what you want to perform actions on and eventually perform those actions. So the next piece we add is “.extensions()” because that’s the category of data we want. Then, specifically, we want the sitelink extensions, so we add “.sitelinks()”. Now we want to specify a condition to filter our results even more. Any sitelinks that are disapproved will have a string of text in its “DisapprovalShortNames” dimension explaining why its disapproved. If it’s not disapproved, that string will be empty. So, by adding “.withCondition(“DisapprovalShortNames != ””), we are saying, “only get sitelinks where this dimension is NOT empty.” FYI, “!=” means DOES NOT EQUAL. Finally, we get the data by adding .get(). And at the end of every variable, you need to add a “;” which signals the end of the variable.

You can break it up in multiple lines or have it all in one line, like this:

var sitelinkIterator = AdsApp.extensions().sitelinks().withCondition("DisapprovalShortNames != ''").get(); 

Here’s what our code looks like now.

create iterator
create iterator

The next part is to take that raw data and put into an array, which is like a mini .csv file. This starts by defining an array. Add the following below the code you just wrote.

var disapproved = []; 

Below that, we add code that will iterate through each row of the raw data we grabbed and push the data into our array.

while (sitelinkIterator.hasNext()) {
    var sitelink = sitelinkIterator.next();
    disapproved.push(sitelink);
} 

Let’s unpack that.

The first line is saying “the data we grabbed might have multiple rows of data.” The next line is saying, “for each line of data, lets temporarily define that data (sitelink in our case) as “sitelink”.
The final line is saying, “let’s send each temporary variable called “sitelink” to our array called “disapproved”. It then loops through the code over and over until it runs out of rows.

Here’s what our code looks like now:

create array adwords script
create array adwords script

At this point in the script, the array has all of our disapproved sitelinks in it. Let’s check to see what’s in there.

We can do that by adding a line of code that “logs” data to the console. The console is below the code in Google Ads. Under the line that says “disapproved.push(sitelink);”, but before the “}” we’ll add the following line:

Logger.log("sitelink disapproved: " + sitelink.getLinkText());  

This will produce a line for each row of data that has a disapproved sitelink. The line will include the text “sitelink disapproved: ” plus the name of the sitelink that is disapproved. The code at this point looks like this.

create logger adwords script
create logger adwords script

Now hit “Preview” on the bottom right of the screen. This will run the script, but it won’t make any changes. And while preview won’t make changes in Google Ads, it will do other things like send emails if we have that code in there, which we’ll add later. Assuming you don’t have any disapproved sitelinks, nothing will print in the console and it will look like this.

no errors logger adwords script
no errors logger adwords script

But let’s edit the code real quick just to check what would happen. Remove the “!” from line 6 (right before the “=” )so it looks like this:

function main() {
  getSitelinkExtensions ();
}

function getSitelinkExtensions(){
    var sitelinkIterator = AdsApp.extensions().sitelinks().withCondition("DisapprovalShortNames = ''").get();

    var disapproved = [];
    
    while (sitelinkIterator.hasNext()) {
      var sitelink = sitelinkIterator.next();
      disapproved.push(sitelink);
      Logger.log("sitelink disapproved: " + sitelink.getLinkText());
    }
}
 

Now run Preview again. It will print all your approved sitelinks.

approved sitelinks adwords scripts
approved sitelinks adwords scripts

Ok, so it works. Put the “!” back in the code.

The last thing we need to do is send an email if there are any disapproved sitelinks. Let’s write it so that you can easily use this code in multiple accounts. We’ll create a couple variables we can easily change for other accounts without digging through the code. Start by making a global variable at the very top called “var account = “(Account Name)”;
Below that create a variable called “var TO_NOTIFY = “(your email address)”;

Your code should look like this:

var account = "Brooklyn Blackpipe";
var TO_NOTIFY = "[email protected]";

function main() {
  getSitelinkExtensions ();
}

function getSitelinkExtensions(){
    var sitelinkIterator = AdsApp.extensions().sitelinks().withCondition("DisapprovalShortNames != ''").get();

    var disapproved = [];
    
    while (sitelinkIterator.hasNext()) {
      var sitelink = sitelinkIterator.next();
      disapproved.push(sitelink);
      Logger.log("sitelink disapproved: " + sitelink.getLinkText());
    }
}
 

Now add the code that sends the alert email:

if (disapproved.length > 0) {
    MailApp.sendEmail(TO_NOTIFY,
    ""+account+" has "+disapproved.length+" disapproved Sitelink extension/s.",
    ""+account+" has "+disapproved.length+" disapproved Sitelink extension/s.");
} 

Let’s dissect that. First we have an “if” statement that says, “if our array of disapproved sitelinks has anything in it, do the following…” We define “anything in it’” with “disapproved.length > 0”. “Length” is a count of items in an array. Then we use the MailApp to send the email. Adding “.sendEmail()” tells the app to send an email. The info in the brackets tells it who to send to and what to say. “TO_NOTIFY” is the variable we defined above. The next two lines define what the headline and the body will say. The entire expression for each line is wrapped in double quotes. The first word in the expression is the name of the account we defined above. This variable has a plus “+” on both sides and is wrapped in its own double quotes. The next variable (“+disapproved.length+”) will print the number of disapprovals we have.

Here’s the final code:

var account = "Brooklyn Blackpipe";
var TO_NOTIFY = "[email protected]";

function main() {
  getSitelinkExtensions ();
}

function getSitelinkExtensions(){
    var sitelinkIterator = AdsApp.extensions().sitelinks().withCondition("DisapprovalShortNames != ''").get();

    var disapproved = [];
    
    while (sitelinkIterator.hasNext()) {
      var sitelink = sitelinkIterator.next();
      disapproved.push(sitelink);
      Logger.log("sitelink disapproved: " + sitelink.getLinkText());
    }
  
    if (disapproved.length > 0) {
      MailApp.sendEmail(TO_NOTIFY, 
      ""+account+" has "+disapproved.length+" disapproved Sitelink extension/s.",
      ""+account+" has "+disapproved.length+" disapproved Sitelink extension/s.");
    }
}
 

Hit Preview. If you have no disapprovals, you won’t get any results in the logger, nor will you get an email. But let’s test what it looks like when we DO have a disapproval. Remove that same “!” we temporarily removed before. (Line 9 for my example). Now hit Preview. You should see your approved sitelinks in your logger as you did before and you should have gotten an email at the address you specified in the script. The email will look like this.

email alert headline adwords script
email alert headline adwords script
email alert body adwords script
email alert body adwords script

Add the “!” back and that’s it for the script. Congratulations, you’re a developer now 🙂

The last thing you want to do is schedule this script to run on a regular basis. Hit Save, then Close at the bottom of the page with the code. Here’s what you’ll see:

schedule frequency adwords script
schedule frequency adwords script

Hover your mouse over the empty space under “Frequency.” Click the pencil. Select how often you want to run it. There are technical reasons why you’d want to run it at certain hours over others, but if this is your only script, it won’t matter. Hourly is probably overkill. I’d set it to run Daily. Perhaps an hour before you start the work day so that it’s fresh in your inbox when you start working. Everything is done. Your script will now run on autopilot and you don’t have to worry about checking sitelinks unless you get an email. Obviously, it’s always a good idea to confirm your results for a day or two, but after that, just set and forget it. One thing to note, when you add new email address to a script, the script will likely ask you to re-authorize, like this:

authorize again adwords script
authorize again adwords script

It’s normal. Just authorize it like you did earlier.

Ok, that was how to write a basic, but useful script. Now what? It’s tempting to just go wild and start writing scripts, but probably isn’t a good use of time. I’d head over to Google’s existing set of example scripts and see if there is something there that piques your interest. It’s rare that any script is going to be perfect for your needs right out of the box. So find one that is close to what you need and modify it. After you exhaust all sample scripts on Google and Free AdWord Scripts, it’s time to start building scripts from scratch.

Here’s the process you want to follow. Document your manual process. Write it down in detail. As you go through the process a few times, ask yourself how (or if) you can translate those steps into code. Don’t worry too much about the exact code when you do this. As long as you’re mostly sure it can be done, that’s good enough. You can figure out the exact code later.

What you’ll find is that many things you do manually can be automated. What you’ll also find out is that many things you do require a human touch. You can still use scripts, but your task in those cases is going to be to make the human element of the process better and more efficient.

Your time is precious and investing a little bit of it upfront can save you a lot of it down the road. Plus, scripts don’t make mistakes. Humans do. So if you can write a script properly upfront, it will do exactly the same thing every single time, removing the risk of making a human error.

If you get stuck on code and can’t figure it out, be sure to check out the Google Ads Scripts Forum. Post your problem and there’s a very good chance someone on the Google Ads Scripts Team will reply directly to you.

© 2023 Nic Pederson. Built with Care