1

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());
Community
  • 1
  • 1
CodeMonkey
  • 11,196
  • 30
  • 112
  • 203

1 Answers1

2

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.

hidro
  • 12,333
  • 6
  • 53
  • 53
  • OK it seems to work. But if I need to add code in each and every activity to enable the reporting then where is the "automated" part in "automated screen viewing reporting"? And why would I stop the reporting? Isn't a reporting just a one time thing it sends to the GA server which lets me know that someone is viewing the screen and that's it? – CodeMonkey May 10 '15 at 12:13
  • I think it could be some legacy issue that prevents this to work for API before 14? – hidro May 10 '15 at 12:21
  • and regarding the second question - why would I stop the reporting? Isn't a reporting just a one time thing it sends to the GA server which lets me know that someone is viewing the screen and that's it? – CodeMonkey May 10 '15 at 12:25
  • I believe it's to differentiate sessions. – hidro May 10 '15 at 12:29