Disable tracking while not logged in in a Single Page Application

Hello all,

I’ve been working on this issue for a few days now and I’m getting desperate.

I have a single page application, in which I would like to track after successful login (without a new reload of the page) via Matomo. For privacy reasons, the page should not be tracked before login.

How could something like this be implemented?

I have already tried various possibilities without success.

I thought of integrating the tracking code quasi deactivated and on it, after login to activate the tracking, unfortunately I find nothing helpful in the API.

My tracking code currently looks like this.

<script type="text/javascript">
            var _paq = _paq || [];
            _paq.push(['setDocumentTitle', 'Index']);
            _paq.push(['trackPageView']);
            _paq.push(['enableLinkTracking']);
            (function () {
                var u = "//stats.mydomain.com/";
                _paq.push(['setTrackerUrl', u + 'piwik.php']);
                _paq.push(['setSiteId', 1]);
                var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0];
                g.type = 'text/javascript';
                g.defer = true;
                g.async = true;
                g.src = u + 'piwik.js';
                s.parentNode.insertBefore(g, s);
            })();
        }
</script>

I am looking forward to ideas and suggestions!

Kind regards,
Shaack

My idea was to disableLinkTracking by default and while logging in enableLinkTracking again. Is that possible in any way?

Reloading of the page is not an option. Also I do not want to use a GDPR cookie bar.

I did similar task using PHP, but it was not single page application. I think you can use setSiteId property to enable and disable tracking code. For logged in users, it should be
setSiteId=23 otherwise setSiteId=0; That’s it.

Sounds pretty similar to what I want to do. But how can change siteId using javascript after the login?

I would prefer to have tracking disabled while not logged in instead of tracking against siteId 0.

Hi,

There are two easy ways to achieve this:

(I can’t guarantee that setting the siteID to 0 won’t still send (now invalid) data to Matomo)

Either you follow everything from https://developer.matomo.org/guides/tracking-consent

So you add _paq.push(['requireConsent']); to your tracking code. And once your user has logged in, you call _paq.push(['rememberConsentGiven']);.
BUT this has one important caveat that isn’t obvious: On a SPA, all tracking requests that are recorded while no consent is given are then sent at once, when consent is given. Which I think is not what you want.

So the other way avoids this:
The Matomo Tracking code is plain normal Javascript. So simply just call the Tracking code only after the user has logged in and don’t load Matomo at all else.
Just pay attention to replace var _paq = _paq || []; with var _paq = window._paq = window._paq || [];. This makes sure that _paq is always a global variable.
And while this makes not tracking people before they log in easier, it means that people will probably still be tracked if they log out and don’t reload the website.

1 Like

That is a non-secure method to deactivate the tracking, because its logged as

Tracking failures: Unknown (Id 0)

(Matomo 4.14.1)

Hi @Lukas

I’m implementing this second solution you gave for tracking user after they are logged in and, as you say, when they log out, they remain been tracked. Is there any solution already implemented to avoid this? Because I would love to end tracking when the user logs out, and restart the tracking once the user logs in again (either with the same account or with another), registering each log in as a different visit.

It’s important to mention that i’m using Matomo Tag Manager (MTM) to track the events. I tried to disconnect it when the user logs out by deleting the connection script that goes in the head section and, when the user reconnects, put it in again; but even if i delete the script, MTM continues to track the events, and when I write it again, it even duplicates the events i send via the DataLayer (because it writes again the custom HTML tags i have).

So, right now i’m using a variable to analyse if the user is log in and using it as a condition to fire the triggers in order to do not track when the user is not logged in. I also use this variable as user id to recognise every different user.

Any idea of how can i achieve this idea of disabling the MTM and reactivating it when the user logs in?

Thank you very much