Setting userid in wordpress

(Zunaid) #1

Hi there

I am hoping someone can help me please.

I have a wordpress site where I want to track logged in users.

I’m attempting to do this via the UserID in Piwik and I’ve added the following into the tracking script (after the line that says: var _paq = _paq || []; )

_paq.push([‘setUserId’, ‘<?php global $piwikuserid;
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$piwikuserid = $current_user->user_login;
} else {
$piwikuserid = “visitor555”;
echo $piwikuserid;

However, once I make the above change it doesn’t track anything at all.
Can anyone please assist ? I am sure that the php code itself is correct.

(Zunaid) #2

I’ve also tried the following which also doesn’t work.

<?php global $piwikuserid; if ( is_user_logged_in() ) { $current_user = wp_get_current_user(); $piwikuserid = $current_user->user_login; } else { $piwikuserid = "visitor555"; } echo sprintf("_paq.push(['setUserId', '%s']);", $piwikuserid); ?>


@zunaidk, you ever get this working? I’m trying to do the same thing right now.


Figured this out by following the docs. Added this to my footer script:

global $current_user;
<!-- Piwik -->
<script type="text/javascript">
  var _paq = _paq || [];
 // If used 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($current_user->ID)) {
     echo sprintf("_paq.push(['setUserId', '%s']);", $current_user->ID);
//Set Any other custom variables here