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
  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


9 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
    • Krishant

      August 22, 2019

      Hi Marco
      Thanks for this tip. I tried using the code you suggested for Modern SharePoint (SPFx) however I am not seeing any data in my GA for User ID.
      Has this changed recently?
      Thanks

      Reply
      • João Ferreira

        August 22, 2019

        Hi Krishant,

        I’m not aware of any change on the Google Analytics side.
        Are you not seeing any user, or is just yours?

        Reply
  3. Simon Henry

    July 23, 2019

    Great post João, thanks.

    Do you know how the user ID can be encoded / anonymised to prevent the username or email address from being shared with Google Analytics? This way the configuration is compliant with Google Analytics terms of service (shown below) and prevents personally identifiable information from being sent to Google.

    “The Analytics terms of service, which all Analytics customers must adhere to, prohibits sending personally identifiable information (PII) to Analytics (such as names, social security numbers, email addresses, or any similar data), or data that permanently identifies a particular device (such as a mobile phone’s unique device identifier if such an identifier cannot be reset). Learn more about how to avoid sending PII. Your Analytics account could be terminated and your data destroyed if you use any of this information.”

    Reply
  4. Simon Henry

    July 23, 2019

    This is from the analytics documentation – I assume from the above example the User ID from SharePoint is the same as the username eg. user@domain.tld:

    “User ID
    User ID enables the analysis of groups of sessions, across devices, using a unique, persistent, and non-personally identifiable ID string representing a user. To learn why you should implement the User ID, see Benefits of using the User ID feature.

    To implement the User ID with analytics.js:

    Provide your own unique, persistent, and non-personally identifiable string ID to represent each signed-in user. This ID is most often provided by an authentication system.
    Set the User ID on the tracker:
    ga(‘create’, ‘UA-XXXXX-Y’, ‘auto’, {
    userId: USER_ID
    });
    ga(‘send’, ‘pageview’);
    Important! The USER_ID value should be a unique, persistent, and non-personally identifiable string identifier that represents a user or signed-in account across devices.”

    Reply
    • João Ferreira

      August 9, 2019

      Hi Simon,

      You will have to encrypt the email in the JavaScript file before sending it to Goggle.
      Here is an algorithm that you can use
      If you use always the same algorithm you will be able to track the individual users.

      Reply
      • Simon Henry

        August 9, 2019

        Thank you very much, I can’t see the algorithm yet are you sharing it as a comment or an attachment?

        Kind regards, Simon

        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.