Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically collect analytics events for button click in android?

I have to implement Automatic Event Tracking in android

Need to automatically collect analytics data on all button clicks and page views but it has to be done in a generic way so that I don't need to write the Analytics code again for every click.

Example: I have 2 buttons on my activity each of them having a click listener. Now i want to call Analytics.track(String buttonName) so that i do not have to add this in every click listener. The data that should be passed in tracking is button Name.

like image 633
user1721936 Avatar asked Feb 08 '16 09:02

user1721936


People also ask

Can Google Analytics track a button click?

If lead generation is a goal of your website, then you'll want to track how often the button on your contact or lead generation forms is clicked. Google Analytics doesn't track a click on a button out of the box.


1 Answers

A way (probably not the ultimate way) to do that could be extending Button (or View), and putting analytics code into the View#performClick() method.

As for the buttonName, it can be a a field of your custom View class, that you can set programmatically or even via an XML custom attribute.

Global implementation :

  • Create a custom XML attribut : create a file named attrs.xml in the ressource folder :

    <resources>
        <declare-styleable name="tracking">
            <attr name="tracking_name" format="string" />
        </declare-styleable>
    </resources>
    
  • Create a custom Button (or View) class, that overwrite performClick() method and call Analytics.track() with the string gotten from your XML custom attribute or set programmatically :

    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;
        }
    }
    
  • Use your new class everywhere you want to track the event, for example in a layout file :

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

Once again, this is one way, may be some other easier/better/better with your implementation ways :)

like image 130
meynety Avatar answered Oct 15 '22 13:10

meynety