Send SharePoint user profile details to Google Analytics

Integrations are one of my favorite things on technology and today I’ll write about it again!

How to add Google Analytics to SharePoint without modifying the master page is on of the most read articles in this blog; Recently I was asked about sending user profile information from SharePoint to Google Analytics to build better reports and to understand who is using the portals.

In this article I’m explaining step by step how to modify the previous solution and how-to setup Google Analytics to receive the logged in user email or id.

Setup Google Analytics

  1. Sign into Google Analytics and click Admin
  2. On the Property column click Custom Definitions and then Custom Dimensions
  3. Click New custom dimension
  4. Give it a name, select User as a scope and click Save
  5. Store the Index of the dimension
  6. From the left bar click on Customizations and then Custom Reports
  7. Click on New Custom Report
  8. Give it a Title
  9. On Metric Groups select your metric e.g. Page Views/li>
  10. On Dimension Drilldowns click add dimension and select your property from Custom Dimensions
  11. Click Save

Modify SharePoint Google Analytics Solution

  1. Open your site collection with SharePoint Designer
  2. Go to All Files Style Library GoogleAnalytics js
  3. Check out and edit in advanced mode the file ga.js
  4. Locate the lines below
    ga('create', 'UA-0000000-1', 'auto');
    ga('send', 'pageview');
    
  5. Replace it by
    ExecuteOrDelayUntilScriptLoaded(getPageContextInfo, "core.js");
    function getPageContextInfo() {
    	ga('create', 'UA-0000000-1', 'auto');
    	ga('send', 'pageview',{
    	  'dimension1':  _spPageContextInfo.userLoginName
    	});
    }
    
  6. Replace the UA-0000000-1 by your Tracking Id, if needed replace dimension1 by the index of your dimension e.g. dimension3
  7. Save the file, check it in and publish the major version

View custom properties on Google Analytics

  1. Open Google Analytics
  2. Click on Customization and then Custom Reports
  3. Open the custom report to view the custom profile properties

Conclusion

There are several custom properties that you can send from SharePoint to Google Analytics to help understanding better how users are interacting with the intranet portals, however they might be different between SharePoint versions.

If you want to implement this scenario on EU please make sure that all the collected data is GDPR compliant.

Designed by Freepik


2 Responses to “Send SharePoint user profile details to Google Analytics”

  1. Warwick

    April 20, 2018

    Great post Joao, quick simple in the end, always keen to extend analytics to provide to clients, thanks.

    Reply
  2. Marco Santos

    May 21, 2018

    Very useful post. Just dropping here some additional information that may be helpful for others.

    To implement the GA code in the gtag.js format check the following documentation link: https://developers.google.com/analytics/devguides/collection/gtagjs/migration#send_custom_dimensions_and_metrics

    If you’re implementing this in Modern framework you cannot resort to the _spPageContextInfo JS variable. In this case use this.context.pageContext.legacyPageContext[‘userLoginName’]); in SPFx context. You can check the other context variables here: https://medium.com/@rjesh/access-sppagecontextinfo-details-in-sharepoint-framework-8daa3479bf27

    Reply

Leave a Reply


Web developer focused on SharePoint branding, blogger, tech enthusiast. Travelling and sports are my addictions, knowledge and success are my daily motivations.