希望将面包屑数据拉入 GTM
Looking to pull the breadcrumb data into GTM
我正忙着标记网站上这个凌乱的小面包屑...
<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中的两个变量中。在这种情况下,"Wheels" 作为类别,"By Wheel Diameter" 作为子类别。
在这里找到了一些示例,这些示例仅涉及从面包屑中提取 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>
我正忙着标记网站上这个凌乱的小面包屑...
<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中的两个变量中。在这种情况下,"Wheels" 作为类别,"By Wheel Diameter" 作为子类别。
在这里找到了一些示例,这些示例仅涉及从面包屑中提取 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>