Add Plausible Analytics or Fathom tracking to Discourse

To add support for Plausible Analytics to your Discourse instance

First, add your domain to your Plausible Analytics instance and grab your tracking code. It should look something like this:

<script async defer data-domain="community.example.com" src="https://plausible.example.com/js/plausible.js"></script>

Next, you do need to make a modification for this to work with Discourse’s single-page Ember app.

  • Change plausible.js to plausible.hash.js.

The final result should look like this:

<script async defer data-domain="community.example.com" src="https://plausible.example.com/js/plausible.hash.js"></script>

Finally, create a new theme component from Admin > Customize > Themes. Then paste the modified tracking code in the </head> section. Click Save.

Make sure to add this to all available themes so you can track analytics across all users!

That’s it!

To add support for Fathom

Follow the same steps as above, but use the following script.

<script src="https://cdn.usefathom.com/script.js" data-spa="auto" data-site="YOUR_SITE_ID" defer></script>

Be sure to replace YOUR_SITE_ID with your Fathom site ID.

For further info, see Fathom’s guide here:

24 Likes

According to Plausible’s roadmap on Github it seems that it’s going to be possible sometime in the near future to embed one’s Plausible page into one’s website.

https://github.com/plausible/analytics/discussions/213

Would something like this be embeddable into Discourse, or might it require a plugin, or…?

4 Likes

You need a theme-component for that.

6 Likes

Hey @Falco, has anything changed about your frontend routing? I can see you recommend people use the hash version of our script which is provided for hash-based routers. It seems to me like Discourse is using the HTML5 PushState API now, is that correct?

2 Likes

Not sure why @justin picked the hash version here, since we use HTML5 History API since forever. Made the OP a wiki, so it’s editable by all.

1 Like

I use plausible and I can confirm that the regular plausible.js does NOT log the urls if a user moves from topic to topic. So if I need accurate representation of my visitors, I have to use the hashed plausible js.

I have same issue with the hashed version though

Weird to have these conflicting reports. Theoretically since Discourse uses the HTML5 history API, I would think that our standard script (not the hash script) would be the one that tracks correctly.

Unfortunately I don’t have a discourse instance set up that I could test with.

Probably my lack of knowledge about that :slight_smile: I originally went with the hash version due to us being a SPA and having custom tracking for other analytics providers. If plausible.js works, feel free to change it!

1 Like

Hi @ukutaht

I switched from plausible.hash.js to plausible.js for one of my two discourse sites and this is what happens when I do that.

The site is opened in another tab and it shows no activity at all

Here is my other site with plausible.hash.js which correctly shows 1 online and generally, correct pages are also shown in the top pages widget.

I’m happy to try out any troubleshooting in case it helps eliminate need for the hashed js

3 Likes

Ah yes I recall seeing the same behavior now that you say this – which is why I went with the hashed script route.

3 Likes

Dumb question. Does adding this feature track what’s going on inside your discourse community or does it track outbound traffic to another domain the community is linked to?

What sort of metrics can you find out from this?

1 Like

I can (in my plausible dashboard) see which links of my discourse forum are being visited and from which country.

2 Likes

So, there are some bugs with using Plausible as Analytics?

No it should work just fine. I’ve been using it on my community for a while now and I haven’t noticed any issues.

1 Like

I’ve edited the OP to include simple directions for using Fathom. Please expand as necessary and further tested.

2 Likes

It’s working great :smiley:

2 Likes

Hi there and thanks for your guide.

I want to integrate fathom to discourse, do I need to add hash.js to the code, just like you did with plausible ?

I think it’s script.js as indicated in the OP (read till To add support for Fathom).

2 Likes

Update: The guys at Fathom put together a simple doc to walk you through what you need to do. Enjoy!

Fathom Note: There may be a way to change it, but Discourse adds the rel="canonical" tag for all pages back to the homepage. So all of our page views in Fathom showed as only coming from the homepage.

I talked to Fathom and they said, add this to your tracking script, canonical="false" so it looks as below.

<!-- Fathom - beautiful, simple website analytics -->
<script src="https://cdn.domain.com/script.js" data-spa="auto" data-site="ADHGFREZ" data-canonical="false" defer></script>
 <!-- / Fathom ->

That did the trick.

1 Like