简体   繁体   中英

Android : using google analytics v4 to track fragment?

How can we track fragment with google analytics v4 ?

Here is my application class:

public class MyApplication extends Application {
    private static final String PROPERTY_ID = "UA-XXXXXXX-1";
    public static int GENERAL_TRACKER = 0;

    public enum TrackerName {
        APP_TRACKER
    }

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

    public MyApplication() {
        super();
    }

    synchronized Tracker getTracker(TrackerName trackerId) {
        if (!mTrackers.containsKey(trackerId)) {
            GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
            Tracker t = analytics.newTracker(R.xml.app_tracker);
            mTrackers.put(trackerId, t);

        }
        return mTrackers.get(trackerId);
    }
}
Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(
    TrackerName.APP_TRACKER);
t.setScreenName("FRAGMENT_NAME");
t.send(new HitBuilders.AppViewBuilder().build());

This way should suit your needs, isn't it?

or go this way:

t.send(new HitBuilders.EventBuilder()
    .setCategory("Category")
    .setAction("Action_Id")
    .setLabel("Label")
    .build());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM