Azure Application Insights or Google Analytics with SharePoint

GA-vs-AAI

Recently I was encountered with a question about implementing Google Analytics in a SharePoint on-premise environment that triggered a discussion, whether we should choose Google Analytics or look for Azure Application Insights, which is available in Azure stack. Azure was in use already since SharePoint Servers were hosted on Azure VMs and the Application Insight reports look quite impressive.

I started to look into more details about what to expect from both of these.

Google Analytics

Lets take a look at the Google Analytics first. Google analytics is intended for website usage tracking and traffic optimization, has a nice dashboard and is quite extendable with additional options. It is focused on more on users than the activities/actions they perform on web site. It is obviously well known, free to use and used in various web sites we see over internet.

Google Analytics

 

Azure Application Insights

Azure Application Insights is a comprehensive APM (Application Performance Monitoring) and log analytics solution. It does provide a good level of usage tracking as well but that’s not as rich as Google Analytics by default and rightly so as it’s not intended for stuffs like AdWords. However, where Azure Application Insights scores is it’s extensibility and much richer performance monitoring and failure analytics. This can easily be extended to include additional events like menu item clicks, button clicks and any other such events, which makes it a good candidate to be used with SharePoint, specially when used in intranet environments where actions are more than just browsing the sites. This also comes with a great looking dashboard –

Azure Application Insights

It goes without saying that these dashboards are extendable too and additional charts can be added easily based on multiple parameters –

Chart

Other aspect of Azure Application Insight is a richer performance monitoring and failure analytics. It provides extensive insights about what is going in an application and can be configured to monitor such parameters both at client and server sides. In our case, lets see what it includes in a normal client side configuration which doesn’t require any additional setup or configuration beyond what’s required for normal usage tracking.

Azure Application Insights

And we can drill down to details of each call and dependencies and even below

Drill Down

Cost

Azure Application Insight comes at a cost if your data volume is higher. As explained in the Application Insights Pricing page, in the basic plan, first GB is free every month and you pay about $2.30 per GB every month after that.

Azure Application Insight Pricing

Google Analytics in comparison, is free to use.

The Question

So you got the idea, both of these analytics tools have enough for us. So, naturally the question comes which one to chose for your SharePoint environment.

Many organizations already have been using Google Analytics for their company web sites and other such web applications and are used to seeing usage in Google Analytics Dashboard. But then, having Application Insights gives them the right set of data to improve their SharePoint environment by looking into Slower performing pages, calls and by capturing the details if users are using a particular app which they recently rolled out !

So, here is my take – Go for BOTH 🙂 and you will get best of both worlds.

Yes, that’s not only possible but easy to configure too.

Google Analytics in SharePoint

You can find various articles about how to register for google analytics and related stuffs so I will just focus on the integration part.

So, get the script from analytics.google.com with the tracking code for your SharePoint site or just replace the Tracking code (in bold) in the script below –

  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', 'UA-10162xxxx-1', 'auto');
  ga('send', 'pageview');

Save this as lets say spGoogleAnalytics.js in your local machine.

Note that this doesn’t have the “script” tag at the start or end of the script.

Azure Application Insights in SharePoint

Lets get the script for tracking in Application Insights as well and we will add both in SharePoint in one go. You can choose to do these separately also.

Again, there are various articles on how to create an Application Insight resource and get the tracking script. Essentially, you just create Azure Application Insights resource from Azure portal and from the blade, select Getting Started and then Monitor and Diagnose client side application. In the next blade, you will see the tracking script with the tracking code.

Application Insight Tracking Script

You can either copy the entire script from there or just replace the Instrumentation Key in the script below. You can find the instrumentation key either from that script or from under “Properties” of the Application Insight Resource on Azure portal.

   var appInsights=window.appInsights||function(config){
    function i(config){t[config]=function(){var i=arguments;t.queue.push(function(){t[config].apply(t,i)})}}var t={config:config},u=document,e=window,o="script",s="AuthenticatedUserContext",h="start",c="stop",l="Track",a=l+"Event",v=l+"Page",y=u.createElement(o),r,f;y.src=config.url||"https://az416426.vo.msecnd.net/scripts/a/ai.0.js";u.getElementsByTagName(o)[0].parentNode.appendChild(y);try{t.cookie=u.cookie}catch(p){}for(t.queue=[],t.version="1.0",r=["Event","Exception","Metric","PageView","Trace","Dependency"];r.length;)i("track"+r.pop());return i("set"+s),i("clear"+s),i(h+a),i(c+a),i(h+v),i(c+v),i("flush"),config.disableExceptionTracking||(r="onerror",i("_"+r),f=e[r],e[r]=function(config,i,u,e,o){var s=f&&f(config,i,u,e,o);return s!==!0&&t["_"+r](config,i,u,e,o),s}),t
    }({
        instrumentationKey:"619886e1-xxxx-yyyy-zzzz-3573169e37cb"
    });
       
    window.appInsights=appInsights;
    appInsights.trackPageView();

