Search code examples
androidgoogle-analyticsgoogle-analytics-apigoogle-analytics-firebase

When is the ga_autoActivityTracking fired?


It is possible to track all Activities with Google Analytics automatically, when you enable the ga_autoActivityTracking attrubute in the tracker config.

So I'm asking myself when this action gets fired, because I want to exclude it on a special time.

Is it fired when some of the onCreate, onResume or other methods of a Activity getting called?

I'm starting the tracking in MyApplications onCreate() methode:

@Override
    public void onCreate(){
        if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this)== ConnectionResult.SUCCESS) {
            // Get tracker.
            Tracker t = getTracker(
                    MyApplication.TrackerName.APP_TRACKER);

            // Send screen view.
            t.send(new HitBuilders.AppViewBuilder().build());
        }
    }

and initialize the tracker with the following file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Enable automatic Activity measurement -->
    <bool name="ga_autoActivityTracking">true</bool>

    <!-- The screen names that will appear in reports -->
    <screenName name="de.example.app.MainActivity">
        MainActivity
    </screenName>
    ...
    <string name="ga_trackingId">UA-59000000-1</string>
</resources>

Solution

  • Setting ga_autoActivityTracking to true in your tracker configuration xml is equivalent of calling tracker.enableAutoActivityTracking(true) when you are initializing your tracker. When auto activity tracking is enabled an screen view event will be send on activity start (when the Application.ActivityLifecycleCallbacks.onActivityStarted is called). The screen view name will be derived from the activity class name by default. You can define custom screen view name in your tracker xml config by mapping an Activity class name to your preferred screen name. Once you enable auto activity tracking all activities your app will send screen view hit on Activity onStart callback.

    In your code you are checking for Google Play service availability. Generally you should not need to do that. Google Analytics will happily function without Google Play service on the device (for example on Amazon Fire phone device). While the Google Analytics SDK is part of the Google Play library the presence of Google Play service on the device is optional.

    In your code you also are sending screen view on application creation (App view is the same as screen view). Android application can be created for non-interactive reasons like receiving a broadcast request. When you send a screen view from Applicaion.onCreate any application creation will appear as if an user has started your application. This include non-interactive application start in your active user number and will inflate the reported value. Instead, you should create and configure your tracked inside the Application onCreate but only send screen views when your activity becomes visible (onStart callback is a good place for that or enable automatic activity tracking). Creating and configuring your tracker early on is especially important if you enable exception reporting as only exceptions that occur after you create the tracker will be reported. You should note that enabling auto activity tracking tracks automatically only Activities, not fragments. You will need to manually send screen view hits from your fragments if then need to be tracked.