Home » Martech » Scripting Cost Alerts!

Scripting Cost Alerts!

Reading Time: 2 minutes

The successful performance of paid search marketing depends upon the level of optimization acquired by advertiser’s accounts. The advertiser has to make sure that accounts generate sizeable revenue while the costs are kept under control. These cost constraints have always remained challenging in the search advertising space and Account Managers at NetElixir constantly explore better ways to remain watchful on these restraints. One method NetElixir uses is to keep an eye on the cost factor i.e., to employ an Adwords Script which runs every day and alerts the advertiser when the account level monthly cost exceeds a certain percentage of the budget. The script is explained below along with comments:

< —- Script starts after this line —– >

//The script will send an email alert if the total cost of the account exceeds 70% of the allocated budget.


function main() {
var tot_cost_mtd = _get_total_cost();
var this_month_budget = _get_monthly_budget();
if(tot_cost_mtd >= (.7*this_month_budget)){
MailApp.sendEmail(“advertiser@netelixir.com”, “Cost Alert”, “The monthly spend has touched 70% of the allocated budget. Please take necessary actions.”);
//Get the monthly budget from a Google spreadsheet in which the budget amount has to be mentioned by the advertiser

function _get_monthly_budget(){
var spreadsheet = getSpreadsheet(SURL);
var sheet = spreadsheet.getSheetByName(‘MENTION THE SHEET NAME’);
var thisMonthBudget = sheet.getRange(1,1).getValue();
return thisMonthBudget;

//Identify the total cost incurred in the Adwords account – till date, for the month

function _get_total_cost() {
var camp_rows = AdWordsApp.campaigns().get();
var tot_cost = 0;
while(camp_rows.hasNext()) {
tot_cost += camp_rows.next().getStatsFor(TIMEFRAME).getCost();
return tot_cost;

//Invoke the spreadsheet function from the URL
function getSpreadsheet(spreadsheetUrl) {
var matches = new RegExp(‘key=([^&#]*)’).exec(spreadsheetUrl);
if (!matches || !matches[1]) {
throw ‘Invalid spreadsheet URL: ‘ + spreadsheetUrl;
var spreadsheetId = matches[1];
return SpreadsheetApp.openById(spreadsheetId);

< —- Script ends before this line —– >

This ability to automate a script allows advertisers to have a vigilant observation on the cost factor and take immediate action when necessary.

Now, Adwords script writing has become much easier with the availability of many code snippets in Google Developers as well as in other search marketing forums. It is quite simple to plug in different snippets and organize them under a logical flow to achieve the advertiser’s objective. So, to the fellow advertisers who have not yet picked up Adwords scripts for their armoury, we at NetElixir say – Gear Up! It is high time!