How to add Google tag manager globally to SharePoint Online
A solution to add google tag manager has being requested a few times to me since I released the solutions to add Google Analytics to SharePoint.
In this article you will find a pre-built solution to add Google tag manager to modern SharePoint Online using an Application Customizer.
What is Google Tag Manager?
Google Tag Manager is a tag management system created by Google to manage JavaScript and HTML tags used for tracking and analytics on websites.
How to Setup Google Tag Manager
First things first, before we get into the SharePoint bits let’s set up google tag manager and get everything you need.
- Sign in to your Tag Manager account
- Create a Workspace for your SharePoint site
- From the top bar copy the Tracking ID as shown in the picture below
How to add Google Tag Manager to modern SharePoint
To get the solution provided in this article globally deployed follow the steps below:
- Download and Extract the solution from here
- Open your tenant app catalog
- Click on Apps for SharePoint
- Upload the solution to the catalog
- Check the option Make this solution available to all sites in the organization
- Go to Site Contents
- Open the list Tenant Wide Extensions
- Edit the Gtagmanager item
- Modify the Component Properties and replace the trackingID value by your Google Tag Manager tracking ID.
- Save the item and all your modern SharePoint sites on Office 365 will get Google Tag Manager. If this was the first solution globally deployed on your tenant, it may take up to 20 minutes get available.
As always the source code of this solution is available on GitHub
February 11, 2020
This will record all sites? What about individual site only? For me I’m only looking for metrics on 1 site only
February 12, 2020
Hi Jon,
You can install it in a single site collection or in a single sub site following these steps.
Download it and deploy the solution to the global app catalog, when you reach steps number 5 Uncheck the option Make this solution available to all sites in the organization
Open PowerShell and execute the following commands, PnP PowerShell needs to be installed in your machine
1 – Connect to the site where you want to add the tag manager, change the URL to your own
Connect-PnPOnline -UseWebLogin -Url https://contoso.sharepoint.com/
2 – Apply the extension to the site
Add-PnPCustomAction -ClientSideComponentId “8b00fe9b-cce0-4a46-b74b-0263532d8f94” -Name “GTAGManager” -Title “GTAGManager” -Location ClientSideExtension.ApplicationCustomizer -ClientSideComponentProperties: ‘{“trackingID”:”GTM-*******”}’ -Scope site
Change the tracking id to your own value.
If you want to apply it to a subsite only do the following:
1 – Connect to the sub site using powershell
2 – On the scope parameter use web instead of site
To check if the extension was applied to the site you can use this command
Get-PnPCustomAction -scope site
Change the scope to the value you have used to install the app.
Hope this helps, have a nice day 🙂