Created
November 13, 2020 11:56
-
-
Save vyskoczilova/39bb03b76680a9f4f0661834e4b201e8 to your computer and use it in GitHub Desktop.
Parse UTM codes to Contact Form 7 by javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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