Show Wordpress User Name

I also want to track my Wordpress logins, so that I can see when a user uses it.

But the Piwik Tracking code seem to have changed since then. Mine looks like:


<script type="text/javascript">
  var _paq = _paq || [];
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u=(("https:" == document.location.protocol) ? "https" : "http") + "://mydomain.com/";
    _paq.push(['setTrackerUrl', u+'piwik.php']);
    _paq.push(['setSiteId', 1]);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript';
    g.defer=true; g.async=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
  })();
</script>

So where to put this custom variable now?


<?php if (!empty($Username)) {echo "piwikTracker.setCustomVariable(1, 'Visitor', '".$Username."', 'visit');";} else {}?>

And the function for Wordpress? Just in the footer above the Piwik tracking code. Right?


<?php 
global $current_user;
get_currentuserinfo();
$user_info = get_userdata(1);
$Username =  $current_user->user_login;
?>

Any help appreciated! :slight_smile:

to set the user id I would recommend to use the User ID feature: User ID - Analytics Platform - Matomo

But I want to track the Wordpress username, for example how often a certain login is used.

If I understood the User-ID feature and the Piwik documentation on custom variables (JavaScript Tracking Client: API Reference - Matomo Analytics (formerly Piwik Analytics) - Developer Docs - v3) right, this must be the correct code snippet for Wordpress user/login tracking:


<!-- Function to get the username from WordPress for Piwik User Tracking -->
<?php
global $current_user;
get_currentuserinfo();
$user_info = get_userdata(1);
$Username =  $current_user->user_login;
?>

<!-- Piwik Code -->
<script type="text/javascript">
  var _paq = _paq || [];
<!-- Piwik Custom Variable for User Tracking -->
<?php if (!empty($Username)) {echo "piwikTracker.setCustomVariable(1, 'Visitor', '".$Username."', 'visit');";} else {}?>
<!-- End Piwik Custom Variable for User Tracking -->
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u=(("https:" == document.location.protocol) ? "https" : "http") + "://mydomain.com/";
    _paq.push(['setTrackerUrl', u+'piwik.php']);
    _paq.push(['setSiteId', 1]);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript';
    g.defer=true; g.async=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
  })();
</script>
<noscript>
<!-- Piwik Image Tracker Fallback -->
<img src="http://mydomain.com/piwik.php?idsite=1&rec=1" style="border:0" alt="" />
<!-- End Piwik Image Tracker Fallback -->
</noscript>
<!-- End Piwik Code -->

But I can’t see any statistics for Wordpress users on my Piwik site. Who can help?

The custom variable for user tracking has to be adjusted to the new piwik code layout:


_paq.push(['setCustomVariable',1,"Username", "<?php if (empty($Username)) {echo 'Guest';} else {echo $Username;}?>","visit"]);

Then it works. I hope this is also helpful to others. :slight_smile:

1 Like