Вопрос

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"?

Это было полезно?

Решение

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.

Другие советы

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());
      }
    });
  }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top