# JENTIS Data Layer State

The `jts_push_submit(cb)` function is a utility used to **intercept and enhance the behavior of the native JENTIS `_jts.push()`** method. It allows custom logic (like firing callbacks, triggering events, or modifying payloads) to be executed **whenever tracking data is pushed** to the JENTIS Data Layer.

This is especially useful for extending or customizing the tracking behavior (e.g. triggering beacons, transforming legacy data formats, or resetting the data layer after submission).

### Purpose

* Wraps the original `_jts.push()` function.
* Monitors push calls, including special tracking commands like `submit`, `event`, and `promotion*`.
* Executes a callback `cb` when certain conditions are met (e.g. tracking submission).
* Optionally sends data using the `sendBeacon` method if certain commands are tracked.

### Key Behaviors

#### 1. **Overwrites `_jts.push()`**

* Saves the original `_jts.push()` to `oldFunc`.
* Replaces `_jts.push` with a new function (`newPushFunc`) that includes additional logic before optionally calling `oldFunc`.

#### 2. **Handles Legacy Promotions**

* When tracking commands like `promotionclick` or `promotionimpression` are detected, `_transformPromotion()` is called.
* This method reads the last promotion object, transforms it into the current format, and re-pushes it as a `promotion` with the correct `type`.

#### 3. **Handles Special Track Commands**

* The `cb()` callback is invoked when any of the following is true:
  * A `track: "submit"` or `track: "event"` is pushed.
  * The second argument is `true` (indicating immediate submission).
* These are typically moments when all queued data is sent to the server.

#### 4. **Supports sendBeacon Requests**

* If the `track` command is one of the values in `arrSendBeaconConfig` (e.g., `addtocart`, `event`), the function marks the request as `sendBeacon`.

### Function Breakdown

#### `arrSendBeaconConfig`

An array of tracking types that should be sent via the `sendBeacon` API for more reliable background delivery.

```js
jsCopyEdit["event", "addtocart", "removefromcart", "addtowishlist", "removefromwishlist", "productlistclick", "promotionclick", "promotionimpression"]
```

#### `_transformPromotion(promotionType)`

Transforms legacy `promotion*` tracking commands into the modern format and pushes the transformed object to the data layer.

#### `_findElement(arr, target)`

Simple helper function to check whether a specific value (`target`) exists in an array (`arr`).

#### `optionalParamObj`

An object passed to the callback containing:

* A `stateCallback` used to reset the `window._jtsDataLayerContext`.
* An optional `requestType` (e.g., `sendBeacon`) when applicable.

### Initial Data Handling

If the `_jts` array already contains items (i.e., data was pushed before the override), they are immediately passed through the new push function to ensure consistent processing.

```js
jsCopyEditif (typeof _jts === "object") {
  for (...) {
    newPushFunc(_jts[i]);
  }
}
```

### Example Usage

You might use `jts_push_submit()` to trigger custom logic (e.g., analytics logging or DOM manipulation) every time a submission occurs:

```js
jsCopyEditjts_push_submit(({ stateCallback, requestType }) => {
  // Custom logic on submit/event
  console.log("Data layer submitted");
  stateCallback(); // Reset the current context if needed
});
```

### Summary

The `jts_push_submit(cb)` function is a powerful hook into the JENTIS data layer that allows developers to:

* Monitor and enhance tracking behavior
* Transform legacy tracking formats
* Trigger custom logic upon data submission
* Utilize modern delivery methods like `sendBeacon`

It provides an elegant way to extend the functionality of JENTIS without altering the default data capturing mechanisms.
