@shopify/hydrogen-react
Version:
React components, hooks, and utilities for creating custom Shopify storefronts
65 lines (64 loc) • 1.76 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
function schemaWrapper(schemaId, payload) {
return {
schema_id: schemaId,
payload,
metadata: {
event_created_at_ms: Date.now()
}
};
}
function parseGid(gid) {
const defaultReturn = {
id: "",
resource: null,
resourceId: null,
search: "",
searchParams: new URLSearchParams(),
hash: ""
};
if (typeof gid !== "string") {
return defaultReturn;
}
try {
const { search, searchParams, pathname, hash } = new URL(gid);
const pathnameParts = pathname.split("/");
const lastPathnamePart = pathnameParts[pathnameParts.length - 1];
const resourcePart = pathnameParts[pathnameParts.length - 2];
if (!lastPathnamePart || !resourcePart) {
return defaultReturn;
}
const id = `${lastPathnamePart}${search}${hash}` || "";
const resourceId = lastPathnamePart || null;
const resource = resourcePart ?? null;
return { id, resource, resourceId, search, searchParams, hash };
} catch {
return defaultReturn;
}
}
function addDataIf(keyValuePairs, formattedData) {
if (typeof keyValuePairs !== "object") {
return {};
}
Object.entries(keyValuePairs).forEach(([key, value]) => {
if (value) {
formattedData[key] = value;
}
});
return formattedData;
}
function errorIfServer(fnName) {
if (typeof document === "undefined") {
console.error(
`${fnName} should only be used within the useEffect callback or event handlers`
);
return true;
}
return false;
}
exports.addDataIf = addDataIf;
exports.errorIfServer = errorIfServer;
exports.parseGid = parseGid;
exports.schemaWrapper = schemaWrapper;
//# sourceMappingURL=analytics-utils.js.map