Show Wordpress User Name

Are you still having issues? What is the database error?

I am still having issues. I simply want to show the username instead of the IP address - in visitor logs. Is that possible? And if so - please tell me exactly how. Thank you.

Well you would anonymize the IP. If you set a custom variable with a visit scope the username would be available in a clear way. Is custom variable not working for you?

Not saying it isn’t working for me because I just am not clear on how to implement the custom variable to simply show my clients the names of the WP users. This client site is “closed” to members only - and they want to see what members are using the site - the IP address of the user is useless.

Sorry. I was unclear, you mention that you get a database error, that is why I was unclear.

If you are using Custom Variable you just need to use the normal set up something like below:


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

To get $Username, I assume you are using the WordPress database. You would get that from this function


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

see WordPress Codex

Hope that helps

Great and thanks so much - but where exactly do I put this code please? It appears the first piwikTracker goes in the code on the site - but where does the second piece of php code go?

p.s. it wasn’t me that had a database error…

Sorry changed my code a bit, so it does not fire if no username is present. The tracking code should go in the Piwik Tracker
inbetween here:


var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
<!--PLACE CODE HERE-->
piwikTracker.trackPageView();

The PHP code which calls user, can be anywhere on the page as long as it’s above your tracker. You can place in the header.php of you WordPress install, just at end of the file, or in the footer above the Piwik tracking code…

Good luck!

Okay - have put this code in and nothing is different - no username is on the Piwik visitor log - just the same IP address and other info.

I put the 1st code in the piwikTracker as indicated.

I put the 2nd code above that in the footer as indicated.

What am I doing wrong?

Can you share the website with me? or PM me the website?

It is a rather complex multi-site network. Would I have to put the code into every site on the network for this to work, as I have just put it on the main member entry site just now?

Here is what would happen. A custom variable will be set for the session on your site. If they move to another site, this info will not follow them.

From Docs

Custom variables of scope “visit” are stored in a first party cookie that is valid only for the current visit. When the same visitor returns to your site, the custom variables will be empty. If you want to persist a custom variable to a visitor, for example “UserType = Customer”, you must call the JavaScript function setCustomVariable( index, name, value, scope=”visit” ) at least once during each visit.

Read more on the docs for custom variables.

I sent you access via PM -

So, maybe what I want is not possible?

I just got your PM. I will try my best to look at it now, but it may be later today.

That would be fine. BTW - the piwik code is found in: Custom Choices >> Theme Options >> last field “google code”

Sent you some info via PM…Let’s take it via PM for now.

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