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

Negative Keywords

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

Add negative keyword to a campaign

function addNegativeKeywordToCampaign() {
  var campaignIterator = AdsApp.campaigns()
        .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
        .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    campaign.createNegativeKeyword('[Budget hotels]');
  }
}

Get negative keywords in a campaign

function getNegativeKeywordForCampaign() {
  var campaignIterator = AdsApp.campaigns()
      .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
      .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    var negativeKeywordIterator = campaign.negativeKeywords().get();
    while (negativeKeywordIterator.hasNext()) {
      var negativeKeyword = negativeKeywordIterator.next();
      Logger.log('Text: ' + negativeKeyword.getText() + ', MatchType: ' +
          negativeKeyword.getMatchType());
    }
  }
}

Add a negative keyword to an ad group

function addNegativeKeywordToAdGroup() {
  // If you have multiple ad groups with the same name, this snippet will
  // pick an arbitrary matching ad group each time. In such cases, just
  // filter on the campaign name as well:
  //
  // AdsApp.adGroups()
  //     .withCondition('Name = "INSERT_ADGROUP_NAME_HERE"')
  //     .withCondition('CampaignName = "INSERT_CAMPAIGN_NAME_HERE"')
  var adGroupIterator = AdsApp.adGroups()
      .withCondition('Name = "INSERT_ADGROUP_NAME_HERE"')
      .get();
  if (adGroupIterator.hasNext()) {
    var adGroup = adGroupIterator.next();
    adGroup.createNegativeKeyword('[Budget hotels]');
  }
}

Get negative keywords in an ad group

function getNegativeKeywordForAdGroup() {
  // If you have multiple ad groups with the same name, this snippet will
  // pick an arbitrary matching ad group each time. In such cases, just
  // filter on the campaign name as well:
  //
  // AdsApp.adGroups()
  //     .withCondition('Name = "INSERT_ADGROUP_NAME_HERE"')
  //     .withCondition('CampaignName = "INSERT_CAMPAIGN_NAME_HERE"')
  var adGroupIterator = AdsApp.adGroups()
      .withCondition('Name = "INSERT_ADGROUP_NAME_HERE"')
      .get();
  if (adGroupIterator.hasNext()) {
    var adGroup = adGroupIterator.next();
    var negativeKeywordIterator = adGroup.negativeKeywords().get();
    while (negativeKeywordIterator.hasNext()) {
      var negativeKeyword = negativeKeywordIterator.next();
      Logger.log('Text: ' + negativeKeyword.getText() + ', MatchType: ' +
          negativeKeyword.getMatchType());
    }
  }
}