Skip to content

📈 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!

working on warning banner when user runs out of available tier events

Blue Mousea316164dba2b
diff --git a/src/components/EventsRunningOutBanner.jsx b/src/components/EventsRunningOutBanner.jsx
new file mode 100644
index 000000000..772f2e954
--- /dev/null
+++ b/src/components/EventsRunningOutBanner.jsx
@@ -0,0 +1,51 @@
+import React, { memo } from 'react'
+import { ExclamationIcon, XIcon } from '@heroicons/react/outline'
+import { useDispatch } from 'react-redux'
+import { useTranslation } from 'react-i18next'
+import { authActions } from 'redux/actions/auth'
+
+const EventsRunningOutBanner = () => {
+ const { t } = useTranslation('common')
+ const dispatch = useDispatch()
+
+ const logoutHandler = () => {
+ dispatch(authActions.logout())
+ }
+
+ return (
+ <div className='bg-yellow-400'>
+ <div className='max-w-7xl mx-auto py-3 px-3 sm:px-6 lg:px-8'>
+ <div className='flex items-center justify-between flex-wrap'>
+ <div className='w-0 flex-1 flex items-center'>
+ <span className='flex p-2 rounded-lg bg-yellow-600'>
+ <ExclamationIcon className='h-6 w-6 text-white' aria-hidden='true' />
+ </span>
+ <p className='ml-3 font-medium text-black truncate'>
+ <span className='md:hidden'>You are running out of events!</span>
+ <span className='hidden md:inline'>You have used more than 85% of the available events per your tier for this month.</span>
+ </p>
+ </div>
+ <div className='order-3 mt-2 flex-shrink-0 w-full sm:order-2 sm:mt-0 sm:w-auto'>
+ {/* TODO: SHOW MODAL */}
+ <span
+ className='flex items-center justify-center px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-yellow-600 bg-white hover:bg-yellow-50'
+ >
+ Learn more
+ </span>
+ </div>
+ <div className='order-2 flex-shrink-0 sm:order-3 sm:ml-3'>
+ <button
+ type='button'
+ className='-mr-1 flex p-2 rounded-md hover:bg-yellow-500 focus:outline-none focus:ring-2 focus:ring-white sm:-mr-2'
+ >
+ <span className='sr-only'>Dismiss</span>
+ <XIcon className='h-6 w-6 text-black' aria-hidden='true' />
+ </button>
+ </div>
+ </div>
+ </div>
+ </div>
+ )
+}
+
+export default memo(EventsRunningOutBanner)
diff --git a/src/pages/Dashboard/Dashboard.jsx b/src/pages/Dashboard/Dashboard.jsx
index 76cee68d1..96bb7ab6c 100644
--- a/src/pages/Dashboard/Dashboard.jsx
+++ b/src/pages/Dashboard/Dashboard.jsx
@@ -19,6 +19,7 @@ import { ActivePin, InactivePin } from 'ui/Pin'
import PulsatingCircle from 'ui/icons/PulsatingCircle'
import routes from 'routes'
import { isSelfhosted } from 'redux/constants'
+import EventsRunningOutBanner from 'components/EventsRunningOutBanner'
const ProjectCart = ({
name, url, created, active, overall, t, language, live, isPublic,
@@ -152,38 +153,41 @@ const Dashboard = ({ projects, isLoading, error, user }) => {
if (!isLoading) {
return (
<Title title={t('titles.dashboard')}>
- <div className='min-h-min-footer bg-gray-50 dark:bg-gray-800 flex flex-col py-6 px-4 sm:px-6 lg:px-8'>
- <div className='max-w-7xl w-full mx-auto'>
- <div className='flex justify-between'>
- <h2 className='mt-2 text-3xl font-extrabold text-gray-900 dark:text-gray-50'>
- {t('titles.dashboard')}
- </h2>
- <span onClick={onNewProject} className='inline-flex cursor-pointer items-center border border-transparent leading-4 font-medium rounded-md focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 px-4 py-2 text-sm'>
- {t('dashboard.newProject')}
- </span>
- </div>
- {_isEmpty(projects) ? (
- <NoProjects t={t} />
- ) : (
- <div className='bg-white shadow overflow-hidden sm:rounded-md mt-10'>
- <ul className='divide-y divide-gray-200 dark:divide-gray-500'>
- {_map(_filter(projects, ({ uiHidden }) => !uiHidden), ({ name, id, created, active, overall, live, public: isPublic }) => (
- <ProjectCart
- key={id}
- t={t}
- language={language}
- name={name}
- created={created}
- active={active}
- isPublic={isPublic}
- overall={overall}
- live={_isNumber(live) ? live : 'N/A'}
- url={routes.project.replace(':id', id)}
- />
- ))}
- </ul>
+ <div className='min-h-min-footer bg-gray-50 dark:bg-gray-800'>
+ <EventsRunningOutBanner />
+ <div className='flex flex-col py-6 px-4 sm:px-6 lg:px-8'>
+ <div className='max-w-7xl w-full mx-auto'>
+ <div className='flex justify-between'>
+ <h2 className='mt-2 text-3xl font-extrabold text-gray-900 dark:text-gray-50'>
+ {t('titles.dashboard')}
+ </h2>
+ <span onClick={onNewProject} className='inline-flex cursor-pointer items-center border border-transparent leading-4 font-medium rounded-md focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 px-4 py-2 text-sm'>
+ {t('dashboard.newProject')}
+ </span>
</div>
- )}
+ {_isEmpty(projects) ? (
+ <NoProjects t={t} />
+ ) : (
+ <div className='bg-white shadow overflow-hidden sm:rounded-md mt-10'>
+ <ul className='divide-y divide-gray-200 dark:divide-gray-500'>
+ {_map(_filter(projects, ({ uiHidden }) => !uiHidden), ({ name, id, created, active, overall, live, public: isPublic }) => (
+ <ProjectCart
+ key={id}
+ t={t}
+ language={language}
+ name={name}
+ created={created}
+ active={active}
+ isPublic={isPublic}
+ overall={overall}
+ live={_isNumber(live) ? live : 'N/A'}
+ url={routes.project.replace(':id', id)}
+ />
+ ))}
+ </ul>
+ </div>
+ )}
+ </div>
</div>
</div>
<Modal
diff --git a/src/redux/actions/ui/index.js b/src/redux/actions/ui/index.js
index 8410e850b..516e580a6 100644
--- a/src/redux/actions/ui/index.js
+++ b/src/redux/actions/ui/index.js
@@ -11,6 +11,20 @@ const setProjects = (projects) => ({
},
})
+const setTotalMonthlyEvents = (totalMonthlyEvents) => ({
+ type: types.SET_TOTAL_MONTHLY_EVENTS,
+ payload: {
+ totalMonthlyEvents,
+ },
+})
+
+const setShowNoEventsLeftBanner = (showNoEventsLeftBanner) => ({
+ type: types.SET_SHOW_NO_EVENTS_LEFT,
+ payload: {
+ showNoEventsLeftBanner,
+ },
+})
+
const setLiveStats = (data) => ({
type: types.SET_LIVE_STATS,
payload: {
@@ -110,6 +124,8 @@ const UIActions = {
setTheme,
setGeneralStats,
setPaddleLastEvent,
+ setTotalMonthlyEvents,
+ setShowNoEventsLeftBanner,
}
export default UIActions
diff --git a/src/redux/actions/ui/types.js b/src/redux/actions/ui/types.js
index 50bee0d9f..86b093b2f 100644
--- a/src/redux/actions/ui/types.js
+++ b/src/redux/actions/ui/types.js
@@ -13,4 +13,6 @@ export const types = {
SET_THEME: 'UI_SET_THEME',
SET_GENERAL_STATS: 'UI_SET_GENERAL_STATS',
SET_PADDLE_LAST_EVENT: 'UI_SET_PADDLE_LAST_EVENT',
+ SET_TOTAL_MONTHLY_EVENTS: 'UI_SET_TOTAL_MONTHLY_EVENTS',
+ SET_SHOW_NO_EVENTS_LEFT: 'UI_SET_SHOW_NO_EVENTS_LEFT',
}
diff --git a/src/redux/reducers/ui/misc.js b/src/redux/reducers/ui/misc.js
index 414ecccd6..bfd9ed1f4 100644
--- a/src/redux/reducers/ui/misc.js
+++ b/src/redux/reducers/ui/misc.js
@@ -8,13 +8,15 @@ const initialState = {
},
paddle: {
lastEvent: null,
- }
+ },
+ showNoEventsLeftBanner: false,
}
const miscReducer = (state = initialState, { type, payload }) => {
switch (type) {
case types.SET_GENERAL_STATS: {
const { stats } = payload
+
return {
...state,
stats,
@@ -23,12 +25,26 @@ const miscReducer = (state = initialState, { type, payload }) => {
case types.SET_PADDLE_LAST_EVENT: {
const { event } = payload
+
return {
...state,
paddle: {
...state.paddle,
lastEvent: event,
- }
+ },
+ }
+ }
+
+ case types.SET_SHOW_NO_EVENTS_LEFT: {
+ const { showNoEventsLeftBanner } = payload
+
+ if (!showNoEventsLeftBanner) {
+ // set cookie
+ }
+
+ return {
+ ...state,
+ showNoEventsLeftBanner,
}
}
diff --git a/src/redux/reducers/ui/projects.js b/src/redux/reducers/ui/projects.js
index 9cbb35962..37702dd92 100644
--- a/src/redux/reducers/ui/projects.js
+++ b/src/redux/reducers/ui/projects.js
@@ -8,6 +8,7 @@ const getInitialState = () => {
projects: [],
isLoading: true,
error: null,
+ totalMonthlyEvents: null,
}
}
@@ -22,6 +23,14 @@ const projectsReducer = (state = getInitialState(), { type, payload }) => {
}
}
+ case types.SET_TOTAL_MONTHLY_EVENTS: {
+ const { totalMonthlyEvents } = payload
+ return {
+ ...state,
+ totalMonthlyEvents,
+ }
+ }
+
case types.SET_LIVE_STATS: {
const { data } = payload
diff --git a/src/redux/sagas/ui/index.js b/src/redux/sagas/ui/index.js
index d7d92985f..da572f20d 100644
--- a/src/redux/sagas/ui/index.js
+++ b/src/redux/sagas/ui/index.js
@@ -6,11 +6,13 @@ import loadProjects from './load_projects'
import initialise from './initialise'
import liveVisitors from './worker_live_visitors'
// import generalStats from './worker_general_stats'
+import isEventsAvailable from './worker_is_events_available'
import logout from './logout'
function* mainUISaga() {
yield fork(initialise)
yield fork(liveVisitors)
+ yield fork(isEventsAvailable)
// yield fork(generalStats)
yield takeEvery(types.LOAD_PROJECTS, loadProjects)
yield takeEvery(authTypes.LOGOUT, logout)
diff --git a/src/redux/sagas/ui/load_projects.js b/src/redux/sagas/ui/load_projects.js
index f31453c87..96320112e 100644
--- a/src/redux/sagas/ui/load_projects.js
+++ b/src/redux/sagas/ui/load_projects.js
@@ -12,7 +12,7 @@ const debug = Debug('swetrix:rx:s:load-projects')
export default function* loadProjects() {
try {
- let { results } = yield call(getProjects)
+ let { results, totalMonthlyEvents } = yield call(getProjects)
const pids = _map(results, result => result.id)
let overall
@@ -27,6 +27,7 @@ export default function* loadProjects() {
overall: overall?.[res.id],
}))
yield put(UIActions.setProjects(results))
+ yield put(UIActions.setTotalMonthlyEvents(totalMonthlyEvents))
const liveStats = yield call(getLiveVisitors, pids)
yield put(UIActions.setLiveStats(liveStats))
diff --git a/src/redux/sagas/ui/worker_general_stats.js b/src/redux/sagas/ui/worker_general_stats.js
index 297d47585..64a01af4d 100644
--- a/src/redux/sagas/ui/worker_general_stats.js
+++ b/src/redux/sagas/ui/worker_general_stats.js
@@ -7,10 +7,13 @@ import { GENERAL_STATS_UPDATE_INTERVAL } from 'redux/constants'
const debug = Debug('swetrix:rx:s:general-stats')
+const NOT_AUTHED_INTERVAL = 5000 // 5 seconds
+
export default function* generalStats() {
while (true) {
const isAuthenticated = yield select(state => state.auth.authenticated)
if (isAuthenticated) {
+ yield delay(NOT_AUTHED_INTERVAL)
continue
}
diff --git a/src/redux/sagas/ui/worker_is_events_available.js b/src/redux/sagas/ui/worker_is_events_available.js
new file mode 100644
index 000000000..4407c7622
--- /dev/null
+++ b/src/redux/sagas/ui/worker_is_events_available.js
@@ -0,0 +1,40 @@
+import { put, delay, select } from 'redux-saga/effects'
+import _isNull from 'lodash/isNull'
+
+import UIActions from 'redux/actions/ui'
+
+const NOT_AUTHED_INTERVAL = 1000 // 1 second
+const NO_DATA_YET = 1000 // 1 second
+const SHOW_BANNER_AT_PERC = 85 // show banner when 85% of events in tier are used
+
+export default function* checkEventsAvailable() {
+ while (true) {
+ // todo: check banner cookie
+
+ const isAuthenticated = yield select(state => state.auth.authenticated)
+
+ if (!isAuthenticated) {
+ yield delay(NOT_AUTHED_INTERVAL)
+ continue
+ }
+
+ const totalMonthlyEvents = yield select(state => state.ui.projects.totalMonthlyEvents)
+
+ if (_isNull(totalMonthlyEvents)) {
+ yield delay(NO_DATA_YET)
+ continue
+ }
+
+ const maxEventsCount = 5000 // yield select(state => state.auth.user.maxEventsCount)
+
+ const eventsUsedPercentage = totalMonthlyEvents * 100 / maxEventsCount
+
+ console.log(totalMonthlyEvents, maxEventsCount, eventsUsedPercentage)
+
+ if (eventsUsedPercentage >= SHOW_BANNER_AT_PERC) {
+ yield put(UIActions.setShowNoEventsLeftBanner(true))
+ }
+
+ return
+ }
+}