New script to add labels to campaign within MCC

362 views
Skip to first unread message

Rens ten Tusscher

unread,
Feb 4, 2020, 5:27:05 AM2/4/20
to Google Ads Scripts Forum

Hi all, 

I'm quite new to google ads scripts, but I tried to create one that I need. What it needs to do is very simple, it just needs to add a label to all enabled campaigns within my MCC. Most important thing, it needs to add the label to all new created campaigns, as all the running campaigns already have this label attached. This is the script I created, but it's not working yet; 

function applyLabel() {
  // Retrieve a campaign, and apply a label to it. Applying labels to other
  // object types are similar.
  var campaignIterator = AdsApp.campaigns()
      .withCondition("Status = ENABLED")
      .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    campaign.applyLabel('Bulk negatives');
  }

Is there someone here that can tell me what i need to change? Thanks in advance!

Google Ads Scripts Forum Advisor

unread,
Feb 4, 2020, 10:47:37 AM2/4/20
to adwords-scripts+apn2wqfwfj66b43j...@googlegroups.com, adwords-scripts+apn2wqfwfj66b43j...@googlegroups.co, adwords...@googlegroups.com
Hello,

You will need to actually call the function applyLabel in main(). An empty main function will be in the editor when you create a new script. The script below will call and run the applyLabel function:

function main() {
  applyLabel(); //Call the function, applyLabel

}

function applyLabel() {
  // Retrieve a campaign, and apply a label to it. Applying labels to other
  // object types are similar.
  var campaignIterator = AdsApp.campaigns()
      .withCondition("Status = ENABLED")
      .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    campaign.applyLabel('Bulk negatives');
  }

However, the script above will only apply the label to the first campaign in your campaign selector (which has no particular order). In order to apply the label to all enabled campaigns, use a while-loop instead of an if-statement:

//change if to:
  while (campaignIterator.hasNext()) {

    var campaign = campaignIterator.next();
    campaign.applyLabel('Bulk negatives');
  }
 
Scripts does require basic Javascript knowledge, but you can learn more about Ads scripts through our guides.

Thanks,
Matt
Google Ads Scripts Team


ref:_00D1U1174p._5001UUzytS:ref

Rens ten Tusscher

unread,
Feb 11, 2020, 6:48:30 AM2/11/20
to Google Ads Scripts Forum
Thanks!! But this one only works on account-level, not on MCC-level right? How do I rebuild it to a MCC-level script? 

Google Ads Scripts Forum Advisor

unread,
Feb 11, 2020, 1:36:51 PM2/11/20
to adwords-scripts+apn2wqfwfj66b43j...@googlegroups.com, adwords-scripts+apn2wqfwfj66b43j...@googlegroups.co, adwords...@googlegroups.com
Hi Rens,

Please see our guide on implementing manager level scripts. You can iterate over accounts, running code on each one in the following way:

function main() {

  var accounts = AdsManagerApp.accounts().get();
  while(accounts.hasNext()) {
    var account = accounts.next();
    AdsManagerApp.select(account);         
    
    // Account level implementation here  
  
  }
}

So, you could change your script to the following:

function main() {

  var accounts = AdsManagerApp.accounts().get();
  while(accounts.hasNext()) {
    var account = accounts.next();
    AdsManagerApp.select(account);         
    
    applyLabel();  

  
  }
}

function applyLabel() {
  // Retrieve a campaign, and apply a label to it. Applying labels to other
  // object types are similar.
  var campaignIterator = AdsApp.campaigns()
      .withCondition("Status = ENABLED")
      .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    campaign.applyLabel('Bulk negatives');
  }  
}

Regards,
Reply all
Reply to author
Forward
0 new messages