Forum Discussion

akshays's avatar
12 months ago

Is there any way to get the user data in Page Head Content in JavaScript?

I want to get user data like roles, username, etc in the Page Head Content ( in the wrapper ) and use the data to integrate an analytics tool. The issue is that this user data is fetchable only using Freemarker and freemarker would not work in the header script. Do we know if therei is a way to fetch all this data in a script tag itself? 

I know one way would be to make an AJAX request to an endpoint and getting all the data from the endpoint's response. But, that would mean making an API call on home page load and might not be good for performance.

  • A lot of the user information necessary for analytics and tracking is actually exposed within the LITHIUM.CommunityJsonObject object so no need to resort to Freemarker. I would recommend to explore the content of this object as easily as just querying it in your Web Developer console.

    For Google Analytics you might do something like the following on your skin's head wrapper:

    analytics.ready(function() {
    		if(LITHIUM.CommunityJsonObject.User.registered){
    		if(!analytics.user().traits().email){analytics.identify({'email':LITHIUM.CommunityJsonObject.User.emailRef})};
    		}
    	});

    Also if you truly find that you indeed need to expose some additional Freemarker context object information to the JavaScript on client side you could maybe do something hacky like this untested code:

    <@liaAddScript>
        var userName = ${user.login};
    </@liaAddScript>