Skip to content

Commit

Permalink
fix cart analytics to accept edges
Browse files Browse the repository at this point in the history
  • Loading branch information
wizardlyhel committed Apr 26, 2024
1 parent dbc7207 commit ac4755d
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/hydrogen/src/analytics-manager/CartAnalytics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
type Carts,
} from './AnalyticsProvider';
import {type CartUpdatePayload} from './AnalyticsView';
import {flattenConnection} from '@shopify/hydrogen-react';

function logMissingField(fieldName: string) {
// eslint-disable-next-line no-console
Expand Down Expand Up @@ -96,9 +97,12 @@ export function CartAnalytics({
}),
);

const previousCartLines = flattenConnection(prevCart?.lines);
const currentCartLines = flattenConnection(cart.lines);

// Detect quantity changes and missing cart lines
prevCart?.lines?.nodes?.forEach((prevLine) => {
const matchedLineId = cart?.lines.nodes.filter(
previousCartLines?.forEach((prevLine) => {
const matchedLineId = currentCartLines.filter(
(line) => prevLine.id === line.id,
);
if (matchedLineId?.length === 1) {
Expand All @@ -125,8 +129,8 @@ export function CartAnalytics({
});

// Detect added to cart
cart?.lines?.nodes?.forEach((line) => {
const matchedLineId = prevCart?.lines.nodes.filter(
currentCartLines?.forEach((line) => {
const matchedLineId = previousCartLines.filter(
(previousLine) => line.id === previousLine.id,
);
if (!matchedLineId || matchedLineId.length === 0) {
Expand Down

0 comments on commit ac4755d

Please sign in to comment.