How to implement google tag manager in android app?
@pietro
To implement Google Tag Manager in an Android app, follow these steps:
1 2 3 |
dependencies {
implementation 'com.google.android.gms:play-services-tagmanager:<latest_version>'
}
|
1
|
<uses-permission android:name="android.permission.INTERNET" /> |
1 2 3 4 |
<?xml version="1.0" encoding="utf-8"?>
<resource>
<string name="container_id">YOUR_CONTAINER_ID</string>
</resources>
|
Replace YOUR_CONTAINER_ID with the container ID you obtained from the Google Tag Manager website.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import com.google.android.gms.tagmanager.DataLayer;
import com.google.android.gms.tagmanager.TagManager;
public class MyApplication extends Application {
private TagManager tagManager;
private DataLayer dataLayer;
public void onCreate() {
super.onCreate();
tagManager = TagManager.getInstance(this);
tagManager.setVerboseLoggingEnabled(true);
dataLayer = tagManager.getDataLayer();
dataLayer.pushEvent("appLaunch", DataLayer.mapOf("event", "appLaunch"));
}
public TagManager getTagManager() {
return tagManager;
}
public DataLayer getDataLayer() {
return dataLayer;
}
}
|
Be sure to update the container ID and enable/disable verbose logging as needed.
1 2 3 4 5 |
<TextView
...
android:text="@string/container_id"
android:tag="GTM-YOUR_CONTAINER_ID"
... />
|
Replace YOUR_CONTAINER_ID with your container ID.
1
|
tagManager.setContainerLoadedListener(new ContainerLoadedCallback()); |
Note: It is recommended to thoroughly read through Google's official documentation for more detailed information on implementing and using Google Tag Manager in your Android app.