Announcement

#1 2021-01-27 19:45:06

dadoc
Member
2020-06-24
12

[solved] track userID in Matomo (aka Piwik)? $ user, $username empty?

Hi,
I've just spent a couple of hours trying to get Matomo's userId tracking feature running, without success so far (the regular Matomo tracking works fine with Piwigo).
In order to pass the Piwigo user name over to Matomo, they say:

User ID is a feature in Matomo that lets you connect together a given user's data collected from multiple devices and multiple browsers. There are two steps to implementing User ID:

You must assign a unique and persistent non-empty string that represents each logged-in user. Typically, this ID will be an email address or a username provided by your authentication system.
You must set the user ID for each pageview, otherwise the pageview will be tracked without the user ID set.
You must then pass this User ID string to Matomo via the setUserId method call just before calling any of the track* functions (trackPageview, trackEvent, trackGoal, trackSiteSearch, etc.) for example:
_paq.push(['setUserId', 'USER_ID_HERE']);
_paq.push(['trackPageView']);
Note: USER_ID_HERE must be a unique and persistent non-empty string that represents a user across devices.

The tracking JS example given is:

Code:

var _paq = window._paq = window._paq || [];

<?php
// If user is logged-in then call 'setUserId'
// $userId variable must be set by the server when the user has successfully authenticated to your app.
if (isset($userId)) {
     echo sprintf("_paq.push(['setUserId', '%s']);", $userId);
}
?>
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);

Obviously, the php part won't work if you put it into the header (where the rest of Matomos JS tracking code goes). I tried putting it into header.tpl this way:

Code:

<!-- Matomo -->
<script type="text/javascript">
var _paq = window._paq = window._paq || [];

{if isset($user) && isset($user.username)}
   _paq.push(['setUserId', '{$user.username}']
{/if}

/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
  var u="//mysite.com/analytics/matomo/";
  _paq.push(['setTrackerUrl', u+'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.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<!-- End Matomo Code -->

But no success -

Code:

{if isset($user) && isset($user.username)}
   _paq.push(['setUserId', '{$user.username}']
{/if}

is filtered out (= disappears in the source code oft the pages).
It seems that $user, $username etc. aren't available globally?

Any idea what I could do to get the plain username into $userId in the header? Please take into account that I'm not more than a coding hobbyist...
Thanks & best regards
Martin

Last edited by dadoc (2021-01-28 18:04:28)

Offline

 

#2 2021-01-28 12:41:07

dadoc
Member
2020-06-24
12

Re: [solved] track userID in Matomo (aka Piwik)? $ user, $username empty?

One night later I found out that the user name is in $USERNAME.
Substitution of $USERNAME für the real user name now works (looking at the source code of the page), tracking however doesn't...

Offline

 

#3 2021-01-28 18:06:32

dadoc
Member
2020-06-24
12

Re: [solved] track userID in Matomo (aka Piwik)? $ user, $username empty?

Tracking of user names is working now. I just had to be pacient, as UserID data, other than other tracking data, is not processed in real time, but only every 60-90 min.
Many thanks to myself ;)
Martin

Offline

 

Board footer

Powered by FluxBB

github twitter newsletter Donate Piwigo.org © 2002-2024 · Contact