Place this code before the integration (as high as possible) of Google Tag Manager on your pages:
<?=Breadcrumb::c()->dataLayer()?>
You will now have two variables (SiteBreadcrumb
and Breadcrumb
) in the dataLayer of your container.
in GTM:
With the SiteBreadcrumb
variable, you will be able to identify eligible pages with a trigger;
Then get the Breadcrumb
variable and with this content in a custom tag, everything is done:
<script>
(function(){
var ld =
{
"@context": "http://schema.org",
"@type": "BreadcrumbList",
"itemListElement": {{Breadcrumb}}
};
var s= document.createElement('script');
s.setAttribute('type', 'application/ld+json');
s.textContent = JSON.stringify(ld);
document.body.appendChild(s);
})();
</script>
Put this snippet of code in the desired location and use Google's Rich Results Testing Tool to preview the result.
<?=Breadcrumb::c()->List()?>