Are you struggling to track which pages drive the most conversions? With a simple jQuery script, you can dynamically add UTM parameters to your WordPress buttonsβno plugin required. This not only makes your campaign tracking more accurate but also helps you understand user journeys in Google Analytics. In this guide, weβll show you how to implement dynamic UTM tags step-by-step so every click tells the right story.
<!-- dynamically add page url (path) to the utm_source parameter -->
<script>
jQuery(document).ready(function(){
// get current url and trib trim it to get the page details
var thispageURL = jQuery(location).attr("href");
var trimmed_url = window.location.pathname;
var semi = trimmed_url.replaceAll("/", "_");
var final = semi.substring(1, semi.length-1);
// console.log(final);
// code to get url of consultation button
var btn_url = jQuery(".sfc_button a").attr('href');
if (final == null || final=='' || final=='_')
{
// if parameter is empty
// code to get url and update the parameter
var update_url = new URL(btn_url);
update_url.searchParams.set("utm_medium", "HomePage");
// setting your param
var final_updated_Url = update_url.href;
}
else{
// if parameter is not empty
// code to get url and update the parameter
var update_url = new URL(btn_url);
update_url.searchParams.set("utm_medium", final);
// setting your param
var final_updated_Url = update_url.href;
}
jQuery(".sfc_button a").attr("href", final_updated_Url);
});
</script>
Get the free WordPress Security Checklist 2026
25-point checklist PDF - malware detection, hardening guide, login security. Used by 500+ WordPress site owners.
- β 25-point security checklist PDF
- β WordPress malware scan guide
- β Hardening checklist for any WordPress site
No spam. Unsubscribe any time.
You're in!
Check your inbox - the checklist PDF is on its way.
Need help with your WordPress site?
I'm a freelance WordPress developer who fixes exactly this kind of problem.
150+ projects. Clients in UK, US, UAE & Ireland. Fast turnaround.
Rajan Gupta
Freelance WordPress DeveloperI'm Rajan Gupta, a freelance WordPress developer based in India with 150+ projects delivered for agencies and businesses in the UK, US, UAE, and Ireland. I specialise in performance optimisation, Core Web Vitals, custom Sage/ACF builds, and WooCommerce development - every project ships with a 90+ PageSpeed baseline.