Reputation: 14581
I am adding a new preference on my app, to allow the user to opt out of analytics reports. I am using in my app Crashlytics and Answers by Fabric.
I have this code within onCreate:
if (PreferenceHelper.getAllowAnalytics(context)) {
Fabric.with(this, new Crashlytics());
} else {
//no crash or answers to be sent
}
}
and each time I want to send an event I do it like this:
Answers.getInstance().logCustom(new CustomEvent("test event"));
This works well, when the user allows it.
How should I handle the else
when the user decides not to allow analytic?
Upvotes: 1
Views: 1156
Reputation: 1064
You could wrap all of your calls to Answers.getInstance in a new class that checks your preference and does nothing if it's not enabled? IE: Instead of Answers.getInstance().logCustom() WrappedAnswers.getInstance().logCustom(). And wrapped answers would do your if/else check in logCustom()
Upvotes: 0
Reputation: 1954
Could do something like this without using an if/else:
CrashlyticsCore core = new CrashlyticsCore.Builder().disabled(!PreferenceHelper.getAllowAnalytics(context)).build();
Fabric.with(this, new Crashlytics.Builder().core(core).build());
This way if your getAllowAnalytics method returns false, then Fabric/Crashlytics will be disabled, otherwise it will be enabled.
Upvotes: 2