What This Script Does?
This JavaScript function is designed to run in the Google Ads Script environment. It automatically identifies search terms with poor click-through rates (CTR) and adds them as negative keywords to prevent your ads from showing for these underperforming queries.
function main() {
var CTR_THRESHOLD = 0.05; // 5% CTR threshold
var IMPRESSION_THRESHOLD = 10; // Minimum impressions to consider
var DATE_RANGE = 'LAST_30_DAYS'; // Time period to analyze
var query = "SELECT campaign.name, ad_group.name, search_term_view.search_term, " +
"metrics.clicks, metrics.impressions, metrics.ctr " +
"FROM search_term_view " +
"WHERE campaign.advertising_channel_type = 'SEARCH' " +
"AND metrics.impressions > " + IMPRESSION_THRESHOLD + " " +
"AND metrics.ctr < " + CTR_THRESHOLD + " " +
"AND segments.date DURING " + DATE_RANGE;
var report = AdsApp.search(query);
while (report.hasNext()) {
var row = report.next();
var campaignName = row.campaign.name;
var adGroupName = row.adGroup.name;
var searchTerm = row.searchTermView.searchTerm;
var clicks = row.metrics.clicks;
var impressions = row.metrics.impressions;
var ctr = row.metrics.ctr;
Logger.log("Campaign: " + campaignName +
", Ad Group: " + adGroupName +
", Search Term: " + searchTerm +
", Clicks: " + clicks +
", Impressions: " + impressions +
", CTR: " + (ctr * 100).toFixed(2) + "%");
// Add the search term as a negative keyword at the ad group level
var adGroupIterator = AdsApp.adGroups()
.withCondition("CampaignName = '" + campaignName.replace(/'/g, "\\'") + "'")
.withCondition("Name = '" + adGroupName.replace(/'/g, "\\'") + "'")
.get();
if (adGroupIterator.hasNext()) {
var adGroup = adGroupIterator.next();
try {
adGroup.createNegativeKeyword(searchTerm);
Logger.log("Added negative keyword '" + searchTerm + "' to ad group '" + adGroupName + "' in campaign '" + campaignName + "'");
} catch (e) {
Logger.log("Error adding negative keyword '" + searchTerm + "' to ad group '" + adGroupName + "' in campaign '" + campaignName + "': " + e);
}
} else {
Logger.log("Could not find ad group '" + adGroupName + "' in campaign '" + campaignName + "'");
}
}
}
Script Output
Campaign: Summer Sale, Ad Group: Women’s Shoes, Search Term: cheap knockoff sneakers, Clicks: 1, Impressions: 32, CTR: 3.13%
Added negative keyword ‘cheap knockoff sneakers’ to ad group ‘Women’s Shoes’ in campaign ‘Summer Sale’
Campaign: Brand Awareness, Ad Group: Electronics, Search Term: free phone giveaway, Clicks: 0, Impressions: 25, CTR: 0.00%
Added negative keyword ‘free phone giveaway’ to ad group ‘Electronics’ in campaign ‘Brand Awareness’
Campaign: Fall Collection, Ad Group: Men’s Jackets, Search Term: jacket repair shop near me, Clicks: 2, Impressions: 47, CTR: 4.26%
Added negative keyword ‘jacket repair shop near me’ to ad group ‘Men’s Jackets’ in campaign ‘Fall Collection’
Campaign: Product Launch, Ad Group: Gaming Consoles, Search Term: how to fix broken xbox, Clicks: 1, Impressions: 28, CTR: 3.57%
Added negative keyword ‘how to fix broken xbox’ to ad group ‘Gaming Consoles’ in campaign ‘Product Launch’
Campaign: Holiday Promotion, Ad Group: Gift Ideas, Search Term: cheap alternatives to, Clicks: 3, Impressions: 79, CTR: 3.80%
Added negative keyword ‘cheap alternatives to’ to ad group ‘Gift Ideas’ in campaign ‘Holiday Promotion’
Campaign: Business Solutions, Ad Group: Cloud Storage, Search Term: free cloud backup unlimited, Clicks: 0, Impressions: 18, CTR: 0.00%
Added negative keyword ‘free cloud backup unlimited’ to ad group ‘Cloud Storage’ in campaign ‘Business Solutions’
Error Handling Outputs
Campaign: Brand Awareness, Ad Group: Electronics, Search Term: free phone giveaway, Clicks: 0, Impressions: 25, CTR: 0.00%
Error adding negative keyword ‘free phone giveaway’ to ad group ‘Electronics’ in campaign ‘Brand Awareness’: Invalid keyword text
Campaign: Local Services, Ad Group: Plumbers, Search Term: bathroom installation tutorial, Clicks: 1, Impressions: 43, CTR: 2.33%
Could not find ad group ‘Plumbers’ in campaign ‘Local Services’
Key Components Breakdown
- Configuration Variables:
CTR_THRESHOLD = 0.05
: Sets a 5% CTR cutoff – any search term below this is considered underperformingIMPRESSION_THRESHOLD = 10
: Only evaluates search terms with more than 10 impressions to ensure statistical relevanceDATE_RANGE = 'LAST_30_DAYS'
: Analyzes data from the past 30 days
- Search Query:
- Pulls data from Google Ads’
search term report
- Filters for search campaigns only
- Applies the impression threshold and CTR threshold
- Restricts the date range to the specified period
- Pulls data from Google Ads’
- Data Processing Loop:
- Iterates through each under-performing search query
- Logs detailed information about each term (campaign, ad group, metrics, etc.)
- Negative Keyword Creation:
- For each underperforming search query, the script:
- Locates the correct ad group
- Adds the search term as a negative keyword at the ad group level
- Logs success or failure messages
- For each underperforming search query, the script:
Why This Is Useful?
This script automates an important optimization task that would be time-consuming to do manually. By automatically excluding search terms with poor CTR:
- It improves account quality score
- Reduces wasted ad spend on irrelevant clicks
- Focuses your budget on better-performing queries
- Maintains a cleaner, more effective account
The script includes error handling to safeguard against failures when adding negative keywords and provides comprehensive logging so you can review what changes were made.
Steps to Implement Google Ads Script
To implement Google Ads scripts effectively, I’ll walk you through the process step by step:
- Access Google Ads Scripts
- Log into your Google Ads account
- Click on “Tools & Settings” in the top navigation
- Select “Scripts” under the “Bulk Actions” section
- Create a New Script
- Click the blue “+” button
- Give your script a descriptive name that reflects its purpose
- Write or Paste Your Script
- Use the built-in editor to write JavaScript code
- Google Ads scripts use a specialized JavaScript API specific to Google Ads
- Test Your Script
- Click “Preview” to test your script without making changes
- Review logs and check for errors in the execution panel
- Authorize API Access
- Grant appropriate permissions when prompted
- This allows the script to interact with your account data
- Schedule Your Script (Optional)
- Set frequency (hourly, daily, weekly, monthly)
- Choose specific times for execution
- Save and Run Your Script
- Click “Save” to store your script
- Click “Run” to execute it immediately if needed