如何在android中实现谷歌分析?

时间:2015-03-03 03:46:23

标签: android google-analytics

在我的应用程序中,我实施了Google Analytics。我为Google分析做了所有事情。我的问题是它在我的应用程序中不起作用。请告诉我我做错了什么。

应用:

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" />

请任何人帮助我。

1 个答案:

答案 0 :(得分: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