I've implemented an AMP layout where a unique code is added to the URL based on the traffic source. This code is used to update phone numbers displayed on the site.
For instance, if you visit https://example.com/text?bid=1234
I created a script that should update any links on the page with the bid ID from the URL, but it doesn't seem to be functioning properly:
$(function() {
updateLinksHref();
});
function updateLinksHref() {
var bid = getUrlParameter('bid');
if(bid == undefined ){
bid = localStorage.getItem('bid');
}else{
localStorage.setItem('bid',bid)
}
if (bid && bid !== undefined) {
$.each($('a'), function(i, link) {
if ($(link).attr('href') !== '' || $(link).attr('href') !== '#') {
$(link).attr('href', $(link).attr('href') + '?bid=' + bid)
}
});
}
}
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
};
Even after the bid is successfully set to 1234 in local storage, the HTML links are not being updated as expected.