Setup Google Tag Manager for Analytics

I’m also trying to set up Tag Manager to track how many visits end in a sign up in the site from organic and paid,but I can’t clearly understand which are the tags and events related to sign up. Anyone does?’


I want use the User-ID feature in Analytics with Tag Manager.
Can you help me?
How can i generate a user id data layer?

I think adding userId as in the screenshot below works:


With the below config, you are loading Analytics on every virtual page view. Wouldn’t be enough to use a regular page view as a trigger? I guess this would save reloading analytics when someone visits a post coming from the topic list, for example. You can still send a page view event on every virtual page view, though.

Any thoughts?

@neil, I’m curious why you decided to trigger the reload of the whole Analytics tag on each virtualPageView event. AFAIK, Google Analytics recommends against it.

I was going to change my Analytics tag to trigger on the All Pages trigger (see below), but will hold off in case I’m missing something.

Screen Shot 2020-05-27 at 16.52.20.png

I didn’t decide to trigger a reload, not sure what you mean. If there are better instructions that work with Discourse, please share and I can update the post. Keep in mind that these instructions are four years old now, so things have probably changed.


You are triggering the Google Analytics tag on each virtualPageView (see quote below). I might be wrong, but here is how I think it should work:

  • Fire the Google Analytics tag with the trigger All Pages. In the context of Discourse, given it is a SPA, it would be just on the initial page load.
  • Fire page view events with the trigger virtualPageView. If a user navigates from the topic list to a specific topic, Google Analytics is not reloaded – only the page view event is sent.
1 Like

@agmontpetit Did you ever resolve whether the original (4-year-old) instructions were correct or if there is a better way to implement GTM? As a GTM beginner, it’s hard to see what I should be doing here

After spending a bit of time with the debugging console, I see what @agmontpetit is saying and agree. If you enable debug mode for the GA tag, you can see that it creates a new tracker each time a virtualPageView occurs, which as the Google link points out is undesirable.

That said, I am still a beginner as I mentioned, so I’m going with the All Page Views load method for now, but will be trying to do some external validation somehow

Based on this reading, it appears that the Discourse GTM part is implemented correctly:

With Google Tag Manager, every single Universal Analytics Tag that fires on the site creates a new, unique tracker object.

The above linked post described the issue Google is trying to warn you of:

This means that the Document Location field is updated with every Tag you fire, which is a problem if the URL changes due to browser history manipulation. Thus you can end up with a situation where the first Universal Analytics Tag has gclid in the URL, attributing the session to AdWords, but the next pageview doesn’t have this in the URL anymore, as you would not include it in the “virtual” pageview path names. Instead, since gclid is no longer in the URL, GA looks at the HTTP referrer of the page to see what the previous page was for attribution. It finds , as you came from the search engine (HTTP referrer is not updated when manipulating the URL with the browser History API). Thus a new session starts with attribution to Google Organic! I’ve dubbed this as the Rogue Referral problem.

The post provides several solutions on how to deal with the problem, hopefully they work


Apologies for not updating the thread. I agree that the current instructions from @neil are right – I was wrong!

Most tracking software (e.g. Facebook Pixel) ask you to add a snippet that needs to be loaded first. I assumed Analytics needed the same thing – thus, I was worried this initial snippet was being loaded more than once.

It turns out that you don’t need any Analytics code snippet if you use GTM’s Analytics template. You only have the following choices (see image below), and none of them is actually loading the actual Analytics snippet – I assume Analytics tracker is created under the hood.

Screen Shot on 2020-06-09 at 12-29-04.png

Thanks for sharing the article on the Rogue Referral problem. I’ll look into it.


Thank you @agmontpetit and @randyzwitch for investigating! It’s helpful to make sure that these instructions are still correct.


@Simon_Cossar In this script, you have <script type="text/discourse-plugin" version="0.8.32">…are you serving this JavaScript from GTM or do you need to serve it from inside the Discourse app?

That script is added to the head section of a Discourse theme. The reason it is using type="text/discourse-plugin" is so the script can use the Discourse Plugin API methods. For details about how that works, see Using the PluginAPI in Site Customizations.