Program/Android Java

Google Analytics SDK for Android

너구리V 2012. 9. 6. 15:02

https://developers.google.com/analytics/devguides/collection/android/


package com.google.android.apps.analytics.sample;

import com.google.android.apps.analytics.GoogleAnalyticsTracker;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TestActivity extends Activity {

 
GoogleAnalyticsTracker tracker;

 
@Override
 
protected void onCreate(Bundle savedInstanceState) {
   
super.onCreate(savedInstanceState);

    tracker
= GoogleAnalyticsTracker.getInstance();

   
// Start the tracker in manual dispatch mode...
    tracker
.startNewSession("UA-YOUR-ACCOUNT-HERE", this);

   
// ...alternatively, the tracker can be started with a dispatch interval (in seconds).
   
//tracker.startNewSession("UA-YOUR-ACCOUNT-HERE", 20, this);

    setContentView
(R.layout.main);
   
Button createEventButton = (Button)findViewById(R.id.NewEventButton);
    createEventButton
.setOnClickListener(new OnClickListener() {
     
@Override
     
public void onClick(View v) {
        tracker
.trackEvent(
           
"Clicks",  // Category
           
"Button",  // Action
           
"clicked", // Label
           
77);       // Value
     
}
   
});

   
Button createPageButton = (Button)findViewById(R.id.NewPageButton);
    createPageButton
.setOnClickListener(new OnClickListener() {
     
@Override
     
public void onClick(View v) {
       
// Add a Custom Variable to this pageview, with name of "Medium" and value "MobileApp" and
       
// scope of session-level.
        tracker
.setCustomVar(1, "Navigation Type", "Button click", 2);
       
// Track a page view. This is probably the best way to track which parts of your application
       
// are being used.
       
// E.g.
       
// tracker.trackPageView("/help"); to track someone looking at the help screen.
       
// tracker.trackPageView("/level2"); to track someone reaching level 2 in a game.
       
// tracker.trackPageView("/uploadScreen"); to track someone using an upload screen.
        tracker
.trackPageView("/testApplicationHomeScreen");
     
}
   
});

   
Button quitButton = (Button)findViewById(R.id.QuitButton);
    quitButton
.setOnClickListener(new OnClickListener() {
     
@Override
     
public void onClick(View v) {
        finish
();
     
}
   
});

   
Button dispatchButton = (Button)findViewById(R.id.DispatchButton);
    dispatchButton
.setOnClickListener(new OnClickListener() {
     
@Override
     
public void onClick(View v) {
       
// Manually start a dispatch, not needed if the tracker was started with a dispatch
       
// interval.
        tracker
.dispatch();
     
}
   
});
 
}

 
@Override
 
protected void onDestroy() {
   
super.onDestroy();
   
// Stop the tracker when it is no longer needed.
    tracker
.stopSession();
 
}
}

. Tracking PageViews and Events - 페이지뷰, 이벤트 트래킹

- 페이지뷰 : trackPageView 메소드 사용

tracker.trackPageView("/testApplicationHomeScreen");

- 이벤트 : trackEvent 메소드 사용

 tracker.trackEvent(
           
"Clicks",  // Category
           
"Button",  // Action
           
"clicked", // Label
           
77);       // Value

. Custom Variables 사용

- setCustomVar 메소드 사 용 

tracker.setCustomVar(1, "Navigation Type", "Button click", 2);

. Ecommerce Tracking 사용

- addTransaction : Transaction추가 

- addItem : Item 추가 

- trackTransaction : 서버로 전송 

- clearTransaction : 내부 버퍼 지우기 

internal Ecommerce buffer에 transaction 이나 item을 담기 위해서 addTransaction, addItem  메소드를 호출한다.

이후 trackTransaction 메소드를 호출하여 Google Analytics서버로 보낸다.

내부 버퍼를 비우기 위해서 clearTransaction 메소드를 호출한다.

/**
   * The purchase was processed.  We will track the transaction and its associated line items
   * now, but only if the purchase has been confirmed.
   *
   * @param purchase A PurchaseObject containing all of the transaction information needed to
   *     send the ecommerce hit to Google Analytics.
   */

 
public void onPurchaseCompleted(PurchaseObject purchase) {
    tracker
.addTransaction(new Transaction.Builder(
        purchase
.getOrderId(),
        purchase
.getTotal())
       
.setStoreName(purchase.getStoreName())
       
.setTotalTax(purchase.getTotalTax())
       
.setShippingCost(purchase.getShippingCost())
       
.build());
   
for (PurchaseLineItem lineItem : purchase.getLineItems()) {
        tracker
.addItem(new Item.Builder(
            purchase
.getOrderId(),
            lineItem
.getItemSKU(),
            lineItem
.getItemCost(),
            lineItem
.getQuantity())
           
.setItemName(lineItem.getItemName())
           
.setItemCategory(lineItem.getItemCategory())
           
.build());
   
}
   
if (purchase.isConfirmed()) {
      tracker
.trackTransactions();
   
} else {
     
// The purchase was denied or failed in some way.  We need to clear out
     
// any data we've already put in the Ecommerce buffer.
      tracker
.clearTransactions();
   
}
 
}

. Anonymize IP (익명 아이피로 설정)

- setAnonymizeIp 메소드를 호출하면 방문자의 아이피 정보를 익명아이피로 설정할 수 있다.


. Setting Sample Rate (수집 비율 설정)

- setSampleRate 메소드로 트래킹이 전송되는 비율을 설정할 수 있다.

어플이 너무 많은 분석 트래픽을 생성한다면 sample rate를 설정할 수 있다.

0~100사이의 값을 받는다. 0은 기능 끄 기 

Sampling Concepts Guide

반응형