Show Wordpress User Name


#1

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!


(Matthieu Aubry) #2

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


#3

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:


#4

Its work, Thank you Zeeshan.


(Ertekaz) #5

I got database error when i tried these.

تصميم مواقع


(Adrian) #6

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


#7

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.


(Adrian) #8

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?


#9

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.


(Adrian) #10

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


#11

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…


(Adrian) #12

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!


#13

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?


(Adrian) #14

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


#15

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?


(Adrian) #16

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.


#17

I sent you access via PM -

So, maybe what I want is not possible?


(Adrian) #18

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


#19

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


(Adrian) #20

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