How to add Google Analytics to SharePoint without modifying the master page

Google Analytics is the most widely used web analytics service in the internet, it provides a concise analysis with reports of everything that happens in your site as well as real time reports, mostly targeted for public sites it also works on intranets.

Intranet portals are the central hub of companies; important information is communicated to employees through this portals and you need to make sure that everyone can access to it no matter the device or technologies used.

Analyzing your intranet portal will help you to understand how users interact with it, what are the most viewed sites and what sites do you need to improve.

Add JavaScript File

With this being said let’s check how you can add google analytics tracking to your SharePoint site without touching the master page.

In this article, I explain step by step how to build and deploy a script link using a custom action into SharePoint, I am also providing the installation files and the instructions to use it if you don’t want to build it yourself.

How to setup Google Analytics

First things first, before we get into the SharePoint bits let’s set up google analytics and get everything you need.

  1. Sign in to your Analytics account
  2. Select the Admin tab
  3. Select an account from the menu in the ACCOUNT column, or CREATE NEW ACCOUNT if you don’t have one already
  4. Select a property from the menu in the PROPERTY column
  5. Under PROPERTY, click Tracking Info Tracking Code
  6. Save the Website tracking script code
    Save Google Analytics tracking code

More information about google analytics setup can be found here.

Create SriptLink Custom Action

If you are not into code jump to the next section.

  1. Open Visual Studio and create a new project
  2. Select Office/SharePoint templates and SharePoint Empty Project
  3. I’ll call it SharePointGoogleAnalytics, but you can call it whatever you want
    New SharePoint project
  4. Provide the URL for the site to use to debug
  5. Select Deploy as sandboxed solution and click Finish
    SharePoint Customisation Wizard
  6. Once your project opens go to the Properties and set Include Assembly in Package to False, this is required to build a declarative sandbox solution
    SharePoint Solution Properties
  7. Right click on your project, go to Add and select New Item
  8. Select Empty Element and call it AnalyticsCA, this will be the ScriptLink Custom Action
    Add new item
  9. Right click on your project, go to Add and select New Item
  10. Select Module and call it Style Library
  11. Delete the Sample.txt file that is created by default
  12. Create the folder structure below inside the Style Library Module
    GoogleAnalytics js
  13. Right click in the js folder, go to Add and select New Item, from the Web group select the JavaScript File and call it ga.js
    SharePoint Project Structure
  14. At this point your project structure should look like the image below
    SharePoint Feature Properties
  15. Open the Elements.xml file inside the Style Library module, this file is generated by Visual Studio automatically however I like to modify it to it to publish the files automatically once the solution is activated and to replace the files if a previous version already exists in the system. Select the entire content of the file and replace it by the code below.
    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="">
      <Module Name="StyleLibraryJS" Url="Style Library/GoogleAnalytics/js" RootWebOnly="FALSE" Path="Style Library\GoogleAnalytics\js">
        <File Url="ga.js" Type="GhostableInLibrary" Level="Published" ReplaceContent="TRUE"  />
  16. Open the Elements.xml inside the AnalyticsCA module and paste the code below.
  17. <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="">
        ScriptSrc="~SiteCollection/Style Library/GoogleAnalytics/js/ga.js"

    This xml file will register the custom action and will execute the ga.js in all sites of your site collection.

  18. Open the Features folder and rename the Feature1 to GoogleAnalytics feature
  19. Open GoogleAnalytics feature
  20. Type a Title and Description for your features, both fields will be visible in your site collection
  21. Change the scope from Web to Site, this way the script will be deployed to all the sites in the site collection
    Add JavaScript File
  22. Open the ga.js file in the Style Library module and paste there the script code you got from Google without the script tags. Your code will be like the code below except for the Track ID
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      ga('create', 'Track ID', 'auto');
      ga('send', 'pageview');
  23. Compile your project and deploy the solution to all the site collection you want to track

Once you deploy the solution your Google Analytics dashboard will immediately display real time data.

Add JavaScript File

Install Google Analytics on SharePoint

