@shopify/hydrogen-react 2023.7.1 → 2023.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser-dev/analytics-utils.mjs +21 -6
- package/dist/browser-dev/analytics-utils.mjs.map +1 -1
- package/dist/browser-prod/analytics-utils.mjs +21 -6
- package/dist/browser-prod/analytics-utils.mjs.map +1 -1
- package/dist/node-dev/analytics-utils.js +21 -6
- package/dist/node-dev/analytics-utils.js.map +1 -1
- package/dist/node-dev/analytics-utils.mjs +21 -6
- package/dist/node-dev/analytics-utils.mjs.map +1 -1
- package/dist/node-prod/analytics-utils.js +21 -6
- package/dist/node-prod/analytics-utils.js.map +1 -1
- package/dist/node-prod/analytics-utils.mjs +21 -6
- package/dist/node-prod/analytics-utils.mjs.map +1 -1
- package/dist/types/analytics-types.d.ts +2 -1
- package/dist/umd/hydrogen-react.dev.js +21 -6
- package/dist/umd/hydrogen-react.dev.js.map +1 -1
- package/dist/umd/hydrogen-react.prod.js +5 -5
- package/dist/umd/hydrogen-react.prod.js.map +1 -1
- package/package.json +1 -1
|
@@ -8,17 +8,32 @@ function schemaWrapper(schemaId, payload) {
|
|
|
8
8
|
};
|
|
9
9
|
}
|
|
10
10
|
function parseGid(gid) {
|
|
11
|
-
const defaultReturn = {
|
|
11
|
+
const defaultReturn = {
|
|
12
|
+
id: "",
|
|
13
|
+
resource: null,
|
|
14
|
+
resourceId: null,
|
|
15
|
+
search: "",
|
|
16
|
+
searchParams: new URLSearchParams(),
|
|
17
|
+
hash: ""
|
|
18
|
+
};
|
|
12
19
|
if (typeof gid !== "string") {
|
|
13
20
|
return defaultReturn;
|
|
14
21
|
}
|
|
15
|
-
|
|
16
|
-
|
|
22
|
+
try {
|
|
23
|
+
const { search, searchParams, pathname, hash } = new URL(gid);
|
|
24
|
+
const pathnameParts = pathname.split("/");
|
|
25
|
+
const lastPathnamePart = pathnameParts[pathnameParts.length - 1];
|
|
26
|
+
const resourcePart = pathnameParts[pathnameParts.length - 2];
|
|
27
|
+
if (!lastPathnamePart || !resourcePart) {
|
|
28
|
+
return defaultReturn;
|
|
29
|
+
}
|
|
30
|
+
const id = `${lastPathnamePart}${search}${hash}` || "";
|
|
31
|
+
const resourceId = lastPathnamePart || null;
|
|
32
|
+
const resource = resourcePart ?? null;
|
|
33
|
+
return { id, resource, resourceId, search, searchParams, hash };
|
|
34
|
+
} catch {
|
|
17
35
|
return defaultReturn;
|
|
18
36
|
}
|
|
19
|
-
const id = matches[2] ?? null;
|
|
20
|
-
const resource = matches[1] ?? null;
|
|
21
|
-
return { id, resource };
|
|
22
37
|
}
|
|
23
38
|
function addDataIf(keyValuePairs, formattedData) {
|
|
24
39
|
if (typeof keyValuePairs !== "object") {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"analytics-utils.mjs","sources":["../../src/analytics-utils.ts"],"sourcesContent":["import type {\n ShopifyMonorailPayload,\n ShopifyMonorailEvent,\n ShopifyGid,\n} from './analytics-types.js';\n\n/**\n * Builds a Shopify Monorail event from a Shopify Monorail payload and a schema ID.\n * @param payload - The Monorail payload\n * @param schemaId - The schema ID to use\n * @returns The formatted payload\n **/\nexport function schemaWrapper(\n schemaId: string,\n payload: ShopifyMonorailPayload,\n): ShopifyMonorailEvent {\n return {\n schema_id: schemaId,\n payload,\n metadata: {\n event_created_at_ms: Date.now(),\n },\n };\n}\n\n/**\n * Parses global id (gid) and returns the resource type and id.\n * @see https://shopify.dev/api/usage/gids\n * @param gid - A shopify GID (string)\n *\n * @example\n * ```ts\n * const {id, resource} = parseGid('gid://shopify/Order/123')\n * // => id = \"123\", resource = 'Order'\n *\n * * const {id, resource} = parseGid('gid://shopify/Cart/abc123')\n * // => id = \"abc123\", resource = 'Cart'\n * ```\n **/\nexport function parseGid(gid: string | undefined): ShopifyGid {\n const defaultReturn = {id: ''
|
|
1
|
+
{"version":3,"file":"analytics-utils.mjs","sources":["../../src/analytics-utils.ts"],"sourcesContent":["import type {\n ShopifyMonorailPayload,\n ShopifyMonorailEvent,\n ShopifyGid,\n} from './analytics-types.js';\n\n/**\n * Builds a Shopify Monorail event from a Shopify Monorail payload and a schema ID.\n * @param payload - The Monorail payload\n * @param schemaId - The schema ID to use\n * @returns The formatted payload\n **/\nexport function schemaWrapper(\n schemaId: string,\n payload: ShopifyMonorailPayload,\n): ShopifyMonorailEvent {\n return {\n schema_id: schemaId,\n payload,\n metadata: {\n event_created_at_ms: Date.now(),\n },\n };\n}\n\n/**\n * Parses global id (gid) and returns the resource type and id.\n * @see https://shopify.dev/api/usage/gids\n * @param gid - A shopify GID (string)\n *\n * @example\n * ```ts\n * const {id, resource} = parseGid('gid://shopify/Order/123')\n * // => id = \"123\", resource = 'Order'\n *\n * * const {id, resource} = parseGid('gid://shopify/Cart/abc123')\n * // => id = \"abc123\", resource = 'Cart'\n * ```\n **/\nexport function parseGid(gid: string | undefined): ShopifyGid {\n const defaultReturn: ShopifyGid = {\n id: '',\n resource: null,\n resourceId: null,\n search: '',\n searchParams: new URLSearchParams(),\n hash: '',\n };\n\n if (typeof gid !== 'string') {\n return defaultReturn;\n }\n\n try {\n const {search, searchParams, pathname, hash} = new URL(gid);\n const pathnameParts = pathname.split('/');\n const lastPathnamePart = pathnameParts[pathnameParts.length - 1];\n const resourcePart = pathnameParts[pathnameParts.length - 2];\n\n if (!lastPathnamePart || !resourcePart) {\n return defaultReturn;\n }\n\n const id = `${lastPathnamePart}${search}${hash}` || '';\n const resourceId = lastPathnamePart || null;\n const resource = resourcePart ?? null;\n\n return {id, resource, resourceId, search, searchParams, hash};\n } catch {\n return defaultReturn;\n }\n}\n\n/**\n * Filters properties from an object and returns a new object with only the properties that have a truthy value.\n * @param keyValuePairs - An object of key-value pairs\n * @param formattedData - An object which will hold the truthy values\n * @returns The formatted object\n **/\nexport function addDataIf(\n keyValuePairs: ShopifyMonorailPayload,\n formattedData: ShopifyMonorailPayload,\n): ShopifyMonorailPayload {\n if (typeof keyValuePairs !== 'object') {\n return {};\n }\n Object.entries(keyValuePairs).forEach(([key, value]) => {\n if (value) {\n formattedData[key] = value;\n }\n });\n return formattedData;\n}\n\n/**\n * Utility that errors if a function is called on the server.\n * @param fnName - The name of the function\n * @returns A boolean\n **/\nexport function errorIfServer(fnName: string): boolean {\n if (typeof document === 'undefined') {\n console.error(\n `${fnName} should only be used within the useEffect callback or event handlers`,\n );\n return true;\n }\n return false;\n}\n"],"names":[],"mappings":"AAYgB,SAAA,cACd,UACA,SACsB;AACf,SAAA;AAAA,IACL,WAAW;AAAA,IACX;AAAA,IACA,UAAU;AAAA,MACR,qBAAqB,KAAK,IAAI;AAAA,IAChC;AAAA,EAAA;AAEJ;AAgBO,SAAS,SAAS,KAAqC;AAC5D,QAAM,gBAA4B;AAAA,IAChC,IAAI;AAAA,IACJ,UAAU;AAAA,IACV,YAAY;AAAA,IACZ,QAAQ;AAAA,IACR,cAAc,IAAI,gBAAgB;AAAA,IAClC,MAAM;AAAA,EAAA;AAGJ,MAAA,OAAO,QAAQ,UAAU;AACpB,WAAA;AAAA,EACT;AAEI,MAAA;AACI,UAAA,EAAC,QAAQ,cAAc,UAAU,SAAQ,IAAI,IAAI,GAAG;AACpD,UAAA,gBAAgB,SAAS,MAAM,GAAG;AACxC,UAAM,mBAAmB,cAAc,cAAc,SAAS,CAAC;AAC/D,UAAM,eAAe,cAAc,cAAc,SAAS,CAAC;AAEvD,QAAA,CAAC,oBAAoB,CAAC,cAAc;AAC/B,aAAA;AAAA,IACT;AAEA,UAAM,KAAK,GAAG,gBAAgB,GAAG,MAAM,GAAG,IAAI,MAAM;AACpD,UAAM,aAAa,oBAAoB;AACvC,UAAM,WAAW,gBAAgB;AAEjC,WAAO,EAAC,IAAI,UAAU,YAAY,QAAQ,cAAc;EAAI,QACtD;AACC,WAAA;AAAA,EACT;AACF;AAQgB,SAAA,UACd,eACA,eACwB;AACpB,MAAA,OAAO,kBAAkB,UAAU;AACrC,WAAO;EACT;AACO,SAAA,QAAQ,aAAa,EAAE,QAAQ,CAAC,CAAC,KAAK,KAAK,MAAM;AACtD,QAAI,OAAO;AACT,oBAAc,GAAG,IAAI;AAAA,IACvB;AAAA,EAAA,CACD;AACM,SAAA;AACT;AAOO,SAAS,cAAc,QAAyB;AACjD,MAAA,OAAO,aAAa,aAAa;AAC3B,YAAA;AAAA,MACN,GAAG,MAAM;AAAA,IAAA;AAEJ,WAAA;AAAA,EACT;AACO,SAAA;AACT;"}
|
|
@@ -8,17 +8,32 @@ function schemaWrapper(schemaId, payload) {
|
|
|
8
8
|
};
|
|
9
9
|
}
|
|
10
10
|
function parseGid(gid) {
|
|
11
|
-
const defaultReturn = {
|
|
11
|
+
const defaultReturn = {
|
|
12
|
+
id: "",
|
|
13
|
+
resource: null,
|
|
14
|
+
resourceId: null,
|
|
15
|
+
search: "",
|
|
16
|
+
searchParams: new URLSearchParams(),
|
|
17
|
+
hash: ""
|
|
18
|
+
};
|
|
12
19
|
if (typeof gid !== "string") {
|
|
13
20
|
return defaultReturn;
|
|
14
21
|
}
|
|
15
|
-
|
|
16
|
-
|
|
22
|
+
try {
|
|
23
|
+
const { search, searchParams, pathname, hash } = new URL(gid);
|
|
24
|
+
const pathnameParts = pathname.split("/");
|
|
25
|
+
const lastPathnamePart = pathnameParts[pathnameParts.length - 1];
|
|
26
|
+
const resourcePart = pathnameParts[pathnameParts.length - 2];
|
|
27
|
+
if (!lastPathnamePart || !resourcePart) {
|
|
28
|
+
return defaultReturn;
|
|
29
|
+
}
|
|
30
|
+
const id = `${lastPathnamePart}${search}${hash}` || "";
|
|
31
|
+
const resourceId = lastPathnamePart || null;
|
|
32
|
+
const resource = resourcePart ?? null;
|
|
33
|
+
return { id, resource, resourceId, search, searchParams, hash };
|
|
34
|
+
} catch {
|
|
17
35
|
return defaultReturn;
|
|
18
36
|
}
|
|
19
|
-
const id = matches[2] ?? null;
|
|
20
|
-
const resource = matches[1] ?? null;
|
|
21
|
-
return { id, resource };
|
|
22
37
|
}
|
|
23
38
|
function addDataIf(keyValuePairs, formattedData) {
|
|
24
39
|
if (typeof keyValuePairs !== "object") {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"analytics-utils.mjs","sources":["../../src/analytics-utils.ts"],"sourcesContent":["import type {\n ShopifyMonorailPayload,\n ShopifyMonorailEvent,\n ShopifyGid,\n} from './analytics-types.js';\n\n/**\n * Builds a Shopify Monorail event from a Shopify Monorail payload and a schema ID.\n * @param payload - The Monorail payload\n * @param schemaId - The schema ID to use\n * @returns The formatted payload\n **/\nexport function schemaWrapper(\n schemaId: string,\n payload: ShopifyMonorailPayload,\n): ShopifyMonorailEvent {\n return {\n schema_id: schemaId,\n payload,\n metadata: {\n event_created_at_ms: Date.now(),\n },\n };\n}\n\n/**\n * Parses global id (gid) and returns the resource type and id.\n * @see https://shopify.dev/api/usage/gids\n * @param gid - A shopify GID (string)\n *\n * @example\n * ```ts\n * const {id, resource} = parseGid('gid://shopify/Order/123')\n * // => id = \"123\", resource = 'Order'\n *\n * * const {id, resource} = parseGid('gid://shopify/Cart/abc123')\n * // => id = \"abc123\", resource = 'Cart'\n * ```\n **/\nexport function parseGid(gid: string | undefined): ShopifyGid {\n const defaultReturn = {id: ''
|
|
1
|
+
{"version":3,"file":"analytics-utils.mjs","sources":["../../src/analytics-utils.ts"],"sourcesContent":["import type {\n ShopifyMonorailPayload,\n ShopifyMonorailEvent,\n ShopifyGid,\n} from './analytics-types.js';\n\n/**\n * Builds a Shopify Monorail event from a Shopify Monorail payload and a schema ID.\n * @param payload - The Monorail payload\n * @param schemaId - The schema ID to use\n * @returns The formatted payload\n **/\nexport function schemaWrapper(\n schemaId: string,\n payload: ShopifyMonorailPayload,\n): ShopifyMonorailEvent {\n return {\n schema_id: schemaId,\n payload,\n metadata: {\n event_created_at_ms: Date.now(),\n },\n };\n}\n\n/**\n * Parses global id (gid) and returns the resource type and id.\n * @see https://shopify.dev/api/usage/gids\n * @param gid - A shopify GID (string)\n *\n * @example\n * ```ts\n * const {id, resource} = parseGid('gid://shopify/Order/123')\n * // => id = \"123\", resource = 'Order'\n *\n * * const {id, resource} = parseGid('gid://shopify/Cart/abc123')\n * // => id = \"abc123\", resource = 'Cart'\n * ```\n **/\nexport function parseGid(gid: string | undefined): ShopifyGid {\n const defaultReturn: ShopifyGid = {\n id: '',\n resource: null,\n resourceId: null,\n search: '',\n searchParams: new URLSearchParams(),\n hash: '',\n };\n\n if (typeof gid !== 'string') {\n return defaultReturn;\n }\n\n try {\n const {search, searchParams, pathname, hash} = new URL(gid);\n const pathnameParts = pathname.split('/');\n const lastPathnamePart = pathnameParts[pathnameParts.length - 1];\n const resourcePart = pathnameParts[pathnameParts.length - 2];\n\n if (!lastPathnamePart || !resourcePart) {\n return defaultReturn;\n }\n\n const id = `${lastPathnamePart}${search}${hash}` || '';\n const resourceId = lastPathnamePart || null;\n const resource = resourcePart ?? null;\n\n return {id, resource, resourceId, search, searchParams, hash};\n } catch {\n return defaultReturn;\n }\n}\n\n/**\n * Filters properties from an object and returns a new object with only the properties that have a truthy value.\n * @param keyValuePairs - An object of key-value pairs\n * @param formattedData - An object which will hold the truthy values\n * @returns The formatted object\n **/\nexport function addDataIf(\n keyValuePairs: ShopifyMonorailPayload,\n formattedData: ShopifyMonorailPayload,\n): ShopifyMonorailPayload {\n if (typeof keyValuePairs !== 'object') {\n return {};\n }\n Object.entries(keyValuePairs).forEach(([key, value]) => {\n if (value) {\n formattedData[key] = value;\n }\n });\n return formattedData;\n}\n\n/**\n * Utility that errors if a function is called on the server.\n * @param fnName - The name of the function\n * @returns A boolean\n **/\nexport function errorIfServer(fnName: string): boolean {\n if (typeof document === 'undefined') {\n console.error(\n `${fnName} should only be used within the useEffect callback or event handlers`,\n );\n return true;\n }\n return false;\n}\n"],"names":[],"mappings":"AAYgB,SAAA,cACd,UACA,SACsB;AACf,SAAA;AAAA,IACL,WAAW;AAAA,IACX;AAAA,IACA,UAAU;AAAA,MACR,qBAAqB,KAAK,IAAI;AAAA,IAChC;AAAA,EAAA;AAEJ;AAgBO,SAAS,SAAS,KAAqC;AAC5D,QAAM,gBAA4B;AAAA,IAChC,IAAI;AAAA,IACJ,UAAU;AAAA,IACV,YAAY;AAAA,IACZ,QAAQ;AAAA,IACR,cAAc,IAAI,gBAAgB;AAAA,IAClC,MAAM;AAAA,EAAA;AAGJ,MAAA,OAAO,QAAQ,UAAU;AACpB,WAAA;AAAA,EACT;AAEI,MAAA;AACI,UAAA,EAAC,QAAQ,cAAc,UAAU,SAAQ,IAAI,IAAI,GAAG;AACpD,UAAA,gBAAgB,SAAS,MAAM,GAAG;AACxC,UAAM,mBAAmB,cAAc,cAAc,SAAS,CAAC;AAC/D,UAAM,eAAe,cAAc,cAAc,SAAS,CAAC;AAEvD,QAAA,CAAC,oBAAoB,CAAC,cAAc;AAC/B,aAAA;AAAA,IACT;AAEA,UAAM,KAAK,GAAG,gBAAgB,GAAG,MAAM,GAAG,IAAI,MAAM;AACpD,UAAM,aAAa,oBAAoB;AACvC,UAAM,WAAW,gBAAgB;AAEjC,WAAO,EAAC,IAAI,UAAU,YAAY,QAAQ,cAAc;EAAI,QACtD;AACC,WAAA;AAAA,EACT;AACF;AAQgB,SAAA,UACd,eACA,eACwB;AACpB,MAAA,OAAO,kBAAkB,UAAU;AACrC,WAAO;EACT;AACO,SAAA,QAAQ,aAAa,EAAE,QAAQ,CAAC,CAAC,KAAK,KAAK,MAAM;AACtD,QAAI,OAAO;AACT,oBAAc,GAAG,IAAI;AAAA,IACvB;AAAA,EAAA,CACD;AACM,SAAA;AACT;AAOO,SAAS,cAAc,QAAyB;AACjD,MAAA,OAAO,aAAa,aAAa;AAC3B,YAAA;AAAA,MACN,GAAG,MAAM;AAAA,IAAA;AAEJ,WAAA;AAAA,EACT;AACO,SAAA;AACT;"}
|
|
@@ -10,17 +10,32 @@ function schemaWrapper(schemaId, payload) {
|
|
|
10
10
|
};
|
|
11
11
|
}
|
|
12
12
|
function parseGid(gid) {
|
|
13
|
-
const defaultReturn = {
|
|
13
|
+
const defaultReturn = {
|
|
14
|
+
id: "",
|
|
15
|
+
resource: null,
|
|
16
|
+
resourceId: null,
|
|
17
|
+
search: "",
|
|
18
|
+
searchParams: new URLSearchParams(),
|
|
19
|
+
hash: ""
|
|
20
|
+
};
|
|
14
21
|
if (typeof gid !== "string") {
|
|
15
22
|
return defaultReturn;
|
|
16
23
|
}
|
|
17
|
-
|
|
18
|
-
|
|
24
|
+
try {
|
|
25
|
+
const { search, searchParams, pathname, hash } = new URL(gid);
|
|
26
|
+
const pathnameParts = pathname.split("/");
|
|
27
|
+
const lastPathnamePart = pathnameParts[pathnameParts.length - 1];
|
|
28
|
+
const resourcePart = pathnameParts[pathnameParts.length - 2];
|
|
29
|
+
if (!lastPathnamePart || !resourcePart) {
|
|
30
|
+
return defaultReturn;
|
|
31
|
+
}
|
|
32
|
+
const id = `${lastPathnamePart}${search}${hash}` || "";
|
|
33
|
+
const resourceId = lastPathnamePart || null;
|
|
34
|
+
const resource = resourcePart ?? null;
|
|
35
|
+
return { id, resource, resourceId, search, searchParams, hash };
|
|
36
|
+
} catch {
|
|
19
37
|
return defaultReturn;
|
|
20
38
|
}
|
|
21
|
-
const id = matches[2] ?? null;
|
|
22
|
-
const resource = matches[1] ?? null;
|
|
23
|
-
return { id, resource };
|
|
24
39
|
}
|
|
25
40
|
function addDataIf(keyValuePairs, formattedData) {
|
|
26
41
|
if (typeof keyValuePairs !== "object") {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"analytics-utils.js","sources":["../../src/analytics-utils.ts"],"sourcesContent":["import type {\n ShopifyMonorailPayload,\n ShopifyMonorailEvent,\n ShopifyGid,\n} from './analytics-types.js';\n\n/**\n * Builds a Shopify Monorail event from a Shopify Monorail payload and a schema ID.\n * @param payload - The Monorail payload\n * @param schemaId - The schema ID to use\n * @returns The formatted payload\n **/\nexport function schemaWrapper(\n schemaId: string,\n payload: ShopifyMonorailPayload,\n): ShopifyMonorailEvent {\n return {\n schema_id: schemaId,\n payload,\n metadata: {\n event_created_at_ms: Date.now(),\n },\n };\n}\n\n/**\n * Parses global id (gid) and returns the resource type and id.\n * @see https://shopify.dev/api/usage/gids\n * @param gid - A shopify GID (string)\n *\n * @example\n * ```ts\n * const {id, resource} = parseGid('gid://shopify/Order/123')\n * // => id = \"123\", resource = 'Order'\n *\n * * const {id, resource} = parseGid('gid://shopify/Cart/abc123')\n * // => id = \"abc123\", resource = 'Cart'\n * ```\n **/\nexport function parseGid(gid: string | undefined): ShopifyGid {\n const defaultReturn = {id: ''
|
|
1
|
+
{"version":3,"file":"analytics-utils.js","sources":["../../src/analytics-utils.ts"],"sourcesContent":["import type {\n ShopifyMonorailPayload,\n ShopifyMonorailEvent,\n ShopifyGid,\n} from './analytics-types.js';\n\n/**\n * Builds a Shopify Monorail event from a Shopify Monorail payload and a schema ID.\n * @param payload - The Monorail payload\n * @param schemaId - The schema ID to use\n * @returns The formatted payload\n **/\nexport function schemaWrapper(\n schemaId: string,\n payload: ShopifyMonorailPayload,\n): ShopifyMonorailEvent {\n return {\n schema_id: schemaId,\n payload,\n metadata: {\n event_created_at_ms: Date.now(),\n },\n };\n}\n\n/**\n * Parses global id (gid) and returns the resource type and id.\n * @see https://shopify.dev/api/usage/gids\n * @param gid - A shopify GID (string)\n *\n * @example\n * ```ts\n * const {id, resource} = parseGid('gid://shopify/Order/123')\n * // => id = \"123\", resource = 'Order'\n *\n * * const {id, resource} = parseGid('gid://shopify/Cart/abc123')\n * // => id = \"abc123\", resource = 'Cart'\n * ```\n **/\nexport function parseGid(gid: string | undefined): ShopifyGid {\n const defaultReturn: ShopifyGid = {\n id: '',\n resource: null,\n resourceId: null,\n search: '',\n searchParams: new URLSearchParams(),\n hash: '',\n };\n\n if (typeof gid !== 'string') {\n return defaultReturn;\n }\n\n try {\n const {search, searchParams, pathname, hash} = new URL(gid);\n const pathnameParts = pathname.split('/');\n const lastPathnamePart = pathnameParts[pathnameParts.length - 1];\n const resourcePart = pathnameParts[pathnameParts.length - 2];\n\n if (!lastPathnamePart || !resourcePart) {\n return defaultReturn;\n }\n\n const id = `${lastPathnamePart}${search}${hash}` || '';\n const resourceId = lastPathnamePart || null;\n const resource = resourcePart ?? null;\n\n return {id, resource, resourceId, search, searchParams, hash};\n } catch {\n return defaultReturn;\n }\n}\n\n/**\n * Filters properties from an object and returns a new object with only the properties that have a truthy value.\n * @param keyValuePairs - An object of key-value pairs\n * @param formattedData - An object which will hold the truthy values\n * @returns The formatted object\n **/\nexport function addDataIf(\n keyValuePairs: ShopifyMonorailPayload,\n formattedData: ShopifyMonorailPayload,\n): ShopifyMonorailPayload {\n if (typeof keyValuePairs !== 'object') {\n return {};\n }\n Object.entries(keyValuePairs).forEach(([key, value]) => {\n if (value) {\n formattedData[key] = value;\n }\n });\n return formattedData;\n}\n\n/**\n * Utility that errors if a function is called on the server.\n * @param fnName - The name of the function\n * @returns A boolean\n **/\nexport function errorIfServer(fnName: string): boolean {\n if (typeof document === 'undefined') {\n console.error(\n `${fnName} should only be used within the useEffect callback or event handlers`,\n );\n return true;\n }\n return false;\n}\n"],"names":[],"mappings":";;AAYgB,SAAA,cACd,UACA,SACsB;AACf,SAAA;AAAA,IACL,WAAW;AAAA,IACX;AAAA,IACA,UAAU;AAAA,MACR,qBAAqB,KAAK,IAAI;AAAA,IAChC;AAAA,EAAA;AAEJ;AAgBO,SAAS,SAAS,KAAqC;AAC5D,QAAM,gBAA4B;AAAA,IAChC,IAAI;AAAA,IACJ,UAAU;AAAA,IACV,YAAY;AAAA,IACZ,QAAQ;AAAA,IACR,cAAc,IAAI,gBAAgB;AAAA,IAClC,MAAM;AAAA,EAAA;AAGJ,MAAA,OAAO,QAAQ,UAAU;AACpB,WAAA;AAAA,EACT;AAEI,MAAA;AACI,UAAA,EAAC,QAAQ,cAAc,UAAU,SAAQ,IAAI,IAAI,GAAG;AACpD,UAAA,gBAAgB,SAAS,MAAM,GAAG;AACxC,UAAM,mBAAmB,cAAc,cAAc,SAAS,CAAC;AAC/D,UAAM,eAAe,cAAc,cAAc,SAAS,CAAC;AAEvD,QAAA,CAAC,oBAAoB,CAAC,cAAc;AAC/B,aAAA;AAAA,IACT;AAEA,UAAM,KAAK,GAAG,gBAAgB,GAAG,MAAM,GAAG,IAAI,MAAM;AACpD,UAAM,aAAa,oBAAoB;AACvC,UAAM,WAAW,gBAAgB;AAEjC,WAAO,EAAC,IAAI,UAAU,YAAY,QAAQ,cAAc;EAAI,QACtD;AACC,WAAA;AAAA,EACT;AACF;AAQgB,SAAA,UACd,eACA,eACwB;AACpB,MAAA,OAAO,kBAAkB,UAAU;AACrC,WAAO;EACT;AACO,SAAA,QAAQ,aAAa,EAAE,QAAQ,CAAC,CAAC,KAAK,KAAK,MAAM;AACtD,QAAI,OAAO;AACT,oBAAc,GAAG,IAAI;AAAA,IACvB;AAAA,EAAA,CACD;AACM,SAAA;AACT;AAOO,SAAS,cAAc,QAAyB;AACjD,MAAA,OAAO,aAAa,aAAa;AAC3B,YAAA;AAAA,MACN,GAAG,MAAM;AAAA,IAAA;AAEJ,WAAA;AAAA,EACT;AACO,SAAA;AACT;;;;;"}
|
|
@@ -8,17 +8,32 @@ function schemaWrapper(schemaId, payload) {
|
|
|
8
8
|
};
|
|
9
9
|
}
|
|
10
10
|
function parseGid(gid) {
|
|
11
|
-
const defaultReturn = {
|
|
11
|
+
const defaultReturn = {
|
|
12
|
+
id: "",
|
|
13
|
+
resource: null,
|
|
14
|
+
resourceId: null,
|
|
15
|
+
search: "",
|
|
16
|
+
searchParams: new URLSearchParams(),
|
|
17
|
+
hash: ""
|
|
18
|
+
};
|
|
12
19
|
if (typeof gid !== "string") {
|
|
13
20
|
return defaultReturn;
|
|
14
21
|
}
|
|
15
|
-
|
|
16
|
-
|
|
22
|
+
try {
|
|
23
|
+
const { search, searchParams, pathname, hash } = new URL(gid);
|
|
24
|
+
const pathnameParts = pathname.split("/");
|
|
25
|
+
const lastPathnamePart = pathnameParts[pathnameParts.length - 1];
|
|
26
|
+
const resourcePart = pathnameParts[pathnameParts.length - 2];
|
|
27
|
+
if (!lastPathnamePart || !resourcePart) {
|
|
28
|
+
return defaultReturn;
|
|
29
|
+
}
|
|
30
|
+
const id = `${lastPathnamePart}${search}${hash}` || "";
|
|
31
|
+
const resourceId = lastPathnamePart || null;
|
|
32
|
+
const resource = resourcePart ?? null;
|
|
33
|
+
return { id, resource, resourceId, search, searchParams, hash };
|
|
34
|
+
} catch {
|
|
17
35
|
return defaultReturn;
|
|
18
36
|
}
|
|
19
|
-
const id = matches[2] ?? null;
|
|
20
|
-
const resource = matches[1] ?? null;
|
|
21
|
-
return { id, resource };
|
|
22
37
|
}
|
|
23
38
|
function addDataIf(keyValuePairs, formattedData) {
|
|
24
39
|
if (typeof keyValuePairs !== "object") {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"analytics-utils.mjs","sources":["../../src/analytics-utils.ts"],"sourcesContent":["import type {\n ShopifyMonorailPayload,\n ShopifyMonorailEvent,\n ShopifyGid,\n} from './analytics-types.js';\n\n/**\n * Builds a Shopify Monorail event from a Shopify Monorail payload and a schema ID.\n * @param payload - The Monorail payload\n * @param schemaId - The schema ID to use\n * @returns The formatted payload\n **/\nexport function schemaWrapper(\n schemaId: string,\n payload: ShopifyMonorailPayload,\n): ShopifyMonorailEvent {\n return {\n schema_id: schemaId,\n payload,\n metadata: {\n event_created_at_ms: Date.now(),\n },\n };\n}\n\n/**\n * Parses global id (gid) and returns the resource type and id.\n * @see https://shopify.dev/api/usage/gids\n * @param gid - A shopify GID (string)\n *\n * @example\n * ```ts\n * const {id, resource} = parseGid('gid://shopify/Order/123')\n * // => id = \"123\", resource = 'Order'\n *\n * * const {id, resource} = parseGid('gid://shopify/Cart/abc123')\n * // => id = \"abc123\", resource = 'Cart'\n * ```\n **/\nexport function parseGid(gid: string | undefined): ShopifyGid {\n const defaultReturn = {id: ''
|
|
1
|
+
{"version":3,"file":"analytics-utils.mjs","sources":["../../src/analytics-utils.ts"],"sourcesContent":["import type {\n ShopifyMonorailPayload,\n ShopifyMonorailEvent,\n ShopifyGid,\n} from './analytics-types.js';\n\n/**\n * Builds a Shopify Monorail event from a Shopify Monorail payload and a schema ID.\n * @param payload - The Monorail payload\n * @param schemaId - The schema ID to use\n * @returns The formatted payload\n **/\nexport function schemaWrapper(\n schemaId: string,\n payload: ShopifyMonorailPayload,\n): ShopifyMonorailEvent {\n return {\n schema_id: schemaId,\n payload,\n metadata: {\n event_created_at_ms: Date.now(),\n },\n };\n}\n\n/**\n * Parses global id (gid) and returns the resource type and id.\n * @see https://shopify.dev/api/usage/gids\n * @param gid - A shopify GID (string)\n *\n * @example\n * ```ts\n * const {id, resource} = parseGid('gid://shopify/Order/123')\n * // => id = \"123\", resource = 'Order'\n *\n * * const {id, resource} = parseGid('gid://shopify/Cart/abc123')\n * // => id = \"abc123\", resource = 'Cart'\n * ```\n **/\nexport function parseGid(gid: string | undefined): ShopifyGid {\n const defaultReturn: ShopifyGid = {\n id: '',\n resource: null,\n resourceId: null,\n search: '',\n searchParams: new URLSearchParams(),\n hash: '',\n };\n\n if (typeof gid !== 'string') {\n return defaultReturn;\n }\n\n try {\n const {search, searchParams, pathname, hash} = new URL(gid);\n const pathnameParts = pathname.split('/');\n const lastPathnamePart = pathnameParts[pathnameParts.length - 1];\n const resourcePart = pathnameParts[pathnameParts.length - 2];\n\n if (!lastPathnamePart || !resourcePart) {\n return defaultReturn;\n }\n\n const id = `${lastPathnamePart}${search}${hash}` || '';\n const resourceId = lastPathnamePart || null;\n const resource = resourcePart ?? null;\n\n return {id, resource, resourceId, search, searchParams, hash};\n } catch {\n return defaultReturn;\n }\n}\n\n/**\n * Filters properties from an object and returns a new object with only the properties that have a truthy value.\n * @param keyValuePairs - An object of key-value pairs\n * @param formattedData - An object which will hold the truthy values\n * @returns The formatted object\n **/\nexport function addDataIf(\n keyValuePairs: ShopifyMonorailPayload,\n formattedData: ShopifyMonorailPayload,\n): ShopifyMonorailPayload {\n if (typeof keyValuePairs !== 'object') {\n return {};\n }\n Object.entries(keyValuePairs).forEach(([key, value]) => {\n if (value) {\n formattedData[key] = value;\n }\n });\n return formattedData;\n}\n\n/**\n * Utility that errors if a function is called on the server.\n * @param fnName - The name of the function\n * @returns A boolean\n **/\nexport function errorIfServer(fnName: string): boolean {\n if (typeof document === 'undefined') {\n console.error(\n `${fnName} should only be used within the useEffect callback or event handlers`,\n );\n return true;\n }\n return false;\n}\n"],"names":[],"mappings":"AAYgB,SAAA,cACd,UACA,SACsB;AACf,SAAA;AAAA,IACL,WAAW;AAAA,IACX;AAAA,IACA,UAAU;AAAA,MACR,qBAAqB,KAAK,IAAI;AAAA,IAChC;AAAA,EAAA;AAEJ;AAgBO,SAAS,SAAS,KAAqC;AAC5D,QAAM,gBAA4B;AAAA,IAChC,IAAI;AAAA,IACJ,UAAU;AAAA,IACV,YAAY;AAAA,IACZ,QAAQ;AAAA,IACR,cAAc,IAAI,gBAAgB;AAAA,IAClC,MAAM;AAAA,EAAA;AAGJ,MAAA,OAAO,QAAQ,UAAU;AACpB,WAAA;AAAA,EACT;AAEI,MAAA;AACI,UAAA,EAAC,QAAQ,cAAc,UAAU,SAAQ,IAAI,IAAI,GAAG;AACpD,UAAA,gBAAgB,SAAS,MAAM,GAAG;AACxC,UAAM,mBAAmB,cAAc,cAAc,SAAS,CAAC;AAC/D,UAAM,eAAe,cAAc,cAAc,SAAS,CAAC;AAEvD,QAAA,CAAC,oBAAoB,CAAC,cAAc;AAC/B,aAAA;AAAA,IACT;AAEA,UAAM,KAAK,GAAG,gBAAgB,GAAG,MAAM,GAAG,IAAI,MAAM;AACpD,UAAM,aAAa,oBAAoB;AACvC,UAAM,WAAW,gBAAgB;AAEjC,WAAO,EAAC,IAAI,UAAU,YAAY,QAAQ,cAAc;EAAI,QACtD;AACC,WAAA;AAAA,EACT;AACF;AAQgB,SAAA,UACd,eACA,eACwB;AACpB,MAAA,OAAO,kBAAkB,UAAU;AACrC,WAAO;EACT;AACO,SAAA,QAAQ,aAAa,EAAE,QAAQ,CAAC,CAAC,KAAK,KAAK,MAAM;AACtD,QAAI,OAAO;AACT,oBAAc,GAAG,IAAI;AAAA,IACvB;AAAA,EAAA,CACD;AACM,SAAA;AACT;AAOO,SAAS,cAAc,QAAyB;AACjD,MAAA,OAAO,aAAa,aAAa;AAC3B,YAAA;AAAA,MACN,GAAG,MAAM;AAAA,IAAA;AAEJ,WAAA;AAAA,EACT;AACO,SAAA;AACT;"}
|
|
@@ -10,17 +10,32 @@ function schemaWrapper(schemaId, payload) {
|
|
|
10
10
|
};
|
|
11
11
|
}
|
|
12
12
|
function parseGid(gid) {
|
|
13
|
-
const defaultReturn = {
|
|
13
|
+
const defaultReturn = {
|
|
14
|
+
id: "",
|
|
15
|
+
resource: null,
|
|
16
|
+
resourceId: null,
|
|
17
|
+
search: "",
|
|
18
|
+
searchParams: new URLSearchParams(),
|
|
19
|
+
hash: ""
|
|
20
|
+
};
|
|
14
21
|
if (typeof gid !== "string") {
|
|
15
22
|
return defaultReturn;
|
|
16
23
|
}
|
|
17
|
-
|
|
18
|
-
|
|
24
|
+
try {
|
|
25
|
+
const { search, searchParams, pathname, hash } = new URL(gid);
|
|
26
|
+
const pathnameParts = pathname.split("/");
|
|
27
|
+
const lastPathnamePart = pathnameParts[pathnameParts.length - 1];
|
|
28
|
+
const resourcePart = pathnameParts[pathnameParts.length - 2];
|
|
29
|
+
if (!lastPathnamePart || !resourcePart) {
|
|
30
|
+
return defaultReturn;
|
|
31
|
+
}
|
|
32
|
+
const id = `${lastPathnamePart}${search}${hash}` || "";
|
|
33
|
+
const resourceId = lastPathnamePart || null;
|
|
34
|
+
const resource = resourcePart ?? null;
|
|
35
|
+
return { id, resource, resourceId, search, searchParams, hash };
|
|
36
|
+
} catch {
|
|
19
37
|
return defaultReturn;
|
|
20
38
|
}
|
|
21
|
-
const id = matches[2] ?? null;
|
|
22
|
-
const resource = matches[1] ?? null;
|
|
23
|
-
return { id, resource };
|
|
24
39
|
}
|
|
25
40
|
function addDataIf(keyValuePairs, formattedData) {
|
|
26
41
|
if (typeof keyValuePairs !== "object") {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"analytics-utils.js","sources":["../../src/analytics-utils.ts"],"sourcesContent":["import type {\n ShopifyMonorailPayload,\n ShopifyMonorailEvent,\n ShopifyGid,\n} from './analytics-types.js';\n\n/**\n * Builds a Shopify Monorail event from a Shopify Monorail payload and a schema ID.\n * @param payload - The Monorail payload\n * @param schemaId - The schema ID to use\n * @returns The formatted payload\n **/\nexport function schemaWrapper(\n schemaId: string,\n payload: ShopifyMonorailPayload,\n): ShopifyMonorailEvent {\n return {\n schema_id: schemaId,\n payload,\n metadata: {\n event_created_at_ms: Date.now(),\n },\n };\n}\n\n/**\n * Parses global id (gid) and returns the resource type and id.\n * @see https://shopify.dev/api/usage/gids\n * @param gid - A shopify GID (string)\n *\n * @example\n * ```ts\n * const {id, resource} = parseGid('gid://shopify/Order/123')\n * // => id = \"123\", resource = 'Order'\n *\n * * const {id, resource} = parseGid('gid://shopify/Cart/abc123')\n * // => id = \"abc123\", resource = 'Cart'\n * ```\n **/\nexport function parseGid(gid: string | undefined): ShopifyGid {\n const defaultReturn = {id: ''
|
|
1
|
+
{"version":3,"file":"analytics-utils.js","sources":["../../src/analytics-utils.ts"],"sourcesContent":["import type {\n ShopifyMonorailPayload,\n ShopifyMonorailEvent,\n ShopifyGid,\n} from './analytics-types.js';\n\n/**\n * Builds a Shopify Monorail event from a Shopify Monorail payload and a schema ID.\n * @param payload - The Monorail payload\n * @param schemaId - The schema ID to use\n * @returns The formatted payload\n **/\nexport function schemaWrapper(\n schemaId: string,\n payload: ShopifyMonorailPayload,\n): ShopifyMonorailEvent {\n return {\n schema_id: schemaId,\n payload,\n metadata: {\n event_created_at_ms: Date.now(),\n },\n };\n}\n\n/**\n * Parses global id (gid) and returns the resource type and id.\n * @see https://shopify.dev/api/usage/gids\n * @param gid - A shopify GID (string)\n *\n * @example\n * ```ts\n * const {id, resource} = parseGid('gid://shopify/Order/123')\n * // => id = \"123\", resource = 'Order'\n *\n * * const {id, resource} = parseGid('gid://shopify/Cart/abc123')\n * // => id = \"abc123\", resource = 'Cart'\n * ```\n **/\nexport function parseGid(gid: string | undefined): ShopifyGid {\n const defaultReturn: ShopifyGid = {\n id: '',\n resource: null,\n resourceId: null,\n search: '',\n searchParams: new URLSearchParams(),\n hash: '',\n };\n\n if (typeof gid !== 'string') {\n return defaultReturn;\n }\n\n try {\n const {search, searchParams, pathname, hash} = new URL(gid);\n const pathnameParts = pathname.split('/');\n const lastPathnamePart = pathnameParts[pathnameParts.length - 1];\n const resourcePart = pathnameParts[pathnameParts.length - 2];\n\n if (!lastPathnamePart || !resourcePart) {\n return defaultReturn;\n }\n\n const id = `${lastPathnamePart}${search}${hash}` || '';\n const resourceId = lastPathnamePart || null;\n const resource = resourcePart ?? null;\n\n return {id, resource, resourceId, search, searchParams, hash};\n } catch {\n return defaultReturn;\n }\n}\n\n/**\n * Filters properties from an object and returns a new object with only the properties that have a truthy value.\n * @param keyValuePairs - An object of key-value pairs\n * @param formattedData - An object which will hold the truthy values\n * @returns The formatted object\n **/\nexport function addDataIf(\n keyValuePairs: ShopifyMonorailPayload,\n formattedData: ShopifyMonorailPayload,\n): ShopifyMonorailPayload {\n if (typeof keyValuePairs !== 'object') {\n return {};\n }\n Object.entries(keyValuePairs).forEach(([key, value]) => {\n if (value) {\n formattedData[key] = value;\n }\n });\n return formattedData;\n}\n\n/**\n * Utility that errors if a function is called on the server.\n * @param fnName - The name of the function\n * @returns A boolean\n **/\nexport function errorIfServer(fnName: string): boolean {\n if (typeof document === 'undefined') {\n console.error(\n `${fnName} should only be used within the useEffect callback or event handlers`,\n );\n return true;\n }\n return false;\n}\n"],"names":[],"mappings":";;AAYgB,SAAA,cACd,UACA,SACsB;AACf,SAAA;AAAA,IACL,WAAW;AAAA,IACX;AAAA,IACA,UAAU;AAAA,MACR,qBAAqB,KAAK,IAAI;AAAA,IAChC;AAAA,EAAA;AAEJ;AAgBO,SAAS,SAAS,KAAqC;AAC5D,QAAM,gBAA4B;AAAA,IAChC,IAAI;AAAA,IACJ,UAAU;AAAA,IACV,YAAY;AAAA,IACZ,QAAQ;AAAA,IACR,cAAc,IAAI,gBAAgB;AAAA,IAClC,MAAM;AAAA,EAAA;AAGJ,MAAA,OAAO,QAAQ,UAAU;AACpB,WAAA;AAAA,EACT;AAEI,MAAA;AACI,UAAA,EAAC,QAAQ,cAAc,UAAU,SAAQ,IAAI,IAAI,GAAG;AACpD,UAAA,gBAAgB,SAAS,MAAM,GAAG;AACxC,UAAM,mBAAmB,cAAc,cAAc,SAAS,CAAC;AAC/D,UAAM,eAAe,cAAc,cAAc,SAAS,CAAC;AAEvD,QAAA,CAAC,oBAAoB,CAAC,cAAc;AAC/B,aAAA;AAAA,IACT;AAEA,UAAM,KAAK,GAAG,gBAAgB,GAAG,MAAM,GAAG,IAAI,MAAM;AACpD,UAAM,aAAa,oBAAoB;AACvC,UAAM,WAAW,gBAAgB;AAEjC,WAAO,EAAC,IAAI,UAAU,YAAY,QAAQ,cAAc;EAAI,QACtD;AACC,WAAA;AAAA,EACT;AACF;AAQgB,SAAA,UACd,eACA,eACwB;AACpB,MAAA,OAAO,kBAAkB,UAAU;AACrC,WAAO;EACT;AACO,SAAA,QAAQ,aAAa,EAAE,QAAQ,CAAC,CAAC,KAAK,KAAK,MAAM;AACtD,QAAI,OAAO;AACT,oBAAc,GAAG,IAAI;AAAA,IACvB;AAAA,EAAA,CACD;AACM,SAAA;AACT;AAOO,SAAS,cAAc,QAAyB;AACjD,MAAA,OAAO,aAAa,aAAa;AAC3B,YAAA;AAAA,MACN,GAAG,MAAM;AAAA,IAAA;AAEJ,WAAA;AAAA,EACT;AACO,SAAA;AACT;;;;;"}
|
|
@@ -8,17 +8,32 @@ function schemaWrapper(schemaId, payload) {
|
|
|
8
8
|
};
|
|
9
9
|
}
|
|
10
10
|
function parseGid(gid) {
|
|
11
|
-
const defaultReturn = {
|
|
11
|
+
const defaultReturn = {
|
|
12
|
+
id: "",
|
|
13
|
+
resource: null,
|
|
14
|
+
resourceId: null,
|
|
15
|
+
search: "",
|
|
16
|
+
searchParams: new URLSearchParams(),
|
|
17
|
+
hash: ""
|
|
18
|
+
};
|
|
12
19
|
if (typeof gid !== "string") {
|
|
13
20
|
return defaultReturn;
|
|
14
21
|
}
|
|
15
|
-
|
|
16
|
-
|
|
22
|
+
try {
|
|
23
|
+
const { search, searchParams, pathname, hash } = new URL(gid);
|
|
24
|
+
const pathnameParts = pathname.split("/");
|
|
25
|
+
const lastPathnamePart = pathnameParts[pathnameParts.length - 1];
|
|
26
|
+
const resourcePart = pathnameParts[pathnameParts.length - 2];
|
|
27
|
+
if (!lastPathnamePart || !resourcePart) {
|
|
28
|
+
return defaultReturn;
|
|
29
|
+
}
|
|
30
|
+
const id = `${lastPathnamePart}${search}${hash}` || "";
|
|
31
|
+
const resourceId = lastPathnamePart || null;
|
|
32
|
+
const resource = resourcePart ?? null;
|
|
33
|
+
return { id, resource, resourceId, search, searchParams, hash };
|
|
34
|
+
} catch {
|
|
17
35
|
return defaultReturn;
|
|
18
36
|
}
|
|
19
|
-
const id = matches[2] ?? null;
|
|
20
|
-
const resource = matches[1] ?? null;
|
|
21
|
-
return { id, resource };
|
|
22
37
|
}
|
|
23
38
|
function addDataIf(keyValuePairs, formattedData) {
|
|
24
39
|
if (typeof keyValuePairs !== "object") {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"analytics-utils.mjs","sources":["../../src/analytics-utils.ts"],"sourcesContent":["import type {\n ShopifyMonorailPayload,\n ShopifyMonorailEvent,\n ShopifyGid,\n} from './analytics-types.js';\n\n/**\n * Builds a Shopify Monorail event from a Shopify Monorail payload and a schema ID.\n * @param payload - The Monorail payload\n * @param schemaId - The schema ID to use\n * @returns The formatted payload\n **/\nexport function schemaWrapper(\n schemaId: string,\n payload: ShopifyMonorailPayload,\n): ShopifyMonorailEvent {\n return {\n schema_id: schemaId,\n payload,\n metadata: {\n event_created_at_ms: Date.now(),\n },\n };\n}\n\n/**\n * Parses global id (gid) and returns the resource type and id.\n * @see https://shopify.dev/api/usage/gids\n * @param gid - A shopify GID (string)\n *\n * @example\n * ```ts\n * const {id, resource} = parseGid('gid://shopify/Order/123')\n * // => id = \"123\", resource = 'Order'\n *\n * * const {id, resource} = parseGid('gid://shopify/Cart/abc123')\n * // => id = \"abc123\", resource = 'Cart'\n * ```\n **/\nexport function parseGid(gid: string | undefined): ShopifyGid {\n const defaultReturn = {id: ''
|
|
1
|
+
{"version":3,"file":"analytics-utils.mjs","sources":["../../src/analytics-utils.ts"],"sourcesContent":["import type {\n ShopifyMonorailPayload,\n ShopifyMonorailEvent,\n ShopifyGid,\n} from './analytics-types.js';\n\n/**\n * Builds a Shopify Monorail event from a Shopify Monorail payload and a schema ID.\n * @param payload - The Monorail payload\n * @param schemaId - The schema ID to use\n * @returns The formatted payload\n **/\nexport function schemaWrapper(\n schemaId: string,\n payload: ShopifyMonorailPayload,\n): ShopifyMonorailEvent {\n return {\n schema_id: schemaId,\n payload,\n metadata: {\n event_created_at_ms: Date.now(),\n },\n };\n}\n\n/**\n * Parses global id (gid) and returns the resource type and id.\n * @see https://shopify.dev/api/usage/gids\n * @param gid - A shopify GID (string)\n *\n * @example\n * ```ts\n * const {id, resource} = parseGid('gid://shopify/Order/123')\n * // => id = \"123\", resource = 'Order'\n *\n * * const {id, resource} = parseGid('gid://shopify/Cart/abc123')\n * // => id = \"abc123\", resource = 'Cart'\n * ```\n **/\nexport function parseGid(gid: string | undefined): ShopifyGid {\n const defaultReturn: ShopifyGid = {\n id: '',\n resource: null,\n resourceId: null,\n search: '',\n searchParams: new URLSearchParams(),\n hash: '',\n };\n\n if (typeof gid !== 'string') {\n return defaultReturn;\n }\n\n try {\n const {search, searchParams, pathname, hash} = new URL(gid);\n const pathnameParts = pathname.split('/');\n const lastPathnamePart = pathnameParts[pathnameParts.length - 1];\n const resourcePart = pathnameParts[pathnameParts.length - 2];\n\n if (!lastPathnamePart || !resourcePart) {\n return defaultReturn;\n }\n\n const id = `${lastPathnamePart}${search}${hash}` || '';\n const resourceId = lastPathnamePart || null;\n const resource = resourcePart ?? null;\n\n return {id, resource, resourceId, search, searchParams, hash};\n } catch {\n return defaultReturn;\n }\n}\n\n/**\n * Filters properties from an object and returns a new object with only the properties that have a truthy value.\n * @param keyValuePairs - An object of key-value pairs\n * @param formattedData - An object which will hold the truthy values\n * @returns The formatted object\n **/\nexport function addDataIf(\n keyValuePairs: ShopifyMonorailPayload,\n formattedData: ShopifyMonorailPayload,\n): ShopifyMonorailPayload {\n if (typeof keyValuePairs !== 'object') {\n return {};\n }\n Object.entries(keyValuePairs).forEach(([key, value]) => {\n if (value) {\n formattedData[key] = value;\n }\n });\n return formattedData;\n}\n\n/**\n * Utility that errors if a function is called on the server.\n * @param fnName - The name of the function\n * @returns A boolean\n **/\nexport function errorIfServer(fnName: string): boolean {\n if (typeof document === 'undefined') {\n console.error(\n `${fnName} should only be used within the useEffect callback or event handlers`,\n );\n return true;\n }\n return false;\n}\n"],"names":[],"mappings":"AAYgB,SAAA,cACd,UACA,SACsB;AACf,SAAA;AAAA,IACL,WAAW;AAAA,IACX;AAAA,IACA,UAAU;AAAA,MACR,qBAAqB,KAAK,IAAI;AAAA,IAChC;AAAA,EAAA;AAEJ;AAgBO,SAAS,SAAS,KAAqC;AAC5D,QAAM,gBAA4B;AAAA,IAChC,IAAI;AAAA,IACJ,UAAU;AAAA,IACV,YAAY;AAAA,IACZ,QAAQ;AAAA,IACR,cAAc,IAAI,gBAAgB;AAAA,IAClC,MAAM;AAAA,EAAA;AAGJ,MAAA,OAAO,QAAQ,UAAU;AACpB,WAAA;AAAA,EACT;AAEI,MAAA;AACI,UAAA,EAAC,QAAQ,cAAc,UAAU,SAAQ,IAAI,IAAI,GAAG;AACpD,UAAA,gBAAgB,SAAS,MAAM,GAAG;AACxC,UAAM,mBAAmB,cAAc,cAAc,SAAS,CAAC;AAC/D,UAAM,eAAe,cAAc,cAAc,SAAS,CAAC;AAEvD,QAAA,CAAC,oBAAoB,CAAC,cAAc;AAC/B,aAAA;AAAA,IACT;AAEA,UAAM,KAAK,GAAG,gBAAgB,GAAG,MAAM,GAAG,IAAI,MAAM;AACpD,UAAM,aAAa,oBAAoB;AACvC,UAAM,WAAW,gBAAgB;AAEjC,WAAO,EAAC,IAAI,UAAU,YAAY,QAAQ,cAAc;EAAI,QACtD;AACC,WAAA;AAAA,EACT;AACF;AAQgB,SAAA,UACd,eACA,eACwB;AACpB,MAAA,OAAO,kBAAkB,UAAU;AACrC,WAAO;EACT;AACO,SAAA,QAAQ,aAAa,EAAE,QAAQ,CAAC,CAAC,KAAK,KAAK,MAAM;AACtD,QAAI,OAAO;AACT,oBAAc,GAAG,IAAI;AAAA,IACvB;AAAA,EAAA,CACD;AACM,SAAA;AACT;AAOO,SAAS,cAAc,QAAyB;AACjD,MAAA,OAAO,aAAa,aAAa;AAC3B,YAAA;AAAA,MACN,GAAG,MAAM;AAAA,IAAA;AAEJ,WAAA;AAAA,EACT;AACO,SAAA;AACT;"}
|
|
@@ -150,8 +150,9 @@ export type ShopifyCookies = {
|
|
|
150
150
|
/** Shopify session token: Value of `_shopify_s` cookie. */
|
|
151
151
|
[SHOPIFY_S]: string;
|
|
152
152
|
};
|
|
153
|
-
export type ShopifyGid = {
|
|
153
|
+
export type ShopifyGid = Pick<URL, 'search' | 'searchParams' | 'hash'> & {
|
|
154
154
|
id: string;
|
|
155
155
|
resource: string | null;
|
|
156
|
+
resourceId: string | null;
|
|
156
157
|
};
|
|
157
158
|
export {};
|
|
@@ -2271,17 +2271,32 @@ Refer to the authentication https://shopify.dev/api/storefront#authentication do
|
|
|
2271
2271
|
};
|
|
2272
2272
|
}
|
|
2273
2273
|
function parseGid(gid) {
|
|
2274
|
-
const defaultReturn = {
|
|
2274
|
+
const defaultReturn = {
|
|
2275
|
+
id: "",
|
|
2276
|
+
resource: null,
|
|
2277
|
+
resourceId: null,
|
|
2278
|
+
search: "",
|
|
2279
|
+
searchParams: new URLSearchParams(),
|
|
2280
|
+
hash: ""
|
|
2281
|
+
};
|
|
2275
2282
|
if (typeof gid !== "string") {
|
|
2276
2283
|
return defaultReturn;
|
|
2277
2284
|
}
|
|
2278
|
-
|
|
2279
|
-
|
|
2285
|
+
try {
|
|
2286
|
+
const { search, searchParams, pathname, hash } = new URL(gid);
|
|
2287
|
+
const pathnameParts = pathname.split("/");
|
|
2288
|
+
const lastPathnamePart = pathnameParts[pathnameParts.length - 1];
|
|
2289
|
+
const resourcePart = pathnameParts[pathnameParts.length - 2];
|
|
2290
|
+
if (!lastPathnamePart || !resourcePart) {
|
|
2291
|
+
return defaultReturn;
|
|
2292
|
+
}
|
|
2293
|
+
const id = `${lastPathnamePart}${search}${hash}` || "";
|
|
2294
|
+
const resourceId = lastPathnamePart || null;
|
|
2295
|
+
const resource = resourcePart ?? null;
|
|
2296
|
+
return { id, resource, resourceId, search, searchParams, hash };
|
|
2297
|
+
} catch {
|
|
2280
2298
|
return defaultReturn;
|
|
2281
2299
|
}
|
|
2282
|
-
const id = matches[2] ?? null;
|
|
2283
|
-
const resource = matches[1] ?? null;
|
|
2284
|
-
return { id, resource };
|
|
2285
2300
|
}
|
|
2286
2301
|
function addDataIf(keyValuePairs, formattedData) {
|
|
2287
2302
|
if (typeof keyValuePairs !== "object") {
|