Overview
Analytics tool monitoring is essential for any site (it could be a SharePoint site, ASP .Net site or a site created using any other technology and platform) to track the user interaction with the site. The analytics data will help to analyze the pattern or the information that attracts the attention of the user. The analytics enables us to track the interaction of users from different regions and use different devices to access the information from site.
SharePoint Analytics
SharePoint out of the box provides the usage analytics. The analytics engine was completely revamped in SharePoint 2013. The analytics which was a separate component in SharePoint 2010 has now become part of Search service application from the SharePoint 2013 version and it helps to track user interaction such as views, likes, comments, social tags, etc. By default in SharePoint, Usage Reports or Popularity trends show the historical data based on unique users and their hits.
Google Analytics Advantages and Limitations
Advantages of Google analytics
- Site Traffic
Google analytics can provide real time hits information and track the active users and their activity in real time.
- Location based tracking
Google analytics can offer tracking based on users’ geographic location.
- Device based tracking
Google analytics can help to track the request based on devices used to access the information.
Limitations of Google analytics
- File downloads
If file is directly linked from website or in email, Google analytics cannot track it.
- No User names
Google analytics does not provide information about individual user and content accessed by him/her.
- Missing Data Security
SharePoint sites may contain confidential information about employees or customers. Analytics data is stored with Google. So, we cannot guarantee data privacy.
Setup Google Analytics
- Sign in to Analytics account at https://analytics.google.com
- Select Admin tab
- Select Account from dropdown (if it already exists) or click Create Account
- Under Property Settings, click Tracking Info > Tracking Code
- Copy the Website Tracking JavaScript code for later use.
Install Google Analytics on Classic SharePoint sites
Upload Google Analytics Script to SharePoint site
- Save the Website Tracking JavaScript code in a file as ga.js
- Open Classic SharePoint site
- Upload the ga.cs to Style Library
Load the script on SharePoint site
There are multiple ways to add the Google Analytics script to SharePoint site. A simple solution is to add the reference of ga.js uploaded to Style Library in the custom master page and the script will be loaded on all the pages to track the user activities.
If you are not using the custom master page, the other alternative is to inject this script using custom action to SharePoint site. We can use SharePoint Online Management Shell for this. The article has the PowerShell scripts attached to help inject the Google Analytics script to SharePoint site as custom action.
- ps1
This script contains set of functions to add / delete the user custom actions to SharePoint site.
- Activate-GoogleAnalytics.ps1
This script will call UserCustomActions.ps1 to register Google Analytics script as custom action. Please mention your Google analytics tracking code in function Activate-GoogleAnalytics (line# 31)
- ga('create', 'UA-', 'auto');
Once you run the Activate-GoogleAnalytics.ps1, Google Analytics dashboard will immediately start displaying the real time data.
Install Google Analytics on Modern SharePoint sites
- Create a SharePoint Framework (SPFx) Extension solution
- In the interface, add property
- export interface IAnalyticsCustomizerProperties {
- trackingID: string;
- }
- In the class (AnalyticsApplicationCustomizer.ts), add below code
- public onInit(): Promise<void> {
- let trackingID: string = this.properties.trackingID;
- if (!trackingID) {
- Log.info(LOG_SOURCE, "Tracking ID not provided";);
- }else{
- var gtagScript = document.createElement("script");
- gtagScript.type = "text/javascript";
- gtagScript.src = `https:
- gtagScript.async = true;
- document.head.appendChild(gtagScript);
-
- eval(`
- window.dataLayer = window.dataLayer || [];
- function gtag(){dataLayer.push(arguments);}
- gtag('js', new Date());
- gtag('config', '${trackingID}');
- `);
- }
- return Promise.resolve();
- }
- Package the solution
- gulp bundle --ship
- gulp package-solution --ship
- Deploy the sppkg file to SharePoint Modern site.
Summary
Analytics is useful to track the user activities on a site. Use SharePoint out of box analytics to see how users are interacting with the site. Custom Google Analytics can be added to SharePoint sites to track more details.