Editor’s Note: This blog has been updated on June 06, 2017 as per the latest version of GTM and Android
At I/O 2013, Google added new features to the Google Tag Manager. They launched Google Tag Manager for Android and iOS devices. It is used in Mobile App along with Google Analytics to track various user behavior and help the marketers identify the trends, needs of users, app performance and other peculiar insights.
Google Tag Manager for mobile app developer devices also makes their development easy and dynamic. It enables the developers to change configuration values in their mobile applications using the tag manager interface, without having to rebuild and resubmit application binaries to the app marketplaces.
In order to walk through the implementation, you must be familiar with Android and iOS development to understand and implement tags for web analytics using Google Tag Manager.
Before starting implementing GTM, I recommend drafting a plan for tagging for google tag manager it will help to structure the core objective of data collection.
Steps to set up GTM in an Android Project (Click here or iOS)
- Create a Google Tag Manager Account
- Create a Mobile App container
- Note down the Container ID (It would be of the form of GTM-XXXXX)
- Add Google Play Services library to your project
NOTE: Check for latest updated version of Google Play services library when adding it in project. - Add the following permissions in your AndroidManifest.xml
- Also Add the Install Referrer Receiver in AndroidManifest.xml to track Campaign Data.
- Now, Create a Trigger with the following configuration in Google Tag Manager Container.
- Create a DataLayer Variable to fetch the value of screenName from the datalayer.
- Create a Tag to fire the Screen View from the app.
- Add Google Tag Manager’s Default Container to your Project.
- Within Google Tag Manager interface select VERSIONS -> ACTION -> DOWNLOAD this will download the container binary file.
- Within your Android project, create a raw folder in project-root / res path.
- Copy the downloaded binary file to the path project-root / res / raw with changing the filename to gtm_defaultcontainer_gtm_id.
- Within Google Tag Manager interface select VERSIONS -> ACTION -> DOWNLOAD this will download the container binary file.
Now, Let’s get started with initializing Tag Manager Instance and using DataLayer
- Create a ContainerHolderSingleton Class.
ContainerHolderSingleton class is used to have access to a single instance of TagManager Object at a time. - Create a class ContainerLoadedCallback Class.
ContainerLoadedCallback class is used to load the fresh GTM Container when it is available - Initialize Tag Manager in Application Class of your app (Recommended) or First Screen of the App (i.e. Splash Screen or Main Screen)
- Load the latest container by specifying the CONTAINER_ID
- Use ResultCallback to return the ContainerHolder once it has finished loading or has timed out for fetching the latest container
- After Initializing GTM well, it’s time to add the tracking code, which is implemented using the DataLayer Object.
DataLayer provides a layer of persistence that you use to store data in key-value pairs form and then send it to Google Analytics.
Create one method in general until class to push the openScreen event and start tracking the screen views from your app. You can call this method in onCreate()/onStart Method of your app activities with the screenName that you want to send.
With the above steps performed we have successfully implemented the Google Tag Manager in Android. To know that your implementation is working well, verify your data in Google Analytics Report.
Also please note that to track any kind of action through GTM, an event must be pushed in dataLayer and for the very same event, a Tag must be configured in GTM Interface.
So, that’s it. This is how you can integrate Google Tag Manager version 4 container in your Android app. Recently, Google rolled out version 5 container for the app
Now, the question is which version should you use in your app. Stay tuned for the benefits of the version 5 container over the version 4 container and steps to integrate it.
Till then HAPPY TAGGING!
29 Comments. Leave new
Hi Vatsal! Loved your article. Could you please help me out with the implementation? Did not get the part after “initializing tag manager instance and using DataLayer”. Also, could you please provide me with the raw code as this in an image format?
awesome post, I loved your post and your post is really helpful for me because I am a developer. and you want to know the about chatting apps that are related to opposite gender so you can visit my site because we talked about the how to talked about the some dating apps.
Great post! Thanks for sharing very powerful post, It’s very helpful. Keep it up.
thanks a lot for this tutorial. it worked like charm for me.
very useful tutorial. works without any problem. keep this coming.
useful tutorial
Thanks for reading!
Great blog! I would like to thank the efforts you have made in writing this post. I hope the same best work from you in the future
We are glad you liked the blog and grateful that look forward to more content from us. We promise to do our best and keep sharing the helpful content.
very useful tutorial
Hey Gagan! Thanks for the feedback. We are glad you found this blog resourceful
Great and an informative article!
Thanks for reading!
Great work! This blog is very useful. Hoping for same kind of work from you in the future.
Thanks for reading.
Thanks a lot for this tutorial. it worked like charm for me. Thanks.
Hey Matt! Thanks for letting us know! We are immensely happy that you found our blog resourceful. Subscribe to us for get more essential updates.
Good and useful Article
Thanks Bindu! We’re glad you found our article useful.
I was looking for a tutorial on this. Thanks so much! I
Really helpful guide on How to Implement Google Tag Manager. Also, thanks for sharing the detailed guide with images, it really helped a lot.
this tutorial is amazing I earn a lot of knowledge from this tutorial
Thanks Guri! We’re glad to help you.
Really useful and amazing tutorial. Thanks for sharing
Thanks Narya for the feedback.
Very useful guide for gtm.
Thanks Abhishek! We’re glad you found our article useful.
Thank you for your lessons, it’s very useful for the learner and also easy for them to follow each instruction.Once again much appreciated with that.
Thank you for your kind words
Thank you! Subscribe to our blog for further such posts!