Skip to content

Instantly share code, notes, and snippets.

@ameen-sarsour
Last active April 17, 2016 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ameen-sarsour/8180ab52469191c4f202 to your computer and use it in GitHub Desktop.
Save ameen-sarsour/8180ab52469191c4f202 to your computer and use it in GitHub Desktop.
migrate google analytics library for android v2 to v4
In our project we have v2 of Google analytic lib, and we call the functions from all activates.
To migrate it with V4 I've created a class I called it EasyTracker, it's have the same functions of v2 with the same parameters, each function working as v4 need.
The I replaced the import of EasyTracker from google to be my EasyTracker
import android.app.Activity;
import android.content.Context;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Logger;
import com.google.android.gms.analytics.Tracker;
/**
* Created by android on 1/20/2015.
*/
public class EasyTracker {
private static EasyTracker tracker;
private Activity context ;
private static final String PROPERTY_ID = "UA-XXXXXXXX-XX";
public static EasyTracker getTracker() {
return getInstance();
}
public static EasyTracker getInstance() {
if(tracker == null)
tracker = new EasyTracker();
return tracker;
}
public void setContext(Context context) {
this.context = (Activity)context ;
}
public void setContext(Activity context) {
this.context = context;
}
synchronized Tracker getGoogleTracker( ) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(context);
analytics.getLogger().setLogLevel(Logger.LogLevel.VERBOSE);
Tracker t = analytics.newTracker(PROPERTY_ID) ;
t.enableAdvertisingIdCollection(true);
return t;
}
public void sendEvent(String action, String event, String screenName, long l){
// Get tracker.
Tracker t = getGoogleTracker( );
// Set screen name.
t.setScreenName(screenName);
// Send a screen view.
// t.send(new HitBuilders.AppViewBuilder().build());
t.send(new HitBuilders.EventBuilder()
.setAction(action)
.setLabel("event")
.setValue(l)
.build());
}
public void sendView(String screenName) {
Tracker t = getGoogleTracker( );
t.setScreenName(screenName);
t.send( new HitBuilders.EventBuilder().build());
}
public void activityStart(Activity activity) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(context);
analytics.reportActivityStart(activity);
}
}
@paskino
Copy link

paskino commented Oct 4, 2015

excellent!

@ameen-sarsour
Copy link
Author

@paskino thank you

@HarmeetSingh24
Copy link

Hi @ameen-sarsour can you help how with the build properties ,I am getting
at com.google.android.gms.common.internal.zzx.zzl(Unknown Source)
at com.google.android.gms.analytics.GoogleAnalytics.getInstance(Unknown Source)

Thank you

@JCarlosR
Copy link

@HarmeetSingh24 I have the same error. Do you found a solution?

Edit: I am trying with ...
if (context == null) context = activity;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment