16

With the release of Google Analytics v4 (Android), what is the recommended way to track Fragment views? Is this solution still the recommended way - https://stackoverflow.com/a/19284014/413254?

The sample in the docs (https://developers.google.com/analytics/devguides/collection/android/v4/#analytics-xml) has the following config:

global_tracker.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="ga_sessionTimeout">300</integer>
    <bool name="ga_autoActivityTracking">true</bool>
    <screenName name="com.google.android.gms.analytics.samples.mobileplayground.ScreenviewFragment">
        AnalyticsSampleApp ScreenView
    </screenName>
    <screenName name="com.google.android.gms.analytics.samples.mobileplayground.EcommerceFragment">
        AnalyticsSampleApp EcommerceView
    </screenName>
    <!--  The following value should be replaced with correct property id. -->
    <string name="ga_trackingId">UA-XXXXXXX-Y</string>
</resources>

This configuration looks to be enabling auto tracking for Activities, but I'd assume this doesn't work any magic for Fragments? In this example, I'd assume the "AnalyticsSampleApp ScreenView" screen event will be sent if t.setScreenName(path); is called and path is "com.google.android.gms.analytics.samples.mobileplayground.EcommerceFragment"?

Community
  • 1
  • 1
loeschg
  • 29,961
  • 26
  • 97
  • 150

2 Answers2

5

Yes, you'll have to use the solution described in the link you posted. The main reason for this is because the lifetime of fragments is not as straightforward as that of Activities. Android does not provide callbacks for fragment lifecycle.

What you should do is set the fragment identifier as the screen name whenever the fragment is shown. In the sample app, if you look at MobilePlayground.java, you'll see onTabSelected. In the sample app, this function is called whenever the screen changes. That would be a good place to set screen and possibly send screenview/appview hits.

Let me know if you want more detailed examples.

Avi
  • 2,373
  • 17
  • 22
  • With the global_tracker.xml String path = "com.google.android.gms.analytics.samples.mobileplayground.EcommerceFragment"; t.setScreenName(path); The code above is executed in OnActivityCreated of the EcommerceFragment. "AnalyticsSampleApp ScreenView" screen event is supposed to be reported in the Google Analytic Report, however the actual event report is show as com.google.android.gms.analytics.samples.mobileplayground.EcommerceFragment – worawee.s Apr 11 '14 at 08:08
  • It seems to be that, the mapping in configuration xml is not working. What is the right way to make this work properly? – worawee.s Apr 11 '14 at 08:08
  • 2
    I am not sure I understand. If you are seeing com.google.android.gms.analytics.samples.mobileplayground.EcommerceFragment, that is probably because it is a fragment and not an Activity. The xml file currently only supports Activities because of the APIs that Android supports. – Avi Apr 11 '14 at 19:05
  • Thanks @Avi, and could you please update this document https://developers.google.com/analytics/devguides/collection/android/v4/screens#automatic. It uses Home instead of which is quite confusing. The tag should be the right one from what we discuss in this topic. – worawee.s Apr 13 '14 at 00:40
  • 4
    @Avi But you still use fragments in tags and there seems to be no way to get this info back when dealing with fragments screen views. It's massively misleading. See: https://developers.google.com/analytics/devguides/collection/android/v4/?hl=pl#tracking-methods (3) – Michał Klimczak Oct 20 '14 at 09:30
0

Use this code from Google's docs.

package com.google.android.apps.mobileplayground;

import com.google.android.apps.mobileplayground.AnalyticsSampleApp.TrackerName;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

/**
 * Class to exercise Event hits.
 */
public class EventFragment extends Fragment {

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    View view = inflater.inflate(R.layout.event, container, false);

    setupEvent(view, R.id.video1Play, R.string.videoCategory, R.string.videoPlay, R.string.video1);
    setupEvent(view, R.id.video1Pause, R.string.videoCategory, R.string.videoPause,
        R.string.video1);
    setupEvent(view, R.id.video2Play, R.string.videoCategory, R.string.videoPlay, R.string.video2);
    setupEvent(view, R.id.video2Pause, R.string.videoCategory, R.string.videoPause,
        R.string.video2);

    setupEvent(view, R.id.book1View, R.string.bookCategory, R.string.bookView, R.string.book1);
    setupEvent(view, R.id.book1Share, R.string.bookCategory, R.string.bookShare, R.string.book1);

    final Button dispatchButton = (Button) view.findViewById(R.id.eventDispatch);
    dispatchButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        // Manually start a dispatch (Unnecessary if the tracker has a dispatch interval)
        GoogleAnalytics.getInstance(getActivity().getApplicationContext()).dispatchLocalHits();
      }
    });
    return view;
  }

  private void setupEvent(View v, int buttonId, final int categoryId, final int actionId,
      final int labelId) {
    final Button pageviewButton = (Button) v.findViewById(buttonId);
    pageviewButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        // Get tracker.
        Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(
            TrackerName.APP_TRACKER);
        // Build and send an Event.
        t.send(new HitBuilders.EventBuilder()
            .setCategory(getString(categoryId))
            .setAction(getString(actionId))
            .setLabel(getString(labelId))
            .build());
      }
    });
  }
}
Marco Altran
  • 376
  • 2
  • 9