简体   繁体   中英

How to measure time in the app using Google Analytics

As I am reading through the poorly written documentation of Google Analytics for Android, I can't figure out how exactly to measure the session time in my app.

What I need exactly, is to know how much time a user spends on a given screen, for example I want to know how much time they spend on the Main Menu screen, and how much time in the Select Level screen.

First, here's how I initialize the Google Analytics:

analytics = GoogleAnalytics.getInstance(this);
tracker = analytics.newTracker(ANALYTICS_KEY);
tracker.enableExceptionReporting(true);
tracker.enableAdvertisingIdCollection(true);
tracker.enableAutoActivityTracking(true);
tracker.setAppName(appName);
tracker.setScreenName("screenName");
tracker.setTitle("title-" + "screenName");

I have 2 pieces of code, and I don' know which one would do that:

tracker.send(new HitBuilders.TimingBuilder()
       .setCategory("category-" + "screenName")
       .setValue(1)
       .setVariable("timing name-" + "screenName")
       .setLabel("label-" + "screenName")
       .build());

or

tracker.send(new HitBuilders.ScreenViewBuilder()
       .setCustomDimension(1, null)
       .build());

Which way would do the job I want?

Also, bonus points if you can explain what are these methods for:

setValue
setVariable
setCategory
setCustomDimension

you have to count the time yourself. Put this code inside your activity's onResume() method to get the time "screen" is drawn:

long startTime = System.currentTimeMillis();

Then on the onPause()

long endTime = System.currentTimeMillis();

tracker.send(new HitBuilders.TimingBuilder()
       .setCategory("Category Name")
       .setValue(endTime - startTime)
       .setVariable("Timing Name")
       .setLabel("Timing Label")
       .build());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM