How to use Plausible analytics in a Chrome Extension on Manifest v3
If you want to use Plausible.io analytics for a Chrome Extension on the new v3 manifest – which might be a better choice than Google Analytics if you care about GDPR compliance without needing a cookie consent banner - then you'll quickly hit some snags, so here's how to work around them.
Manifest v3 Content Security Policy
Chrome extensions no longer allow loading external scripts. This means you can't use
<script defer data-domain="yourdomain.com" src="https://plausible.io/js/plausible.js"></script>
Instead, you need to visit https://plausible.io/js/plausible.js directly, then copy that script into a file in your extension itself.
Modifications to the Plausible snippet
You need to modify the default snippet above to look like:
<script src="plausible.js" data-api="https://plausible.io/api/event" data-domain="your-extension.com"></script>
Note it has the following modifications:
src="plausible.js"
is the name of the script you copied locally.data-api="https://plausible.io/api/event"
avoids a 404 error for the api/event filedata-domain="your-extension.com"
while Chrome extensions don't have domains, you should match this to whatever domain the plausible account is set up to track. You can make up a phrase if you're only tracking a Chrome extension.
I would just add that the npm package is an alternative to this. I also am led to believe that
trackEvent
doesn't work in the extension context (adding the goal in Plausible won't show it on the dashboard), but loading the script manually like this would perhaps allow you to implement a workaround that can't be done when loading via npm package. Here's what I learned from a Plausible team member when I asked: