0

Thanks for the answer, but it still does not work.

I have create :

public class GAnalytics extends Application{

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

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 GAnalytics() {
    super();
}

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

        GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
        Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID)
                : (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(
                        R.xml.global_tracker)
                        : analytics.newTracker(R.xml.ecommerce_tracker);
        mTrackers.put(trackerId, t);

    }
    return mTrackers.get(trackerId);
}

}

Under OnCreate in my MainActivity:

String ACTIVITY_NAME = "MyApp";
// Get tracker.
    Tracker t = ((GAnalytics) getApplication()).getTracker(TrackerName.APP_TRACKER);

    // Set screen name.
    // Where path is a String representing the screen name.
    t.setScreenName(ACTIVITY_NAME);

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

I have added the manifest under Application (android: name...)

And my Log:

05-16 18:27:41.705: E/AndroidRuntime(25187): FATAL EXCEPTION: main 05-16 18:27:41.705: E/AndroidRuntime(25187): Process: com.MyApplication.Banana, PID: 25187 05-16 18:27:41.705: E/AndroidRuntime(25187): java.lang.NoClassDefFoundError: com.google.android.gms.analytics.GoogleAnalytics 05-16 18:27:41.705: E/AndroidRuntime(25187): at com.MyApplication.Banana.GAnalytics.getTracker(GAnalytics.java:30) 05-16 18:27:41.705: E/AndroidRuntime(25187): at com.MyApplication.Banana.MainActivity.onCreate(MainActivity.java:56) 05-16 18:27:41.705: E/AndroidRuntime(25187): at android.app.Activity.performCreate(Activity.java:5231) 05-16 18:27:41.705: E/AndroidRuntime(25187): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 05-16 18:27:41.705: E/AndroidRuntime(25187): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 05-16 18:27:41.705: E/AndroidRuntime(25187): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 05-16 18:27:41.705: E/AndroidRuntime(25187): at android.app.ActivityThread.access$800(ActivityThread.java:135) 05-16 18:27:41.705: E/AndroidRuntime(25187): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 05-16 18:27:41.705: E/AndroidRuntime(25187): at android.os.Handler.dispatchMessage(Handler.java:102) 05-16 18:27:41.705: E/AndroidRuntime(25187): at android.os.Looper.loop(Looper.java:136) 05-16 18:27:41.705: E/AndroidRuntime(25187): at android.app.ActivityThread.main(ActivityThread.java:5017) 05-16 18:27:41.705: E/AndroidRuntime(25187): at java.lang.reflect.Method.invokeNative(Native Method) 05-16 18:27:41.705: E/AndroidRuntime(25187): at java.lang.reflect.Method.invoke(Method.java:515) 05-16 18:27:41.705: E/AndroidRuntime(25187): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 05-16 18:27:41.705: E/AndroidRuntime(25187): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 05-16 18:27:41.705: E/AndroidRuntime(25187): at dalvik.system.NativeStart.main(Native Method)

NewYorker
  • 67
  • 1
  • 8

2 Answers2

2

As described in your link, the GA-related code should be placed inside the Application class instead of the Activity.

Create a class extends Application like this:

public class MyApplication extends Application{
    //the tracker related code should be place here (the enum, getTracker, mTrackers)
}

then in your AndroidManifest.xml, under the application tag, replace the android:name with the above MyApplication class.

The tracker code should be placed in the Activity.oncreate method such as

public class MainActivity extends Activity {
.
.
.
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
// Get tracker.
    Tracker t = ((AnalyticsSampleApp) getApplication()).getTracker(
        TrackerName.APP_TRACKER);

    // Set screen name.
    // Where path is a String representing the screen name.
    t.setScreenName(path);

    // Send a screen view.
    t.send(new HitBuilders.AppViewBuilder().build());
}
.
.
.
}
goofyz
  • 243
  • 4
  • 10
  • Thanks, I've done it, but unfortunately it still does not go. – NewYorker May 15 '14 at 18:16
  • You don't need to use `getActivity()` in the `MainActivity`, just call `getApplication()` is enough. My code is updated accordingly. – goofyz May 16 '14 at 01:28
  • Thank you very much I changed it to GetApplication () and deleted getActivity (). Now I get the above (changed) error message in the log. java.lang.NoClassDefFoundError: com.google.android.gms.analytics.GoogleAnalytics com.MyApplication.Banana.GAnalytics.getTracker(GAnalytics.java:30) Please, continue helf me – NewYorker May 16 '14 at 16:39
  • can it be that my Java build path is to blame it does not work? I have "Android private libraries" disable otherwise it displays an error to me: Dex Loader] Unable to execute dex: Multiple dex files define Lcom/google/ads/AdRequest$ErrorCode; LEDSuperFlashlightFree] Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lcom/google/ads/AdRequest$ErrorCode; – NewYorker May 16 '14 at 18:34
  • Please try to "clean" your project and re-build it to see if it helps. And please make sure the Java version you used for your project and the google play services library are the same. – goofyz May 19 '14 at 01:23
  • Clean Have performed. Java version is ok. But still do not work unfortunately. – NewYorker May 19 '14 at 07:31
  • You should include the "Android Private libraries" as it contains the library for deploy. The problem `Multiple dex files dfine` is another issue you should solved. Please double check your build path to ensure no duplicate jar is included (Please check all library project as well). [This link](http://stackoverflow.com/questions/7870265/unable-to-execute-dex-multiple-dex-files-define-lcom-myapp-rarray) may help you. – goofyz May 19 '14 at 09:07
  • Thank you for your help. If I remove AdMob.jar, runs Analytics. But Admob no more. So google play services does not understand with Admob.jar ... – NewYorker May 19 '14 at 16:48
  • [Google Mobile Ads](https://developers.google.com/mobile-ads-sdk/download) is now served through Google Play Services as well. You may need to update your mobile ads code. – goofyz May 20 '14 at 00:43
  • What's the point of using auto activity tracking if I still have to manually set the name? Wouldn't that make the XML pretty pointless? – Bill Mote Aug 27 '14 at 17:39
  • @BillMote If you didn't set the name, you will get the activity's full class path (e.g. com.example.activity) in your analytics, which is difficult to read. You can set the name in XML, it has the same effect as setting in code. Anyway, I think the problem here is not about auto tracking. – goofyz Aug 28 '14 at 07:16
2

Today i faced same issue and got your post, but no one has pointed to right direction.

But i found solution and got the google analytics code working.

Please do follow:

1st - Move tracking code to your fragment class in public void onCreate(Bundle savedInstanceState) method (this has been changed in V4)
Reason: Because the lifetime of fragments is not as straightforward as that of Activities. Android does not provide callbacks for fragment lifecycle.

2nd - Step 2 from v4 implementation doc the Initialize Trackers code should be written into the Application class. Important for Application Class refer application tag and property as android:name="com.brodev.socialapp.entity.User" from AndroidManifest.xml

Please mark answer UP if this helps. So it will help others.

Rakesh
  • 310
  • 2
  • 7