Show Wordpress User Name

Can someone please let me know if it is possible to use custom variables to show the WP username on the Visitor Log and exactly the custom variable to include in the tracking code? Much appreciated, as I cannot find this info anywhere!

Maybe ask in : Custom Variable Case Study: Divezone.net - Analytics Platform - Matomo as itā€™s related to wordpress!

Here is a nice example I came up with.

<?php  if (is_user_logged_in()){
global $current_user; get_currentuserinfo();
echo('Welcome, ' . $current_user->user_firstname . ' | ');
}
else {
echo "Welcome, Visitor | ";
};
?>

The user_firstname can be changed to a number of things. See:

Its work, Thank you Zeeshan.

I got database error when i tried these.

ŲŖŲµŁ…ŁŠŁ… Ł…ŁˆŲ§Ł‚Ų¹

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.