I am attempting to run an MCC level script that monitors the Impression level and will generate an alert email if any Account Impressions drop below the set threshold however, I am receiving
I have checked parenthesis and capitalization but nothing seems to help. Please take a moment to see the script below and let me know if you have any suggestions.
function main() {
var dateRange = getDateRangeYesterdayToToday();
var currentDate = new Date();
var accountIterator = MccApp.accounts().get();
while ( accountIterator.hasNext() ) {
var account = accountIterator.next();
MccApp.select(account);
var queryText = 'SELECT ' + METRIC_TO_CHECK + '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.");
}
}}