Android Google Analytics(分析)集成教程

This tutorial is about google analytics android integration.

本教程是關於Google Analytics(分析)Android集成的。

Google Analytics will help you to track how many people are using your app, real time users, etc.

Google Analytics(分析)將幫助您跟蹤有多少人在使用您的應用,實時用戶等。

The tutorial is divided into three parts, first create an account in Google Analytics, then generate configuration file and finally implement google analytics in your app.

本教程分爲三個部分,首先是在Google Analytics(分析)中創建一個帳戶,然後生成配置文件,最後在您的應用中實現google Analytics(分析)。

Android Google Analytics(分析)集成教程 (Android Google Analytics Integration Tutorial)

1.創建Google Analytics(分析)帳戶 (1. Create Google Analytics Account)

Go to https://analytics.google.com and login with your gmail credentials.

轉到https://analytics.google.com並使用您的Gmail憑據登錄。

There select Mobile app type and add all details like Account Name, App Name, etc according to you. I have shown one example in below screenshot.

在此處選擇「 移動應用程序類型」,然後根據您的需要添加所有詳細信息,例如「 帳戶名稱」 ,「 應用程序名稱 」等。 我在下面的屏幕快照中顯示了一個示例。

Android Google Analytics Integration Tutorial 1

Now click on Get Tracking ID button and then accept the terms to create the account. After creating the account you will see a screen as shown below. Here you can see the Tracking ID, just save it somewhere as we will need it later.

現在,單擊「 獲取跟蹤ID」按鈕,然後接受條款創建帳戶。 創建帳戶後,您將看到如下所示的屏幕。 在這裏您可以看到Tracking ID,只需將其保存在以後需要的地方即可。

Android Google Analytics Integration Tutorial 2

2.生成google-services.json配置文件 (2. Generate google-services.json Configuration File)

1. Go to https://developers.google.com/mobile/add. Make sure you are logged in with same gmail account with which you created analytics account.

1.轉到https://developers.google.com/mobile/add 。 確保您使用與創建分析帳戶相同的Gmail帳戶登錄。

2. Click on Pick a platform and choose Android App. Give any app name and enter the package name of your android app in which you want to integrate analytics. See below screenshot as an example.

2.單擊選擇平臺,然後選擇Android應用程序 。 輸入任何應用程序名稱,然後輸入要在其中集成分析的android應用程序的程序包名稱。 請參見下面的屏幕截圖作爲示例。

Android Google Analytics Integration Tutorial 3

Click on Choose and configure services to continue.

單擊選擇並配置服務以繼續。

3. Then select Analytics as services, see below screenshot.

3.然後選擇Google Analytics(分析)作爲服務,請參見以下屏幕截圖。

Android Google Analytics Integration Tutorial 4

4. Now select the google analytics account that you created earlier. As I created it with name Google Analytics so I selected it, see below screenshot.

4.現在,選擇您之前創建的Google Analytics(分析)帳戶。 當我使用Google Analytics(分析)名稱創建它時,我選擇了它,請參見下面的屏幕截圖。

Android Google Analytics Integration Tutorial 5

After that click on Enable Analytics Service and then click on Generate configuration files button.

之後,單擊「 啓用Analytics Service」 ,然後單擊「 生成配置文件」按鈕。

5. In the next window click on Download button to download google-services.json file.

5.在下一個窗口中,單擊下載按鈕以下載google-services.json文件。

Android Google Analytics Integration Tutorial 6

3. Android應用集成 (3. Android App Integration)

Here I have created a demo android project with name GoogleAnalytics as an example.

在這裏,我以名稱GoogleAnalytics爲例創建了一個演示android項目。

1. Paste the google-services.json configuration file inside app folder of your project, see below screenshot.

1.將google-services.json配置文件粘貼到您項目的app文件夾中,請參見下面的屏幕截圖。

Android Google Analytics Integration Tutorial 7

2. Add following dependency in project level build.gradle file.

2.在項目級別的build.gradle文件中添加以下依賴

1
classpath 'com.google.gms:google-services:3.0.0'
1
classpath 'com.google.gms:google-services:3.0.0'

3. Add another dependency in app level build.gradle file. After that sync the project.

3.在應用程序級別的build.gradle文件中添加另一個依賴 。 之後,同步項目。

1
compile 'com.google.android.gms:play-services-analytics:10.2.4'
1
compile 'com.google.android.gms:play-services-analytics:10.2.4'

4. Create AnalyticsApplication.java file inside your project package and add following code in it.

4.在項目包內創建AnalyticsApplication.java文件,並在其中添加以下代碼。

AnalyticsApplication.java

