I've read the documentation on the GoogleAnalytics v2 website (I've basically read all the pages from https://developers.google.com/analytics/devguides/) but was not able to find an accurate enough to answer my question. It is either missing or mixed with the version 1 documentation.
I know that with EasyTracker, you can set a timeout_session parameter. But I dont want to use EasyTracker and I want to explicitly stop a session at some point in my code. (not setting a timeout)
I start a session when I open my application with :
mTracker.setStartSession(true);
and was wondering if calling
mTracker.setStartSession(false);
explicitely stops it.
Any help or point to a better tutorial/documentation would be greatly appreciated.
Typically, it's when: The session duration reaches a time limit: a session ends by default after 30 minutes of inactivity by the user (aka a 'timeout') or when the day ends at midnight.
For a session to be considered engaged, a visitor has to do one or more of the following: Engage actively on your website or mobile app for over 10 seconds. Have two or more screen or page views. Fire a conversion event.
Essentially, Direct sessions occur any time Google Analytics cannot determine another referring source or channel. This differentiates Direct traffic from other default channel groupings like Organic, Referrals, Social, Email and Paid.
Overview
So I spent some time going through the Measurement Protocol as well as looking through the debug logs in LogCat. When GA on your phone 'dispatches' a bunch of hits, every hit seems to have a corresponding HTTP request in the log that begins with:
GET /collect?...
and is followed by a bunch of parameters that define the type of hit (e.g. event, social, e-commerce) and some basic info about the app (e.g. app id, tracking id, timestamp).
Here's what I learned:
setStartSession(false)
does not end a session.
How I Discovered It
As I said earlier, every hit represents some type of an action. However, session starts or session ends are not considered hits. They are merely additional data that is added on to the most recent hit that tell GA to group the future hits in a new session.
So if you sendEvent(...)
and then setStartSession(true)
, and then dispatch()
, you'll see ONE hit in the logs that describes the event with an additional parameter &sc=start
that describes the starting of a new session.
I then tried doing the above using setStartSession(false)
and I did not notice the additional &sc
parameter. It should have been &sc=end
, as described here.
Potential Hack
The tracker had a send(...)
method that seems like it would allow you to send a custom hit by specifying the necessary parameters. After some trial and error, the following successfully created an event and attached the session ending parameter as described above.
Map<String, String> data;
data = EasyTracker.getTracker().constructEvent("Test", "Test", "Test", 0L);
data.put("sessionControl", "end");
EasyTracker.getTracker().send("event", data);
So theoretically, every time you want to end a session, you could a dummy event (like above), add the sessionControl
parameter, and dispatch. From the logs it seems to work perfectly, but I haven't verified this on my GA dashboard.
And make sure you disable automatic session control by setting ga_sessionTimeout
to -1
in your analytics.xml
file.
I also uploaded my project here, if you want to try looking through the logs and comparing the hits. Make sure you update your GA tracking id. Hope this helps!
My Logs
Start Session + Test Event, Dispatch
GET /collect?ul=en-us&ev=0&ht=1362779137510&sr=720x1184&a=0&sc=start&aid=com.example.com.example.sessiontest&ea=Test&cid=ae57a272-89b2-46ab-8c82-7acdb49c3669&ec=Test&av=1.0&v=1&t=event&el=Test&an=com.example.sessiontest&tid=UA-XXXXXXXX-X&_u=.sMC&_v=ma1b4&cd=com.example.com.example.sessiontest.MainActivity&qt=2788&z=48 HTTP/1.1
End Session + Test Event, Dispatch
GET /collect?ul=en-us&ev=0&ht=1362779233499&sr=720x1184&a=0&aid=com.example.com.example.sessiontest&ea=Test&cid=ae57a272-89b2-46ab-8c82-7acdb49c3669&ec=Test&av=1.0&v=1&t=event&el=Test&an=com.example.sessiontest&tid=UA-XXXXXXXX-X&_u=.ssMMC&_v=ma1b4&cd=com.example.com.example.sessiontest.MainActivity&qt=3726&z=50 HTTP/1.1
End Session Hack + Test Event, Dispatch
GET /collect?ul=en-us&ev=0&ht=1362779194381&sr=720x1184&a=0&sc=end&aid=com.example.com.example.sessiontest&ea=Test&cid=ae57a272-89b2-46ab-8c82-7acdb49c3669&ec=Test&av=1.0&v=1&t=event&el=Test&an=com.example.sessiontest&tid=UA-XXXXXXXX-X&_u=.ssyL&_v=ma1b4&cd=com.example.com.example.sessiontest.MainActivity&qt=3581&z=49 HTTP/1.1
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