To install the solution in your SharePoint and start tracking your sites follow this steps:

  1. Download the solution
  2. On your site collection root site go to Settings Site Settings Solutions
  3. Upload and activate the solution, once you activate the solution google analytics script is immediately added to your site however it is not tracking your site yet, you still need to add your track id to the script
  4. Open your site with SharePoint designer
  5. Go to All Files Style Library Google Analytics js
  6. Check out and edit the ga.js file in advanced mode
  7. Locate the words TRACK ID and replace it by your own track id from Google Analytics
    Add JavaScript File
  8. Save the file check it in and publish it as major version.

You are good to go; your site collection is being tracked by google analytics.

If you have multiple site collections and don’t want to open SharePoint designer for every single one to edit the JavaScript file you can use my SharePoint WSP Repackage tool to modify the solution and create your own unique WSP with your Track ID.

How deactivate Google Analytics tracking

If you want to stop tracking a specific site collection do the following:

  1. On your site collection root site and go to Settings Site Settings Site Collection Features
  2. Locate the Google Analytics feature and deactivate it
    Add JavaScript File


In this article, you learned how to add Google Analytics to SharePoint without touching the master pages and how to create a script link custom action step by step.

If the master page was blocking the analytics adoption in your company either because you are on SharePoint Online or because you don’t want to break your beautiful custom master page this is the way to go.

Unfortunately, this solution will not work on the new Team Sites and Document Libraries on SharePoint Online because for now they are no script sites. As soon as something comes out to support those sites I’ll review this article.

A new version of this article for Modern SharePoint is available here.

As always the source code of this project is available on my personal GitHub repository and you can download the WSP solution from this article, the links are down below.

Download Google Analytics WSP
Explore Solution on GitHub

Designed by Freepik

9 Responses to “How to add Google Analytics to SharePoint without modifying the master page”

  1. Tobias Roth

    September 26, 2017

    Hey Joao,
    thank you for the Article. Great News for me who’s working with GA and who has several potential clients where I can implement this!

    Since it is a JavaScript that was set up, you will not have any problems with implementing other (tracking-) codes right? If I’m not mistaken you will also be able to put in your Google Tag Manager!? Would be great!

    Greetings from Germany

    • João Ferreira

      October 11, 2017

      Hi Tobias,

      Since this is just script you can implement what ever you want.
      You have access to the script file, it’s located in the style library.

  2. Gaurav Goyal

    October 22, 2017


  3. Alex

    November 3, 2017

    Hi Joao,

    Nice solution it works really great. I’m just wonder how can I go about using this solution with a blog site? It seems Google Analytics doesn’t show any information when I’m on a blog site (I use this for the news).

    Should I just put the tracking code on this site or is there another way to do it clean?


    • João Ferreira

      November 12, 2017

      Hi Alexander,

      Unless you have a script error this solution will run in all your sites with the exception of the modern office 365.
      Make sure that the feature is active in the site collection where the blog is.

  4. Dean Whittaker

    November 22, 2017

    Hi Joao,

    I set this up on a site we use at work using the 2nd method, it seems to be working fine but the page views only show the home page and not any of the new blogs etc.. Have I done something wrong in the setup?



    • João Ferreira

      November 22, 2017

      Hi Dean,

      Can you please check in the browser console if there is any script error?
      The analytics script is added to all the sites and pages with the exception of modern site in the site collection.
      I’ll inspect this further to check if there is something that I can do in the code specifically for blogs.


    December 18, 2017

    This is code is not working for me. Not sure what i am doing wrong?

    • João Ferreira

      December 18, 2017

      Hi Anderson,

      Are you using the WSP provided by me or have you built the solution yourself?
      Please make sure that the tracking code on the ga.js script was replaced, and the file is published and checked in.
      To debug the ga.js script do the following using google chrome.

      1. On the site where you have the google analytics press F12
      2. Press CTRL + P and then type ga.js
      3. Open the ga.js file and verify if the line ga('create', 'UA-XXXXXX-1', 'auto'); has your unique tracking code

      Let me know how it went.
      Please notice that this solution is only valid to the classic SharePoint it will not work for the modern pages, I’m working on a solution for the modern pages and it should be available here in the blog before Christmas.


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.