Yonatan Nir Yonatan Nir -4 years ago 159
Android Question

GoogleAnalytics enableAutoActivityTracking not working

I got the same problem as in this question:

enableAutoActivityTracking not automatically tracking activities?

The problem is that the automated tracking does not work using Google Analytics.

However, calling

enableAutoActivityReports
does not work in my case.

This is the configuration XML file:

<?xml version="1.0" encoding="utf-8"?>




<!-- Google Analytucs property id. -->

<integer name="ga_sessionTimeout">300</integer>

<!-- catch and report uncaught exceptions from the app -->
<bool name="ga_reportUncaughtExceptions">true</bool>

<!-- Enable automatic Activity measurement -->
<bool name="ga_autoActivityTracking">true</bool>

<!-- The screen names that will appear in reports -->
<screenName name=".LoginActivity">
Login Activity
</screenName>

</resources>


In the application class:

private Tracker googleAnalyticsTracker;

public synchronized Tracker getGoogleAnalyticsTracker()
{
if (googleAnalyticsTracker == null)
{
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
analytics.enableAutoActivityReports(this);
googleAnalyticsTracker = analytics.newTracker(R.xml.global_tracker);
}
return googleAnalyticsTracker;
}


In the activity's
onCreate
:
((MyApplication) getApplication()).getGoogleAnalyticsTracker();


However, in the same activity, if I put a manual screen view sending, I DO see it in the GA console. I do it like this:

Tracker t = ((MyApplication) getApplication()).getGoogleAnalyticsTracker();
t.setScreenName("Login Screen");
t.send(new HitBuilders.ScreenViewBuilder().build());

Answer Source

You need to report starting and stopping of each activity.

public abstract class TrackableActivity extends AppCompatActivity {
    @Override
    protected void onStart() {
        super.onStart();
        GoogleAnalytics.getInstance(this).reportActivityStart(this);
    }

    @Override
    protected void onStop() {
        GoogleAnalytics.getInstance(this).reportActivityStop(this);
        super.onStop();
    }
}

I couldn't find the documentation that specifies this anymore. I recall I used to follow that. Anyway, from API documentation, this will be required if you want to auto track activity for API lower than 14:

public void reportActivityStart (Activity activity)

If auto activity reports are enabled (see enableAutoActivityReports(Application)) on a device running API level 14 or above, this method will be a noop.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download