feat(analytics): added products to add to cart event

This commit is contained in:
oybek 2024-04-01 18:28:57 +04:00
parent 7be35ae4e3
commit ff5c75bbc5
5 changed files with 45 additions and 4 deletions

View File

@ -4,11 +4,14 @@ import { TAGS } from 'lib/constants';
import { addToCart, createCart, getCart, removeFromCart, updateCart } from 'lib/shopify';
import { revalidateTag } from 'next/cache';
import { cookies } from 'next/headers';
import { ShopifyAnalyticsProduct } from '@shopify/hydrogen-react';
import { productToAnalytics } from '../../lib/utils';
type AddItemResponse = {
cartId?: string;
success: boolean;
message?: string;
products?: ShopifyAnalyticsProduct[];
};
export async function addItem(
@ -17,6 +20,7 @@ export async function addItem(
): Promise<AddItemResponse> {
let cartId = cookies().get('cartId')?.value;
let cart;
const quantity = 1;
if (cartId) {
cart = await getCart(cartId);
@ -33,12 +37,13 @@ export async function addItem(
}
try {
await addToCart(cartId, [{ merchandiseId: selectedVariantId, quantity: 1 }]);
const response = await addToCart(cartId, [{ merchandiseId: selectedVariantId, quantity }]);
revalidateTag(TAGS.cart);
return {
success: true,
message: 'Item added to cart',
cartId
cartId,
products: productToAnalytics(response.lines, quantity, selectedVariantId)
};
} catch (e) {
return { success: false, message: 'Error adding item to cart' };

View File

@ -87,10 +87,12 @@ export function AddToCart({
useEffect(() => {
if (response?.success && response.cartId) {
sendAddToCart({
cartId: response.cartId
cartId: response.cartId,
products: response.products,
totalValue: Number(response.products?.[0]?.price)
});
}
}, [response?.success, response?.cartId, sendAddToCart]);
}, [response?.success, response?.cartId, sendAddToCart, response?.products]);
return (
<form action={actionWithVariant}>

View File

@ -37,6 +37,10 @@ const cartFragment = /* GraphQL */ `
name
value
}
price {
amount
currencyCode
}
product {
...product
}

View File

@ -21,6 +21,7 @@ export type CartItem = {
merchandise: {
id: string;
title: string;
price: Money;
selectedOptions: {
name: string;
value: string;

View File

@ -1,4 +1,6 @@
import { ReadonlyURLSearchParams } from 'next/navigation';
import { Cart } from './shopify/types';
import { ShopifyAnalyticsProduct } from '@shopify/hydrogen-react';
export const createUrl = (pathname: string, params: URLSearchParams | ReadonlyURLSearchParams) => {
const paramsString = params.toString();
@ -37,3 +39,30 @@ export const validateEnvironmentVariables = () => {
);
}
};
/**
* This function takes a cart and a quantity and returns an array of ShopifyAnalyticsProduct objects.
* */
export const productToAnalytics = (
cartItems: Cart['lines'],
quantity: number,
variantId: string
) => {
const line = cartItems.find((line) => line.merchandise.id === variantId);
if (!line) return;
const { merchandise } = line;
if (!merchandise) return;
return [
{
productGid: merchandise?.product.id,
variantGid: variantId,
name: merchandise?.product.title,
variantName: merchandise?.title,
price: merchandise?.price.amount,
quantity
} as ShopifyAnalyticsProduct
];
};