Tea leaves with water droplets on them.

Plausible & Gitea

Tracking activity on Gitea using Plausible

2021-01-19
2 minutes

Yesterday, I moved my Git server from GitLab to Gitea. There’s nothing wrong with GitLab, I actually quite like it, but it’s a rather large tool for my needs. Gitea is much more lightweight, faster, and provides all its features for free. Now, it’d be nice to add some analytics to Plausible, for no reason other than personal interest.

Plausible, my analytics tool of choice, simply requires a single script tag be added somewhere on the page. With GitLab, modifying templates or adding custom ones was a bit of a pain. GitLab has a tonne of theming and configuration options, but none seem to quite go as far as “here’s some custom HTML”. Gitea on the other hand has some great options for adding custom HTML to certain parts of the page, and even supports just being given an entirely custom theme.

#Custom footer content

Plausble recommends putting the tracking script in the <head /> of the page. Personally I prefer to keep scripts like this at the end of the page, so they don’t impact the actual site load. If you want to follow Plausible rather than me, just change footer.tmpl below with header.tmpl, and everything should work fine.

Gitea’s documentation on this used to be a little hard to understand, so I fixed it just before writing this post. Gitea wants the custom templates to live in a templates directory, which will sit next to the conf directory with your app.ini. Here we need to create the file custom/footer.tmpl, which will contain the extra HTML Gitea will put immediately before the closing</body>.

Inside footer.tmpl, add the script tag for Plausible. You can find this in the Plausible admin panel.

HTML
<script async defer data-domain="yourdomain.com" src="https://plausible.io/js/plausible.js"></script>

Once added, simply restart Gitea, and it’ll start being tracked by Plausible!

#Just tracking unauthenticated users

Now analytics is working, that’s great! But personally I don’t really want it tracking me as I use it, just other people. Eagle-eyed readers will notice the extension on footer.tmpl implies it’s a template - because it is. footer.tmpl is a fully-fledged Go template, meaning you can use Go’s template system as needed.

One of the items in the footer context is SignedUserName. If the user is logged in, this is their name. If they’re not logged in, it’s empty. Therefore, this can be used to check whether the user is logged in, and only show the script if they aren’t:

HTML+Handlebars
{{ if not .SignedUserName}}
  <script async defer data-domain="yourdomain.com" src="https://plausible.io/js/plausible.js"></script>
{{ end }}

Now, when anonymous users visit the page, the Plausible snippet will be injected, and their browsing tracked. When you’re browsing around, there’ll be no snippet, so no tracking!

For more on how to customize your Gitea instance, check out their docs. For my Gitea instance, check out git.theorangeone.net.

Share this page

Similar content

View all →

Self hosting Plausible

2020-08-05
4 minutes

I only recently talked about using GoAccess as an analytics tool. Over the last couple months, I’ve been more interested in how many hits this website actually gets. GoAccess was a really simple solution which worked on log files and only captured the bits of information I really cared about:…

None

Updating GitLab project dates

2021-07-08
3 minutes

As a developer I do basically everything in git and for fun I run my own git server on my home server. I’ve swapped around quite a lot between GitLab and Gitea, but finally settled on GitLab. It’s a bit heavy, but the deep CI integration is really nice. Since…

Rhinoceros sculpture in a park

Lightweight GitLab

2021-11-11
6 minutes

It’s no secret that GitLab is a beast of an application. As self-hosted git servers go, it’s easily the most powerful and feature complete. But that weight comes at a cost: resource usage. GitLab is no slouch, easily consuming upwards of 6GB of RAM by default without doing anything, and…