Getting Logged In User - Javascript for Piwik
-
I have recently deployed a Piwik installation for my Nodebb forum site. I wish to analyze my traffic by the logged in Nodebb users. According to the Piwik documentation I need to pass the userid information in my tracking javascript code like this:
_paq.push(['setUserId', 'USER_ID_HERE']);
I have been searching around and found that I may have to use the app.user object from NodeBB. But how? Would someone help me construct the javascript code to put the NodeBB user (or userid) into a variable that I could then use in the _paq.push... that I listed above?
Edit: Let me add that I am passing the Piwik javascript tracker code via the "Custom HTML & CSS > Custom Header" area. The generic Piwik Javascrip does work I just wish to add the additional component of the user if one is logged in.
Thank you.
-
@baris Thank you for the advice. I gave your suggestion a try and it does not seem to work. The app.user.uid is not being converted into the actual userid. When I look at the page source, after visiting the page, I see this:
(...) <script type="text/javascript"> var _paq = _paq || []; _paq.push(['setUserId', app.user.uid]); _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { (...)
As you can see app.user.uid was not replaced with my actual user id. Do you have another possible suggestion?
Thank you.
-
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