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:

  1. Download and Extract the solution from here
  2. Open your tenant app catalog
  3. Click on Apps for SharePoint
  4. Upload the solution to the catalog
  5. Check the option Make this solution available to all sites in the organization
  6. Go to Site Contents
  7. Open the list Tenant Wide Extensions
    SharePoint Solution Deplayment
  8. Edit the GoogleAnalytics item
  9. 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.
  10. 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.

Add JavaScript File

Download Google Analytics SPPKG V3
Explore Solution on GitHub

Business vector created by rawpixel.com – www.freepik.com


35 Responses to “Deploy Google Analytics globally on modern SharePoint”

  1. Chris

    February 12, 2019

    Hi Joao, how long will it take for metrics to start appearing in GA?

    Reply
    • João Ferreira

      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.

      Reply
  2. Leonardo

    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

    Reply
    • Leonardo

      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.’);

      Reply
      • João Ferreira

        February 14, 2019

        Hi Leonardo,

        Are you installing the solution in the tenant app catalog?

        Reply
  3. […] Deploy Google Analytics globally on modern SharePoint – João Ferreira (BindTuning) […]

    Reply
  4. Josiah

    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.

    Reply
    • João Ferreira

      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

      Reply
      • Josiah

        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.

        Reply
  5. chris

    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!

    Reply
  6. Irfan Cheema

    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
    });
    }

    Reply
    • João Ferreira

      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.

      Reply
      • Peter Fell

        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
        });

        Reply
        • João Ferreira

          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 🙂

          Reply
  7. Shawn Duggan

    August 15, 2019

    Hi João ,
    Quick question. Do you have to enable custom scripting on the site to enable this solution?

    Thanks,
    Shawn

    Reply
    • João Ferreira

      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?

      Reply
      • Shawn Duggan

        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

        Reply
        • João Ferreira

          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.

          Reply
        • Shawn Duggan

          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

          Reply
          • João Ferreira

            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.

          • Shawn Duggan

            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.

          • João Ferreira

            August 16, 2019

            No wiorries Shawn 🙂 exactly you don’t need to setup the development environment.

  8. matthew howell

    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!

    Reply
  9. Sam

    February 3, 2020

    Thank João,

    Was so easy to follow.

    Nice work.

    Reply
  10. Gruff

    March 25, 2020

    Hi,

    It’s possible to have a zip of sources files from this sppkg (v3) ?

    Thanks!

    Reply
  11. Gruff

    March 25, 2020

    Hip hip hip for João Ferreira ! :o))

    Reply
  12. Eric Deferm

    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?

    Reply
  13. Pradnya

    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?

    Reply
    • João Ferreira

      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 🙂

      Reply
  14. Davinder Singh

    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

    Reply

Leave a Reply


Web developer focused on SharePoint branding, blogger, tech enthusiast. Travelling and sports are my addictions, knowledge and success are my daily motivations.