Setup Google Tag Manager for Analytics

I went through the setup and made sure everything is correct but I still get the notification. It is tracking visits so maybe I should ignore it?

Where are you getting that notification? I don’t see it anywhere when I visit

Just an FYI; If you are sharing a Tag Container between your main domain name and the forum as a subdomain, you will still get double tracking on the first hit if your main analytics tag is set to “All Pages”. To fix this you would need to create a “Forum Pages” Trigger that targets your forums subdomain (e.g. Page Hostname contains and set that as an exclusion rule on your primary Google Analytics targeting all pages.


Hi there!

Thanks for adding this feature! It’s actually really useful to understand user behaviour and such so we can improve the conversation in our forums…

Anyways, I’ve been trying to setup goals in Google Analytics, specifically the one where I can track how many visits resulted in a sign up. I know I’d have to use Google Tag Manager for that, but I have no idea how that would work with Discourse and how I can set it up. Can anyone shed some light on this for me?

Thanks in advance! :slight_smile:

1 Like

Hi all,

I’ve followed the tutorial and think everything was setup as told but something’s amiss.

I see that the GTM tag fires in Google Tag Assistent plugin in Chrome. But that’s about it. While running preview mode, no panel shows up in the bottom and no tags are being fired.

This might be a bit vague to troubleshoot but I’m not sure where the error lies myself.

So any support is much appreciated!

Hi Jeffrey, it seems that you forgot to add the exceptions to the content security policy as is indicated in the OP:

:loudspeaker: :warning: For security reasons, all scripts are now blocked if the Content Security Policy site setting is enabled . On hosted sites and new Discourse installs, this setting is enabled by default . It is necessary to whitelist the URLs in the site setting content security policy script src .

For Google Tag Manager add



I added the URLs in whitelist. Can you see if GTM works now?


Thank you Daniela!

This solved it for me. All works well now :slight_smile:


Two quick questions:

  1. Is there a list somewhere of all the default events in Discourse that trigger GTM events?

  2. How would one go about adding custom GTM events?

I’ve been testing this out with Google tag manager and analytics. It seems that all that needs to be done is to make a call to window.dataLayer.push({ 'event': eventName});, with eventName set to the name of the event. I’d like to get confirmation of this from someone with more knowledge of GTM though.

Once the variables, triggers, and tags for the events are configured on GTM, the following is passing the event data to Google Analytics for me:

<script type="text/discourse-plugin" version="0.8.32">
api.onAppEvent("post:created", post => {
    if (post) {
        'event': 'postCreated'

api.onAppEvent("topic:created", (post, composerModel) => {
    if (post) {
        // I am able to create a `topicCategory` variable on GTM and pass it to analytics,
        // but am not sure this is the correct approach.
            'event': 'topicCreated',
            'topicCategory': composerModel.get("")

api.onAppEvent("page:bookmark-post-toggled", post => {
    if (post && post.bookmarked) {
        const event = post.post_number === 1 ? "topicBookmarked" : "postBookmarked"
            'event': event

api.onAppEvent("page:like-toggled", (post, likeAction) => {
  let topic = post.topic;
  if (post && topic && likeAction && likeAction.acted) {
      'event': 'postLiked'

Hi @simon

It looks simple enough :slight_smile:

Is there a place where one can see a list of possible onAppEvents?

Not that I know of. Searching the Discourse code for appEvents.trigger might be the best way to find them. The appEvents that I’m seeing that could be useful are:

  • post:created
  • topic-notifications-button:changed
  • topic:created
  • page:bookmark-post-toggled
  • page:like-toggled

It would be great to have a real-world example of the kind of events on Discourse that you are wanting to track.

I think the only event that Discourse sends to GTM by default is the virtualPageView event that’s outlined in the first post in this topic.The method that sends the virtualPageView event is called whenever the user browses to a new ‘page’ on Discourse. It sets a page object that has two properties: title and url. The code is here.

The Discourse PluginApi has an addGTMPageChangedCallback method that can be used to modify the data that gets sent to Google Tag Manager on the page changed event. There is a code example in that method’s comment block.


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