Once the instrumentation key has been updated, save the file as spApplicationInsights.js in your local machine.

Integrate with SharePoint

Now that we have both Google Analytics and Azure Application Insights tracking scripts ready. Lets take a look at the alternatives for integrating these with SharePoint.

Most of the articles like this for Application Insights and this one for Google Analytics talk about either add the script directly in the SharePoint pages in script editor web part or add these to the master page. Well, adding to each page is not practical and modifying master page is not recommended and also, even if you are using custom master page, there could be many such master pages making it difficult to maintain.

This nice article from bindtuning explains close to what we are going to do – “JavaScript Injection”. But even this one has a lot of steps.

This is not yet supported in SharePoint Online Modern Sites. If used, it will only track the usage of pages which are in classic mode.

We’ll go with a simpler alternative – PowerShell and PnP to help 🙂

Steps to Implement

  • To start with, upload both the JS files (spApplicationInsights.js and spGoogleAnalytics.js) which we created previously in a SharePoint location where all your users have just read access. An ideal place could be in your root site collection’s Assets library.
  • Go to github and download the relevant PnP extension from https://github.com/SharePoint/PnP-PowerShell/releases for your SharePoint environment on your client machine from where the PowerShell scripts would be executed. It has 3 different installers – for SP 2013, for SP 2016 and for SP online.
  • Once installed, open your PowerShell console and execute the following command

 Install-Module SharePointPnPPowerShellOnline
 Connect-PnPOnline –Url "site URL" –Credentials (Get-Credential)
 Add-PnPJavaScriptLink -Name GoogleAnalytics -Url https://site URL/SiteAssets/spGoogleAnalytics.js -Sequence 9998 -Scope Site 
 Add-PnPJavaScriptLink -Name AppInsights -Url https://site URL/SiteAssets/appInsights.js -Sequence 9999 -Scope Site 

While running the first command “Install-Module SharePointPnPPowerShellOnline" if you receive a message like “The term ‘Install-Module’ is not recognized as the name of a cmdlet” just install this PackageManagement PowerShell Modules and that error will go away

  • Above example is for SharePoint Online, if you are using an On-premise version, just change the script as below. Seems like the command expects a connection similar to SharePoint online, so we still need to run Connect-PnpOnline, but provide your on-premise URL and -CurrenetCredentials should work, since you would run these commands from one for the SP servers

 Install-Module SharePointPnPPowerShell2016
 Connect-PnPOnline –Url "site URL" –CurrentCredentials 
 Add-PnPJavaScriptLink -Name GoogleAnalytics -Url https://site URL/SiteAssets/spGoogleAnalytics.js -Sequence 9998 -Scope Site 
 Add-PnPJavaScriptLink -Name AppInsights -Url https://site URL/SiteAssets/appInsights.js -Sequence 9999 -Scope Site 

Scope selected in the command above is Site, which means site collection. If you want to add analytics in all site collections, just run a loop for all site collections. It uses the site collection or sub site which is used in the Connect-PnPOnline URL parameter

To remove the tracking from any site, you can use Remove-PnpJavaScriptLink command.

Performance

Worried about performance? I wouldn’t worry about that. Both Google Analytics and Azure Application Insights are optimized for performance and run in a separate thread without interrupting the users browsing experience.

And that’s it. All the pages of your SharePoint environment will start sending the tracking details to both Google Analytics and Azure Application Insights immediately. You can see Azure Application Analytics dashboard showing content within minutes, Google Analytics dashboard normally takes few hours to a day to reflect.

Enjoy,
Anupam

You may also like

2 comments

  1. Thanks for one’s marvelous posting! I really enjoyed reading it, you may be a great author.
    I will always bookmark your blog and may come back in the future.
    I want to encourage you to definitely continue your great posts, have a
    nice weekend!

Leave a Reply

Your email address will not be published. Required fields are marked *

19 − 6 =