2015-03-03 55 views
-2

在我的應用程序中,我實施了Google Analytics(分析)。我爲Google分析做了所有事情。我的問題是它不適用於我的應用程序。請告訴我我做錯了什麼。如何在android中實現谷歌分析?

應用:

public class RApplication extends Application { 

    // The following line should be changed to include the correct property id. 
    private static final String PROPERTY_ID = "xxxxxxxxx"; 

    static final String TAG = ReloadApplication.class.getSimpleName(); 

    public static int GENERAL_TRACKER = 0; 

    public enum TrackerName { 
     APP_TRACKER, // Tracker used only in this app. 
     GLOBAL_TRACKER, // Tracker used by all the apps from a company. eg: 
     // roll-up tracking. 
     ECOMMERCE_TRACKER, // Tracker used by all ecommerce transactions from a 
     // company. 
    } 

    HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>(); 

    public ReloadApplication() { 
     super(); 
    } 

    @Override 
    public void onCreate() { 
     ACRA.init(this); 
     super.onCreate(); 

     traceD(" Reload oncreate "); 

     Tracker tracker = getTracker(TrackerName.APP_TRACKER); 
//  tracker.se 
//  tracker.send(mTrackers); 

    } 

    UncaughtExceptionHandler exceptionHandler = new UncaughtExceptionHandler() { 

     @Override 
     public void uncaughtException(Thread thread, Throwable ex) { 

      traceD(" exceptionHandler : " + ex.getClass() + "" 
        + ex.getMessage()); 
      System.exit(1); 
     } 
    }; 

    class AnalyticsExceptionParser implements ExceptionParser { 
     /* 
     * (non-Javadoc) 
     * 
     * @see 
     * com.google.analytics.tracking.android.ExceptionParser#getDescription 
     * (java.lang.String, java.lang.Throwable) 
     */ 
     public String getDescription(String p_thread, Throwable p_throwable) { 
      return "Thread: " + p_thread + ", Exception: " 
        + p_throwable.getStackTrace(); 
     } 
    } 

    void traceD(String msg) { 

     //Log.d(TAG, msg); 
    } 

    synchronized public Tracker getTracker(TrackerName trackerId) { 
     if (!mTrackers.containsKey(trackerId)) { 

      GoogleAnalytics analytics = GoogleAnalytics.getInstance(this); 
      //analytics.getLogger().setLogLevel(Logger.LogLevel.VERBOSE); 
      analytics.enableAutoActivityReports(this); 
      Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics 
        .newTracker(PROPERTY_ID) : analytics 
        .newTracker(R.xml.global_tracker); 
      t.enableAdvertisingIdCollection(true); 
      mTrackers.put(trackerId, t); 
     } 
     return mTrackers.get(trackerId); 
    } 

    public void sendScreenName(String screenName) { 
     Tracker easyTracker = 
       getTracker(TrackerName.APP_TRACKER); 

     // Set screen name. 
     easyTracker.setScreenName(screenName); 
     easyTracker.setAppName("Reload Android"); 
     easyTracker.setTitle("Reload Android"); 

     // Send a screen view. 
     easyTracker.send(new HitBuilders.AppViewBuilder().build()); 
    } 
} 

XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string name="ga_trackingId">xxxxxxx</string> 
    <integer name="ga_sessionTimeout">300</integer> 
    <bool name="ga_reportUncaughtExceptions">true</bool> 
    <bool name="ga_autoActivityTracking">true</bool> 
    <screenName name="com.reloadapp.reload.fragments.Mobile_Fragment"> 
     Browse Plans 
    </screenName> 
    <!-- The following value should be replaced with correct property id. --> 

</resources> 

庫:

compile fileTree(include: ['*.jar'], dir: 'libs') 
    compile 'com.android.support:appcompat-v7:21.0.3' 
    compile 'com.squareup.picasso:picasso:2.5.0' 
    compile project(':mobihelp_sdk_android_v1.3.1') 

    compile 'ch.acra:acra:4.5.0' 
    compile files('libs/libGoogleAnalyticsV2.jar') 
    compile 'com.google.android.gms:play-services:6.5.87' 

清單文件:

<meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 
     <meta-data 
      android:name="com.google.android.gms.analytics.globalConfigResource" 
      android:resource="@xml/analytics_global_config" /> 

請任何人幫我一把。

+0

請具體說明! – 2015-03-03 03:48:11

+0

不工作是什麼意思?日誌中有任何錯誤? – 2015-03-03 03:48:32

+0

從你上面的代碼,它似乎是你只宣佈的功能,並呼籲他們在任何地方 – Fahim 2015-03-03 03:49:14

回答

0

爲了捕獲屏幕調用這個代碼在您的所有活動

// May return null if EasyTracker has not yet been initialized with a property 
// ID. 
Tracker easyTracker = EasyTracker.getInstance(this); 

// This screen name value will remain set on the tracker and sent with 
// hits until it is set to a new value or to null. 
easyTracker.set(Fields.SCREEN_NAME, "Home Screen"); 

easyTracker.send(MapBuilder 
    .createAppView() 
    .build() 
); 

您可以參考更多信息此鏈接 https://developers.google.com/analytics/devguides/collection/android/v3/screens

+0

fahim ....對於我需要在oncreate中編寫此代碼的所有活動? EasyTracker不來 – 2015-03-03 04:06:55

+0

是的..你在使用哪個庫? – Fahim 2015-03-03 04:51:59

+0

請參閱我的文章中的libs – 2015-03-03 04:53:18