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.
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.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With