{{ searchResult.published_at | date:'d MMMM yyyy' }}

Loading ...
Loading ...

Enter a search term such as “mobile analytics” or browse our content using the filters above.

No_results

That’s not only a poor Scrabble score but we also couldn’t find any results matching “”.
Check your spelling or try broadening your search.

Logo_distressed

Sorry about this, there is a problem with our search at the moment.
Please try again later.

The scripting feature allows users to automate management of their AdWords accounts.

Examples range from a simple command that sends an hourly email with updated spend, to a bid management operation that adjusts bids based on the stock market.

AdWords scripts are the future of paid search management, particularly for large accounts which require automation.

Writing your first snippet of code may seem complex at first, but it doesn't take long to pick up, and is accessible without the usual long-winded process of writing an application for the AdWords API.

By following the steps in this article, you’ll have written your first script in no time at all.

Getting started: going to the scripts page

You can find the scripts page in the Bulk Operations tab hiding in the sidebar. Click the +Script button to begin. You will be taken to a page containing a box, a space for writing and executing your code.

The first time you start a new script you’ll be shown an example which counts the clicks accrued by your campaigns.

Clicking on API Documentation takes you to the reference pages for AdWords scripting. Becoming proficient at scripting means understanding and becoming adept at using the reference documentation.

We will frequently link to the relevant documentation pages when discussing scripts. 

Getting to grips with JavaScript

All AdWords scripts are written in JavaScript (and also in AdWords Query Language, AWQL, which is similar to SQL).

If you’re new to JS or have never programmed before, make sure you understand the basics. There are many great online resources to learn from, including Codecademy.com.

We'll be sharing scripts with you throughout this series, but a basic JavaScript understanding is invaluable when tailoring scripts to your accounts.

Your first script: sending an automated email

The code below will allow you to send an email with an attachment to recipients of your choice.

You can use this script to schedule automated reports or to alert you to an event, such as you account spend has passing a certain threshold.

Example AdWords script 

Before we get started, a bit of terminology

  • Variable: A variable contains information which can be accessed at a later point in time.
  • String: A string is information in the form of text wrapped in quotation marks. It is one of the types of data that can be stored in a variable; the other type is numbers.
  • Function: A function contains code between two curly brackets which can be executed over and over again by writing out the function’s name - known as calling the function.
  • Parameter: A parameter is information in the form of a variable which is needed by a function. A function can have any number of parameters, including none at all.
  • Object: An object is a collection of variables and functions contained between two curly brackets. The object pointed to in the picture, MailApp, is one of the many objects that comes ready-made in AdWords.
  • Method: When a function is contained within an object, it is known as a method. From the picture: the sendMail method is one of the functions belonging to the MailApp object.

Let's go through this code example line by line: 

  • Line 1: Every AdWords script requires a function called main. The code inside the curly brackets of the main function is executed when your script is run.
  • Line 3-5: Here we store the values of our recipient, subject and body in appropriately named variables so that we can access them later when sending the email - we’ll be using them as the parameters of a function. Feel free to change the values to those of your recipients and desired message contents.

To add more than one recipient, separate the emails with a comma inside the string (e.g. “userA@example.com,userB@example.com”).

  • Line 7: The attachment variable houses the data we want to attach to the email. This entry can be in the form of a string or a number.
  • Line 8: The options object specifies the file type and filename of the attachment. We've chosen to use a CSV, but you can also add text and HTML files. Simply replace all occurrences of "csv" with your desired file type, e.g. “txt” or “html”.
  • Line 12: Now that we’ve set up the recipients and contents of the email, let’s send it. Here we have to use the MailApp object, which contains a function - known as a method, remember - called sendMail. The sendMail method takes as parameters the four variables we created earlier. If you don’t want to send an attachment, you can leave out the options parameter.

The full reference for the MailApp object can be found here.

  • Line 14: The closing curly bracket which completes the function main.

Once you’re done, hit the preview button and you’ll be prompted to authorise your script. Do so and you’ll be ready to roll.

Great stuff, you’ve written your first script. In our next article we’ll show you how to actually send important data as an email attachment.

Daniel Gilbert

Published 20 March, 2014 by Daniel Gilbert

Daniel Gilbert is Managing Director at Brainlabs Digital and a contributor to Econsultancy. You can connect with Daniel on Google Plus

6 more posts from this author

Comments (3)

Avatar-blank-50x50

Ron Martin

Keyword relevancy is one of the keys to success with Google AdWords. While it may be tempting to include all sorts of keywords you can get your hands on, be aware that this is often the #1 mistake advertisers make when creating their first campaign. You only want prospects who “Need You Now” and are going to be motivated to contact your company, or purchase your services if they click your ad. People bid on the wrong keywords all the time and it hurts them big time. My company was losing an average of $0.67 per click until Simon over at RDM helped us get our ducks in a row with the campaign and now it makes $2.19 per click on average instead of -$0.67. He's really helpful on the phone, his number is 256-398-3835.

over 2 years ago

Avatar-blank-50x50

Andrey

"Line 7: The attachment variable houses the data we want to attach to the email. This entry can be in the form of a string or a number."

Excuse me, it`s not clear enough, what kind of number or string? If it is the number of Adwords report, where can we find it?

over 2 years ago

Daniel Gilbert

Daniel Gilbert, Managing Director at Brainlabs

Hi Andrey,

Not sure what you mean. The "number or string" could be any data you decide you want to attach from all the different AdWords reports. This could clicks, CTR, Average position, etc.

You'll find more information in the second article of the series here:

https://econsultancy.com/blog/64647-how-to-create-automatically-send-emails-with-adwords-spend

over 2 years ago

Comment
No-profile-pic
Save or Cancel
Daily_pulse_signup_wide

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 Daily Pulse newsletter. Each weekday, you ll 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.