@replohq/sdk 1.7.3 → 1.8.0
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/analytics/hooks/use-collection-viewed-analytics.d.ts +19 -0
- package/analytics/hooks/use-collection-viewed-analytics.js +26 -0
- package/analytics/sinks/elevar-sink.js +1 -3
- package/analytics/sinks/ga4-sink.js +23 -1
- package/analytics/sinks/pinterest-sink.js +19 -1
- package/analytics/sinks/reddit-sink.js +16 -1
- package/analytics/sinks/tiktok-sink.js +18 -1
- package/analytics/utils/analytics-products.d.ts +36 -0
- package/analytics/utils/analytics-products.js +57 -0
- package/analytics/utils/collection-analytics.d.ts +44 -0
- package/analytics/utils/collection-analytics.js +41 -0
- package/lib/buildMetadata.js +3 -3
- package/package.json +8 -4
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { AnalyticsCollection, AnalyticsCollectionProduct } from "../utils/collection-analytics";
|
|
2
|
+
interface UseCollectionViewedAnalyticsProps {
|
|
3
|
+
collection: AnalyticsCollection | null | undefined;
|
|
4
|
+
/**
|
|
5
|
+
* The collection's products as the collection-products loader returns them.
|
|
6
|
+
* Pass `undefined` while they are still loading so the event carries the
|
|
7
|
+
* list when it fires.
|
|
8
|
+
*/
|
|
9
|
+
products: AnalyticsCollectionProduct[] | null | undefined;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Fires a `viewCollection` analytics event carrying the collection's products
|
|
13
|
+
* once the collection and its product list are both available. Re-renders
|
|
14
|
+
* and product-list updates for the same collection do not fire again; a
|
|
15
|
+
* different collection does, and returning to an earlier one counts as a new
|
|
16
|
+
* view, matching `useProductViewedAnalytics`.
|
|
17
|
+
*/
|
|
18
|
+
export declare function useCollectionViewedAnalytics({ collection, products, }: UseCollectionViewedAnalyticsProps): void;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useEffect, useRef } from "react";
|
|
3
|
+
import { useAnalytics } from "../analytics-provider";
|
|
4
|
+
import { buildCollectionViewedAnalyticsData } from "../utils/collection-analytics";
|
|
5
|
+
function useCollectionViewedAnalytics({
|
|
6
|
+
collection,
|
|
7
|
+
products
|
|
8
|
+
}) {
|
|
9
|
+
const analytics = useAnalytics();
|
|
10
|
+
const lastTrackedRef = useRef(null);
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
if (!analytics || !collection || !products) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
if (lastTrackedRef.current === collection.id) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
lastTrackedRef.current = collection.id;
|
|
19
|
+
void analytics.viewCollection({
|
|
20
|
+
data: buildCollectionViewedAnalyticsData({ collection, products })
|
|
21
|
+
});
|
|
22
|
+
}, [analytics, collection, products]);
|
|
23
|
+
}
|
|
24
|
+
export {
|
|
25
|
+
useCollectionViewedAnalytics
|
|
26
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
+
import { legacyResourceId } from "../utils/analytics-products";
|
|
2
3
|
const GUEST_USER_PROPERTIES = { visitor_type: "guest" };
|
|
3
4
|
const FALLBACK_CURRENCY_CODE = "USD";
|
|
4
5
|
const nonEmptyStringSchema = z.union([z.string().min(1), z.number().finite().transform(String)]).optional().catch(void 0);
|
|
@@ -45,9 +46,6 @@ const collectionPayloadSchema = z.object({
|
|
|
45
46
|
function formatMoney(amount) {
|
|
46
47
|
return amount.toFixed(2);
|
|
47
48
|
}
|
|
48
|
-
function legacyResourceId(id) {
|
|
49
|
-
return id.replace(/^gid:\/\/shopify\/\w+\//, "");
|
|
50
|
-
}
|
|
51
49
|
function parseCheckoutLine(line) {
|
|
52
50
|
const parsed = checkoutLineSchema.safeParse(line);
|
|
53
51
|
if (!parsed.success || !parsed.data.merchandise) {
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import {
|
|
2
|
+
legacyResourceId,
|
|
3
|
+
splitCollectionProducts,
|
|
4
|
+
withoutUndefined
|
|
5
|
+
} from "../utils/analytics-products";
|
|
1
6
|
import { createEventQueue } from "../utils/analytics-utils";
|
|
2
7
|
class GA4Sink {
|
|
3
8
|
send;
|
|
@@ -48,7 +53,24 @@ class GA4Sink {
|
|
|
48
53
|
this.send("begin_checkout", data);
|
|
49
54
|
}
|
|
50
55
|
async viewCollection({ data }) {
|
|
51
|
-
|
|
56
|
+
const { products, rest } = splitCollectionProducts(data);
|
|
57
|
+
this.send("view_item_list", {
|
|
58
|
+
item_list_id: data.collectionId,
|
|
59
|
+
item_list_name: data.collectionTitle,
|
|
60
|
+
currency: data.currency,
|
|
61
|
+
...products.length > 0 ? {
|
|
62
|
+
items: products.map((product, index) => {
|
|
63
|
+
return withoutUndefined({
|
|
64
|
+
item_id: legacyResourceId(product.productId),
|
|
65
|
+
item_name: product.productTitle,
|
|
66
|
+
item_variant: product.variantTitle,
|
|
67
|
+
price: product.price,
|
|
68
|
+
index
|
|
69
|
+
});
|
|
70
|
+
})
|
|
71
|
+
} : {},
|
|
72
|
+
...rest
|
|
73
|
+
});
|
|
52
74
|
}
|
|
53
75
|
}
|
|
54
76
|
export {
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import {
|
|
2
|
+
legacyResourceId,
|
|
3
|
+
splitCollectionProducts,
|
|
4
|
+
withoutUndefined
|
|
5
|
+
} from "../utils/analytics-products";
|
|
1
6
|
import { createEventQueue } from "../utils/analytics-utils";
|
|
2
7
|
class PinterestSink {
|
|
3
8
|
track;
|
|
@@ -61,9 +66,22 @@ class PinterestSink {
|
|
|
61
66
|
});
|
|
62
67
|
}
|
|
63
68
|
async viewCollection({ data }) {
|
|
69
|
+
const { products, rest } = splitCollectionProducts(data);
|
|
64
70
|
this.track("viewcategory", {
|
|
65
71
|
product_category: data.category,
|
|
66
|
-
|
|
72
|
+
currency: data.currency,
|
|
73
|
+
...products.length > 0 ? {
|
|
74
|
+
line_items: products.map((product) => {
|
|
75
|
+
return withoutUndefined({
|
|
76
|
+
product_id: legacyResourceId(product.productId),
|
|
77
|
+
product_name: product.productTitle,
|
|
78
|
+
product_price: product.price,
|
|
79
|
+
product_variant_id: product.variantId === void 0 ? void 0 : legacyResourceId(product.variantId),
|
|
80
|
+
product_variant: product.variantTitle
|
|
81
|
+
});
|
|
82
|
+
})
|
|
83
|
+
} : {},
|
|
84
|
+
...rest
|
|
67
85
|
});
|
|
68
86
|
}
|
|
69
87
|
}
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import {
|
|
2
|
+
legacyResourceId,
|
|
3
|
+
splitCollectionProducts,
|
|
4
|
+
withoutUndefined
|
|
5
|
+
} from "../utils/analytics-products";
|
|
1
6
|
import { createEventQueue } from "../utils/analytics-utils";
|
|
2
7
|
class RedditSink {
|
|
3
8
|
track;
|
|
@@ -64,10 +69,20 @@ class RedditSink {
|
|
|
64
69
|
});
|
|
65
70
|
}
|
|
66
71
|
async viewCollection({ data }) {
|
|
72
|
+
const { products, rest } = splitCollectionProducts(data);
|
|
67
73
|
this.track("ViewContent", {
|
|
68
74
|
content_category: data.category,
|
|
69
75
|
content_type: "category",
|
|
70
|
-
|
|
76
|
+
currency: data.currency,
|
|
77
|
+
...products.length > 0 ? {
|
|
78
|
+
products: products.map((product) => {
|
|
79
|
+
return withoutUndefined({
|
|
80
|
+
id: legacyResourceId(product.productId),
|
|
81
|
+
name: product.productTitle
|
|
82
|
+
});
|
|
83
|
+
})
|
|
84
|
+
} : {},
|
|
85
|
+
...rest
|
|
71
86
|
});
|
|
72
87
|
}
|
|
73
88
|
}
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import {
|
|
2
|
+
legacyResourceId,
|
|
3
|
+
splitCollectionProducts,
|
|
4
|
+
withoutUndefined
|
|
5
|
+
} from "../utils/analytics-products";
|
|
1
6
|
import { createEventQueue } from "../utils/analytics-utils";
|
|
2
7
|
class TikTokSink {
|
|
3
8
|
track;
|
|
@@ -63,10 +68,22 @@ class TikTokSink {
|
|
|
63
68
|
});
|
|
64
69
|
}
|
|
65
70
|
async viewCollection({ data }) {
|
|
71
|
+
const { products, rest } = splitCollectionProducts(data);
|
|
66
72
|
this.track("ViewContent", {
|
|
67
73
|
content_type: "product_group",
|
|
68
74
|
content_category: data.category,
|
|
69
|
-
|
|
75
|
+
currency: data.currency,
|
|
76
|
+
...products.length > 0 ? {
|
|
77
|
+
contents: products.map((product) => {
|
|
78
|
+
return withoutUndefined({
|
|
79
|
+
content_id: legacyResourceId(product.productId),
|
|
80
|
+
content_type: "product_group",
|
|
81
|
+
content_name: product.productTitle,
|
|
82
|
+
price: product.price
|
|
83
|
+
});
|
|
84
|
+
})
|
|
85
|
+
} : {},
|
|
86
|
+
...rest
|
|
70
87
|
});
|
|
71
88
|
}
|
|
72
89
|
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* The flat per-product line the cart provider and the viewed hooks emit.
|
|
4
|
+
* Prices are decimal major units.
|
|
5
|
+
*/
|
|
6
|
+
declare const analyticsProductLineSchema: z.ZodObject<{
|
|
7
|
+
productId: z.ZodCatch<z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodPipe<z.ZodNumber, z.ZodTransform<string, number>>]>>>;
|
|
8
|
+
productTitle: z.ZodCatch<z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodPipe<z.ZodNumber, z.ZodTransform<string, number>>]>>>;
|
|
9
|
+
variantId: z.ZodCatch<z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodPipe<z.ZodNumber, z.ZodTransform<string, number>>]>>>;
|
|
10
|
+
variantTitle: z.ZodCatch<z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodPipe<z.ZodNumber, z.ZodTransform<string, number>>]>>>;
|
|
11
|
+
variantSku: z.ZodCatch<z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodPipe<z.ZodNumber, z.ZodTransform<string, number>>]>>>;
|
|
12
|
+
price: z.ZodCatch<z.ZodOptional<z.ZodUnion<readonly [z.ZodNumber, z.ZodPipe<z.ZodPipe<z.ZodString, z.ZodTransform<number, string>>, z.ZodNumber>]>>>;
|
|
13
|
+
currency: z.ZodCatch<z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodPipe<z.ZodNumber, z.ZodTransform<string, number>>]>>>;
|
|
14
|
+
quantity: z.ZodCatch<z.ZodOptional<z.ZodUnion<readonly [z.ZodNumber, z.ZodPipe<z.ZodPipe<z.ZodString, z.ZodTransform<number, string>>, z.ZodNumber>]>>>;
|
|
15
|
+
}, z.core.$strip>;
|
|
16
|
+
export type AnalyticsProductLine = z.infer<typeof analyticsProductLineSchema> & {
|
|
17
|
+
productId: string;
|
|
18
|
+
};
|
|
19
|
+
export declare function parseAnalyticsProductLines(value: unknown): AnalyticsProductLine[];
|
|
20
|
+
/**
|
|
21
|
+
* Separates a collection-view payload's `products` list from the scalar
|
|
22
|
+
* fields, so a sink can map the list into its own item shape and forward the
|
|
23
|
+
* rest untouched.
|
|
24
|
+
*/
|
|
25
|
+
export declare function splitCollectionProducts(data: Record<string, unknown>): {
|
|
26
|
+
products: AnalyticsProductLine[];
|
|
27
|
+
rest: Record<string, unknown>;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Shopify catalogs synced into ad platforms key on legacy numeric ids, while
|
|
31
|
+
* the Storefront API hands sites GIDs (`gid://shopify/Product/123`). Pixel
|
|
32
|
+
* catalog matching is an exact string match, so ids are sent bare.
|
|
33
|
+
*/
|
|
34
|
+
export declare function legacyResourceId(id: string): string;
|
|
35
|
+
export declare function withoutUndefined<RecordType extends Record<string, unknown>>(record: RecordType): Partial<RecordType>;
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
const nonEmptyStringSchema = z.union([z.string().min(1), z.number().finite().transform(String)]).optional().catch(void 0);
|
|
3
|
+
const finiteNumberSchema = z.union([
|
|
4
|
+
z.number().finite(),
|
|
5
|
+
z.string().trim().min(1).transform(Number).pipe(z.number().finite())
|
|
6
|
+
]).optional().catch(void 0);
|
|
7
|
+
const analyticsProductLineSchema = z.object({
|
|
8
|
+
productId: nonEmptyStringSchema,
|
|
9
|
+
productTitle: nonEmptyStringSchema,
|
|
10
|
+
variantId: nonEmptyStringSchema,
|
|
11
|
+
variantTitle: nonEmptyStringSchema,
|
|
12
|
+
variantSku: nonEmptyStringSchema,
|
|
13
|
+
price: finiteNumberSchema,
|
|
14
|
+
currency: nonEmptyStringSchema,
|
|
15
|
+
quantity: finiteNumberSchema
|
|
16
|
+
});
|
|
17
|
+
function parseAnalyticsProductLines(value) {
|
|
18
|
+
if (!Array.isArray(value)) {
|
|
19
|
+
return [];
|
|
20
|
+
}
|
|
21
|
+
return value.flatMap((item) => {
|
|
22
|
+
const parsed = analyticsProductLineSchema.safeParse(item);
|
|
23
|
+
if (!parsed.success || !parsed.data.productId) {
|
|
24
|
+
return [];
|
|
25
|
+
}
|
|
26
|
+
return [{ ...parsed.data, productId: parsed.data.productId }];
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
function splitCollectionProducts(data) {
|
|
30
|
+
const { products, ...rest } = data;
|
|
31
|
+
const lines = parseAnalyticsProductLines(products);
|
|
32
|
+
if (Array.isArray(products) && products.length > 0 && lines.length === 0) {
|
|
33
|
+
console.warn(
|
|
34
|
+
"[analytics] collection view products did not match the per-line shape and were dropped",
|
|
35
|
+
{ products }
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
return { products: lines, rest };
|
|
39
|
+
}
|
|
40
|
+
function legacyResourceId(id) {
|
|
41
|
+
return id.replace(/^gid:\/\/shopify\/\w+\//, "");
|
|
42
|
+
}
|
|
43
|
+
function withoutUndefined(record) {
|
|
44
|
+
const compacted = {};
|
|
45
|
+
for (const key in record) {
|
|
46
|
+
if (record[key] !== void 0) {
|
|
47
|
+
compacted[key] = record[key];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return compacted;
|
|
51
|
+
}
|
|
52
|
+
export {
|
|
53
|
+
legacyResourceId,
|
|
54
|
+
parseAnalyticsProductLines,
|
|
55
|
+
splitCollectionProducts,
|
|
56
|
+
withoutUndefined
|
|
57
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export interface AnalyticsMoney {
|
|
2
|
+
amount: number | string;
|
|
3
|
+
currencyCode: string;
|
|
4
|
+
}
|
|
5
|
+
export interface AnalyticsCollection {
|
|
6
|
+
id: string;
|
|
7
|
+
title?: string | null;
|
|
8
|
+
handle?: string | null;
|
|
9
|
+
}
|
|
10
|
+
export interface AnalyticsCollectionProductVariant {
|
|
11
|
+
id: string;
|
|
12
|
+
title?: string | null;
|
|
13
|
+
sku?: string | null;
|
|
14
|
+
price?: AnalyticsMoney | null;
|
|
15
|
+
}
|
|
16
|
+
export interface AnalyticsCollectionProduct {
|
|
17
|
+
id: string;
|
|
18
|
+
title?: string | null;
|
|
19
|
+
variants?: AnalyticsCollectionProductVariant[] | null;
|
|
20
|
+
minPrice?: AnalyticsMoney | null;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The `viewCollection` payload: collection identity plus one line per product
|
|
24
|
+
* in the same shape the cart events use, priced from the first variant (or
|
|
25
|
+
* the product's minimum price) with no quantity.
|
|
26
|
+
*/
|
|
27
|
+
export declare function buildCollectionViewedAnalyticsData({ collection, products, }: {
|
|
28
|
+
collection: AnalyticsCollection;
|
|
29
|
+
products: AnalyticsCollectionProduct[];
|
|
30
|
+
}): {
|
|
31
|
+
collectionTitle?: string | undefined;
|
|
32
|
+
collectionHandle?: string | undefined;
|
|
33
|
+
currency?: string | undefined;
|
|
34
|
+
collectionId: string;
|
|
35
|
+
products: {
|
|
36
|
+
productTitle?: string | undefined;
|
|
37
|
+
variantId?: string | undefined;
|
|
38
|
+
variantTitle?: string | undefined;
|
|
39
|
+
variantSku?: string | undefined;
|
|
40
|
+
price?: number | undefined;
|
|
41
|
+
currency?: string | undefined;
|
|
42
|
+
productId: string;
|
|
43
|
+
}[];
|
|
44
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { withoutUndefined } from "./analytics-products";
|
|
2
|
+
function parseAmount(money) {
|
|
3
|
+
if (!money) {
|
|
4
|
+
return void 0;
|
|
5
|
+
}
|
|
6
|
+
const amount = Number(money.amount);
|
|
7
|
+
return Number.isFinite(amount) ? amount : void 0;
|
|
8
|
+
}
|
|
9
|
+
function buildCollectionProductLine(product) {
|
|
10
|
+
const variant = product.variants?.[0];
|
|
11
|
+
const money = variant?.price ?? product.minPrice ?? void 0;
|
|
12
|
+
return {
|
|
13
|
+
productId: product.id,
|
|
14
|
+
...withoutUndefined({
|
|
15
|
+
productTitle: product.title ?? void 0,
|
|
16
|
+
variantId: variant?.id,
|
|
17
|
+
variantTitle: variant?.title ?? void 0,
|
|
18
|
+
variantSku: variant?.sku ?? void 0,
|
|
19
|
+
price: parseAmount(money),
|
|
20
|
+
currency: money?.currencyCode
|
|
21
|
+
})
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function buildCollectionViewedAnalyticsData({
|
|
25
|
+
collection,
|
|
26
|
+
products
|
|
27
|
+
}) {
|
|
28
|
+
const lines = products.map(buildCollectionProductLine);
|
|
29
|
+
return {
|
|
30
|
+
collectionId: collection.id,
|
|
31
|
+
products: lines,
|
|
32
|
+
...withoutUndefined({
|
|
33
|
+
collectionTitle: collection.title ?? void 0,
|
|
34
|
+
collectionHandle: collection.handle ?? void 0,
|
|
35
|
+
currency: lines.find((line) => line.currency)?.currency
|
|
36
|
+
})
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export {
|
|
40
|
+
buildCollectionViewedAnalyticsData
|
|
41
|
+
};
|
package/lib/buildMetadata.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export const BUILD_METADATA = {
|
|
2
2
|
"packageName": "@replohq/sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"branch": "HEAD",
|
|
5
|
-
"commit": "
|
|
6
|
-
"builtAt": "2026-09-
|
|
5
|
+
"commit": "b651d52d898d9c6fc4fdca33165feed30505e056",
|
|
6
|
+
"builtAt": "2026-09-15T18:43:12.533Z"
|
|
7
7
|
};
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@replohq/sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"reploBuild": {
|
|
5
5
|
"packageName": "@replohq/sdk",
|
|
6
|
-
"version": "1.
|
|
6
|
+
"version": "1.8.0",
|
|
7
7
|
"branch": "HEAD",
|
|
8
|
-
"commit": "
|
|
9
|
-
"builtAt": "2026-09-
|
|
8
|
+
"commit": "b651d52d898d9c6fc4fdca33165feed30505e056",
|
|
9
|
+
"builtAt": "2026-09-15T18:43:12.533Z"
|
|
10
10
|
},
|
|
11
11
|
"description": "Replo SDK — cart, analytics, and data loaders for agent-built Next.js sites.",
|
|
12
12
|
"license": "SEE LICENSE IN LICENSE",
|
|
@@ -89,6 +89,10 @@
|
|
|
89
89
|
"types": "./analytics/hooks/use-product-viewed-analytics.d.ts",
|
|
90
90
|
"default": "./analytics/hooks/use-product-viewed-analytics.js"
|
|
91
91
|
},
|
|
92
|
+
"./analytics/hooks/use-collection-viewed-analytics": {
|
|
93
|
+
"types": "./analytics/hooks/use-collection-viewed-analytics.d.ts",
|
|
94
|
+
"default": "./analytics/hooks/use-collection-viewed-analytics.js"
|
|
95
|
+
},
|
|
92
96
|
"./loaders/collection-loader": {
|
|
93
97
|
"types": "./loaders/collection-loader.d.ts",
|
|
94
98
|
"default": "./loaders/collection-loader.js"
|