Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parse UTM codes to Contact Form 7 by javascript
/**
* Solving a trouble with WP Engine which is caching website without URL parameters and re-attaches them back once the request is compiled.
* If you need to add UTM values to any form (not only CF7) you can use this JS snippet on the form page.
*
* Author: Karolína Vyskočilová (karolina@kybernaut.cz)
* Author website: https://kybernaut.cz
* Date created: 2020-11-13
*
* Read more about WP Engine's cache: https://wpengine.com/support/utm-gclid-variables-caching/
*/
(function() {
var urlParams = new URLSearchParams(window.location.search);
var paramNames = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_content'];
for ( var i = 0, length = paramNames.length; i < length; i ++ ) {
if ( urlParams.get(paramNames[i])) {
document.querySelector('.wpcf7-hidden[name="'+paramNames[i]+'"]').value = urlParams.get(paramNames[i]);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment