MCC Account Anomaly Detector - Exclude Inactive Accounts with 0 historical impressions

39 views
Skip to first unread message

Benedicte Jeulin

unread,
Oct 29, 2024, 5:18:09 AM10/29/24
to Google Ads Scripts Forum
Hi, 

I set up the MCC Account Anomaly Detector and would like to exclude inactive accounts.
Can you please help me update the script below accordingly?
I found in a different Conversation "In the Init function (line 433-447), you can include additional withCondition to filter those zero impressions.", but I don't know how to write this condition.

Thanks!

const SPREADSHEET_URL = CONFIG.spreadsheet_url;
const REPORTING_OPTIONS = CONFIG.reporting_options;
const MCC_CHILD_ACCOUNT_LIMIT = CONFIG.mcc_child_account_limit;

const STATS = CONFIG.advanced_options.spreadsheet_setup.columns;
const CONST = CONFIG.advanced_options.spreadsheet_setup.const;

const DAYS = [
  'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday',
  'Sunday'
];

function main() {
  let mccAccount;
  const alertText = [];
  const sheetUtil = new SheetUtil();
  const mccManager = new MccManager();
  // Set up internal variables; called only once, here.
  mccManager.init();

  console.log(`Using spreadsheet - ${SPREADSHEET_URL}.`);
  const spreadsheet = validateAndGetSpreadsheet(SPREADSHEET_URL);
  spreadsheet.setSpreadsheetTimeZone(AdsApp.currentAccount().getTimeZone());

  let dataRow = CONST.FIRST_DATA_ROW;

  sheetUtil.setupData(spreadsheet, mccManager);

  console.log(`Manager account: ${mccManager.getMccAccount().getCustomerId()}`);
  while (mccAccount = mccManager.getNextAccount()) {
    console.log(`Processing account ${mccAccount.getCustomerId()}`);
    alertText.push(processAccount(mccAccount, spreadsheet, dataRow, sheetUtil));
    dataRow++;
  }

  sendEmail(mccManager.getMccAccount(), alertText, spreadsheet);
}

/**
 * For each of Impressions, Clicks, Conversions, and Cost, check to see if the
 * values are out of range. If they are, and no alert has been set in the
 * spreadsheet, then 1) Add text to the email, and 2) Add coloring to the cells
 * corresponding to the statistic.
 * @param {string} account An account of Mcc manager.
 * @param {string} spreadsheet An Url of spreadsheet.
 * @param {number} startingRow A number of a row defined in constant.
 * @param {!object} sheetUtil An object of SheetUtil class.
 * @return {string} the next piece of the alert text to include in the email.
 */
