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

Limitations of Google analytics

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.

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. eval(`
    12. window.dataLayer = window.dataLayer || [];
    13. function gtag(){dataLayer.push(arguments);}
    14. gtag('js', new Date());
    15. gtag('config', '${trackingID}');
    16. `);
    17. }
    18. return Promise.resolve();
    19. }
  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.