Created
July 17, 2018 18:58
-
-
Save simeonpashley/ccb54fffacb07bc71aa99edd0cab3212 to your computer and use it in GitHub Desktop.
AW Check If Account Is Offline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
OPTMYZR.COM - PPC AUTOMATION AND TOOLS | |
--------------------------------------- | |
Script by Optmyzr Inc. 2016-2017 | |
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 May 9, 2017 | |
*/ | |
var EMAIL_ADDRESS_TO_NOTIFY = "[email protected]"; | |
var NUM_HOURS_TO_CHECK = 6; | |
var METRIC_TO_CHECK = "Impressions"; | |
var DEBUG = 0; | |
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 getDateRangeYesterdayToToday() { | |
var currentDate = new Date(); | |
var tempDate = new Date(); | |
tempDate.setDate(tempDate.getDate()-1); | |
var yesterdayDate = tempDate; | |
return yesterdayDate.yyyymmdd() + "," + currentDate.yyyymmdd(); | |
} | |
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); | |
} | |
function main() { | |
var dateRange = getDateRangeYesterdayToToday(); | |
var currentDate = new Date(); | |
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(dayFactor +","+ hourFactor + " => " + currentRow["Impressions"]); | |
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 = new Date(currentDate.getTime()); | |
tempDate.setHours(tempDate.getHours() - i); | |
var hourIndexToCheck = tempDate.getDay() * 24 + tempDate.getHours(); | |
if(impressionsByHour[hourIndexToCheck] != undefined && impressionsByHour[hourIndexToCheck] != 0){ | |
foundEntry = true; | |
break; | |
} | |
} | |
if(foundEntry){ | |
Logger.log("ALL OK! The Account seems to be active in the last " + NUM_HOURS_TO_CHECK + " hours."); | |
} | |
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."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment