Index

  • 安裝 GooglePlayService 並且在 AndroidManifest.xml 開啟權限
  • res/xml/global_tracker.xml
  • extends Application
  • onStart(), onStop() 開啟記錄
  • initTracker()

安裝 GooglePlayService 並且在 AndroidManifest.xml 開啟權限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    ... >
  <!-- Google Analytics Version v4 needs this value for easy tracking -->
  <meta-data android:name="com.google.android.gms.analytics.globalConfigResource"
  android:resource="@xml/global_tracker" />

res/xml/global_tracker.xml

這裡我並沒有跟第一個教學連結裡一樣開兩個 xml, 而是直接用單一個 global_tracker

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="TypographyDashes">
    <string name="ga_trackingId">UA-XXXXXXXX-X</string>
    <integer name="ga_sessionTimeout">300</integer>
    <bool name="ga_autoActivityTracking">true</bool>
    <bool name="ga_reportUncaughtExceptions">true</bool>
    <!-- Set true to treat events as test events and don't send to google -->
    <bool name="ga_dryRun">false</bool>
</resources>

extends Application

因為暫時沒有需要另外的 tracker.xml, 所以我就把教學文件裡的 code 再修改一點點,反正多出來的也用不到~
請找出您 AndroidManifest.xml 引用的是哪個 Application (Android Bootstrap 的話是 .BootstrapApplication),並在其中加入以下 code。

// The following line should be changed to include the correct property id.
    private static final String PROPERTY_ID = "UA-XXXXXXXX-X";
    public enum TrackerName {
        APP_TRACKER // Tracker used only in this app.
    }
    HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();

    public 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)
                    : analytics.newTracker(R.xml.global_tracker);
            t.enableAdvertisingIdCollection(true);
            mTrackers.put(trackerId, t);
        }
        return mTrackers.get(trackerId);
    }

onStart(), onStop() 開啟記錄

請在您所有需要記錄的 activity 裡的 onStart(), onStop() 加入以下 code。
StackOverflow 上有提到,這是 Google 官方沒有提到的部份。

@Override
public void onStart() {
    super.onStart();
    GoogleAnalytics.getInstance(this).reportActivityStart(this);
}
 
@Override
public void onStop() {
    super.onStop();
    GoogleAnalytics.getInstance(this).reportActivityStop(this);
}

initTracker()

這就是設定 Tracker 的地方了,還有非常多可以客製化的部份,這裡只是簡單地記錄 ScreenName 而已~
請在您所有需要記錄的 activity 裡的 onResume 引用此函式。
如果是要放在 Fragment 請在您所有需要記錄的 Fragment 裡的 onResume 引用此函式。

public static void initTracker(Activity activity) {
        Tracker t = ((BootstrapApplication) activity.getApplication()).getTracker(
                BootstrapApplication.TrackerName.APP_TRACKER);
        t.setScreenName(activity.getClass().getSimpleName());
        t.send(new HitBuilders.AppViewBuilder().build());
    }
    
public static void initTracker(Activity activity, String fragmentName) {
        Tracker t = ((BootstrapApplication) activity.getApplication()).getTracker(
                BootstrapApplication.TrackerName.APP_TRACKER);
        t.setScreenName(activity.getClass().getSimpleName() + " - " + fragmentName);
        t.send(new HitBuilders.AppViewBuilder().build());
    }

Comments

comments powered by Disqus
Copyright © 2013 Andro Chen
Powered by Logdown and Greyshade
Favicon from The Noun Project