google-ads-apigoogle-searchgoogle-ads-script

How to exclude search terms in bulk using Google Ads script


I have a list of Google Ads search terms (more than 500) and want to exclude them from their campaigns. Google Ads UI doesn't allow me to perform bulk exclude more than 500. How can I use a Google Ads script to tackle this issue?


Solution

  • Prepare your search terms report as a Google spreadsheet. Your columns should be in this order:

    1. Campaign ID
    2. Match Type
    3. Search Term

    Once the spreadsheet is ready, get its sharable link - with the 'View' access. Use this link and insert into the following script:

    function main() {
      var SPREADSHEET_URL = "SPREADSHEET_URL"; // Replace with your Google Sheet URL
      var sheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL).getActiveSheet();
      var data = sheet.getDataRange().getValues();
    
      for (var i = 1; i < data.length; i++) { // Start from 1 to skip the header row
        var campaignId = data[i][0];
        var matchType = data[i][1].toLowerCase(); // Assuming the match type is in column B
        var negativeKeyword = data[i][2];
    
        if (campaignId && negativeKeyword) {
          addNegativeKeyword(campaignId, matchType, negativeKeyword);
        }
      }
    }
    
    function addNegativeKeyword(campaignId, matchType, keyword) {
      var campaign = AdsApp.campaigns().withIds([campaignId]).get().next();
      var negativeKeywordText = keyword;
    
      switch (matchType) {
        case 'exact match':
          negativeKeywordText = '[' + keyword + ']';
          break;
        case 'phrase match':
          negativeKeywordText = '"' + keyword + '"';
          break;
        case 'broad':
          // If you want to add a broad match negative keyword, simply remove any special characters
          negativeKeywordText = keyword.replace(/[^\w\s]/gi, '');
          break;
        default:
          Logger.log('Invalid match type: ' + matchType + ' for keyword: ' + keyword);
          return;
      }
    
      campaign.createNegativeKeyword(negativeKeywordText);
      Logger.log('Added ' + matchType + ' match negative keyword "' + keyword + '" to campaign ' + campaignId);
    }