1import { getMetadata } from "./aem.js"; 2import { formatDate, getLanguage, getTags } from "./utils.js"; 3 4const SITE_ENV_MAPPING = { 5 localhost: "local", 6 qa: "qa", 7 dev: "dev", 8 stg: "stage", 9 uat: "uat", 10}; 11const PAGE_PATH = window.location.pathname; 12 13export function renderWBDataLayer() { 14 // const userIp = await getUserIp(); 15 16 let bussVPUnit = getMetadata("originating_unit"); 17 if (bussVPUnit && bussVPUnit.length > 0) { 18 bussVPUnit = bussVPUnit.substring(bussVPUnit.lastIndexOf(",") + 1).trim(); 19 } 20 let pageName = getMetadata("pagename") || ""; 21 const currentPath = window.location.pathname; 22 23 if (currentPath.includes("/country/")) { 24 pageName = currentPath.replace("/ext/", "").replace(/\//g, ":"); 25 } 26 27 else if (currentPath.includes("/topic/")) { 28 pageName = currentPath.replace("/ext/", "").replace(/\//g, ":"); 29 } 30 31 else if (currentPath.includes("/region/")) { 32 pageName = currentPath.replace("/ext/", "").replace(/\//g, ":"); 33 } 34 35 let contentType = getMetadata("content_type") || "other"; 36 if (currentPath.includes("/country/")) { 37 contentType = "country"; 38 } else if (currentPath.includes("/region/")) { 39 contentType = "Region"; 40 } else if (currentPath.includes("/topic/")) { 41 contentType = "Topic"; 42 } 43 44 getPathForSiteSection(); 45 46 window.wbgData.page = { 47 pageInfo: { 48 pageCategory: getMetadata("pagecategory") || getPageCategory(), 49 channel: getMetadata("channel") || getChannel(bussVPUnit), 50 contentType, 51 pageUid: getMetadata("pageuid") || "", 52 pageName, 53 pageFirstPub: formatDate(getMetadata("published-time")) || "", 54 pageLastMod: formatDate(getMetadata("modified-time")) || "", 55 webpackage: getMetadata("webpackage_id") || "", 56 topicTag: getTags("subject") || "", 57 }, 58 }; 59 60 window.wbgData.site = { 61 siteInfo: { 62 siteLanguage: getLanguage() || "en", 63 siteType: "main", 64 siteEnv: getEnvironment(), 65 siteCountry: getTags("country") || "", 66 siteRegion: getTags("region") || getTags("regions") || "", 67 }, 68 69 techInfo: { 70 cmsType: "aem edge", 71 bussVPUnit: bussVPUnit || "ECRCC", 72 bussUnit: getMetadata("buss_unit") || "ECRCC", 73 bussUserGroup: "external", 74 bussAgency: "ibrd", 75 }, 76 }; 77} 78 79function getPageCategory() { 80 let pageCategory = "other"; 81 const level = getPathLevel(PAGE_PATH); 82 83 if (PAGE_PATH.endsWith("home-page") || PAGE_PATH.endsWith("home")) { 84 pageCategory = "home"; 85 } else if (PAGE_PATH.includes("/search")) { 86 pageCategory = "search results"; 87 } else if (level === 1) { 88 pageCategory = "navigation landing"; 89 } else if (level === 2) { 90 pageCategory = "sub navigation"; 91 } else if (level >= 3) { 92 pageCategory = "content page"; 93 } 94 return pageCategory; 95} 96 97async function getUserIp() { 98 try { 99 const res = await fetch("https://api.ipify.org?format=json"); 100 const data = await res.json(); 101 return data.ip || ""; 102 } catch (e) { 103 console.error("IP fetch failed", e); 104 return ""; 105 } 106} 107 108function getPathLevel(path) { 109 return path.replace(/^\/|\/$/g, "").split("/").length - 2; // excluding ext and language 110} 111 112function getPathForSiteSection(level) { 113 const pathLevel = getPathLevel(PAGE_PATH); 114 if (pathLevel < level) { 115 return ""; 116 } else if (pathLevel >= level) { 117 const segments = PAGE_PATH.split(`/ext/${getLanguage()}`)?.[1]?.split("/"); 118 return segments?.slice(0, level + 1)?.join("/") || ""; 119 } 120} 121 122function getEnvironment() { 123 for (const key in SITE_ENV_MAPPING) { 124 if (window.location.hostname.includes(key)) { 125 return SITE_ENV_MAPPING[key]; 126 } 127 } 128 return "Prod"; 129} 130 131function getChannel(bussVPUnit) { 132 let siteName = ""; 133 const currentPath = window.location.pathname; 134 135 if (currentPath.includes("/topic/")) { 136 const urlParts = currentPath?.split("/topic/") || []; 137 siteName = urlParts?.[1] || ""; 138 siteName = siteName?.replace("-", " ")?.toLocaleUpperCase(); 139 } 140 return [bussVPUnit?.trim(), siteName?.trim(), "EXT"] 141 .filter(Boolean) 142 .join(" "); 143}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.