配置
项目配置
修改项目AndroidManifest.xml文件,增加INTERNET、ACCESS_NETWORK_STATE权限,更改application的name属性。
<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" ... /> />`
build.gradle配置
项目一级目录 buid.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
// ��重要
classpath 'com.google.gms:google-services:1.5.0-beta2' }
}应用一级目录 build.gradle:
apply plugin: 'com.android.application'
android {
...
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0'
// ��重要
compile 'com.google.android.gms:play-services-analytics:8.3.0'
}
// ��重要
apply plugin: 'com.google.gms.google-services'获取配置文件
获取应用app_name与package_name完善信息(获取配置文件部分)➡️链接
将配置文件放到项目的app文件夹��
新建AnalyticsApplication类
import android.app.Application;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Logger;
import com.google.android.gms.analytics.Tracker;
public class AnalyticsApplication extends Application {
private Tracker mTracker;
synchronized public Tracker getDefaultTracker() {
if (mTracker == null) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
mTracker = GoogleAnalytics.getInstance(this).newTracker("UA-XXXXXX-X");
}
return mTracker;
}
}查看统计数据
跟踪Activity Fragment
在onCreate函数中setContentView
语句后添加下面代码,获取跟踪实例:
AnalyticsApplication application = (AnalyticsApplication) getApplication();
mTracker = application.getDefaultTracker();
发送事件
mTracker.send(new HitBuilders.EventBuilder()
.setCategory("Action")
.setAction("Share")
.build());
事件
字段:
category 类别
action 操作
label 标签
value 熟知
// Get tracker.
Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(
TrackerName.APP_TRACKER);
// Build and send an Event.
t.send(new HitBuilders.EventBuilder()
.setCategory(getString(categoryId))
.setAction(getString(actionId))
.setLabel(getString(labelId))
.build());
屏幕浏览
屏幕浏览数据:
手动衡量:
// Get tracker.
Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(
TrackerName.APP_TRACKER);
// Set screen name.
t.setScreenName(screenName);
// Send a screen view.
t.send(new HitBuilders.ScreenViewBuilder().build());
自动衡量:
<!-- Enable automatic Activity measurement -->
<bool name="ga_autoActivityTracking">true</bool>
<!-- The screen names that will appear in reports -->
<screenName name="com.google.android.gms.analytics.samples.mobileplayground.ScreenviewFragment">
AnalyticsSampleApp ScreenViewSampleScreen
</screenName>
<screenName name="com.google.android.gms.analytics.samples.mobileplayground.EcommerceFragment">
AnalyticsSampleApp EcommerceSampleScreen
</screenName>
社交互动
Social Network String 用户与之互动的社交网络(Facebook、Google+、Twitter)。
Social Action String 进行的社交操作(例如赞、分享)。
Social Target String 发生社交操作的内容(例如某篇文章或某段视频)。
实现:
// Get tracker.
Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(
TrackerName.APP_TRACKER);
// Build and send social interaction.
t.send(new HitBuilders.SocialBuilder()
.setNetwork(getSocialNetwork())
.setAction(getSocialAction())
.setTarget(getSocialTarget())
.build());
时间统计
// Get tracker.
Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(
TrackerName.APP_TRACKER);
// Build and send timing.
t.send(new HitBuilders.TimingBuilder()
.setCategory(getTimingCategory())
.setValue(getTimingInterval())
.setVariable(getTimingName())
.setLabel(getTimingLabel())
.build());
部分方法
mTracker.setAnonymizeIp(true) IP地址进行匿名处理
GoogleAnalytics.getInstance(this).setAppOptOut(true) 应用级停用
GoogeAnalytics.getInstance(this).setDryRun(true); 不发送任何数据(调试测试等情况)