Welcome back to our Intro to AdWords scripts series where we’re training you to automate your account management using scripts.

If you missed our first post in the series, check out How to use AdWords scripts, the future of paid search.

This article is for the enthusiastic reporters. We’ll teach you how to calculate the cost of your campaigns over the last week and automate an email with the details. 

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

To check out what else you can do with the objects in this script, hit one of the links below. 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.





Daniel Gilbert

Published 7 April, 2014 by Daniel Gilbert

Daniel Gilbert is CEO at Brainlabs Digital and a contributor to Econsultancy. You can connect with Daniel on LinkedIn

9 more posts from this author

You might be interested in

Comments (4)


James Hantman

So it should be quite easy to write an AdWords script to send the latest spend just for today every hour? We like to keep on top of spend because it can get out of control.

almost 4 years ago

Daniel Gilbert

Daniel Gilbert, CEO at Brainlabs

Hi James,

Yes, it is quite easy! Simply change the date range on line 14 to read "TODAY" to pull stats for today. I've pasted the script below for you to easily copy & paste.

function main() {

var emailContent = "";
var total = 0;

var campaignSelector = AdWordsApp.campaigns()
.withCondition("Cost > 10")

var campaignIterator = campaignSelector.get();
var campaign = campaignIterator.next();
var campaignName = campaign.getName();
var stats = campaign.getStatsFor("TODAY");
var cost = stats.getCost();
emailContent = emailContent + "The campaign " + campaignName + " spent £" + cost + "\n";
total = total + cost;

var recipient = "you_are_so@good_at_this.com";
var subject = "Subject of the message";
var body = "The cost of your campaigns today: \n\n" + emailContent + ".\n\n" + "Total cost: £" + total;

MailApp.sendEmail(recipient, subject, body);


almost 4 years ago


Mark Riley

Daniel that is awesome, thanks. I have installed the script on one account and even modified it for a different account to report monthly, so thanks, I am learning loads.

BTW the link to CampaignSelector at the foot of the article appears to be broken, but I guessed it was LAST_MONTH

almost 4 years ago

Daniel Gilbert

Daniel Gilbert, CEO at Brainlabs

Thanks Mark - glad it's useful for someone! Keep an eye out for more advanced scripts coming up soon.

Correct link is https://developers.google.com/adwords/scripts/docs/reference/adwordsapp/adwordsapp_campaignselector

almost 4 years ago

Save or Cancel

Enjoying this article?

Get more just like this, delivered to your inbox.

Keep up to date with the latest analysis, inspiration and learning from the Econsultancy blog with our free Digital Pulse newsletter. You will receive a hand-picked digest of the latest and greatest articles, as well as snippets of new market data, best practice guides and trends research.