Getting Logged In User - Javascript for Piwik
-
It shouldn't be converted into anything, it is a javascript variable that holds the value of the currently logged in user.
You can see its value by logging it to the browser console. For example
(...) <script type="text/javascript"> var _paq = _paq || []; console.log('user id is ' + app.user.uid); _paq.push(['setUserId', app.user.uid]); _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { (...)
-
@baris Got it! Thank you. So much to learn about javascript. Now I am off to figure out why I am not seeing the user id being reported in Piwik. As an aside I had tried the suggestion you made a few days ago but without seeing a noticeable change in the Piwik reporting and not seeing the user id in the html page source I thought I had setup the line incorrectly.
-
Strange, strange, strange. When I look at the browser console log I do see the "user id is 1' entry. (Yes, my user id on my forum is #1 -- my account was first.) But yet Piwik still does not see the number 1. If I put app.user.uid in single quotes then Piwik sees myself as user "app.user.uid". So, some thing is happening in the _paq.push(['setUserId', app.user.uid]); line such that even though the console is showing user 1 it is still not being parsed by Piwik.
Sooo frustrating.
-
FIXED!
This is what works:
_paq.push(['setUserId', ''+app.user.uid+'']);
Two single quotes and the plus symbol!
Geez, it feels like I just gave birth!
Now to learn how to do an if then statement in javascript. I want to only include that line if the user is logged in, ie., the app.user.uid value is not zero.
-
@rod It's nothing special, it's just a habit that I have trained myself into.
The reason I check for the existence of
app.user
is because you cannot reference an object property (that is, theuid
property of theuser
object inapp
) if the object itself does not exist. You'll get a javascript error (ReferenceError: user is not defined
).If there's even a chance the parent object is not defined, I make a check for it first (though
app
is always defined in NodeBB, so I skip that).The proper way to write it would be:
if (app.hasOwnProperty('user'))
, but even sometimes that can be tripped up ifapp.user
is a property, butundefined