代码之家  ›  专栏  ›  技术社区  ›  Charl

希望将面包屑数据拉入GTM

  •  0
  • Charl  · 技术社区  · 6 年前

    坐在一个我正忙着标记的网站上和这个杂乱的小面包屑在一起。。。

    <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>
    

    我想将breadcrumb的第二个和第三个元素捕获到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;
        }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Nedo    6 年前

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

    <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>