How to implement Google TagManager in Android app

I want to implement Google Tag Manager and Google Analytics in my android app.

I am completely new to analytics and google tagmanager. I am following this link link1 , link2 thanks to this I got some general understanding of Google Tag Manager and Google Analytics.

What I did:
I created a Google TagManager and Google Analytics account. Created all basic steps in TagManager and Analytics account. Also I added libGoogleAnalyticsServices this file for the lib project and gtm container in the project file resource.

Now I need. What are all the codes I need to add to my application? And where do I need to add this. I created one seperate java file in src and I added this code

import com.google.android.gms.analytics.Logger;
import com.google.android.gms.analytics.Tracker;
import com.google.android.gms.analytics.GoogleAnalytics;

import android.app.Application;

import java.util.HashMap;

public class AnalyticsSampleApp extends Application {

// The following line should be changed to include the correct property id.
private static final String PROPERTY_ID = "UA-XXXXX-Y";

public static int GENERAL_TRACKER = 0;

public enum TrackerName {
    APP_TRACKER, // Tracker used only in this app.
    GLOBAL_TRACKER, // Tracker used by all the apps from a company. eg: roll-up tracking.
    ECOMMERCE_TRACKER, // Tracker used by all ecommerce transactions from a company.
}

HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();

public AnalyticsSampleApp() {
    super();
}

synchronized Tracker getTracker(TrackerName trackerId) {
    if (!mTrackers.containsKey(trackerId)) {

        GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
        analytics.getLogger().setLogLevel(Logger.LogLevel.VERBOSE);
        Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID)
                : (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(
                        R.xml.global_tracker)
                        : analytics.newTracker(R.xml.ecommerce_tracker);
        t.enableAdvertisingIdCollection(true);
        mTrackers.put(trackerId, t);
    }
    return mTrackers.get(trackerId);
}
}

      

Just tell me what remaining codes I need to add and what I need to change in this. If my question is not clear please see below. Let me clarify this again.

Many thanks.

+3


source to share


1 answer


My advice is to only use GTM, and from your GTM account, you can link Google Analytics services and other services that might help you.

Here is a good guide to help you.

In a few words, you need:

  • Include Google Play Services as a library for your project.
  • Add a default container to your app under the res / row folder. This container you will be able to export it from GTM account after setting up macros and tags
  • Add these lines to your app manifest file:

    <meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    
          

  • Open a container on the splash screen (like Splash screen)

    TagManager tagManager = TagManager.getInstance(this);
        tagManager.setVerboseLoggingEnabled(true);
    PendingResult<ContainerHolder> pending =
            tagManager.loadContainerPreferNonDefault(CONTAINER_ID,
            R.raw.default_container);
    pending.setResultCallback(new ResultCallback<ContainerHolder>() {
        @Override
        public void onResult(ContainerHolder containerHolder) {
            ContainerHolderSingleton.setContainerHolder(containerHolder);
            containerLoaded = true;
            startMainActivity();
        }
    }, 2, TimeUnit.SECONDS);
    
          

  • create class ContainerHolderSingleton

    public class ContainerHolderSingleton {
    private static ContainerHolder containerHolder;
    
    private ContainerHolderSingleton() {
    }
    
    public static ContainerHolder getContainerHolder() {
        return containerHolder;
    }
    
    public static void setContainerHolder(ContainerHolder c) {
        containerHolder = c;
    }
    
          

    }

    1. create another class GtmUtils

public class GtmUtils {

private GtmUtils() {
}

public static void pushOpenScreenEvent(Context context, String screenName) {
    DataLayer dataLayer = TagManager.getInstance(context).getDataLayer();
    dataLayer.pushEvent("OpenScreen", DataLayer.mapOf("screenName", screenName));
}

      



}

  1. And finally, from whatever activity / fragment you want to send, you can do the following:

GtmUtils.pushOpenScreenEvent (this, "login");

What is it. You just need to set up your GTM account and link it to Analytics, etc.

Also you can find the official documentation and some samples here.

Home it will help.

+4


source







All Articles