In order to better understand how Flipboard traffic performs compared to normal web traffic, we'd like to use a unique Amazon affiliate ID for Flipboard when they load our AMP page in their mobile app.
AMP offers amp-script for custom JavaScript, but it has significant limitations. Notably, it doesn't let you mutate the DOM without associated user input. The best idea I've come to is:
<script id="entry-content" type="text/plain" target="amp-script">
document.querySelectorAll('a').forEach(function(el) {
el.addEventListener('click', function() {
var link = el.getAttribute('href');
if (-1 === link.indexOf('amazon.com')) {
return;
}
el.setAttribute('href', link.replace(/tag=[A-Za-z0-9\-]+/, 'tag=newid' ));
});
});
</script>
AMP allows the mutation but the browser uses the original link
value for the new tab, so that doesn't work.
I've also tried:
- Calling
window.open()
in theclick
event handler.open()
isn't available though. - Simply modifying the links on
document.addEventListener('scroll')
. AMP killed the mutations.
Any other crafty ideas?