3

I'm using V4 of the Google Analytics Library as provided by Google Play services. Long story short: I find passing a reference to the tracker to all the components my application to be quite tedious. I know in previous versions of the library a Singleton for managing one instance was provided (EasyTracker) but no longer.

Would anyone know what the detriments are to putting a tracker in a singleton once the application starts?

rodentry
  • 268
  • 3
  • 12

2 Answers2

2

No one ever answered you, and I'm sure you figured it out. I've implemented this with a Singelton and it seems to work fine. If anyone else has the same question, I put my wrapper up on github. maybe it will save someone else some time:

https://github.com/uglyelf/android-helpers/blob/master/AnalyticsWrapper.java

  • hi! I figured this out a while ago and wrote something along the lines you did, i.e. build a simple API/wrapper on top of the hitbuilders which my app could then leverage where-ever it needed to. For being so kind as to reply, please have my upvote and the answer selected. Thanks! – rodentry Jan 27 '15 at 21:38
  • Thanks! And I'm glad to hear you did something similar. I was worried there was a hidden gotcha I was unaware of. But I guess not. – gregory randolph Jan 27 '15 at 21:41
2

I have been trying to find an answer to this question myself, the top answer on this question gave me reason enough to pursue this approach:

One reason that tends to come up over and over again on the internets is that of a "logging" class (which you mentioned). In this case, a Singleton can be used instead of a single instance of a class because a logging class usually needs to be used over and over again ad nauseam by every class in a project. If every class uses this logging class, dependency injection becomes cumbersome.

Along with @gregory-randolph's answer of course.

Community
  • 1
  • 1
Leon
  • 3,614
  • 1
  • 33
  • 46