如何在Android中自动收集按钮单击的分析事件?

用户名

我必须在android中实现自动事件跟踪

需要自动收集所有按钮单击和页面浏览的分析数据,但是必须以一种通用的方式来完成,这样我就不必为每次单击再次编写Analytics代码。

示例:我的活动中有2个按钮,每个按钮都有一个点击侦听器。现在,我想调用Analytics.track(String buttonName),这样我就不必在每个单击侦听器中都添加它。跟踪中应传递的数据是“名称”按钮。

中度

一种可能的方法(可能不是最终方法)可以扩展Button(或View),然后将分析代码放入该View#performClick()方法中。

至于buttonName,它可以是您自定义View类的一个字段,您可以通过编程方式甚至通过XML自定义属性进行设置。

全球实施:

  • 创建一个自定义XML属性:创建一个attrs.xml在ressource文件夹中命名的文件:

    <resources>
        <declare-styleable name="tracking">
            <attr name="tracking_name" format="string" />
        </declare-styleable>
    </resources>
    
  • 创建一个自定义Button(或View)类,该类将覆盖performClick()方法并Analytics.track()使用从XML自定义属性中获取或通过编程方式设置的字符串进行调用

    public class TrackedClickButton extends Button {
    
        private String mTrackingName;
    
        public TrackedClickButton(Context context) {
            super(context);
        }
    
        public TrackedClickButton(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context, attrs);
        }
    
        public TrackedClickButton(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init(context, attrs);
        }
    
        @TargetApi(21)
        public TrackedClickButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
            init(context, attrs);
        }
    
        private void init(Context context, AttributeSet attrs) {
            TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.tracking);
    
            if (array.hasValue(R.styleable.tracking_name)) {
                mTrackingName = array.getString(R.styleable.tracking_name);
            }
        }
    
        public void setTrackingName(String trackingName) {
            this.mTrackingName = trackingName;
        }
    
        @Override
        public boolean performClick() {
            //Make sure the view has an onClickListener that listened the click event,
            //so that we don't report click on passive elements
            boolean clickHasBeenPerformed = super.performClick();
            if(clickHasBeenPerformed && mTrackingName != null) {
                Analytics.track(mTrackingName);
            }
            return clickHasBeenPerformed;
        }
    }
    
  • 在要跟踪事件的任何地方使用新类,例如在布局文件中:

    <com.heysolutions.dentsply.Activites.MainActivity.TrackedClickButton
        xmlns:tracking="http://schemas.android.com/apk/res-auto"
        android:id="@+id/button"
        android:layout_width="50dp"
        android:layout_height="50dp"
        tracking:tracking_name="buttonTrackingName"/>
    

再一次,这是一种方式,可能与您的实施方式相比更容易/更好/更好:)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Android Studio中添加按钮单击事件

单击同一收集单元中的按钮时如何在收集单元中获取标签

如何在android analyitcs api v4中跟踪按钮单击事件

如何在按钮单击事件中获取图像?

如何在表中设置按钮单击事件的状态

如何在 Xamarin 中调用按钮单击事件

如何在JavaScript中单击带有事件的按钮?

如何在Python中获取多个按钮单击事件

如何从单击事件中获取有关自动生成的按钮的特定信息

如何在Android中获取群集标记单击事件

如何在Android WebView中启用jQuery单击事件

如何在angular2中更改按钮文本并在单击事件上禁用按钮

如何从按键事件中单击按钮

如何自动单击javascript中的按钮?

如何在Android中单击按钮更改笔的颜色

如何在Android中单击按钮隐藏和显示密码?

如何在android中单击按钮时更改ImageView?

如何在Android应用中多次单击按钮并更改模式

如何在Android中单击多个按钮以打开相机?

如何在WebView Android中检测按钮单击?

如何在Android中单击/使用动态创建的按钮

如何在HTML中的单击事件上通过下拉按钮从Django中调用外部python脚本

如何在ionic 2 angular 2中的单击事件中获取按钮的id和名称值

如何在C#中的program.cs中处理按钮单击事件

如何在 React JS 中访问按钮单击事件中的数据属性?

如何将按钮单击事件传递给Android 中PagedList 中的Activity?

如何在swift4中单击按钮时触发文本框的editchanged事件

如何在GridView侧面的按钮单击事件中隐藏GridView列

如何在Universal Windows Platform Apps中更改“单击事件”上按钮的背景颜色?