Configuring Google Analytics in Google Tag Manager

Google Tag Manager (GTM) allows you to set up Google Analytics, from tracking simple pageviews to custom events, without having to add additional code to your site.

In this article, we’ll walk through the process of configuring Google Analytics in GTM.

Deploying the Global Google Analytics Code

To start, you’ll need to have created a GTM account and placed the container code on your site.

First, let’s cover getting the basic Google Analytics tracking code in place across your site. Create a new tag and click within the “Tag Configuration box to choose a tag type. Select Universal Analytics.

Leave “Track Type” as “Pageview” and select “New Variable” under the Google Analytics Settings dropdown.

A new box will appear where you can insert your Google Analytics ID, which you can find in the Admin section of your Google Analytics account (Property Settings > Tracking Info > Tracking Code). Copy and paste this ID number into the “Tracking ID” box. Save and note that, in the future, you can simply select this variable instead of entering the ID each time you create a new tag.

Next, you’ll need to define the trigger, which determines where the tag containing the Google Analytics code will fire on your site. In most cases, you’ll want the code to be deployed across your entire site, so you can select All Pages. Note that you can set up a custom trigger if you only want the code to fire on certain pages or exclude pages.

Save your tag and click Submit to deploy it live.

You can use the Tag Assistant extension to verify that the code is firing properly on your site.

Tracking Custom Events

Custom events in Google Analytics allow you to record activities such as clicks, video views, or form submissions that may not be tracked by default. GTM allows you to fire events into your Analytics account based on the triggers you define. While there are many possible uses, we’ll cover a couple of common ones here.

Form Tracking

If a form has a “thank you” page, it’s simple to track a goal in Google Analytics by inserting the URL. However, many sites contain forms that submit within the same page without the URL changing. In this case, you’ll need to fire an event for the form submission instead of defining a “thank you” URL.

Thankfully, GTM’s form trigger provides a handy workaround. To get started, create a tag and select Universal Analytics. Next, choose a Track Type of Event. Now, define the event parameters; in this case, we’re using the following:

  • Category: form
  • Action: submit
  • Label: contact

Note that you’ll need to create a goal in Google Analytics utilizing the same parameters you use for the event in GTM. Next, create your trigger and choose Form Submission.

In the box that appears, configure the form trigger details. The “Wait for Tags” checkbox ensures that GTM waits for necessary tags to fire before the form submits, to ensure that all proper pieces are in place for tracking. The “Check Validation” box, if checked, only allows the tag to fire if the form is successfully submitted.

The next field defines where the trigger should be actively listening for form submissions. In this case, we’re tracking a form on a “Contact” page and define the URL accordingly. Finally, the “All Forms/Some Forms” options allow you to define a specific form to track if there are multiple.

Save your tag and test to ensure that GTM does indeed detect the submissions to fire the tag. Use the Preview feature in GTM for testing this configuration.

Note that some types of forms (such as those built using Ajax) aren’t detected by the GTM form trigger and require additional workarounds that entail development support. For a more advanced discussion, see Simo Ahava’s article on Tracking Form Engagement with Google Tag Manager.

Click Tracking

By default, Google Analytics doesn’t track clicks that don’t lead to a different URL, take a user to a different domain, or open a document such as a PDF. GTM can fire events for tracking a wide range of click activity.

As an example, let’s cover firing an event for PDF clicks. Create a Universal Analytics tag and select Event as the tag type. Define the parameters for the event:

  • Category: PDF
  • Action: Click
  • Label {{Click URL}} (this is a variable that will auto-populate the URL of the document clicked)

Next, let’s set up the trigger, choosing “Click – Just Links” as the trigger type. Under “Enable this trigger when,” select Page URL > Matches Regex and insert .* into the text field (matching all pages).

Under “This trigger fires on,” select “Some Link Clicks.” Choose Click URL > Ends with and insert .pdf into the text field. Finally, save the trigger and test it to ensure you’re indeed seeing events show up in Google Analytics for PDF clicks.

We’ve covered the basics of getting Google Analytics in place via GTM, but countless possible custom uses exist for your tracking needs! For more reading, see:

We’d love to hear how you utilize Google Tag Manager in the comments!