Introduction to Adwords Scripts
Join our free Facebook group and learn from other advertisers. Join now!

AdWords scripts are a powerful way to automate a lot of the tedious parts of your ad campaign. In this post I’ll walk through the basics of Scripts and then show some examples and explain how they work. You don’t need any special technical skill to use scripts, just a very basic grasp of Javascript. It’s a bit more complex than point-and-click but learning how to use these will save you loads of time down the road.

Creating your first script

Adwords can be a bit of a hassle to manage, particularly once you get into larger, more complex campaigns.

To solve this problem, we’re going to use scripts. These let you automate virtually all aspects of an Adwords campaign with just a bit of basic javascript.

To create your first script, navigate to the Campaigns tab in your Adwords account.

Then click Bulk Operations in the left hand navigation. Select Scripts from the drop down.

Click + Script to create a new script.

You’ll be taken to the script code panel. There should be an example script pre-set that gets the 10 campaigns with the most clicks. You can click Authorize now and Run script to use this script. This panel is where we’ll be writing our new scripts.

That’s all there is to creating a script — pretty simple. The more difficult part that we’ll now get into is understanding how to write a script.

Components of scripts

There are two main components of all Adwords scripts.

One is the selector. A selector tells Adwords which data you want to retrieve. For example: we’d create a selector to retrieve all keywords with no clicks within the past 7 days.

Another components is the iterator. The iterator is what goes through all of the data we have retrieved and does something with it. For example: we’d use an iterator to log the keyword name of all keywords that meet the conditions of our selector.

These are the building blocks of all Adwords scripts. Now let’s go into more depth on each of them so we can really understand how to start creating our own scripts.

Understanding selectors

Usually your scripts will begin with a selector of some sort. You’ll use the selector to tell Adwords what data to retrieve and what conditions you want to place on that data. Let’s look at a selector and break down how it is doing this.

The first thing happening here is the selector is defined. This is the var campaignSelector = AdWordsApp.campaigns(). The variable ‘campaignSelector’ is set to include all Adwords campaigns.

If we were to iterate through this data and print the names to our logs, we’d get a list of all Adwords campaigns in our account. Not very useful.

That’s why we add conditions. The two lines below the variable that begin with .withcondition constrain our results to campaigns with More than 10 clicks and More than 1000 impressions. That’s a little bit more helpful. We could actually use that data for something useful.

Then if you skip to the bottom we have another constraint, which is .fordaterange. This tells the selector to only get campaigns that met the above conditions (> 10 clicks and > 1000 impressions) yesterday. So if a campaign had 20 clicks 8 days ago and only 7 clicks yesterday, it would not be retrieved.

Finally, this selector orders how we are presented with the data. The .orderby function tells the selector to order the retrieved data by number of impressions in descending order (so we’ll get the campaigns with the most impressions first).

At the end we include the get() selector, which is an instruction to execute the selector and retrieve all the requested data.

This is a simple selector, but they are all built using basically the same set of tools. Google has a well-documented list of all the different types of conditions and constraints you can use to build your selectors. Usually a quick Google search will also help you (or you can ask in our members group and we can lend a hand).

Now let’s learn about how to actually get at all that data our selectors are retrieving for us using iterators.

Understanding iterators

Once the selector grabs the data we need, we need to use an iterator to look at all the data and do something with it.

To do with, we use a while loop. This loop iterates through our data so long as a certain condition we set is true. The loops stops as soon as the condition is false. So for example, we can ask an iterator to loop through our data until there is no more data to loop through. That’s a pretty common loop.

Let’s add an iterator to the selector we created earlier to see how iterators work in practice. For now we’ll just have it loop through our campaigns from the selector:

In the while loop we have added, we’re taking the selector variable (‘campaignSelector’) and telling the iterator to loop through each of the retrieved campaigns until there are no more campaigns to loop through. So this iterator returns all the campaigns grabbed by our selector.

But this isn’t very useful on it’s own since we haven’t told the iterator to do anything with that data. So let’s add an instruction to have it tell us the name of the campaigns the selector has retrieved:

Now we have told our iterator to take all the retrieved campaigns and get their name (using Adwords’ getName() function).

Then we use the Logger function to print out the name of each campaign (Logger is just Adwords equivalent of a print or console.log() function).

I have also wrapped the entire script in a function — this is just something required by Adwords. You can use the same function name (main) in every script.

One thing to note: in our example, the campaign variable contains all available data about each of the campaigns from our selector. We have elected to get the name of the campaign, but you could pull out any other data you were interested in seeing. There’s a complete list of all the data you can get and the functions to use to get it in the Adwords docs.

Where to find scripts

The sample script we have used isn’t very useful, but thankfully there are plenty of extremely useful scripts already written — you just have to enter a few variables and authorize them. Very little coding is actually required.

The first place to get these is from Adwords itself. You can find a wide range of pre-built scripts that will help you with reporting, automatic bid adjustments, and more.

Another source is Russell Savage’s website. It’s not updated much anymore, but its archive is an excellent source of handy Adwords scripts.

And as always, if you need help getting your scripts up and running feel free to use the members group.





Learn how to run profitable Facebook ad campaigns

Get our free 7-part email course on finding your perfect Facebook audience. You'll learn expert strategies to lower costs and increase results.