📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
added plan usage section
Blue Mouse829b542118b9
diff --git a/public/locales/en.json b/public/locales/en.jsonindex fc5841ef3..aa2c4c89e 100644--- a/public/locales/en.json+++ b/public/locales/en.json@@ -689,7 +689,14 @@"cancelSub": "Cancel subscription","noSubWarning": "You don't have an active subscription.\nNo new analytics data will be stored into our database. You can still view the data that was collected before the subscription was cancelled.","inactive": "Inactive",- "cancelledSubMessage": "You've cancelled your subscription.\nThe subscription will be active until {{date}}.\nAfter that, no new analytics data will be stored into our database. You can still view the data that was collected before the subscription was cancelled."+ "cancelledSubMessage": "You've cancelled your subscription.\nThe subscription will be active until {{date}}.\nAfter that, no new analytics data will be stored into our database. You can still view the data that was collected before the subscription was cancelled.",+ "planUsage": "Plan usage",+ "usageOverview": "This month you have tracked {{tracked}} events ({{trackedPerc}}%) of the {{maxEvents}} events in your plan.",+ "breakdown": "Breakdown by the event type:",+ "pageviews": "Pageview events: {{quantity}} ({{percentage}}%)",+ "customEvents": "Custom events: {{quantity}} ({{percentage}}%)",+ "captcha": "CAPTCHA events: {{quantity}} ({{percentage}}%)",+ "gotoUsage": "Go to plan usage"},"modals": {"paidFeature": {diff --git a/src/api/index.ts b/src/api/index.tsindex fe55f173d..fcad268dc 100644--- a/src/api/index.ts+++ b/src/api/index.ts@@ -959,3 +959,14 @@ export const getPaymentMetainfo = () =>? error.response.data: error.response.data.message})++export const getUsageInfo = () =>+ api+ .get('user/usageinfo')+ .then((response) => response.data)+ .catch((error) => {+ debug('%s', error)+ throw _isEmpty(error.response.data?.message)+ ? error.response.data+ : error.response.data.message+ })diff --git a/src/pages/Billing/index.tsx b/src/pages/Billing/index.tsxindex 063a349fd..e675cc950 100644--- a/src/pages/Billing/index.tsx+++ b/src/pages/Billing/index.tsx@@ -1,13 +1,19 @@/* eslint-disable jsx-a11y/anchor-has-content, lodash/prefer-lodash-method */-import React, { memo, useMemo, useState } from 'react'+import React, {+ memo, useMemo, useState, useEffect,+} from 'react'import { useTranslation, Trans } from 'react-i18next'-import { useSelector } from 'react-redux'+import { useSelector, useDispatch } from 'react-redux'+import { HashLink } from 'react-router-hash-link'import dayjs from 'dayjs'import utc from 'dayjs/plugin/utc'import duration from 'dayjs/plugin/duration'import { ExclamationTriangleIcon, InformationCircleIcon } from '@heroicons/react/24/outline'+import _round from 'lodash/round'import { CONTACT_EMAIL, paddleLanguageMapping } from 'redux/constants'+import routes from 'routes'+import sagaActions from 'redux/sagas/actions'import { withAuthentication, auth } from 'hoc/protected'import Title from 'components/Title'import Modal from 'ui/Modal'@@ -20,11 +26,12 @@ dayjs.extend(duration)const Billing = (): JSX.Element => {const [isCancelSubModalOpened, setIsCancelSubModalOpened] = useState<boolean>(false)- const { metainfo } = useSelector((state: StateType) => state.ui.misc)+ const { metainfo, usageinfo } = useSelector((state: StateType) => state.ui.misc)const { user }: {user: IUser} = useSelector((state: StateType) => state.auth)const { theme } = useSelector((state: StateType) => state.ui.theme)+ const dispatch = useDispatch()const { t, i18n: { language } }: {t: (key: string, optinions?: {[key: string]: string | number,@@ -34,11 +41,16 @@ const Billing = (): JSX.Element => {},} = useTranslation('common')const {- nextBillDate, planCode, subUpdateURL, trialEndDate, timeFormat, cancellationEffectiveDate, subCancelURL,+ nextBillDate, planCode, subUpdateURL, trialEndDate, timeFormat, cancellationEffectiveDate, subCancelURL, maxEventsCount,} = userconst isTrial: boolean = planCode === 'trial'const isNoSub: boolean = planCode === 'none'+ const totalUsage = _round((usageinfo.total / maxEventsCount) * 100, 2)++ useEffect(() => {+ dispatch(sagaActions.loadUsageinfo())+ }, [dispatch])const isTrialEnded = useMemo(() => {if (!trialEndDate) {@@ -151,6 +163,10 @@ const Billing = (): JSX.Element => {</div><p className='text-lg text-gray-900 dark:text-gray-50 tracking-tight'>{t('billing.desc')}+ <br />+ <HashLink to={`${routes.billing}#usage`} className='text-indigo-600 dark:text-indigo-400 hover:text-indigo-500 dark:hover:text-indigo-500'>+ {t('billing.gotoUsage')}+ </HashLink></p><hr className='mt-3 mb-2 border-gray-200 dark:border-gray-600' />{cancellationEffectiveDate && (@@ -207,6 +223,39 @@ const Billing = (): JSX.Element => {}}/></p>+ <h2 id='usage' className='mt-5 text-3xl font-bold text-gray-900 dark:text-gray-50 tracking-tight mr-2'>+ {t('billing.planUsage')}+ </h2>+ <p className='mt-1 text-lg text-gray-900 dark:text-gray-50 tracking-tight'>+ {t('billing.usageOverview', {+ tracked: usageinfo.total,+ trackedPerc: totalUsage,+ maxEvents: maxEventsCount,+ })}+ </p>+ <div className='mt-2 text-lg text-gray-900 dark:text-gray-50 tracking-tight'>+ {t('billing.breakdown')}+ <ul className='list-disc list-inside'>+ <li>+ {t('billing.pageviews', {+ quantity: usageinfo.traffic,+ percentage: usageinfo.trafficPerc,+ })}+ </li>+ <li>+ {t('billing.customEvents', {+ quantity: usageinfo.customEvents,+ percentage: usageinfo.customEventsPerc,+ })}+ </li>+ <li>+ {t('billing.captcha', {+ quantity: usageinfo.captcha,+ percentage: usageinfo.captchaPerc,+ })}+ </li>+ </ul>+ </div></div><ModalonClose={() => {diff --git a/src/redux/models/IUsageinfo.ts b/src/redux/models/IUsageinfo.tsnew file mode 100644index 000000000..755fa9e16--- /dev/null+++ b/src/redux/models/IUsageinfo.ts@@ -0,0 +1,9 @@+export interface IUsageInfo {+ total: number+ traffic: number+ customEvents: number+ captcha: number+ trafficPerc: number+ customEventsPerc: number+ captchaPerc: number+}diff --git a/src/redux/models/IUser.ts b/src/redux/models/IUser.tsindex 0f6ead34a..cc3639c28 100644--- a/src/redux/models/IUser.ts+++ b/src/redux/models/IUser.ts@@ -33,5 +33,6 @@ export interface IUser {googleId: string | nullgithubId: number | nullregisteredWithGithub: boolean+ maxEventsCount: numbertierCurrency: 'USD' | 'EUR' | 'GBP' | null}diff --git a/src/redux/reducers/ui/misc.ts b/src/redux/reducers/ui/misc.tsindex d6b863d88..6361dd7a3 100644--- a/src/redux/reducers/ui/misc.ts+++ b/src/redux/reducers/ui/misc.ts@@ -4,10 +4,13 @@ import { setCookie } from 'utils/cookie'import { secondsTillNextMonth } from 'utils/generic'import { IStats } from 'redux/models/IStats'import { IMetainfo } from 'redux/models/IMetainfo'+import { IUsageInfo } from 'redux/models/IUsageinfo'interface IInitialState {stats: IStatsmetainfo: IMetainfo+ usageinfo: IUsageInfo+ usageinfoLoaded: booleanpaddleLoaded: booleanpaddle: anyshowNoEventsLeftBanner: boolean@@ -26,6 +29,16 @@ const initialState: IInitialState = {symbol: '$',code: 'USD',},+ usageinfo: {+ total: 0,+ traffic: 0,+ customEvents: 0,+ captcha: 0,+ trafficPerc: 0,+ customEventsPerc: 0,+ captchaPerc: 0,+ },+ usageinfoLoaded: false,paddleLoaded: false,paddle: {},showNoEventsLeftBanner: false,@@ -57,6 +70,10 @@ const miscSlice = createSlice({setMetainfo(state, { payload }: PayloadAction<IMetainfo>) {state.metainfo = payload},+ setUsageinfo(state, { payload }: PayloadAction<IUsageInfo>) {+ state.usageinfoLoaded = true+ state.usageinfo = payload+ },setLastBlogPost(state, { payload }: PayloadAction<any>) {state.lastBlogPost = payload},diff --git a/src/redux/sagas/actions/index.ts b/src/redux/sagas/actions/index.tsindex 2841474c1..2b5fbe3ac 100644--- a/src/redux/sagas/actions/index.ts+++ b/src/redux/sagas/actions/index.ts@@ -16,6 +16,10 @@ const loadMetainfo = () => ({type: types.LOAD_METAINFO,})+const loadUsageinfo = () => ({+ type: types.LOAD_USAGEINFO,+})+const loadSharedProjects = (take?: number, skip?: number) => ({type: types.LOAD_SHARED_PROJECTS,payload: { take, skip },@@ -143,6 +147,7 @@ const sagaActions = {linkSSO,unlinkSSO,loadMetainfo,+ loadUsageinfo,}export default sagaActionsdiff --git a/src/redux/sagas/actions/types.ts b/src/redux/sagas/actions/types.tsindex 6bd321ba1..96faa5091 100644--- a/src/redux/sagas/actions/types.ts+++ b/src/redux/sagas/actions/types.ts@@ -2,6 +2,7 @@ const sagaTypes = Object.freeze({LOAD_EXTENSIONS: 'LOAD_EXTENSIONS',LOAD_PROJECTS: 'LOAD_PROJECTS',LOAD_METAINFO: 'LOAD_METAINFO',+ LOAD_USAGEINFO: 'LOAD_USAGEINFO',LOAD_SHARED_PROJECTS: 'LOAD_SHARED_PROJECTS',LOAD_PROJECT_ALERTS: 'LOAD_PROJECT_ALERTS',LOGIN_ASYNC: 'LOGIN_ASYNC',diff --git a/src/redux/sagas/ui/index.ts b/src/redux/sagas/ui/index.tsindex 2a5ddeffc..8e530ebbd 100644--- a/src/redux/sagas/ui/index.ts+++ b/src/redux/sagas/ui/index.ts@@ -9,6 +9,7 @@ import isEventsAvailable from './worker_is_events_available'import shareVerify from './share_verify'import loadExtensions from './load_extensions'import loadMetainfo from './load_metainfo'+import loadUsageinfo from './load_usageinfo'import logout from './logout'import loadSharedProjects from './load_shared_projects'import loadProjectAlerts from './load_alerts'@@ -22,6 +23,8 @@ function* mainUISaga() {yield takeEvery(sagaTypes.LOAD_PROJECTS, loadProjects)// @is-ignoreyield takeEvery(sagaTypes.LOAD_METAINFO, loadMetainfo)+ // @is-ignore+ yield takeEvery(sagaTypes.LOAD_USAGEINFO, loadUsageinfo)// @ts-ignoreyield takeEvery(sagaTypes.LOAD_SHARED_PROJECTS, loadSharedProjects)yield takeEvery(sagaTypes.LOAD_EXTENSIONS, loadExtensions)diff --git a/src/redux/sagas/ui/load_metainfo.ts b/src/redux/sagas/ui/load_metainfo.tsindex 2910c7c3f..8ca0ac711 100644--- a/src/redux/sagas/ui/load_metainfo.ts+++ b/src/redux/sagas/ui/load_metainfo.ts@@ -8,7 +8,7 @@ import {getPaymentMetainfo,} from '../../../api'-const debug = Debug('swetrix:rx:s:load-extensions')+const debug = Debug('swetrix:rx:s:metainfo')export default function* loadMetainfo() {if (isSelfhosted) {diff --git a/src/redux/sagas/ui/load_usageinfo.ts b/src/redux/sagas/ui/load_usageinfo.tsnew file mode 100644index 000000000..c697c7100--- /dev/null+++ b/src/redux/sagas/ui/load_usageinfo.ts@@ -0,0 +1,35 @@+import { put, call, select } from 'redux-saga/effects'+import Debug from 'debug'++import UIActions from 'redux/reducers/ui'+import { isSelfhosted } from 'redux/constants'+import { IUsageInfo } from 'redux/models/IUsageinfo'+import {+ getUsageInfo,+} from '../../../api'++const debug = Debug('swetrix:rx:s:usageinfo')++export default function* loadUsageinfo() {+ if (isSelfhosted) {+ return+ }++ const isUsageinfoLoaded: boolean = yield select(state => state.ui.misc.usageinfoLoaded)++ if (isUsageinfoLoaded) {+ return+ }++ try {+ const info: IUsageInfo = yield call(getUsageInfo)++ yield put(UIActions.setUsageinfo(info))+ } catch (e: unknown) {+ const { message } = e as { message: string }+ // if (_isString(message)) {+ // yield put(UIActions.setProjectsError(message))+ // }+ debug('failed to load usageinfo: %s', message)+ }+}