在实时运行期间禁用Google Analytics跟踪

时间:2022-02-27 15:16:27

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.

根据Google分析的条款,您需要为用户提供选择退出跟踪的选项。但我觉得,文档不清楚如何做到这一点。这就是我到目前为止所得到的。

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?

如果不是,如何在用户设备上运行期间完全禁用跟踪?

2 个解决方案

#1


You need to use GoogleAnalytics.getInstance(this).setAppOptOut(true);.

您需要使用GoogleAnalytics.getInstance(this).setAppOptOut(true);.

In Google Analytics documentation(1), there is simple example with shared preferences.

在Google Analytics文档(1)中,有一个使用共享首选项的简单示例。

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)

注意:这与在测试/开发期间禁用Google Analytics不同。如果您的目标是在测试/开发期间禁用,请使用(2)

GoogeAnalytics.getInstance(this).setDryRun(true);

#2


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

您可以设置SharedPreference设置,如果用户选择退出Google Analytics跟踪,则不会发送Google Analytics更新。

#1


You need to use GoogleAnalytics.getInstance(this).setAppOptOut(true);.

您需要使用GoogleAnalytics.getInstance(this).setAppOptOut(true);.

In Google Analytics documentation(1), there is simple example with shared preferences.

在Google Analytics文档(1)中,有一个使用共享首选项的简单示例。

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)

注意:这与在测试/开发期间禁用Google Analytics不同。如果您的目标是在测试/开发期间禁用,请使用(2)

GoogeAnalytics.getInstance(this).setDryRun(true);

#2


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

您可以设置SharedPreference设置,如果用户选择退出Google Analytics跟踪,则不会发送Google Analytics更新。