-->

Cara Mudah Menambahkan Fitur Google Analytic ke Dalam Aplikasi Android

This guide shows how to add Analytics to your Android app to measure user activity to named screens. If you don't have an application yet and just want to see how Analytics works, take a look at our sample application.
Required: Latest versions of Android Studio and Google Play Services










Set up your project

AndroidManifest.xml
Update your project's AndroidManifest.xml file to include the INTERNET and ACCESS_NETWORK_STATE permissions:
<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>
Add the following dependency to your project-level build.gradle:
dependencies {
  // ...
  classpath 'com.google.gms:google-services:3.0.0'
}
Add the following dependency on Google Play Services to app/build.gradle:
dependencies {
  // ...
  compile 'com.google.android.gms:play-services-analytics:10.2.4'
}

Create global_tracker.xml

Create the file app/src/res/xml/global_tracker.xml with the following content:
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="ga_trackingId" translatable="false">${YOUR_TRACKING_ID}</string>
</resources>
Replace ${YOUR_TRACKING_ID} with your tracking ID.

Add screen tracking

Here you’ll send a named screen view to Analytics whenever the user opens or changes screens on your app. Your code should do the following:
  • Provide the shared tracker via an Application subclass.
  • Override the callback method for the foreground activity.
  • Provide a name for the screen and execute tracking.

Application

AnalyticsApplication.java
You should subclass Application and provide a helper method that returns your application's tracker.

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;
  }
}

Activity or fragment 

MainActivity.java
Open the Activity that you'd like to track. You could also track a Fragment, but ensure that it correctly represents a screen view.
Override the onCreate method of the Activity or Fragment you want to track to obtain the shared Tracker instance:
// Obtain the shared Tracker instance.
AnalyticsApplication application = (AnalyticsApplication) getApplication();
mTracker = application.getDefaultTracker();

Override the appropriate method, such as onResume for an Activity or onPageSelected for a ViewPager to log when the screen changes.
Log.i(TAG, "Setting screen name: " + name);
mTracker.setScreenName("Image~" + name);
mTracker.send(new HitBuilders.ScreenViewBuilder().build());

Add tracking code to every Activity or Fragment that represents a screen. Be sure to set a name inside every Activity or Fragment if you want to differentiate between screen views for your app in Analytics. All activity recorded on the shared tracker sends the most recent screen name until replaced or cleared (set to null).

Send an event

To send an event, set the screen field values on the tracker, then send the hit. The following example uses the HitBuilders.EventBuilder to send an Event:
mTracker.send(new HitBuilders.EventBuilder()
    .setCategory("Action")
    .setAction("Share")
    .build());

Next steps

0 Posting Komentar untuk "Cara Mudah Menambahkan Fitur Google Analytic ke Dalam Aplikasi Android"

1. Berkomentarlah dengan tata bahasa yang baik.
2. Silahkan tulis komentar Anda yang masih ada kaitanya dengan postingan.
3. Semua komentar kami baca, namun tidak semua bisa balas, harap maklum.
4. Beri tanda centang pada "Beri tahu saya", untuk pemberitahuan jika komentar telah kami balas.
5. Promosi produk/jasa tidak akan diterbitkan kecuali telah ada kerja sama.

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel