Add Google Analytics To SharePoint Sites

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

  1. Sign in to Analytics account at https://analytics.google.com
  2. Select Admin tab

    SharePoint
  1. Select Account from dropdown (if it already exists) or click Create Account

    SharePoint
  1. Under Property Settings, click Tracking Info > Tracking Code

    SharePoint
  1. Copy the Website Tracking JavaScript code for later use.

Install Google Analytics on Classic SharePoint sites

Upload Google Analytics Script to SharePoint site

  1. Save the Website Tracking JavaScript code in a file as ga.js
  2. Open Classic SharePoint site
  3. 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)
    1. ga('create''UA-''auto');  

Once you run the Activate-GoogleAnalytics.ps1, Google Analytics dashboard will immediately start displaying the real time data.

SharePoint

Install Google Analytics on Modern SharePoint sites
  1. Create a SharePoint Framework (SPFx) Extension solution
  2. In the interface, add property
    1. export interface IAnalyticsCustomizerProperties {  
    2.      trackingID: string;  
    3. }  
  1. In the class (AnalyticsApplicationCustomizer.ts), add below code
    1. public onInit(): Promise<void> {      
    2.     let trackingID: string = this.properties.trackingID;  
    3.     if (!trackingID) {  
    4.         Log.info(LOG_SOURCE, "Tracking ID not provided";);  
    5.     }else{  
    6.         var gtagScript = document.createElement("script");  
    7.         gtagScript.type = "text/javascript";  
    8.         gtagScript.src = `https://www.googletagmanager.com/gtag/js?id=${trackingID}`;      
    9.         gtagScript.async = true;  
    10.         document.head.appendChild(gtagScript);    
    11.    
    12.         eval(`  
    13.             window.dataLayer = window.dataLayer || [];  
    14.             function gtag(){dataLayer.push(arguments);}  
    15.             gtag('js'new Date());      
    16.             gtag('config',  '${trackingID}');  
    17.         `);  
    18.     }  
    19.     return Promise.resolve();  
    20. }  
  1. Package the solution
    1. gulp bundle --ship  
    2. gulp package-solution --ship  
  1. 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.