Blog

Previous: SharePoint Framework: The Future
Next: Allcloud Joins 1871 Digital Startup Hub
Optimizing Google Analytics for SharePoint and Office 365

Optimizing Google Analytics for SharePoint and Office 365

Most enterprises use third-party analytics tools for tracking SharePoint and Office 365 usage. Marketing teams often use tools like Google Analytics, WebTrends, and Piwik for public analytics, so those become the de facto standards for internal analytics as well.

Each is a great option, but web analytics platforms don’t really understand the intricacies of SharePoint and Office 365. For instance, there’s absolutely no way to filter by specific sites, site collections, or lists by default.

Although SharePoint natively tracks some useful data such as daily hits per site, the built-in interfaces for extracting that data have always been very limited. This has been especially true since SharePoint 2013 re-engineered the analytics engine and hid most reports.

When it comes to SharePoint and Office 365 sites, there are common questions you’re likely to ask:

  1. Which of my sites are most active? Are there dormant sites we should retire?
  2. Which users are most engaged? Are there users completely avoiding the intranet?
  3. Besides pages, which content is most popular?

Fortunately, it’s possible to answer each of those questions through a one-time configuration that will reap long-term rewards.

Step 1: Add Custom Dimensions to Google Analytics

These custom variables can be tracked as Google Analytics custom dimensions. You can add up to 20 custom dimensions, which allow you to filter and create reports based on criteria you define.

  1. To add these dimensions, first log into your account.
  2. Browse to the property specific to your SharePoint or Office 365 environment. If you don’t have a property already, create one.
  3. Once that’s in place, click “Admin”.
  4. Under the “Property” menu, click “Custom Definitions”, then select “Custom Dimensions”.
  5. Click ”New Custom Dimension”, then add each of the twelve dimensions below.

SharePoint Google Analytics Custom Dimensions

Dimensions will include:

  1. Site Collection Title
  2. Site Collection URL
  3. Site Title
  4. Site URL
  5. List ID
  6. List Title
  7. List URL
  8. Item ID
  9. Item Title
  10. Item URL
  11. Name
  12. Username

Step 2: Update Your Master Page to Populate Dimensions

Next, let’s edit our SharePoint / Office 365 master page to ensure the Google Analytics logic runs on every page. If you have multiple site collections, the master page will need to be updated in each site collection, which can be automated through PowerShell.

The following JavaScript code should be added to your master page, either directly in the page or linked as a “.js” file. Update the “googleAnalyticsKey” value to match that of your property.

<script type="text/javascript">
// From https://allcloud.com/blog/optimizing-google-analytics-for-sharepoint-and-office-365

// Update this to match your Google Analytics key.
var googleAnalyticsKey = '<strong>UA-12345678-1</strong>';

// Load Google Analytics library.
(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');

// Send SharePoint context to Google Analytics.
function SharePointGoogleAnalytics(){
	// Load client libraries on demand.
	ExecuteOrDelayUntilScriptLoaded(function(){
		ExecuteOrDelayUntilScriptLoaded(function(){
			var clientContext = new SP.ClientContext();
			var site = clientContext.get_site();
			var rootWeb = site.get_rootWeb();
			var web = clientContext.get_web();
			var user = web.get_currentUser();
			clientContext.load(site);
			clientContext.load(rootWeb);
			clientContext.load(web);
			clientContext.load(user);

			var listId = _spPageContextInfo.pageListId;
			var itemId = _spPageContextInfo.pageItemId;				
			var list = null;
			var item = null;
			if (listId != null){
				list = web.get_lists().getById(listId);
				clientContext.load(list, 'Title', 'DefaultViewUrl');

				if (itemId != null){
					item = list.getItemById(itemId);
					clientContext.load(item, 'Id', 'DisplayName');
				}
			}

			function SPGASend(){
				ga('create', googleAnalyticsKey, 'auto');
				
				var userNameParts = user.get_loginName().split('|');

				ga('send', 'pageview', {
					'dimension1': rootWeb.get_title(),
					'dimension2': site.get_url(),
					'dimension3': web.get_title(),
					'dimension4': web.get_url(),
					'dimension5': listId,
					'dimension6': (list ? list.get_title() : ''),
					'dimension7': (list ? list.get_defaultViewUrl() : ''),
					'dimension8': itemId,
					'dimension9': (item ? item.get_displayName() : ''),
					'dimension10': (item ? (list.get_defaultViewUrl() + '?ID=' + item.get_id()) : ''),
					'dimension11': user.get_title(),
					'dimension12': userNameParts[userNameParts.length - 1]
				});					
			}

			function SPGAError(errObj, errMsg){
				// Optional error handling.
			}

			clientContext.executeQueryAsync(Function.createDelegate(this, SPGASend), Function.createDelegate(this, SPGAError));
		}, 'SP.js');
	}, 'SP.Runtime.js');
}

// Load once SharePoint has fully rendered.
_spBodyOnLoadFunctionNames.push("SharePointGoogleAnalytics");
</script>

Step 3: Wait

That’s it! Within a day or two, the lagging indicators should be visible within Google Analytics.

For a detailed look at how to take advantage of these metrics, check out this post by LunaMetrics.

Contact Allcloud to design a targeted, analytics-driven intranet:

/ 844-6-CLOUD-6

Previous: SharePoint Framework: The Future
Next: Allcloud Joins 1871 Digital Startup Hub