Juri Strumpflohner

RSS
all notes🌱seedling·Updated

Embed Plausible Analytics with Partytown on Astro

Author profile pic
Juri Strumpflohner

I’m not doing any marketing on my own blog, but I’d like to see and understand what content people like, how they move on my site etc. I’ve been using Google Analytics in the past, but it became an unmanageable monster (especially for what I need it) and it is not GDPR compliant (hence requires annoying cookie consent popups). A cool alternative is Plausible.

What is Plausible?

Plausible is a “easy to use and privacy-friendly Google Analytics alternative”. Why does that matter for me?

  • Google Analytics has become a monstrously complex beast (unless you’re a SEO professional)
  • I hate GDPR banners and most of them don’t even work appropriately

Embed into Astro

// BaseLayout.astro
---
...
---

<html lang="en">
  <head>
	...
    <script
      type="text/partytown"
      async
      data-domain="juri.dev"
      src="https://plausible.io/js/script.js"></script>
  </head>
  <body...>
	...
    <main>
      <slot />
    </main>
    <Footer />
  </body>
</html>

Note I load the script with Partytown. The reason is that analytic scripts

  • can be quite heavy
  • they are optional, I don’t want users to have to wait for the primary content just for the script to load

Partytown allows you to offload the loading of the script from the main thread onto a service worker.