Search code examples
javascriptgoogle-analyticsgoogle-analytics-api

How to reliably get session id generated by Google Analytics?


Using the latest analytics.js code (as of today) is there a way through their JS API I can get the session id?


Solution

  • If you're talking about the client ID, you can get that as follows:

    ga(function(tracker) {
      var clientId = tracker.get('clientId');
      console.log(clientId);
    });
    

    Here's where that's stated in the documentation:
    https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id#getting_the_client_id_from_the_cookie

    Keep in mind that the client ID is (usually) stored in a cookie, so if the user clears their cookies, they'll be assigned a new client ID that may not match the one you have.