# How to create a Custom Tracker via the DataLayer

You can create Custom Trackers directly linked to the DataLayer you have implemented in AB Tasty. These Custom Trackers can then be used to create metrics based on your DataLayer data.

{% embed url="<https://fast.wistia.net/embed/iframe/nxz6sqgm8v?seo=true&web_component=true>" %}

{% hint style="info" %}
Please note that since the video production, AB Tasty has implemented [the tracker status monitoring feature](/reporting-and-performances/reporting/tracker-status-monitoring.md).

Custom tracker can also be created JavaScript code, for more information, refer to [How to create a Custom Tracker with custom JavaScript code](/assets-library/trackers-page/how-to-create-a-custom-tracker-with-custom-javascript-code.md).
{% endhint %}

## Requirements <a href="#h_01hxs27h60fwsp3zsa6hkt1hay" id="h_01hxs27h60fwsp3zsa6hkt1hay"></a>

To create a Custom Tracker via DataLayer you must:

* Declare a DataLayer on this [page](https://app2.abtasty.com/settings/integration/datalayer)

  <figure><img src="https://support.abtasty.com/hc/article_attachments/13960298429724" alt=""><figcaption></figcaption></figure>

The available DataLayer are the following:

* Google Tag Manager
* Tealium
* Commander Act

We don’t manage the custom dataLayer as we can’t predict its structure.

To create a custom tracker for your DataLayer:

1. From the [Trackers page](/assets-library/trackers-page.md) (Library > Tracking > Trackers), go to the Custom Trackers tab.
2. Click *Create Custom Tracker* on the top right of the table.
3. Select *From a DataLayer.*

<img src="/files/YsN628tNZNgsJt6WpGVz" alt="" width="345">

## Tracking the value of a key in your DataLayer <a href="#h_01hkyyxqns2pjzsg4qygjdkps5" id="h_01hkyyxqns2pjzsg4qygjdkps5"></a>

<img src="/files/Imh5TbO1X3H4ll2kgw9R" alt="" width="375">

### Structure of the DalaLayer items <a href="#h_01hxs2ba0m8n0r1p2rybkef1rh" id="h_01hxs2ba0m8n0r1p2rybkef1rh"></a>

Each dataLayer has their own structure.\
Let’s focus on Google Tag Manager. Inside GTM, the items belonging to the DataLayer have 2 different structures:

#### Objects <a href="#h_01hxs2ba0mx1xx2dg9rcnyczxy" id="h_01hxs2ba0mx1xx2dg9rcnyczxy"></a>

It can be an item that looks like a JS object with an association of key / value.

```
{
    "event": "nameEvent",
    "eventCategory": "nameCategory",
    "eventAction": "nameAction",
     "gtm.uniqueEventId": 332
}
```

To retrieve one key / value association inside this payload, you have to select the object option. (for example, to search the eventCategory)

<img src="/files/SKejMm9pLJMRibBIEJBt" alt="" width="375">

#### “Arrays” (specific to GTM) <a href="#h_01hxs2ehqmhh1vthzz87zcdvwg" id="h_01hxs2ehqmhh1vthzz87zcdvwg"></a>

Or, we could have other items with a different structure. We call them array items (FYI, it’s not a real JS array). But, we have some items with this structure:

```
[
.....
13: Arguments(3) : ['event', 'nameEvent', { payload here}], 
....
]
```

Deeper with details

```
{
    "0": "event",
    "1": "abtasty",
    "2": {
        "abtasty_campaign": "[campaignId] campagnName",
        "abtasty_variation": "[variationId] variationName",
        "send_to": "XXX",
        "exp_variant_string": "value variant string"
    }
}
```

Here, we have different indexes. The index 0 is relative to one event (string), the second one to another string (name of the event) and the third (last) one to a payload. **This structure is not a structure by default.** You can have another structure (payload inside item 1 for example).\
To look for the **abtasty\_campaign** for example, you have to choose the array option and provide on which index we have to research.

<img src="/files/FcGUZYNKY61rRr8fvSoU" alt="" width="375">

We are not able to research a string inside an array (as a value). For example, searching for the index 1 with the value **abtasty** is not possible. The payload linked to the dedicated index has to be a JS object.

The custom tracker will be fired when a field, specified by you, matches a certain condition. These are the steps:

1. Giving the custom tracker variable a name. This name can be anything you choose and only aims at helping you recognize what you create.
2. The “key” is the name of the field you want to track in your DataLayer
3. The dropdown menu contains a list of operators that allows you to check for the state or value of the field in the DataLayer. The list of operators:

| **Operator**        | **Description**                                                                                                      |
| ------------------- | -------------------------------------------------------------------------------------------------------------------- |
| Equals              | Real key value = Target value                                                                                        |
| Does not equal      | Real key value ≠ Target value                                                                                        |
| Is lower than       | Real key value < Target value. This has to be a numerical value.                                                     |
| Is higher than      | Real key value > Target value. This has to be a numerical value.                                                     |
| Is included between | <p>Upper Target value > Real key value > Lower Target value.<br>These have to be numerical values.</p>               |
| Contains            | Example: the value “homeowner” contains “meow”. That will trigger the tracker. This is mainly for string data types. |
| Is true             | for a boolean value. value is `true`                                                                                 |
| Is false            | for a boolean value. value is `false`                                                                                |
| Is declared         | The key exists in the DataLayer. This does not need a value.                                                         |
| Is not declared     | The key does not exist in the DataLayer. This does not need a value                                                  |

4. Select the value of the key that would trigger the Custom Tracker:\
   Each time the value will match your rule, an event will be fired via our tag Datacollect.

An example is given in the image below:

<img src="/files/r4GIglYgdo17OgLnuCfU" alt="" width="375">

It means that, once saved, the custom tracker will be triggered when the DataLayer has a field “event” having a value of **exactly** “gtm.load”.\
The final steps are to Save the custom tracker, and reload your tag.

## Managing the dot (“.) inside a key <a href="#h_01hxs2j4xk9hbsg27t77zngrv1" id="h_01hxs2j4xk9hbsg27t77zngrv1"></a>

### Tracking the value of nested keys in your DataLayer <a href="#h_01hxs2j4xk0wfq1265p42v1mpd" id="h_01hxs2j4xk0wfq1265p42v1mpd"></a>

The DataLayer typically will not have a single level of key:value pairs. It may have nested keys, like in a typical object.\
In the previous section, the key:value pair was a first level pair.

<img src="/files/UdA4DaiYMi7gpJT03Ckm" alt="" width="140">

You may want to track a 2nd level or even 3rd level key:value pair, like in the image below:

<img src="/files/PnSwOVIUYASmLIAwOkID" alt="" width="179">

To track nested keys like “pagetype” or “type”, the key field in the custom tracker creation modal can be input with dots (.) as shown:

* page.pageType - this will have a value of “HOME”
* page.device.type - this will have a value of “mobile”

Arrays are not handled in the DataLayer for the moment, but only objects and subobjects.

<img src="/files/CF6IXNgCdCzF4wAcqs7h" alt="" width="375">

### Tracking a key with a dot inside <a href="#h_01hxs2jvzwf1hmmg6mcz4pm1r6" id="h_01hxs2jvzwf1hmmg6mcz4pm1r6"></a>

Instead of looking for the value of nested keys, we can have keys using dots (“.”).

For example:

```
{
    "gtm.start": 1714033074154,
    "event": "gtm.js",
    "gtm.uniqueEventId": 1
}
```

To search the gtm.uniqueEventId, you must use double quotes in order to specify that the key has a dot (“.”) and that you don’t want to track a value of a nested key.

<img src="/files/o9Facn28D9plEeed4vwF" alt="" width="375">

## Tracking a combination of key:value pairs <a href="#h_01hkyyxqnt73nppx01w32cy4t5" id="h_01hkyyxqnt73nppx01w32cy4t5"></a>

It is possible to have a custom tracker triggered by several conditions being true. Taking the last examples in the previous section, the custom tracker will be triggered if “page.pageType” is equal to “HOME” only.\
The Custom tracker can also be triggered by **BOTH** “page.pageType” = “Home” **AND** “page.device.type” = “mobile”. To do this, create a custom tracker and click on “Add condition”. This will show a second row of inputs to specify another condition.

<img src="/files/72xW3D8jU5Trrsae0ujK" alt="" width="375">

### Option Some <a href="#h_01hxs38gs3mv379k9xttdm5jb1" id="h_01hxs38gs3mv379k9xttdm5jb1"></a>

As soon as you have at least 2 conditions, a new dropdown appears. The first option is “**some**”. It means that at least 1 condition needs to be true for the tracker to be triggered. If both are true, it will also be triggered. If none are true, it will not be triggered.

### Option All <a href="#h_01hxs38gs3jcz51ab5wjrgsm1g" id="h_01hxs38gs3jcz51ab5wjrgsm1g"></a>

If you select “**all**”, it means that both (or all, in case there are more than 2) conditions need to be true for the tracker to be triggered. If only 1 is true, the tracker will not be triggered. If none are true, it will also not be triggered.

When selecting the “**all**” option, you can choose between two other options:

<img src="/files/ZoxVBMpWb78ArxKFW4PF" alt="" width="375">

#### In any Event of the Datalayer : (Only for GTM) <a href="#h_01hxs3ckpfq1akpv43ytp0s8zj" id="h_01hxs3ckpfq1akpv43ytp0s8zj"></a>

It means that you can check if there’s a matching of all the conditions in all the items of the DataLayer. For example, a DataLayer with this structure:

```
[
......
{
 event: "page_view"
 page: {
  pageType: "HOME"
}
},
..., 
{
event: "device"
page: {
 device: {
  type: "mobile"
}
}
}
]
```

In this example of a GTM DataLayer, we search these two conditions in all the DL. In this case, the matching is a success.

#### In the same event : (Only for GTM) <a href="#h_01hxs3ckpfmdh94ygsnrvfg0de" id="h_01hxs3ckpfmdh94ygsnrvfg0de"></a>

If you select this option (for the “**all**” option), all the conditions must be in the same event. If it’s not the case, we don’t parse the other items belonging to the DL (instead of the other option). That’s why in the previous example, the dataLayer must be like this:

```
[
....
{
page: 
{
 pageType: 'HOME'
device: 
{
type: "mobile"
  }
}
}
....
]
```

Once again, once the custom tracker is created and saved, do not forget **to refresh your tag.**

## Using the custom tracker <a href="#h_01hkyyxqnt51cq2anjjh3ahjj3" id="h_01hkyyxqnt51cq2anjjh3ahjj3"></a>

Once custom trackers are created, they can be used as goals for your reporting. To include them in your Reportings:

1. Go to the goal step of your campaign
2. Choose “Custom Trackers”
3. In the middle pane, you should see the custom tracker you created with the name you gave them (see first section)
4. You can then drag and drop them as primary or secondary goals from here.
5. Save.

<img src="/files/WIFNbHkrG0Ca2fURzVvZ" alt="" width="563">

The DataLayer is a global array which is displayed when one website is also loaded. When a user makes any action on the website, some new items could be pushed inside this array. That’s why the DataLayer could grow up. Once you’ve finished setting up one or several custom trackers on app2, we are able to detect if the association key:value is already inside the DataLayer or if it is inside a new pushed item.


---

# 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.abtasty.com/assets-library/trackers-page/how-to-create-a-custom-tracker-via-the-datalayer.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.
