How to Replace Amazon Tag for Mobile Devices

This is a small snippet that show how to replace all your page amazon tags for a different tag. Used primarily for changing desktop tag to mobile affiliate tag.

This is a short snippet on how to replace all amazon tag for mobile devices. I came across this problem when a client wanted to see what their conversion rate was for mobile devices. So i needed to search all the DOM and replace all the tag elements with a different mobile assigned tag. 

If you are struggling with replacing the amazon tag with a different one instead of manually doing it. This snipped could help you. 

 

<script>
if (jQuery(window).width() < 767) {
	jQuery('a').each(function() { 
	    var jQuerythis = jQuery(this),
	    aHref = jQuerythis.attr('href');  
	    jQuerythis.attr('href', aHref.replace('?tag=mydesktoptag','?tag=mymobiletag')); 
	});
	
}
</script>