With Condition for all Campaigns ( or All Ad Groups, or All Ads, or All Audiences etc)

182 views
Skip to first unread message

Liam Holmes

unread,
Aug 17, 2018, 11:00:56 AM8/17/18
to AdWords Scripts Forum
Hi AdWords Scripts Group,

I notice that many of the script samples are geared for making individual actions, as opposed to account-wide changes.

For example    AdWordsApp. CampaignSelector has with.conditions to filter specific campaigns. 

In the below script example, the default template sample script is to add a phone number to one campaign. 
I would prefer to add this to all campaigns at once- as I would typically add phone numbers to all campaigns (Or, at least all search campaigns). 
I would always use the same phone number (Unless there were local offices). The same would apply to many other ad extensions- (unless product or service specific).

I thought of a few scenarios such as campaign does not contain, impressions > 10, label name contains or does not contain etc, etc. 
However, I can't quite get the selector to filter the TRUE campaigns- and then apply the action (e.g. Apply phone ad extension to all campaigns- or just a wider filter than one entity). 

can you advise,
Thanks


// --------

function main() {
  addPhoneNumber();
}


function addPhoneNumber() {
  var campaignIterator = AdWordsApp.campaigns()
      .withCondition("LabelNames CONTAINS_ANY ['all', 'any']")        // .withCondition('Name = "Text Ads Search UK"')    removed the default with condition
      .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    var phoneNumberBuilder = AdWordsApp.extensions().newPhoneNumberBuilder();
    var newPhoneNumber = phoneNumberBuilder
        .withCountry('GB')
        .withPhoneNumber('01899999999')   // example phone number
        .build()
        .getResult();

    // Add phone number to campaign. Adding phone number to ad group is
    // similar.
    campaign.addPhoneNumber(newPhoneNumber);
  }
}

Liam Holmes

unread,
Aug 17, 2018, 11:05:48 AM8/17/18
to AdWords Scripts Forum
P.s- I have also tried removing the condition altogether- but this does not seem to select all the available campaign entities.

Anthony Madrigal

unread,
Aug 17, 2018, 3:51:38 PM8/17/18
to AdWords Scripts Forum
Hi Liam,

The bolded condition in your example above is selecting a collection of campaigns. This collection of campaigns must be iterated through in order for the action to be applied to all selected campaigns. However, your code does not contain any sort of loop, so it gets the first campaign, processes it, and then the script is done. For example, the following code will loop through all campaigns and print the campaign name:
function main() {  
 
var campaignIterator = AdWordsApp.campaigns().get();  
 
while(campaignIterator.hasNext()) {  
   
var campaign = campaignIterator.next();
   
Logger.log(campaign.getName());    
 
}
}

Hope this helps. Let us know if you have any other questions.

Cheers,
Anthony 
AdWords Scripts Team

Reply all
Reply to author
Forward
0 new messages