MailApp Script Looping Issue - Multiple Emails

303 views
Skip to first unread message

BT

unread,
Feb 25, 2015, 12:35:08 AM2/25/15
to adwords...@googlegroups.com
Hi there,

I am using MailApp to send an email with a quick summary of account info within the MCC.

However with the code I have written I get multiple emails for each account.

Any help is appreciated in assisting to update the code, so the script sends out one email with summary of all accounts.

I have tried placing sendEmail outside the loop but that sends email with only Customer ID 3. 

Cheers,

BT



function main() {
   
  var accountSelector = MccApp
  .accounts()
  .withIds(['Customer ID 1','Customer ID 2','Customer ID 3'])           
  .withCondition("Impressions > 1")
  .forDateRange("YESTERDAY");

  var accountIterator = accountSelector.get();

  while (accountIterator.hasNext()) {
  var account = accountIterator.next(); 
  MccApp.select(account);
  account = AdWordsApp.currentAccount();
  var stats = account.getStatsFor("YESTERDAY"); 
  var body = (account.getName() + "  Clicks -" + stats.getClicks() + "  Cost -" + stats.getCost());
  
    Logger.log(body);  
            
    var recipient = "email ID";
    var subject = "Summary";
    MailApp.sendEmail(recipient, subject, body);
    
  }
}

Alexander Wang

unread,
Feb 25, 2015, 1:59:03 PM2/25/15
to adwords...@googlegroups.com
Hi,

The script as written is going to send one email for each account that your iterator returns because it's inside of the while loop. Right now that's "Customer ID 1", "Customer ID 2", and "Customer ID 3" (if they have at least one impression yesterday).

If you're looking to send a single email with info on all of these accounts, you do need to move the MailApp call outside of the while loop. You also should build up the body of the email inside of the while loop instead of setting it to an account's info. Basically, the call to "var body = ..." is overwriting what was stored in the previous iteration with the account you are currently looking at. The while loop terminates after fetching "Customer ID 3", so that's the last account saved into the "body" variable.

This is how I'd update the code:
function main() {
   
 
var accountSelector = MccApp
 
.accounts()
 
.withIds(['Customer ID 1','Customer ID 2','Customer ID 3'])          
 
.withCondition("Impressions > 1")
 
.forDateRange("YESTERDAY");

 
var accountIterator = accountSelector.get();

 
var accountInfos = [];


 
while (accountIterator.hasNext()) {
   
var account = accountIterator.next();
   
MccApp.select(account);
    account
= AdWordsApp.currentAccount();
   
var stats = account.getStatsFor("YESTERDAY");

   
var accountInfo = (account.getName() + "  Clicks -" + stats.getClicks() + "  Cost -" + stats.getCost());

   
Logger.log(accountInfo);
   
accountInfos.push(accountInfo);

 
}
 
var recipient = "email ID";
 
var subject = "Summary";

 
// This will combine all of your account information.
 
// The '\n' means newline and makes it so there's a new line in between each account.
 
var body = accountInfos.join('\n');

 
MailApp.sendEmail(recipient, subject, body);
}

I recommend taking a look at Codecademy's Javascript tutorial. They go over things like while loops and I think it's probably easiest to learn through practice.

W3Schools also has some short tutorials on Strings and arrays that might be helpful.

Cheers,
Alex

BT

unread,
Feb 25, 2015, 8:40:56 PM2/25/15
to adwords...@googlegroups.com
Thanks Alex,

Updates work well.

Appreciate your help.

Cheers

BT
Reply all
Reply to author
Forward
0 new messages