本指南介绍了如何将 Google Analytics(分析)添加到您的 Android 应用以衡量用户在已命名屏幕上的活动。如果您目前没有应用,而是仅仅想了解一下 Google Analytics(分析)的工作原理,请参阅我们的示例应用。
必需:最新版 Android Studio 和 Google Play 服务
设置项目
更新您项目的 AndroidManifest.xml 文件,以使其包含 INTERNET 和 ACCESS_NETWORK_STATE 权限:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.analytics">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application android:name="AnalyticsApplication">
...
</application>
</manifest>
适用于 Gradle 的 Google 服务插件会解析 google-services.json 文件中的配置信息。通过更新您的顶级 build.gradle 和应用一级 build.gradle 文件来将该插件添加到您的项目中,具体操作如下所示:
- 将下面的依赖关系添加到您的项目一级
build.gradle中:classpath 'com.google.gms:google-services:3.0.0'
- 将此插件添加到应用级
build.gradle的底部:apply plugin: 'com.google.gms.google-services'
现在,您需要为 Google Play 服务添加一个依赖关系。为此,请在您应用的 build.gradle 中添加以下内容:
compile 'com.google.android.gms:play-services-analytics:9.2.0'
获取配置文件
点击下面的按钮即可获取配置文件以将其添加到您的项目中。
该配置文件提供了您的应用的服务专用信息。要获取该文件,您必须为您的应用选择一个现有项目或新建一个项目。此外,您还需要为应用提供一个文件包名称。
Get a Configuration File将配置文件添加到项目中
将您刚刚下载的 google-services.json 文件复制到您 Android Studio 项目的 app/ 或 mobile/ 目录中。打开 Android Studio 的“终端”窗格:
$ mv path-to-download/google-services.json app/
$ move path-to-download/google-services.json app/
添加屏幕跟踪
此时,每当用户打开或切换您应用上的屏幕时,您都会向 Google Analytics(分析)发送一次已命名的屏幕浏览。您的代码应能够:
- 通过 Application 子类提供共享的跟踪器。
- 替换前台活动的回调方法。
- 为屏幕提供名称并执行跟踪。
应用
您应该将 Application 纳入子类并提供一个可返回应用跟踪器的辅助方法。
/*
* Copyright Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.quickstart.analytics;
import android.app.Application;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
/**
* This is a subclass of {@link Application} used to provide shared objects for this app, such as
* the {@link Tracker}.
*/
public class AnalyticsApplication extends Application {
private Tracker mTracker;
/**
* Gets the default {@link Tracker} for this {@link Application}.
* @return tracker
*/
synchronized public Tracker getDefaultTracker() {
if (mTracker == null) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
// To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
mTracker = analytics.newTracker(R.xml.global_tracker);
}
return mTracker;
}
}
Activity 或 Fragment
打开您要跟踪的 Activity。您也可以跟踪 Fragment,但请确保其准确代表屏幕浏览。
替换您要跟踪的 Activity 或 Fragment 的 onCreate 方法来获取共享的 Tracker 实例:
// Obtain the shared Tracker instance. AnalyticsApplication application = (AnalyticsApplication) getApplication(); mTracker = application.getDefaultTracker();
替换为合适的方法,例如使用 onResume 来跟踪 Activity 或者使用 onPageSelected 来跟踪 ViewPager,以记录屏幕切换情况。
Log.i(TAG, "Setting screen name: " + name);
mTracker.setScreenName("Image~" + name);
mTracker.send(new HitBuilders.ScreenViewBuilder().build());
向代表屏幕的每个 Activity 或 Fragment 添加跟踪代码。如果您想在 Google Analytics(分析)中区分您应用的不同屏幕浏览数据,请务必在每个 Activity 或 Fragment 内设置一个名称。记录在共享跟踪器上的所有活动会发送最新的屏幕名称,直到这些名称被替换或清除(设置为 null)。
发送事件
要发送事件,请在跟踪器上设置屏幕字段值,然后发送匹配。下面的示例使用 HitBuilders.EventBuilder 来发送 Event:
mTracker.send(new HitBuilders.EventBuilder()
.setCategory("Action")
.setAction("Share")
.build());
