बेहतर कॉन्फ़िगरेशन - Android SDK v2 (लेगसी)

इस दस्तावेज़ में Android v2 के लिए Google Analytics SDK की कुछ बेहतर कॉन्फ़िगरेशन सुविधाओं का व्यूव्यू दिया गया है.

खास जानकारी

Android के लिए Google Analytics SDK टूल, लागू करने की ग्लोबल स्थिति को मैनेज करने और Google Analytics सर्वर पर डेटा भेजने के लिए, दो क्लास का इस्तेमाल करता है. EasyTracker इन क्लास को रैप करता है, ताकि कॉन्फ़िगरेशन और सेशन को आसानी से मैनेज किया जा सके:

  • GoogleAnalytics – यह सिंगलटन है जो आपके लागू किए जाने की ग्लोबल स्थिति को मैनेज करता है. इसमें Tracker ऑब्जेक्ट पाना भी शामिल है.
  • Tracker – वह क्लास जिससे Google Analytics को डेटा भेजा जाता है.

उदाहरण के लिए, किसी सामान्य Activity के व्यू को मेज़र करने के लिए:

import android.app.Activity

import com.google.analytics.tracking.android.GoogleAnalytics
import com.google.analytics.tracking.android.Tracker

/**
 * A simple Activity that sends a screen view to Google Analytics
 * when it is displayed to the user.
 */
public class HomeScreen extends Activity {

  private Tracker mGaTracker;
  private GoogleAnalytics mGaInstance;

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

    // Get the GoogleAnalytics singleton. Note that the SDK uses
    // the application context to avoid leaking the current context.
    mGaInstance = GoogleAnalytics.getInstance(this);

    // Use the GoogleAnalytics singleton to get a Tracker.
    mGaTracker = mGaInstance.getTracker("UA-XXXX-Y"); // Placeholder tracking ID.
    ... // The rest of your onCreate() code.
  }

  @Override
  public void onStart() {
    super.onStart();

    // Send a screen view when the Activity is displayed to the user.
    mGaTracker.sendView("/HomeScreen");
  }
}

कई ट्रैकर इस्तेमाल करना

SDK टूल के वर्शन 2 से, एक ही बार में एक से ज़्यादा ट्रैकर इस्तेमाल किए जा सकते हैं. हर यूनीक ट्रैकिंग आईडी के लिए एक ट्रैकर. सभी ट्रैकर का ग्लोबल स्टेट GoogleAnalyticsआपके सिंगलटन में एक ही होता है.

नीचे दिए गए उदाहरण में, दो ट्रैकर का इस्तेमाल करके एक स्क्रीन व्यू को दो अलग-अलग प्रॉपर्टी को भेजा गया है, जिनमें से हर एक का अपना यूनीक प्रॉपर्टी आईडी है:

import android.app.Activity

import com.google.analytics.tracking.android.GoogleAnalytics
import com.google.analytics.tracking.android.Tracker

/**
 * A simple Activity that sends a screen view to Google Analytics
 * when it is displayed to the user.
 */
public class HomeScreen extends Activity {

  private GoogleAnalytics mGaInstance;
  private Tracker mGaTracker1;
  private Tracker mGaTracker2;

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

    // Get the GoogleAnalytics singleton.
    mGaInstance = GoogleAnalytics.getInstance(this);

    // Use the GoogleAnalytics singleton to get two Trackers with
    // unique property IDs.
    mGaTracker1 = mGaInstance.getTracker("UA-XXXX-Y");
    mGaTracker2 = mGaInstance.getTracker("UA-XXXX-Z");

    ... // The rest of your onCreate() code.
  }

  @Override
  public void onStart() {
    super.onStart();

    // Send a screen view to "UA-XXXX-Y" the Activity is displayed to the user.
    mGaTracker1.sendView("/HomeScreen");

    // Send another screen view to the second property, "UA-XXXX-Z".
    mGaTracker2.sendView("/Home");
  }
}

ध्यान रखें कि EasyTracker से मिलने वाली अपने-आप मेज़रमेंट की सुविधाएं, अपने कॉल करने के लिए सिर्फ़ डिफ़ॉल्ट ट्रैकर का इस्तेमाल करेंगे. अगर आप इन EasyTracker की सुविधाओं का इस्तेमाल कर रहे हैं और आपको दूसरे ट्रैकर का इस्तेमाल करके डेटा भेजना है, तो आपको यह काम मैन्युअल तरीके से करना होगा.

डिफ़ॉल्ट ट्रैकर

