How to Use Scripts for Negative Keywords: A Step-by-Step Guide for Google Ads
Google Ads scripts automate the tedious process of finding and adding negative keywords by using JavaScript to pull search term data, flag wasteful queries, and apply negatives across multiple campaigns—eliminating hours of manual spreadsheet work. This guide shows you how to use scripts for negative keywords with step-by-step instructions on accessing the scripts interface, building your first automation, scheduling runs, and troubleshooting common errors.
If you've ever spent an hour combing through search term reports, manually copying bad queries into a spreadsheet, then pasting them back into Google Ads as negatives, you already know the pain. Now imagine doing that across 20 campaigns. Or 50. That's where Google Ads scripts come in—they let you automate the entire negative keyword workflow, from pulling search term data to flagging wasteful queries and adding negatives at scale, all without touching a spreadsheet.
Scripts use JavaScript to talk directly to the Google Ads API, which means you can build custom automation that fits exactly how your account works. You're not stuck with one-size-fits-all tools or manual processes that eat up hours every week.
This guide walks you through the practical steps: accessing the scripts interface, understanding the core functions, setting up your first negative keyword script, building an automated search term miner, scheduling runs, and troubleshooting the errors that trip up most first-time users. Whether you're managing one account or dozens, this is the fastest way to scale your negative keyword strategy without losing your mind.
Step 1: Access the Google Ads Scripts Interface
First things first: you need to get into the scripts editor. In your Google Ads account, click on Tools & Settings in the top navigation. Under the Bulk Actions section, you'll see Scripts. Click that.
You'll land on a page that lists any existing scripts (probably empty if this is your first time). Click the blue plus button to create a new script. Google will open a blank editor that looks a lot like a basic code editor—white background, line numbers on the left, a big empty text area in the middle.
Before you can run anything, Google needs permission to access your account data. This is the authorization process. When you hit Preview or Run for the first time, you'll see a pop-up asking you to authorize the script. Click Authorize, then choose your Google account. Google will show you a scary-looking permissions screen that says the script wants to "manage your Google Ads campaigns." That's normal. Click Allow.
Here's the thing: some older Google Ads accounts don't have scripts enabled by default. If you don't see the Scripts option under Bulk Actions, you may need to contact Google Ads support to activate it. This is rare now, but it happens with accounts created before 2015 or accounts with unusual permission setups.
Once you're in the editor, take a minute to poke around. The top bar has buttons for Preview (runs the script without making changes), Run (executes the script live), and Logs (shows you what happened during the last run). The left sidebar shows your script name, which you can edit by clicking on "Untitled script" at the top.
Rename your script something useful like "Negative Keyword Automation" so you can find it later. The editor auto-saves as you type, but get in the habit of clicking the save icon anyway.
One last thing: scripts have a 30-minute execution limit for standard accounts. If you're running scripts on a manager (MCC) account, you get more time, but for most single-account use cases, 30 minutes is plenty. Just keep that in mind when you're building scripts that process huge amounts of data.
Step 2: Understand the Core Script Functions for Negative Keywords
Google Ads scripts use JavaScript syntax, but you don't need to be a developer to use them. You just need to understand a few key objects and methods that do the heavy lifting.
The main object you'll work with is AdsApp. Think of it as your gateway to everything in your Google Ads account. Want to access campaigns? Use AdsApp.campaigns(). Need to pull ad groups? AdsApp.adGroups(). Want to grab search term data? AdsApp.report().
For negative keywords specifically, the most important method is createNegativeKeyword(). This method adds a negative keyword to a campaign or ad group. The syntax looks like this: campaign.createNegativeKeyword("keyword text"). You can specify match types by wrapping the keyword in brackets for broad match, quotes for phrase match, or square brackets for exact match. Understanding how match types work for negative keywords is essential before you start writing scripts.
Now, here's where it gets interesting. Google Ads scripts can pull search term reports programmatically using AdsApp.report(). This method takes a GAQL query (Google Ads Query Language) as input. GAQL is basically SQL for Google Ads data. You write a query that says "give me all search terms from the last 30 days with more than 10 clicks and zero conversions," and the script returns that data as a report object.
The difference between campaign-level and ad group-level negatives matters here. Campaign-level negatives block a keyword across all ad groups in that campaign. Ad group-level negatives only block it for that specific ad group. In scripts, you add campaign-level negatives using campaign.createNegativeKeyword(), and ad group-level negatives using adGroup.createNegativeKeyword(). Most accounts use campaign-level negatives because they're simpler to manage at scale.
You'll also want to get comfortable with Logger.log(). This is your debugging friend. Anytime you want to see what your script is doing, add Logger.log("some message") and check the Logs tab after running the script. In most accounts I audit, the biggest mistake people make is running scripts blind without logging. You have no idea what got added or why until you check the change history manually.
The Google Ads scripts documentation is your best resource for learning these functions. Go to developers.google.com/google-ads/scripts and bookmark it. The docs include code examples, method references, and use case guides. When you're stuck, search the docs for the specific object or method you're trying to use. The examples are usually copy-paste ready.
Step 3: Set Up a Basic Negative Keyword Script
Let's build your first working script. This one will take a list of negative keywords and add them to specific campaigns. It's simple, but it teaches you the core workflow.
Open your script editor and paste this starter template:
function main() {
var campaignNames = ["Campaign 1", "Campaign 2"];
var negativeKeywords = ["cheap", "free", "discount"];
var campaignIterator = AdsApp.campaigns()
.withCondition("Name IN ['" + campaignNames.join("','") + "']")
.get();
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
for (var i = 0; i < negativeKeywords.length; i++) {
campaign.createNegativeKeyword("[" + negativeKeywords[i] + "]");
Logger.log("Added negative keyword: " + negativeKeywords[i] + " to " + campaign.getName());
}
}
}
Here's what this script does. The campaignNames array lists which campaigns you want to target. Replace "Campaign 1" and "Campaign 2" with your actual campaign names. The negativeKeywords array lists the keywords you want to add as negatives. Replace "cheap", "free", "discount" with your actual junk terms. If you need ideas, check out this negative keywords list for Google Ads as a starting point.
The script uses AdsApp.campaigns() to grab all campaigns that match the names in your list. Then it loops through each campaign and adds each negative keyword using createNegativeKeyword(). The square brackets around the keyword text make it exact match. If you want phrase match, use quotes instead. For broad match, leave the keyword text plain.
The Logger.log() line records what the script did. After you run the script, click Logs in the top menu to see the output. You should see lines like "Added negative keyword: cheap to Campaign 1".
Before you run this live, hit Preview. Preview mode runs the script without making changes. Check the logs to make sure it's targeting the right campaigns and keywords. If everything looks good, click Run.
What usually happens here is people forget to update the campaign names and the script runs on the wrong campaigns. Or they leave the default negative keywords in place and add "cheap" to campaigns that don't need it. Always customize the arrays before running.
If you want to pull negative keywords from a Google Sheet instead of hardcoding them in the script, you can use SpreadsheetApp.openByUrl() to connect to a sheet. That's more advanced, but it's useful if you're managing a master negative keyword list that multiple people update.
Step 4: Create an Automated Search Term Mining Script
Now let's build something more powerful: a script that analyzes your search term report and automatically flags poor performers as negatives. This is where scripts really shine—they can process thousands of search terms in seconds and apply consistent rules across your entire account.
Here's the logic: pull all search terms from the last 30 days, filter for terms with at least 10 clicks and zero conversions, then add them as negatives to the campaigns where they appeared. You can adjust the thresholds to match your account's performance standards.
Start with this template:
function main() {
var threshold = {
minClicks: 10,
maxConversionRate: 0,
maxCostPerConversion: 50
};
var query = "SELECT search_term_view.search_term, campaign.name, metrics.clicks, metrics.conversions " +
"FROM search_term_view " +
"WHERE segments.date DURING LAST_30_DAYS " +
"AND metrics.clicks >= " + threshold.minClicks + " " +
"AND metrics.conversions = 0";
var report = AdsApp.report(query);
var rows = report.rows();
while (rows.hasNext()) {
var row = rows.next();
var searchTerm = row["search_term_view.search_term"];
var campaignName = row["campaign.name"];
Logger.log("Flagged: " + searchTerm + " in " + campaignName);
}
}
This script uses AdsApp.report() to run a GAQL query against your search term data. The query pulls search terms that got at least 10 clicks in the last 30 days with zero conversions. The while loop iterates through the results and logs each flagged term. Learning how to find negative keywords in Google Ads through search term analysis is the foundation of this approach.
Right now, this script just flags terms—it doesn't add them as negatives yet. That's intentional. In most accounts I audit, the first run of a search term mining script flags way more terms than you actually want to block. You need to review the output before letting the script add negatives automatically.
To export the findings to a Google Sheet, add SpreadsheetApp code after the Logger.log() line. Create a new Google Sheet, grab its URL, and use SpreadsheetApp.openByUrl() to write the flagged terms to a sheet. Then you can review the list, remove any false positives, and run a second script that adds the approved terms as negatives.
Once you've tested the script and you're confident in your thresholds, you can add the createNegativeKeyword() method inside the while loop to automate the entire process. Just make sure you're logging everything so you can audit what the script did.
The mistake most agencies make is setting thresholds too aggressively. If you flag terms with 5 clicks and zero conversions, you'll block a lot of keywords that just haven't had time to convert yet. Start conservative—10 clicks minimum, zero conversions—and adjust based on your account's conversion cycle.
Step 5: Schedule and Monitor Your Scripts
Scripts are most useful when they run automatically. You don't want to log into Google Ads every day and click Run manually. That defeats the whole point of automation.
To schedule a script, open the script in the editor and click the three-dot menu in the top right. Select "Set up schedule" or "Add schedule" (the wording varies). Google will show you a scheduling interface where you can choose how often the script runs: hourly, daily, weekly, or monthly.
For negative keyword scripts, daily is usually the sweet spot. Hourly is overkill unless you're running massive accounts with search term data that changes by the hour. Weekly works if you're managing smaller accounts where search term volume is low. If you're unsure about timing, this guide on how often to review your negative keyword list can help you decide.
When you set up a schedule, Google also asks if you want email notifications. Turn these on. You'll get an email when the script runs successfully, and more importantly, you'll get an email if the script fails. Authorization errors, API timeouts, and syntax bugs all trigger failure emails.
After your script runs, always check the logs. Click on your script in the Scripts dashboard, then click Logs. You'll see a timestamped record of every Logger.log() call from the last run. This is how you catch issues before they impact campaigns.
Here's the thing: scripts fail silently unless you're logging and monitoring. I've seen accounts where a negative keyword script stopped working three months ago because the authorization expired, but nobody noticed because they weren't checking the logs. The script just sat there doing nothing while wasted spend piled up.
Best practice: test scripts on a small campaign first. Create a test campaign with a few ad groups and known bad search terms. Run your script against that campaign and verify it adds negatives correctly. Check the campaign's negative keyword list manually to make sure everything looks right. Once you're confident, expand the script to your full account.
Also, use preview mode religiously. Before scheduling a script, run it in preview mode at least twice. Check the logs to see what it would do if you ran it live. This catches 90% of mistakes before they become problems.
Step 6: Troubleshoot Common Script Errors
Even well-written scripts break. Here's how to fix the most common issues.
Authorization expired errors: This is the number one problem. Google's OAuth tokens expire after a few months, and when they do, your script stops working. You'll get an email that says "Authorization required." To fix it, open the script, click Run, and go through the authorization flow again. Click Authorize, choose your account, and click Allow. The script will work again.
API limits and timeouts: Google Ads scripts have execution time limits (30 minutes for standard accounts) and API rate limits. If your script processes too much data, it will time out or hit the rate limit. The fix is to batch your operations. Instead of processing all campaigns at once, process them in chunks. Use a loop that runs the script multiple times, each time processing a subset of campaigns.
Syntax errors: These show up as red text in the editor. Common ones include missing semicolons, mismatched brackets, or typos in method names. Use the preview function to catch syntax errors before running the script live. The preview mode will show you exactly which line has the error.
Scripts run but don't add negatives: This usually means your targeting conditions are wrong. Check the campaign names in your script against the actual campaign names in your account. Google Ads is case-sensitive, so "Campaign 1" is different from "campaign 1". Also check that your GAQL query is returning data. Add Logger.log() calls inside your loops to see if the script is actually finding search terms to process.
What usually happens here is people copy-paste code from the internet without understanding what it does. Then when it breaks, they have no idea how to fix it. The solution: read the Google Ads scripts documentation and understand the core methods. You don't need to be a JavaScript expert, but you need to know what AdsApp.campaigns() does and how createNegativeKeyword() works.
If you're stuck, check the Google Ads Scripts community forum. Thousands of advertisers post questions and solutions there. Most common problems already have documented fixes. You might also want to explore negative keyword tools that can complement your scripting workflow.
One issue that trips up many advertisers is conflicting negative keywords that block traffic you actually want. Always audit your negative keyword lists after running automated scripts to catch these conflicts early.
Putting It All Together: Your Negative Keyword Script Checklist
Let's recap the six steps to get your negative keyword scripts running smoothly. First, access the scripts interface under Tools & Settings and authorize your account. Second, learn the core functions: AdsApp.campaigns(), AdsApp.report(), and createNegativeKeyword(). Third, set up a basic script that adds negatives from a predefined list. Fourth, build an automated search term mining script with thresholds for clicks, conversions, and cost. Fifth, schedule your script to run daily and enable email notifications. Sixth, troubleshoot common errors like authorization issues and API timeouts.
Here's your checklist for script setup success: Test in preview mode before running live. Log everything so you can audit what the script did. Start with conservative thresholds and adjust based on results. Review the first few runs manually to catch false positives. Schedule scripts to run daily unless you have a specific reason for hourly or weekly runs. Monitor your email notifications and check logs regularly.
Now, when do scripts make sense versus other tools? Scripts are powerful for accounts with high search term volume and repetitive optimization tasks. If you're managing dozens of campaigns and processing thousands of search terms every week, scripts save massive amounts of time. But if you're running smaller accounts or you need to make quick, one-off optimizations, scripts can be overkill.
That's where tools like Keywordme come in. Instead of writing code and scheduling scripts, you can optimize directly inside the Google Ads interface with one-click actions. No spreadsheets, no scripting knowledge required. For day-to-day optimization work—removing junk search terms, adding high-intent keywords, applying match types—an extension that lives inside Google Ads is often faster than building and maintaining scripts.
For more advanced script templates, check out the Google Ads Scripts solution gallery. You'll find pre-built scripts for everything from automated bidding to budget pacing. The Free AdWords Scripts website also has a huge library of community-contributed scripts with documentation and use cases.
Scripts are a powerful tool for scaling negative keyword management, but they're not the only tool. Use them when automation makes sense, and use faster manual tools when you need quick wins. The goal is to spend less time on repetitive tasks and more time on strategy.
Optimize Google Ads Campaigns 10X Faster. Without Leaving Your Account. Keywordme lets you remove junk search terms, build high-intent keyword lists, and apply match types instantly—right inside Google Ads. No spreadsheets, no switching tabs, just quick, seamless optimization. Start your free 7-day trial (then just $12/month) and take your Google Ads game to the next level.