I'm working with two different Analytics services (Google Analytics and Mixpanel) for my Android app.
I created an abstract base class like so:
public abstract class AnalyticsService {
public abstract void trackAppOpen();
public abstract void identifyUserOnLogin();
}
Then I created concrete subclasses which extended from the AnalyticsService base class.
public class MPAnalytics extends AnalyticsService {
@Override
public void trackAppOpen() {
//Send event to MixPanel
}
@Override
public void identifyUserOnLogin() {
//Identify user with MixPanel
}
}
public class GoogleAnalytics extends AnalyticsService {
@Override
public void trackAppOpen() {
//Send event to Google
}
@Override
public void identifyUserOnLogin() {
//Do nothing
}
}
Now Google Analytics doesn't actually support user identification so I had to create an empty implementation of the registerUserOnLogin() method.
I feel this probably is not the right way to do it since user identification is not a behavior of Google Analytics and hence it should ideally be not part of the base AnalyticsService class and should instead be defined in the MPAnalytics class.
But the problem with that approach is on the client, I want to be able to refer to all the different analytics services by using the base AnalyticsService class and not worry about the concrete implementation.
List<AnalyticsService> analyticsServices;
for (AnalyticsService service : analyticsServices) {
service.trackAppOpen();
service.identifyUserOnLogin(); // where should this method be?
}
Is there a better way to do this? Thanks.
Aucun commentaire:
Enregistrer un commentaire