How to stop a session in Google mobile analytics v2 for android without EasyTracker

后端 未结 3 1656
刺人心
刺人心 2021-02-14 01:52

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 f

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-14 02:43

    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 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
    

提交回复
热议问题