|
| 1 | +# Google Tag Manager Injector |
| 2 | + |
| 3 | +## Summary |
| 4 | + |
| 5 | +This SharePoint Framework (SPFx) Application Customizer injects **Google Tag Manager (GTM)** into SharePoint Modern pages, enabling analytics and tag management across your tenant. It initializes the standard GTM `dataLayer`, loads the GTM script via `SPComponentLoader`, and hooks into SharePoint's navigation events to track **Single Page Application (SPA) route changes** — ensuring every page view is captured without a full browser reload. |
| 6 | + |
| 7 | +Built to be compliant with the **SharePoint Online Content Security Policy (CSP)** enforcement introduced in March 2026. Because GTM is loaded dynamically at runtime, `https://www.googletagmanager.com` must be manually registered as a **Trusted Script Source** in the SharePoint Admin Center before deployment. See [Prerequisites](#prerequisites) for the required steps. |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +## Compatibility |
| 12 | + |
| 13 | +| :warning: Important | |
| 14 | +|:---------------------------| |
| 15 | +| Every SPFx version is optimally compatible with specific versions of Node.js. In order to be able to Toolchain this sample, you need to ensure that the version of Node on your workstation matches one of the versions listed in this section. This sample will not work on a different version of Node.| |
| 16 | +|Refer to <https://aka.ms/spfx-matrix> for more information on SPFx compatibility. | |
| 17 | + |
| 18 | +This sample is optimally compatible with the following environment configuration: |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +-Incompatible-red.svg "SharePoint Server 2016 Feature Pack 2 requires SPFx 1.1") |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +## Applies to |
| 31 | + |
| 32 | +- [SharePoint Framework](https://aka.ms/spfx) |
| 33 | +- [Microsoft 365 tenant](https://docs.microsoft.com/sharepoint/dev/spfx/set-up-your-developer-tenant) |
| 34 | + |
| 35 | +> Get your own free development tenant by subscribing to [Microsoft 365 developer program](http://aka.ms/o365devprogram) |
| 36 | +
|
| 37 | +## Prerequisites |
| 38 | + |
| 39 | +- Node.js v22.14.0 or compatible version |
| 40 | +- A Google Tag Manager account and Container ID (e.g. `GTM-XXXXXXX`) |
| 41 | +- Tenant Administrator access to the SharePoint Admin Center |
| 42 | + |
| 43 | +### ⚠️ Required: CSP Trusted Script Source Registration |
| 44 | + |
| 45 | +SharePoint Online enforces [Content Security Policy (CSP)](https://learn.microsoft.com/sharepoint/dev/spfx/content-securty-policy-trusted-script-sources) on all Modern pages from **March 1, 2026** onwards. This solution uses `SPComponentLoader.loadScript()` to load GTM dynamically at runtime — SharePoint does **not** auto-trust sources loaded this way, so you must register the GTM domain manually. |
| 46 | + |
| 47 | +**Via SharePoint Admin Center:** |
| 48 | + |
| 49 | +1. Go to **SharePoint Admin Center** → **Advanced** → **Script sources** |
| 50 | +2. Select **Add source** |
| 51 | +3. Enter `https://www.googletagmanager.com` and save |
| 52 | + |
| 53 | +> Wildcard expressions (e.g. `*.googletagmanager.com`) are not supported. Use the exact origin. |
| 54 | +
|
| 55 | +**Verify:** Load a Modern page with the solution active and append `?csp=enforce` to the URL. Open DevTools (F12) → Console. No `Content Security Policy` violations means the configuration is correct. |
| 56 | + |
| 57 | + |
| 58 | +## Contributors |
| 59 | + |
| 60 | +- [@saiiiiiii](https://github.com/saiiiiiii) | |
| 61 | + |
| 62 | +## Version history |
| 63 | + |
| 64 | +| Version | Date | Comments | |
| 65 | +| ------- | ------------- | --------------- | |
| 66 | +| 1.0 | April 2026 | Initial release | |
| 67 | + |
| 68 | +## Disclaimer |
| 69 | + |
| 70 | +**THIS CODE IS PROVIDED _AS IS_ WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.** |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +## Minimal Path to Awesome |
| 75 | + |
| 76 | +- Clone this repository |
| 77 | +- Update your GTM Container ID in `src/extensions/gtmInjector/GtmInjectorApplicationCustomizer.ts`: |
| 78 | + |
| 79 | + ```typescript |
| 80 | + const GTM_ID: string = 'GTM-XXXXXXX'; // Replace with your GTM Container ID |
| 81 | + ``` |
| 82 | + |
| 83 | +- Ensure that you are at the solution folder |
| 84 | +- In the command-line run: |
| 85 | + - `npm install -g @rushstack/heft` |
| 86 | + - `npm install` |
| 87 | + - `heft start` |
| 88 | +- Add your SharePoint site URL to `config/serve.json` under `pageUrl` |
| 89 | +- Complete the [CSP Trusted Script Source registration](#-required-csp-trusted-script-source-registration) before testing against a CSP-enforced tenant |
| 90 | + |
| 91 | +**To build and package for deployment:** |
| 92 | + |
| 93 | +```bash |
| 94 | +heft test --clean --production |
| 95 | +heft package-solution --production |
| 96 | +``` |
| 97 | + |
| 98 | +Upload the generated `.sppkg` from `sharepoint/solution/gtm-tracker.sppkg` to your Tenant App Catalog. The solution is configured with `skipFeatureDeployment: true`, allowing tenant-wide activation directly from the App Catalog. |
| 99 | + |
| 100 | +Other build commands can be listed using `heft --help`. |
| 101 | + |
| 102 | +## Features |
| 103 | + |
| 104 | +This extension injects Google Tag Manager into SharePoint Modern pages and enables full analytics tracking across Single Page Application (SPA) navigations. |
| 105 | + |
| 106 | +This extension illustrates the following concepts: |
| 107 | + |
| 108 | +- Dynamically loading an external analytics script using `SPComponentLoader.loadScript()` in a CSP-compliant manner — avoiding inline scripts which are blocked by SharePoint Online from March 2026 onwards |
| 109 | +- Initializing a standard GTM `dataLayer` and pushing the `gtm.js` start event on page load |
| 110 | +- Tracking SPA navigations by listening to both SharePoint's `navigatedEvent` (from `this.context.application`) and the browser's native `popstate` event |
| 111 | +- Preventing duplicate script injection across component re-renders using a `window.__gtmLoaded` guard flag |
| 112 | +- Deploying a tenant-wide Application Customizer with `skipFeatureDeployment: true` for zero-touch rollout |
| 113 | + |
| 114 | +> Notice that better pictures and documentation will increase the sample usage and the value you are providing for others. Thanks for your submissions in advance. |
| 115 | +
|
| 116 | +> Share your web part with others through Microsoft 365 Patterns and Practices program to get visibility and exposure. More details on the community, open-source projects and other activities from http://aka.ms/m365pnp. |
| 117 | +
|
| 118 | +## References |
| 119 | + |
| 120 | +- [Getting started with SharePoint Framework](https://docs.microsoft.com/sharepoint/dev/spfx/set-up-your-developer-tenant) |
| 121 | +- [Support for Content Security Policy (CSP) in SharePoint Online](https://learn.microsoft.com/sharepoint/dev/spfx/content-securty-policy-trusted-script-sources) |
| 122 | +- [SharePoint Online CSP Enforcement Dates and Guidance](https://techcommunity.microsoft.com/blog/spblog/sharepoint-online-content-security-policy-csp-enforcement-dates-and-guidance/4472662) |
| 123 | +- [Use Microsoft Graph in your solution](https://learn.microsoft.com/sharepoint/dev/spfx/web-parts/get-started/using-microsoft-graph-apis) |
| 124 | +- [Publish SharePoint Framework applications to the Marketplace](https://learn.microsoft.com/sharepoint/dev/spfx/publish-to-marketplace-overview) |
| 125 | +- [Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp) - Guidance, tooling, samples and open-source controls for your Microsoft 365 development |
| 126 | +- [Google Tag Manager — Developer Quickstart](https://developers.google.com/tag-platform/tag-manager/web) |
| 127 | +- [Heft Documentation](https://heft.rushstack.io/) |
| 128 | + |
| 129 | + |
| 130 | +## Help |
| 131 | + |
| 132 | +If you encounter any issues while using this sample, [create a new issue](https://github.com/saiiiiiii/sp-dev-fx-extensions/samples/js-application-gtm-injector/issues/new). |
| 133 | + |
| 134 | +<img src="https://m365-visitor-stats.azurewebsites.net/sp-dev-fx-extensions/samples/js-application-gtm-injector" /> |
0 commit comments