diff --git a/components/Image.tsx b/components/Image.tsx index 40ae4b97..9ac75965 100644 --- a/components/Image.tsx +++ b/components/Image.tsx @@ -19,17 +19,26 @@ const imageKit = new ImageKit({ const FACTORS = [1, 1.5, 2]; +export const getOptimizedMediaUrl = ( + { originalSrc, width, height, factor }: { + originalSrc: string; + width: number; + height?: number; + factor: number; + }, +) => + imageKit.url({ + path: originalSrc, + transformation: [{ + width: `${Math.trunc(factor * width)}`, + height: height ? `${Math.trunc(factor * height)}` : undefined, + }], + }); + export const getSrcSet = (src: string, width: number, height?: number) => FACTORS .map((factor) => - `${ - imageKit.url({ - path: src, - transformation: [{ - width: `${Math.trunc(factor * width)}`, - height: height ? `${Math.trunc(factor * height)}` : undefined, - }], - }) + `${getOptimizedMediaUrl({ originalSrc: src, width, height, factor })}) } ${Math.trunc(factor * width)}w` ) .join(", "); diff --git a/components/VTEXPortalDataLayerCompatibility.tsx b/components/VTEXPortalDataLayerCompatibility.tsx new file mode 100644 index 00000000..c8ad71ef --- /dev/null +++ b/components/VTEXPortalDataLayerCompatibility.tsx @@ -0,0 +1,163 @@ +import { Product } from "deco-sites/std/commerce/types.ts"; + +// deno-lint-ignore no-explicit-any +function addVTEXPortalDataSnippet(accountName: any) { + const url = new URL(window.location.href); + const structuredDataScripts = + document.querySelectorAll('script[type="application/ld+json"]') || []; + // deno-lint-ignore no-explicit-any + const structuredDatas: Record[] = []; + // deno-lint-ignore no-explicit-any + structuredDataScripts.forEach((v: any) => { + structuredDatas.push(JSON.parse(v.text)); + }); + const isProductPage = structuredDatas.some((s) => s["@type"] === "Product"); + + // deno-lint-ignore no-explicit-any + const props: Record = { + pageCategory: "Home", + pageDepartment: null, + pageUrl: window.location.href, + pageTitle: document.title, + skuStockOutFromShelf: [], + skuStockOutFromProductDetail: [], + accountName: `${accountName}`, + pageFacets: [], + shelfProductIds: [], + }; + + if (isProductPage) { + props.pageCategory = "Product"; + const scriptEl: HTMLScriptElement | null = document.querySelector( + 'script[data-id="vtex-portal-compat"]', + ); + if (scriptEl) { + Object.assign(props, JSON.parse(scriptEl.dataset.datalayer || "{}")); + } + } + + const breadcrumbSD = structuredDatas.find(( + s, + ) => (s["@type"] === "BreadcrumbList")); + if (breadcrumbSD) { + const department = breadcrumbSD?.itemListElement?.[0]; + props.pageDepartment = department?.name || null; + if (props.pageDepartment) { + !isProductPage && (props.pageCategory = "Category"); + const category = breadcrumbSD?.itemListElement + ?.[breadcrumbSD?.itemListElement.length - 1]; + // TODO: Corrigir na pDP + props.categoryName = category?.name; + } else { + props.pageCategory = new URL(window.location.href).pathname.split("/") + .filter(Boolean).join(" "); + } + } + + document.querySelectorAll("[data-product-id]").forEach( + (el) => { + props.shelfProductIds.push((el as HTMLDivElement).dataset.productId); + }, + ); + + window.dataLayer = window.dataLayer || []; + window.dataLayer.unshift(props); + + if (url.pathname === "/") { + window.dataLayer.push({ event: "homeView" }); + } else if (props.pageCategory === "Product") { + window.dataLayer.push({ event: "productView" }); + } else if (props.pageCategory === "Category") { + window.dataLayer.push({ event: "categoryView" }); + } else { + window.dataLayer.push({ event: "otherView" }); + } +} + +export function AddVTEXPortalData({ accountName }: { accountName: string }) { + return ( +