Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add metametrics events to carousel #29141

Merged
merged 7 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions shared/constants/metametrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,9 @@ export enum MetaMetricsEventName {
AppUnlockedFailed = 'App Unlocked Failed',
AppLocked = 'App Locked',
AppWindowExpanded = 'App Window Expanded',
BannerDisplay = 'Banner Display',
BannerCloseAll = 'Banner Close All',
BannerSelect = 'Banner Select',
BridgeLinkClicked = 'Bridge Link Clicked',
BitcoinSupportToggled = 'Bitcoin Support Toggled',
BitcoinTestnetSupportToggled = 'Bitcoin Testnet Support Toggled',
Expand Down Expand Up @@ -910,6 +913,7 @@ export enum MetaMetricsEventCategory {
App = 'App',
Auth = 'Auth',
Background = 'Background',
Banner = 'Banner',
// The TypeScript ESLint rule is incorrectly marking this line.
/* eslint-disable-next-line @typescript-eslint/no-shadow */
Error = 'Error',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect } from 'react';
import React, { useEffect, useContext, useState, useCallback } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { removeSlide, updateSlides } from '../../../store/actions';
import { Carousel } from '..';
Expand All @@ -16,6 +16,12 @@ import {
///: BEGIN:ONLY_INCLUDE_IF(build-main,build-beta,build-flask)
import { getPortfolioUrl } from '../../../helpers/utils/portfolio';
///: END:ONLY_INCLUDE_IF
import { MetaMetricsContext } from '../../../contexts/metametrics';
import {
MetaMetricsEventName,
MetaMetricsEventCategory,
} from '../../../../shared/constants/metametrics';
import type { CarouselSlide } from '../../../../shared/constants/app-state';
import {
AccountOverviewTabsProps,
AccountOverviewTabs,
Expand All @@ -42,6 +48,8 @@ export const AccountOverviewLayout = ({
const slides = useSelector(getSlides);
const totalBalance = useSelector(getSelectedAccountCachedBalance);
const isLoading = useSelector(getAppIsLoading);
const trackEvent = useContext(MetaMetricsContext);
const [hasRendered, setHasRendered] = useState(false);

const hasZeroBalance = totalBalance === ZERO_BALANCE;

Expand Down Expand Up @@ -75,8 +83,8 @@ export const AccountOverviewLayout = ({
dispatch(updateSlides(defaultSlides));
}, [hasZeroBalance]);

///: BEGIN:ONLY_INCLUDE_IF(build-main,build-beta,build-flask)
const handleCarouselClick = (id: string) => {
///: BEGIN:ONLY_INCLUDE_IF(build-main,build-beta,build-flask)
if (id === 'bridge') {
const portfolioUrl = getPortfolioUrl(
'bridge',
Expand All @@ -90,26 +98,57 @@ export const AccountOverviewLayout = ({
url: `${portfolioUrl}&token=${defaultSwapsToken}`,
});
}
///: END:ONLY_INCLUDE_IF

trackEvent({
event: MetaMetricsEventName.BannerSelect,
category: MetaMetricsEventCategory.Banner,
properties: {
banner_name: id,
},
});
};
///: END:ONLY_INCLUDE_IF

const handleRemoveSlide = (id: string) => {
if (id === 'fund' && hasZeroBalance) {
return;
}
if (slides.length === 1) {
trackEvent({
event: MetaMetricsEventName.BannerCloseAll,
category: MetaMetricsEventCategory.Banner,
});
}
dispatch(removeSlide(id));
};

const handleRenderSlides = useCallback(
(renderedSlides: CarouselSlide[]) => {
if (!hasRendered) {
renderedSlides.forEach((slide) => {
trackEvent({
event: MetaMetricsEventName.BannerDisplay,
category: MetaMetricsEventCategory.Banner,
properties: {
banner_name: slide.id,
},
});
});
setHasRendered(true);
}
},
[hasRendered, trackEvent],
);

return (
<>
<div className="account-overview__balance-wrapper">{children}</div>
<Carousel
slides={slides}
isLoading={isLoading}
///: BEGIN:ONLY_INCLUDE_IF(build-main,build-beta,build-flask)
onClick={handleCarouselClick}
///: END:ONLY_INCLUDE_IF
onClose={handleRemoveSlide}
onRenderSlides={handleRenderSlides}
/>
<AccountOverviewTabs {...tabsProps}></AccountOverviewTabs>
</>
Expand Down
14 changes: 13 additions & 1 deletion ui/components/multichain/carousel/carousel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
import { Carousel as ResponsiveCarousel } from 'react-responsive-carousel';
import { useI18nContext } from '../../../hooks/useI18nContext';
import { Box, BoxProps, BannerBase } from '../../component-library';
Expand All @@ -24,6 +24,7 @@ export const Carousel = React.forwardRef(
isLoading = false,
onClose,
onClick,
onRenderSlides,
...props
}: CarouselProps,
ref: React.Ref<HTMLDivElement>,
Expand All @@ -44,6 +45,17 @@ export const Carousel = React.forwardRef(
})
.slice(0, MAX_SLIDES);

useEffect(() => {
if (
visibleSlides &&
visibleSlides.length > 0 &&
onRenderSlides &&
!isLoading
) {
onRenderSlides(visibleSlides);
}
}, [visibleSlides, onRenderSlides, isLoading]);

const handleClose = (e: React.MouseEvent<HTMLElement>, slideId: string) => {
e.preventDefault();
e.stopPropagation();
Expand Down
1 change: 1 addition & 0 deletions ui/components/multichain/carousel/carousel.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export type CarouselProps = {
isLoading?: boolean;
onClose?: (id: string) => void;
onClick?: (id: string) => void;
onRenderSlides?: (slides: CarouselSlide[]) => void;
};
Loading