Tracking 404 pages in Plausible Analytics

After reading through the great guidance on this forum for options to enable site visitor analytics, I’ve dived headfirst into getting Plausible running and am very satisfied with my choice.

The specifics of the Plausible + Ghost combination, however, were not obvious and the installation has required some effort. I’ve found a way forward with all the essentials and am now just adding the cream.

I’m now enabling 404 error page tracking, as per 404 error pages tracking | Plausible docs

It requires a tag be added to the site’s 404 page. In Ghost this page is generated by the error-404.hbs file. I could add it to this file, but I’d like to avoid editing the Theme if possible (I’ve managed to make a bunch of significant customisations without resorting to modifying the Theme, so far).

Is there another way to add a small <script> tag to the 404 page? For example, by adding something to Code Injection that checks the page is the 404 page before taking action? Or conversely, setting Plausible to detect something that is already present in Ghost 404 pages?

I don’t think there’s another way to do this - this is a fairly specific custom piece of functionality and editing the theme file would generally be the correct/expected flow for something like this

Okay, thank you.

I did see that the generally advised way of determining whether the current page is a 404 with client side scripting is to do a XMLHttpRequest on window.location.href and check the status code that is returned. That way you can have one <script> embedded in all pages that can take action only if it’s a 404.

But it seems crude to request the page from the page for every page, effectively doubling the page hits. Other CMS frameworks like Drupal (via drupal_get_http_header("status")) and Laravel (via $exception) store the http result so you can access it without making another request. I guess there’s no public equivalent in Ghost?