This guide will help you set up Google Tag Manager and use it with Google Analytics on your Discourse site. Additionally, it covers configuring variables, triggers, and tags in Google Tag Manager to track page views.
This guide covers setting up Google Tag Manager. If you only want to enable Google Analytics for your site, set the G-Tag in the
ga universal tracking code
site setting. Our settings may still refer to all analytics asuniversal
, but this is a legacy field naming artifact. The following instructions use, and work for, Google Tags.Required user level: Administrator
Integrating Google Tag Manager (GTM) with Google Analytics (GA) allows you to manage and track events on your Discourse site effectively. This guide covers the full setup process, from creating a GA account to configuring GTM with the necessary tags and triggers.
Summary
- Create a Google Analytics account and property.
- Set up Google Tag Manager.
- Configure GTM variables, triggers, and tags.
- Integrate GTM with your Discourse site.
Create a Google Analytics account
-
Visit Google Analytics and sign in with your Google account.
-
If you don’t have an account yet, follow the guided steps to create one.
-
Create a property by clicking on “Admin” and then “Create Property”.
-
Fill in the required details on the “Property creation” screen.
-
Enter your business details on the next screen.
-
Select your business objectives appropriately.
Info: Selecting the four sections will most closely match the reports Analytics has typically provided:
7. Choose “Web” as the platform for data collection.
- Set up a data stream by entering your site’s URL and a stream name.
After successfully creating your web stream, make a note of your Measurement ID. You’ll need this for GTM setup.
Set up Google Tag Manager
- Visit Google Tag Manager and create an account.
- Set up a container by following the guided steps, and choose “Web” as the platform.
Configure GTM
Variables
- Go to the “Variables” tab in GTM and click “New” in the User-Defined Variables section.
- Create two Data Layer 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
As an example, choose the variable type:
Then configure the variables, as in this example:
Triggers
-
Go to the “Triggers” tab and click “New”.
-
Choose “Custom Event” as the type.
-
Name the trigger
virtualPageView
and Save it.The end result should look like this:
Tags
-
Go to the “Tags” tab and create a new tag.
-
Choose
Google Analytics: Google Tag
as the tag type.
-
Configure the tag with the following details:
- Measurement ID: Enter your Measurement ID from GA.
-
Required Fields to Set:
-
title
:{{DL-page-title}}
-
page
:{{DL-page-url}}
-
send_page_view
:true
-
page_path
:{{Page Path}}
-
When finished it should look like this (note that you can add a userID
parameter in addition to the parameters above, but you will need to add an additional variable like DL-page-url
.):
- Choose the
virtualPageView
trigger for this tag.
- Name your tag and Save it.
Publish your GTM workspace
Publish your container in GTM by clicking the blue “Submit” button in the upper-right corner of the page.
Important: If you encounter 404 errors from googletagmanager.com
, ensure your GTM container changes have been published.
Integrate GTM with Discourse
- Go to your Discourse Admin settings and search for
gtm container id
. - Enter your GTM Container ID (format:
GTM-XXXXXX
) in the corresponding field.
Note: Remove any existing Analytics tracking code from the ga universal tracking code
and ga tracking code
settings in Discourse.
Reload your site and check the Real-Time content view in GA to confirm that data is flowing through GTM.
Additional Uses
With GTM, you can add more tags to track additional events such as:
- Google DFP
- AdWords
Related Links
- Using nonces in Google Tag Manager scripts
- Push custom events to Google Tag Manager and Analytics
- How to add Google Auto Ads to Discourse via Google Tag Manager
Last edited by @jessii 2024-08-03T01:04:37Z
Check document
Perform check on document: