Add custom attributes - GTM
Add a custom attribute with the name category to the pageview event using Google Tag Manager (GTM).
Before you begin:
- Configure GTM with Exchange prerequisites.
- Create a custom dimension field with a value dimension2 and with Hit scope in GA. One custom dimension for Personalization is required to send the category attribute data.
To add custom attributes, complete the following steps:
- Create a user-defined variable
customAttributeDimensionVariable
in GTM.
- Variable Type: Data Layer Variable
- Data Layer Variable Name: dimension2 [or dimensionN]
- Data Layer Version: Version 2
- In your GA Settings variable
GA-Id
, set the references to custom variables. Use the GA settings variable GA-Id already created before.
Note:
The values for dimensions that are shown here are only for illustrative purposes. You must use the
Index
andDimension Value
according to your setup.
Custom Dimensions > Index: 2,
Dimension Value: {{customAttributeDimensionVariable}}
- In the script execution code added to the GTM tag, add the following to the
customFunction
.
google_ubx.googleToUBXPageViewEvent.attributesMapper.push({ "googleName": "cd2", "ubxName": "category", "type": "string" });
In this code snippet, cd2 indicates dimension2. Use the index value as applicable.
- Next, add the following code to the
customFunction
.
google_tag_manager[{{Container ID}}].dataLayer.set('dimension2','<<comma-separated category values>>');
You can also add the following code to your HTML page or web page after the GTM code snippet.
window.dataLayer.push({'dimension2' : '<<comma-separated category values>>'});
Updated over 3 years ago