In this post, we will cover how to get started with GTM for mobile apps and how to implement Universal Analytics tags using the GTM SDK for Android. As a heads up, this will occasionally get pretty technical, however I believe it is important to understand the product from its fundamentals.
Initial Set Up
- Downloading and adding the GTM library to our app project
- Ensuring our app can access the internet and the network state
- Adding a Default container to our app project
Create an App Container
Opening a Container
ContainerOpener.openContainer(
mTagManager, // TagManager instance.
“GTM-XXXX”, // Tag Manager Container ID.
OpenType.PREFER_NON_DEFAULT, // Prefer not to get the default container, but stale is OK.
null, // Timeout period. Default is 2000ms.
new ContainerOpener.Notifier() { // Called when container loads.
@Override
public void containerAvailable(Container container) {
// Handle assignment in callback to avoid blocking main thread.
mContainer = container;
}
}
);
- Container from network: Container with the most recent tags and configurations as currently published in the GTM interface
- Saved container: Container saved locally on the device
- Fresh vs. Stale container Saved container that is less vs. greater than 12 hours old
- Default container: Container file with default configuration values manually added to the app project prior to shipping
We may change the open type from PREFER_NON_DEFAULT to PREFER_FRESH, which means Google Tag Manager will try to retrieve a Fresh container either from the network or disk. The main difference is hence that a Stale container will not be used if we implement PREFER_FRESH unless no other container is available or the timeout period is exceeded. We may also adjust the timeout period for both PREFER_NON_DEFAULT and PREFER_FRESH, however we should think carefully about whether longer request times negatively affects the user experience before doing so.
Tag Example: Universal Analytics Tags
Step 1: Push Values to the DataLayer Map
public class ExampleActivity extends Activity {
private static final String SCREEN_NAME = "example screen";
private DataLayer mDataLayer;
public void onStart() {
super.onStart();
mDataLayer = TagManager.getInstance(this).getDataLayer();
mDataLayer.push(DataLayer.mapOf("event", "screenVisible",
"screenName", SCREEN_NAME));
}
//..the rest of our activity code
}
Note: the container must be open by the time we push values into the DataLayer or GTM will not be able to evaluate them.
Step 2: Set Up Macros In Google Tag Manager
Step 3: Configure an App View Tag
Step 4: Configure a Firing Rule For Our Tag
Step 5: Save and Publish
Default Containers
Let’s back up a little bit. In the GTM world, tag creation, configuration, settings, etc. is primarily handled in the web-based GTM interface. The power of this is obvious: we no longer need to rely on our development teams to push code for every change we want to make. Instead, we make changes in the GTM interface, publish them, and our tags and values are updated accordingly for our user base. This of course relies on the ability of our websites or applications to reach the GTM servers so that the updates can take effect. Here things get a bit more tricky for mobile apps, which partly live offline, than for websites.
Summary
- After completing some initial configuration steps, we created a new app container in the web-based GTM interface
- We figured out how to open our container as users launch our app, choosing the most suitable opening type and timeout value (taking into consideration user experience and performance)
- We then implemented code to push an event to the Data Layer as various screens become visible to our users, setting up a Universal Analytics App View tag in GTM to fire every time this happens
- We downloaded the binary file of our container and added it to our app project to be used as a Default container
- Lastly, we created and published our container in GTM
No comments:
Post a Comment