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

Email Stats: Add Opens and Clicks tooltips #97729

Open
wants to merge 10 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
85 changes: 81 additions & 4 deletions client/my-sites/stats/stats-email-summary/index.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Tooltip } from '@automattic/components';
import { localize } from 'i18n-calypso';
import { useRef, useState } from 'react';
import { connect } from 'react-redux';
import titlecase from 'to-title-case';
import JetpackColophon from 'calypso/components/jetpack-colophon';
Expand All @@ -13,6 +15,30 @@ import '../stats-module/summary-nav.scss';

const StatsStrings = statsStringsFactory();

const TooltipWrapper = ( { value, item, renderContent } ) => {
const triggerRef = useRef( null );
const [ showTooltip, setShowTooltip ] = useState( false );

const formattedValue =
typeof value === 'number' ? Number( value ).toFixed( 2 ) : value.replace( '%', '' ).trim();

return (
<span className="stats-email__tooltip-wrapper">
<span
ref={ triggerRef }
className="stats-email__tooltip-trigger"
onMouseEnter={ () => setShowTooltip( true ) }
onMouseLeave={ () => setShowTooltip( false ) }
>
{ `${ formattedValue }%` }
</span>
<Tooltip position="top" context={ triggerRef.current } isVisible={ showTooltip }>
{ renderContent( item ) }
</Tooltip>
</span>
);
};

const StatsEmailSummary = ( { translate, period, siteSlug } ) => {
// Navigation settings. One of the following, depending on the summary view.
// Traffic => /stats/day/
Expand All @@ -37,6 +63,44 @@ const StatsEmailSummary = ( { translate, period, siteSlug } ) => {
}
const navigationItems = [ { label: backLabel, href: backLink }, { label: title } ];

const renderTooltipContent = ( item ) => {
const opensUnique = parseInt( item.opens_unique, 10 );
const clicksUnique = parseInt( item.clicks_unique, 10 );
const opens = parseInt( item.opens, 10 );
const clicks = parseInt( item.clicks, 10 );
const opensRate = parseFloat( item.opens_rate );
const clicksRate = parseFloat( item.clicks_rate );
const totalSends = parseInt( item.total_sends, 10 );

return (
<div className="stats-email__tooltip">
<div>
{ translate( 'Subscribers reached: %(sends)d', {
args: { sends: totalSends },
} ) }
</div>
<div>
{ opensUnique
? translate( 'Unique opens: %(uniqueOpens)d (%(openRate).2f%%)', {
args: { uniqueOpens: opensUnique, openRate: opensRate },
} )
: translate( 'Opens: %(opens)d)', {
args: { opens },
} ) }
</div>
<div>
{ clicksUnique
? translate( 'Unique clicks: %(uniqueClicks)d (%(clickRate).2f%%)', {
args: { uniqueClicks: clicksUnique, clickRate: clicksRate },
} )
: translate( 'Clicks: %(clicks)d ', {
args: { clicks },
} ) }
</div>
</div>
);
};

return (
<Main className="has-fixed-nav" wideLayout>
<PageViewTracker
Expand Down Expand Up @@ -64,9 +128,11 @@ const StatsEmailSummary = ( { translate, period, siteSlug } ) => {
</>
),
body: ( item ) => (
<>
<span>{ `${ item.opens_rate }%` }</span>
</>
<TooltipWrapper
value={ `${ item.opens_rate }%` }
item={ item }
renderContent={ renderTooltipContent }
/>
),
} }
path="emails"
Expand All @@ -78,7 +144,18 @@ const StatsEmailSummary = ( { translate, period, siteSlug } ) => {
hideSummaryLink
metricLabel={ translate( 'Clicks' ) }
valueField="clicks_rate"
formatValue={ ( value ) => `${ value }%` }
formatValue={ ( value, item ) => {
if ( item?.opens !== undefined ) {
return (
<TooltipWrapper
value={ `${ value }%` }
item={ item }
renderContent={ renderTooltipContent }
/>
);
}
return <span>{ `${ value }%` }</span>;
} }
listItemClassName="stats__summary--narrow-mobile"
/>
<JetpackColophon />
Expand Down
20 changes: 18 additions & 2 deletions client/state/stats/lists/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -974,19 +974,35 @@ export const normalizers = {
const emailsData = get( data, [ 'posts' ], [] );

return emailsData.map(
( { id, href, date, title, type, opens, clicks, opens_rate, clicks_rate } ) => {
( {
id,
href,
date,
title,
type,
opens,
clicks,
opens_rate,
clicks_rate,
opens_unique,
clicks_unique,
total_sends,
} ) => {
const detailPage = site ? `/stats/email/opens/day/${ id }/${ site.slug }` : null;
return {
id,
href,
date,
label: title,
type,
value: clicks || '0',
value: clicks_rate || '0',
opens: opens || '0',
clicks: clicks || '0',
opens_rate: opens_rate || '0',
clicks_rate: clicks_rate || '0',
opens_unique: opens_unique || '0',
clicks_unique: clicks_unique || '0',
lezama marked this conversation as resolved.
Show resolved Hide resolved
total_sends: total_sends || '0',
page: detailPage,
actions: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const HorizontalBarListItem = ( {
return <ShortenedNumber value={ value } />;
}
if ( formatValue ) {
return formatValue( value );
return formatValue( value, data );
}
return usePlainCard ? value : numberFormat( value, 0 );
};
Expand Down Expand Up @@ -193,6 +193,7 @@ const HorizontalBarListItem = ( {
isStatic={ isStatic }
usePlainCard={ usePlainCard }
isLinkUnderlined={ isLinkUnderlined }
formatValue={ formatValue }
/>
);
} ) }
Expand Down
5 changes: 4 additions & 1 deletion packages/components/src/horizontal-bar-list/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ export type HorizontalBarListItemProps = {
* @property {boolean} hasNoBackground - don't render the background bar and adjust indentation
*/
hasNoBackground?: boolean;
formatValue?: ( value: number ) => string;
/**
* @property {Function} formatValue - function to format the value display. Can optionally receive the full item data.
*/
formatValue?: ( value: number, item?: StatDataObject ) => React.ReactNode;
};

type StatDataObject = {
Expand Down
Loading