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
- Sign into Google Analytics and click Admin
- On the Property column click Custom Definitions and then Custom Dimensions
- Click New custom dimension
- Give it a name, select User as a scope and click Save
- Store the Index of the dimension
- From the left bar click on Customizations and then Custom Reports
- Click on New Custom Report
- Give it a Title
- On Metric Groups select your metric e.g. Page Views
- On Dimension Drilldowns click add dimension and select your property from Custom Dimensions
- Click Save
Modify SharePoint Google Analytics Solution
- Open your site collection with SharePoint Designer
- Go to All Files Style Library GoogleAnalytics js
- Check out and edit in advanced mode the file ga.js
- Locate the lines below
ga('create', 'UA-0000000-1', 'auto'); ga('send', 'pageview');
- Replace it by
ExecuteOrDelayUntilScriptLoaded(getPageContextInfo, "core.js"); function getPageContextInfo() { ga('create', 'UA-0000000-1', 'auto'); ga('send', 'pageview',{ 'dimension1': _spPageContextInfo.userLoginName }); }
- Replace the UA-0000000-1 by your Tracking Id, if needed replace dimension1 by the index of your dimension e.g. dimension3
- Save the file, check it in and publish the major version
View custom properties on Google Analytics
- Open Google Analytics
- Click on Customization and then Custom Reports
- 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.
April 20, 2018
Great post Joao, quick simple in the end, always keen to extend analytics to provide to clients, thanks.
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
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
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?
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.”
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.”
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.
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
August 15, 2019
Hi Simon,
I forgot to add the link with the code, you can check it here