I've created a block targeting the body so that the code will check the url whenever the shop is opened, and the goal is to loop through all url parameters and store any utm sources in the cart for later use even if they are no longer in the url.
Here is my current block:
{% comment %}
Embed to provide global js to check the utms when user enters shop
{% endcomment %}
<script>
const newAttributes = {{ cart.attributes }}
if (!newAttributes.UTMs) newAttributes.UTMs = []
const urlParams = new URLSearchParams(window.location.search);
for (const [key, value] of urlParams.entries()) {
if (key === "utm_source" && !newAttributes.UTMs.includes(value)) {
newAttributes.UTMs.push(value)
console.log(newAttributes, 'other attributes')
}
}
fetch(window.Shopify.routes.root + 'cart/update.js', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
attributes: newAttributes
})
})
.then(function(res) {
return res.json();
})
.then(function(result) {
console.log(result);
})
.catch(function(err) {
console.error(err);
});
</script>
{% schema %}
{
"name": "UtmCheck",
"target": "body",
"settings": []
}
{% endschema %}```
and the error I keep getting is saying `Uncaught SyntaxError: Unexpected token '=>'`