Alin
Alin

Reputation: 14581

Disable Crashlytics/Answers based on user setting

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

Answers (2)

natez0r
natez0r

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

joelreeves
joelreeves

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

Related Questions