Tracking Chef
  • Guides
    • Google Ads
    • Google Analytics
    • Google Tag Manager
    • Google Data Studio
    • Facebook
    • LinkedIn Ads
    • Hubspot
    • Segment
    • Quora Ads
    • Reddit Ads
  • Blog
  • GTM Playground
  • Swag
  • About
  • Guides
    • Google Ads
    • Google Analytics
    • Google Tag Manager
    • Google Data Studio
    • Facebook
    • LinkedIn Ads
    • Hubspot
    • Segment
    • Quora Ads
    • Reddit Ads
  • Blog
  • GTM Playground
  • Swag
  • About
Tracking Chef
  • Guides
    • Google Ads
    • Google Analytics
    • Google Tag Manager
    • Google Data Studio
    • Facebook
    • LinkedIn Ads
    • Hubspot
    • Segment
    • Quora Ads
    • Reddit Ads
  • Blog
  • GTM Playground
  • Swag
  • About
  • Guides
    • Google Ads
    • Google Analytics
    • Google Tag Manager
    • Google Data Studio
    • Facebook
    • LinkedIn Ads
    • Hubspot
    • Segment
    • Quora Ads
    • Reddit Ads
  • Blog
  • GTM Playground
  • Swag
  • About
How to add Session ID to GA4 Measurement Protocol events?
Home » Google Analytics
January 15, 2023 Or Blan

I was recently helping a client migrate to GA4 (it’s right around the corner, July 1st, 2023). This client, a SaaS B2B product, has a complex funnel, in which deals are closed offline via sales representatives. Most leads are generated on the marketing site and sent directly to their CRM (Hubspot), and from there the client wanted to report that conversion back to both GA4 and the various marketing platforms. Unsurprisingly, the answer was using GA’s Measurement Protocol API to report to GA. 

But the reports connecting the conversion to user visits were broken and nobody knew why. 

When I started investigating it, I found out that they didn’t pass the session_id param in the Measurement Protocol hit to GA4. HOLD ON? Sessions? In GA4? YES!

Universal analytics was based on sessions, and once you passed the ga cookie id (i.e. client id) in the measurement protocol hit, then GA would match that hit to its corresponding “place”, but GA4 is built on events and not sessions, and this is why we pass to “tell” GA the session_id.

Google hasn’t done the best job in terms of documentation on this one, to say the least. I went over the measurement protocol for GA 4 documentation and CTR+F’d for “session_id”, and came up short. I was however able to find this release note from May ‘22 introducing the session_id param, and further exploration led me to this thread in which someone named Kevin from the Google Analytics API team acknowledges that Google needs to update their documentation on this matter (but can’t commit on a timeline).

Se let me provide you guys with an answer on how to add session_id to GA4 measurement protocol (and from to take it from). According to Google: “In order for user activity to display in standard reports like Realtime, engagement_time_msec and session_id must be supplied as part of the params for an event.” 

This means that session_id should be part of the params array of the payload as such:

{
"client_id": "123123123",
"events": [
  {
    "name": "offline_purchase",
    "params": {
      "engagement_time_msec": "100",
      "session_id": "123456"
    }
  }
]}

So now that we know where to put it, we need to find how to pull the session_id and from where. When a session_start event is fired a new session_id is created by GA, but it is not part of the _ga cookie like it was in Universal, it’s part of a new cookie that contains the container_id

And looks something like this: _ga_XXXXXXXXXX, where the X’s stand for the stream id. 

Here you can see an example, in which the highlighted part is the session_id:

image-7796109
The Session ID in the cookie

Just before the session_id is the session_count, i.e. the numeric counter of sessions, in this case, it’s the first session. Now all you have to do is direct your developer to this number to be pulled from the client and then pass it to the measurement protocol hit, as a session_id param under the params array.

Google Analytics 4 Measurement Protocol

About the Author

Or Blan View all posts by Or Blan

All around analytics veteran of the industry. A Monty Python fan and the co-founder of the Israeli Hamburger Community

« Previous
Next »
Leave a Reply

Click here to cancel reply.

Made with ❤️ by Elad Levy
Disclaimer: Some of the links shared on this site are affiliate links, so if you choose to make a purchase, I’ll earn a small commission. Please note, I only recommend tools I’ve used and have found useful, the commission is not a driving factor