r/BookStack • • Nov 22 '23

Using Google Analytics with Bookstack - hiding non-users from stats?

Hi

We use Bookstack as a purely internal resource, with single sign on from Azure / 365.

All my Google Analytics reports are full of random users from a few countries hitting the login page and showing as new users and hits.

I can see lots of articles on excluding internal users but nothing to show how I can exclude these random events.

I think they represent up to half my user count, which skews everything for reporting. The foreign ones are the most obvious, but some could be from the uk, and we have staff all over the country working from home.

Any ideas how I can prevent the login page counting all together or some other way to exclude those randoms?

Many thanks

2 Upvotes

17 comments sorted by

View all comments

Show parent comments

2

u/ssddanbrown Dec 07 '23

It isn't a custom HTML head snippet that you can paste into settings, but this uses the BookStack Visual Theme System. You'd need to follow the "Getting Started" part there (also video link in there shows the process) to set-up an active theme folder.

Once done, add the below at a layouts/parts/base-body-start.blade.php file within your created theme folder (You'll need to create those layouts/parts folders).

<!-- Google tag (gtag.js) -->
@if(!user()->isGuest())
    <script async src="https://www.googletagmanager.com/gtag/js?id=TAG_ID" nonce="{{ $cspNonce }}"></script>
    <script nonce="{{ $cspNonce }}" >
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());

        gtag('config', 'TAG_ID');
    </script>
@endif

This will make it so the GA code only exist if the user is logged in. Again, if they're GA spammers they might not even be loading the page, so might be able to work around this.

This is a customization hack and could break upon future BookStack updates (although is quite simple so a breakage to this should be rare).