Adwords Scripts (en)

Adwords Scripts allow you to query data or modify AdWords accounts quickly and automatically using JavaScript.

Before we continue, we’d like to talk briefly about Adwords management. The most effective account managers spend most of their time analysing the status of Adwords campaigns and evaluating the different options available for optimising them. This work can be broken down into a series of periodic tasks (daily, weekly, every two weeks) which, once completed, gives us an overview and facilitates decision-making.

Some of these tasks can be automated to save time. For example, I can arrange to receive a particular report every Monday; start and stop an ad at a specific time and date; or automatically upload the CPC to make certain keywords for the ads always appear on the first page.

There are two ways of scheduling these tasks: automated rules or scripts. The basic difference is that the rules are predefined and are designed for most accounts. The scripts are for everything else...

Let’s consider a real case. We have two campaigns, one Remarketing and one Display campaign with a monthly budget of €2,000. The sector is peculiar, which results in erratic performance. So the customer decides to approve a daily budget for each campaign which, if implemented, would far exceed the monthly €2,000. To avoid this, the customer wants to receive an email when the cost of both campaigns combined reaches €1,900.

We go to campaigns. We select Remarketing and Display and create a rule to send an email when the cost is greater than €1,900. We schedule it to run daily using the data for the month. However, when we click on Preview to check the results after running some tests, we see that the rule is treating the campaigns separately. In other words, it would send an email when the first campaign reaches €1,900 and would then remain unchanged until the second campaign does the same. Obviously, this is not what the customer wants. The solution lies in an Adwords script.

Here is how we solved the problem. Look at the comments and, if you want to use it, modify data that is boldfaced.

var costes = new Array();
var total = 0;
var todos = new Array();
var sumaLog = "";
var mail = "";
var datos = "";
var cadena = "";
var costeInt = 0;
var totalCoste = 0;
var limiteCampaign = 1900;    //Change the limit here. 

function campaigns (){   
   var campaignsName = ["Campaign 1","Campaign 2"];    //Add the names of campaigns, in quotes and separated by commas.
   return campaignsName;  

   function procesaDatos (objeto){    //This function writes the log 
   var suma = "";
   suma = sumaCostes(costes);
   if(suma >= limiteCampaign){
   mail = todos + "\n" + sumaLog;        
   return (mail.replace(",",""));
   function sumaCostes(costes){
   for(var cont=0;
   cont < costes.length;
   total = total + costes[cont];
   totalCoste = parseInt(total);
   sumaLog = "The total cost of all campaigns is " + totalCoste + "\n";
   return (totalCoste);
   function enviaMail(){    // Useful for long-running scripts: send yourself an email when the script is done!  
   var recipient = "";    // Email
   var subject = "Más de 1.900€";    // Emai subject
   var body = "Result of the query:\n\n" + datos;

   MailApp.sendEmail(recipient, subject, body);
   function main() {
   var objeto = new Object();
   objeto.nombre = "";
   objeto.coste = 0;
   var nameCamp = "";
   var campaignsName = campaigns();    //Get our campaigns
   var campaignIterator = AdWordsApp.campaigns()    //Get Google AdWords
   while (campaignIterator.hasNext()) {
   var campaign =;
   var stats = campaign.getStatsFor("THIS_MONTH");
   nameCamp = campaign.getName();
   for(var i=0; i<campaignsName.length;i++ ){
   objeto.nombre = nameCamp;
   objeto.coste = stats.getCost();
   costeInt = parseInt(objeto.coste);
   cadena = "This month the campaign " + objeto.nombre + " costs " + costeInt;
   datos = procesaDatos(objeto);
   if(total >= limiteCampaign){
   //send mail

When we ran the results, we received an email with this text:

Result of the query:
This month the campaign Campaign 1 costs 720 This month the campaign Campaign 2 costs 1258
The total cost of all campaigns is 1978

In theory, Adwords scripts are designed for managing large accounts but we believe they can be a solution for any Adwords manager who spends too much time on repetitive and tedious tasks. How much is your time worth? How much time do you spend on these tasks? Figure it out and talk to us: it will save you time and be worth your while.


We use our own or third party cookies to improve your web browsing experience. If you continue to browse we consider that you accept their use.  Accept