Disable google analytics tracking during live run - android

According to the terms for Google analytics you need to provide the user with a option to opt out of the tracking. But I feel, documentation is not very clear on how to do that. This what I got so far.
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
Tracker t = analytics.newTracker(R.xml.tracker);
t.enableAdvertisingIdCollection(false);
t.enableAutoActivityTracking(false);
t.enableExceptionReporting(false);
Will this disable all tracking including events like this.
t.send(new HitBuilders.EventBuilder()
.setCategory("category")
.setAction("test")
.setLabel("label")
.build());
If not how do I completely disable tracking during run on user device?

You need to use GoogleAnalytics.getInstance(this).setAppOptOut(true);.
In Google Analytics documentation(1), there is simple example with shared preferences.
SharedPreferences userPrefs = PreferenceManager.getDefaultSharedPreferences(this);
userPrefs.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener () {
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(TRACKING_PREF_KEY)) {
GoogleAnalytics.getInstance(getApplicationContext()).setAppOptOut(sharedPreferences.getBoolean(key, false));
} else {
// Any additional changed preference handling.
}
}
});
Note: This is different from disabling Google Analytics during testing / development. If your aim is to disable during test/development, use (2)
GoogeAnalytics.getInstance(this).setDryRun(true);

You can set a SharedPreference setting and if the user opt-out Analytics tracking you don't send the Analytics update.

Related

Disable/Enable Firebase Crashlytics during runtime

With Google sending out waves of warnings about the need to let users opt-in to crash reporting (source) I'm trying to build an opt-in dialog for Firebase Crashlytics.
To be able to do this I would need to know whether Crashlytics is currently enabled (to decide whether to show the dialog or not). It seems CrashlyticsCore.disabled is responsible for tracking this but I haven't found any way to access the value because it's package private and doesn't have an accessor. How can I know during runtime if Firebase Crashlytics is enabled?
The second thing I'd need to do is disable crash reporting by default and enable it when the user agrees to opt-in. How I suspected to be able to do this:
AndroidManifest:
<meta-data
android:name="firebase_crashlytics_collection_enabled"
android:value="false" />
Activity:
boolean userAgrees = true;
CrashlyticsCore core = new CrashlyticsCore.Builder().disabled(!userAgrees).build();
Fabric.with(this, new Crashlytics.Builder().core(core).build());
throw new RuntimeException("why doesn't this show up in Firebase Crashlytics?");
Why isn't my RuntimeException showing up in Firebase Crashlytics?
P.S. Crashlytics is working fine when removing these attempts to create an opt-in dialog.
It seems toggling Crashlytics will only take effect after your app restarts. Fabric is a singleton and all calls to its Fabric.with method will simply be ignored after it's been initialized. Source code below.
public static Fabric with(Context context, Kit... kits) {
if (singleton == null) {
Class var2 = Fabric.class;
synchronized(Fabric.class) {
if (singleton == null) {
setFabric((new Fabric.Builder(context)).kits(kits).build());
}
}
}
return singleton;
}
One way around it is to keep track of the value yourself using SharedPreferences or other persistent storage methods and update your UI accordingly.

Debug Android Analytics event tracking - Analytics not picking up events

I've successfully added Analytics to an Android app and it is picking up session data just fine. However, I've also tried to track a specific event, but it does not seem to be sent to Analytics like the rest of the session data. I've followed the code suggested in the current Analytics documentation and still no luck, so I'm pasting here in the hopes that it's something obvious to more experienced folks. Many thanks in advance for any guidance.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_import);
user = getIntent().getStringExtra("user");
statusUpdateArea = (EditText)findViewById(R.id.dataStatusUpdateField);
progressBar = (ProgressBar)findViewById(R.id.progressBar);
progressBar.setVisibility(View.INVISIBLE);
welcomeMessage = (TextView)findViewById(R.id.WelcomeMessageTextView);
welcomeMessage.append(user);
// getting tracker & setting User ID field
AnalyticsApplication application = (AnalyticsApplication) getApplication();
mTracker = application.getDefaultTracker();
mTracker.set("&uid", user);
// build and send event
mTracker.send(new HitBuilders.EventBuilder()
.setCategory("User ID")
.setAction("Data login")
.setLabel(user)
.build());
...
maybe you need use
mTracker = application.getTracker();
and
mTracker.setScreenName("yourscreenname");
Seems you are calling send before calling build .
Create a tracker with all the event info then call build and once done call send function .
Have worked on webtrends analytics product so I know a thing or two about analytics

