Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

This script will run through all your AdWords accounts and store your data in a Google Spreadsheet.

View mcc-account-reporting.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
/********************************************************************************
* This script will run through all your AdWords accounts and store your data in
* a Google Spreadsheet.
*
* @author Russell Savage <russellsavage@gmail.com>
* @version 1.0
*
* THIS SOFTWARE IS PROVIDED BY Russell Savage ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Russell Savage BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
********************************************************************************/
function main() {
MccApp.accounts().withLimit(50).executeInParallel('runOnEachAccount', 'finished');
}
 
function runOnEachAccount() {
Logger.log('Starting on: '+AdWordsApp.currentAccount().getCustomerId());
var results = getAccountReport();
Logger.log(results);
return JSON.stringify(results);
}
 
function getAccountReport() {
var OPTIONS = { includeZeroImpressions : true };
var cols = getCols();
var report = 'ACCOUNT_PERFORMANCE_REPORT';
var query = ['select',cols.join(','),'from',report,
'during','YESTERDAY'].join(' ');
var reportIter = AdWordsApp.report(query, OPTIONS).rows();
var resultsArray = [];
while(reportIter.hasNext()) {
var row = reportIter.next();
for(var i in cols) {
resultsArray.push(row[cols[i]]);
}
}
return resultsArray;
}
 
function getCols() {
return ['Date',
'ExternalCustomerId',
'AccountDescriptiveName',
'Impressions',
'Clicks',
'Cost',
'Conversions',
'ConversionValue'
];
}
 
function getTotalsCols() {
return ['Date',
'Impressions',
'Clicks',
'Cost',
'Conversions',
'ConversionValue'];
}
 
function getSpreadsheet(name) {
var fileIter = DriveApp.getFilesByName(name);
var spreadsheet;
if(fileIter.hasNext()) {
spreadsheet = SpreadsheetApp.openById(fileIter.next().getId());
} else {
//create a new one
spreadsheet = SpreadsheetApp.create(name);
}
return spreadsheet;
}
 
function addResultsToSpreadsheet(spreadsheet,accountResults,sheetName) {
var sheet = getSheet(spreadsheet,sheetName,getCols());
if(shouldAddRow(sheet,accountResults,getCols())) {
sheet.appendRow(accountResults);
}
}
 
function finished(resultsArray) {
var spreadsheetName = 'MCC Account Report';
var spreadsheet = getSpreadsheet(spreadsheetName);
var totalResults;
for(var i in resultsArray) {
var accountResults = JSON.parse(resultsArray[i].getReturnValue());
var sheetName = accountResults[getCols().indexOf('ExternalCustomerId')];
if(!sheetName) { continue; }
addResultsToSpreadsheet(spreadsheet,accountResults,sheetName);
totalResults = addToTotalResults(totalResults,accountResults);
}
var totalsSheetName = 'All Accounts';
addResultsToSpreadsheet(spreadsheet,totalResults,totalsSheetName);
if(spreadsheet.getSheetByName('Sheet1')) {
spreadsheet.deleteSheet(spreadsheet.getSheetByName('Sheet1'));
}
Logger.log('Your results are: '+spreadsheet.getUrl());
}
 
function addToTotalResults(totalResults,accountResults) {
if(!totalResults) { totalResults = ['',0,0,0,0,0]; }
totalResults[getTotalsCols().indexOf('Date')] =
accountResults[getCols().indexOf('Date')];
totalResults[getTotalsCols().indexOf('Impressions')] +=
parseInt(accountResults[getCols().indexOf('Impressions')]);
totalResults[getTotalsCols().indexOf('Clicks')] +=
parseInt(accountResults[getCols().indexOf('Clicks')]);
totalResults[getTotalsCols().indexOf('Cost')] +=
parseFloat(accountResults[getCols().indexOf('Cost')]);
totalResults[getTotalsCols().indexOf('Conversions')] +=
parseFloat(accountResults[getCols().indexOf('Conversions')]);
totalResults[getTotalsCols().indexOf('ConversionValue')] +=
parseFloat(accountResults[getCols().indexOf('ConversionValue')]);
return totalResults;
}
 
function shouldAddRow(sheet,results,cols) {
var shouldAdd = true;
var dates = sheet.getRange('A:A').getValues();
for(var i in dates) {
var dateStr = Utilities.formatDate(new Date(dates[i][0]),AdWordsApp.currentAccount().getTimeZone(),'yyyy-MM-dd');
if(dateStr === results[cols.indexOf('Date')]) {
return false;
}
}
return true;
}
 
function getSheet(spreadsheet,sheetName,columns) {
var sheet = spreadsheet.getSheetByName(sheetName);
if(!sheet) {
sheet = spreadsheet.insertSheet(sheetName);
sheet.appendRow(columns);
}
return sheet;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.