I have this piece of code as a javascript custom variable in the Google Tag Manager. I would like to get breadcrumbs content.
This part of the code works well. But some pages do not have breadcrumbs, for these pages I use title instead. Because I do not want to have undefined in Google Analytics dimension.
So far everything is ok as a separate part of code. But If I try to check whether breadcrumbs variable is undefined (there is not breadcrumbs class on the page and breadcrumbs variable is based on the content of breadcrumbs class), I will not be able to check this. "IF" statement just does not work.
I have tried multiple approaches to check variable (typeof, check value ...) but nothing works for me.
Can you help me?
function() {
var breadcrumbs = document.getElementsByClassName('breadcrumbs').item(0).innerText;
if (typeof breadcrumbs === 'undefined'){
breadcrumbs = {{JS - Title}}.substring(0, breadcrumbs.indexOf(' |'));
}else{
breadcrumbs = breadcrumbs.replace(/(?:\r\n|\r|\n)/g, '| ');
}
return breadcrumbs;
}