Storing utm_source and utm_content without using GF's PHP based dynamic population

Hello folks,

As already mentioned in another thread (NGINX Caching, PHP dynamic field population, UTM parameters and other problems), I wanted to save utm_source and utm_content in 2 fields of my GF form but ideally WITH caching enabled.

I finally found a way like this:

<script type="text/javascript">
jQuery(document).ready(function($) {
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
//GETTING UTM source & content PARAMETERs
UtmSource = getParameterByName("utm_source");
UtmContent = getParameterByName("utm_content");
$('.js_cachebuster_utm_source input').val(UtmSource);
$('.js_cachebuster_utm_content input').val(UtmContent);
});
</script>

This just needs the form to have 2 fields with CSS classes of js_cachebuster_utm_source and js_cachebuster_utm_content to work fine.

Right now I’ve put that script in my post HTML, right below the form shortcode… It looks like it can also work inside a form HTML field but I’m wondering : where is the best place to add that kind of script?

Thanks a lot,

Cheers,

You can add that script to an HTML field in the form, or you can enqueue the script via your theme functions.php file (you would add the script to an external file, then include that file as necessary, in your theme functions.php file.)

1 Like

Thanks a lot for confirming this.

I wasn’t sure if it could make any difference to have it inside the form in an HTML field vs. having it in the HTML of the post/page vs. having it in an enqueued file.

Looks like it doesn’t make any difference based on your answer!