Skip to content

Instantly share code, notes, and snippets.

@vyskoczilova
Created November 13, 2020 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vyskoczilova/39bb03b76680a9f4f0661834e4b201e8 to your computer and use it in GitHub Desktop.
Save vyskoczilova/39bb03b76680a9f4f0661834e4b201e8 to your computer and use it in GitHub Desktop.
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