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.