Rohit Bajaj
asked on
Better way to write code in java example
Hi,
I have the following classes :
GATracker.java
ITracker.java
Now i am using this GATracker inside another class like this :
This seems a lot of code to write.. Is there any optimal way to achieve the same thing i am doing above or better way.. or is the above way ok ?
Thanks
I have the following classes :
GATracker.java
package to.go.app.analytics.ga;
import android.content.Context;
import android.content.res.Resources.NotFoundException;
import com.google.analytics.tracking.android.Fields;
import com.google.analytics.tracking.android.GAServiceManager;
import com.google.analytics.tracking.android.GoogleAnalytics;
import com.google.analytics.tracking.android.MapBuilder;
import com.google.analytics.tracking.android.Tracker;
import com.google.common.base.Strings;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import olympus.clients.zeus.api.Response.CompleteProfile;
import to.go_internal.R;
import to.go.account.AccountService;
import to.talk.logging.Logger;
import to.talk.logging.LoggerFactory;
import to.talk.utils.event.EventHandler;
public abstract class GATracker implements ITracker
{
public static final int DIM_USER_DOMAIN = 1;
public static final String UID = "&uid";
protected final Tracker _tracker;
private Context _context;
private AccountService _accountService;
private Logger _logger = LoggerFactory.getTrimmer(GATracker.class, "ga");
public GATracker(@NotNull Context context, @NotNull GAConfig config,
@NotNull AccountService accountService)
{
_context = context;
_accountService = accountService;
GoogleAnalytics googleAnalytics = GoogleAnalytics.getInstance(context);
googleAnalytics.setAppOptOut(config.getAppOptOut());
googleAnalytics.setDryRun(config.isDryRunEnabled());
googleAnalytics.getLogger().setLogLevel(config.getLogLevel());
_tracker = googleAnalytics.getTracker(config.getTrackerId());
Integer dispatchPeriod = _context.getResources().getInteger(R.integer.ga_dispatchPeriod);
GAServiceManager.getInstance().setLocalDispatchPeriod(dispatchPeriod);
setUserDetails();
addUserProfileListeners(accountService);
}
private void addUserProfileListeners(final AccountService accountService)
{
accountService.addProfileFetchedListener(new EventHandler<CompleteProfile>()
{
@Override
public void run(CompleteProfile result)
{
setUserDetails();
}
});
}
private void setUserDetails()
{
if (_accountService.hasSignedIn()) {
String currGuid = _tracker.get(Fields.CLIENT_ID);
String guid = _accountService.getGuid().orNull();
if (!Strings.isNullOrEmpty(guid) && !guid.equals(currGuid)) {
_tracker.set(Fields.CLIENT_ID, guid);
_tracker.set(UID, guid);
_tracker.set(Fields.customDimension(DIM_USER_DOMAIN),
_accountService.getEmail().get().getDomainName());
}
}
}
protected void sendEvent(String category, String action, @Nullable String label,
@Nullable Long value)
{
_tracker.send(MapBuilder.createEvent(category, action, label, value).build());
}
private String getString(int resId)
{
String string = null;
try {
string = _context.getString(resId);
} catch (NotFoundException e) {
_logger.error("Could not find name for resource id:{}", resId);
}
return string;
}
}
ITracker.java
package to.go.app.analytics.ga;
public interface ITracker
{
void sendEvent(int category, int action);
void sendEvent(int category, int action, int label);
void sendEvent(int category, int action, int label, Long value);
void sendScreen(int screenName);
}
Now i am using this GATracker inside another class like this :
if (gaEnabled) {
_gaReporter = new GATracker(getApplicationContext(), ConfigBuilder.getGAConfig(),
_accountService)
{
public void sendEvent(int category, int action)
{
sendEvent(getString(category), getString(action), null, null);
}
public void sendEvent(int category, int action, int label)
{
sendEvent(getString(category), getString(action), getString(label), null);
}
public void sendEvent(int category, int action, int label, Long value)
{
sendEvent(getString(category), getString(action), getString(label), value);
}
public void sendScreen(int screenName)
{
String name = getString(screenName);
if (name != null) {
_tracker
.send(MapBuilder.createAppView().set(Fields.SCREEN_NAME, name).build());
}
}
};
} else {
_gaReporter = new GATracker(getApplicationContext(), ConfigBuilder.getGAConfig(),
_accountService)
{
@Override
public void sendEvent(int category, int action)
{
}
@Override
public void sendEvent(int category, int action, int label)
{
}
@Override
public void sendEvent(int category, int action, int label, Long value)
{
}
@Override
public void sendScreen(int screenName)
{
}
};
}
This seems a lot of code to write.. Is there any optimal way to achieve the same thing i am doing above or better way.. or is the above way ok ?
Thanks
I'm sure there is a better way of doing this, however, can you explain a bit more about what it is that this code is trying to do? My assumption is that, based on the value of "gaEnabled" you want to construct an object that either sends events (gaEnabled == true) or does nothing (gaEnabled == false), would that be correct?
ASKER
yes your understanding is correct.
ASKER
Hi Mccarl
Any ways to optimize the above code ?
Any ways to optimize the above code ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.