function processAccount(account, spreadsheet, startingRow, sheetUtil) {
  const sheet = spreadsheet.getSheets()[0];

  const thresholds = sheetUtil.getThresholds();
  const today = AdsApp.search(sheetUtil.getTodayQuery(), REPORTING_OPTIONS);
  const past = AdsApp.search(sheetUtil.getPastQuery(), REPORTING_OPTIONS);

  const hours = sheetUtil.getHourOfDay();
  const todayStats = accumulateRows(today, hours, 1);  // just one week
  const pastStats = accumulateRows(past, hours, sheetUtil.getWeeksToAvg());

  let alertText = [`Account ${account.getCustomerId()}`];
  const validWhite = ['', 'white', '#ffffff'];  // these all count as white

  // Colors cells that need alerting, and adds text to the alert email body.
  function generateAlert(field, emailAlertText) {
    // There are 2 cells to check, for Today's value and Past value
    const bgRange = [
      sheet.getRange(startingRow, STATS[field].Column, 1, 1),
      sheet.getRange(
          startingRow, STATS[field].Column + STATS.NumOfColumns, 1, 1)
    ];
    const bg = [bgRange[0].getBackground(), bgRange[1].getBackground()];

    // If both backgrounds are white, change background Colors
    // and update most recent alert time.
    if ((-1 != validWhite.indexOf(bg[0])) &&
        (-1 != validWhite.indexOf(bg[1]))) {
      bgRange[0].setBackground([[STATS[field]['Color']]]);
      bgRange[1].setBackground([[STATS[field]['Color']]]);

      spreadsheet.getRangeByName(STATS[field]['AlertRange'])
          .setValue(`Alert at ${hours}:00`);
      alertText.push(emailAlertText);
    }
  }

  if (thresholds.Impressions &&
      todayStats.Impressions < pastStats.Impressions * thresholds.Impressions) {
    generateAlert(
        `Impressions`,
        `    Impressions are too low: ${todayStats.Impressions}` +
            ` Impressions by ${hours}:00, expecting at least ` +
            `${parseInt(pastStats.Impressions * thresholds.Impressions, 10)}`);
  }

  if (thresholds.Clicks &&
      todayStats.Clicks < (pastStats.Clicks * thresholds.Clicks).toFixed(1)) {
    generateAlert(
        `Clicks`,
        `    Clicks are too low: ${todayStats.Clicks}` +
            ` Clicks by ${hours}:00, expecting at least ` +
            `${(pastStats.Clicks * thresholds.Clicks).toFixed(1)}`);
  }

  if (thresholds.Conversions &&
      todayStats.Conversions <
          (pastStats.Conversions * thresholds.Conversions).toFixed(1)) {
    generateAlert(
        `Conversions`,
        `    Conversions are too low: ${todayStats.Conversions}` +
            ` Conversions by ${hours}:00, expecting at least ` +
            `${(pastStats.Conversions * thresholds.Conversions).toFixed(1)}`);
  }

  if (thresholds.Cost &&
      todayStats.Cost > (pastStats.Cost * thresholds.Cost).toFixed(2)) {
    generateAlert(
        `Cost`,
        `    Cost is too high: ${todayStats.Cost} ` +
            `${account.getCurrencyCode()} by ${hours}` +
            `:00, expecting at most ` +
            `${(pastStats.Cost * thresholds.Cost).toFixed(2)}`);
  }

  // If no alerts were triggered, we will have only the heading text. Remove it.
  if (alertText.length === 1) {
    alertText = [];
  }

  const dataRows = [[
    account.getCustomerId(), todayStats.Impressions, todayStats.Clicks,
    todayStats.Conversions, todayStats.Cost, pastStats.Impressions.toFixed(0),
    pastStats.Clicks.toFixed(1), pastStats.Conversions.toFixed(1),
    pastStats.Cost.toFixed(2)
  ]];

  sheet
      .getRange(
          startingRow, CONST.FIRST_DATA_COLUMN, 1, CONST.TOTAL_DATA_COLUMNS)
      .setValues(dataRows);

  return alertText;
}

class SheetUtil {
  constructor() {
    this.thresholds = {};
    this.upToHour = 1;  // default
    this.weeks = 26;    // default
    this.todayQuery = '';
    this.pastQuery = '';
  }

