Deploy Google Analytics globally on modern SharePoint
Google Analytics articles are very popular in this blog and there are a few already available here, here and here but there is still room for one more, as the SharePoint Framework evolves so does this solution.
Today’s article introduces an easy way to deploy Google Analytics globally to all modern sites in your tenant taking advantage of the Tenant Wide Deployment.
On the previous versions this solution navigation between pages was not properly monitored, this happened due to a bug in the navigation event. Joel Rodrigues took the original code and implemented a fix to it that is included in this version.
The original project available on GitHub was migrated to SPFx 1.7.1, to make use of the Tenant Wide Deployment, the use of this feature requires version 1.6 or higher.
How to use Google Analytics on modern SharePoint
The new version of the solution no longer requires the use of PowerShell, to get it 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 GoogleAnalytics item
- Modify the Component Properties and replace the trackingID value by your Google Analytics tracking ID. Instructions on how to get the tracking ID are available here.
- Save the item and all your modern SharePoint site on Office 365 are ready to be monitored. If this was the first solution globally deployed on your tenant, it may take up to 20 minutes get available.
Download Google Analytics SPPKG V3
Explore Solution on GitHub
February 12, 2019
Hi Joao, how long will it take for metrics to start appearing in GA?
February 14, 2019
Hi Chris,
It doesn’t take long for it to be applied.
Sometimes it doesn’t work if you are connected to the analytics in the same browser where you are using SharePoint.
Let me know if you were able to apply it.
February 12, 2019
Hi Joao,
I’m getting the following error when I run gulp package-solution –ship:
Warning – Admins can make this solution available to all sites immediately, but the solution also contains feature.xml elements for provisioning. Feature.xml elements are not automatically applied unless the solution is explicitly installed on
a site.
[12:12:51] Project google-analytics version:0.0.1
[12:12:51] Build tools version:3.8.33
[12:12:51] Node version:v8.11.3
[12:12:51] Total duration:5.03 s
[12:12:51] Task warnings:1
The build failed because a task wrote output to stderr.
Do you have any clue about this issue?
Thanks
February 12, 2019
As the solution breaks only because a warning. I added a suppression in the gulpfile.js
build.addSuppression(‘Warning – Admins can make this solution available to all sites immediately, but the solution also contains feature.xml elements for provisioning. Feature.xml elements are not automatically applied unless the solution is explicitly installed on a site.’);
February 14, 2019
Hi Leonardo,
Are you installing the solution in the tenant app catalog?
February 17, 2019
[…] Deploy Google Analytics globally on modern SharePoint – João Ferreira (BindTuning) […]
March 11, 2019
Is there a way to publish this on individual sites instead of tenant wide? I got it to publish only to one site but now I can’t put the GA tracking code in anymore. I tried just putting it statically in the source code (from your git hub source) then packaging and uploading it but that didn’t work either.
Thanks.
March 26, 2019
Hi Josiah,
You can if you install the solution in the site collection app catalogs instead of the global app catalog, more information can be found here
March 27, 2019
João, thanks for the response! I ended up hard coding our Analytics ID into the solution then adding the app from the tenant wide catalogue to only the sites we needed. In the future I’ll be sure to remember that Sites Collections can have their own catalogue too. Thanks for the advice.
April 11, 2019
Hi João,
I’m in the same boat as Josiah. Is there a way to deploy to the app catalog and then use powershell to push to an individual site(s). I’m concerned if we install to individual tenant app catalogs, if we update the app we’ll have to reinstall on every single one.
Thanks!
April 23, 2019
Hi Chris,
If you create the app catalog on the site collections where you want to activate the solution you will be able to monitor just the sites where you have it installed.
More information about site collection app catalogs can be found here.
http://sharepoint-tricks.com/tenant-app-catalog-vs-site-collection-app-catalog/
August 6, 2019
Hi João,
I have deployed your solution and it works great. But i am trying to bring in the custom user properties from User Profile. I have read in some article that one access the classic _spPageContextInfo using “this.context.pageContext.legacyPageContext” in SPFX extensions but don’t know how to make use of it.
For example for classic sites:
ExecuteOrDelayUntilScriptLoaded(getPageContextInfo, “core.js”);
function getPageContextInfo() {
ga(‘create’, ‘UA-0000000-1’, ‘auto’);
ga(‘send’, ‘pageview’,{
‘dimension1’: _spPageContextInfo.userLoginName
});
}
August 6, 2019
Hi Irfan,
Thanks for your comment, the code you are sending is for classic SharePoint, and will not work on the modern pages.
On modern SharePoint the core.js is not loaded by default and it needs to be requested by the app so it will need to be implemented in a different way.
I’ll add this feature to my back log to implement it in a feature article.
August 29, 2019
Hi João,
Just wanted to add my organisation’s need for this (or similar) too.
What we’re actually looking for is using the Google Analytics UserID feature to pull in the AAD ObjectID of the account that’s signed in. By utilising the UserID feature, we’re able to use session unification to stitch together sessions and more accurately log new vs. returning users.
Previously in the classic experience, we would use the following:
var spoGoogleAnalyticsUserId = _spPageContextInfo.aadUserId
ga(‘create’, ‘UA-78729897-1’, ‘auto’, {
userId: spoGoogleAnalyticsUserId
});
August 30, 2019
Hi Peter,
Already added a new project to my pipeline, I’ll build a new version of this solution that will get the user ID and will send it to Google Analytics.
Have a nice day 🙂
August 15, 2019
Hi João ,
Quick question. Do you have to enable custom scripting on the site to enable this solution?
Thanks,
Shawn
August 15, 2019
Hi Shawn,
you don’t need to enable the custom scripts to deploy the solution on modern sites.
The custom scripts are only required to enable solutions on classic site collections.
Are you having any issue deploying the solution?
August 16, 2019
Hi João,
We haven’t it tried it yet. I’m not an SP Developer by the way, only an Admin. Before we try this out in our test tenant we want to ensure the we don’t have to enable custom scripts for this to work. We don’t want to open the door to any potential abuses when/if we deploy in production. Also, is there any pre-work we have to do or do we just need to download the solution and upload it to the App Catalog (per your instructions)? It looks like you took all the work you did creating a Application Customizer here:(https://sharepoint.handsontek.net/2017/12/21/how-to-add-google-analytics-to-the-modern-sharepoint/) and made it into a package that we can deploy using our own GA code, is that correct?
P.S. Thanks for the quick response! Shawn
August 16, 2019
Hi Shawn,
The modern solution does not require the use of the custom scripts you can have them disabled as they are by default.
The code for the solution is available on GitHub in case you want to check how it was implemented, in the article I have the instructions to setup your own project showing the google analytics code.
You don’t need to modify the solution to include your own ID when installing it using PnP PowerShell you will be able to pass it through parameter.
You can test this solution on your environment without deploying it globally, you can create a site collection app catalog and install it there. More information on how to create the app catalog can be found here.
If you don’t feel confortable with this approach you can always get a full development tenant to test this kind of solutions before deploying it globally in production, you can get one from here.
Hope you can get the solution to work.
Have a nice day.
August 16, 2019
One more question if you don’t mind…Do we only need to set up the SharePoint Framework environment if we want to ‘create’ new solutions? Thanks
August 16, 2019
Yes, you just need to setup an environment with all the requirements to build SPFx solutions.
I wrote an article a couple years ago explaining how it works it might be useful if you are starting now.
To get the latest version of the framework I suggest you follow the official documentation here.
August 16, 2019
Thanks so much!!! Just a clarification on your last response. I “Do Not” have to go through the steps outlined in “Set up your SharePoint Framework development environment” if I am only going to deploy your solution, if I understand you correctly? Last question, I promise. 🙂 Thanks again.
August 16, 2019
No wiorries Shawn 🙂 exactly you don’t need to setup the development environment.
August 15, 2019
Just adding that for folks not seeing GA results right away, try the real time option or change date to today’s. Also, this works on modern pages in classic sites, which is a bonus. Brilliant, Joao!
August 15, 2019
Hi Matthew, Thanks for your feedback
February 3, 2020
Thank João,
Was so easy to follow.
Nice work.
March 25, 2020
Hi,
It’s possible to have a zip of sources files from this sppkg (v3) ?
Thanks!
March 25, 2020
Hi Gruff,
Sure the preject is available on GitHub here, https://github.com/joaoferreira/SPFx-Google-Analytics/
Once you open de online repo click on the green button that says Clone or Download and then click on Download zip.
Have a nice day
March 25, 2020
Hip hip hip for João Ferreira ! :o))
April 9, 2020
Hi João, tx for sharing. Deployed in our UAT environment. Just wondering does the Google way of collecting analytics contains any confidential information (user information, internal corporate news, vision & mission corporate statements, …) if it’s enabled for all Site Collections. Is it possible to collect stats (using this method) for a selection of Site Collections?
April 9, 2020
Hi Eric,
This solution will not send any custom data to google other than the one that is collected by the anylicts unless you explicity send it as explained in this other article I wrote for classic SharePoint.
https://sharepoint.handsontek.net/2018/04/18/send-sharepoint-user-profile-details-to-google-analytics/
If you are concerned about the data that is sent to Google you can alway user Azure Application Insights to measure it, I also did a solution to easily deploy it for modern SharePoint.
https://sharepoint.handsontek.net/2018/04/18/send-sharepoint-user-profile-details-to-google-analytics/
Unfortunately both solutions for modern SharePoint are deployed globally and will target all modern site collections in the tenant, this has been a common request a definitely need to create a guide where all of this is explained with a new solution to target individual site collections.
Have a nice day and stay safe 🙂
April 9, 2020
1K Tx for your prompt reply
May 28, 2020
Hi João,
Does this solution capture information like what is mostly searched on modern SharePoint? Or any way we can configure in GA script?
May 29, 2020
Hi Praydnya,
This solution does not capture any information directly from search however i think you should be able to apply a filter on Google analytics using the URL.
The SharePoint searches are added to the URL as query strings.
Have a nice day 🙂
May 29, 2020
Hi Joao,
I have quick question. I’m not seeing the component properties option after I had deployed the packaged extension. Do you an idea why is that so? Thanks in advance
Davinder
January 25, 2023
Ignore that for anyone else who misses it, there is a separate list called ‘Tenant Wide Extensions’ where the Component Properties field lives, I was looking for it in the SharePoint Apps columns.