Set up organic keywords with Google Tag Manager

Set-up instructions for Keyword Provider with Google Tag Manager and Google Analytics.

What to expect

Using this guide, you will be able to set up Keyword Provider with Google Tag Manager and get data in the Organic Keywords reports in Google Analytics.

Google Analytics Organic Keywords Report with Provided Keyword Data

This particular guide uses the Google Analytics Settings variable. There’s another guide if you’re not using the Settings variable in the Google Analytics tag.

What you’ll need:

  • Publish rights to Google Tag Manager
  • Restricted, Full, or Owner access to your Google Search Console account

Create a Keyword Provider Account and get an Account ID

Follow this link to set up your Keyword Provider account in the customer portal.

Keep this tab open or save the Account ID for use later in this guide:

Authorize Google Search Console access to Keyword Provider

From the customer portal, give Keyword Provider access to your site’s Search Console data.

You can revoke Keyword Provider’s access to your Search Console data at any time on your Google Account permission page.

Add variables in Google Tag Manager

Add the following variables in Google Tag Manager:

  • Provided Keyword
    of type Data Layer Variable with the name RoyAppKeywordProvider.keyword
  • Provided Position
    of type Data Layer Variable with the name RoyAppKeywordProvider.position
  • Set Keyword Data
    of type Custom JavaScript with the code below:

Update the Google Analytics Settings variable

Find the Google Analytics Settings variable. It is easy to find by opening your default Universal Analytics Pageview tag and clicking the small information symbol:

  • Expand More Settings > Fields to Set and + Add Field
  • Enter the Field Name customTask, with the Value {{Set Keyword Data}}

  • Save the variable

Update the Google Analytics pageview trigger

Instead of having the Google Analytics tag trigger for all pages, we will let Keyword Provider do the keyword lookup first, and then trigger the tag.

Even if there is no keyword data, the trigger will fire for all pages as usual.

  • On the Google Analytics pageview tag, edit the triggers by clicking the small pen icon

  • Remove the All Pages trigger
  • Add and create a new Custom Event trigger called All Pages with Keyword Provider
  • Add All Pages with Keyword Provider as Event Name:

  • Select the trigger for the Google Analytics pageview tag:

Add the Keyword Provider snippet

  • Create a new Custom HTML Tag and name it Keyword Provider snippet
  • Add the trigger All Pages
  • Copy the code below and paste it in the HTML field
  • Change the Account ID to the one from the customer portal

Publish!

You’re done – publish!

How does this work?

What happens now is that on all pages, the Keyword Provider snippet will do a lookup to see if there is keyword data. If it is there, it will store the keyword data in Google Tag Manager’s data layer and notify the data layer that Keyword Provider is done.

This event is triggering the Google Analytics pageview, which has a setting variable that looks for keyword data in the data layer. If there is data there, it will overwrite the campaign source, medium, name, keyword, and ad content fields.

Modified on:
2018-05-03
Published on:
2016-12-19
Published by:
David Jurelius