  /**
   * A function to set the data from spreadsheet.
   */
  setupData(spreadsheet, mccManager) {
    console.log('Running setupData');
    spreadsheet.getRangeByName('date').setValue(new Date());
    spreadsheet.getRangeByName('account_id')
        .setValue(mccManager.getMccAccount().getCustomerId());

    const thresholds = this.thresholds;

    function getThresholdFor(field) {
      thresholds[field] =
          parseField(spreadsheet.getRangeByName(field).getValue());
    }

    getThresholdFor('Impressions');
    getThresholdFor('Clicks');
    getThresholdFor('Conversions');
    getThresholdFor('Cost');

    const now = new Date();

    // Basic reporting statistics are usually available with no more than
    // a 3-hour delay.
    const upTo = new Date(now.getTime() - 3 * 3600 * 1000);
    this.upToHour = parseInt(getDateStringInTimeZone('H', upTo), 10);

    spreadsheet.getRangeByName('timestamp')
        .setValue(
            `${DAYS[getDateStringInTimeZone('u', now)]}, ${this.upToHour}:00`);

    if (this.upToHour === 1) {
      // First run of the day, clear existing alerts.
      spreadsheet.getRangeByName(STATS['Clicks']['AlertRange']).clearContent();
      spreadsheet.getRangeByName(STATS['Impressions']['AlertRange'])
          .clearContent();
      spreadsheet.getRangeByName(STATS['Conversions']['AlertRange'])
          .clearContent();
      spreadsheet.getRangeByName(STATS['Cost']['AlertRange']).clearContent();

      // Reset background and font Colors for all data rows.
      const bg = [];
      const ft = [];
      const bg_single = [
        'white', 'white', 'white', 'white', 'white', 'white', 'white', 'white',
        'white'
      ];
      const ft_single = [
        'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black',
        'black'
      ];

      // Construct a 50-row array of colors to set.
      for (let a = 0; a < MCC_CHILD_ACCOUNT_LIMIT; ++a) {
        bg.push(bg_single);
        ft.push(ft_single);
      }

      const dataRegion = spreadsheet.getSheets()[0].getRange(
          CONST.FIRST_DATA_ROW, CONST.FIRST_DATA_COLUMN,
          MCC_CHILD_ACCOUNT_LIMIT, CONST.TOTAL_DATA_COLUMNS);

      dataRegion.setBackgrounds(bg);
      dataRegion.setFontColors(ft);
    }

    const weeksStr = spreadsheet.getRangeByName('weeks').getValue();
    this.weeks = parseInt(weeksStr.substring(0, weeksStr.indexOf(' ')), 10);

    const dateRangeToCheck = getDateStringInPast(0, upTo);
    const dateRangeToEnd = getDateStringInPast(1, upTo);
    const dateRangeToStart = getDateStringInPast(1 + this.weeks * 7, upTo);
    const fields = `segments.hour, segments.day_of_week, metrics.clicks, ` +
        `metrics.impressions, metrics.conversions, metrics.cost_micros`;

    this.todayQuery = `SELECT ${fields} FROM customer ` +
        `WHERE segments.date BETWEEN "${dateRangeToCheck}" ` +
        `AND "${dateRangeToCheck}"`;
    this.pastQuery = `SELECT ${fields} FROM customer ` +
        `WHERE segments.day_of_week=` +
        `${DAYS[getDateStringInTimeZone('u', now)].toUpperCase()} ` +
        `AND segments.date BETWEEN "${dateRangeToStart}" ` +
        `AND "${dateRangeToEnd}"`;
  }

  /**
   * Returns the thresholds.
   *
   * @return {!Object} An object of thresholds data.
   */
  getThresholds() {
    return this.thresholds;
  }

  /**
   * Returns the hour ofdDay.
   *
   * @return {number} A value of uptoHour.
   */
  getHourOfDay() {
    return this.upToHour;
  }

  /**
   * Returns the Weeks .
   *
   * @return {number} A value of weeks.
   */
  getWeeksToAvg() {
    return this.weeks;
  }

  /**
   * Returns the past query.
   *
   * @return {string} Past query is returned.
   */
  getPastQuery() {
    return this.pastQuery;
  }

  /**
   * Returns the today query.
   *
   * @return {string} Today query is returned.
   */
  getTodayQuery() {
    return this.todayQuery;
  }
}

function sendEmail(account, alertTextArray, spreadsheet) {
  let bodyText = '';

  for (const alertText of alertTextArray) {
    if (alertText.length != 0) {
      bodyText += alertText.join('\n') + '\n\n';
    }
  }
  bodyText = bodyText.trim();

  const email = spreadsheet.getRangeByName('email').getValue();
  if (bodyText.length > 0 && email && email.length > 0 &&
      email != 'benedict...@1800gotjunk.com') {
    console.log('Sending Email');
    MailApp.sendEmail(
        email, `Google Ads Account ${account.getCustomerId()} misbehaved.`,
        `Your account ${account.getCustomerId()}` +
            ` is not performing as expected today: \n\n` +
            `${bodyText}\n\n` +
            `Log into Google Ads and take a look: ` +
            `ads.google.com\n\nAlerts dashboard: ` +
            `${SPREADSHEET_URL}`);
  } else if (bodyText.length === 0) {
    console.log('No alerts triggered. No email being sent.');
  }
}

/**
 * Converts the value passed as number into a float value.
 *
 * @param {number} value that needs to be converted.
 * @return {number} A value that is of type float.
 */
