最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I store utm values in the shopify cart through a theme app extension? - Stack Overflow

programmeradmin1浏览0评论

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 '=>'`
发布评论

评论列表(0)

  1. 暂无评论