Output Any Scripts Report to a Sheet: Google Ads Script

Get Alerted When Your Impression Share Drops With this Google Ads Script

Here’s another quick script in our series of free scripts we’ve been creating based on requests we receive on social media.

The script looks through all of your campaigns (or a subset of campaigns if the campaignsContain parameter is used) and finds any where the impression share has dropped more than a certain percentage between two days ago and yesterday (unfortunately today’s impression share data isn’t available via scripts).

It then sends you an alert with any of the campaigns it’s found – simple as that!

/*

    _   ___ _   _ _____ ___   _     
   /_\ / __| | | |_   _/ _ \ (_)___ 
  / _ \ (__| |_| | | || (_) || / _ \
 /_/ \_\___|\___/  |_| \___(_)_\___/


*/

var config = {
  campaignsContain: 'Brand', // Script will only look for campaigns that contain this string - leave blank to work on all campaigns
  imprShareDropThreshold: 0.1, // The percent change to trigger an alert (0.1 = 10% change)
  emails: '[email protected], [email protected]', // Comma-separated list of emails to alert 
}

function main() {
  var startDate = new Date();
  startDate.setDate(startDate.getDate() - 2);
  var formattedStartDate = Utilities.formatDate(startDate, AdsApp.currentAccount().getTimeZone(), 'EEE, MMM d, YYYY');
  startDate = Utilities.formatDate(startDate, AdsApp.currentAccount().getTimeZone(), 'YYYYMMdd');
  var endDate = new Date();
  endDate.setDate(endDate.getDate() - 1);
  var formattedEndDate = Utilities.formatDate(endDate, AdsApp.currentAccount().getTimeZone(), 'EEE, MMM d, YYYY');
  endDate = Utilities.formatDate(endDate, AdsApp.currentAccount().getTimeZone(), 'YYYYMMdd');

  var campaignFilter = config.campaignsContain.length > 0 ? ' AND CampaignName CONTAINS "' + config.campaignsContain + '" ' : '';
  var query = 'SELECT CampaignName, SearchImpressionShare, Date FROM CAMPAIGN_PERFORMANCE_REPORT WHERE CampaignStatus = ENABLED ' + campaignFilter + 'AND AdvertisingChannelType = SEARCH DURING ' + startDate + ',' + endDate;
  var report = AdsApp.report(query);
  var campaigns = {};
  var rows = report.rows();
  while (rows.hasNext()) {
    var row = rows.next();
    var campaign = row.CampaignName;
    var imprShare = parseFloat(row.SearchImpressionShare.replace('%', '')) / 100;
    var date = row.Date.replace(/-/g, '');
    if (!campaigns[campaign]) campaigns[campaign] = {};
    if (!campaigns[campaign][date]) campaigns[campaign][date] = imprShare;
  }
  
  var alerts = [];
  Object.keys(campaigns).forEach(function(campaign) {
    var difference = (campaigns[campaign][endDate] - campaigns[campaign][startDate]) / campaigns[campaign][startDate];
    campaigns[campaign].difference = difference;
    if (difference < -config.imprShareDropThreshold) {
      alerts.push('    - ' + campaign + ': from ' + (campaigns[campaign][startDate] * 100).toFixed(2) + '% to ' + (campaigns[campaign][endDate] * 100).toFixed(2) +'%');
    }
  });
  
  var message = 
      'Hello, \n\n' +
      'The following campaigns saw impression share drop by more than ' + (Math.abs(config.imprShareDropThreshold) * 100).toFixed(2) + '% between ' + formattedStartDate + ' and ' + formattedEndDate + '. \n\n' +
      alerts.join('\n') + '\n\n' +
      'Thanks,\nAcuto Scripts';
  
  if (alerts.length > 0) {
    MailApp.sendEmail(config.emails, 'Impr. Share Drop Alert!', message);
  }
}
Recent Posts
Recent Posts