# Creating Variables

Often, when we activate states based on datalayer events, we also want to read data from the same source (`window.dataLayer` and its object pushed with that event). We need to create a JTM variable that reads those values.

<figure><img src="https://2315305008-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fy15ncufYr341K5U8q6Of%2Fuploads%2Fgit-blob-8ded24ace50e2b829603fdec4b4259451a1214a5%2FScreenshot%202025-07-21%20at%2020.05.48.png?alt=media" alt=""><figcaption><p>Navigate to the "Variables" section in your DCP and create a new element (click the “Add New Variable”-Icon).</p></figcaption></figure>

Choose “GTM data layer value” on the next page as the variable template type.

<figure><img src="https://2315305008-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fy15ncufYr341K5U8q6Of%2Fuploads%2Fgit-blob-1bddc31b5e615863f34e633c49736fd26a44287e%2FScreenshot%202025-07-21%20at%2020.06.45.png?alt=media" alt=""><figcaption></figcaption></figure>

The value to be read from the dataLayer in our example is the parameter key “event\_category”:

<figure><img src="https://2315305008-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fy15ncufYr341K5U8q6Of%2Fuploads%2Fgit-blob-1fef531cc5ff12f77e3ae1938dffd4009ebe7ea5%2FScreenshot%202025-07-21%20at%2020.08.41.png?alt=media" alt=""><figcaption></figcaption></figure>

This will retrieve the value of “event\_category” that was pushed to the dataLayer object accordingly in this example:

{% code lineNumbers="true" %}

```javascript
window.dataLayer.push({
  event:"gtm.init",
  event_category:"category_value"
});
```

{% endcode %}

This is just for the visual example. Our JTM variable would report the value from this according dataLayer.push (“category\_value”).

{% hint style="warning" %}
Watch out! This configuration might be ambiguous, though.
{% endhint %}

This is where the “Occurrence pointer” comes into play. The options here are:

* first
* last
* state context

Meaning that in an array (dataLayer) a property might be named multiple times. The `event` property, for example, appears with each push in that array. So it is ambiguous. You can select “last” and refer to the most recent event (latest push, highest index in that array), however, this might not work in all situations.

Hence, the “event\_object” is submitted to the state in the code above. This way, you can point exactly to the property of the event object. Specifically with race conditions, this option might be a good idea.

Read more on this topic of [contextual states here](https://jentis-documentation.gitbook.io/jentis-documentation/~/revisions/1DMs0aeoEuKK3MFz5UCA/jentis-dcp-elements/states).
