Integrate Flurry Analytics in Android Tutorial

Udemy Generic 728x90

Objective

In this tutorial, we will learn how to integrate Flurry Analytics in your Android Application through a simple example.

 




Analytics can be defined as analysis of historical data to examine the current patterns, derive potential trends and new equations and use them to enhance the business performance and make better decisions for future goals. Flurry Analytics is one of the popular service which tracks the data of Android app and converts it into more meaningful format. Through Flurry, we can analyze the data to recognize potentials trends and can make various decisions to quantify the performance based on the analysis.

In this tutorial, we learn how to implement Flurry Analytics in Android application and track user engagement on the app through a simple example.

Step 2

Create New Project

Create a new project in Android Studio, named FlurryDemo.

 

Step 3

Create Flurry API Key

To integrate Flurry Analytics, you need to add your app at Flurry Developer’s Console. So, open your Flurry developer console and add your app there.

flurry3
flurry5
Once your app is added, a unique application key will be generated automatically. This unique key will be used to in your app so that flurry recognizes it and track data from it.

flurry4

Step 4

Integrate Flurry SDK

Next, you need to integrate Flurry SDK in your application as dependency. To add SDK, open app’s build.gradle file and following in dependencies section :

<br />
compile 'com.flurry.android:analytics:6.2.0'<br />

 

Step 4

Initialize Flurry SDK

Once Flurry SDK is added as dependency, you can use it to track the data of your app. But, before start tracking data you need to initialize flurry SDK and provide your unique key. Flurry SDK needs API key to determine the user account for which it will be tracking the data. So, create a new application class named, DemoApplication and initialize Flurry inside the onCreate() method of the class.
Here is the sample code for the same :

<br />
public class DemoApplication extends Application {</p>
<p>    @Override<br />
    public void onCreate() {<br />
        super.onCreate();</p>
<p>        // configure Flurry<br />
        FlurryAgent.setLogEnabled(false);</p>
<p>        // init Flurry<br />
        FlurryAgent.init(this, &quot;YOUR_API_KEY&quot;);<br />
    }<br />
}<br />

Also, you need to specify the name of your application class in manifest file. So, open AndroidManifest.xml file and add name attribute to Application tag.

<br />
android:name=&quot;.DemoApplication&quot;<br />

Next, you need to provide Internet permission to enable communication between Flurry SDK to Flurry backend. Add the following line in manifest file :

<br />
&lt;uses-permission android:name=&quot;android.permission.INTERNET&quot;/&gt;<br />

 

Step 4

Track Sessions

Once Flurry is initialized, you can start tracking app’s data. First of all, we will talk about tracking app sessions. If you are targeting app for android api version 14 or more  then you need not worry about session handling as sessions are handled automatically. But, if minimum target version is less than api version 14 then you need to track sessions manually through code. Flurry SDK has the following methods to track the sessions :

  • onStartSession() method : it starts tracking session for the provided context.
  • onEndSession() method : it ends the session tracking.

To start session tracking, add FlurryAgent.onStartSession(context) inside onStart() method of your activity. You can end session when your activity is stopped, so add FlurryAgent.onStopSession(context) inside onStop() method of activity.

Here is the sample code snippet for the same :

<br />
@Override<br />
protected void onStart() {<br />
    super.onStart();</p>
<p>    // starts a new session<br />
    FlurryAgent.onStartSession(this);<br />
}</p>
<p>@Override<br />
protected void onStop() {<br />
    super.onStop();</p>
<p>    // ends current session<br />
    FlurryAgent.onEndSession(this);<br />
}<br />

 

Step 6

Track Custom Events

You can use custom events to track specific actions that users take within your app. These events helps you understand the way users are interacting with your app. Flurry SDK provides a method, named logEvents() to track events.

<br />
FlurryAgent.logEvent(&quot;Custom Event&quot;);<br />

While creating a custom event, you also add various information with event that will help you understand the behavior of the event more clearly. To send additional information with event, you have to add all your information in HashMap object and then pass that object to logEvent() method as second argument.

In this tutorial, we will track a click event. So, first you need to add a button in the layout. So, open your activity_main xml file and add following code snippet there :

<br />
    &lt;Button<br />
        android:layout_width=&quot;wrap_content&quot;<br />
        android:layout_height=&quot;wrap_content&quot;<br />
        android:text=&quot;Click Event&quot;<br />
        android:id=&quot;@+id/button&quot;<br />
        android:layout_gravity=&quot;center_horizontal&quot;<br />
        android:onClick=&quot;clickEvent&quot;/&gt;<br />

Next, you need to implement a method that will be called on the button’s click.Within the method, create a custom event using logEvent() method and add information using HashMap object.
Here is the sample code to track a custom event with additional information :

<br />
public void clickEvent(View view){</p>
<p>    Map&lt;String, String&gt; eventParams = new HashMap&lt;String, String&gt;();</p>
<p>    //param keys and values have to be of String type<br />
    eventParams.put(&quot;event&quot;, &quot;click&quot;);<br />
    eventParams.put(&quot;value&quot;, &quot;test&quot;);</p>
<p>    //up to 10 params can be logged with each event<br />
    FlurryAgent.logEvent(&quot;button is clicked&quot;, eventParams);<br />
    Toast.makeText(this, &quot;Flurry Event Tracked&quot;, Toast.LENGTH_SHORT).show();<br />
}<br />

 

Step 7

Run Your Applicationd

So, your basic Flurry analytics integration is complete now. To learn about Flurry in more depth, follow Flurry Documentation.

Now, run your application and you will see a button. Click on the button to submit your your custom event. You can check your logged event in Flurry dashboard.

flurry2
flurry1
 


Download Source Codedownload

Udemy Generic 728x90

Spread the word. Share this post!