none
AppInsights and SharePoint document usage RRS feed

  • Question

  • hi,

    I am using appinsights on my on-premise sharepoint farm to track page views etc - works great 

    however I would like to track document usage aswell - I thought it would just be the case of drilling down into pageviews and requests to capture when a word or excel document is requested - however I do not see any of that information being captured.

    I set up my custom property collection on my master pages with script like this

    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.load(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||"//az416426.vo.msecnd.net/scripts/a/ai.0.js",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
    		}({
    			instrumentationKey:INSTRUMENTATION_KEY
    		});
    		window.appInsights=appInsights;
    		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) { 
    	} 
    }

    how would I also capture the document requests ?

    thx

    Friday, September 13, 2019 10:40 AM

All replies

  • It's going to depend a bit on how the documents are linked on your Sharepoint pages, but the basic idea would be to add a click handler to your <a> tags and send events to App Insights when the link matches a request for an Excel or Word doc.

    Blogger Ejaz Hussain posted something similar on his blog for capturing requests to external URLs.

    $('a').click(function () {
    	var link = this.hostname;
    	if ($('a').isExternal(link)) {           
    		window.appInsights.trackEvent('External link clicked with URL',
    			{
    				Url: $(this).attr('href')
    			},
    			{});
    	}
    	else { 
    	}
    });


    Wednesday, September 18, 2019 4:43 PM
    Moderator
  • that may work - but to implement on a sharepoint farm with over 3000 sites might require a bit of effort.

    adding the appinsight js to the wopiframe and wopiframe2 aspx pages - is actually alot easier :)

    Wednesday, September 25, 2019 6:42 PM