Google Ads Scripts for Marketers: A Safe Search Terms N-Gram Script
Google Ads scripts for marketers: where to run them, limits, safety rules, and a read-only script that sends search term n-grams to a Sheet.
Google Ads scripts are short JavaScript programs that run inside your account on a schedule, and the safest first one is read-only: it pulls data and writes it to a Google Sheet. This lesson gives you a working search term n-gram script that changes nothing in the account.
- Scripts live under Tools → Bulk actions → Scripts and need your authorisation.
- Always use Preview first; changes made by scripts can't be undone.
- Single-account scripts run for up to 30 minutes.
- Use
AdsApp.search()with a GAQL query for reports. - Start with read-only scripts that write to a Sheet; add changes only after review.
Checked 4 November 2026 (CET) against Google Ads Help and Google developer documentation. Google Ads menus, defaults and rollouts change often, so confirm in your own account. "Our view" marks MentionX opinion. Ledgerline is a fictional company used for illustration.
01What are the limits?
| Limit | Value from Google's documentation |
|---|---|
| Run time, single account | 30 minutes |
| Manager scripts using executeInParallel with a callback | Up to 60 minutes across up to 50 child accounts |
| Schedule | Once, hourly, daily, weekly or monthly |
| Undo | Not possible: preview first |
02What does the n-gram script do?
It reads the last 30 days of search terms, splits each term into one-, two- and three-word phrases, adds up cost, clicks and conversions for each phrase, and writes the results to a Sheet. Phrases with high cost and no conversions are negative keyword candidates (lesson 8). It makes no changes to the account.
// Read-only: writes search term n-grams to a Google Sheet.
const SHEET_URL = 'PASTE_YOUR_SHEET_URL';
const MIN_COST = 20; // in account currency
function main() {
const query = 'SELECT search_term_view.search_term, metrics.cost_micros, ' +
'metrics.clicks, metrics.conversions FROM search_term_view ' +
'WHERE segments.date DURING LAST_30_DAYS AND metrics.impressions > 0';
const grams = {};
const rows = AdsApp.search(query);
while (rows.hasNext()) {
const row = rows.next();
const words = row.searchTermView.searchTerm.toLowerCase().split(/\s+/);
const cost = Number(row.metrics.costMicros) / 1e6;
for (let n = 1; n <= 3; n++) {
for (let i = 0; i + n <= words.length; i++) {
const g = words.slice(i, i + n).join(' ');
if (!grams[g]) grams[g] = [g, n, 0, 0, 0];
grams[g][2] += cost;
grams[g][3] += Number(row.metrics.clicks);
grams[g][4] += Number(row.metrics.conversions);
}
}
}
const out = Object.values(grams)
.filter(r => r[2] >= MIN_COST)
.sort((a, b) => b[2] - a[2]);
const sheet = SpreadsheetApp.openByUrl(SHEET_URL).getSheets()[0];
sheet.clear();
sheet.appendRow(['n-gram', 'words', 'cost', 'clicks', 'conversions']);
if (out.length) sheet.getRange(2, 1, out.length, 5).setValues(out);
Logger.log(out.length + ' n-grams written');
}03How do you run it safely?
- Create a Sheet and paste its URL into
SHEET_URL. - Paste the script, click Authorize, then Preview.
- Check the logs and the Sheet.
- Schedule it weekly.
Note that because a phrase can appear in many terms, cost is counted once per term containing it; use it for ranking, not accounting. Search terms reporting excludes low-volume terms for privacy, so totals won't match campaign totals.
Our view: keep scripts read-only until someone on the team can read the code. Automated changes are where scripts get expensive.
04How do you install the script safely, step by step?
- Go to Tools → Bulk actions → Scripts and create a new script.
- Paste the code and authorise it.
- Click Preview: no changes are made, and you see the logs.
- Check the output sheet or log for sensible results.
- Schedule it (hourly, daily, weekly or monthly). Weekly suits an n-gram review.
- Keep the script read-only: it reports, a person decides what to exclude.
05How do you read the n-gram output?
| Pattern | Meaning | Action |
|---|---|---|
| Word with high cost and no conversions | Waste theme | Consider a negative after checking converting terms |
| Word with good conversions | Valuable theme | Add exact keywords; write ads for it |
| Word with low volume | Not enough data | Ignore for now |
06Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
| Authorisation error | Script not authorised | Re-authorise |
| Time-out | Too much data | Shorten the date range |
| Empty output | Date range or filters exclude everything | Check the query |
07Practitioner hacks for scripts
- Keep scripts read-only by default. Reports are safe; scripts that change the account need more care and testing.
- Store scripts in version control or a shared document with a note on what each does.
- Name scheduled scripts clearly, with owner and purpose.
- Use a separate output sheet per script, shared only with the team.
- Review logs after each scheduled run in the first month.
08Questions to ask before running any script
- What does it read, and what does it change?
- Who wrote it, and who maintains it?
- Has it been previewed?
- Where does the output go, and who can see it?
09What edge cases come up?
- Large accounts can hit time limits; shorten the date range or split by campaign.
- Manager accounts need a different kind of script to run across many accounts.
- Sharing output sheets exposes account data; keep sharing to the people who need it.
- Ownership changes: if the person who authorised a script leaves, scheduled runs may stop. Check after team changes.
10How does the script fit the review routine?
Schedule the n-gram script weekly, then use its output in the monthly review (lesson 20). Anything you exclude goes into the right negative layer (lesson 8). Anything valuable becomes an exact keyword (lesson 7).
To analyse the output with an assistant, see Claude for CSV analysis of GA4 and Ads data, keeping customer data out of anything you paste.
11A 30-minute audit for this lesson
- List scheduled scripts with owner and purpose.
- Check which change the account.
- Check logs of recent runs.
- Check who can see output sheets.
- Preview the n-gram script.
12Key terms
| Term | Meaning |
|---|---|
| Script | JavaScript code that runs in Google Ads |
| Preview | Runs without making changes |
| N-gram | A word or word sequence counted across search terms |
| Schedule | How often a script runs |
13Common scenarios and fixes
The script times out. Diagnosis: Too much data. Fix: Shorten the date range or split by campaign.
Scheduled runs stopped. Diagnosis: Authorising user left or permissions changed. Fix: Re-authorise under a current owner.
Output shows nothing. Diagnosis: Filters exclude everything. Fix: Check the date range and query.
14What makes a script safe?
A safe script does only what you expect, on the data you expect, and leaves a record. The n-gram script in this lesson reads search terms and writes a report; it doesn't change bids, keywords or budgets. That keeps the risk low.
Before running any script you didn't write, read it. Look for calls that create, pause, remove or change entities, and for anything that sends data outside Google, such as emails or external URLs. If you don't understand a part, don't run it until someone who does has checked it.
Always preview first. Preview runs the code without making changes and shows logs, so you can see what it would do. Schedule only after a clean preview, and check the logs after the first few scheduled runs. Keep a list of active scripts with owner, purpose and schedule, and review it during the monthly routine.
15How can you extend the script safely?
Once the n-gram report works, small read-only additions are low risk: adding a cost-per-conversion column, a minimum cost filter, or separate tabs for Search and Performance Max terms where available. Test each change in preview before scheduling. Avoid adding automatic negatives or bid changes until the team has used the report for a few months and agreed rules for what should happen. A report that people read and act on is more valuable than automation nobody trusts.
16Worked example: Ledgerline's weekly n-grams
Ledgerline is a fictional accounts-payable software company. Details and numbers are illustrative.
Ledgerline scheduled the script weekly. In the first run, the phrase "free" appeared across many terms with high cost and no demos, and "template" showed the same pattern. The team added both as phrase match negatives to its shared list after checking they never appeared in converting terms.
Walkthrough: Ledgerline's weekly run
- Previewed the script, checked the log, and scheduled it weekly.
- The first run showed "salary" and "course" with cost and no conversions; both went to the negative lists after a check.
- "Approval" showed strong conversions, so the team added exact keywords.
17Try it yourself
- Install the script in preview mode.
- List the top five cost words with no conversions.
- Check each against converting terms before adding any negative.
18Quick quiz
1. What does Preview do?
Answer: It runs the script without making changes.
2. Which schedules can scripts use?
Answer: Hourly, daily, weekly or monthly.
3. Should the n-gram script add negatives automatically?
Answer: No; keep it read-only and let a person decide.
19What should you check before the next lesson?
- Create a Sheet and run the script in Preview.
- Check the top 20 n-grams by cost for negative candidates.
- Schedule it weekly and add it to your review routine.
- Document who owns each script in the account.
Next: A monthly Google Ads review routine. Or return to the Google Ads for Marketers track.
20Common questions
Where do I find Google Ads scripts? In Google Ads, go to Tools, then Bulk actions, then Scripts. Create a script, authorise it, preview it, then schedule it.
How long can a script run? Google's limits page says a single-account script can run for up to 30 minutes.
Can scripts changes be undone? No. Changes made by a script can't be undone automatically, so preview first and start with read-only scripts.
Questions people ask
3 answersWhere do I find Google Ads scripts?
+
In Google Ads, go to Tools, then Bulk actions, then Scripts. Create a script, authorise it, preview it, then schedule it.
How long can a script run?
+
Google's limits page says a single-account script can run for up to 30 minutes.
Can scripts changes be undone?
+
No. Changes made by a script can't be undone automatically, so preview first and start with read-only scripts.
Sources
- Google Ads Help, Using scripts to make automated changessupport.google.com
- Google for Developers, Google Ads scripts: Product overviewdevelopers.google.com
- Google for Developers, Google Ads scripts: Limitsdevelopers.google.com
- Google Ads Help, About the search terms reportsupport.google.com
- All accessed 4 Nov 2026.