Google Analytics is a web analytics service offered by Google that tracks and reports website traffic. SharePoint provides some reports too, but these ones can be quickly insufficient if you need more KPI.

Previously, on the classic experience of SharePoint, it was possible to add Google Analytics script by:

  • Creating a Master Page and including the script (it was not a Best Practice by the way 🤭)
  • Creating a Custom Action 👍🏻

Fast forward to today, modern SharePoint experiences support the most common scenarios that organizations use SharePoint for. Many organizations have already switched to using modern experiences, and many more are evaluating how they would benefit of modernizing their intranets.

Waldek Mastykarz

That is totaly true! How to visualize in real time or through a complete dashboard several native and custom KPI?

In addition, you can access several features for FREE 💸.

Google Analytics for Modern SharePoint - for everybody

So, now... How to setup this solution to your Modern SharePoint sites? This subject has already been addressed in the post of Joao Ferreira.

What can I bring more to that?

  1. Upgrade his solution to use the latest version of SharePoint Framework
  2. Perform some enhancements
  3. Allow you to deploy the solution from any platform thanks to Office365-CLI. If you too, you are a macOS user and working on several SharePoint solutions yourself, you want to have a way to provision Google Analytics, without having to use a VM or a Windows computer
  4. Create scripts to implement the solution easier

Google Analytics implementation

I wrote two scripts that will allow you to implement the solution easily.

Prerequisites

  1. Google Analytics Tracking ID: https://support.google.com/analytics/answer/1008080
  2. Tenant App Catalog or Site Collection App Catalog:
  3. If you choose to use PowerShell solution, you'll need to install the PnP PowerShell module
  4. If you choose to use the any platform solution, you'll need Bash and Office365-CLI
Note: the user account that will perform the following actions has to have sufficient permissions on the target sites collection and on the App Catalog.

PowerShell (Windows only)

  1. Download the PowerShell solution from the repository
  1. Unzip the package
  2. From your PowerShell terminal, connect to your target site collection with Connect-PnPOnline cmdlet
Connect-PnPOnline -Url https://contoso.sharepoint.com/sites/target-site
  1. Launch the setup.ps1
.\setup.ps1 -siteUrl https://contoso.sharepoint.com/sites/target-site -trackingID 'UA-000000000-1'

If you can't execute the PowerShell script, cause of security reason, before unzip the package, execute the Unblock-File cmdlet.

Note: in this example, the package is deploy to the Site Collection App Catalog. You have other options like illustrated into the README.md, that allow you to manage the deployment.

Any platform (Linux, MacOS, Windows)

  1. Download the any platform solution from the repository
  1. Unzip the package
  2. Allow setup.sh script execution
chmod +x ./setup.sh
  1. From your Bash terminal, connect to your target site collection with spo login cmdlet
spo login https://contoso.sharepoint.com/sites/target-site
  1. Launch the setup.sh
./setup.sh --siteUrl https://contoso.sharepoint.com/sites/target-site --trackingID 'UA-000000000-1'

Note: in this example, the package is deploy to the Site Collection App Catalog. You have other options like illustrated into the README.md, that allow you to manage the deployment

Pretty simple, isn't it ?

Enjoy 💪🏻

Hoping this post will help you 😉