Custom Variables in Google Analytics, Part 2

Alex Centeno

Director of Digital at AndiSites Inc.
Alex is AndiSites' Director of Digital. He writes on all things digital, including web design, development, SEO, online marketing, photography, and video.
Alex Centeno

In our previous post, we learned what custom variables are. We also learned the differences among custom variables, custom metrics, and custom dimensions–and when to use them. Now let’s learn how to set them up for a website.

First, you’ll need to set up the custom dimension or metric inside your Google Analytics platform:

  1. Log into your Google Analytics platform and click “Admin”.
  2. Select the property you want to add the dimension or metric to.
  3. Click on the “Custom Definitions” button. (see image below)
    Custom Definitions Google Analytics
  4. Select either “Custom Dimensions” or “Custom Metrics” depending on what you’d like to define.

Four Steps to Setting Up a Custom Dimension

  1. Click on the “New Custom Dimension” button at the top. (see image below)
    New Custom Dimension
  2. Give it a name that summarizes the batch of information you are capturing.
  3. Define a Scope. Read more about scopes in the Google Analytics developer guide.
    The scope would be one of the following:

    • Hit
    • Session
    • User
    • Product
  4. Check the “Active” box to start collecting custom dimension data and seeing it in your reports. Remember that you can return to the checkbox and make the dimension inactive. However, you won’t be able to modify it to capture different information.

Three Steps to Setting Up a Custom Metric

  1. Select a Scope and Formatting Type (Integer, Currency, or Time).
  2. You can also input a minimum and maximum value to capture.
  3. Click “Create”.

Add Custom Metric

The Code

Once you’ve created the custom dimensions and metrics in your property’s admin section, you’ll modify the tracking code. (NOTE:  This part is a bit more complex, so make sure that you understand how to modify your environment before proceeding, or ask for help.)

  1. Note the index that identifies your custom metric. For example, if you’re sending data for a “pageview” hit with the index of “2”, and the data is “London”, the code looks like this:
        ga(‘send’, ‘pageview’, { ‘dimension2’ : ‘London’ });
    
  2. In our custom metric, let’s say that visitors to  our website fill out a contact form. Then they select whether they’re interested in information about middle school or high school. You can modify the tracking code to segment the visits based on their selection by creating a dimension called “School Preference”. Depending on the number of dimensions that you have in place, Google Analytics will assign your new dimension an ID number between 1 and 20. In this case let’s say that the dimension was assigned an ID number of “4”, so the code would look like this:
        ga(‘send’, ‘pageview’, { ‘dimension4’ :  “<?php print $school_preference; ?>” });
    

    NOTE:  In this example I use PHP to print the variable named “school_preference” that holds the actual value of the selection in the form.

 

You’re done! Now you can create custom metrics in your properties and understand your users at a higher level of complexity.

Back to Blog