# 5. Creating Custom GTM Variables

## Custom GTM Variables

To actually get this data into your GTM and then into your GA4 account, you will need to create what's called "Custom Variables" in GTM. I will show you how to do this, and I will link to Google's help documentation if you need further assistance. By following my exact directions, you will be able to create custom variables for any and all widget event data you want to track.

{% hint style="info" %}
**IMPORTANT:** Every widget event sends potentially different data. For example, if the widget event is "widget opened" you will not see specific data for a "tool". So to get every single data point you will need to test all 7 widget events.
{% endhint %}

{% hint style="info" %}
**IMPORTANT:** ChatHQ may also add more widget events in the future, or add more data points to existing widget events. Please keep that in mind.

<mark style="color:red;">You can also request specific data and we will do our best to add it to the JSON payload.</mark>
{% endhint %}

### Creating Custom Variables

* First, you need to open the GTM Tag that you want to add custom variables to. For this example I am using the "Tool Opened" widget event tag in my GTM. When adding custom variables, you will need to identify if the variable falls under **"EVENT PARAMETERS"** or **"USER PROPERTIES".**

<figure><img src="https://2949826416-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mh0SIdnTfBpdN11l3wk%2Fuploads%2FSX4M9AeFz0UEYvdgP5wz%2FXnip2023-04-27_12-53-38.jpg?alt=media&#x26;token=3708e21b-cff4-4ec1-b3e8-7c79a536b8fb" alt=""><figcaption></figcaption></figure>

**Event Parameters** consist of data that refers to the widget such as:

* <mark style="color:blue;">Widget ID</mark>
* <mark style="color:blue;">Tool Name</mark>
* <mark style="color:blue;">Tool ID</mark>
* <mark style="color:blue;">Welcome Message ID</mark>
* <mark style="color:blue;">Ect</mark>

**User Properties** consist of data that refers to users and accounts such as:

* <mark style="color:blue;">User ID</mark>

* <mark style="color:blue;">Account ID</mark>

* <mark style="color:blue;">Location ID</mark>

* <mark style="color:blue;">Contact</mark>

* <mark style="color:blue;">Token</mark>

* <mark style="color:blue;">Ect</mark>

* Depending on the type of variable you're adding, you want to **"add a new row"** to that specific section as shown in the screenshot below. In this example, I am adding a custom variable in **"EVENT PARAMETERS"** for the data set **"toolId"**.

<figure><img src="https://2949826416-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mh0SIdnTfBpdN11l3wk%2Fuploads%2FwZ0DpE8o21lgsLeiOBHd%2FXnip2023-04-27_12-55-17.jpg?alt=media&#x26;token=4371dce3-05b9-48bd-a70d-ec0428cc39f4" alt=""><figcaption></figcaption></figure>

* Next, you will need to click the small icon on the far right of the input field that looks like a Leggo with a + sign in the middle of it.

<figure><img src="https://2949826416-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mh0SIdnTfBpdN11l3wk%2Fuploads%2F99sSo2Yl2AtbOGW80AVw%2FXnip2023-04-27_12-55-44.jpg?alt=media&#x26;token=49656778-0d77-4005-9192-b3e3d5dac7c5" alt=""><figcaption></figcaption></figure>

* Once you click that button, you will see a new screen. On this new screen, you will see a list of GTM variables. This is where you will see all the custom variables you've created moving forward. **To create a new custom variable, click the "+" sign in the top right corner of the screen.**

<figure><img src="https://2949826416-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mh0SIdnTfBpdN11l3wk%2Fuploads%2FIfOHUmbQSJEQo6nwLQPT%2FXnip2023-04-27_12-56-03.jpg?alt=media&#x26;token=1aead8bd-274f-4da7-8c1d-539097e97568" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2949826416-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mh0SIdnTfBpdN11l3wk%2Fuploads%2Fh5xj34CAxzgVavOjdZBz%2FXnip2023-04-27_12-56-29.jpg?alt=media&#x26;token=20331507-0557-4c3e-8009-228d4bc88630" alt=""><figcaption></figcaption></figure>

* Next, you need to select the category of variable you are adding to your GTM. You will need to select the **"DATA LAYER VARIABLE"** as shown in the screenshot below:

<figure><img src="https://2949826416-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mh0SIdnTfBpdN11l3wk%2Fuploads%2F4ETegNDwujjE4EiG29t2%2FXnip2023-04-27_12-56-48.jpg?alt=media&#x26;token=9f818cf6-13fe-42a9-bd32-472983e21cda" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2949826416-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mh0SIdnTfBpdN11l3wk%2Fuploads%2FoeFa6LR3zKegC7mwMsxr%2FXnip2023-04-27_12-57-06.jpg?alt=media&#x26;token=96815cd6-d670-4869-8cae-8410c07a8dd1" alt=""><figcaption></figcaption></figure>

