Flurry analytics in every Activity?
Asked Answered
D

2

5

I want to integrate flurry analytics in my android application, it looks really simple. But i am not familiar with flurry and how it works.

Should i add the code :

public void onStart()
{
super.onStart();
FlurryAgent.onStartSession(sample, “APIXXXXXXXXXXXX”);

}

in every activity?

My application uses a lot of activities and i don't really care for tracking which of the activities is used, only the number of installations, sessions and session length. But is the session length available if the flurry code is only added in the startup activity?

I know most of the information i want is available in play store already, but i want to try this to have an overview of applications on different platforms.

Droshky answered 6/9, 2012 at 19:37 Comment(0)
E
16

Here is a great answer : https://mcmap.net/q/527238/-how-to-use-flurry-in-an-application

I suggest you to create a "BaseActivity" and to tell all your activities to extend it so you don't have to copy/paste those lines in every activity class.

Something like this :

public class BaseActivity extends Activity
{
    public void onStart()
    {
       super.onStart();
       FlurryAgent.onStartSession(this, "YOUR_KEY");
       // your code
    }

    public void onStop()
    {
       super.onStop();
       FlurryAgent.onEndSession(this);
       // your code
    }
}

In response to @conor comment :

From Flurry's documentation

So long as there is any Context that has called onStartSession(Context, String) but not onEndSession(Context), the session will be continued. Also, if a new Context calls onStartSession(Context, String) within 10 seconds (the default session timeout length) of the last Context calling onEndSession, then the session will be resumed, instead of a new session being created. Session length, usage frequency, events and errors will continue to be tracked as part of the same session. This ensures that as a user transitions from one Activity to another in your application they will not have a separate session tracked for each Activity, but will have a single session that spans many activities.

Epimenides answered 6/9, 2012 at 20:21 Comment(4)
That's exactly how I used it ;)Erkan
great! Never used a baseActivity, comes in really handy for more things!Droshky
Would this code not have the effect of creating multiple Flurry sessions per each actual single usage session?Peroxide
Can I do the same in Application class of my app? This is because I am already half way through my app and changing my each activity parent won't be feasible now. If yes, how?Stir
M
4

Answer from florianmski has sense, but there are some problems when you have to use different kinds of activities in your application such as FragmentActivity, TabActivity, ListActivity and so on. In this case you are not able to extend all your activities from single BaseActivity. Personally I would prefer to put calls of onStartSession and onEndSession in each activity's onStart and onStop methods, but before wrap them into some class, for example:

public class Analytics {
    public static void startSession(Context context) {
        FlurryAgent.onStartSession(context, Config.FLURRY_KEY);
        // here could be some other analytics calls (google analytics, etc)
    }
    public static void stopSession(Context context) {
        FlurryAgent.onEndSession(context);
        // other analytics calls
    }
}

Inside each activity:

public void onStart() {
    super.onStart();
    Analytics.startSession(this);
}

public void onStop() {
    super.onStop()
    Analytics.stopSession(this);
}
Macklin answered 29/5, 2014 at 8:52 Comment(2)
This is definitely the way to go, plus it makes a great entry point for logging etc.Consentient
I like this way to use but I've changed the code of stopSession(Context context) method with FlurryAgent.onEndSession(context);Tamie

© 2022 - 2024 — McMap. All rights reserved.