问题
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.
回答1:
In the GA v3, to manually start or end a session, use the tracker's session control parameter as follows:
Start a new session. The next hit from this tracker will be the first in a new session.
[tracker set:kGAISessionControl value:@"start"];
End a session. The next hit from this tracker will be the last in the current session.
[tracker set:kGAISessionControl value:@"end"];
This information is available from the link: https://developers.google.com/analytics/devguides/collection/ios/v3/sessions
回答2:
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
回答3:
In GoogleAnalytics v2 they have new ways to start/stop the session.
I have not seen your code of mTracker.setStartSession(true)
and mTracker.setStartSession(false)
used before.
Now, if you don't want to use the EasyTracker methods you can start a new session by the following:mTracker.startNewSession("UA-xxxxxxx-x", 20, this);
With the first input being(String uniqueGoogleAnalyticsKey, int autoDispatchTimer, Context this)
To stop it:
tracker.stopSession();
来源:https://stackoverflow.com/questions/13601916/how-to-stop-a-session-in-google-mobile-analytics-v2-for-android-without-easytrac