SetUserId will also create a new Visit

Hi,

i’m trying to implement a pretty simple tracking, but as soon as i set an userId a new Visit is created. This is the code i’m using:

example code
<script type="text/javascript">
  var _paq = window._paq || [];

  _paq.push(['enableLinkTracking']);

  (function() {
    _paq.push(['setTrackerUrl', 'https://example.com/matomo.php']);
    _paq.push(['setSiteId', 1]);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
  })();
</script>

<div id="click-me" onclick="track('meow')">set user id</div>
<div id="click-me" onclick="track()">track without</div>

<script type="text/javascript">
    function track(id) {
      var _paq = window._paq || [];
      if (!!id && id.length > 0) {
        _paq.push(['setUserId', id]);
      }
      _paq.push(['trackPageView']);
    }
</script>

If i understand the documentation correct (https://matomo.org/docs/user-id/#how-requests-with-a-user-id-are-tracked) it should reuse the current visit and match it with he new userId

When a visitor connects to your website and is not logged-in (User ID is not set) then a visit is created. If the visitor then logs-in your website and has a User ID set then their existing visit will be re-used

I’ve found a couple of topics regarding this issue, but no answers/solutions.

Has anybody any Idea what i’m doing wrong?

Thanks in advance,
Voltacer

1 Like

Okay, so apparently this behaviour is intended, even though the documentation states otherwise.
In Version 3.12 this should be fixed: https://github.com/matomo-org/matomo/pull/14360.

1 Like