लागू करने के लिए एक से ज़्यादा ट्रैकर का इस्तेमाल किया जा सकता है, लेकिन दुनिया भर में इसका एक डिफ़ॉल्ट ट्रैकर होता है. हासिल किया गया पहला Tracker, डिफ़ॉल्ट ट्रैकर बन जाता है.

डिफ़ॉल्ट ट्रैकर पाने के लिए इनका इस्तेमाल करें:

// Get singleton.
GoogleAnalytics myInstance = GoogleAnalytics.getInstance(this);

// Get default tracker.
Tracker myDefault = myInstance.getDefaultTracker();

डिफ़ॉल्ट ट्रैकर सेट करने के लिए, इसका इस्तेमाल करें:

// First get a tracker using a new property ID.
Tracker newTracker = myInstance.getTracker("UA-XXXX-2");

// Then make newTracker the default tracker globally.
myInstance.setDefaultTracker(newTracker);

डिस्पैच किया जा रहा है

डिस्पैच सेटिंग को GAServiceManager सिंगलटन मैनेज करता है. डिस्पैच अवधि सेट करने के लिए:

// Set dispatch period to 30 seconds.
GAServiceManager.getInstance().setDispatchPeriod(30);

मैन्युअल तरीके से, शेड्यूल किए गए इंटरवल के बाहर भेजने के लिए:

// Manually dispatch any queued hits.
GAServiceManager.getInstance().dispatch();

सैंपलिंग

Google Analytics को भेजे जाने वाले हिट की संख्या को सीमित करने के लिए, क्लाइंट-साइड सैंपलिंग चालू की जा सकती है. अगर आपके ऐप्लिकेशन में बहुत ज़्यादा उपयोगकर्ता हैं या Google Analytics को बहुत ज़्यादा डेटा भेजता है, तो सैंपलिंग चालू करने से रिपोर्टिंग बिना किसी रुकावट के होने में मदद मिलेगी.

उदाहरण के लिए, EasyTracker और एक्सएमएल के ज़रिए 50% की दर से क्लाइंट-साइड सैंपलिंग चालू करने के लिए, अपनी analytics.xml फ़ाइल में इस पैरामीटर का इस्तेमाल करें:

<string name="ga_sampleFrequency">50.0</string>

प्रोग्राम के हिसाब से, क्लाइंट-साइड सैंपलिंग की सुविधा भी चालू की जा सकती है:

// Get tracker from singleton.
GoogleAnalytics gaInstance = GoogleAnalytics.getInstance(this);
Tracker tracker = gaInstance.getTracker("UA-XXXX-Y"); // Placeholder property ID.

// Set a sample rate of 50%.
tracker.setSampleRate(50.0d);

ऐप्लिकेशन-लेवल से ऑप्ट आउट करना

ऐप्लिकेशन-लेवल पर ऑप्ट आउट फ़्लैग को चालू किया जा सकता है, जो Google Analytics को पूरे ऐप्लिकेशन के लिए बंद कर देगा. सेट करने के बाद, फ़्लैग हमेशा ऐप्लिकेशन के चालू रहने या उसे रीसेट किए जाने तक बना रहेगा.

ऐप्लिकेशन लेवल से ऑप्ट आउट करने की सेटिंग पाने के लिए, इनका इस्तेमाल करें:

// Get singleton.
GoogleAnalytics myInstance = GoogleAnalytics.getInstance(this);

// Get the app opt out preference using an AppOptOutCallback.
myInstance.requestAppOptOut(new AppOptOutCallback() {
   @Override
   public void reportAppOptOut(boolean optOut) {
     if (optOut) {
     ... // Alert the user that they've opted out.
     }
   });
}

ऐप्लिकेशन-लेवल का ऑप्ट आउट फ़्लैग सेट करने के लिए, इनका इस्तेमाल करें:

myInstance.setAppOptOut(appPreferences.userOptOut);

जांच और डीबग करना

Android के लिए Google Analytics SDK एक डीबग मोड देता है, जो आपके लॉग में Google Analytics को भेजे जा रहे डेटा के बारे में उपयोगी जानकारी प्रिंट करेगा.

EasyTracker का इस्तेमाल करके डीबग मोड चालू करने के लिए, अपनी analytics.xml संसाधन फ़ाइल में यह जोड़ें:

<bool name="ga_debug">true</bool>

प्रोग्राम के हिसाब से डीबग मोड को चालू करने के लिए, इन विकल्पों का इस्तेमाल करें:

// Get singleton using application context.
GoogleAnalytics myInstance = GoogleAnalytics.getInstance(this);

// Enable debug mode.
myInstance.setDebug(true);

आउटपुट को, टैग GAV2 का इस्तेमाल करके logcat में प्रिंट किया जाएगा.