[Java] 使用Google Analytics(分析)跟踪片段


Answers

这是一个使用FragmentActivity和fragment的例子:

  1. 在值文件夹中创建XML文件( values/analytics.xml ):

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <!-- Replace placeholder ID with your tracking ID -->
        <string name="ga_trackingId">XX-xxxxxxxx-x</string>
    
        <!-- Enable Activity tracking -->
        <bool name="ga_autoActivityTracking">true</bool>
    
        <!-- Enable debug -->
        <bool name="ga_debug">true</bool>
    
        <!-- The screen names that will appear in your reporting -->
        <string name="com.example.myapp.FragmentActivity">Fragment activity</string>
    
        <!--
            The inverval of time after all the collected data
            should be sent to the server, in seconds.
        -->
        <integer name="ga_dispatchPeriod">20</integer>
    
    </resources>
  2. 在你的FragmentActivity类中,添加这个:

    @Override
    protected void onStart() {
        super.onStart();
        EasyTracker.getInstance().setContext(this.getBaseContext());
        EasyTracker.getInstance().activityStart(this); // Add this method
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        EasyTracker.getInstance().activityStop(this); // Add this method
    }
  3. 在包中创建新的类: TrackedFragment.java

    public class TrackedFragment extends Fragment {
        private Tracker tracker;
        private String activityId;
        private String fragmentId;
    
        @Override
        public void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            EasyTracker.getInstance().setContext(getActivity().getApplicationContext());
            this.tracker = EasyTracker.getTracker();
            this.fragmentId = getClass().getSimpleName();
            this.activityId = getActivity().getClass().getSimpleName();
        }
    
        @Override
        public void onResume() {
            super.onResume();
            this.tracker.sendView("/" + this.activityId + "/" + this.fragmentId);
        }
    }
  4. 最后,你的片段应该从TrackedFragment扩展如下:

    public class NewFragment extends TrackedFragment {
    
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.newfragment, null);
        }
    
    }
Question

只需要知道实施Google分析的正确方法,以便实时跟踪用户何时处于碎片状态

@Override
public void onResume() {
    super.onResume();
    Tracker myTracker = parentActivity.getTracker();
    myTracker.setCustomMetric(1, (long) 1);               
    myTracker.sendView("Music View"); 
}

getTracker类在我的主要活动中,只是返回主活动中的跟踪器实例

任何帮助将非常感激!




V3的另一种方法(因为onResume()与Activity相关,而不是Fragment。当父/子关系众所周知时,这很有效。

父片段发送onStart()初始事件:

public class ParentFragment extends Fragment {
    private Tracker mTracker;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mTracker = EasyTracker.getInstance(getActivity());
    }

    @Override
    public void onStart() {
        super.onStart();

        mTracker.set(Fields.SCREEN_NAME, "Parent Fragment");
        mTracker.send(MapBuilder.createAppView().build());
    }
}

子片段覆盖onStart()onStop()

public class ChildFragment extends Fragment {
    private Tracker mTracker;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mTracker = EasyTracker.getInstance(getActivity());
    }

    @Override
    public void onStart() {
        super.onStart();

        mTracker.set(Fields.SCREEN_NAME, "Child Fragment");
        mTracker.send(MapBuilder.createAppView().build());
    }

    @Override
    public void onStop() {
        super.onStop();

        mTracker.set(Fields.SCREEN_NAME, "Parent Fragment");
        mTracker.send(MapBuilder.createAppView().build());
    }
}



与Android谷歌分析V4

我试过这个,它的工作

引用此https://developers.google.com/analytics/devguides/collection/android/v4/events

import java.net.URLEncoder;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Xml.Encoding;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ScrollView;
import android.widget.TextView;
import com.Blog.gkgyan.AnalyticsSampleApp.TrackerName;
import com.Blog.gkgyan.parser.RSSFeed;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
public class DetailFragment extends Fragment {
    private int fPos;
    RSSFeed fFeed;
    String country;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fFeed = (RSSFeed)getArguments().getSerializable("feed");
        fPos = getArguments().getInt("pos");
        Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(
                TrackerName.APP_TRACKER);
            // Build and send an Event.
            t.send(new HitBuilders.EventBuilder()
                .setCategory(fFeed.getItem(fPos).getTitle())
                .setAction("viewpager click")
                .setLabel("viewpager label")
                .build());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.detail_fragment, container, false);
      // Initializr views
        TextView title = (TextView)view.findViewById(R.id.title);
        WebView desc = (WebView)view.findViewById(R.id.desc);
        // Enable the vertical fading edge (by default it is disabled)
        ScrollView sv = (ScrollView)view.findViewById(R.id.sv);
        sv.setVerticalFadingEdgeEnabled(true);

        // Set the views
        desc.getSettings().setJavaScriptEnabled(true);
        title.setText(fFeed.getItem(fPos).getTitle());

        country = "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"><style type=\"text/css\">p{text-align:justify;font-size:125%;}</style></head><body>" + "<p>" + fFeed.getItem(fPos).getDescription()+"</p>"+"</body></html>";
        //desc.loadData( country, "text/html", "UTF-8");
        //desc.loadData( country,  "text/html; charset=utf-8", "utf-8");
        desc.loadData( URLEncoder.encode(country).replaceAll("\\+", " "), "text/html", Encoding.UTF_8.toString());
        return view;
      }
}