Show User name RRS feed

  • Question

  • Hi,

    I have recently created an insights application, so fetch data from my on-prem SharePoint enviroment.
    So far so good, I am getting alot of data, but one annoying thing, is that even tough I have successfully configured my SharePoint applications to feed usage data to app insights, I still can't see the actual user name, only a useless userID looking something like this: njQBs

    Obviously this is not very usefull, but does anyone know how to get the actual username to show? 

    I tried alot of different stuff already, and I see many articles talking about including some kind of c# code to the global.asax file, but they don't really explain in detail how this is done.

    This is the article that comes the closest to explaining how to achieve this, but details are missing, for someone like me, that has no idea how to add this kind of stuff:

    I also tried this out, but that didint do anything either:

    Hope someone in here has some ideas as to how I can make this work.


    • Edited by MichaelIT02 Monday, November 19, 2018 12:32 PM
    Monday, November 19, 2018 9:59 AM

All replies

  • Hello Michael,

    SetAuthenticatedUserContext() is the right way to achieve this. You will, most likely, need to add it to the SharePoint master page into AI script right before tracking page view and set this method to drop a user cookie as well via the third parameter of that method (if your site is OK with dropping non-essential cookies and already notifies user about it):

    setAuthenticatedUserContext(authenticatedUserId: string, accountId?: string, storeInCookie = false)

    That should do the trick.

    Dmitry Matveev

    Tuesday, November 20, 2018 2:12 AM
  • Hi Dmitry,

    Thank you for the reply.

    So if I understand you correctly, the script should just look like this:

    <script type="text/javascript">
    var appInsights=window.appInsights||function(a){
      function b(a){c[a]=function(){var b=arguments;c.queue.push(function(){c[a].apply(c,b)})}}var c={config:a},d=document,e=window;setTimeout(function(){var b=d.createElement("script");b.src=a.url||"",d.getElementsByTagName("script")[0].parentNode.appendChild(b)});try{c.cookie=d.cookie}catch(a){}c.queue=[];for(var f=["Event","Exception","Metric","PageView","Trace","Dependency"];f.length;)b("track"+f.pop());if(b("setAuthenticatedUserContext"),b("clearAuthenticatedUserContext"),b("startTrackEvent"),b("stopTrackEvent"),b("startTrackPage"),b("stopTrackPage"),b("flush"),!a.disableExceptionTracking){f="onerror",b("_"+f);var g=e[f];e[f]=function(a,b,d,e,h){var i=g&&g(a,b,d,e,h);return!0!==i&&c["_"+f](a,b,d,e,h),i}}return c
    setAuthenticatedUserContext(authenticatedUserId: string, accountId?: string, storeInCookie = false)


    • Edited by MichaelIT02 Monday, November 26, 2018 7:48 AM Clean up script
    Tuesday, November 20, 2018 8:58 AM
  • Hello Michael,

    I would expect that you'd add your actual values to the setAuthenticatedUserContext() call, for instance:

    setAuthenticatedUserContext("User1", "Account1", true)

    Or, better, with variables:

    setAuthenticatedUserContext(currentUserId, currentAccount, true)
    Apart from that, the script you posted looks like the default one, so should work just fine.

    Dmitry Matveev

    Thursday, November 29, 2018 10:57 PM
  • I do this that works for username and screen resolution

    function AppInsightsUtils() { };
    AppInsightsUtils.initialize = function () {
    	var currentUser;
    	SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);
    	function sharePointReady() {
    		var context = new SP.ClientContext.get_current();
            var web = context.get_web();
            currentUser = web.get_currentUser();
    	    context.executeQueryAsync(onRequestSuccess, onRequestFail);
    	// This function runs if the executeQueryAsync call succeeds. 
    	function onRequestSuccess() { 
    	var appInsights=window.appInsights||function(config){
    	function s(config){t[config]=function(){var i=arguments;t.queue.push(function(){t[config].apply(t,i)})}}var t={config:config},r=document,f=window,e="script",o=r.createElement(e),i,u;for(o.src=config.url||"//",r.getElementsByTagName(e)[0].parentNode.appendChild(o),t.cookie=r.cookie,t.queue=[],i=["Event","Exception","Metric","PageView","Trace"];i.length;)s("track"+i.pop());return config.disableExceptionTracking||(i="onerror",s("_"+i),u=f[i],f[i]=function(config,r,f,e,o){var s=u&&u(config,r,f,e,o);return s!==!0&&t["_"+i](config,r,f,e,o),s}),t
    		appInsights.queue.push(function () {
    				appInsights.context.addTelemetryInitializer(function (envelope) {
    					envelope.tags = envelope.tags || {};
    					if (typeof screen !== "undefined") {
    						envelope.tags["device.screenResolution"] = screen.width + "X" + screen.height;
    						envelope.tags["device.locale"] = navigator.language || navigator.browserLanguage || "unknown";
    		var account = currentUser.get_loginName();
            var currentUserAccount = account.substring(account.indexOf("|") + 1);
    		appInsights.trackPageView(document.title,window.location.href, {User: currentUserAccount,Screen:screen.width + "x" + screen.height});
    	// This function runs if the executeQueryAsync call fails. 
    	function onRequestFail(sender, args) { 

    hope that helps


    Friday, September 13, 2019 10:34 AM