This is the legacy documentation for Google Ads scripts. Go to the current docs.

Excluded Locations

Stay organized with collections Save and categorize content based on your preferences.

Add excluded locations for a campaign

function excludeLocationTarget() {
  var campaignIterator = AdsApp.campaigns()
      .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
      .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    // Exclude Tennessee, United States (location id = 21175) See
    // https://developers.google.com/adwords/api/docs/appendix/geotargeting
    // for list of all supported geo codes.
    // You could pass either the location code, or a TargetedLocation or
    // ExcludedLocation object from an existing campaign.
    var tennessee_id = 21175;
    campaign.excludeLocation(tennessee_id);
  }
}

Get excluded locations for a campaign

function getExcludedLocations() {
  var campaignIterator = AdsApp.campaigns()
      .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
      .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    var locationIterator = campaign.targeting().excludedLocations().get();

    while (locationIterator.hasNext()) {
      var excludedLocation = locationIterator.next();
      Logger.log('Location id: ' + excludedLocation.getId().toFixed(0) +
          ', Name: ' + excludedLocation.getName() + ', Country code: ' +
          excludedLocation.getCountryCode());
    }
  }
}