I am using the below script to dynamically change links with query parameters. The links are dynamically changed when I click on the button.
example.com/abc?client_id=123
The problem is when a user lands on the site with this URL from Google Ads - example.com/abc?gclid_id=786
, and then clicks on the button the URL is dynamically changed to example.com/abc?gclid=786?client_id=123
I want it example.com/abc?gclid=786&client_id=123
Can I modify the existing script if the users come from Google change the parameter to &client_id=123
and when the user comes organically change the script to ?client_id=123
The parameter values are dynamic and they are mapped with cookie values (This is sorted). The main problem is &
and ?
<script>
(function () {
var links = document.querySelectorAll('a[href*=abc]');
var clientId = '?client_id={{Read Cookie Value}}'
links.forEach(function(link) {
var original = link.getAttribute('href');
link.setAttribute('href', original+clientId)
})
}) ();
</script>