Monday, July 1, 2013

Campaign and Keyword Performance Reporting

Many of the daily or weekly reporting tasks that your boss asks you to put together can be automated using AdWords Scripts. One of my readers passed along the following request for a script that would save him time each day putting together some of his performance reports. Maybe it can save you some time as well.

The following script will build out a Google Spreadsheet with five tabs:
  1. Campaign Performance Summary for the Past 7 Days
  2. Campaign Performance Month to Date
  3. Campaign Performance for Last Month
  4. Keyword Performance Summary for the Past 7 Days
  5. Daily Keyword Performance for the Past 7 Days

All you need to do to get this up and running is to create a New Google Spreadsheet and paste the url into the script. Then you can schedule this script to run daily and when you get to work in the morning, all of your data will be refreshed.


* Campaign and Keyword Summary Report
* Version: 1.2
* ChangeLog v1.1 - Removed apiVersion from reporting call
* Created By: Russ Savage

function main() {
  //These names are important. change them with caution
  var tabs = ['camp_perf_7_days','camp_perf_mtd','camp_perf_last_month','keyword_perf_7_days','keyword_perf_7_days_daily'];
  for(var i in tabs) {
    var results = runQuery(tabs[i]);

//Helper function to get or create the spreadsheet
function getSheet(tab) {
  var s_sheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
  var sheet;
  try {
    sheet = s_sheet.getSheetByName(tab);
    if(!sheet) {
      sheet = s_sheet.insertSheet(tab, 0);
  } catch(e) {
    sheet = s_sheet.insertSheet(tab, 0);
  return sheet

//Function to write the rows of the report to the sheet
function writeToSpreadsheet(tab,rows) {
  var to_write = convertRowsToSpreadsheetRows(tab,rows);
  var s_sheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
  var sheet = getSheet(tab);
  var numRows = sheet.getMaxRows();
  if(numRows < to_write.length) {
  var range = sheet.getRange(1,1,to_write.length,to_write[0].length);

//A generic function used to build and run the report query
function runQuery(tab) {
  var API_VERSION = { includeZeroImpressions : false };
  var cols = getColumns(tab);
  var report = getReport(tab);
  var date_range = getDateRange(tab);
  var where = getWhereClause(tab);
  var query = ['select',cols.join(','),'from',report,where,'during',date_range].join(' ');
  var report_iter =, API_VERSION).rows();
  var rows = [];
  while(report_iter.hasNext()) { 
  return rows;
//This function will convert row data into a format easily pushed into a spreadsheet
function convertRowsToSpreadsheetRows(tab,rows) {
  var cols = getColumns(tab);
  var ret_val = [cols];
  for(var i in rows) {
    var r = rows[i];
    var ss_row = [];
    for(var x in cols) {
  return ret_val;

//Based on the tab name, this returns the report type to use for the query
function getReport(tab) {
  if(tab.indexOf('camp_') == 0) {
  if(tab.indexOf('keyword_') == 0) {
  throw new Exception('tab name not recognized: '+tab);

//Based on the tab name, this returns the where clause for the query
function getWhereClause(tab) {
  if(tab.indexOf('camp_') == 0) {
    return 'where CampaignStatus = ENABLED';
  if(tab.indexOf('keyword_') == 0) {
    return 'where CampaignStatus = ENABLED and AdGroupStatus = ENABLED and Status = ENABLED';
  throw new Exception('tab name not recognized: '+tab);

//Based on the tab name, this returns the columns to add into the report
function getColumns(tab) {
  var ret_array = [];
  if(tab.indexOf('daily') >= 0) {
  if(tab.indexOf('keyword_') == 0) {
    ret_array = ret_array.concat(['AdGroupName',
  return ret_array.concat(['Clicks',

//Based on the tab name, this returns the date range for the data.
function getDateRange(tab) {
  if(tab.indexOf('7_days') >= 0) {
    return 'LAST_7_DAYS';
  if(tab.indexOf('mtd') >= 0) {
    return 'THIS_MONTH';
  if(tab.indexOf('last_month') >= 0) {
    return 'LAST_MONTH';
  throw new Exception('tab name not recognized: '+tab);


  1. It’s very excellent information and more real facts to provided that post.Thank you for sharing this information. 700-651 Exam Practice Test

  2. KFC Customer Satisfaction Survey. We value your candid comments and respect you taking the time to finish our survey. kfc customer survey

  3. HealthFusion Login - Are you getting trouble while log into your account, No issue, Here is the complete step by step technique. Healthfusion Login

  4. TuTuApp is just an amazing app store from where you can download everything for free. TuTuApp is available for Android and iOS devices. TuTuApp

  5. If you are looking for the complete procedure of MyAARPMedicare Login guide and rewards, forgot the password, contact details, Plans, phone numbers and other things. www myaarpmedicare com

  6. Do not know how to format your research paper? follow this link and learn!


  7. Here is everything you need to know about Dogpile web search, www Dogpile com search platform and its interesting features. dogpile search engine


  8. Here is your quick guide to Beyoncé biography, her hips, bra size, and other body measurements. Read everything about Beyoncé here. beyonce height

  9. MyAARPMedicare is one of the most popular medical insurance plans in the United States. The main reason for the popularity of MyAARPMedicare plans is its service quality and the rewards/benefits that customers.