This script extends Multi Bidder to run for multiple accounts under a manager account.
Multi Bidder offers functionality similar to that of Automated Rules based on a spreadsheet. Each row in a spreadsheet is in effect equivalent to an entire Automated Rule. Managing these rules in a spreadsheet is easier than managing them in Google Ads.
The spreadsheet above demonstrates a single rule that
- Examines last week's statistics for Customer ID:
918-501-8835
. - Finds all keywords in
Mobile Campaign
that received less than 5 clicks and whose CTR is greater than 25%. - Increases their bids by 10%, while not exceeding $1.40.
How it works
The script works the same way as the single customer Multi Bidder, but with
an extra Customer ID
column that contains the customer IDs to which you want
the automated rule to be applied. Only one customer ID is supported per row, so
create multiple rows if you want the same rule to apply on multiple accounts.
The account running the script should be a Google Ads client account, not a manager account.
Setup
- Set up a spreadsheet-based script with the source code below. Use the Manager Multi Bidder template spreadsheet.
- Update
YOUR_SPREADSHEET_URL
in your copy of the code example. - Schedule the script as required.
Scheduling
The most common scheduling options for bidding rules are Daily and Weekly, though in some cases, you might not want to schedule the script at all.
Be mindful of how the schedule frequency interplays with the statistics date range you select. Since Google Ads statistics can be delayed by up to 3 hours, avoid scheduling your script Hourly.
// Copyright 2015, Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @name MCC Multi Bidder
*
* @overview The MCC Multi Bidder script offers functionality similar to that of
* Automated Rules based on a spreadsheet. The script runs for multiple
* accounts under an MCC account. See
* https://developers.google.com/google-ads/scripts/docs/solutions/adsmanagerapp-multi-bidder
* for more details.
*
* @author Google Ads Scripts Team [adwords-scripts@googlegroups.com]
*
* @version 2.0
*
* @changelog
* - version 2.0
* - Updated to use new Google Ads scripts features.
* - version 1.0.3
* - Replaced deprecated keyword.getMaxCpc() and keyword.setMaxCpc().
* - version 1.0.2
* - Added validation of user settings.
* - version 1.0.1
* - Improvements to time zone handling.
* - version 1.0
* - Released initial version.
*/
// The spreadsheet URL. This should be a copy of https://goo.gl/Au1RQF
const SPREADSHEET_URL = 'YOUR_SPREADSHEET_URL';
function main() {
const spreadsheetAccess = new SpreadsheetAccess(SPREADSHEET_URL, 'Rules');
// Make sure the spreadsheet is using the account's timezone.
spreadsheetAccess.spreadsheet.setSpreadsheetTimeZone(
AdsApp.currentAccount().getTimeZone());
prepareSheet(spreadsheetAccess);
let row = spreadsheetAccess.nextRow();
while (row != null) {
let argument;
let stopLimit;
try {
argument = parseArgument(spreadsheetAccess, row);
stopLimit = parseStopLimit(spreadsheetAccess, row);
} catch (e) {
logError(spreadsheetAccess, e);
row = spreadsheetAccess.nextRow();
continue;
}
let customerId = row[spreadsheetAccess.CUSTOMERID_INDEX];
let account = null;
try {
const accountIterator = AdsManagerApp.accounts()
.withIds([customerId]).get();
if (accountIterator.totalNumEntities() == 0) {
throw (`Missing account: ${customerId}`);
} else {
account = accountIterator.next();
}
} catch (e) {
logError(spreadsheetAccess, e);
row = spreadsheetAccess.nextRow();
continue;
}
AdsManagerApp.select(account);
let selector = buildSelector(spreadsheetAccess, row);
let keywords = selector.get();
try {
keywords.hasNext();
} catch (e) {
logError(e);
row = spreadsheetAccess.nextRow();
continue;
}
let action = row[spreadsheetAccess.RULE_INDEX];
let results = applyRules(keywords, action, argument, stopLimit);
logResult(`${spreadsheetAccess}, Fetched ${results.fetched} \nChanged ` +
`${results.changed}`);
row = spreadsheetAccess.nextRow();
}
spreadsheetAccess.spreadsheet.getRangeByName('last_execution')
.setValue(new Date());
}
/**
* Prepares the spreadsheet for saving data.
*
* @param {Object} spreadsheetAccess the SpreadsheetAccess instance that
* handles the spreadsheet.
*/
function prepareSheet(spreadsheetAccess) {
// Clear the results column.
spreadsheetAccess.sheet.getRange(
spreadsheetAccess.START_ROW,
spreadsheetAccess.RESULTS_COLUMN_INDEX + spreadsheetAccess.START_COLUMN,
spreadsheetAccess.MAX_COLUMNS, 1).clear();
}
/**
* Builds a keyword selector based on the conditional column headers in the
* spreadsheet.
*
* @param {Object} spreadsheetAccess the SpreadsheetAccess instance that
* handles the spreadsheet.
* @param {Object} row the spreadsheet header row.
* @return {Object} the keyword selector, based on spreadsheet header settings.
*/
function buildSelector(spreadsheetAccess, row) {
const columns = spreadsheetAccess.getColumnHeaders();
const selector = AdsApp.keywords();
for (let i = spreadsheetAccess.FIRST_CONDITIONAL_COLUMN;
i < spreadsheetAccess.RESULTS_COLUMN_INDEX; i++) {
const header = columns[i];
let value = row[i];
if (!isNaN(parseFloat(value)) || value.length > 0) {
if (header.indexOf("'") > 0) {
value = value.replace(/\'/g, "\\'");
} else if (header.indexOf('\"') > 0) {
value = value.replace(/"/g, '\\\"');
}
const condition = header.replace('?', value);
selector.withCondition(condition);
}
}
selector.forDateRange(spreadsheetAccess.spreadsheet
.getRangeByName('date_range').getValue());
return selector;
}
/**
* Applies the rules in the spreadsheet.
*
* @param {Object} keywords the keywords selector.
* @param {String} action the action to be taken.
* @param {String} argument the parameters for the operation specified by
* action.
* @param {Number} stopLimit the upper limit to the bid value when applying
* rules.
* @return {Object} the number of keywords that were fetched and modified.
*/
function applyRules(keywords, action, argument, stopLimit) {
let fetched = 0;
let changed = 0;
for (const keyword of keywords) {
let oldBid = keyword.bidding().getCpc();
let newBid = 0;
fetched++;
if (action == 'Add') {
newBid = addToBid(oldBid, argument, stopLimit);
} else if (action == 'Multiply by') {
newBid = multiplyBid(oldBid, argument, stopLimit);
} else if (action == 'Set to First Page Cpc' ||
action == 'Set to Top of Page Cpc') {
let newBid = action == 'Set to First Page Cpc' ?
keyword.getFirstPageCpc() : keyword.getTopOfPageCpc();
let isPositive = newBid > oldBid;
newBid = applyStopLimit(newBid, stopLimit, isPositive);
}
if (newBid < 0) {
newBid = 0.01;
}
newBid = newBid.toFixed(2);
if (newBid != oldBid) {
changed++;
}
keyword.bidding().setCpc(newBid);
}
return {
'fetched': fetched,
'changed': changed
};
}
/**
* Adds a value to an existing bid, while applying a stop limit.
*
* @param {Number} oldBid the existing bid.
* @param {Number} argument the bid increment to apply.
* @param {Number} stopLimit the cutoff limit for modified bid.
* @return {Number} the modified bid.
*/
function addToBid(oldBid, argument, stopLimit) {
return applyStopLimit(oldBid + argument, stopLimit, argument > 0);
}
/**
* Multiplies an existing bid by a value, while applying a stop limit.
*
* @param {Number} oldBid the existing bid.
* @param {Number} argument the bid multiplier.
* @param {Number} stopLimit the cutoff limit for modified bid.
* @return {Number} the modified bid.
*/
function multiplyBid(oldBid, argument, stopLimit) {
return applyStopLimit(oldBid * argument, stopLimit, argument > 1);
}
/**
* Applies a cutoff limit to a bid modification.
*
* @param {Number} newBid the modified bid.
* @param {Number} stopLimit the bid cutoff limit.
* @param {Boolean} isPositive true, if the stopLimit is an upper cutoff limit,
* false if it a lower cutoff limit.
* @return {Number} the modified bid, after applying the stop limit.
*/
function applyStopLimit(newBid, stopLimit, isPositive) {
if (stopLimit) {
if (isPositive && newBid > stopLimit) {
newBid = stopLimit;
} else if (!isPositive && newBid < stopLimit) {
newBid = stopLimit;
}
}
return newBid;
}
/**
* Parses the argument for an action on the spreadsheet.
*
* @param {Object} spreadsheetAccess the SpreadsheetAccess instance that
* handles the spreadsheet.
* @param {Object} row the spreadsheet action row.
* @return {Number} the parsed argument for the action.
* @throws error if argument is missing, or is not a number.
*/
function parseArgument(spreadsheetAccess, row) {
if (row[spreadsheetAccess.ARGUMENT_INDEX].length == 0 &&
(row[spreadsheetAccess.RULE_INDEX] == 'Add' ||
row[spreadsheetAccess.RULE_INDEX] == 'Multiply by')) {
throw ('\"Argument\" must be specified.');
}
const argument = parseFloat(row[spreadsheetAccess.ARGUMENT_INDEX]);
if (isNaN(argument)) {
throw 'Bad Argument: must be a number.';
}
return argument;
}
/**
* Parses the stop limit for an action on the spreadsheet.
*
* @param {Object} spreadsheetAccess the SpreadsheetAccess instance that
* handles the spreadsheet.
* @param {Object} row the spreadsheet action row.
* @return {Number} the parsed stop limit for the action.
* @throws error if the stop limit is not a number.
*/
function parseStopLimit(spreadsheetAccess, row) {
if (row[spreadsheetAccess.STOP_LIMIT_INDEX].length == 0) {
return null;
}
const limit = parseFloat(row[spreadsheetAccess.STOP_LIMIT_INDEX]);
if (isNaN(limit)) {
throw 'Bad Argument: must be a number.';
}
return limit;
}
/**
* Logs the error to the spreadsheet.
*
* @param {Object} spreadsheetAccess the SpreadsheetAccess instance that
* handles the spreadsheet.
* @param {String} error the error message.
*/
function logError(spreadsheetAccess, error) {
console.log(error);
spreadsheetAccess.sheet.getRange(spreadsheetAccess.currentRow(),
spreadsheetAccess.RESULTS_COLUMN_INDEX +
spreadsheetAccess.START_COLUMN, 1, 1)
.setValue(error)
.setFontColor('#c00')
.setFontSize(8)
.setFontWeight('bold');
}
/**
* Logs the results to the spreadsheet.
*
* @param {Object} spreadsheetAccess the SpreadsheetAccess instance that
* handles the spreadsheet.
* @param {String} result the result message.
*/
function logResult(spreadsheetAccess, result) {
spreadsheetAccess.sheet.getRange(spreadsheetAccess.currentRow(),
spreadsheetAccess.RESULTS_COLUMN_INDEX +
spreadsheetAccess.START_COLUMN, 1, 1)
.setValue(result)
.setFontColor('#444')
.setFontSize(8)
.setFontWeight('normal');
}
/**
* Controls access to the data spreadsheet.
*
* @param {String} spreadsheetUrl the spreadsheet url.
* @param {String} sheetName name of the spreadsheet that contains the bid
* rules.
* @constructor
*/
function SpreadsheetAccess(spreadsheetUrl, sheetName) {
/**
* Gets the next row in sequence.
*
* @return {?Array.<Object> } the next row, or null if there are no more
* rows.
* @this SpreadsheetAccess
*/
this.nextRow = function() {
for (; this.rowIndex < this.cells.length; this.rowIndex++) {
if (this.cells[this.rowIndex][0]) {
return this.cells[this.rowIndex++];
}
}
return null;
};
/**
* The current spreadsheet row.
*
* @return {Number} the current row.
* @this SpreadsheetAccess
*/
this.currentRow = function() {
return this.rowIndex + this.START_ROW - 1;
};
/**
* The total number of data columns for the spreadsheet.
*
* @return {Number} the total number of data columns.
* @this SpreadsheetAccess
*/
this.getTotalColumns = function() {
let totalCols = 0;
const columns = this.getColumnHeaders();
for (let i = 0; i < columns.length; i++) {
if (columns[i].length == 0 || columns[i] == this.RESULTS_COLUMN_HEADER) {
totalCols = i;
break;
}
}
return totalCols;
};
/**
* Gets the list of column beaders.
*
* @return {Array.<String>} the list of column headers.
* @this SpreadsheetAccess
*/
this.getColumnHeaders = function() {
return this.sheet.getRange(
this.HEADER_ROW,
this.START_COLUMN,
1,
this.MAX_COLUMNS - this.START_COLUMN + 1).getValues()[0];
};
/**
* Gets the results column index.
*
* @return {Number} the results column index.
* @throws exception if results column is missing.
* @this SpreadsheetAccess
*/
this.getResultsColumn = function() {
let columns = this.getColumnHeaders();
let totalColumns = this.getTotalColumns();
if (columns[totalColumns] != 'Results') {
throw ('Results column is missing.');
}
return totalColumns;
};
/**
* Initializes the class methods.
*
* @this SpreadsheetAccess
*/
this.init = function() {
this.HEADER_ROW = 5;
this.FIRST_CONDITIONAL_COLUMN = 4;
this.START_ROW = 6;
this.START_COLUMN = 2;
console.log(`Using spreadsheet - ${spreadsheetUrl}.`);
this.spreadsheet = validateAndGetSpreadsheet(spreadsheetUrl);
this.sheet = this.spreadsheet.getSheetByName(sheetName);
this.RESULTS_COLUMN_HEADER = 'Results';
this.MAX_ROWS = this.sheet.getMaxRows();
this.MAX_COLUMNS = this.sheet.getMaxColumns();
this.CUSTOMERID_INDEX = 0;
this.RULE_INDEX = 1;
this.ARGUMENT_INDEX = 2;
this.STOP_LIMIT_INDEX = 3;
this.RESULTS_COLUMN_INDEX = this.getResultsColumn();
this.cells = this.sheet.getRange(this.START_ROW, this.START_COLUMN,
this.MAX_ROWS, this.MAX_COLUMNS).getValues();
this.rowIndex = 0;
};
this.init();
}
/**
* Validates the provided spreadsheet URL
* to make sure that it's set up properly. Throws a descriptive error message
* if validation fails.
*
* @param {string} spreadsheeturl The URL of the spreadsheet to open.
* @return {Spreadsheet} The spreadsheet object itself, fetched from the URL.
* @throws {Error} If the spreadsheet URL hasn't been set
*/
function validateAndGetSpreadsheet(spreadsheeturl) {
if (spreadsheeturl == 'YOUR_SPREADSHEET_URL') {
throw new Error('Please specify a valid Spreadsheet URL. You can find' +
' a link to a template in the associated guide for this script.');
}
return SpreadsheetApp.openByUrl(spreadsheeturl);
}