Description
The gform_googleanalytics_mp_session_id filter allows developers to override the session ID used by the Google Analytics 4 Measurement Protocol integration.
Usage
add_filter( 'gform_googleanalytics_mp_session_id', 'your_function_name', 10, 3 );
Parameters
Parameter | Type | Description |
---|---|---|
$session_id | string|null | Parsed session ID from the cookie, or null if not found. |
$cookie_name | string | Name of the Google Analytics cookie being used. |
$ measurement_id | string | The GA4 Measurement ID associated with the current tracking configuration. |
Examples
add_filter( 'gform_googleanalytics_mp_session_id', 'add_custom_ga_session_id', 10, 3 );
function add_custom_ga_session_id( $session_id, $cookie_name, $measurement_id ) {
// If a custom cookie is set with the session ID, use it.
if ( isset( $_COOKIE['my_custom_session_cookie'] ) ) {
$cookie_value = sanitize_text_field( wp_unslash( $_COOKIE['my_custom_session_cookie'] ) );
return $cookie_value;
}
// Fallback to the default session ID.
return $session_id;
}
Placement
This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.
See also the PHP section in this article: Where Do I Put This Code?
Since
Google Analytics Add-On 2.4.0.
Source Code
This filter is located in includes/class-gf-google-analytics-measurement-protocol.php