Help us understand the problem. What is going on with this article?

AndroidにGoogle Analytics v4を導入する

More than 3 years have passed since last update.

GoogleAnalytics v4をAndroidに導入してみました。

アプリの利用状況なんかがわかるのはすごくいいので、是非アプリ公開前にはGoogleAnalyticsを導入してみてはいかがでしょうか?
ではさっそく忘れない内に手順を書いてみようと思います。

まずGoogleAnalyticsに登録しましょう。
http://www.google.com/analytics/ に行き、アカウント作成します。

スクリーンショット 2015-02-27 11.32.27.png
ここでモバイルアプリを選択し、任意のアカウント名(複数作成できます)、アプリ名などを入力してトラッキングIDを取得します。

スクリーンショット 2015-02-27 11.36.35.png

トラッキングIDを取得したらGoogleAnalyticsの登録は以上です。
では、次にGoogleAnalyticsをアプリへ組み込んでいきます。

▶︎GooglePlayServicesを導入します。
File -> Project Structure -> Dependencies内に左下の+ボタンからLibrary Dependencyを選択し
GooglePlayServiceを導入します。
build.gradleに入っていればOKです。

▶︎Manifestへ以下を追記します。

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

applicationタグ内に

Manifest.xml
    <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>

を追加します。

▶︎res/xml内に以下のxmlファイルを作成します。

global_tracker.xml
          <resources>
              <!-- How long a session exists before giving up -->
              <integer name="ga_sessionTimeout">300</integer>
              <!-- Enable automatic Activity measurement -->
              <bool name="ga_autoActivityTracking">true</bool>
              <!-- The screen names that will appear in reports -->
              <screenName name=“com.example.activity.HogeActivity“>
              </screenName>
              <!--  The following value should be replaced with correct property id. -->
              <string name=“ga_trackingId”>UA-XXXXXXX-X</string>
           </resources>
ecommerce_tracker.xml
          <resources>
              <integer name="ga_sessionTimeout">60</integer>
              <!--  The following value should be replaced with correct property id. -->
              <string name="ga_trackingId">UA-XXXXXXX-X</string>
          </resources>

▶︎Applicationを継承したクラスを作成します。

App.java
    public class App extends Application {

    private static final String PROPERTY_ID = "UA-xxxxxxxx-x";
    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>();
    @Override
    public void onCreate() {
        super.onCreate();
    }
    public synchronized Tracker getTracker(TrackerName trackerId) {
        if (!mTrackers.containsKey(trackerId)) {

            GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
            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);
    }
    }

▶︎ActivityのonStart内で

MainActivity.java
      Tracker t = ((App)getApplication()).getTracker(App.TrackerName.APP_TRACKER);
      t.setScreenName(this.getClass().getSimpleName());
      t.send(new HitBuilders.AppViewBuilder().build());

以上でGoogleAnalyticsのレポートが取得できると思います。
無料なのでどんどん活用していきたいと思います。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした