If you require dimension names in Google Analytics that are not included in the default dimensions and metrics, you can define custom dimensions. Use custom dimensions to collect and analyze data that Analytics doesn't automatically capture. You can send values to custom dimensions with a variable that either pulls data from your web page or uses the data layer to pass specific values.
Use a page variable to set custom dimension values
If you need to associate a value from a list to a Google Analytics session, use a lookup table variable in Tag Manager.
Step 1: Create a custom dimension
To create a custom dimension in Google Analytics:
- Log in to your Google Analytics account.
- Navigate to Admin Property: Custom Definitions Custom Dimensions.
- Click +New Custom Dimension.
- Provide your own value for the Name field and set the Scope menu to the desired value. Ensure that Active is checked.
Example: Set the Name field to 'destination', and the Scope menu to 'Hit'. - Click Create.
- The dimension number will be found in the code examples shown on the Created Custom Dimension page. Record the dimension number. In this example the dimension's index number is 6:
JavaScript:
var dimensionValue = 'SOME_DIMENSION_VALUE';
ga('set', 'dimension6', dimensionValue);
Android SDK:
String dimensionValue = "SOME_DIMENSION_VALUE";
tracker.set(Fields.customDimension(6), dimensionValue);
iOS SDK:
NSString *dimensionValue = @"SOME_DIMENSION_VALUE";
[tracker set:[GAIFields customDimensionForIndex:6] value:dimensionValue];
Step 2: Set up a Tag Manager variable to capture page values
The next step is to set up a Tag Manager variable that will capture a value from a web page that can be used to populate the value of a custom dimension field. The value that you target could come from a number of different sources: An element ID on the page, a value that is generated when an event occurs (e.g. a timer event), or text found in a URL.
For this example, the required value is found in the page URL as a fragment; a string of text that shows up after the hash symbol (#) at the end of the URL. Examples:
- https://example.com/destinations/#paris
- https://example.com/destinations/#newyork
- https://example.com/destinations/#tokyo
Tag Manager can capture the URL fragment values, put those values into a variable, and then use that variable to provide the value for the custom dimension that we want to work with:
- In Tag Manager, click Variables.
- Under User-Defined Variables, click New.
- Click Variable Configuration and select URL as the variable type.
- On the Variable Configuration page, set the Component Type to Fragment.
- Name the variable, e.g. "URL Fragment Variable".
- Click Save.
Step 3: Enable custom dimensions in your Universal Analytics tags
The final step is to enable custom dimensions in your Google Analytics Settings variable:
- Click Variables and then select your Google Analytics Settings variable.
- Open the tag for editing and navigate to More settings Custom Dimensions.
- Click +Add Custom Dimension
- Enter the Index value that you captured in step 1.
- For Dimension Value, use the variable selector icon to select the name of the variable that you created in step 2.
- Click Save.
Set custom dimension values from the data layer
The data layer is the best method for ensuring consistent results in your reports, because you retain full control of the values that are passed to your tags. In this example, dataLayer.push()
is used on a website to add a destination value to the data layer when a button is clicked:
Book this trip to Paris!
</button>
To use Tag Manager to send this value to Google Analytics as a custom dimension, use a data layer variable and then apply the custom dimension to the appropriate Google Analytics tags already deployed on your website.
Step 1: Create a custom dimension
To create a custom dimension in Google Analytics:
- Log in to your Google Analytics account.
- Navigate to Admin Property column: Custom Definitions Custom Dimensions.
- Click +New Custom Dimension.
- Provide your own value for the Name field and set the Scope menu to the desired value. Ensure that Active is checked.
Example: Set the Name field to 'Destination', and the Scope menu to 'Hit'. - Click Create.
- The dimension number will be found in the code examples shown on the Created Custom Dimension page. Record the dimension number. In this example the dimension's index number is 6:
JavaScript:
var dimensionValue = 'SOME_DIMENSION_VALUE';
ga('set', 'dimension6', dimensionValue);
Android SDK:
String dimensionValue = "SOME_DIMENSION_VALUE";
tracker.set(Fields.customDimension(6), dimensionValue);
iOS SDK:
NSString *dimensionValue = @"SOME_DIMENSION_VALUE";
[tracker set:[GAIFields customDimensionForIndex:6] value:dimensionValue];
Step 2: Create a data layer variable
The next step is to create a variable that will capture the correct data layer values:
- In Tag Manager, click Variables.
- Under User-Defined Variables, click New.
- Select Data Layer Variable for the variable type.
- Set the Data Layer Variable Name text field to destination.
- Name the variable, e.g. "destination data layer variable".
- Click Save.
Step 3: Enable a custom dimension in your Universal Analytics tags
The final setup piece is to enable a custom dimension in your Google Analytics Settings variable:
- Click Variables and then click on your Google Analytics Settings variable.
- Click Variable Configuration.
- Click More settings Custom Dimensions.
- Click +Add Custom Dimension.
- Enter the Index value that you captured in step 1.
- For Dimension Value, enter the name of the variable that you created in step 2.
- Click Save.
- Publish your container for the changes to take effect.