Set up Google Tag Manager for analytics

This topic will show you how to use Google Universal Analytics through Google Tag Manager. After you’re done, the Google Tag Manager API will be running on your Discourse site, so you could in theory send arbitrary events to any service that integrates with Tag Manager. Analytics is only the beginning.

:warning: For security reasons, all up to date Discourse installs have a Content Security Policy that blocks all non-Discourse scripts. This means that if you add external scripts to your GTM integration, you might have to allowlist their domains in order for the scripts to be allowed to run.

For example, if you have enabled Advertising Features or AdWords integration, you would need to add

  • https://stats.g.doubleclick.net
  • https://www.google.com

to the content security policy script src site setting.

If you have your own custom scripts in Custom HTML tags, you can follow this guide to allow them to run in Discourse.


Universal Analytics

The first thing you’ll need is to create an Analytics account. If you’re signed in with a Google account, then simply visiting https://analytics.google.com will guide you through creating an account.

Once you have a new account, create a property.

Choose “Website” as the type of app, and fill in the rest of the fields for your Discourse site. At the end, you’ll get a tracking id, which looks like UA-12345678-1. Keep it handy because you’ll need to give it to Tag Manager.

:warning: If you don’t see a Tracking ID, you have a Google Analytics 4 property. (Google Analytics 4 properties do not have a Tracking ID.) You will need to create a Universal Analytics property instead. Follow these instructions carefully, because the Universal Analytics property option is hidden under Show advanced options . Once you have created a Universal Analytics property, you can follow the instructions above to find your “UA-” Tracking ID.

Tag Manager

Visit https://tagmanager.google.com and let Google set you up. Once you’re in, you’ll be prompted to create your first “Account” and “Container”. When asked where the container will be used, choose “Web”.

Once your container is created, we need to hook up some events from the tag manager api to Google Analytics.

Variables

Go to the Variables tab on the left. Click the “New” button in the User-Defined Variables section at the bottom. We’ll be creating two variables.

Title: DL - page title
Type: Data Layer Variable
Data Layer Variable Name: page.title
Data Layer Version: 2

Title: DL - page url
Type: Data Layer Variable
Data Layer Variable Name: page.url
Data Layer Version: 2

Here’s an example:

Triggers

Now let’s create a trigger that does something when those variables arrive from the API. Click the “Triggers” tab on the left and click New. Choose “Page View” as the type, click save, and name the trigger virtualPageView.

image

Your trigger should look like this once it’s been saved.

Tags

Finally, we’re ready to add Analytics. Click the Tags tab on the left and create a new tag.

  • Tag Type: choose Google Analytics: Universal Analytics
  • Configure Tag:
    • Track Type: choose Page View
    • Google Analytics Settings: choose New Variable…
    • Variable Configuration:
      • Tracking ID: enter your tracking id from Universal Analytics (UA-xxxxxxxx-x)
      • Cookie Domain: Add these three fields exactly as shown:
        • name: cookieDomain, value: auto
        • name: title, value: {{DL - page title}} (can be chosen from a dropdown)
        • name: page, value: {{DL - page url}}
    • Triggering:
      • Choose virtualPageView
  • Save and Name Tag

image

Your Tag should look similar to this when finished:

Publish

None of this work is live until you click the Publish button on the top right of the page. It will be red if there are changes that haven’t been published.

:warning: If your Discourse site is reporting 404 errors from googletagmanager.com, it’s probably because you didn’t publish your changes.

Discourse

GTM is waiting for data, so let’s add it to our Discourse site. Go to the Settings tab of admin and search for gtm container id and enter your tag manager id. It should always be displayed on the top navigation bar of the tag manager UI.

:warning: Also remove your Analytics tracking code from the ga universal tracking code and ga tracking code settings.

Reload the page and data should be flowing through GTM to Analytics. Watch the real-time content view to see traffic and url’s of your site visitors.

More?

Now that you have pageview data going into tag manager, you could add more Tags that receive the events. Google DFP, AdWords, LinkedIn, and others are in the list.

Related Links:


Last Reviewed by @AlexDev on 2022-06-06T23:00:00Z

36 Likes

When I create a Universal Analytics property, i receive this warning:

“Universal Analytics properties will stop collecting data starting July 1, 2023. It’s recommended that you create a Google Analytics 4 property instead.”

Won’t it be a problem in near future?

image