event<\/em>.\u201d <\/p>\n\n\n\nThis means that session_id should be part of the params array of the payload as such:<\/p>\n\n\n\n
{\n\"client_id\": \"123123123\",\n\"events\": [\n {\n \"name\": \"offline_purchase\",\n \"params\": {\n \"engagement_time_msec\": \"100\",\n \"session_id\": \"123456\"\n }\n }\n]}<\/code><\/pre>\n\n\n\nSo now that we know where to put it, we need to find how to pull the session_id<\/em> and from where. When a session_start<\/em> event is fired a new session_id<\/em> is created by GA, but it is not part of the _ga cookie like it was in Universal, it\u2019s part of a new cookie that contains the container_id<\/em><\/p>\n\n\n\nAnd looks something like this: _ga_XXXXXXXXXX, where the X\u2019s stand for the stream id. <\/p>\n\n\n\n
Here you can see an example, in which the highlighted part is the session_id<\/em>:<\/p>\n\n\n\n
The Session ID in the cookie<\/figcaption><\/figure><\/div>\n\n\nJust before the session_id<\/em> is the session_count<\/em>, i.e. the numeric counter of sessions, in this case, it\u2019s 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<\/em> param under the params array.<\/p>\n\n\n\nGrabbing the Session ID<\/h2>\n\n\n\n To grab the Session ID you can use this script:<\/p>\n\n\n\n
function getSessionNumber() {\n\/\/ replace ABCDEFG with your GA Stream ID\n const pattern = \/_ga_ABCDEFG=GSd.d.(w+).(d+)\/;\n const match = document.cookie.match(pattern);\n const sessionNumber = match?.[2];\n\n if (!sessionNumber) {\n \/\/ Cookie not yet available; wait a bit and try again.\n window.setTimeout(() => getSessionNumber(callback), 200);\n return;\n }\n\n return sessionNumber;\n}\n<\/code><\/pre>\n\n\n\nIf you’re using GTM, you can use this script instead:<\/p>\n\n\n\n
function getSessionId() {\r\n var pattern = \/_ga_0J0X8GBMJE=GSd.d.(w+).(d+)\/;\r\n var match = document.cookie.match(pattern);\r\n var sessionId = match && match[1];\r\n\r\n if (!sessionId) {\r\n return;\r\n }\r\n\r\n return sessionId;\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"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 […]<\/p>\n","protected":false},"author":6,"featured_media":5499,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[136,135],"class_list":["post-5497","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-google-analytics","tag-google-analytics-4","tag-measurement-protocol"],"_links":{"self":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts\/5497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/comments?post=5497"}],"version-history":[{"count":3,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts\/5497\/revisions"}],"predecessor-version":[{"id":5619,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts\/5497\/revisions\/5619"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/media\/5499"}],"wp:attachment":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/media?parent=5497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/categories?post=5497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/tags?post=5497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}