Fetching GAds account with zero impression in the last fresh hour, return false negative results

62 views Asked by At

I've tried to write an ads-script that returns all the child accounts which had zero impression in the latest available hour (3 hours ago)

However the script returns false negative results. Meaning there was an account with zero impressions, but the script flagged it as non-zero.

What am I missing?

function main() {

  Logger.log("now.getHours(); = "+new Date().getHours());
  var past = new Date(new Date().getTime() - HOURS_BACK * 3600 * 1000);
  var pastHour = past.getHours();
  var pastDateStr = getDateStringInTimeZone(past, 'yyyy-MM-dd');
  query = "SELECT customer.id, metrics.impressions, segments.hour FROM customer WHERE metrics.impressions = 0 AND segments.hour = " + pastHour + " AND segments.date = '" + pastDateStr + "'";
  Logger.log("query " + query);
  updateAccountsInParallel();
}




function updateAccountsInParallel() {
  // You can use this approach when you have a large amount of processing
  // to do in each of your client accounts.


  // Select the accounts to be processed. You can process up to 50 accounts.
  var accountSelector = AdsManagerApp.accounts();

  // Process the account in parallel. The callback method is optional.
  accountSelector.executeInParallel('processAccount', 'allFinished', query);
}

/**
 * Process one account at a time. This method is called by the executeInParallel
 * method call in updateAccountsInParallel function for every account that
 * it processes.
 */
function processAccount(query) {
  // executeInParallel will automatically switch context to the account being
  // processed, so all calls to AdsApp will apply to the selected account.
  var customerId = AdsApp.currentAccount();
  if (excludedAccountIds.includes(customerId)) return null;
  var currentZeroImpressionRows = AdsApp.report(query, { apiVersion: 'v10' });
  var rows = currentZeroImpressionRows.rows();
  var accounts = [];
  while (rows.hasNext()) {
    var row = rows.next();
    Logger.log(JSON.stringify(row));
    accounts = accounts.push(row["customer.id"] + " " + row["customer.descriptive_name"]);
  }  

  // Optional: return a string value. If you have a more complex JavaScript
  // object to return from this method, use JSON.stringify(value). This value
  // will be passed on to the callback method, if specified, in the
  // executeInParallel method call.
  return accounts.length > 0 ? account.getCustomerId() + " " + account.getName() : null;
}

/**
 * Post-process the results from processAccount. This method will be called
 * once all the accounts have been processed by the executeInParallel method
 * call.
 *
 * @param {Array.<ExecutionResult>} results An array of ExecutionResult objects,
 * one for each account that was processed by the executeInParallel method.
 */
function allFinished(results) {
  var todayZeroCostAccounts = [];

  for (var i = 0; i < results.length; i++) {
    // Get the ExecutionResult for an account.
    var result = results[i];
    //Logger.log('Customer ID: %s; status = %s.', result.getCustomerId(),
    //  result.getStatus());

    // Check the execution status. This can be one of ERROR, OK, or TIMEOUT.
    if (result.getStatus() == 'ERROR') {
      Logger.log("-- Failed with error: '%s'.", result.getError());
    } else if (result.getStatus() == 'OK') {

      // This is the value you returned from processAccount method. If you
      // used JSON.stringify(value) in processAccount, you can use
      // JSON.parse(text) to reconstruct the JavaScript object.
      var retval = result.getReturnValue();

      if (retval != null) {
        Logger.log('%s had 0 impressions in that hour.', result.getCustomerId());
        todayZeroCostAccounts.push(retval);
      }
      else
      {
       Logger.log('%s had positive impressions in that hour.', result.getCustomerId());
      }
      
    } else {
      // Handle timeouts here.
    }
  }
}
0

There are 0 answers