If you're like me, you might get a little obsessed with performance and want the best experience for your users. That eventually leads to wanting to have reliable metrics for your SharePoint online tenant. In our case, I wanted better information about our SharePoint based intranet than what I was able to get from our existing web analytics platform. So, I did what any self-respecting geek would do; I threw more instrumentation at the problem.

What's Application Insight And Why Would I Use it?

Application Insights is Microsoft's all singing, all dancing, application performance, analytics, and diagnostics platform. Generally, I use it to get data on custom built applications, but you can also use it for the client-side data of SharePoint Online. You'll notice I said, rather specifically, "client-side" data. Since we don't have access to the back-end of the cloud we can't get the server-side telemetry. Nevertheless, the client-side data is gold.

Getting Started

Before you can add Application Insights, you'll need:

  1. An Azure account to setup an Application Insight instance
  2. Permissions to edit the master page of the SharePoint site that you want creepy-level tracking data about

Create the Application Insights Instance

  1. Login to your Azure Portal
  2. Click on "More Services" on the main menu blade
    More Services in the Azure Portal
  3. Use the search filter to find "Application Insights" and click on it
    Searching for Application Insights
  4. Click on "+ Add" to create a new instance
    Add a new item
  5. Give you new instance a name, select the "ASP.Net web application", and either create a new resource group or pick and existing one. Once you have that filled out, click on "Create" at the bottom. Optionally, you can click on the "Pin to dashboard" option so it appears on your dashboard.
    Configure the Application Insights resource
  6. Grab a coffee or adult beverage while Azure deploys your new Application Insights instance
  7. You should now see the new instance in the list; click on it to load it
    Application Insights item list
  8. Under "Configure" in the menu, click on "Getting Started"
    Getting started item in Application Insights
  9. Next, select "Monitor and Diagnose Client Side Application"
    Choosing the client side monitoring and diagnostics
  10. You'll see the chunk of code that needs to be added to your master page, click on the "Copy" icon to copy it to your clipboard.
    Copying the Application Insights telemetry code

Add The Monitoring Script to Your Master Page

To get the application telemetry, you'll need to add the script to your site's master page. The neat thing is that the short bit of script will automatically grab the full Application Insight package from a CDN and load it.

Note: You can also use SharePoint Designer to edit the master page and I find that's MUCH faster, but it's not for everyone...

  1. Open the "Site Settings" for the site you want to edit
    SharePoint site settings
  2. Click on "Master pages" in the "Web Designer Galleries" section
    Master pages selection in SharePoint
  3. Download the master page you want to edit, in my example I'm updating the oslo.master. Your mileage may vary here...
  4. Locate the closing tag and paste in the code you copied earlier immediately before the . It'll look something like this:
	<SharePoint:CssRegistration Name="Themable/oslo.css" runat="server" EnableCssTheming="true" />
        <script type="text/javascript">  
           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: "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" }); 

          window.appInsights = appInsights; 
  1. Save the file, upload it back to SharePoint and then publish the new version and you're done!
  2. You can now return to your Application Insights instance and start analyzing all the creepy tracking data that it captures

Gotchas and Potential Problems

  1. After setting up Application Insights to monitor our intranet and being ridiculously impressed with the metrics and when it proactively emailed me about a problem, I was ridiculously annoyed to find out my MSDN subscription won't let me increase the amount of data that's logged each day. You can fix this by switching to a pay-as-you-go or using your enterprise agreement.
  2. If other people need to access the data, you'll need to use the "Access control (IAM)" menu to add them to the appropriate role. Selecting the right role is another discussion for later.