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…

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.