Monthly budget pacing alert
Emails you when month-to-date spend runs more than a set percentage ahead of or behind your monthly budget, with the projected month-end spend.
Show the code28 lines
// Serann Marketing · Monthly budget pacing alert (read only, changes nothing)
// https://serann.marketing/tools/google-ads-scripts/
var CONFIG = {
EMAIL: '__EMAIL__', // where alerts go, comma separate several
MONTHLY_BUDGET: __BUDGET__, // in the account currency
ALERT_PERCENT: 10 // alert when pacing is off by this much
};
function main() {
var acc = AdsApp.currentAccount(), tz = acc.getTimeZone(), now = new Date();
var day = +Utilities.formatDate(now, tz, 'd');
var y = +Utilities.formatDate(now, tz, 'yyyy'), m = +Utilities.formatDate(now, tz, 'M');
var daysInMonth = new Date(y, m, 0).getDate();
var rows = AdsApp.search('SELECT metrics.cost_micros FROM customer WHERE segments.date DURING THIS_MONTH');
var spend = 0;
while (rows.hasNext()) spend += Number(rows.next().metrics.costMicros || 0) / 1e6;
var expected = CONFIG.MONTHLY_BUDGET * day / daysInMonth;
var projected = day > 0 ? spend / day * daysInMonth : 0;
var off = expected > 0 ? (spend - expected) / expected * 100 : 0;
var msg = 'Account: ' + acc.getName() + ' (' + acc.getCustomerId() + ')\n'
+ 'Spend this month: ' + spend.toFixed(2) + ' ' + acc.getCurrencyCode() + '\n'
+ 'Expected by today: ' + expected.toFixed(2) + '\n'
+ 'Projected month end: ' + projected.toFixed(2) + ' against a budget of ' + CONFIG.MONTHLY_BUDGET;
Logger.log(msg);
if (Math.abs(off) >= CONFIG.ALERT_PERCENT && CONFIG.EMAIL.indexOf('@') > 0) {
MailApp.sendEmail(CONFIG.EMAIL, 'Budget pacing: ' + acc.getName() + ' is ' + Math.abs(off).toFixed(0) + '% ' + (off > 0 ? 'over' : 'under'), msg);
}
}