前回ImageViewでユーザーアイコンの表示を円形に表示させました。
今回はグロースハックのお友達Google アナリティクスです。これも5.0関係ないけど、今丁度僕が実装中なのでついでに記載。
SDK v4からはlibは不要となってgoogle play serviceを導入するだけで動きます。んー、便利!
色々と詰め込みすぎで個人的に不安ですが。。。
環境
- android:minSdkVersion="14"
- android:targetSdkVersion="21"
- andoid studio 0.8.9
import android.content.Context;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import android.util.Log;
public class AnalyticsManager {
private static Context sAppContext = null;
private static Tracker mTracker;
private final static String TAG = "AnalyticsManager";
public static synchronized void setTracker(Tracker tracker) {
mTracker = tracker;
}
private static boolean canSend() {
// We can only send Google Analytics when ALL the following conditions are true:
// 1. This module has been initialized.
// 2. The user has accepted the ToS.
// 3. Analytics is enabled in Settings.
return sAppContext != null && mTracker != null;
}
public static void sendScreenView(String screenName) {
if (canSend()) {
mTracker.setScreenName(screenName);
mTracker.send(new HitBuilders.AppViewBuilder().build());
if (BuildConfig.DEBUG) {
Log.d(TAG, "Screen View recorded: " + screenName);
}
} else {
if (BuildConfig.DEBUG) {
Log.d(TAG, "Screen View NOT recorded (analytics disabled or not ready).");
}
}
}
public static void sendEvent(String category, String action, String label, long value) {
if (canSend()) {
mTracker.send(new HitBuilders.EventBuilder()
.setCategory(category)
.setAction(action)
.setLabel(label)
.setValue(value)
.build());
if (BuildConfig.DEBUG) {
Log.d(TAG, "Event recorded:");
Log.d(TAG, "\tCategory: " + category);
Log.d(TAG, "\tAction: " + action);
Log.d(TAG, "\tLabel: " + label);
Log.d(TAG, "\tValue: " + value);
}
} else {
if (BuildConfig.DEBUG) {
Log.d(TAG, "Analytics event ignored (analytics disabled or not ready).");
}
}
}
public static void sendEvent(String category, String action, String label) {
sendEvent(category, action, label, 0);
}
public Tracker getTracker() {
return mTracker;
}
public static synchronized void initializeAnalyticsTracker(Context context) {
sAppContext = context;
if (mTracker == null) {
int useProfile;
if (BuildConfig.DEBUG) {
Log.d(TAG, "Analytics manager using DEBUG ANALYTICS PROFILE.");
useProfile = R.xml.analytics_debug;
} else {
useProfile = R.xml.analytics_release;
}
mTracker = GoogleAnalytics.getInstance(context).newTracker(useProfile);
}
}
}
あとはコード中で必要な箇所で呼び出すだけです
画面名
if (!BuildConfig.DEBUG) {
/* [ANALYTICS:SCREEN]
* TRIGGER: View the Login screen.
* LABEL: 'Login'
* [/ANALYTICS]
*/
AnalyticsManager.sendScreenView(SCREEN_LABEL);
}
activityやFragmentのonCreateで呼びます。
イベント
if (!BuildConfig.DEBUG) {
/* [ANALYTICS:EVENT]
* TRIGGER: button is pushed.
* CATEGORY: 'Show List Detail'
* ACTION: buttonListDetail
* LABEL: visible gone
* [/ANALYTICS]
* */
AnalyticsManager.sendEvent("Show List Detail", "buttonListDetail", label, 0L);
}
button押下時などのアクション時に呼びます。
公式サイトにはApplicationクラスに記述するのがお勧めと記載されていますが、実際はutilとしてAnalyticsManagerクラスをつくっているのがgoogleらしいですねw。 でもこちらの方が使いやすいし、汎用性もあります。debugとreleaseのxmlファイルでの分け方はマネすると便利です。
参考サイト
0 件のコメント:
コメントを投稿