Thursday, December 13, 2012

[Gd] AdWords Analysis in Google Apps Script

| More

Google Apps Developer Blog: AdWords Analysis in Google Apps Script

Editor’s Note: Guest author David Fothergill works at QueryClick, a search-engine marketing company based in the UK. — Eric Koleda

Working in Paid Search account management, I've often found tremendous wins from making reports more useful and efficient. Refining your analytics allows you to streamline your workflow, allowing more time for strategic and proactive thinking — and that's what we're paid for, not endless number-crunching.

The integration between Google Analytics and Apps Script has opened up lots of opportunities for me to make life easier through automation. In a recent blog post on my agency's website, I outlined how an automated report can quickly “heatmap” conversion rate by time and day. The aim of the report is to provide actionable analysis to inform decisions on day-part bidding and budget strategies.

In that post, I introduce the concepts and provide the scripts, sheet, and instructions to allow anyone to generate the reports by hooking the scripts up to their own account. Once the initial sheet has been created, the script only requires the user to provide a Google Analytics profile number and a goal for which they want to generate heatmaps. In this post, we’ll break down the code a bit.


Querying the API

This is a slight amendment to the code that queries the Core Reporting API. Apart from customising the optArgs dimensions to use day and hour stats, I have modified it to use goal data from the active spreadsheet, because not all users will want to measure the same goals:


function getReportDataForProfile(ProfileId, goalNumber) {
//take goal chosen on spreadsheet and select correct metric
var tableId = 'ga:' + ProfileId;
if (goalNumber === 'eCommerce Trans.') {
var goalId = 'ga:Transactions' ;
} else {
var goalId = 'ga:goal' + goalNumber + 'Completions';
}
// Continue as per example in google documentation ...
}

Pivoting the Data

Once we’ve brought the Google Analytics data into the spreadsheet in raw form, we use a pivot table to plot the hour of the day against the day of the week.

For this type of report, I'd like to use conditional formatting to heatmap the data — but conditional formatting in Google Sheets is based on fixed values, whereas we want the thresholds to change based on cell values. However, thanks to the flexibility of scripts, I was able to achieve dynamic conditional formatting.


Conditional Formatting Using Scripts

The script needs to know the boundaries of our data, so I’ve set up several cells that display the maximums, minimums, and so forth. Once these were in place, the next step was to create a function that loops through the data and calculates the desired background color for each cell:


function formatting() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().
getSheetByName('Heatmap');

var range = sheet.getRange('B2:H25');
range.setBackgroundColor('white');
var values = range.getValues()

//get boundaries values for conditional formatting
var boundaries = sheet.getRange('B30:B35').getValues();

//get range to 'heatmap'
var backgroundColours = range.getBackgroundColors();

for (var i = 0; i < values.length; i++) {
for (var j = 0; j < values[i].length; j++) {
// Over 90%
if (values[i][j] > boundaries[1][0]) {
backgroundColours[i][j] = '#f8696b';
}
// Between 80% and 90%
if (values[i][j] < boundaries[1][0]
&& values[i][j] >= boundaries[2][0]) {
backgroundColours[i][j] = '#fa9a9c';
}
// Between 60% and 80%
if (values[i][j] < boundaries[2][0]
&& values[i][j] >= boundaries[3][0]) {
backgroundColours[i][j] = '#fbbec1';
}
// Between 40% and 60%
if (values[i][j] < boundaries[3][0]
&& values[i][j] >= boundaries[4][0]) {
backgroundColours[i][j] = '#fcdde0';
}
// Between 20% and 40%
if (values[i][j] < boundaries[4][0]
&& values[i][j] >= boundaries[5][0]) {
backgroundColours[i][j] = '#ebf0f9';
}
// Less than 20%
if (values[i][j] < boundaries[5][0]) {
backgroundColours[i][j] = '#dce5f3';
}
}
}
// set background colors as arranged above
range.setBackgroundColors(backgroundColours);
}

Calling the functions based on the profile ID and goal number specified in the main sheet gives us a quick, actionable report that can easily be adapted for use across multiple accounts.


function generateHeatmap() {
try {
var profileId = SpreadsheetApp.getActiveSpreadsheet()
.getSheetByName('Heatmap').getRange(4,10).getValue();
var goalNumber = SpreadsheetApp.getActiveSpreadsheet()
.getSheetByName('Heatmap').getRange(7,10).getValue();
if (profileId === '') {
Browser.msgBox('Please enter a valid Profile ID');
} else {
var results = getReportDataForProfile(profileId, goalNumber);
outputToSpreadsheet(results);
formatting();
}
} catch(error) {
Browser.msgBox(error.message);
}
}

This was my first foray into the slick integration between the Core Reporting API and spreadsheets, but has proven a valuable test case for how effective it will be to roll this method of reporting into our daily process of managing accounts.

We have now started the next steps, which involves building out “client dashboards” that will allow account managers access to useful reports at the press of a button. This moves us toward the goal of minimizing the time gathering and collating data, freeing it up to add further value to client projects.

Editor's Note: If you're interested in further scripting your AdWords accounts, take a look at AdWords Scripts, a version of Apps Script that's embedded right into the AdWords interface.

David Fothergill   profile | twitter | LinkedIn

Guest author David Fothergill is a Project Director at the search marketing agency QueryClick, focusing on Paid Search and Conversion Optimisation. He has been working in the field for around 8 years and currently handles a range of clients for the company, in verticals ranging from fashion and retail through to industrial services.


URL: http://googleappsdeveloper.blogspot.com/2012/12/adwords-analysis-in-google-apps-script.html

No comments: