Google解析多个Android活动

时间:2014-12-10 11:25:31

标签: android android-activity google-analytics google-play-services google-analytics-v4

我发布此问题是因为我无法找到明确的答案。 :)

我是Android新手,目前正在制作一款有多项活动的应用。我使用Google Play服务进行谷歌分析。通过谷歌开发指南,我设法让它工作。但每当我创建一个新的Activity(屏幕)时,我必须在每个活动类中重复相同的代码。

例如:-Activity1.java

public class Activity1 extends Activity {

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_activity1);
    setAnalytics();
}

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

synchronized Tracker getTracker(TrackerName trackerId) {
   if (!mTrackers.containsKey(trackerId)) {
      GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
      Tracker tracker = analytics.newTracker(R.xml.global_tracker);
      mTrackers.put(trackerId, tracker);
   }
   return mTrackers.get(trackerId);
}

private void setAnalytics(){
    // Get tracker.
    Tracker t = getTracker(TrackerName.APP_TRACKER);
    // Set screen name.
    //t.setScreenName("Activity 1");
    // Send a screen view.
    t.send(new HitBuilders.AppViewBuilder().build());
}
}

我的global_tracker.xml是

<!-- Enable automatic Activity measurement -->
<bool name="ga_autoActivityTracking">true</bool>

<!-- The screen names that will appear in reports -->
<screenName name="com.test.activity1">Activity1</screenName>
<screenName name="com.test.activity2">Activity2</screenName>

<!--  The following value should be replaced with correct property id. -->
<string name="ga_trackingId">UA-XXXXXXXX-1</string>

此设置工作正常。我的问题是&#34;这是最好的方法吗?&#34;。是否可以使用另一种方法来最小化每个Activity类中的代码?因为每次我都要重复相同的代码。是否可以创建一个公共类来保存这些公共代码集并将其用作每个Activity类中的对象?

任何想法的家伙?提前谢谢。

1 个答案:

答案 0 :(得分:0)

您应该将这些代码放在Application object中,而不是在每个活动中进行配置。

示例:

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

<强>参考:

请查看提供的示例应用程序。您可以在以下路径的android sdk目录中找到它:

<android-sdk-directory>/extras/google/google_play_services/analytics/mobileplayground

<强>文档

Google Analytics v4