Google Analytics is a web traffic and demographics reporting service provided free of charge by Google. Google Analytics functionality can be added to any web site by setting up an account and adding a snippet of JavaScript to every page within the web site. Once configured you can see all types of statistics such as how users were referred to your site, number of hits per page, where your users are located geographically, what types of browsers and OS they are using, and much more.
This post outlines how to add Google Analytics to a SharePoint Publishing site. You must have edit rights for all site Master Pages. Google Analytics can complement the Usage Reports available to Site Collection Administrators and Search Term Reports available to Shared Service Provider Administrators. Analytics can also be used to provide read-only access to users who do not have administrative rights on the SharePoint site but whose job function might benefit from access to detailed site usage data.
1-1. Click here to register your site for Google Analytics. Note that the web site you are registering can be a public, intranet, or private company site. You can register the site with a new or existing Google account, and you have the option at any time to allow other Google accounts to act as administrator or have read-only rights to the Analytics reports.
1-2. After you have registered and created a profile for your site, get the JavaScript code for the site. These 2 help pages on Google explain how to find the JavaScript code and add it to your site pages (don’t add any JavaScript to your site pages yet):
Where can I find my tracking code from within my Google Analytics account?
How do I add tracking code to my website?
1-3. Open your site in a browser, and evaluate the page titles displayed in the browser title bar and tabs as you click through the site. Do your pages have meaningful page titles that are unique for each page? If not, you should correct this before adding Google Analytics to your site. Otherwise you will not be able to differentiate the traffic for each page.
Google Analytics provides a quick and easy way to track the search terms being entered by users of your SharePoint Publishing site. Although SharePoint can be configured to track search terms, it requires additional configuration by the Farm Administrator and the search terms are only visible to the Shared Service Provider Administrators. To fully take advantage of SharePoint search functionality such as Best Bets, Site Collection Administrators and Content Authors need to know what search terms are being entered by users. Having to manually request the Shared Service Provider deliver this information on an ongoing basis is a waste of resources when you can easily configure Google Analytics to track it at no expense.
Assuming you are using a SharePoint search site and have not altered any of the defaults or code for search queries, all SharePoint search term queries are submitted using the parameter name k. It’s important that you are not using the parameter name k for any of your forms or custom development running within the site. To configure Google Analytics to track the search term queries, you need to configure the Site Search settings in the Google Analytics site profile. See the Google Analytics Help article How do I set up Site Search for my profile? for details of how to find and configure the Site Search settings.
After you have setup Site Search, your settings should look like the screenshot below:
The next step is to add the JavaScript tracking code obtained in step 1-2 to your site Master Pages. As described in the Google references in section 1, you need to add the tracking code to the end of each site Master page, just prior to the </body> tag. How you do this depends on which of the 3 cases below your site development and publishing processes fall into:
3-1. If the site is production only, meaning you don’t have a development, test, or staging environment, and the changes you make to the Master Pages are immediately applied to production, then just paste the JavaScript into the master pages prior to the </body> tag, save, and publish.
3-2. If the site has pre-production environments such as development, test, or staging, and site Master Pages are maintained as content specific to each environment and solution files or content deployment are not used to publish the same Master Page to each environment, then you can still copy-paste tracking code to the production environment only.
3-3a. If the site has pre-production environments such as development, test, or stage, and the same version of each Master Page is published to each environment with solution files or content deployment, then you will want to setup a mechanism to prevent having your pre-production traffic logged as production traffic. Otherwise the clicks generated in your pre-production environments by developers and testers will show up in the reports and bias the interpretations of site traffic, what pages are most popular, etc.
If your site falls into case 3-3, you have 2 choices for filtering out the pre-production clicks from your Google Analytics reports. If you know the IP addresses or range of IP addresses for all users who will access the pre-production sites, and you don’t need to track those users when they access your production site, then you can exclude specific IP addresses or a range of IP addresses in the Analytics Settings. If you cannot use IP filtering for all pre-production users, or if you need to capture the IP addresses of those users in production, then you can add code to your Master Pages or create an ASP.NET User Control and include it in your Master Pages to conditionally render the tracking code.
If your situation falls into category 3-1 or 3-2 above, then you can skip this section. If your site falls into case 3-3 and you need to conditionally include or exclude the JavaScript tracking code depending which environment you are running in, then you can achieve this in a reusable fashion with an ASP.NET User Control.
3-3-1. Add an AppSetting configuration property to Web.config or any config file readable by the ASP.NET ConfigurationManager which sets a flag to display or hide the tracking code in the environment in which the code is running. This flag would be set to true only in the production environment, or in pre-production environments when testing that the code is included correctly.
<add key="IncludeGoogleAnalytics" value="false" />
3-3-2. Create a user control called GoogleAnalytics.ascx and deploy to _controlTemplates. The code below does not display the tracking code when the flag from step 3-3-1 is set to false or the current page is in edit mode. This is to prevent tracking of these requests and also to limit the possibility of JavaScript clashes when editing publishing pages:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="GoogleAnalytics.ascx.cs" Inherits="GoogleAnalytics" %> <script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> try { var pageTracker = _gat._getTracker("YOUR_SITE_TRACKER_ID"); pageTracker._trackPageview(); } catch(err) {}</script>
public partial class GoogleAnalytics : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) { // FormMode publishing states: Display, Edit, Invalid, New this.Visible = ConfigurationManager.AppSettings["IncludeGoogleAnalytics"] == "true" && (SPContext.Current.FormContext.FormMode != SPControlMode.Edit && SPContext.Current.FormContext.FormMode != SPControlMode.New); } }
3-3-3. Include the new user control at the end of each Master Page.
<%@ Register TagPrefix="Google" TagName="GoogleAnalytics" src="~/_controltemplates/YOURPATH/GoogleAnalytics.ascx" %>
<Google:GoogleAnalytics Visible="false" runat="server" /> </body> </html>
After your have published the Google Analytics tracking code and confirmed all pages in your production environment are including the JavaScript tracking code, then you can start to track your site reports in Google Analytics. Analytics currently updates once each night for the previous day’s traffic, and after you deploy the code for the first time it will take 12-24 hours for data to start showing up in the Analytics Dashboard.
In addition to the Dashboard displayed when you click to view the Site Reports in Google Analytics, I have found the 4 screens listed below to be very useful in understanding how users are accessing the site. The navigation path required to reach each screen are pictured as a reference for how to access these reports.
4-1. Content by Title
4-2. Map Overlay
4-3. Site Search Terms
4-4. Browsers and OS
a@href@title@target, b, blockquote@cite, em, i, img@src, strike, strong, sub, sup, u