UNPKG

@shopify/hydrogen-react

Version:

React components, hooks, and utilities for creating custom Shopify storefronts

93 lines (92 loc) 3.46 kB
const SHOPIFY_VISIT_TOKEN_HEADER = "X-Shopify-VisitToken"; const SHOPIFY_UNIQUE_TOKEN_HEADER = "X-Shopify-UniqueToken"; const cachedTrackingValues = { current: null }; function getTrackingValues() { var _a, _b, _c; let trackingValues; if (typeof window !== "undefined" && typeof window.performance !== "undefined") { try { const resourceRE = /^https?:\/\/([^/]+)(\/api\/(?:unstable|2\d{3}-\d{2})\/graphql\.json(?=$|\?))?/; const entries = performance.getEntriesByType( "resource" ); let matchedValues; for (let i = entries.length - 1; i >= 0; i--) { const entry = entries[i]; if (entry.initiatorType !== "fetch") continue; const currentHost = window.location.host; const match = entry.name.match(resourceRE); if (!match) continue; const [, matchedHost, sfapiPath] = match; const isMatch = ( // Same origin (exact host match) matchedHost === currentHost || // Subdomain with SFAPI path sfapiPath && (matchedHost == null ? void 0 : matchedHost.endsWith(`.${currentHost}`)) ); if (isMatch) { const values = extractFromPerformanceEntry(entry); if (values) { matchedValues = values; break; } } } if (matchedValues) { trackingValues = matchedValues; } if (trackingValues) { cachedTrackingValues.current = trackingValues; } else if (cachedTrackingValues.current) { trackingValues = cachedTrackingValues.current; } if (!trackingValues) { const navigationEntries = performance.getEntriesByType( "navigation" )[0]; trackingValues = extractFromPerformanceEntry(navigationEntries, false); } } catch { } } if (!trackingValues) { const cookie = ( // Read from arguments to avoid declaring parameters in this function signature. // This logic is only used internally from `getShopifyCookies` and will be deprecated. typeof arguments[0] === "string" ? arguments[0] : typeof document !== "undefined" ? document.cookie : "" ); trackingValues = { uniqueToken: ((_a = cookie.match(/\b_shopify_y=([^;]+)/)) == null ? void 0 : _a[1]) || "", visitToken: ((_b = cookie.match(/\b_shopify_s=([^;]+)/)) == null ? void 0 : _b[1]) || "", consent: ((_c = cookie.match(/\b_tracking_consent=([^;]+)/)) == null ? void 0 : _c[1]) || "" }; } return trackingValues; } function extractFromPerformanceEntry(entry, isConsentRequired = true) { let uniqueToken = ""; let visitToken = ""; let consent = ""; const serverTiming = entry.serverTiming; if (serverTiming && serverTiming.length >= 3) { for (let i = serverTiming.length - 1; i >= 0; i--) { const { name, description } = serverTiming[i]; if (!name || !description) continue; if (name === "_y") { uniqueToken = description; } else if (name === "_s") { visitToken = description; } else if (name === "_cmp") { consent = description; } if (uniqueToken && visitToken && consent) break; } } return uniqueToken && visitToken && (isConsentRequired ? consent : true) ? { uniqueToken, visitToken, consent } : void 0; } export { SHOPIFY_UNIQUE_TOKEN_HEADER, SHOPIFY_VISIT_TOKEN_HEADER, cachedTrackingValues, getTrackingValues }; //# sourceMappingURL=tracking-utils.mjs.map