2015-01-05 52 views
0

我們如何使用Google Analytics(分析)v4跟蹤片段?Android:使用谷歌分析v4跟蹤片段?

這裏是我的應用程序類:

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); 
    } 
} 

回答

0
Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(
    TrackerName.APP_TRACKER); 
t.setScreenName("FRAGMENT_NAME"); 
t.send(new HitBuilders.AppViewBuilder().build()); 

這種方式應該滿足您的需求,不是嗎?

或走這條路:

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