简体   繁体   中英

Google Analytics v4 Android HitBuilder

I've found the official guide to google analytics for android ( https://developers.google.com/analytics/devguides/collection/android/v4/ ) lacking in comprehensive examples. Trying to implement it has been confusing so far.

First issue:

This is how my getTracker in a subclass of Application looks like

public synchronized Tracker getTracker() {

    if (_globalTracker == null) {
        GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
        return analytics.newTracker(R.xml.global_tracker);
    }
    return _globalTracker;
}

But eclipse says "global_tracker cannot be resolved or is not a field" even though I've created an XML file called global_tracker.xml -- its under res/xml as directed in the tutorial

Second issue:

In one of my activities, I try to get the tracker to send a screen view, as detailed in the tutorial:

// Get tracker.
    Tracker t = ((StorageApplication) getApplication()).getTracker();

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

Unfortunately, eclipse says "HitBuilders cannot be resolved to a type" even though I'm pretty sure I have google play services and analytics imported and referenced properly (I also use geolocation and other services from google play services and they all work; analytics is imported properly because there is no issue with resolving Tracker).

Anyone have any pointers? I'd really appreciate your help -- been left wondering why this is so confusing!

If someone wanna to use Eclipse for his/her application, the solution is this:

In order to fix it you have to export into Eclipse workspase these libs:

play-services-analytics-impl-9.8.0.arr and play-services-analytics-9.8.0.arr

Using this answer

https://stackoverflow.com/a/33117095/1979882

In breaf, play-services library was divided into several lib in order avoid limitation of classes inside DEX-file (appr. 65000 classes). So, you have to export most common libs too in the same way:

play-services-maps-9.8.0 , play-services-base-9.8.0 , play-services-basement-9.8.0 , play-services-ads-lite-9.8.0

For first issue: Does eclipse also complain about other resource Ids? You'll have to compile the application once before resource id shows up in eclipse.

For second issue:

Can you post the full class if it is not big?

Also, just because i have to ask, can you confirm that you are using the play services version of the sdk and including the classes from the right library? What is the package you are importing for these classes?

ps: I'll update my answer once I have more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM