Stay organized with collections
Save and categorize content based on your preferences.
public class
DataLayer
The data layer is a map holding generic information about the application.
It uses a standard set of keys so it can be read by any party
that understands the specification. The data layer state is updated through its API.
For example, an app might start with the following dataLayer:
{
title: "Original screen title"
}
As the state/data of an app can change, the app can update the dataLayer with a call such as:
dataLayer.push(DataLayer.mapOf("title", "New screen title"));
Now the data layer contains:
{
title: "New screen title"
}
After another push happens:
dataLayer.push(DataLayer.mapOf("xyz", 3));
The dataLayer contains:
{
"title": "New screen title",
"xyz": 3
}
The following example demonstrates how array and map merging works. If the original
dataLayer contains:
Pushes happen synchronously; after the push, changes have been reflected in the model.
When an event key is pushed to the data layer, rules for tags are evaluated and any
tags matching this event will fire.
For example, given a container with a tag whose firing rule is that "event" is equal to
"openScreen", after this push:
Values of this type used in a List causes the List to be sparse when merging; it's as if
there were no element at that index.
Public Methods
public
Object
get(String key)
Returns the object in the model associated with the given key. If the key is not found,
null is returned.
The key can can have embedded periods. For example:
a key of "a.b.c" returns a map with key "c" in a map
with key "b" in a map with key "a" in the model.
public
static
List<Object>
listOf(Object... objects)
Utility method that creates a list.
For example, the following creates a list containing "object1" and
"object2":
List<Object> list = DataLayer.listOf("object1", "object2");
public
static
Map<Object, Object>
mapOf(Object... objects)
Utility method that creates a map. The parameters should be pairs of key
values.
For example, the following creates a map mapping "key1" to "value1"
and "key2" to "value2":
Merges the given update object into the existing data model, calling
any listeners with the update (after the merge occurs).
If you want to represent a missing value (like an empty index in a List),
use the OBJECT_NOT_PRESENT object.
If another thread is executing a push, this call blocks until that thread is
finished.
This is normally a synchronous call. However, if, while the thread is executing
the push, another push happens from the same thread, then that second push is asynchronous
(the second push will return before changes have been made to the data layer). This second
push from the same thread can occur, for example, if a data layer push is made in response
to a tag firing.
If the update contains the key event, rules will be evaluated and
matching tags will fire.
Parameters
update
the update object to process
public
void
push(Object key, Object value)
Pushes a key/value pair of data to the data layer. This is just a convenience method
that calls push(DataLayer.mapOf(key, value)).
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-10-09 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eDataLayer\u003c/code\u003e is a map storing application data using a standard set of keys, updated through its API using \u003ccode\u003epush()\u003c/code\u003e methods.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eDataLayer\u003c/code\u003e supports merging of arrays and maps, with \u003ccode\u003eOBJECT_NOT_PRESENT\u003c/code\u003e allowing for sparse lists.\u003c/p\u003e\n"],["\u003cp\u003ePushing an \u003ccode\u003eevent\u003c/code\u003e key to the \u003ccode\u003eDataLayer\u003c/code\u003e triggers the evaluation of rules and fires matching tags.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eget()\u003c/code\u003e method retrieves data associated with a key, supporting embedded periods for nested access.\u003c/p\u003e\n"],["\u003cp\u003eUtility methods \u003ccode\u003elistOf()\u003c/code\u003e and \u003ccode\u003emapOf()\u003c/code\u003e facilitate the creation of lists and maps, respectively, for use with the \u003ccode\u003eDataLayer\u003c/code\u003e.\u003c/p\u003e\n"]]],["The `DataLayer` class manages a map of application data, accessible via standard keys. Data is updated synchronously using `push()`, which merges new data into the existing layer. `push()` accepts either a map or a key-value pair. `mapOf()` and `listOf()` create maps and lists respectively. The `OBJECT_NOT_PRESENT` value denotes missing list elements. Pushing an `event` key triggers tag evaluations. `get()` retrieves data by key. Multiple pushes from the same thread result in asynchronous behavior for the second push.\n"],null,["# DataLayer\n\npublic class **DataLayer** \nThe data layer is a map holding generic information about the application.\nIt uses a standard set of keys so it can be read by any party\nthat understands the specification. The data layer state is updated through its API.\nFor example, an app might start with the following dataLayer: \n\n```\n {\n title: \"Original screen title\"\n }\n```\nAs the state/data of an app can change, the app can update the dataLayer with a call such as: \n\n```\n dataLayer.push(DataLayer.mapOf(\"title\", \"New screen title\"));\n```\nNow the data layer contains: \n\n```\n {\n title: \"New screen title\"\n }\n```\nAfter another push happens: \n\n```\n dataLayer.push(DataLayer.mapOf(\"xyz\", 3));\n```\nThe dataLayer contains: \n\n```\n {\n \"title\": \"New screen title\",\n \"xyz\": 3\n }\n```\nThe following example demonstrates how array and map merging works. If the original dataLayer contains: \n\n```\n {\n \"items\": [\"item1\", null, \"item2\", {\"a\": \"aValue\", \"b\": \"bValue\"}]\n }\n```\nAfter this push happens: \n\n```\n dataLayer.push(\"items\", DataLayer.listOf(null, \"item6\", DataLayer.OBJECT_NOT_PRESENT,\n DataLayer.mapOf(\"a\", null)));\n```\nThe dataLayer contains: \n\n```\n {\n \"items\": [null, \"item6\", \"item2\", {\"a\": null, \"b\": \"bValue\"}]\n }\n```\n\nPushes happen synchronously; after the push, changes have been reflected in the model.\n\n\nWhen an `event` key is pushed to the data layer, rules for tags are evaluated and any\ntags matching this event will fire.\nFor example, given a container with a tag whose firing rule is that \"event\" is equal to\n\"openScreen\", after this push: \n\n```\n dataLayer.push(\"event\", \"openScreen\");\n```\nthat tag will fire.\n\n\u003cbr /\u003e\n\n### Field Summary\n\n|----------------------------|----------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------|\n| public static final Object | [OBJECT_NOT_PRESENT](../../../com/google/tagmanager/DataLayer.html#OBJECT_NOT_PRESENT) | Values of this type used in a List causes the List to be sparse when merging; it's as if there were no element at that index. |\n\n### Public Method Summary\n\n|------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------|\n| Object | [get](../../../com/google/tagmanager/DataLayer.html#get(java.lang.String))(String *key*) |\n| static List\\\u003cObject\\\u003e | [listOf](../../../com/google/tagmanager/DataLayer.html#listOf(java.lang.Object...))(Object... *objects*) |\n| static Map\\\u003cObject, Object\\\u003e | [mapOf](../../../com/google/tagmanager/DataLayer.html#mapOf(java.lang.Object...))(Object... *objects*) |\n| void | [push](../../../com/google/tagmanager/DataLayer.html#push(java.util.Map\u003cjava.lang.Object, java.lang.Object\u003e))(Map\\\u003cObject, Object\\\u003e *update*) |\n| void | [push](../../../com/google/tagmanager/DataLayer.html#push(java.lang.Object, java.lang.Object))(Object *key*, Object *value*) |\n\nFields\n------\n\n#### public static final Object\n**OBJECT_NOT_PRESENT**\n\nValues of this type used in a List causes the List to be sparse when merging; it's as if\nthere were no element at that index.\n\nPublic Methods\n--------------\n\n#### public Object\n**get**\n(String *key*)\n\nReturns the object in the model associated with the given key. If the key is not found,\n`null` is returned.\n\nThe key can can have embedded periods. For example:\na key of `\"a.b.c\"` returns a map with key `\"c\"` in a map\nwith key `\"b\"` in a map with key `\"a\"` in the model. \n\n#### public static List\\\u003cObject\\\u003e\n**listOf**\n(Object... *objects*)\n\nUtility method that creates a list.\n\n\nFor example, the following creates a list containing `\"object1\"` and\n`\"object2\"`: \n\n```\n List\u003cObject\u003e list = DataLayer.listOf(\"object1\", \"object2\");\n \n```\n\n\u003cbr /\u003e\n\n#### public static Map\\\u003cObject, Object\\\u003e\n**mapOf**\n(Object... *objects*)\n\nUtility method that creates a map. The parameters should be pairs of key\nvalues.\n\n\nFor example, the following creates a map mapping `\"key1\"` to `\"value1\"`\nand `\"key2\"` to `\"value2\"`: \n\n```\n Map\u003cObject, Object\u003e map = DataLayer.mapOf(\"key1\", \"value1\", \"key2\", \"value2\");\n \n```\n\n\u003cbr /\u003e\n\n##### Throws\n\n|--------------------------|------------------------------------------|\n| IllegalArgumentException | if there are an odd number of parameters |\n\n#### public void\n**push**\n(Map\\\u003cObject, Object\\\u003e *update*)\n\nMerges the given `update` object into the existing data model, calling\nany listeners with the update (after the merge occurs).\n\nIf you want to represent a missing value (like an empty index in a List),\nuse the `OBJECT_NOT_PRESENT` object.\n\nIf another thread is executing a push, this call blocks until that thread is\nfinished.\n\nThis is normally a synchronous call. However, if, while the thread is executing\nthe push, another push happens from the same thread, then that second push is asynchronous\n(the second push will return before changes have been made to the data layer). This second\npush from the same thread can occur, for example, if a data layer push is made in response\nto a tag firing.\n\nIf the `update` contains the key `event`, rules will be evaluated and\nmatching tags will fire. \n\n##### Parameters\n\n|--------|------------------------------|\n| update | the update object to process |\n\n#### public void\n**push**\n(Object *key*, Object *value*)\n\nPushes a key/value pair of data to the data layer. This is just a convenience method\nthat calls `push(DataLayer.mapOf(key, value))`."]]