0 Impression Alert Script

2,445 views
Skip to first unread message

help.a...@gmail.com

unread,
Jan 24, 2018, 4:22:01 AM1/24/18
to AdWords Scripts Forum
Hello,

i got a question relatet to a Script i found and use. Yesterday i paused all Campaigns from one Account where i use this Script. The Script performs every hour and should email me when i got 0 Impressions in the past 6 hours. I paused all campaigns at 9 am in the morning and get the first Notification Email only at 1 am the following day. Is there something wrong with the Script or did i make a strange mistake?
The Script which i used is the following:

/*
   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 = "as...@asdf.com";
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.");
  }
   
}

Anthony Madrigal

unread,
Jan 24, 2018, 2:59:07 PM1/24/18
to AdWords Scripts Forum
Hello,

I tested out the script and it appears to be working for me. Can you please reply privately to author your CID and script name so I can take a closer look?

Thanks,
Anthony
AdWords Scripts Team

parmilan....@allresponsemedia.com

unread,
Dec 7, 2018, 6:16:43 PM12/7/18
to Google Ads Scripts Forum
Hi,

I have tried to use this script and I have been met with a similar issue. Did this happen to anyone else? How did you resolve this?

Thanks,
Parmilan

Hiroyuki Miyauchi (AdWords Scripts Team)

unread,
Dec 10, 2018, 1:31:28 AM12/10/18
to Google Ads Scripts Forum
Hello Parmilan,

So I could further investigate the issue, could you please provide your CID and script name as well as the details of the issue that you are encountering via Reply privately to author?

Regards,
Hiroyuki
Google Ads Scripts Team
Reply all
Reply to author
Forward
0 new messages