* After selecting the **"DATA LAYER VARIABLE"** category, you will only need to configure 2 fields to finish setting up your custom variable:
  * <mark style="color:blue;">**Variable Name**</mark>
  * <mark style="color:blue;">**Data Layer Variable Name**</mark>

<figure><img src="https://2949826416-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mh0SIdnTfBpdN11l3wk%2Fuploads%2F0eJBQQ37ku1N2xVswfGG%2FXnip2023-04-27_12-58-01.jpg?alt=media&#x26;token=4d119e25-f33c-4f46-99ee-a148a108bb6b" alt=""><figcaption></figcaption></figure>

* After you save your new customer variable, you will now see it show up in the previously empty row you created earlier in this process. The last step is to simply add a **"PARAMETER NAME"** that will show up in your GA4 analytic dashboards and reports. You will notice in the image below that the **"PARAMETER NAME"** on the left looks clean and has no special characters like {}, and the **"VALUE NAME**" on the right is enclosed in curly brackets {} because that is the custom variable you just created in the previous steps.

<figure><img src="https://2949826416-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mh0SIdnTfBpdN11l3wk%2Fuploads%2FZKHdsPYQp0f2Zliots2d%2FXnip2023-04-27_12-58-38.jpg?alt=media&#x26;token=a7b45ab4-844c-4a01-a6b9-a01fd1529f63" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2949826416-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mh0SIdnTfBpdN11l3wk%2Fuploads%2F8CajoQXHBvOtNIjFbHKB%2FXnip2023-04-27_12-59-39.jpg?alt=media&#x26;token=a1f54b0c-32a4-460b-8849-5ab2b9295205" alt=""><figcaption></figcaption></figure>

## Submit Changes

Lastly, you need to submit the changes made to your GTM and you're all done!

{% hint style="warning" %}
If you try to test before you submit your changes, you **WILL NOT** see the correct data
{% endhint %}

<figure><img src="https://2949826416-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mh0SIdnTfBpdN11l3wk%2Fuploads%2FSbIy2YuKqrzvZ6RD74qG%2FXnip2023-04-27_13-01-00.jpg?alt=media&#x26;token=cf018910-41c4-4208-8c4b-29942881827f" alt=""><figcaption></figcaption></figure>

## **Adding More Event Parameters**

<mark style="color:blue;">Event Parameters</mark> consist of the widget data that is collected when events are triggered in the widget. ChatHQ surfaces many different widget data points that are available to you to track and quantify. This information is found in the [<mark style="color:blue;">Data Layer</mark>](#data-layer).

* **Parameter Name -** <mark style="color:red;">widget</mark> | **Value -** <mark style="color:red;">{{Widget ID}}</mark>
  * This parameter will show up in your Google Analytics so you can identify which events came from which widget using the Widget ID, and set up filters and reports around it.
* **Parameter Name -** <mark style="color:red;">element</mark> | **Value -** <mark style="color:red;">{{UI Element}}</mark>
  * This parameter will show up in your Google Analytics so you can identify the name of the UI Element that triggered the widget event, and set up filters and reports around it.

<figure><img src="https://2949826416-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mh0SIdnTfBpdN11l3wk%2Fuploads%2FdnVfzu5lvPSWAgFZUqMQ%2FXnip2023-04-24_12-50-05.jpg?alt=media&#x26;token=715466f1-f8ad-47f3-affa-9780645b837c" alt=""><figcaption></figcaption></figure>

## **Adding More User Properties**

<mark style="color:blue;">User Properties</mark> consist of the user data that is collected when events are triggered in the widget. ChatHQ surfaces many different user data points that are available to you to track and quantify. This information is found in the [<mark style="color:blue;">Data Layer</mark>](#data-layer).

* **Property Name -** <mark style="color:red;">contactId</mark> | **Value -** <mark style="color:red;">{{Contact ID}}</mark>
  * This user property will show up in your Google Analytics so you can identify the Contact ID of the individual using your widget, and set up filters and reports around it. This works GREAT when using your widget in your HighLevel for support!
* **Parameter Name -** <mark style="color:red;">locationId</mark> | **Value -** <mark style="color:red;">{{Location ID}}</mark>
  * This user property will show up in your Google Analytics so you can identify the Location ID of the individual using your widget, and set up filters and reports around it. This works GREAT when using your widget in your HighLevel for support!

<figure><img src="https://2949826416-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mh0SIdnTfBpdN11l3wk%2Fuploads%2FIhjvrvr4QMegvA0KA8Kk%2FXnip2023-04-24_12-50-35.jpg?alt=media&#x26;token=2941f331-a124-48b4-ae4b-720a908d5509" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.chathq.io/api-and-developer-docs/custom-tracking-and-analytics/widget-events-in-google-analytics/5.-creating-custom-gtm-variables.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