AnalyticsApplication.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.googleanalytics;
import android.app.Application;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
public class AnalyticsApplication extends Application {
    private static GoogleAnalytics sAnalytics;
    private static Tracker sTracker;
    synchronized public Tracker getDefaultTracker() {
        sAnalytics = GoogleAnalytics.getInstance(this);
        if (sTracker == null) {
            sTracker = sAnalytics.newTracker(R.xml.global_tracker);
        }
        return sTracker;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com . googleanalytics ;
import android . app . Application ;
import com . google . android . gms . analytics . GoogleAnalytics ;
import com . google . android . gms . analytics . Tracker ;
public class AnalyticsApplication extends Application {
     private static GoogleAnalytics sAnalytics ;
     private static Tracker sTracker ;
     synchronized public Tracker getDefaultTracker ( ) {
         sAnalytics = GoogleAnalytics . getInstance ( this ) ;
         if ( sTracker == null ) {
             sTracker = sAnalytics . newTracker ( R . xml . global_tracker ) ;
         }
         return sTracker ;
     }
}

5. Create a directory with name xml in res folder of your project. Then create global_tracker.xml file inside xml folder with following code.

5.在項目的res文件夾中創建一個名稱爲xml的目錄。 然後使用以下代碼在xml文件夾中創建global_tracker.xml文件。

global_tracker.xml

global_tracker.xml

1
2
3
4
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="ga_trackingId">Tracking ID</string>
</resources>
1
2
3
4
<? xml version = "1.0" encoding = "utf-8" ?>
< resources >
     < string name = "ga_trackingId" > Tracking ID < / string >
< / resources >

Replace Tracking ID with the ID that you got after creating the analytics account.

跟蹤ID替換爲創建分析帳戶後獲得的ID。

5. Add internet and network access permission in AndroidManifest.xml file. Also add a property android:name=」.AnalyticsApplication」 inside <application> tag. See below screenshot.

5.在AndroidManifest.xml文件中添加Internet和網絡訪問權限。 還要在<application>標籤內添加屬性android:name =」。AnalyticsApplication」 。 請參見下面的屏幕截圖。

Android Google Analytics Integration Tutorial 8

6. Now for sending tracking details to google analytics first create tracker instance by using following lines.

6.現在要向Google Analytics(分析)發送跟蹤詳細信息,請首先使用以下幾行創建跟蹤器實例。

1
2
AnalyticsApplication application = (AnalyticsApplication) getApplication();
mTracker = application.getDefaultTracker();
1
2
AnalyticsApplication application = ( AnalyticsApplication ) getApplication ( ) ;
mTracker = application . getDefaultTracker ( ) ;

7. For sending tracking details you have to use following two lines inside onResume() method of an activity.

7.要發送跟蹤詳細信息,您必須在活動的onResume()方法內使用以下兩行。

1
2
mTracker.setScreenName("Main Activity");
mTracker.send(new HitBuilders.ScreenViewBuilder().build());
1
2
mTracker . setScreenName ( "Main Activity" ) ;
mTracker . send ( new HitBuilders . ScreenViewBuilder ( ) . build ( ) ) ;

For demonstration purpose below I have shown the code of MainActivity.java of my demo project.

爲了演示起見,下面顯示了演示項目的MainActivity.java代碼。

MainActivity.java

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.googleanalytics;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
public class MainActivity extends AppCompatActivity {
    Tracker mTracker;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Obtain the shared Tracker instance
        AnalyticsApplication application = (AnalyticsApplication) getApplication();
        mTracker = application.getDefaultTracker();
    }
    @Override
    protected void onResume() {
        super.onResume();
        //sending tracking information
        mTracker.setScreenName("Main Activity");
        mTracker.send(new HitBuilders.ScreenViewBuilder().build());
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com . googleanalytics ;
import android . support . v7 . app . AppCompatActivity ;
import android . os . Bundle ;
import com . google . android . gms . analytics . HitBuilders ;
import com . google . android . gms . analytics . Tracker ;
public class MainActivity extends AppCompatActivity {
     Tracker mTracker ;
     @Override
     protected void onCreate ( Bundle savedInstanceState ) {
         super . onCreate ( savedInstanceState ) ;
         setContentView ( R . layout . activity_main ) ;
         //Obtain the shared Tracker instance
         AnalyticsApplication application = ( AnalyticsApplication ) getApplication ( ) ;
         mTracker = application . getDefaultTracker ( ) ;
     }
     @Override
     protected void onResume ( ) {
         super . onResume ( ) ;
         //sending tracking information
         mTracker . setScreenName ( "Main Activity" ) ;
         mTracker . send ( new HitBuilders . ScreenViewBuilder ( ) . build ( ) ) ;
     }
}

8. Save and run your app. In google analytics console you will see real time user as shown in below screenshot.

8.保存並運行您的應用程序。 在Google Analytics(分析)控制檯中,您將看到實時用戶,如以下屏幕截圖所示。

Android Google Analytics Integration Tutorial 9

Comment below if you have any queries regarding above Android Google Analytics Integration tutorial.

如果您對以上Android Google Analytics(分析)集成教程有任何疑問,請在下面評論。

翻譯自: https://www.thecrazyprogrammer.com/2017/08/android-google-analytics-integration-tutorial.html