Skip to content

Instantly share code, notes, and snippets.

@siliconvallaeys
Last active June 29, 2023 06:58
Show Gist options
  • Save siliconvallaeys/7f04956c7e2ca81606006d1bb6ce13e0 to your computer and use it in GitHub Desktop.
Save siliconvallaeys/7f04956c7e2ca81606006d1bb6ce13e0 to your computer and use it in GitHub Desktop.
Check Account Is Serving Ads
/*
OPTMYZR.COM - PPC AUTOMATION AND TOOLS
---------------------------------------
Script by Optmyzr Inc. 2016-2018
This script checks whether an AdWords account has gone offline, possibly due to a declined credit card.
It does this by checking if a selected metric (like impressions) has accrued some value over a chosen
number of hours. The user can choose the number of hours to look back so that they can account for
expected periods of non-activity (e.g. due to dayparting)
How To Use:
1. update the value for EMAIL_ADDRESS_TO_NOTIFY (use comma separated email addresses if you want to send a notification to several email addresses)
2. update the value for NUM_HOURS_TO_CHECK (set this at least as long as the duration of expected hours of inactivity. E.g. if your ads are offline for 8 hours due to dayparting, set a value of at least 9 here)
3. update the value for METRIC_TO_CHECK (normally you'd use 'Impressions' but you can also use 'Cost' or 'Conversions' if you prefer to be notofied when these metrics accrue no activity)
This script does NOT make changes to your account. It only emails when an account appears to have become inactive.
Last Updated July 6, 2018
- fixed issue for accounts outside US/Pacific timezones
*/
var EMAIL_ADDRESS_TO_NOTIFY = "[email protected]";
var NUM_HOURS_TO_CHECK = 12;
var METRIC_TO_CHECK = "Impressions";
var DEBUG = 0;
function main() {
var dateRange = getDateRangeYesterdayToToday();
if(DEBUG) Logger.log("dateRange checked: " + dateRange);
var timeZone = AdWordsApp.currentAccount().getTimeZone();
var date = new Date();
var dt =Utilities.formatDate(date, timeZone, 'MMMM dd, yyyy HH:mm:ss');
var currentDate = new Date(dt);
var queryText = "SELECT " + METRIC_TO_CHECK + ", DayOfWeek, HourOfDay FROM ACCOUNT_PERFORMANCE_REPORT DURING " + dateRange;
var result = AdWordsApp.report(queryText);
var rows = result.rows();
var daysMapping = [];
daysMapping["Sunday"] = 0;
daysMapping["Monday"] = 1;
daysMapping["Tuesday"] = 2;
daysMapping["Wednesday"] = 3;
daysMapping["Thursday"] = 4;
daysMapping["Friday"] = 5;
daysMapping["Saturday"] = 6;
var impressionsByHour = {};
while(rows.hasNext()) {
var currentRow = rows.next();
var dayFactor = daysMapping[currentRow["DayOfWeek"]];
var hourFactor = parseFloat(currentRow["HourOfDay"]);
var actualHour = dayFactor * 24 + hourFactor;
if(DEBUG) Logger.log("dow " + dayFactor +", hour "+ hourFactor + " => " + currentRow["Impressions"] + " impr.");
impressionsByHour[actualHour] = currentRow["Impressions"];
}
// check if an entry exists for any of the last 6 hours
var foundEntry = false;
var numHoursToCheck = NUM_HOURS_TO_CHECK + 1;
for(var i=1;i<numHoursToCheck;i++){
var tempDate = currentDate;
tempDate.setHours(tempDate.getHours() - 1);
var hourIndexToCheck = tempDate.getDay() * 24 + tempDate.getHours();
if(DEBUG) Logger.log("checking hour " + hourIndexToCheck);
if(impressionsByHour[hourIndexToCheck] != undefined && impressionsByHour[hourIndexToCheck] != 0){
if(DEBUG) Logger.log(tempDate.getDay() + ", " + tempDate.getHours() + " -> " + impressionsByHour[hourIndexToCheck]);
foundEntry = true;
break;
} else {
if(DEBUG) Logger.log(tempDate.getDay() + ", " + tempDate.getHours() + " -> " + impressionsByHour[hourIndexToCheck]);
}
}
if(foundEntry){
Logger.log("ALL OK! The Account seems to be active in the last " + NUM_HOURS_TO_CHECK + " hours.");
Logger.log(impressionsByHour[hourIndexToCheck] + " impressions during hour " + tempDate.getHours());
}
else {
var subject = "AdWords Account getting no impressions";
var body = "AdWords Account " + AdWordsApp.currentAccount().getName() + " (" + AdWordsApp.currentAccount().getCustomerId() + ") seems to be getting no impressions in the last 6 hours. You may want to check this out. This email was generated by an AdWords Script from Optmyzr.com.";
sendEmailNotifications(EMAIL_ADDRESS_TO_NOTIFY, subject, body, "warning");
Logger.log("WARNING: The Account seems to be inactive in the last " + NUM_HOURS_TO_CHECK + " hours.");
}
Date.prototype.yyyymmdd = function() {
var yyyy = this.getFullYear().toString();
var mm = (this.getMonth()+1).toString();
var dd = this.getDate().toString();
return yyyy + (mm[1]?mm:"0"+mm[0]) + (dd[1]?dd:"0"+dd[0]);
};
function getReportDates(time) {
var timeZone = AdWordsApp.currentAccount().getTimeZone();
var date = new Date();
var dt =Utilities.formatDate(date, timeZone, 'MMMM dd, yyyy HH:mm:ss');
today = new Date(dt);
Logger.log(today);
var reportDates = new Object();
switch(time) {
case "TODAY":
var startDate = new Date().setDate(today.getDate());
var endDate = new Date().setDate(today.getDate());
break;
case "YESTERDAY":
var startDate = new Date().setDate(today.getDate()-1);
var endDate = new Date().setDate(today.getDate()-1);
break;
}
var reportStartDate = Utilities.formatDate(new Date(startDate), "America/Los_Angeles", "yyyyMMdd");
var reportEndDate = Utilities.formatDate(new Date(endDate), "America/Los_Angeles", "yyyyMMdd");
reportDates.reportStartDate = reportStartDate;
reportDates.reportEndDate = reportEndDate;
return(reportDates);
}
function getDateRangeYesterdayToToday() {
var now = getReportDates("TODAY");
var t = now.reportStartDate;
var yesterday = getReportDates("YESTERDAY");
var y = yesterday.reportStartDate;
return y + "," + t;
}
function sendEmailNotifications(emailAddresses, subject, body, emailType ) {
if(emailType.toLowerCase().indexOf("warning") != -1) {
var finalSubject = "[Warning] " + subject + " - " + AdWordsApp.currentAccount().getName() + " (" + AdWordsApp.currentAccount().getCustomerId() + ")"
} else if(emailType.toLowerCase().indexOf("notification") != -1) {
var finalSubject = "[Notification] " + subject + " - " + AdWordsApp.currentAccount().getName() + " (" + AdWordsApp.currentAccount().getCustomerId() + ")"
}
var finalBody = body;
MailApp.sendEmail({
to:emailAddresses,
subject: finalSubject,
htmlBody: finalBody
});
if(DEBUG == 1) Logger.log("email sent to " + emailAddresses + ": " + finalSubject);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment