Google analytics tracking alternative for publish

Use case or problem

I’d like to have some more insight on how much traffic my digital garden is receiving. The only option currently is to add Google Analytics tracking code. I have been trying to degoogle my life as much as possible, and I do it one step at a time.

Publish is an opinionated piece of software. I’m reasonably happy with it, as it works well enough for my needs. But I really don’t like being forced to use a Google service to get such a basic feature working.

Proposed solution

Allow for alternative analytics servers, especially Matomo, as it seems to be the most popular.

Current workaround (optional)

Opting for no analytics.

Related feature requests (optional)

I couldn’t find any related feature requests.

Just to close the loop on this — Publish does support non-Google analytics today, it’s just not
in the settings UI, so it’s easy to miss.

There’s a section in the docs under “Plausible, Fathom, and other analytics services” that says you
can add most analytics services through publish.js. The pattern is just appending a script tag to
document.head on load:

var analyticsScript = document.createElement('script');
analyticsScript.defer = true;
analyticsScript.setAttribute('data-domain', 'yourdomain.com');
analyticsScript.src = 'https://plausible.io/js/plausible.js';
document.head.appendChild(analyticsScript);

Swap the src and the data-* attribute for your provider and it works the same way.

On Matomo specifically: the docs only name Plausible and Fathom as examples, and Matomo’s
snippet works differently — it doesn’t use a data-* attribute at all, it uses a _paq queue that
you configure through push() calls. So you can’t just swap the src. This is the shape Matomo
actually gives you:

var _paq = window._paq = window._paq || [];
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
  var u = "//matomo.yourdomain.com/";
  _paq.push(['setTrackerUrl', u + 'matomo.php']);
  _paq.push(['setSiteId', '1']);
  var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0];
  g.type = 'text/javascript'; g.async = true; g.src = u + 'matomo.js';
  s.parentNode.insertBefore(g, s);
})();

Dropped into publish.js as-is, that should work — the IIFE runs on load and inserts the tag
itself, so you don’t need to build the element manually the way the Plausible example does.

I haven’t run this against a live Matomo instance inside Publish, so treat it as “should work”
rather than “confirmed” — but it’s a plain script tag with no build step, so I’d be surprised if
it didn’t.

Two gotchas I’d flag:

  1. Custom domain is a hard prerequisite. The docs list it under Prerequisites for analytics
    generally — so if your garden is still on publish.obsidian.md/..., you’ll need the custom
    domain first, regardless of which provider you pick.
  2. The Fathom example in the docs has a typo. It reads
    var fathom = analyticsScript.createElement('script') — createElement lives on document,
    not on the script element, so copying that line verbatim throws a TypeError. Use
    document.createElement like the Plausible example does. Worth knowing before you spend twenty
    minutes debugging a blank dashboard.

One caveat on the whole approach: publish.js runs client-side, so anything a content blocker
stops will simply not appear in your numbers. That’s true of GA too, but Plausible/Fathom/Matomo
are blocked a bit more aggressively in my experience, so expect your absolute numbers to look
lower than GA’s rather than higher.

So I’d frame this less as “add Matomo support” and more as “document the publish.js route better”
— the capability is there, it’s the discoverability that isn’t.