This is the mobile implementation of Google Tag Manager (GTM). Sample usage:
Container container = TagManager.getInstance(context).openContainer(myContainerId); String value = container.getString("myKey"); DataLayer dataLayer = TagManager.getInstance(context).getDataLayer(); dataLayer.push(DataLayer.mapOf("event", "openScreen", "screenName", "Main Page"));
The Container
class provides methods for retrieving values given a key.
The routines getBoolean(String)
, getDouble(String)
,
getLong(String)
, getString(String)
return the current
value for the key of a value collection macro,
depending on the rules associated with the container.
As an example, if your container has a value collection macro with a key speed
whose
value is 32, and the enabling rule is Language
is "en"; and another value collection
macro with a key speed
whose value is 45, and the enabling rule is
Language
is not "en", then making the following call:
container.getLong("speed")
The data layer is a map holding generic information about the application.
The DataLayer
class provides methods to push and retrieve data from the data layer.
Pushing an event
key to the data layer will cause tags that match this event
to fire.
An initial version of the container is bundled with the application. It should be
placed as an asset with name tagmanager/containerId where containerId is the same
container ID you will use within this API. When you call openContainer(String, Container.Callback)
, the container
will be returned with those bundled rules/macros.
You will create the container in the UI and use the Download button to download it.
You can modify the container in the UI and publish a new version. In that case, the next time the mobile app refreshes the container from the network (currently every 12 hours), it will get that new version. When you call one of the get... routines, the value will be computed using the most recent rules.
The downloaded container is saved locally; when you call openContainer(String, Container.Callback)
, it will first
load the default container, and will then asynchronously load any saved container. If none is
found, or if it is older than 12 hours, it will try to retrieve a newer version from the network.
You can find the status of those asynchronous loads by passing a
Container.Callback
to openContainer(String, Container.Callback)
.
Sometimes you may want to block until either a non-default container is available, or until a
recent fresh container is available. ContainerOpener
is a utility class that can help.
When you are finished with a container, call close()
.
Nested Class Summary
interface | TagManager.Logger | A simple interface for error/warning/info/debug/verbose logging. | |
enum | TagManager.RefreshMode | Mode for refreshing the container. |
Public Method Summary
Container |
getContainer(String containerId)
|
Context | |
DataLayer | |
static TagManager |
getInstance(Context context)
|
Logger | |
TagManager.RefreshMode | |
Container | |
void | |
void |
Public Methods
public Container getContainer (String containerId)
Returns the opened container associated with the containerId
;
returns null
if the container is not already open.
public Context getContext ()
Returns the saved context associated with this object.
public static TagManager getInstance (Context context)
Get the singleton instance of the TagManager
class, creating it if necessary.
public Container openContainer (String containerId, Container.Callback callback)
Returns a container. Callback will be called as various things happen for the container. At a minimum, openContainer will attempt to load a saved version of the container. If there is no saved version, or if the saved version is out of date, attempt will be made to load from the network.
Usually, the returned container will be empty, but the loading will happen on a separate thread, so the returned container may be refreshed before it is returned, after it is returned, or may never be refreshed (if, for example, there is no network connection during the lifetime of the container).
If you call openContainer
a second time for a given containerId
, an
exception will be thrown.
Parameters
containerId | the ID of the container to open |
callback | an object whose various methods will be called during parts of the loading
process. Note that the methods may be called from different threads. In addition, they may
be called before openContainer returns.
|
public void setRefreshMode (TagManager.RefreshMode mode)
Sets the refresh mode used for all containers.