使用 Google Analytics 跟踪片段 [英] Using Google Analytics To Track Fragments

查看:28
本文介绍了使用 Google Analytics 跟踪片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只需要知道实施 Google 分析以实时跟踪用户何时在片段上的正确方法,这就是现在要做的

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 void onResume() {
    super.onResume();
    Tracker myTracker = parentActivity.getTracker();
    myTracker.setCustomMetric(1, (long) 1);               
    myTracker.sendView("Music View"); 
}

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

the getTracker class is in my main activity and just returns the instance of tracker in the main activity

任何帮助将不胜感激!

推荐答案

Mochini 的回答使用了 Google Analytics V2.波纹管你可以看到如何在 V4 和 V3 上做到这一点:

Mochini's answer uses Google Analytics V2. Bellow you can see how to do it on V4 and V3:

  • V4:

应用:

public class YourApplication extends Application
{
    public synchronized Tracker getTracker() {

        try {
            final GoogleAnalytics googleAnalytics = GoogleAnalytics.getInstance(this);
            return googleAnalytics.newTracker(R.xml.analytics);

        }catch(final Exception e){
            Log.e(TAG, "Failed to initialize Google Analytics V4");
        }

        return null;
    }
}

res/xml/analytics.xml(你可以任意命名,不需要叫analytics")

res/xml/analytics.xml (you can name it anything, it does not need to be called "analytics")

<?xml version="1.0" encoding="utf-8" ?>
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="TypographyDashes">

  <!--Replace placeholder ID with your tracking ID-->
  <string name="ga_trackingId">UA-XXXXXXXX-X</string>

  <!--Enable automatic activity tracking-->
  <bool name="ga_autoActivityTracking">true</bool>

  <!--Disable automatic exception tracking-->
  <bool name="ga_reportUncaughtExceptions">false</bool>

</resources>

build.gradle:

compile 'com.google.android.gms:play-services:7.3.0'

片段超类:

public abstract class TrackedFragment extends Fragment{

    @Override
    public void onResume() {

        super.onResume();

        final Tracker tracker = yourApplicationInstance.getTracker();
        if(tracker != null){

            tracker.setScreenName(getClass().getSimpleName());
            tracker.send(new HitBuilders.ScreenViewBuilder().build());
        }
    }
}

  • V3

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    
    import com.google.analytics.tracking.android.EasyTracker;
    import com.google.analytics.tracking.android.Fields;
    import com.google.analytics.tracking.android.MapBuilder;
    import com.google.analytics.tracking.android.Tracker;
    
    public abstract class TrackedFragment extends Fragment{
    
         private Tracker tracker;
    
         @Override
         public void onActivityCreated(final Bundle savedInstanceState) {
    
             super.onActivityCreated(savedInstanceState);
    
             this.tracker = EasyTracker.getInstance(getActivity());
         }
    
         @Override
         public void onResume() {
    
             super.onResume();
    
             this.tracker.set(Fields.SCREEN_NAME, getClass().getSimpleName());
             this.tracker.send( MapBuilder.createAppView().build() );
         }
    }
    

  • 来源:https://developers.google.com/analytics/devguides/collection/android/v3/migration

    这篇关于使用 Google Analytics 跟踪片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