Zadbaj o dobrą organizację dzięki kolekcji
Zapisuj i kategoryzuj treści zgodnie ze swoimi preferencjami.
klasa publiczna
DataLayer
Warstwa danych to mapa z ogólnymi informacjami o aplikacji.
Wykorzystuje standardowy zestaw kluczy, dzięki czemu może go odczytać każda osoba
który rozumie specyfikację. Stan warstwy danych jest aktualizowany przez interfejs API.
Aplikacja może na przykład zaczynać się od takiej tablicy dataLayer:
{
title: "Original screen title"
}
Gdy stan/dane aplikacji mogą się zmieniać, aplikacja może aktualizować dataLayer za pomocą takiego wywołania:
dataLayer.push(DataLayer.mapOf("title", "New screen title"));
Teraz warstwa danych zawiera:
{
title: "New screen title"
}
Po kolejnym wypychaniu:
dataLayer.push(DataLayer.mapOf("xyz", 3));
Obiekt zawiera:
{
"title": "New screen title",
"xyz": 3
}
Poniższy przykład pokazuje, jak działa scalanie tablic i map. Jeśli oryginał
dataLayer zawiera:
Wypchnięcia następuje synchronicznie. po przesłaniu zmiany zostały odzwierciedlone w modelu.
Po przekazaniu klucza event do warstwy danych oceniane są reguły tagów i wszystkie
uruchomią się tagi pasujące do tego zdarzenia.
Przykład: dla kontenera z tagiem, którego regułą uruchamiania jest „zdarzenie” jest równe
„openScreen” po tym przekazaniu:
Scala dany obiekt update z istniejącym modelem danych, wywołując
wszystkich detektorów z aktualizacją (po scaleniu).
Jeśli chcesz przedstawić brakującą wartość (np. pusty indeks na liście),
użyj obiektu OBJECT_NOT_PRESENT.
Jeśli inny wątek wykonuje push, to wywołanie blokuje się do czasu, aż ten wątek
.
Zazwyczaj jest to wywołanie synchroniczne. Jeśli jednak podczas wykonywania wątku
wypchnięcie następuje po kolejnym wypchnięciu z tego samego wątku, po czym drugie wypchnięcie jest asynchroniczne.
(drugie wypchnięcie powróci, zanim zmiany zostaną wprowadzone w warstwie danych). Sekunda
może zostać wypchnięte z tego samego wątku. Jeśli na przykład w odpowiedzi następuje przekazanie warstwy danych
do uruchomienia tagu.
Jeśli update zawiera klucz event, reguły zostaną ocenione i
pasujące tagi.
Parametry
update
aktualizowany obiekt, który ma zostać przetworzony
publiczne
nieważne
.
push(klucz obiektu, wartość obiektu)
Przekazuje parę klucz-wartość danych do warstwy danych. Jest to jedynie udogodnienie
który dzwoni pod numer push(DataLayer.mapOf(key, value)).
[[["Łatwo zrozumieć","easyToUnderstand","thumb-up"],["Rozwiązało to mój problem","solvedMyProblem","thumb-up"],["Inne","otherUp","thumb-up"]],[["Brak potrzebnych mi informacji","missingTheInformationINeed","thumb-down"],["Zbyt skomplikowane / zbyt wiele czynności do wykonania","tooComplicatedTooManySteps","thumb-down"],["Nieaktualne treści","outOfDate","thumb-down"],["Problem z tłumaczeniem","translationIssue","thumb-down"],["Problem z przykładami/kodem","samplesCodeIssue","thumb-down"],["Inne","otherDown","thumb-down"]],["Ostatnia aktualizacja: 2025-07-25 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))`."]]