Implement of Google Analytic Android Privacy Policy And Opt-Out Policy in Android app

I search a lot then come to know how to implement Google Analytic Policy in your app, Please see my answer below, Here i explained Simplest way for beginner.
There are many different ways to compile your app with Google analytic policy , below is my way just sharing knowledge Please up-vote if you found this useful.
First add below lines into your Play Store App Description
To make "Your App Name" better,This application uses Google Analytics
to anonymously track usage data within the application.
String.xml
<string name="off">Off</string>
<string name="send_usage_statistics">Send anonymous usage statistics</string>
<string name="send_usage_summary">To make <Your App Name> better,This application uses Google Analytics to anonymously track usage data within the application.</string>
In your Prefrence - xml (prefrence.xml)
<CheckBoxPreference android:key="send_usage"
android:defaultValue="true"
android:title="#string/send_usage_statistics"
android:summaryOn="#string/send_usage_summary"
android:summaryOff="#string/off"/>
In Your PrefrenceActivity.java
private void initGoogleAnalytic(CheckBoxPreference checkBoxPreference){
checkBoxPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
CheckBoxPreference swp = (CheckBoxPreference)preference;
boolean isChecked=swp.isChecked();
GoogleAnalytics.getInstance(getApplicationContext()).setAppOptOut(isChecked);
// boolean isd=GoogleAnalytics.getInstance(getApplicationContext()).getAppOptOut();
return false;
}
});
}
Finally call above method as
initGoogleAnalytic((CheckBoxPreference) findPreference("send_usage"));

Android Google Tag Manager does not work

I'm having trouble while implemented the new google tag manager lib.
Here is what I did:
I create a macro dataleyr
I set a rule for an event
I create a tag and set the universal analytics ID on it
I publish my container
Then I tried to use it in my app like this
TagManager mTagManager = TagManager.getInstance(this);
DataLayer mDataLayer = com.google.android.gms.tagmanager.TagManager.getInstance(this).getDataLayer();
ContainerOpener.openContainer(
mTagManager, CONTAINER_ID, OpenType.PREFER_NON_DEFAULT,
TIMEOUT_FOR_CONTAINER_OPEN_MILLISECONDS, new ContainerOpener.Notifier() {
#Override
public void containerAvailable(Container container) {
container.refresh();
// Save container for use by any other activities in the app.
com.appsconceptelite.appsconceptelite.testfunctionnalities.gtm.ContainerHolder.setContainer(container);
mContainer = com.appsconceptelite.appsconceptelite.testfunctionnalities.gtm.ContainerHolder.getContainer();
Utils.pushOpenScreenEvent(LearnActivity.this, "Learn Screen");
}
});
and the method I use to push event is
/**
* Push an "openScreen" event with the given screen name. Tags that match that event will fire.
*/
public static void pushOpenScreenEvent(Context context, String screenName) {
DataLayer dataLayer = TagManager.getInstance(context).getDataLayer();
dataLayer.pushEvent("openScreen", DataLayer.mapOf("screenName", screenName));
}
When I run this code in debug mode, I get the right container name and version but when I check in Google Analytics dashbord, I see no events like if no data have been pushed.
Can you tell me what I'm doing wrong?
Not sure if it will solve the issue but what tripped me is the old google analytics account that hasn't been created a "Mobile apps" account and no results would be seen on the account but were definitely sent out. Create a new "Mobile apps" property and try again.

Google Analytics for tracking search values in android

In my android application, I have a search functionality. I would like to track the search keywords that my users search for. As far as I know, Google Analaytics could track events in my android app. I was wondering if it could do the same for search items.
If it is possible, a sample code or a link would be great!
That's mostly a question of how to segment your data in the Analytics web app, almost a science in itself.
Example:
EasyTracker.getTracker().sendEvent("Search", "Keywords", searchString, 1L);
Remember the basic usage of EasyTracker:
https://developers.google.com/analytics/devguides/collection/android/v2/
tracker = GoogleAnalyticsTracker.getInstance();
tracker.startNewSession("UA-YOUR-ACCOUNT-HERE", this);
After defining tracker, you should call the below method.
public static void trackEventGoogle(String category,String action,String label,int value)
{
try
{
GoogleAnalyticsTracker.getInstance().trackEvent(category,action,label,value);
}
catch (Exception e) {
exceptionLog(e);
}
}
I hope it helps. Don't forget to add analytics jar file to your project.

Categories

Resources