Custom Variables

Custom variables are now split into custom dimensions and custom metrics. This allows more flexibility, and you get an increased number of each (currently 20 of each totaling 40, up from 5 for the free version).


_setCustomVar

Sets the custom variable to your specifications. In analytics.js, you need to decide if your custom variable is a metric or a dimension (more on this). Once this is done, you'll need to set it up within the interface before you can use it. Both custom variables and custom metrics / dimensions need to be sent with or before an event.

It should be noted that we actually call it 2 different ways in the analytics.js examples below. The first example uses the "set" method. This allows all of the following "send" events (pageview, social, events, etc.) to utilize the dimensions and metrics until the visitor visits the next page or until the tracker object expires. The second example passes it within the "send" event itself. This makes those dimensions and metrics available to only that one send call. This is important to consider and will vary depending on what you're sending in. Below, we have made the appropriate choice to send in the image information with the image upload event as it only pertained to that image and would not be useful to any other calls on the page, unlike the information about the visitor in the first example.

via ga.js
// "Dimension" example, what user group is the visitor in?
_gaq.push(['_setCustomVar',
    1,              // Index (custom var #)*
    'user group',   // Name*
    'admin',        // Value*
    3               // Scope
]);

// "Metric" example, how many hosted images do they have?
_gaq.push(['_setCustomVar',
    5,              // Index (custom var #)*
    'image count',  // Name*
    '953',          // Value*
    3               // Scope
]);

_gaq(['_trackPageview']);

// * Designates the required items
via analytics.js
/*
 * We want these values passed in with all calls on this page
 * because in this case, it is information about the user.
 * Therefore, we'll use the 'set' command
 */

// Custom Dimension, what user group is the visitor in?
ga('set', 'dimension1', 'admin');

// Custom Metric, how many hosted images do they have?
ga('set', 'metric5', 953);

ga('send', 'pageview');

Now let's see what it looks like when you're pushing the dimensions & metrics in via a send command…

via ga.js
// "Dimension" example, what was the file type of the image?
_gaq.push(['_setCustomVar',
    2,              // Index (custom var #)*
    'image type',   // Name*
    'png',          // Value*
    3               // Scope
]);

// "Metric" example, how large was the image (in kb)?
_gaq.push(['_setCustomVar',
    4,              // Index (custom var #)*
    'image size',   // Name*
    '356',          // Value*
    3               // Scope
]);

// User uploaded a new image
_gaq(['_trackEvent', 'images', 'upload']);

// * Designates the required items
via analytics.js
/*
 * In this example, we'll pretend a user uploaded a new image.
 * We want these values passed in for only this call
 * because in this case, it is information about the event itself.
 * Therefore, we'll pass the values in via the 'send' command
 */
ga('send', 'event', 'images', 'upload', {
    dimension2: 'png',  // What was the file type of the image?
    metric4: 953        // How large was the image (in kb)?
});