# 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.

Source: https://mentionx.ai/learn/google-ads/google-ads-scripts-marketers  
Author: Dipuck Jones, founder of MentionX  
Updated: 4 November 2026  
Level: Practitioner

**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.

> **Key takeaways**
>
> - 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.*

## What 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 |

## What 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.

```javascript
// 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');
}
```

## How do you run it safely?

1. Create a Sheet and paste its URL into `SHEET_URL`.
2. Paste the script, click **Authorize**, then **Preview**.
3. Check the logs and the Sheet.
4. 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.

> **Pro move:** add a column for cost per conversion and filter for phrases with cost above your target CPA and zero conversions. Review that list weekly and add the clear ones to your negative list by hand.

*Our view:* keep scripts read-only until someone on the team can read the code. Automated changes are where scripts get expensive.

## How do you install the script safely, step by step?

1. Go to **Tools → Bulk actions → Scripts** and create a new script.
2. Paste the code and authorise it.
3. Click **Preview**: no changes are made, and you see the logs.
4. Check the output sheet or log for sensible results.
5. Schedule it (hourly, daily, weekly or monthly). Weekly suits an n-gram review.
6. Keep the script read-only: it reports, a person decides what to exclude.

## How 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 |

## Troubleshooting

| 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 |

## Practitioner 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.**

## Questions to ask before running any script

1. What does it read, and what does it change?
2. Who wrote it, and who maintains it?
3. Has it been previewed?
4. Where does the output go, and who can see it?

## What 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.

## How 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](https://mentionx.ai/learn/claude/claude-csv-analysis-ga4-ads), keeping customer data out of anything you paste.

## A 30-minute audit for this lesson

1. List scheduled scripts with owner and purpose.
2. Check which change the account.
3. Check logs of recent runs.
4. Check who can see output sheets.
5. Preview the n-gram script.

## Key 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 |

## Common 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.

## What 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.

## How 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.

## Worked 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

1. Previewed the script, checked the log, and scheduled it weekly.
2. The first run showed "salary" and "course" with cost and no conversions; both went to the negative lists after a check.
3. "Approval" showed strong conversions, so the team added exact keywords.

## Try it yourself

1. Install the script in preview mode.
2. List the top five cost words with no conversions.
3. Check each against converting terms before adding any negative.

## Quick 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.

## What 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](https://mentionx.ai/learn/google-ads/google-ads-monthly-review-routine). Or return to the [Google Ads for Marketers track](https://mentionx.ai/learn/google-ads/google-ads-for-marketers).

## Common 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.

## Sources

- Google Ads Help, [Using scripts to make automated changes](https://support.google.com/google-ads/answer/188712)
- Google for Developers, [Google Ads scripts: Product overview](https://developers.google.com/google-ads/scripts/docs/start)
- Google for Developers, [Google Ads scripts: Limits](https://developers.google.com/google-ads/scripts/docs/limits)
- Google Ads Help, [About the search terms report](https://support.google.com/google-ads/answer/2472708)
- All accessed 4 Nov 2026.

## Frequently asked 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.

---

Written and reviewed by Dipuck Jones for MentionX Learn. The HTML version of this page: https://mentionx.ai/learn/google-ads/google-ads-scripts-marketers
