The gtag.js get() API has been enhanced to support parsing the Google Analytics session number from the session cookie, expanding beyond the previous capability of only retrieving the session ID. This update aligns with Google Tag Manager's custom templates functionality.
gtag.js get() API Overview
The get
command in gtag.js allows retrieval of various values, including those set with the set
command, using the syntax:
gtag('get', '<target>', '<field_name>', callback)
<target>
: A string specifying the target to fetch values from (e.g.,G-XXXXXXXXXX
).<field_name>
: The name of the field to retrieve.callback
: A function invoked with the requested field value orundefined
if the field is unset.
Supported Field Names
Field names can be custom fields set via gtag('set')
or predefined fields:
Field Name | Supported Targets |
---|---|
client_id |
Google Analytics 4 |
session_id |
Google Analytics 4 |
session_number |
Google Analytics 4 |
gclid |
Google Ads, Floodlight |
This enhancement allows developers to access the session number directly, improving session tracking capabilities within Google Analytics 4 via gtag.js.