function toFloat(value) {
  value = value.toString().replace(/,/g, '');
  return parseFloat(value);
}

/**
 * Converts the value passed to a float value.
 *
 * @param {number} value that needs to be converted.
 * @return {number} A value that is of type float.
 */
function parseField(value) {
  if (value === 'No alert') {
    return null;
  } else {
    return toFloat(value);
  }
}

/**
 * Converts the metrics.cost_micros by dividing it by a million
 * @param {number} value that needs to be converted.
 * @return {string} A value that is of type string.
 */
function toFloatFromMicros(value) {
  value = parseFloat(value);
  return (value / 1000000).toFixed(2);
}

/**
 * Accumulate stats for a group of rows up to the hour specified.
 *
 * @param {!Object} rowsIter The result of query as a iterator over the rows.
 * @param {number} hours The limit hour of day for considering the report rows.
 * @param {number} weeks The number of weeks for the past stats.
 * @return {!Object} Stats aggregated up to the hour specified.
 */
function accumulateRows(rowsIter, hours, weeks) {
  let result = {Clicks: 0, Impressions: 0, Conversions: 0, Cost: 0};
  while (rowsIter.hasNext()) {
    const row = rowsIter.next();
    const hour = row['segments']['hour'];
    if (hour < hours) {
      result = addRow(row, result, 1 / weeks);
    }
  }

  return result;
}

/**
 * Adds two stats rows together and returns the result.
 *
 * @param {!Object} row An individual row on which average operations is
 *     performed for every property.
 * @param {!Object} previous object initialized as 0 for every property.
 * @param {number} coefficient To get the Average of the properties.
 * @return {!Object} The addition of two stats rows.
 */
function addRow(row, previous, coefficient) {
  coefficient = coefficient || 1;
  row = row || {Clicks: 0, Impressions: 0, Conversions: 0, Cost: 0};
  previous = previous || {Clicks: 0, Impressions: 0, Conversions: 0, Cost: 0};
  return {
    Clicks:
        parseInt(row['metrics']['clicks'], 10) * coefficient + previous.Clicks,
    Impressions: parseInt(row['metrics']['impressions'], 10) * coefficient +
        previous.Impressions,
    Conversions: parseInt(row['metrics']['conversions'], 10) * coefficient +
        previous.Conversions,
    Cost: toFloatFromMicros(row['metrics']['costMicros']) * coefficient +
        previous.Cost
  };
}

function checkInRange(today, yesterday, coefficient, field) {
  const yesterdayValue = yesterday[field] * coefficient;
  if (today[field] > yesterdayValue * 2) {
    console.log(`${field} too much`);
  } else if (today[field] < yesterdayValue / 2) {
    console.log(`${field} too little`);
  }
}

/**
 * Produces a formatted string representing a date in the past of a given date.
 *
 * @param {number} numDays The number of days in the past.
 * @param {date} date A date object. Defaults to the current date.
 * @return {string} A formatted string in the past of the given date.
 */
function getDateStringInPast(numDays, date) {
  date = date || new Date();
  const MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
  const past = new Date(date.getTime() - numDays * MILLIS_PER_DAY);
  return getDateStringInTimeZone('yyyy-MM-dd', past);
}


/**
 * Produces a formatted string representing a given date in a given time zone.
 *
 * @param {string} format A format specifier for the string to be produced.
 * @param {date} date A date object. Defaults to the current date.
 * @param {string} timeZone A time zone. Defaults to the account's time zone.
 * @return {string} A formatted string of the given date in the given time zone.
 */
function getDateStringInTimeZone(format, date, timeZone) {
  date = date || new Date();
  timeZone = timeZone || AdsApp.currentAccount().getTimeZone();
  return Utilities.formatDate(date, timeZone, format);
}


/**
 * Module that deals with fetching and iterating through multiple accounts.
 */
class MccManager {
  constructor() {
    this.accountIterator = '';
    this.mccAccount = '';
    this.currentAccount = '';
  }

