javascript - 希望将面包屑数据拉入 GTM

标签 javascript google-tag-manager

坐在一个我正忙着标记的网站上,上面有这个凌乱的小面包屑......

<div class="col-xs-12 col-md-12">
<nav id="breadcrumbs">
    <div class="breadcrumbs">
        <ul itemscope="" itemtype="http://schema.org/BreadcrumbList">

            li class="home" itemprop="itemListElement" itemscope="" itemtype="http://schema.org/ListItem">
                <a href="https://www.twt.co.za/" title="Go to Home Page" itemprop="item">Home </a>
                <span>/ </span>
                <meta itemprop="name" content="Home">
                <meta itemprop="position" content="1">
            </li>

            <li class="category161" itemprop="itemListElement" itemscope="" itemtype="http://schema.org/ListItem">
                <a href="https://www.twt.co.za/wheels" title="" itemprop="item">Wheels </a>
                <span>/ </span>
                <meta itemprop="name" content="Wheels">
                <meta itemprop="position" content="2">
            </li>

            <li class="category691" itemprop="itemListElement" itemscope="" itemtype="http://schema.org/ListItem">
                <a href="https://www.twt.co.za/by-wheel-size" title="" itemprop="item">By Wheel Diameter </a>
                <span>/ </span>
                <meta itemprop="name" content="By Wheel Diameter">
                <meta itemprop="position" content="3">
            </li>

            <li class="category708" itemprop="itemListElement" itemscope="" itemtype="http://schema.org/ListItem">
                <strong>17 Inch Wheels </strong>
                <meta itemprop="name" content="17 Inch Wheels">
                <meta itemprop="position" content="4">
            </li>

        </ul>
    </div>
</nav>

我想将面包屑的第二个和第三个元素捕获到 GTM 中的两个变量中。在本例中,“车轮”作为类别,“按车轮直径”作为子类别。

这里发现了一些只处理从面包屑中提取 URL 的示例,所以我绝对没有能力将其重新编写为 JavaScript。

这有点牵强,我可能会受到打击,但感觉非常困惑如何从网站获得干净的类别和子类别标识符,从理论上讲,这感觉是最好的方法它。当涉及到这么多元素时,只是有点迷失。

 function() { 
       var bcLink = document.getElementsByClassName("breadcrumbs");
       var data = [];
       for (i=0; i<bcLink.length; i++) { 
         data.push({ 'category' : bcLink[i].getAttribute("name") });
       }

    return data;
    }

最佳答案

您可以使用以下代码创建一个标签,将信息推送到数据层,然后创建两个单独的数据层变量来提取信息。

<script>
function breadcrumbLevel() { 
       var bcLink = document.querySelectorAll(".breadcrumbs li a");
       if(typeof bcLink != 'undefined'){
         var data = [];
         for (i=0; i<bcLink.length; i++) { 
           data.push(bcLink[i]);       
         }
         var bLevel1 = data[1].innerText;
         var bLevel2 = data[2].innerText;
         dataLayer.push({'breadcrumbLevel2': bLevel1,
                        'breadcrumbLevel3': bLevel2                   
                       });  
         }
}
breadcrumbLevel();
</script>

关于javascript - 希望将面包屑数据拉入 GTM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52207695/

相关文章:

javascript - 如何防止 mailto 事件在浏览器中打开新选项卡

javascript - 为什么我的页面在刷新期间在 Chrome 中触发 "ready"事件?

javascript - 如何修复GTM中的 "This language feature is only supported for ECMASCRIPT6 mode"?

javascript - Google 标签管理器错误 sumome iframe

javascript - 关闭时未执行 jquery 对话框的 beforeclose 回调

javascript - 将数组元素传递给 foreach div

javascript - "Uncaught TypeError: Object #<Object> has no method ' 使用AJAX后可拖动' "

javascript - 当 Google Analytics 由 Google Tag Manager 管理时,如何跟踪自定义事件?

javascript - 安装谷歌标签管理器后如何修复网站性能?

javascript - 使用 JS 在 Google Analytic 中设置目标