Question about custom variable in Visitor Log

Hi everyone,

I’ve installed Piwik and was trying out the custom variable features to track individual users of my website when I noticed something in the Visitor Log.

For example…when I logged in as “User1” , Visitor Log is able to capture this user’s activities and set the custom variable as Visitor:User1, however when I switch to another username e.g “User2” using the same browser, somehow only the custom variable is replaced (in this case, User1 is replaced by User2). That is, there is no longer Visitor:User1, Visitor Log only shows Visitor:User2 and all the previous activities of User1 is now belong to User2.

So it looks like Visitor log will overwrite the custom variable whenever the IP(or when you are using the same browser? I’m not sure.) is the same, am I doing anything wrong? Or is this how it supposed to work?

Below is the code that I currently use to set my custom variable.


<script type="text/javascript"> 
  var _paq = _paq || [];
  _paq.push(['setCustomVariable',  
    1, 
    "Visitor", 
    "<?php
    if(isset($_SESSION['user'])){ // this is where i track my users
        echo  $_SESSION['user'];
    }
       ?>", 
    "visit" 
    ]);
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u=(("https:" == document.location.protocol) ? "https" : "http") + "XYXYXYXYXXY";
    _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>

Could anyone clarify this? Thanks in advance for your help!

By design, custom variables of “scope = ‘visit’” will be stored once per visit. See: Custom Variables Analytics - Analytics Platform - Matomo
If you want to store multiple values for a same variable, use scope = ‘page’

Yes, as Matt said, this is the design. I needed to track users with multiple accounts, so this is what I used: one variable with visit scope, and one with page scope I only set at sign up/log in. Then, from the Live API I request:


&segment=customVariableName1=@User;customVariableValue1=@-'.$customer_id.'-'

(customer_id is enclosed in - - because search is based on LIKE), and then add


,customVariablePageName3==ID;customVariablePageValue3==$customer_id

(note the , in the beginning - means “OR”).

So, you get everything about this user if the visit scope variable matches, or a page scope variable matches. You end up with the full session, including the parts where he was logged in with another account.

Thanks a lot for the explanation guys!
Yeah it does make sense…