上一篇我們有提到要怎們申請 GA 專屬的ID
這篇我們要提到的事是要怎麼把 GA 埋在我們 APP內
提供我們用來分析客戶的偏好
首先, Gradle 新增
dependencies {
.
.
.
compile 'com.google.android.gms:play-services-analytics:8.4.0'
}
加入網路連線權限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
完成我們的 GA 工具
import android.content.Context;
import android.util.Log;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
public class GATools {
public final String trackingId = "UX-XXXXXXXX-X";
public final String TAG = "Hello";
/**
* 畫面監聽
* @param context
* @param screenName
*/
public static void screenListener(Context context, String screenName){
try {
Tracker tracker = GoogleAnalytics.
getInstance(context).newTracker(trackingId);
tracker.setScreenName(screenName);
tracker.send(new HitBuilders.ScreenViewBuilder().build());
} catch (Exception e) {
Log.d(TAG, "screenListener e :" + e.toString());
}
}
/**
* 事件監聽
* @param context
* @param category
* @param action
* @param label
*/
public static void eventListener(Context context, String category,
String action, String label){
try {
Tracker tracker = GoogleAnalytics.
getInstance(context).newTracker(trackingId);
tracker.send(new HitBuilders.EventBuilder()
.setCategory(category).setAction(action)
.setLabel(label).build());
} catch (Exception e) {
Log.d(TAG, "eventListener e :" + e.toString());
}
}
}
// Screen
GATools.screenListener(this, "MainActivity");
// Event
GATools.eventListener(this, "Main", "button", "clicked");
雖然會有數據在報表內
但是就即時性來講
還是要看者用者手機的狀態
參考資料 : AnalyticsReceiver is not registered or is disabled
參考資料 : How to track button click events in android analyitcs api v4
參考資料 : Google APIs for Android_Tracker
沒有留言:
張貼留言