이 가이드에서는 Android 앱에 애널리틱스를 추가하여 이름이 지정된 화면에 대한 사용자 활동을 측정하는 방법을 보여줍니다. 아직 애플리케이션이 없으며 애널리틱스의 작동 방식을 보려면 샘플 애플리케이션을 참조하세요.
필수: 최신 버전:
프로젝트 설정
INTERNET
및 ACCESS_NETWORK_STATE
권한을 포함하도록 프로젝트의 AndroidManifest.xml
파일을 업데이트합니다.
<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>
프로젝트 수준 build.gradle
에 다음 종속 항목을 추가합니다.
dependencies {
// ...
classpath 'com.google.gms:google-services:3.0.0'
}
app/build.gradle
에 Google Play 서비스의 다음 종속 항목을 추가합니다.
dependencies {
// ...
compile 'com.google.android.gms:play-services-analytics:10.2.4'
}
global_tracker.xml 만들기
다음 콘텐츠로 app/src/res/xml/global_tracker.xml
파일을 만듭니다.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="ga_trackingId" translatable="false">${YOUR_TRACKING_ID}</string>
</resources>
${YOUR_TRACKING_ID}
를 추적 ID로 바꿉니다.
화면 추적 추가
여기에서 사용자가 앱에서 화면을 열거나 변경할 때마다 이름이 지정된 화면 조회수를 애널리틱스로 전송합니다. 코드는 다음을 실행해야 합니다.
- 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 static GoogleAnalytics sAnalytics;
private static Tracker sTracker;
@Override
public void onCreate() {
super.onCreate();
sAnalytics = GoogleAnalytics.getInstance(this);
}
/**
* Gets the default {@link Tracker} for this {@link Application}.
* @return tracker
*/
synchronized public Tracker getDefaultTracker() {
// To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
if (sTracker == null) {
sTracker = sAnalytics.newTracker(R.xml.global_tracker);
}
return sTracker;
}
}
활동 또는 프래그먼트
추적하려는 활동을 엽니다. Fragment
를 추적할 수도 있지만 화면 조회수를 올바르게 나타내는지 확인해야 합니다.
추적하려는 Activity
또는 Fragment
의 onCreate
메서드를 재정의하여 공유 Tracker
인스턴스를 가져옵니다.
// Obtain the shared Tracker instance.
AnalyticsApplication application = (AnalyticsApplication) getApplication();
mTracker = application.getDefaultTracker();
화면이 변경되면 로깅할 수 있도록 Activity
의 경우 onResume
, ViewPager
의 경우 onPageSelected
과 같이 적절한 메서드를 재정의합니다.
Log.i(TAG, "Setting screen name: " + name);
mTracker.setScreenName("Image~" + name);
mTracker.send(new HitBuilders.ScreenViewBuilder().build());
화면을 나타내는 모든 Activity
또는 Fragment
에 추적 코드를 추가합니다. 애널리틱스에서 앱의 화면 뷰를 구분하려면 Activity
또는 Fragment
마다 이름을 설정해야 합니다. 공유 추적기에 기록된 모든 활동은 교체되거나 삭제 (null
로 설정)될 때까지 가장 최근의 화면 이름을 전송합니다.
이벤트 전송
이벤트를 전송하려면 추적기에서 화면 필드 값을 설정한 다음 조회를 전송하세요.
다음 예에서는 HitBuilders.EventBuilder
를 사용하여 Event
를 전송합니다.
mTracker.send(new HitBuilders.EventBuilder()
.setCategory("Action")
.setAction("Share")
.build());
다음 단계
Google 애널리틱스를 사용하여 사용자 상호작용을 측정하고 앱 사용에 관한 질문에 답하는 방법을 알아보려면 모바일 앱 구현 가이드를 읽어보세요.
샘플링, 테스트 및 디버깅, 선택 해제 설정 등의 추가 구성 옵션을 검토합니다.
앱에서 광고 식별자를 수집해야 하는 경우 앱에 광고 기능을 사용 설정합니다.