Google Analytics Setup

How to set up Google Analytics to handle many situations that life will throw at you.

This is a record of the “how” and the “why” of implementing a web analytics solution for your organization. Many of the details are technical in nature but also explain why tracking codes and configurations should be implemented as specified.

Google Analytics Tracking Code (GATC) Implementation

The Google Analytics Tracking Code (GATC) must be correctly configured and placed on every page of your website and its subdomains.

These settings will be reflected in the GATC that follows:

  • Universal Analytics version of the GATC. If you’re using an older version of Google Analytics, you should strongly consider upgrading to Universal Analytics.
  • Multiple top-level domains configuration of the GATC. This allows your organization to track data for domains other than your main website’s domain. This is useful, for example, if you have a 3rd party shopping cart or other piece of Martech that doesn’t allow for custom domains.

Following is the GATC that must be implemented on all HTML pages on your organization’s websites and 3rd party sites (e.g., the shopping cart hosted on a 3rd party domain). The code should be included early within the <head> section of the document.

<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXX-X', 'example.com', 'auto', { 'allowLinker': true });
ga('send', 'pageview');
</script>

Filters

Several filters will need to be applied to the different views depending on the goals of each view. The filters are defined in this section.

Include Authorized Domains

Prevents other sites on the web from either intentionally or unintentionally feeding their website data into your organization’s accounts.

The example below would only allow traffic from the domains samplecompany.com or anotherdomain.com.

Filter Settings

Filter Name Include Authorized Domains
Filter Type Custom filter, Include
Filter Field Hostname
Filter Pattern samplecompany\.com|anotherdomain\.com
Case Sensitive No

Add Hostname to Request URI

Adds the hostname (i.e. www.example.com) as a top-level folder for more specific reporting. For example, the following subdomains would be listed as the first “folders” in the Content Drilldown report:

  • www.example.com
  • subdomain.example.com
  • www.example.co.uk
  • etc.

With this in place, subdomains can be compared with each other at a glance as well as all subdomains combined into a top-level, global set of metrics.

Filter Settings

Filter Name Add Hostname to Request URI
Filter Type Custom filter, Advanced
Field A -> Extract A [Hostname] (.*)
Field B -> Extract B [Request URI] (.*)
Output To -> Constructor [Request URI] $A1$B1
Field A Required Yes
Field B Required No
Override Output Field Yes
Case Sensitive No

All Lowercase

To ensure that URLs stay consistent, this filter converts all characters in the URI to lowercase.

Filter Settings

Filter Name All Lowercase
Filter Type Custom filter, Lowercase
Filter Field Request URI

Exclude Corporate IP Address

This filter excludes traffic from a shared corporate IP address, range of IP addresses, or a list of IP addresses. This keeps you from recording your own employees’ traffic along with your customers’.

Filter Settings

Filter Name Exclude Corporate IP Address
Filter Type Predefined filter
[Exclude] [traffic from the IP addresses] [that are equal to]
IP address XXX.XXX.XXX.XXX

Exclude Staging

This filter excludes traffic from the staging environment at staging.example.com.

Filter Settings

Filter Name Exclude Staging
Predefined filter Predefined filter
[Exclude] [traffic to the hostname] [that are equal to]
Hostname staging.example.com

Track Referring CPC URLs #1

First part to tracking the actual referring domain from a PPC network. By default, PPC networks show up in reports as “google / cpc.” With this series of filters applied, this will be split into lines like “google (via google.com)” and “google (via ask.com).”

This method is mentioned on pp. 276-77 of Advanced Web Metrics with Google Analytics by Brian Clifton.

Filter Settings

Filter Name Track Referring CPC URLs #1
Filter Type Custom filter, Advanced
Field A -> Extract A [Referral] ^https?://([*/]*)
Field B -> Extract B [Campaign Medium] cpc|ppc
Output To -> Constructor [Custom Field 1] $A1
Field A Required Yes
Field B Required Yes
Override Output Field Yes
Case Sensitive No

Track Referring CPC URLs #2

Second part to tracking the actual referring domain from a PPC network. By default, PPC networks show up in reports as “google / cpc.” With this series of filters applied, this will be split into lines like “google (via google.com)” and “google (via ask.com)“.

This method is mentioned on pp. 276-77 of Advanced Web Metrics with Google Analytics by Brian Clifton.

Filter Settings

