Add custom attributes - GTM

Add a custom attribute with the name category to the pageview event using Google Tag Manager (GTM).

Before you begin:

  1. Configure GTM with Exchange prerequisites.
  2. 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:

  1. 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
  1. In your GA Settings variable GA-Id, set the references to custom variables. Use the GA settings variable GA-Id already created before.

    Custom Dimensions > Index: 2,
    Dimension Value: {{customAttributeDimensionVariable}}

    📘

    Note:

    The values for dimensions that are shown here are only for illustrative purposes. You must use the Index and Dimension Value according to your setup.

  2. In the script execution code added to the GTM tag, add the following to the customFunction.

    In this code snippet, cd2 indicates dimension2. Use the index value as applicable.

    google_ubx.googleToUBXPageViewEvent.attributesMapper.push({ "googleName": "cd2", "ubxName": "category", "type": "string" });
  3. 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'});