Passing UTM Cookies into Gravity Forms [RESOLVED]

Hello,

I’m looking to pass UTM parameters stored in a cookie into hidden fields in a gravity form.
I’ve set up a functional cookie called utm_campaign that is storing the UTM URL.

I’ve added the following code from the Gravity Forms support documentation to the functions.php file of my theme:

function populate_utm_campaign( $value ) {
    GFCommon::log_debug( 'Value from cookie => ' . $_COOKIE['utm_campaign'] );
    return $_COOKIE['utm_campaign'];
type or paste code here

Unfortunately, nothing seems to be working at this point. Would love some guidance on how to proceed.

Thanks!

The above snippet is incomplete, assuming you have added the complete example from our documentation, you will want to check the following:

  • Is your page cached? If so you need to exclude it from cache to allow dynamic population to work.

  • Is the cookie really added to your browser? You will want to check the cookie storage for your browser to confirm it.

  • If the above points are ok, you will want to check if your server is passing the value from
    $_COOKIE to Gravity Forms. For this you will want to enable logging and replicate the issue. Then you can search the log for Value from cookie => to confirm if the server is passing the value or not.

Thanks for the reply!

You are correct, apologies, the full code we added is:

add_filter( 'gform_field_value_utm_campaign', 'populate_utm_campaign' );
function populate_utm_campaign( $value ) {
    GFCommon::log_debug( 'Value from cookie => ' . $_COOKIE['utm_campaign'] );
    return $_COOKIE['utm_campaign'];

We just changed the page to never cache. Thank you for pointing that out.

The cookie is in the brower and named correctly.

You’re correct. After looking at the log, the server is not passing the value to Gravity Forms!

2023-08-30 10:12:51.440577 - DEBUG --> Value from cookie =>  
2023-08-30 10:12:52.918704 - DEBUG --> Value from cookie =>  
2023-08-30 10:12:54.424557 - DEBUG --> Value from cookie =>  
2023-08-30 10:12:58.377136 - DEBUG --> Value from cookie =>  
2023-08-30 10:12:59.072503 - DEBUG --> Value from cookie =>  

How would you advise we proceed? Any help would be greatly appreciated.
Thank you.

Update: I was able to solve the problem using a combination of plugins and cache settings!

I used the plugin UTM Grabber, but I believe my major issue was not adding utm associated cookies to whitelist for WP Rocket and WP Engine to prevent caching. Once I made these changes, I was able to get utm cookies to populate into gravity forms.

Hope this helps anyone else who’s having similar issues.

1 Like