Screens in Google Analytics represent content users are viewing within an Mobile app. Measuring screen views allows you to see which content is being viewed most by your users, and how are they are navigating between different pieces of content.

Now let’s start with quick integration.

Get a Property Id

The very first step is to create a new Google Analytics Property for our application or use an existing one. Just head to Analytics website and register a new Mobile Application, you will need the property id for the configuration settings (UA-XXXXXXX-Y).

Android Manifest

We need to add Internet permissions

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

And the following meta-data in the application tag:

    <application>
        ....
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
         ...
    </application>

I also load the global GA config from an xml file so it needs to be added as another meta-data tag:

    <meta-data
       android:name="com.google.android.gms.analytics.globalConfigResource"
       android:resource="@xml/analytics_global_config" />

XML config files

Lets define the global settings for GA. Create a new xml resource directory in /res for the xml files (if you don’t have it already) and create analytics_global_config.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <resources>
        <string name="ga_appName">APP_NAME</string>
        <string name="ga_appVersion">APP_VERSION</string>
        <!--Remember to disable verobse log level for production -->
        <string name="ga_logLevel">verbose</string>
        <integer name="ga_dispatchPeriod">1000</integer>
        <bool name="ga_dryRun">false</bool>
    </resources>

These are the basic settings I use, feel free to change them. Check the docs for more info.

Now we have the global settings file but we also need our application tracker configuration file.
Create another xml named app_tracker.xml with the following content:

    <?xml version="1.0" encoding="utf-8"?>
      <resources>
          <integer name="ga_sessionTimeout">300</integer>

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

          <!-- The screen names that will appear in reports -->
          <screenName name="com.test.MainActivity">
              MainActivity
          </screenName>
          <screenName name="com.test.SecondActivity">
              SecondActivity
          </screenName>
          <!--  The following value should be replaced with correct property id. -->
          <string name="ga_trackingId">UA-XXXXXXX-Y</string>
      </resources>

There are a couple of important points here:

  1. The screenName tags are the Activities names that we will see in our GA dasboard for the defined Activities.
  2. The ga_autoActivityTracking will send a screenView each time an activity is created automatically but I recommend sending them manually for more control (I’ll explain how later).

Java code

The Best approach here is to create a new Class that extends Application in order to access our tracker from anywhere of our code. So create a new file (in this sample I’m using MyApp.java)

public class MyApp extends Application {

    HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();

    public synchronized Tracker getTracker(TrackerName trackerId) {

        if (!mTrackers.containsKey(trackerId)) {
            GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
            Tracker t = analytics.newTracker(R.xml.app_tracker);
            mTrackers.put(trackerId, t);
        }
        return mTrackers.get(trackerId);
    }

    public enum TrackerName {
        APP_TRACKER,
        GLOBAL_TRACKER,
        E_COMMERCE_TRACKER,
    }
}

Note that I’ve left the HashMap and TrackerName sample code in case someone need to use different tracker configurations but I’ll be using just one(the one deffined in the app_tracker.xml).

We also need to add the application:name tag with the class name to the Android Manifest:

    <application
        android:name=".MyApp"
        .... >
            ....
    </application>

And create/init the tracker in your onCreate method of the Main Activity like so:

    Tracker t = ((MyApp) getApplication()).getTracker(MyApp.TrackerName.APP_TRACKER);

Sending ScreenView

For sending new ScreenViews for Activities, Fragments, Dialogs etc… just use the following code in the onCreate (or any other method):

    Tracker t = ((MyApp) getApplication()).getTracker(MyApp.TrackerName.APP_TRACKER);
    t.setScreenName("MainActivity");
    t.send(new HitBuilders.ScreenViewBuilder().build());

To see if it’s working correctly just check the Logcat since we enabled the Verbose mode in the global config. You should see something like this:

11-16 03:14:37.359  32560-32592/com.test V/GAV4﹕ Thread[GAThread,5,main]: Sending hit to service   PATH: https:  PARAMS: ul=en-us,  ht=1416104072499,  sr=1080x1920,  a=2111314142,  sc=start,  aid=com.test,  cid=5f81f0ec-7136-4825-a3d0-7b21905fbb50,  av=1.0,  v=1,  adid=5d6c9236-7265-4513-9bf9-59093326878b,  t=screenview,  an=MyApp,  ate=0,  tid=UA-XXXXXXX-Y,  _u=.8,  cd=MainActivity  

Sending Events

It’s also very usefull to track other events, like button clicks, so we can always know what users are using most. To do so we need to create an event with the HitBuilder:

        Tracker t = ((MyApp) getApplication()).getTracker(MyApp.TrackerName.APP_TRACKER);
        t.send(new HitBuilders.EventBuilder()
                .setCategory("Button")
                .setAction("Click")
                .setLabel("CoolButton")
                .build());

Conclusion Thought

That’s all ! we had successfully Changed Google Analytics Screen view Name on your Mobile App Project. If you have any difficulty while setting it up, feel free to ask in the comments section below.

 


Yatindra Sahu

Data Enthusiast

Related Posts

Digital Marketing

What is Google Data Studio ?

You’re probably wondering what Google Data Studio is, why it’s important, and how it’s different from using Google Analytics. Google Data Studio is a platform where dashboards can be created, easy to read, share and Read more…

Google Analytics Dashboard

GTM Installation On a WordPress Website

Google Tag Manager (GTM) Installation On a Website Often website owners need to add multiple code snippets to track their conversions, traffic, and gather other analytical data. With Google Tag Manager you can easily add Read more…

Google Analytics Dashboard

What is Google Analytics

Whether you’re managing a website for a large corporation or a small business, Google Analytics—the industry standard for web analytics—can help you find out how your website is performing. This powerful platform provides you with Read more…