2021-02-04 20:36:29 +03:00
|
|
|
import Controller from '@ember/controller';
|
2021-02-19 08:48:01 +03:00
|
|
|
import {getSymbol} from 'ghost-admin/utils/currency';
|
2021-02-04 20:36:29 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2021-02-18 17:17:10 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
2021-02-04 20:36:29 +03:00
|
|
|
|
|
|
|
export default class DashboardController extends Controller {
|
2021-02-04 21:35:19 +03:00
|
|
|
@service feature;
|
2021-02-04 20:36:29 +03:00
|
|
|
@service session;
|
2021-02-18 17:17:10 +03:00
|
|
|
@service membersStats;
|
2021-02-19 15:12:53 +03:00
|
|
|
@service store;
|
2021-02-19 16:55:35 +03:00
|
|
|
@service settings;
|
2021-02-18 17:17:10 +03:00
|
|
|
|
|
|
|
@tracked
|
2021-02-19 20:17:28 +03:00
|
|
|
eventsData = null;
|
2021-02-18 20:13:51 +03:00
|
|
|
@tracked
|
2021-02-19 20:17:28 +03:00
|
|
|
eventsError = null;
|
2021-02-19 08:48:01 +03:00
|
|
|
@tracked
|
2021-02-19 20:17:28 +03:00
|
|
|
eventsLoading = false;
|
2021-02-18 17:17:10 +03:00
|
|
|
|
2021-02-19 15:12:53 +03:00
|
|
|
@tracked
|
2021-02-19 20:17:28 +03:00
|
|
|
mrrStatsData = null;
|
|
|
|
@tracked
|
|
|
|
mrrStatsError = null;
|
|
|
|
@tracked
|
|
|
|
mrrStatsLoading = false;
|
|
|
|
|
|
|
|
@tracked
|
|
|
|
memberCountStatsData = null;
|
|
|
|
@tracked
|
|
|
|
memberCountStatsError = null;
|
|
|
|
@tracked
|
|
|
|
memberCountStatsLoading = false;
|
|
|
|
|
|
|
|
@tracked
|
|
|
|
topMembersData = null;
|
|
|
|
@tracked
|
|
|
|
topMembersError = null;
|
|
|
|
@tracked
|
|
|
|
topMembersLoading = false;
|
2021-02-19 15:12:53 +03:00
|
|
|
|
2021-02-22 11:29:48 +03:00
|
|
|
@tracked
|
|
|
|
newsletterOpenRatesData = null;
|
|
|
|
@tracked
|
|
|
|
newsletterOpenRatesError = null;
|
|
|
|
@tracked
|
|
|
|
newsletterOpenRatesLoading = false;
|
|
|
|
|
2021-02-19 15:12:53 +03:00
|
|
|
get showTopMembers() {
|
|
|
|
return this.feature.get('emailAnalytics') && this.settings.get('emailTrackOpens');
|
|
|
|
}
|
|
|
|
|
2021-02-23 17:34:13 +03:00
|
|
|
initialise() {
|
2021-02-18 17:17:10 +03:00
|
|
|
this.loadEvents();
|
2021-02-19 15:12:53 +03:00
|
|
|
this.loadTopMembers();
|
2021-02-18 20:13:51 +03:00
|
|
|
this.loadCharts();
|
|
|
|
}
|
|
|
|
|
2021-02-19 08:48:01 +03:00
|
|
|
loadMRRStats() {
|
2021-02-18 20:13:51 +03:00
|
|
|
this.membersStats.fetchMRR().then((stats) => {
|
2021-02-19 20:17:28 +03:00
|
|
|
this.mrrStatsLoading = false;
|
2021-02-19 08:48:01 +03:00
|
|
|
|
2021-02-19 15:50:41 +03:00
|
|
|
let currencyStats = stats[0] || {
|
|
|
|
data: [],
|
|
|
|
currency: 'usd'
|
|
|
|
};
|
2021-02-19 08:48:01 +03:00
|
|
|
if (currencyStats) {
|
|
|
|
currencyStats.data = this.membersStats.fillDates(currencyStats.data) || {};
|
|
|
|
const dateValues = Object.values(currencyStats.data).map(val => val / 100);
|
|
|
|
const currentMRR = dateValues.length ? dateValues[dateValues.length - 1] : 0;
|
2021-02-19 15:50:41 +03:00
|
|
|
const rangeStartMRR = dateValues.length ? dateValues[0] : 0;
|
2021-02-22 12:17:05 +03:00
|
|
|
const percentGrowth = rangeStartMRR !== 0 ? ((currentMRR - rangeStartMRR) / rangeStartMRR) * 100 : 0;
|
2021-02-19 20:17:28 +03:00
|
|
|
this.mrrStatsData = {
|
2021-02-19 08:48:01 +03:00
|
|
|
current: `${getSymbol(currencyStats.currency)}${currentMRR}`,
|
2021-02-22 12:17:05 +03:00
|
|
|
percentGrowth: percentGrowth.toFixed(1),
|
2021-02-19 08:48:01 +03:00
|
|
|
options: {
|
|
|
|
rangeInDays: 30
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
label: 'MRR',
|
|
|
|
dateLabels: Object.keys(currencyStats.data),
|
|
|
|
dateValues
|
|
|
|
},
|
|
|
|
title: 'MRR',
|
|
|
|
stats: currencyStats
|
|
|
|
};
|
|
|
|
}
|
2021-02-18 20:13:51 +03:00
|
|
|
}, (error) => {
|
2021-02-19 20:17:28 +03:00
|
|
|
this.mrrStatsError = error;
|
|
|
|
this.mrrStatsLoading = false;
|
2021-02-18 20:13:51 +03:00
|
|
|
});
|
2021-02-18 17:17:10 +03:00
|
|
|
}
|
|
|
|
|
2021-02-19 08:48:01 +03:00
|
|
|
loadMemberCountStats() {
|
|
|
|
this.membersStats.fetchCounts().then((stats) => {
|
2021-02-19 20:17:28 +03:00
|
|
|
this.memberCountStatsLoading = false;
|
2021-02-19 08:48:01 +03:00
|
|
|
|
|
|
|
if (stats) {
|
|
|
|
stats.data = this.membersStats.fillCountDates(stats.data) || {};
|
|
|
|
const dateValues = Object.values(stats.data);
|
2021-02-22 12:17:05 +03:00
|
|
|
const currentAllCount = dateValues.length ? dateValues[dateValues.length - 1].total : 0;
|
|
|
|
const currentPaidCount = dateValues.length ? dateValues[dateValues.length - 1].paid : 0;
|
|
|
|
const rangeStartAllCount = dateValues.length ? dateValues[0].total : 0;
|
|
|
|
const rangeStartPaidCount = dateValues.length ? dateValues[0].paid : 0;
|
|
|
|
const allCountPercentGrowth = rangeStartAllCount !== 0 ? ((currentAllCount - rangeStartAllCount) / rangeStartAllCount) * 100 : 0;
|
|
|
|
const paidCountPercentGrowth = rangeStartPaidCount !== 0 ? ((currentPaidCount - rangeStartPaidCount) / rangeStartPaidCount) * 100 : 0;
|
2021-02-19 08:48:01 +03:00
|
|
|
|
2021-02-19 20:17:28 +03:00
|
|
|
this.memberCountStatsData = {
|
2021-02-19 08:48:01 +03:00
|
|
|
all: {
|
2021-02-22 12:17:05 +03:00
|
|
|
percentGrowth: allCountPercentGrowth.toFixed(1),
|
2021-02-19 08:48:01 +03:00
|
|
|
total: dateValues.length ? dateValues[dateValues.length - 1].total : 0,
|
|
|
|
options: {
|
|
|
|
rangeInDays: 30
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
label: 'Members',
|
|
|
|
dateLabels: Object.keys(stats.data),
|
|
|
|
dateValues: dateValues.map(d => d.total)
|
|
|
|
},
|
|
|
|
title: 'Total Members',
|
|
|
|
stats: stats
|
|
|
|
},
|
|
|
|
paid: {
|
2021-02-22 12:17:05 +03:00
|
|
|
percentGrowth: paidCountPercentGrowth.toFixed(1),
|
2021-02-19 08:48:01 +03:00
|
|
|
total: dateValues.length ? dateValues[dateValues.length - 1].paid : 0,
|
|
|
|
options: {
|
|
|
|
rangeInDays: 30
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
label: 'Members',
|
|
|
|
dateLabels: Object.keys(stats.data),
|
|
|
|
dateValues: dateValues.map(d => d.paid)
|
|
|
|
},
|
|
|
|
title: 'Paid Members',
|
|
|
|
stats: stats
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}, (error) => {
|
2021-02-19 20:17:28 +03:00
|
|
|
this.memberCountStatsError = error;
|
|
|
|
this.memberCountStatsLoading = false;
|
2021-02-19 08:48:01 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
loadCharts() {
|
|
|
|
this.loadMRRStats();
|
|
|
|
this.loadMemberCountStats();
|
2021-02-22 11:29:48 +03:00
|
|
|
this.loadNewsletterOpenRates();
|
2021-02-19 08:48:01 +03:00
|
|
|
}
|
|
|
|
|
2021-02-18 17:17:10 +03:00
|
|
|
loadEvents() {
|
2021-02-19 20:17:28 +03:00
|
|
|
this.eventsLoading = true;
|
2021-02-23 15:46:17 +03:00
|
|
|
this.membersStats.fetchTimeline({limit: 5}).then(({events}) => {
|
2021-02-19 20:17:28 +03:00
|
|
|
this.eventsData = events;
|
|
|
|
this.eventsLoading = false;
|
2021-02-18 17:17:10 +03:00
|
|
|
}, (error) => {
|
2021-02-19 20:17:28 +03:00
|
|
|
this.eventsError = error;
|
|
|
|
this.eventsLoading = false;
|
2021-02-18 17:17:10 +03:00
|
|
|
});
|
|
|
|
}
|
2021-02-19 15:12:53 +03:00
|
|
|
|
2021-02-22 11:29:48 +03:00
|
|
|
loadNewsletterOpenRates() {
|
|
|
|
this.newsletterOpenRatesLoading = true;
|
|
|
|
this.membersStats.fetchNewsletterStats().then((results) => {
|
|
|
|
this.newsletterOpenRatesData = {
|
|
|
|
options: {
|
|
|
|
rangeInDays: 30
|
|
|
|
},
|
|
|
|
data: {
|
2021-02-23 14:50:54 +03:00
|
|
|
label: 'Open rate',
|
|
|
|
dateLabels: results.map(d => d.subject),
|
2021-02-22 11:29:48 +03:00
|
|
|
dateValues: results.map(d => d.openRate)
|
|
|
|
},
|
2021-02-23 14:50:54 +03:00
|
|
|
title: 'Open rate',
|
2021-02-22 11:29:48 +03:00
|
|
|
stats: results
|
|
|
|
};
|
|
|
|
this.newsletterOpenRatesLoading = false;
|
|
|
|
}, (error) => {
|
|
|
|
this.newsletterOpenRatesError = error;
|
|
|
|
this.newsletterOpenRatesLoading = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-19 15:12:53 +03:00
|
|
|
loadTopMembers() {
|
2021-02-19 20:17:28 +03:00
|
|
|
this.topMembersLoading = true;
|
2021-02-19 15:12:53 +03:00
|
|
|
let query = {
|
|
|
|
filter: 'email_open_rate:-null',
|
|
|
|
order: 'email_open_rate desc',
|
|
|
|
limit: 10
|
|
|
|
};
|
|
|
|
this.store.query('member', query).then((result) => {
|
2021-02-19 20:17:28 +03:00
|
|
|
this.topMembersData = result;
|
|
|
|
this.topMembersLoading = false;
|
2021-02-19 15:12:53 +03:00
|
|
|
}, (error) => {
|
2021-02-19 20:17:28 +03:00
|
|
|
this.topMembersError = error;
|
|
|
|
this.topMembersLoading = false;
|
2021-02-19 15:12:53 +03:00
|
|
|
});
|
|
|
|
}
|
2021-02-18 17:17:10 +03:00
|
|
|
}
|