Take a look at the block of code below. Do any lines look familiar? It’s great if they do, but don’t worry if not – we’ll comb through it line by line.

Before we begin, here’s the basic road-map of the script:

  • We pull a list of campaigns from AdWords.
  • We go through the list of campaigns one by one and get cost data on each.
  • The results are emailed to a few lucky recipients.

Code example

  • Line 1: As with all AdWords scripts this one contains a function called main. The code inside the curly brackets of the main function is executed when your script is run.
  • Lines 3-4: We’re declaring these variables so we can use them later. The variable emailContent will contain the data to be emailed: the campaigns and how much they cost. The variable total will house the total cost. So far, so good.
  • Lines 6-8: The first step is to create a selector. Selectors and iterators (you’ll meet iterators in a moment) are the bread and butter of AdWords scripting.

In basic terms, a selector is a list of AdWords objects – in our case campaigns – with which you interact. All the changes you make and data you pull will be done through a selector.

  • The methods on lines 7 and 8 provide the conditions on which campaigns are pulled from AdWords. We’ve chosen to go for campaigns which have cost more than £10 (line 7) in the last week (line 8).

By not including any methods after campaigns(), you will return all the campaigns in your account. To see all things you can do with a selector (via the methods belonging to it) check out Google’s CampaignSelector page.

There’s a link at the bottom of the article along with links for all the other AdWords objects we use in this script.

  • Line 10: Now it’s time to create an iterator. The iterator allows you go through each campaign, pulling data or making changes as you go along (for example, you might want to change budgets). This process is known as iterating through the campaigns.

To create an iterator, simply copy the script in line 10 into your own coding.

  • Line 11: Let’s iterate through. Use a while loop together with the hasNext method to do this, as in the script image. You can think of the iterator and while loop as looping through an array of campaigns, illustrated by the image below.

Blog 2 image

  • Line 12: The method next picks out the next campaign in the iterator. You can now interact with the current campaign by pulling data or making changes.
  • Line 13: We want to match a campaign name to its cost. To retrieve the campaign name, use the code set out in line 13. This is the getName method.
  • Line 14: To retrieve performance statistics for the campaigns, first specify a date range. This is the job of the getStatsFor method. We use this method on our campaign with parameter LAST_WEEK to let AdWords know that we want last week’s data.
  • Line 15: Using the stats object, we can retrieve any of last week’s performance data for our campaigns. We’ve chosen the getCost method, which returns the cost. Be sure to check out the link for the stats documentation page at the bottom of the article to see all the other types of data you can pull from AdWords.
  • Line 16: Once we have the data we need (the cost and name of our campaign) we can format the data in a string ready to be emailed. The \n character starts a new line so that each campaign sits neatly on its own row.
  • Line 17: This line gives us the total cost of all the campaigns.
  • Lines 20-24: Now send the email containing the cost of your campaigns!

It was a slog but you got through it. Give yourself a well-deserved pat on the back – you now understand the fundamentals of AdWords scripts.

To take things a step further, why not schedule your script to run daily? If you feel adventurous, add an if condition to the email – maybe you’ll only want to be emailed if you’ve spent over £100.

Blog 2 image 3

If the documentation doesn’t make sense, don’t worry! Our next post will be a super-easy guide to understand the AdWords scripts documentation.