Filter Name Track Referring CPC URLs #2
Filter Type Custom filter, Advanced
Field A -> Extract A [Campaign Source] (.*)
Field B -> Extract B [Custom field 1] (.*)
Output To -> Constructor [Campaign Source] $A1 (via $B1)
Field A Required Yes
Field B Required Yes
Override Output Field Yes
Case Sensitive No

Views

The roll-up view will collect all data for your Google Analytics account’s identifier (e.g., UA-XXXXXXX-X). (See the GATC code under the Google Analytics Tracking Code (GATC) Implementation section above.) Now different views can be created with different settings and filters applied to them. This section lists typical views, their settings, and which filters should be applied to each.

Unfiltered View

Working with filters can sometimes cause problems, especially if a mistake is made while changing one. Implementing an erroneous filter will cause an “outage” in your Google Analytics account until the issue is fixed. Either data will stop collecting in the affected views, or the wrong data will be collected. And Google Analytics has no way of “going back” and fixing the data, even after fixing problems with a filter or configuration error.

Also, it is handy to have an unmodified copy of the data to compare with the data included in the filtered views. For example, if a filter is applied to remove traffic from your oranization’s employees, the unfiltered view can be examined to estimate how much traffic should be filtered out and compared with the filtered version. This can be checked periodically to make sure that the filters are operating correctly.

View Settings

For this reason, your organization should add a view called Unfiltered. The Unfiltered view should be set up with the following settings (leaving fields not mentioned here either blank, as default selections, or configuring as you need):

View Name: Unfiltered

Filters Applied

None.

Roll-up View

The Roll-up View collects and sorts all data into a single view. It gives the “big picture” view of how all of your organization’s properties are performing with customers.

This view also includes a filter that adds the hostname as a top-level folder for more specific reporting. For example, the following subdomains would be listed as the first “folders” in the Content Drilldown report:

  • www.example.com
  • subdomain.example.com
  • www.example.co.uk
  • etc.

With this in place, subdomains can be compared with each other at a glance as well as all subdomains combined into a top-level, global set of metrics.

This view also blocks out the following:

  • Spammy bots that junk up your referral data
  • Tracking of employees’ visits so that only customer traffic will be analyzed

View Settings

The Roll-up View should be set up with the following settings (leaving fields not mentioned here either blank, as default selections, or customized to your needs):

View Name: Roll-up
Exclude all hits from known bots and spiders Yes

Filters Applied

  • Include Authorized Domains
  • Add Hostname to Request URI
  • All Lowercase
  • Exclude Corporate IP Address
  • Exclude Staging
  • Track Referring CPC URLs #1
  • Track Referring CPC URLs #2

Goals

To most effectively utilize Google Analytics, it is necessary to create goals linked to specific conversions.

A goal completion can be defined as simple as a visit to a specific page on the site (a “thank you for signing up” page or perhaps the “contact us” page), a specific amount of time spent on the site, or the triggering of an event.

When possible, a goal funnel should be defined to capture the steps that lead to the completion of a goal. That way, your organization can report on weak spots in processes like shopping cart checkouts, lead form completions, newsletter sign-ups, and so on.

Error Page Tracking

If your website serves up custom 404 and 500 error pages, you can add a small tracking snippet to these pages to track the following:

  • When these errors occur
  • What URLs are causing the errors
  • Where the user was referred from to trip the error

Here’s the snippet to add to your 404 error page:

<script>
var referrer = document.referrer;
if (referrer === '') {
referrer = 'No Referrer';
}
ga('send', 'event', '404 Error', document.location.href, referrer, 0, { nonInteraction: true });
</script>
view raw 404-errors.html hosted with ❤ by GitHub

And the one for 500 pages is similar, with a minor difference:

<script>
var referrer = document.referrer;
if (referrer === '') {
referrer = 'No Referrer';
}
ga('send', 'event', '500 Error', document.location.href, referrer, 0, { nonInteraction: true });
</script>
view raw 500-errors.html hosted with ❤ by GitHub

These errors will then be trackable via Events in Google Analytics, which can be found under the Behavior section.

Related resources

  1. Google Analytics
  2. Advanced Web Metrics with Google Analytics, Brian Clifton
  3. New in the playbook: How I set up Google Analytics, Chris Peters
  4. Universal Analytics Upgrade Center, Google Developers
  5. The Essential Guide To Error Tracking Using Google Analytics, Dan Wilkerson

Last updated on