Yi Jiang
Yi Jiang

Reputation: 4038

How to implement Google Analytics v4 on a Project in Android Studion imported from legacy Eclipse ADT

My legacy application project doesn't have Application class. So, the official document doesn't help.
I don't know where the following code should go:

/**
 * Enum used to identify the tracker that needs to be used for tracking.
 *
 * A single tracker is usually enough for most purposes. In case you do need multiple trackers,
 * storing them all in Application object helps ensure that they are created only once per
 * application instance.
 */
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>();

And these code:

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);
}

I have imported these libs of googleAnalytics.

enter image description here

But I still can't import libs like this way:

import com.google.android.apps.analytics.GoogleAnalyticsTracker;

And, because I am using Android Studio 1.2 Beta3, the tutorial based on Eclipse also not suit for my case.

Is there any simple way to implement Google Analytics on Android app? Like initialising in iOS?

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [GAI sharedInstance].trackUncaughtExceptions = YES;
  [GAI sharedInstance].dispatchInterval = 20;
  [[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelVerbose];
  [[GAI sharedInstance] trackerWithTrackingId:@"UA-XXXX-Y"];
}

Upvotes: 0

Views: 181

Answers (1)

djabi
djabi

Reputation: 5767

The simples complete implementation of Analytics on Android requires:

  1. Link to Google Play Services (It looks like you already have this done)
  2. Add the required network permissions in your app manifest.
  3. Extend Android Application class, override onCreate and set your app class implementation in the <android name='you_app_class'> name attribute.
  4. In onCreate create new tracker and keep it in static var tracker = GoogleAnalytics.getInstance(context).newTracker(); Enable automatic reporting for screens, exceptions, etc.
  5. Register AnalyticsReceiver & AnalyticsService in your Application Manifest.

Google Analytics has a github project with the simplest Analytics hello world all. It show the bare minimum needed for correct reporting on Android.

Upvotes: 1

Related Questions