I hope you’re doing well. I’m currently working on implementing Matomo Tag Manager within my company’s SPA website, but I’ve encountered an issue.
While examining the Visits Log report, I noticed that when a user leaves the page, the visit doesn’t end until 30 minutes have passed (the default value). In other words, if the user returns to the page or logs in again before these 30 minutes have elapsed, this new session is counted as a continuation of the previous one. Consequently, when the user logs in again, it is considered the same visit as before, causing the reports to display a transition from the last page visited in the “first visit” to the home page (the first page visited in the second visit). To enhance accuracy, I would like to conclude the visit when the user logs out or quits the page, treating each subsequent visit as a completely new one, even if they return within the default 30 minutes.
I came across this article [https://matomo.org/faq/how-to/faq_187/] which explains how to create a new visit using JavaScript. I’m curious if it’s possible to implement something like this each time we set the User Id in order to start tracking after every login as a new visit.
Any insights or guidance would be greatly appreciated. Thank you in advance for your help!
Thank you for your prompt response. I understand and agree with your suggestion, especially if the main objective were to precisely track the average time spent on each page. However, our primary goal is slightly different. We aim to register each time the user enters the application as a new visit, even if it occurs within the default 30-minute visit length.
Furthermore, our intention is not only to consider a new visit when the user logs in but also when they perform actions like closing the application tab and returning after a short period (e.g., 1 minute). In such cases, the user should start from the home page since they would still be logged in.
Any suggestions you could offer would be greatly appreciated.
To ensure that each new visit is registered in Matomo, even if it occurs within the default 30-minute visit length, we attempted the following implementation:
window._paq.push(['appendToTrackingUrl', 'new_visit=1']); // forces a new visit
window._paq.push(['setUserId', user.id]);
window._paq.push(['trackPageView', 'session_started']);
window._paq.push(['appendToTrackingUrl', '']); // do not force a new visit anymore
We added this script as a callback to execute as soon as the Matomo JS Tracker is loaded, using window.matomoAsyncInit. However, besides this pageview that we push, all the other events are tracked with the Matomo Tag Manager (MTM), which also loads after the user logs in but only in the first login (because if the user logs out and then logs in again, MTM will be already loaded). The intended sequence is as follows:
The user logs in.
A pageview is pushed with the new visit flag set.
MTM starts tracking the events.
The problem arises because the pageview is not registered until we record an event with MTM. Consequently, this event is tracked as part of the past visit, and the pageview is registered as a new visit. As a result, we are unable to register the new visit pageview as the session’s first event, and we cannot include all the MTM tracked events in this new visit session. Is there a way to force to track first the events pushed with the Matomo JS Tracker ? Alternatively, are there any workarounds that we could implement to solve this issue?
We would greatly appreciate any insights, suggestions, or solutions you may have to overcome this challenge. Thank you in advance for your assistance!