Script for n-gram analysis in Google Ads

In contextual advertising, n-gram analysis is one of the basic methods that influence both the structure of an advertising campaign and its effectiveness. This approach allows you to automatically identify important phrases and words, as well as further reduce statistics on them.

An n-gram is a sequence of n elements. From a semantic point of view, it can be a sequence of sounds, syllables, words, or letters. In practice, the N-gram is more often found as a series of words; stable phrases are called collocations. A sequence of two consecutive elements is often called a bigram, a sequence of three elements is called a trigram. At least four or more elements are designated as an N-gram, and N is replaced by the number of consecutive elements.грамма

I wrote a script that automatically collects data on search phrases, analyzes n-grams on them, and issues a report in Google Spreadsheet. Grouping works similarly to the functionality of «N-Grams» in !SEMTools, or «grouping by phrase composition» in KeyCollector

The script is written for the «New scripts experience» mode and supports version 8 of the Google Ads Api — that is, it works quite quickly.


Let’s take the phrases:

  • buy bmw moscow
  • bmw moscow car dealership

They will hold back:

  • words (n = 1):
    • buy,
    • bmw,
    • moscow,
    • car
    • dealership
  • bigrams (n = 2):
    • buy bmw,
    • bmw moscow,
    • moscow car,
    • car dealership
  • trigrams (n = 3):
    • buy bmw moscow,
    • bmw moscow car,
    • moscow car dealership

If each of the original phrases has its own statistics — we can translate it into an n-gram. And by combining data for matching n-grams from different phrases, you can collect summary statistics.

Report Content

By default, the script builds reports at the following levels::

  1. your account number,
  2. with details by campaign,
  3. with details by ad groups

N — is also set by default. Reports are compiled for phrases of 1, 2, and 3 words.

For each phrase, data is summarized by:

  • Impressions
  • To Clicks
  • CTR
  • Expense
  • Cost per Click
  • Conversions
  • Conversion rate
  • Conversion Costs
  • Revenue from conversions
  • ROAS

If you haven’t done this kind of analysis before, you should definitely try it. This approach finds significantly more insights than a simple mining of negative keywords in the search query report.

sample report

The script is also taught to take into account negative keywords and not display them in the report. This way, if the report is regularly minified and re-created, the already minified phrases will not interfere with you.

Copy the contents of the files, following the project structure, specify the id of the Google tablet, and set the script to autorun, for example, once a week. Well, or you can run it with your hands.


The necessary script settings are located in the file

function config() {
    return {
        is_mcc_account: false,
        // true - if account is MCC

        editors_mails: [
        // List of accounts that will have access to the report

        slack_url: '$/EXAMPLEURLafgjkhafhgafg',
        // Webhook url for sending a message to the slack about the readiness of the report

        custom_date_range: 180,
        // Specify the number of days to select

        custom_date_range_shift: 1,
        // Specify how many days from today we shift the selection. It is necessary in order not to take those days when statistics are late.

        impressions_threshold: 100,
        // Minimum impression threshold for a report
        clicks_threshold: 100
        // Minimum click threshold for a report

This is all that is needed to create the report.

The script files themselves, due to the size, were posted on github: put stars ^_^