坐在一个我正忙着标记的网站上和这个杂乱的小面包屑在一起。。。
<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;
}