DataLayer | Android Developers

Quicknav Quicknav
Links

- Google Play Developer Console
- Android Developers Blog
- About Android

Android Sites

- Android.com
- Android Developers
- Android Open Source Project

Language
close

- - About
  - Wear
  - TV
  - Auto
- - Get Started
  - Devices
  - Style
  - Patterns
  - Building Blocks
  - Downloads
  - Videos
- - Training
  - API Guides
  - Reference
  - Tools
  - Google Services
  - Samples
- - Google Play
  - Essentials
  - Get Users
  - Engage & Retain
  - Monetize
  - Tools & Reference
  - Developer Stories

## Results

Loading...
Summary: Constants | Fields | Methods | Inherited Methods | [Expand All]
public class

# DataLayer

extends Object

java.lang.Object
↳ | com.google.android.gms.tagmanager.DataLayer

## Class Overview

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:

       {
         "items": ["item1", null, "item2", {"a": "aValue", "b": "bValue"}]
       }

After this push happens:

     dataLayer.push("items", DataLayer.listOf(null, "item6", DataLayer.OBJECT_NOT_PRESENT,
         DataLayer.mapOf("a", null)));

The dataLayer contains:

       {
         "items": [null, "item6", "item2", {"a": null, "b": "bValue"}]
       }

Pushes happen synchronously; after the push, changes have been reflected in the model.

When an event 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:

     dataLayer.pushEvent("openScreen", DataLayer.mapOf());

that tag will fire.

## Summary

Constants
String | EVENT_KEY | If a map is pushed containing this key, it's treated as an event, and tags are evaluated.

Fields
public static final Object | 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.

Public Methods
Object | get(String key) Returns the object in the model associated with the given key.
static List<Object> | listOf(Object... objects) Utility method that creates a list.
static Map<String, Object> | mapOf(Object... objects) Utility method that creates a map.
void | push(Map<String, Object> update) Merges the given `update` object into the existing data model, calling any listeners with the update (after the merge occurs).
void | push(String key, Object value) Pushes a key/value pair of data to the data layer.
void | pushEvent(String eventName, Map<String, Object> update) Pushes an event, along with an update map, to the data layer.

[Expand] Inherited Methods
From class java.lang.Object
Object | clone()
boolean | equals(Object arg0)
void | finalize()
final Class<?> | getClass()
int | hashCode()
final void | notify()
final void | notifyAll()
String | toString()
final void | wait()
final void | wait(long arg0, int arg1)
final void | wait(long arg0)

## Constants

#### public static final String EVENT_KEY

If a map is pushed containing this key, it's treated as an event, and tags are evaluated.

Constant Value: "event"

## Fields

#### public static final Object 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.

## 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<String, 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"`:

     Map<String, Object> map = DataLayer.mapOf("key1", "value1",
             "key2", "value2");

##### Throws

IllegalArgumentException | if there are an odd number of parameters or a key is not a string

#### public void push (Map<String, Object> update)

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 (String 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))`.

A key with value `event` will cause rules to be evaluated and matching tags to be fired.

#### public void pushEvent (String eventName, Map<String, Object> update)

Pushes an event, along with an update map, to the data layer.

This is just a convenience method that pushes a map containing a key `event` whose value is `eventName` along with the contents of `update` via `push(Map)`.