The following code works great in browser. It looks for any 'a' tags with an href that includes 'example.com' and the appends it with '&utm_day=monday'. So 'exmaple.com' becomes 'example.com&utm_day=monday'.
<script>
let text = '&utm_day=monday'
document.querySelectorAll('a').forEach(
item => {
if (item.href.includes("example.com")) {
item.href += text
}
}
)
</script>
When trying to inject this code using GTM (Google Tag Manager) I get an error for both the 'let' and the '=>' that says "This language feature is only supported for ECMASCRIPT_2015 mode or better: let declaration."
I assumed using vars would fix this, but my syntax is blah. Any ideas?
<script>
var text = '&utm_day=monday';
var item = document.querySelectorAll('a').forEach;
if (item.href.includes("example.com")) {
item.href += text
}
</script>
forEach is a function that accepts a callback as an argument. you can't just do .forEach
Well, you could, but you're just putting whole forEach function in item. So now item is a function.
Then you try to get attribute href of that function.
Here:
var text = '&utm_day=monday';
document.querySelectorAll('a').forEach(function(item) {
if (item.href.includes("example.com")) {
item.href += text;
}
});