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


i have implement automatic event tracking in android

need automatically collect analytics data on button clicks , page views has done in generic way don't need write analytics code again every click.

example: have 2 buttons on activity each of them having click listener. want call analytics.track(string buttonname) not have add in every click listener. data should passed in tracking button name.

a way (probably not ultimate way) extending button (or view), , putting analytics code view#performclick() method.

as buttonname, can a field of custom view class, can set programmatically or via xml custom attribute.

global implementation :

  • create custom xml attribut : create file named attrs.xml in ressource folder :

    <resources>     <declare-styleable name="tracking">         <attr name="tracking_name" format="string" />     </declare-styleable> </resources> 
  • create custom button (or view) class, overwrite performclick() method , call analytics.track() string gotten 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 view has onclicklistener listened click event,         //so don't report click on passive elements         boolean clickhasbeenperformed = super.performclick();         if(clickhasbeenperformed && mtrackingname != null) {             analytics.track(mtrackingname);         }         return clickhasbeenperformed;     } } 
  • use new class everywhere want track event, example in 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, 1 way, may other easier/better/better implementation ways :)


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -