Enter a search term such as “mobile analytics” or browse our content using the filters above.
Check your spelling or try broadening your search.
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.
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.
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.
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.