0
votes

I've attempted to write a script that will:

  1. Iterate through all the accounts in an MCC and select those which have 'SEM' in the name.
  2. Iterate through campaigns in an account and select those which meet certain conditions.
  3. Email a list of these campaigns to myself.

The problem I have I have is with linking the account loop to the campaign loop.

So my question is; How can I specify an AdWords account when using campaignSelector in AdWords scripts?

If I can specify the account for the campaign iteration (rather than the script defaulting to the account that the script is in), I can put an array containing my chosen accounts there.

Thanks.

The script so far:

//This code is to be placed in an MCC, sift through accounts in that MCC that fit a certain criteria
//then in those selected accounts, sift through the campaigns that fit a certain criteria and add
//these to a report (report code yet to be added)
//The problem we have is getting the campaignSelector() function to ‘look’ at the account that has been passed through from the accountIterator() function

function main() {

 var mccAccount = AdWordsApp.currentAccount();
var childAccounts = MccApp.accounts();


 function accountIterator() 
  {
   var accountSelector = MccApp.accounts()
   .withCondition("AccountDescriptiveName CONTAINS 'SEM'")
   .withCondition("Status = ENABLED");

   var accountIterator = accountSelector.get();

   while (accountIterator.hasNext())
   {
     var account = accountIterator.next();
     var accountName = account.getName();
     Logger.log(accountName);
     campaignSelector(accountName);  //This might be really wrong....
                                     //Need to pass the account name through to the campaignSelector function 
                                     //so that the campaignSelector functions looks at the campaigns in the highlighted account
   }
  }


 function campaignSelector () 
    {
    //SELECT campaigns we're interested in
    var account = AdWordsApp.currentAccount(); //Guessing that we might need to use this?
    var campaignSelector = AdWordsApp.campaigns()
    .withCondition("CampaignName CONTAINS 'Shoop'")
    .withCondition("SearchExactMatchImpressionShare < 95")
    .forDateRange("LAST_7_DAYS")
    .withCondition("Status = ENABLED");

    //GET an iterator to list the selected campaigns
    var campaignIterator = campaignSelector.get();

    //ITERATE through all selected campaigns
    while (campaignIterator.hasNext()) 
     {
       var campaign = campaignIterator.next();
       //Add campaign and account info to a report – to be coded seperately
    }
  }
}
2

2 Answers

0
votes

You can use this part of code to select one account with certain contidion before your code. I hope it helps

var mccAccount = AdWordsApp.currentAccount();


while (accountIterator.hasNext()) {
  var account = accountIterator.next();
  if("condition to get certain account"){
    // Select the client account.
    MccApp.select(account);
  }
}

// Select campaigns under the client account
var campaignIterator = AdWordsApp.campaigns().get();
0
votes

I'd do something like this:

function main() {

  var mccAccount = AdWordsApp.currentAccount();
  var childAccounts = MccApp.accounts();

  var accountIterator = MccApp.accounts().get();
  while (accountIterator.hasNext())
  {
    var account = accountIterator.next();
    campaignSelector(account);
  }
}

function campaignSelector(account) {
  MccApp.select(account); // open the account that we've acquired in the previous function

  var accountName = account.getName();

  var campaignSelector = AdWordsApp.campaigns()
  .withCondition("CampaignName CONTAINS 'Shoop'")
  .withCondition("SearchExactMatchImpressionShare < 95")
  .forDateRange("LAST_7_DAYS")
  .withCondition("Status = ENABLED");

  var campaignIterator = campaignSelector.get();

  while (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    // Reporting
  }
}

I think the main problem in the code you give us is the missing MccApp.select function.