  /**
   * One-time initialization function.
   */
  init() {
    const accountSelector = AdsManagerApp.accounts();

    // Use this to limit the accounts that are being selected in the report.
    if (CONFIG.account_label) {
      accountSelector.withCondition(
          'LabelNames CONTAINS \'' + CONFIG.account_label + '\'');
    }

    accountSelector.withLimit(MCC_CHILD_ACCOUNT_LIMIT);
    this.accountIterator = accountSelector.get();

    this.mccAccount = AdsApp.currentAccount();  // save the mccAccount
    this.currentAccount = AdsApp.currentAccount();
  }

  /**
   * After calling this, AdsApp will have the next account selected.
   * If there are no more accounts to process, re-selects the original
   * MCC account.
   *
   * @return {AdsApp.Account} The account that has been selected.
   */
  getNextAccount() {
    if (this.accountIterator.hasNext()) {
      this.currentAccount = this.accountIterator.next();
      AdsManagerApp.select(this.currentAccount);
      return this.currentAccount;
    } else {
      AdsManagerApp.select(this.mccAccount);
      return null;
    }
  }

  /**
   * Returns the currently selected account. This is cached for performance.
   *
   * @return {AdsApp.Account} The currently selected account.
   */
  getCurrentAccount() {
    return this.currentAccount;
  }

  /**
   * Returns the original MCC account.
   *
   * @return {AdsApp.Account} The original account that was selected.
   */
  getMccAccount() {
    return this.mccAccount;
  }
}

/**
 * Validates the provided spreadsheet URL and email address
 * to make sure that they're set up properly. Throws a descriptive error message
 * if validation fails.
 *
 * @param {string} spreadsheeturl The URL of the spreadsheet to open.
 * @return {Spreadsheet} The spreadsheet object itself, fetched from the URL.
 * @throws {Error} If the spreadsheet URL or email hasn't been set
 */
function validateAndGetSpreadsheet(spreadsheeturl) {
  if (spreadsheeturl === 'YOUR_SPREADSHEET_URL') {
    throw new Error(
        'Please specify a valid Spreadsheet URL. You can find' +
        ' a link to a template in the associated guide for this script.');
  }
  const spreadsheet = SpreadsheetApp.openByUrl(spreadsheeturl);
  const email = spreadsheet.getRangeByName('email').getValue();
  if ('f...@example.com' === email) {
    throw new Error(
        'Please either set a custom email address in the' +
        ' spreadsheet, or set the email field in the spreadsheet to blank' +
        ' to send no email.');
  }
  return spreadsheet;
}

Google Ads Scripts Forum Advisor

unread,
Oct 29, 2024, 9:52:45 AM10/29/24
to adwords...@googlegroups.com

Hi,

Thank you for reaching out to the Google Ads Scripts support team.

I would like to inform you that the Account Anomaly Detector script for MCC accounts have been deprecated and are removed from the Google Ads Script documentation. I would suggest that you use the Account Anomaly Detector - Single Account script in child accounts under the MCC account so that the script will work as intended. 

Please note that we explicitly do not support the deprecated solutions that no longer appear on the developers site. The solutions we do support are the ones that are in the Google Ads account (UI) "Tools > Bulk Actions > Solutions (only in child accounts)" menu.

I hope this helps! Feel free to get back to us if you still face any issues. 

This message is in relation to case "ref:!00D1U01174p.!5004Q02vGhBO:ref" (ADR-00273727)

Thanks,
 
Google Logo Google Ads Scripts Team


Nils Rooijmans

unread,
Oct 30, 2024, 4:14:16 AM10/30/24
to Google Ads Scripts Forum
what you could do, is skip an account based on the account id, you would need to add one line to do so:

 while (mccAccount = mccManager.getNextAccount()) {
    console.log(`Processing account ${mccAccount.getCustomerId()}`);
    if ( mccAccount.getCustomerId() == 'INSERT CLIENT ID HERE') continue;

    alertText.push(processAccount(mccAccount, spreadsheet, dataRow, sheetUtil));
    dataRow++;
  }

Hope this helps,

Nils Rooijmans
https://nilsrooijmans.com
See my Google Ads Scripts FAQ to avoid the same mistakes I made: https://nilsrooijmans.com/google-ads-scripts-faq/

Reply all
Reply to author
Forward
0 new messages