Setup Google Tag Manager for Analytics

@Brock_Busby Yes, that’s right. Websites that don’t do full page reloads each time the visitor navigates the site need to send events to gtm/analytics. Without doing so, you will only track an event for the page where visitors arrive, but will have no data about how long they were on the site, which pages they viewed, etc.

I spent a lot of time trying to use the default “All Pageviews” trigger in GTM, but it only tracks the initial pageview. The instructions outlined here are the best practice for apps like Discourse (ember, angular, etc.). Without setting the trigger, then you will only register the first pageview per visitor, which misses a LOT of data. I suggest you use Universal Analytics instead of GTM if you don’t want to do everything in these instructions.


@neil thanks so much for the explanation! really helps provide clarity

@neil have you looked at being able to collect events (e.g., likes, shares, posts) in addition to pageviews?


No, but a plugin for that should be pretty easy to make.

I would :heart: that plugin


I installed this plugin per the directions but now I get the message

No valid tracking code found for property Cast Iron City’s default URL Make sure your pages are tagged with a supported version of the tracking code.

Can you tell me if I did something wrong? I thought I was supposed to leave GA tag blank?

Thank you,

I can’t see from that screenshot if you did anything wrong. It sounds like you didn’t connect the UA Tag in the GTM UI correctly. Follow the instructions again carefully to see if you missed something. Also make sure you click “Publish”.


Should the ga universal tracking and the ya universal link domains fields in admin be left blank?

Yes, the ga universal tracking code should be blank. Only the gtm container id field needs to be set.


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.