- Print
- DarkLight
- PDF
Tracking Activity and Fragment Lifecycles
In the world of e-commerce, marketers often seek to report on the performance of their advertising tools such as websites or mobile apps so they can adjust their efforts to improve the results. To get analysis of user behavior from your application, you may want to find out which views are more often viewed over others or how users ended up viewing a particular UI. In Android, views, e.g., UI's are generally created by xml layouts and they are driven by either an activity or a fragment. Therefore, we may say that lifecycle events of activities and fragments are direct representation of user navigation, which holds the key to building marketing strategy.
To track UI views on your Android app, the easiest way would be simply following the lifecycle events of activities and fragments. In order to get your data collector setup within any one of these lifecycle events, you should set your collection mode on either automatic or manual. Note that activity lifecycle events can be tracked under either automatic or manual mode, however fragment lifecycle events must be tracked only manually.
Tracking lifecycles in Automatic Mode (activity only)
To enable tracking Activity lifecycle events automatically, you will need to satisfy a few requirements. First, you must set the configuration value of
"wt_dc_activity_automatics_enabled"
to betrue
in yourMyApp/res/values/webtrends.xml
<string name="wt_dc_activity_automatics_enabled">true</string>
Finally, your project application class should extend from
WTApplication
class instead of Application class.import com.webtrends.mobile.analytics.*;
public class MyApplication extends WTApplication{
Please note that when you do this in your project, the following
Application.ActivityLifecycleCallbacks
methods
are set to be triggered automatically to track Activity lifecycles.public void onActivityStarted(Activity activity) public void onActivityResumed(Activity activity) public void onActivityPaused(Activity activity) public void onActivityStopped(Activity activity)
You won't need to write any additional code to track these events in the activities in your app. However, if tracking these events are not sufficient, you can certainly write a couple of lines of code to follow additional lifecycle events in your application.
Tracking lifecycles in Manual Mode (activity and fragment)
Triggering events in automatic mode is a really convenient method for tracking activity lifecycle events because it does not require any additional line of code to trigger events however, sometimes you may want to go beyond just tracking above four activity lifecycle events. Android Fragment was introduced in API 11 for better support on UI by embedding in an activity to provide muli-pane user interface. You can think of a fragment as a modular section of an activity, which has its own lifecycle. To trigger any method of fragment lifecycle event within an activity, you must set data collection method manually.For instance, if you want to track "onStart" execution event in a fragment, you can:
@Override public void onStart() { super.onStart(); WTDataCollector.getInstance().onCustomEvent(".MyFragment", "onStart", null); }
This single line of code is all you need for tracking
onStart()
lifecycle event in a fragment. The first parameter ".MyFragment" represets the path and name of your fragment and the second parameter represents the current event lifecycle. The third (and last) parameter is a Hashmap of custom data that you would like to send along with this event. See "onCustomEvent" for details.Let's make an another example with an activity. Suppose you'd like to track
onDestory()
event in an activity you are interested in:@Override private void onDestroy() { super.onDestroy(); WTDataCollector.getInstance().onCustomEvent(".MyActivity", "onDestroy", null); }
".MyActivity"" is the path and filename of the activity in your application and onDestroy is the event triggered.