I've been trying to create a solution using URL parameters for discount codes. The goal is to be able to share a URL with someone with the discount in the URL, which then gets passed through to app sign up.
I thought I had this all working until I noticed people without a discount were seeing 'null' entered in the discount code box at checkout. So everyone without a discount was receiving an error.
Is there anything clever I can add in to stop the entry of null passing through? Here's the code:
<script>
// create urlParams variable (constant) from URLSearchParams class using current window
const urlParams = new URLSearchParams(window.location.search);
// set UTM medium, source and campaign variables (constants) based on results of URSearchParams
const coupon = urlParams.get('coupon')
// get the RSVP button element
var couponLinks = document.getElementsByClassName("button");
// edit RSVP button element property by appending the URL parameters
for (let couponLink of couponLinks){
couponLink.href += "?" + "coupon=" + coupon;
}
</script>
<script>
// create urlParams variable (constant) from URLSearchParams class using current window
const urlPass = new URLSearchParams(window.location.search);
// set UTM medium, source and campaign variables (constants) based on results of URSearchParams
const param = urlPass.get('coupon')
// get the RSVP button element
var programLinks = document.getElementsByClassName("nav-link program");
// edit RSVP button element property by appending the URL parameters
for (let programLink of programLinks){
programLink.href += "?" + "coupon=" + param;
}
</script>
I can update all CTAs to include 'coupon=' so it doesn't pass over null, but this isn't ideal.