jueves, 13 de septiembre de 2012

Using Google’s AdWords Scripts to set rules on your campaign

Posted 12 September 2012 10:47am by Alistair Dent with 0 comments

By adding a simple JavaScript program, you can link your own data to AdWords, to create rules for your campaigns, or customise reports. 

For example, if you're a retailer, you can link your inventory data to AdWords, to change bidding on a campaign, or stop/start keywords as items go out of stock. Or you can automatically change keywords or bidding on AdWords, based on data trends. 

You can pretty much do anything you want, in fact. We've created a sample script to set up an alert on an AdWords campaign, which we're happy to share.

Google has just put AdWords Scripts on general release (after a limited release in June this year), which lets you link your own data to AdWords campaigns.

Some of the big search technology firms already offer things like inventory linking, of course, but the beauty of Google opening its system up for general use is there's now no need to build an expensive tool from scratch (or pay someone else for theirs) or worry about getting API authorisation. Google's done the work for you.

Google has made the process pretty accessible: so as long as you have a tech-minded person to write the scripts for you, you can schedule changes, create reports and set up alerts. Most things are possible through the AdWords Scripts interface. It's not quite like having access to Google's API, but if you're familiar with JavaScript, there's a lot you can do fairly simply to manage your AdWords campaigns.

Google used to let you create custom alerts, but removed this feature earlier this year. Now, though, you can create customer alerts by using scripts, so to test the system, we created a sample script for alerts – and as we've created it, we thought we'd share it, and hope it's useful.

It is a simple piece of JavaScript that will alert you if your campaign suffers a drop of 80% or more in impressions (something you'd want to know, as it would be a reasonably sure indicator that something's gone wrong).

If you're interested, it's below. You need to copy and paste the text into a new script in the Automation page in AdWords, from the left-hand navigation bar.

There are two sections you need to change before you use it, both at the top of the code:

  1. The 'threshold' variable sets the percentage fall at which to trigger an alert. (We've set it to 80%, but you might want to change that).
  2. The 'strAddress' variable sets the email address that should receive a notification. Obviously you need to set that, too.

Once you've done that, you need to hit 'authorise', save the script and set the schedule (I recommend setting it to 'daily' at 8am).

The script to copy and paste is below. Let me know how you get on!

// Code to create an email to note any and all ad groups with an
// X% or greater drop in impressions vs the same day the previous week

// *****************************************************
//
// Created by Periscopix Ltd
//
// August 2012
//
// *****************************************************

function main() {

   //Set the threshold of impression fall to send an alert
   var threshold = 80;    //Change this to adjust the percentage fall threshold

   //Set the email address to alert in case of changes
   var strAddress = "changethis@example.com";    //Change this to adjust the email recipient 

//Get an iterator over ad groups, ordered by cost
  var adGroupsIterator = AdWordsApp.adGroups()
      .orderBy("Cost")
      .withCondition("Impressions > 99")    //This is to prevent including low volume ad groups
      .forDateRange(getOneWeekBefore(), getOneWeekBefore())
      .get();

   //Initialise the message body variable
   var messagebody = "";

  //Loop through all the ad groups
  while (adGroupsIterator.hasNext()) {

    //Get the next ad group as an object
    var adGroup = adGroupsIterator.next();

    //Get the stats for yesterday
    var yesterdayImpressions = adGroup.getStatsFor("YESTERDAY").getImpressions();

    //Get the stats for the week before
    var strOneWeekBefore = getOneWeekBefore();
    var oneWeekBeforeImpressions = adGroup.getStatsFor(strOneWeekBefore, strOneWeekBefore).getImpressions();

    //Check for a large enough drop in impressions
    if (yesterdayImpressions <= (oneWeekBeforeImpressions * ((100-threshold)/100)))
      {

      //Create a new html table line and add the values to each cell
      messagebody =
        messagebody +
        "<tr><td>" + adGroup.getCampaign().getName() +    //Campaign Name
        "</td><td>" +  adGroup.getName() +                //Ad group name
        "</td><td>" + oneWeekBeforeImpressions +          //Impressions from the previous period
        "</td><td>" + yesterdayImpressions +              //Impressions from yesterday
        "</td></tr>";                                     //End the table row
      }          
  }

  //Send an email with a list of changed ad groups
  generateMail(messagebody, strAddress, threshold);

}

function generateMail(messagebody, recipient, threshold) {

  //messagebody will be an empty string if no lines have been added
  if (messagebody != "")
     {

     //Create the html-formatted email body
     //Create the table and headers, then add the message body then close the table and add the logo
     messagebody = "<table border='1'><tr><td>Campaign</td><td>Ad Group</td><td>Previous Impressions</td><td>Yesterday Impressions</td></tr>" + messagebody;
     messagebody = messagebody + "</table>";

     //Construct the email and send it with an html body and a list of the inline images
     var subject = "Ad Groups with >" + threshold + "% Impression Drop";
     MailApp.sendEmail(recipient, subject, "", {htmlBody: messagebody,});

     }

function getOneWeekBefore() {

  //Return the date in the string YYYYMMDD
  var thisDate = new Date();
  var oneWeekBeforeDate = new Date();
  oneWeekBeforeDate.setMonth(thisDate.getMonth()+1);
  oneWeekBeforeDate.setDate(thisDate.getDate()-8);

  var strOneWeekBeforeDate = oneWeekBeforeDate.getFullYear().toString() + twoDigit(oneWeekBeforeDate.getMonth().toString()) + twoDigit(oneWeekBeforeDate.getDate().toString());

  return strOneWeekBeforeDate;

}

function twoDigit(dayormonth) {

  //If a month or day value is just one digit, convert to two digits
  if (dayormonth.length == 1)
    {
    return "0" + dayormonth;
    }
  else
    {
     return dayormonth;
    }

}

No hay comentarios:

Publicar un comentario