# Advanced State Use Case

In some cases, it is essential to customize a JENTIS State further. The optional properties documented before can be used with the input to the initState function.

Let's run through an example that generates a more enhanced JENTIS State. Here we will activate a JENTIS State for each external data layer item, where we must process each item individually and not from a global perspective.

This is important in situations where JENTIS is loaded asynchronously and accesses a pre-existing data layer. The following might be a simple precondition.

{% code lineNumbers="true" %}

```javascript
var dataLayer = [{
  event: "1"
  name: "event_1"
},{
  event: "2",
  name: "event_2"
}];
```

{% endcode %}

If such a dataLayer object is defined before JENTIS is loaded, we must be sure to process each of these events individually. From a global perspective, there is no certain "event" but multiple entries, which might bring us into trouble if we'd like to activate a trigger on the event name "event\_1." The array contains two such entries, and we might miss either of them if we do not check each event individually as a JENTIS State.

To resolve this, the contextualStateInformation comes into play. For each JENTIS State, an object with a snapshot of an event can be submitted. Let's give this a try:

{% code lineNumbers="true" %}

```javascript
function(initState){
  //generate this object with the mentioned properties in an appropriate scope
  optionalParamObj = {};

  //resetting the JENTIS Data Layer is optional in this case, as no further information is pushed this time
  optionalParamObj.stateCallback = function() {
    window.jentis.tracker.resetDatalayer();
  }

  for(var i = 0; i < dataLayer.length; i++){
    optionalParamObj.contextualStateInformation = dataLayer[i];

    initState(optionalParamObj);
  }
}
```

{% endcode %}

With this example, we execute each entry of the dataLayer (presumably an event object). Now, we can be sure each such external event is translated into a JENTIS State without losing the context (event object).

Now, this would be fruitless if no element took advantage of the contextual object. This can be accessed in frontend variables (custom Javascript variable—[Variables](/developer-guide/variables.md)).

You can create such a variable and access the corresponding object from a JENTIS State. Which might be the event's "name" value.

{% code lineNumbers="true" %}

```javascript
function(stateContextObject){
  if(stateContextObject.stateContext && 
     stateContextObject.stateContext.name)
    return stateContextObject.stateContext.name;
  else
    return undefined;
}
```

{% endcode %}

This will return the event's "name" property (from the dataLayer object submitted in the JENTIS State code above).

With this functionality, you can build and customize your JENTIS DCP configuration around all use cases.


---

# 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.jentis.com/jentis-dcp-elements/states/state-registry-creating-and-managing-jentis-states/advanced-state-use-case.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.
