Set form fields to URL parameters FIRST then to Cookie values

I have my site set up to capture URL param’s named utm_campaign, utm_source and utm_medium and set them to cookies named:

From there I’m able to set invisible form fields using the suggested Gravity forms functions in my function.php file.

	add_filter( 'gform_field_value_utm_campaign', 'populate_utm_campaign' );
function populate_utm_campaign( $value ) {
   return $_COOKIE['last-url-campaign'];

This is the desired behavior because I want the values to follow a user from page to page and be picked up by Gravity forms on any page they exist.

My issue is that on the first visit to a page with new URL parameters the fields aren’t initially set.

I’m presuming it’s a timing issue because the script that sets my cookie values is in the footer.php file and the Gravity forms function is in the functions.php file? I thought that the Gravity form fields would automatically pick up and use the URL param’s since they’re set to allow dynamic population but it doesn’t seem to be the case. The values aren’t initially set (but the cookie is updated) and then they populate on a refresh.

Below are my test URL’s, the first is an on-page form and the second is in a modal when you click the green button at top.

You’ll notice if go to the URL’s above with URL param’s added (below) it won’t initially populate the hidden fields, it requires a refresh.


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.