Tracking User ID - How to do it?

So reading in the guides I see that in order to track the ID you need this:

_paq.push(['setUserId', 'USER_ID_HERE']);

Then we have the full example:

<script type="text/javascript">
  var _paq = window._paq = window._paq || [];
  _paq.push(['setUserId', 'USER_ID_HERE']);
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="//{$MATOMO_URL}/";
    _paq.push(['setTrackerUrl', u+'matomo.php']);
    _paq.push(['setSiteId', {$IDSITE}]);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.type='text/javascript'; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
  })();
</script>

Now in the example above it shows that I need to call the user ID before I call pageview, here’s my question:

I’m going to use user’s email as the ID, and I’m going to get that email when a significant event happens such as lead, book a call or purchase.

Now lead, book a call or purchase doesn’t happen on page load, people need to take actions for that to happen. Exception here can be purchase but I’m not using page load as a way to trigger purchase events instead I hook into events and wait for the user to buy and spend money and then I trigger the purchase event.

My question is, if I call this code:

<script type="text/javascript">
  var _paq = window._paq = window._paq || [];
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="//{$MATOMO_URL}/";
    _paq.push(['setTrackerUrl', u+'matomo.php']);
    _paq.push(['setSiteId', {$IDSITE}]);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.type='text/javascript'; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
  })();
</script>

which doesnt’ have: _paq.push([ā€˜setUserId’, ā€˜USER_ID_HERE’]); before the page view, and I wait for the lead, book a call or purchase event, can I then simply just call this:

_paq.push([ā€˜setUserId’, ā€˜USER_ID_HERE’]);

will that work?

or is calling the push for user ID tied to the whole code below:

<script type="text/javascript">
  var _paq = window._paq = window._paq || [];
  _paq.push(['setUserId', 'USER_ID_HERE']);
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="//{$MATOMO_URL}/";
    _paq.push(['setTrackerUrl', u+'matomo.php']);
    _paq.push(['setSiteId', {$IDSITE}]);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.type='text/javascript'; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
  })();
</script>

Meaning the code for pushing the user ID will not work without the whole code above…

Can anyone shine some light for this situation please…

1 Like

You would just need to track something, like an event for example.

_paq.push([ā€˜setUserId’, ā€˜user_123’]);
_paq.push([ā€˜trackEvent’, ā€˜Login’, ā€˜Success’]);

See bullet point 3 here: JavaScript Tracking Client: Integrate - Matomo Analytics (formerly Piwik Analytics) - Developer Docs - v5

@Jason_1282 Thank you for the reply.

So in my scenario for the purchase, I can call setUserID and call for example login event, AFTER the purchase happens, is that what you’re saying?

So here’s a scenario:

User comes on my pages, don’t have a means to identify him so the user ID is not sent. He visits 3-4 pages then buys a product.

Now from the purchase data I can get his email and now I can send the user id. But matomo initialization code already got executed when he visited the page, so now I can’t call setUserId inside the initialization code, now I have to do it ā€œaloneā€ so to say.

And I can do it like this for example:

_paq.push([ā€˜setUserId’, ā€˜user_123’]);
_paq.push([ā€˜trackEvent’, ā€˜Login’, ā€˜Success’]);

Another thing on the documentation that you linked above, it says this:

" * You must set the user ID for each pageview, otherwise the pageview will be tracked without the user ID set."

But what to do in cases where the website doesn’t even have a login for people, e.g. it’s just website selling stuff and you don’t have to login.

If someone visits 10 pages, then buys something, at that point I get his email and send it as an user id, will matomo be able to stitch together that that user who just bought and who’s ID has been shared, he also visited these 10 pages prior to him making a purchase and then matomo will tie those 10 pages to that user, is this possible?

The user ID will be applied to the visit the issue being you don’t know when the visit will end, they might quit prior to the Visitor ID be tracked. Hopefully you will have all your consent worked out prior to this activity also.

@Jason_1282 that I am aware, but my question is, if someone visits let’s say 5 pages, he’s annonimous, no user ID, on the 6th page he buys, I get his email which I will use as the user ID, I send the user ID, will matomo be able to stitch that it’s the same user who visited 5 pages prior visiting the 6th page and made a purchase?

Based on cookies of course…

And even in the scenario you mentioned, if someone visits for example 3 pages, be bounces, comes back after 7 days, visits 2 more pages then makes a purchase, I get his email send the user ID, will matomo be able to stitch together that he was the same person who 7 days prior visited 3 pages, and now he visited 2 pages and made the purchase based on cookies?

Of course, this is assuming that the cookies are there, they didn’t get deleted by any means from his storage.

Because I do see when I visit my own website in different occasions e.g. I visit today, then visit after 2 days the analytics says returning user, meaning it knows I visited 2 days ago based on cookies.

So in the case of user ID scenarios I described above, am I correct in my assessment?

1 Like

This is explained here: How are requests with a User ID tracked? (login, logout, or on multiple devices) FAQ - General - Matomo Analytics Platform

you can call paq.push([ā€˜setUserId’, ā€˜USER_ID_HERE’]);later, when the user triggers an event like lead or purchase. You don’t need to repeat the full tracking script just this line inside a new tag is enough. It will apply to future events, but not retroactively to the initial pageview.

@lanternfrosty

Are you sure?

Cause I think I read somewhere that just calling push for the user ID wont work without also calling the push for pageview:

_paq.push([ā€˜trackPageView’]);

Do you have a documentation source for what you said?

When you load the Matomo tracking code without calling _paq.push(['setUserId', 'USER_ID_HERE']); before trackPageView, the tracking will work normally but without associating that visitor with a user ID. You can definitely call _paq.push(['setUserId', 'USER_ID_HERE']); later—such as when a user submits a lead form, books a call, or completes a purchase—and Matomo will then link subsequent events or page views to that user ID.

The important part is that the Matomo tracking script (matomo.js) must be loaded on the page first, which happens with the full script block you included. After that, you can push commands like setUserId dynamically anytime on that page session.

So, the initial tracking code without setUserId is fine for anonymous visitors, and when the user ID becomes available (like after a purchase), just push the user ID command. It does not require reloading the entire script or repeating the full initialization. Just ensure the Matomo script is loaded once, then push the user ID and other tracking calls as needed.

For businesses relying on accurate user tracking and analytics such as transport services in Perth using tools like Matomo helps optimize user engagement. For example, companies offering airline crew transport Perth like Majestic Chauffeur can benefit from event-based tracking to better understand client behavior and improve service.

@mannyorton oh okay, thank you.

Didn’t realize that I can call the push user id AFTER the page view and it will associate everything that happened to that user id, that’s good to know.

Thank you again.