Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 39 additions & 3 deletions packages/shared/src/components/PromotionalBanner.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ReactElement } from 'react';
import React from 'react';
import React, { useCallback } from 'react';
import classNames from 'classnames';
import {
Button,
Expand All @@ -13,6 +13,9 @@ import { BannerCustomTheme } from '../graphql/banner';
import { Theme } from './utilities';
import { useBanner } from '../hooks/useBanner';
import CloseButton from './CloseButton';
import { useLogContext } from '../contexts/LogContext';
import { LogEvent, TargetType } from '../lib/log';
import useLogEventOnce from '../hooks/log/useLogEventOnce';

const classNamesByTheme: Record<BannerTheme, string[]> = {
[BannerCustomTheme.CabbageOnion]: [
Expand All @@ -36,8 +39,39 @@ const classNamesByTheme: Record<BannerTheme, string[]> = {

export default function PromotionalBanner(): ReactElement {
const { latestBanner: banner, dismiss } = useBanner();
const { logEvent } = useLogContext();

useLogEventOnce(
() => ({
event_name: LogEvent.Impression,
target_type: TargetType.PromotionalBanner,
target_id: banner?.timestamp,
}),
{ condition: !isTesting && !!banner },
);

const onCtaClick = useCallback(() => {
if (!banner) {
return;
}
logEvent({
event_name: LogEvent.Click,
target_type: TargetType.PromotionalBanner,
target_id: banner.timestamp,
});
}, [banner, logEvent]);

const onDismiss = useCallback(() => {
if (banner) {
logEvent({
event_name: LogEvent.MarketingCtaDismiss,
target_type: TargetType.PromotionalBanner,
target_id: banner.timestamp,
});
}
dismiss();
}, [banner, dismiss, logEvent]);

// Disable this component in Jest environment
if (isTesting) {
return <></>;
}
Expand Down Expand Up @@ -70,6 +104,7 @@ export default function PromotionalBanner(): ReactElement {
variant={ButtonVariant.Primary}
color={ButtonColor.Cabbage}
className="mt-2 laptop:ml-4 laptop:mt-0"
onClick={onCtaClick}
>
{banner.cta}
</Button>
Expand All @@ -80,14 +115,15 @@ export default function PromotionalBanner(): ReactElement {
size={ButtonSize.XSmall}
variant={ButtonVariant.Primary}
className="mt-2 laptop:ml-4 laptop:mt-0"
onClick={onCtaClick}
>
{banner.cta}
</Button>
)}
<CloseButton
size={ButtonSize.XSmall}
className="absolute right-2 top-2 laptop:inset-y-0 laptop:my-auto"
onClick={dismiss}
onClick={onDismiss}
/>
</div>
);
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/lib/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ export enum TargetType {
StreaksMilestone = 'streaks milestone',
StreakRecover = 'streak restore',
PromotionCard = 'promotion_card',
PromotionalBanner = 'promotion_banner',
MarketingCtaPopover = 'promotion_popover',
MarketingCtaPopoverSmall = 'promotion_popover_small',
MarketingCtaPlus = 'promotion_plus',
Expand Down
Loading