GoogleTagManager, Android. Events are sent only on app launch
-
23-12-2019 - |
Question
I need to implement GTM in my application. I did it by this doc and analytics working, but not as I expected. When I starting application, all events that I've pushed to dataLayer before, are sent to analytics, but not every time when I'm pushing some data to dataLayer. It's looks like I'm launching application, it sent one event to analytics and then nothing happens but the collecting data in dataLayer. When I'm launching app again, every collected data are sent to analytics, and then again data just collecting in dataLayer.
In my Application class I'm opening container
mTagManager.openContainer(mContainerId, new Callback() {
@Override
public void containerRefreshSuccess(Container container, RefreshType refreshType) {
}
@Override
public void containerRefreshFailure(
Container container,
RefreshType refreshType,
RefreshFailure refreshFailure) {
}
@Override
public void containerRefreshBegin(Container container, RefreshType refreshType) {
}
});
Than in my Activities I'm pushing data to dataLayer
TagManagager.getInstance(context).getDataLayer().push(map);
If look to the logcat I see this messages:
This after launch
02-27 18:35:55.468: V/TAG_MANAGER(14342): Sending hit to store PATH: https: PARAMS: ul=ru-ru, cd3=, cd4=, cd1=, ht=1393518945465, sr=1920x1032, cd2=, aid=my.app, cid=7087b6ed-b8e2-48b0-9979-c13399e119ff, av=1, v=1, t=appview, an=my.app, tid=UA-203518-14, _u=.4KnL, cd=Main,
02-27 18:35:55.515: V/TAG_MANAGER(14342): Dispatch running...
02-27 18:35:55.890: V/TAG_MANAGER(14342): sent 4 of 4 hits
And this after push data to dataLayer
02-27 18:40:59.679: V/TAG_MANAGER(14342): putHit called
02-27 18:40:59.679: V/TAG_MANAGER(14342): Sending hit to store PATH: https: PARAMS: ul=ru-ru, cd3=some text, cd4=, cd1=80025, ht=1393519259690, sr=1920x1032, cd2=, aid=my.app, cid=7087b6ed-b8e2-48b0-9979-c13399e119ff, av=1, v=1, t=appview, an=my.app, tid=UA-203518-14, _u=.67rs3333KnL, cd=Portal,
I even looked through the sample from Google, but their application cuteAnimals have the same behavior.
How can I sent data every time when I'm pushing data to dataLayer?
Solution
Sending of hits is doing by timer. And by default it is 30 minutes. So I had never waited so much time.
To change time you can call
// Set the dispatch period in seconds.
GAServiceManager.getInstance().setLocalDispatchPeriod(15);
To send hit immediately after pushing dataLayer you can call
GAServiceManager.getInstance().dispatchLocalHits();
Here my implementation:
public void pushDataLayer(Map<Object, Object> update) {
mTagManager.getDataLayer().push(update);
//in production we can use default logic with timer
GAServiceManager.getInstance().dispatchLocalHits();
// clear dataLayer
Map<Object, Object> empty = new HashMap<Object, Object>();
for (Map.Entry<Object, Object> values : update.entrySet()) {
empty.put(values.getKey(), DataLayer.OBJECT_NOT_PRESENT);
}
mTagManager.getDataLayer().push(empty);
}
For more information:
https://developers.google.com/analytics/devguides/collection/android/v3/dispatch