6

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

adam0101
  • 29,096
  • 21
  • 96
  • 174
  • No, Session IDs are calculated on the backend of GA. The Answers given here relate to Client IDs. – BNazaruk Jan 12 '23 at 20:15
  • @BNazaruk: not true for GA4 any more. Session IDs are on the browser, together with the Client ID. – Peter Szoldan Mar 14 '23 at 12:20
  • Indeed, for GA4, you can now get it on the front-end. That answer related to UA tho. GA4 is meant to reduce the cost of Analytics for Google, so no more session generation on the backend. And the session definitions became much much simpler. – BNazaruk Mar 14 '23 at 14:39

4 Answers4

5

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.

Philip Walton
  • 29,693
  • 16
  • 60
  • 84
4

Using Google Tags and Google Analytics 4, the following code will get the session_id (tested code), please note G-XXXXXXXXXX is a placeholder for your analytics ID:

gtag('get', 'G-XXXXXXXXXX', 'session_id', (id) => { console.log(id); } );

will output to the console:

1678297554

Peter Szoldan
  • 4,792
  • 1
  • 14
  • 24
  • 1
    yeah, just don't attempt to use an arrow function in GTM. Also, you can get it from the cookie too. – BNazaruk Mar 14 '23 at 14:41
  • @BNazaruk Is an arrow function unreliable here? Worked for me with no problem. But happy to edit the code not to mislead people. Cookie: true. I still prefer using the published interface just in case they change the data structure in the cookie. – Peter Szoldan Mar 15 '23 at 15:06
  • GTM Allows ES5 only. ES6 is only supported partially and in templates. – BNazaruk Mar 15 '23 at 15:51
2

You can read the cookie created by Google but:

  1. It's a privately (by google) chosen variable name/format and could change at any time, thus breaking your code
  2. It appears to break their TOS if you store that information

https://webmasters.stackexchange.com/questions/51866/can-you-track-one-users-session-with-google-analytics-by-using-the-utma-cooki

Linking Google Analytics event logs to unique session ID

http://www.analytics-ninja.com/blog/2011/08/how-google-analytics-calculates-visits.html

Community
  • 1
  • 1
Don Rhummy
  • 24,730
  • 42
  • 175
  • 330
  • Those links refer to the old ga.js. I'm looking for info about the newer analytics.js. – adam0101 Nov 13 '14 at 19:20
  • @adam0101 my answer still stands: "It's a privately (by google) chosen variable name/format and could change at any time, thus breaking your code" You could download the JS file and look through it yourself to find your answer, but it would be unsupported and not recommended – Don Rhummy Nov 13 '14 at 19:22
1

Be aware! Some ad-blockers like uBlock Origin replaces GA script with its own polyfill.
This means that you can't correctly check is it real GA script are loaded on your page.

Also uBlock's polyfill changes GA API, so when you using ga(function(tracker){}) callback will never called!
So don't relay on callback and never wait for its calling

The better way is to manually store custom clientId in cookie and tell this clientId to GA https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference?hl=en#clientId

D. Naumov
  • 140
  • 2
  • 7