Enable Plausible Analytics on an Obsidian Publish site

I use Plausible Analytics for web analytics. Here’s a method that seems to work for enabling Plausible on an Obsidian Publish site with a custom domain.

Plausible asks you to insert the following snippet in the <head> tag of your website:

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

There doesn’t seem to be a way to do this directly with Obsidian Publish. However, you can run custom JavaScript on your Obsidian Publish site using the publish.js file. With a tiny bit of custom JavaScript, we can construct the Plausible snippet and insert it into the <head> tag as required.

Here’s the JavaScript I added to my publish.js file:

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

If you’d like to use Plausible Analytics, try adding this JavaScript to your own publish.js, replacing yourdomain.com with your site’s domain name, and (re-)publishing the publish.js file. Happy analyzing!

(I know @Bora was asking about this in another thread)

2 Likes

Thank you! Worked like a charm. :heart_eyes: