I'm are working on the dataLayer for our project and I'm pushing title,
url, section, subsection, subsubsection & subsubsubsection from the
request URL.Please find the below sample url and split of different
sections in the URL. Please find the below code snippet from
dataLayer.htmlI want to push data only when it is available. For
instance, if I don't have subsubsection in my URL, I don't wish to see
even empty subsubsection(subsubsection:"") inside the dataLayer object.
window.dataLayer =
window.dataLayer || []; var urlWithoutExt =
window.location.href.replace(location.search, '').replace(/\.[^/.]+$/,
""); console.log("urlWithoutExt::::"+urlWithoutExt); var tempsubsection
= ""; var tempsubsubsection = ""; var tempsubsubsubsection = "";
if(urlWithoutExt.toString().split("/")[3] != null){ tempsubsection =
urlWithoutExt.toString().split("/")[3]; }
if(urlWithoutExt.toString().split("/")[4] != null){ tempsubsubsection =
urlWithoutExt.toString().split("/")[4]; }
if(urlWithoutExt.toString().split("/")[5] != null){ tempsubsubsubsection
= urlWithoutExt.toString().split("/")[5]; } window.dataLayer.push({
event: 'content_landing', eventInfo: { title: '${currentPage.title ||
currentPage.name @ context="text"}', url:
window.location.href.replace(location.search, ''), section:
window.location.hostname, subsection: tempsubsection, subsubsection:
tempsubsubsection, subsubsubsection: tempsubsubsubsection } });
Please find the below ...