Using Google Analytics To Track Fragments

后端 未结 6 1922
我寻月下人不归
我寻月下人不归 2020-12-04 15:39

Just need to know the proper way to implement Google analytics to track when a user is on a fragment in real time this is what is do now

@Override
public voi         


        
6条回答
  •  心在旅途
    2020-12-04 16:16

    with android google analytics v4

    i tried this and it worked

    refering this 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 = "" + "

    " + fFeed.getItem(fPos).getDescription()+"

    "+""; //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; } }

提交回复
热议问题