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

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

Intranet portals are the central hub of companies. Important information is communicated to employees through these portals so you need to make sure that everyone can access to the portal no matter the device or technology.

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

With that being said, let’s see how you can add google analytics tracking code to your SharePoint site without editing the master page.

In this article, I will explain step by step how you can build and deploy a script link using a custom action in SharePoint. I will also provide the installation files and the instructions to use the script, 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

You can find more information about google analytics setuphere.

Create SriptLink Custom Action

If you don’t want to deal with code at all, jump to the next section.

Heads up! The solution built in this tutorial uses a declarative SandBox solution to deploy Google Analytics. These type of solutions are supported by Microsoft in the most recent versions of SharePoint, including SharePoint 2016 and SharePoint Online. You can read more about this type of solution here.
  1. Open Visual Studio and create a new project
  2. Select Office/SharePoint templates and SharePoint Empty Project
  3. I will call it SharePointGoogleAnalytics, but you can call it whatever you want
    New SharePoint project
  4. Provide the URL of the site you will 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 again, 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 bellow 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 bellow
    SharePoint Feature Properties
  15. Open the Elements.xml file inside the Style Library module. This file is generated by Visual Studio automatically. However, I prefer to modify it and publish the files automatically once the solution is activated. I also replace the files, if a previous version already exists in the system.Select the entire content of the file and replace it with the code bellow.
    Note: If you are not using the same names I used previously, make sure you adjust the URL and Path

  16. Open the Elements.xml inside the AnalyticsCA module and paste the code bellow.
  17. This XML file will register the custom action and will execute the ga.js on all sites of your site collection.

    Note: If you are not using the same names I used previously, make sure you adjust the ScriptSrc
  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 on 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 bellow, except for the Track ID.
  23. Compile your project and deploy the solution to all the site collections 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

Note: This topic is mainly for the users that didn’t build their own custom action project.

To install the solution in your SharePoint and start tracking your sites follow these 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, the 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 with your own track id from Google Analytics
    Add JavaScript File
  8. Save the file, check it in and publish it as a major version.

That’s it – you are good to go! Your site collection is now being tracked by Google Analytics.

If you have multiple site collections and don’t you want to open SharePoint Designer for each site collection, 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 to 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

Conclusion

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

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 pages, 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, there are no script sites available. As soon as something comes out to support those sites I’ll review this article again.

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 bellow.

Download Google Analytics WSP
Explore Solution on GitHub

By | 2017-05-08T15:46:51+00:00 March 17th, 2017|Articles|2 Comments

About the Author:

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

2 Comments

  1. soujanya August 30, 2017 at 4:46 am - Reply

    Hi, I have tried the above solution in SharePoint online and it is working fine and I can see script link available in document library and Team sites.

Leave A Comment

Captcha * Time limit is exhausted. Please reload CAPTCHA.