Remove StatsMixin

This commit is contained in:
Amine Ben hammou
2019-06-21 11:58:57 +01:00
committed by M. Shull
parent baf21dcaf4
commit ca1dc6fe59
4 changed files with 203 additions and 208 deletions

View File

@@ -194,227 +194,227 @@ function wrapInLink(content, params, id, totalSent) {
);
}
const StatisticsMixin = {
renderStatistics: function renderStatistics(newsletter, isSent, currentTime) {
let sent = isSent;
if (sent === undefined) {
// condition for standard and post notification listings
sent = newsletter.statistics
const addStatsCTALink = (params) => {
if (window.mailpoet_premium_active) {
return params;
}
const newParams = params;
newParams.link = 'admin.php?page=mailpoet-premium';
newParams.externalLink = true;
newParams.onClick = trackStatsCTAClicked;
return newParams;
};
export const renderStatistics = (newsletter, isSent, currentTime) => {
let sent = isSent;
if (sent === undefined) {
// condition for standard and post notification listings
sent = newsletter.statistics
&& newsletter.queue
&& newsletter.queue.status !== 'scheduled';
}
if (!sent) {
return (
<span>{MailPoet.I18n.t('notSentYet')}</span>
);
}
}
if (!sent) {
return (
<span>{MailPoet.I18n.t('notSentYet')}</span>
);
}
let params = {};
Hooks.addFilter('mailpoet_newsletters_listing_stats_before', 'mailpoet', StatisticsMixin.addStatsCTALink);
params = Hooks.applyFilters('mailpoet_newsletters_listing_stats_before', params, newsletter);
let params = {};
Hooks.addFilter('mailpoet_newsletters_listing_stats_before', 'mailpoet', addStatsCTALink);
params = Hooks.applyFilters('mailpoet_newsletters_listing_stats_before', params, newsletter);
// welcome emails provide explicit total_sent value
const totalSent = Number((newsletter.total_sent || newsletter.queue.count_processed));
// welcome emails provide explicit total_sent value
const totalSent = Number((newsletter.total_sent || newsletter.queue.count_processed));
let percentageClicked = 0;
let percentageOpened = 0;
let percentageUnsubscribed = 0;
let revenue = null;
let percentageClicked = 0;
let percentageOpened = 0;
let percentageUnsubscribed = 0;
let revenue = null;
if (totalSent > 0) {
percentageClicked = (newsletter.statistics.clicked * 100) / totalSent;
percentageOpened = (newsletter.statistics.opened * 100) / totalSent;
percentageUnsubscribed = (newsletter.statistics.unsubscribed * 100) / totalSent;
revenue = newsletter.statistics.revenue;
}
if (totalSent > 0) {
percentageClicked = (newsletter.statistics.clicked * 100) / totalSent;
percentageOpened = (newsletter.statistics.opened * 100) / totalSent;
percentageUnsubscribed = (newsletter.statistics.unsubscribed * 100) / totalSent;
revenue = newsletter.statistics.revenue;
}
// format to 1 decimal place
const percentageClickedDisplay = MailPoet.Num.toLocaleFixed(percentageClicked, 1);
const percentageOpenedDisplay = MailPoet.Num.toLocaleFixed(percentageOpened, 1);
const percentageUnsubscribedDisplay = MailPoet.Num.toLocaleFixed(percentageUnsubscribed, 1);
// format to 1 decimal place
const percentageClickedDisplay = MailPoet.Num.toLocaleFixed(percentageClicked, 1);
const percentageOpenedDisplay = MailPoet.Num.toLocaleFixed(percentageOpened, 1);
const percentageUnsubscribedDisplay = MailPoet.Num.toLocaleFixed(percentageUnsubscribed, 1);
let showStatsTimeout;
let newsletterDate;
let sentHoursAgo;
let tooEarlyForStats;
let showKbLink;
if (currentTime !== undefined) {
// standard emails and post notifications:
// display green box for newsletters that were just sent
showStatsTimeout = 6; // in hours
newsletterDate = newsletter.queue.scheduled_at || newsletter.queue.created_at;
sentHoursAgo = moment(currentTime).diff(moment(newsletterDate), 'hours');
tooEarlyForStats = sentHoursAgo < showStatsTimeout;
showKbLink = true;
} else {
// welcome emails: no green box and KB link
tooEarlyForStats = false;
showKbLink = false;
}
let showStatsTimeout;
let newsletterDate;
let sentHoursAgo;
let tooEarlyForStats;
let showKbLink;
if (currentTime !== undefined) {
// standard emails and post notifications:
// display green box for newsletters that were just sent
showStatsTimeout = 6; // in hours
newsletterDate = newsletter.queue.scheduled_at || newsletter.queue.created_at;
sentHoursAgo = moment(currentTime).diff(moment(newsletterDate), 'hours');
tooEarlyForStats = sentHoursAgo < showStatsTimeout;
showKbLink = true;
} else {
// welcome emails: no green box and KB link
tooEarlyForStats = false;
showKbLink = false;
}
const improveStatsKBLink = 'http://beta.docs.mailpoet.com/article/191-how-to-improve-my-open-and-click-rates';
const improveStatsKBLink = 'http://beta.docs.mailpoet.com/article/191-how-to-improve-my-open-and-click-rates';
// thresholds to display badges
const minNewslettersSent = 20;
const minNewsletterOpens = 5;
// thresholds to display badges
const minNewslettersSent = 20;
const minNewsletterOpens = 5;
let openedAndClickedStats;
if (totalSent >= minNewslettersSent
let openedAndClickedStats;
if (totalSent >= minNewslettersSent
&& newsletter.statistics.opened >= minNewsletterOpens
&& !tooEarlyForStats
) {
// display stats with badges
openedAndClickedStats = (
<div className="mailpoet_stats_text">
<div>
<span>
{ percentageOpenedDisplay }
%
{' '}
</span>
<StatsBadge
stat="opened"
rate={percentageOpened}
tooltipId={`opened-${newsletter.id}`}
/>
</div>
<div>
<span>
{ percentageClickedDisplay }
%
{' '}
</span>
<StatsBadge
stat="clicked"
rate={percentageClicked}
tooltipId={`clicked-${newsletter.id}`}
/>
</div>
<div>
<span className="mailpoet_stat_hidden">
{ percentageUnsubscribedDisplay }
%
</span>
</div>
</div>
);
} else {
// display simple stats
openedAndClickedStats = (
) {
// display stats with badges
openedAndClickedStats = (
<div className="mailpoet_stats_text">
<div>
<span className="mailpoet_stats_text">
<span>
{ percentageOpenedDisplay }
%,
{ ' ' }
{ percentageClickedDisplay }
%
<span className="mailpoet_stat_hidden">
,
{' '}
{ percentageUnsubscribedDisplay }
%
</span>
{' '}
</span>
<StatsBadge
stat="opened"
rate={percentageOpened}
tooltipId={`opened-${newsletter.id}`}
/>
</div>
<div>
<span>
{ percentageClickedDisplay }
%
{' '}
</span>
<StatsBadge
stat="clicked"
rate={percentageClicked}
tooltipId={`clicked-${newsletter.id}`}
/>
</div>
<div>
<span className="mailpoet_stat_hidden">
{ percentageUnsubscribedDisplay }
%
</span>
</div>
);
}
const wrapContentInLink = (content, idPrefix) => wrapInLink(
content,
params,
`${idPrefix}-${newsletter.id}`,
totalSent
</div>
);
const content = (
<>
{ wrapContentInLink(openedAndClickedStats, 'opened-and-clicked') }
{ revenue !== null && revenue.value > 0 && (
<div className="mailpoet_stats_text">
{ wrapContentInLink(revenue.formatted, 'revenue') }
} else {
// display simple stats
openedAndClickedStats = (
<div>
<span className="mailpoet_stats_text">
{ percentageOpenedDisplay }
%,
{ ' ' }
{ percentageClickedDisplay }
%
<span className="mailpoet_stat_hidden">
,
{' '}
<HelpTooltip
tooltip={MailPoet.I18n.t('revenueStatsTooltip')}
place="left"
tooltipId="helpTooltipStatsRevenue"
/>
</div>
) }
{ tooEarlyForStats && wrapContentInLink(
(
<div className="mailpoet_badge mailpoet_badge_green">
{MailPoet.I18n.t('checkBackInHours').replace('%$1d', showStatsTimeout - sentHoursAgo)}
</div>
),
'check-back'
) }
</>
{ percentageUnsubscribedDisplay }
%
</span>
</span>
</div>
);
}
// thresholds to display bad open rate help
const maxPercentageOpened = 5;
const minSentHoursAgo = 24;
const minTotalSent = 10;
const wrapContentInLink = (content, idPrefix) => wrapInLink(
content,
params,
`${idPrefix}-${newsletter.id}`,
totalSent
);
let afterContent;
if (showKbLink
const content = (
<>
{ wrapContentInLink(openedAndClickedStats, 'opened-and-clicked') }
{ revenue !== null && revenue.value > 0 && (
<div className="mailpoet_stats_text">
{ wrapContentInLink(revenue.formatted, 'revenue') }
{' '}
<HelpTooltip
tooltip={MailPoet.I18n.t('revenueStatsTooltip')}
place="left"
tooltipId="helpTooltipStatsRevenue"
/>
</div>
) }
{ tooEarlyForStats && wrapContentInLink(
(
<div className="mailpoet_badge mailpoet_badge_green">
{MailPoet.I18n.t('checkBackInHours').replace('%$1d', showStatsTimeout - sentHoursAgo)}
</div>
),
'check-back'
) }
</>
);
// thresholds to display bad open rate help
const maxPercentageOpened = 5;
const minSentHoursAgo = 24;
const minTotalSent = 10;
let afterContent;
if (showKbLink
&& percentageOpened < maxPercentageOpened
&& sentHoursAgo >= minSentHoursAgo
&& totalSent >= minTotalSent
) {
// help link for bad open rate
afterContent = (
<div>
<a
href={improveStatsKBLink}
target="_blank"
rel="noopener noreferrer"
className="mailpoet_stat_link_small"
>
{MailPoet.I18n.t('improveThisLinkText')}
</a>
</div>
);
}
return (
) {
// help link for bad open rate
afterContent = (
<div>
{content}
{afterContent}
<a
href={improveStatsKBLink}
target="_blank"
rel="noopener noreferrer"
className="mailpoet_stat_link_small"
>
{MailPoet.I18n.t('improveThisLinkText')}
</a>
</div>
);
},
addStatsCTAAction: function addStatsCTAAction(actions) {
if (window.mailpoet_premium_active) {
return actions;
}
actions.unshift({
name: 'stats',
link: function link() {
return (
<a href="admin.php?page=mailpoet-premium" onClick={trackStatsCTAClicked}>
{MailPoet.I18n.t('statsListingActionTitle')}
</a>
);
},
display: function display(newsletter) {
// welcome emails provide explicit total_sent value
const countProcessed = newsletter.queue && newsletter.queue.count_processed;
return Number(newsletter.total_sent || countProcessed) > 0;
},
});
}
return (
<div>
{content}
{afterContent}
</div>
);
};
export const addStatsCTAAction = (actions) => {
if (window.mailpoet_premium_active) {
return actions;
},
addStatsCTALink: function addStatsCTALink(params) {
if (window.mailpoet_premium_active) {
return params;
}
const newParams = params;
newParams.link = 'admin.php?page=mailpoet-premium';
newParams.externalLink = true;
newParams.onClick = trackStatsCTAClicked;
return newParams;
},
}
actions.unshift({
name: 'stats',
link: function link() {
return (
<a href="admin.php?page=mailpoet-premium" onClick={trackStatsCTAClicked}>
{MailPoet.I18n.t('statsListingActionTitle')}
</a>
);
},
display: function display(newsletter) {
// welcome emails provide explicit total_sent value
const countProcessed = newsletter.queue && newsletter.queue.count_processed;
return Number(newsletter.total_sent || countProcessed) > 0;
},
});
return actions;
};
export const checkMailerStatus = (state) => {
@@ -465,5 +465,3 @@ export const checkCronStatus = (state) => {
jQuery('[data-id="mailpoet_cron_error"]')[0]
);
};
export { StatisticsMixin };

View File

@@ -13,7 +13,8 @@ import FeatureAnnouncement from 'announcements/feature_announcement.jsx';
import {
renderQueueStatus,
StatisticsMixin,
renderStatistics,
addStatsCTAAction,
checkCronStatus,
checkMailerStatus,
} from 'newsletters/listings/mixins.jsx';
@@ -57,7 +58,7 @@ let newsletterActions = [
},
];
Hooks.addFilter('mailpoet_newsletters_listings_notification_history_actions', 'mailpoet', StatisticsMixin.addStatsCTAAction);
Hooks.addFilter('mailpoet_newsletters_listings_notification_history_actions', 'mailpoet', addStatsCTAAction);
newsletterActions = Hooks.applyFilters('mailpoet_newsletters_listings_notification_history_actions', newsletterActions);
const NewsletterListNotificationHistory = createReactClass({ // eslint-disable-line react/prefer-es6-class, max-len
@@ -70,8 +71,6 @@ const NewsletterListNotificationHistory = createReactClass({ // eslint-disable-l
}).isRequired,
},
mixins: [StatisticsMixin],
renderItem: function renderItem(newsletter, actions, meta) {
const rowClasses = classNames(
'manage-column',
@@ -103,7 +102,7 @@ const NewsletterListNotificationHistory = createReactClass({ // eslint-disable-l
</td>
{ (mailpoetTrackingEnabled === true) ? (
<td className="column" data-colname={MailPoet.I18n.t('statistics')}>
{ this.renderStatistics(newsletter, undefined, meta.current_time) }
{ renderStatistics(newsletter, undefined, meta.current_time) }
</td>
) : null }
<td className="column-date" data-colname={MailPoet.I18n.t('sentOn')}>

View File

@@ -13,7 +13,8 @@ import FeatureAnnouncement from 'announcements/feature_announcement.jsx';
import {
renderQueueStatus,
StatisticsMixin,
renderStatistics,
addStatsCTAAction,
checkCronStatus,
checkMailerStatus,
} from 'newsletters/listings/mixins.jsx';
@@ -169,7 +170,7 @@ let newsletterActions = [
},
];
Hooks.addFilter('mailpoet_newsletters_listings_standard_actions', 'mailpoet', StatisticsMixin.addStatsCTAAction);
Hooks.addFilter('mailpoet_newsletters_listings_standard_actions', 'mailpoet', addStatsCTAAction);
newsletterActions = Hooks.applyFilters('mailpoet_newsletters_listings_standard_actions', newsletterActions);
const NewsletterListStandard = createReactClass({ // eslint-disable-line react/prefer-es6-class
@@ -182,8 +183,6 @@ const NewsletterListStandard = createReactClass({ // eslint-disable-line react/p
}).isRequired,
},
mixins: [StatisticsMixin],
renderItem: function renderItem(newsletter, actions, meta) {
const rowClasses = classNames(
'manage-column',
@@ -215,7 +214,7 @@ const NewsletterListStandard = createReactClass({ // eslint-disable-line react/p
</td>
{ (mailpoetTrackingEnabled === true) ? (
<td className="column" data-colname={MailPoet.I18n.t('statistics')}>
{ this.renderStatistics(newsletter, undefined, meta.current_time) }
{ renderStatistics(newsletter, undefined, meta.current_time) }
</td>
) : null }
<td className="column-date" data-colname={MailPoet.I18n.t('sentOn')}>

View File

@@ -9,7 +9,8 @@ import ListingHeading from 'newsletters/listings/heading.jsx';
import FeatureAnnouncement from 'announcements/feature_announcement.jsx';
import {
StatisticsMixin,
renderStatistics,
addStatsCTAAction,
checkCronStatus,
checkMailerStatus,
} from 'newsletters/listings/mixins.jsx';
@@ -154,7 +155,7 @@ let newsletterActions = [
},
];
Hooks.addFilter('mailpoet_newsletters_listings_welcome_notification_actions', 'mailpoet', StatisticsMixin.addStatsCTAAction);
Hooks.addFilter('mailpoet_newsletters_listings_welcome_notification_actions', 'mailpoet', addStatsCTAAction);
newsletterActions = Hooks.applyFilters('mailpoet_newsletters_listings_welcome_notification_actions', newsletterActions);
const NewsletterListWelcome = createReactClass({ // eslint-disable-line react/prefer-es6-class
@@ -167,8 +168,6 @@ const NewsletterListWelcome = createReactClass({ // eslint-disable-line react/pr
}).isRequired,
},
mixins: [StatisticsMixin],
updateStatus: function updateStatus(e) {
// make the event persist so that we can still override the selected value
// in the ajax callback
@@ -333,7 +332,7 @@ const NewsletterListWelcome = createReactClass({ // eslint-disable-line react/pr
</td>
{ (mailpoetTrackingEnabled === true) ? (
<td className="column" data-colname={MailPoet.I18n.t('statistics')}>
{ this.renderStatistics(
{ renderStatistics(
newsletter,
newsletter.total_sent > 0 && newsletter.statistics
) }