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!

added full banner support

Blue Mouse94aa505ee0cf
diff --git a/public/locales/en.json b/public/locales/en.json
index 57afad2b1..71344246e 100644
--- a/public/locales/en.json
+++ b/public/locales/en.json
@@ -29,6 +29,7 @@
"settings": "Settings",
"openMenu": "Open menu",
"closeMenu": "Close menu",
+ "learnMore": "Learn more",
"yes": "Yes",
"no": "No",
"is": "is",
@@ -200,6 +201,9 @@
"newProject": "New project",
"verifyEmailTitle": "Please, verify the email address first",
"verifyEmailDesc": "To start using our service and creating your projects, you first need to confirm your email address.\n\nA link to confirm your account was sent to the email address you provided during registration, if it was not delivered - you can request the link again in your account settings.",
+ "lowEventsTitle": "You are running out of events!",
+ "lowEventsDesc": "You have used more than 85% of the available events per your tier for this month.\nThat means that in case you use 100% of the available events until the end of the month, all the subsequent traffic will not be saved.\nTo avoid this we recommend that you update your tier on the Billing page.",
+ "eventsXPercUsed": "You have used more than {{amount}}% of the available events per your tier for this month.",
"noProjects": "You have not yet created any projects",
"createProject": "Create a new project here to start using our service",
"active": "Active",
diff --git a/src/components/EventsRunningOutBanner.jsx b/src/components/EventsRunningOutBanner.jsx
index 772f2e954..5b1d4da54 100644
--- a/src/components/EventsRunningOutBanner.jsx
+++ b/src/components/EventsRunningOutBanner.jsx
@@ -1,50 +1,79 @@
-import React, { memo } from 'react'
+import React, { memo, useState } from 'react'
import { ExclamationIcon, XIcon } from '@heroicons/react/outline'
-import { useDispatch } from 'react-redux'
+import { useDispatch, useSelector } from 'react-redux'
import { useTranslation } from 'react-i18next'
-import { authActions } from 'redux/actions/auth'
+
+import Modal from 'ui/Modal'
+import UIActions from 'redux/actions/ui'
+import { SHOW_BANNER_AT_PERC } from 'redux/constants'
const EventsRunningOutBanner = () => {
const { t } = useTranslation('common')
const dispatch = useDispatch()
+ const showNoEventsLeftBanner = useSelector(state => state.ui.misc.showNoEventsLeftBanner)
+ const [showModeInfoModal, setShowModeInfoModal] = useState(false)
+
+ const closeHandler = () => {
+ dispatch(UIActions.setShowNoEventsLeftBanner(false))
+ }
- const logoutHandler = () => {
- dispatch(authActions.logout())
+ if (!showNoEventsLeftBanner) {
+ return null
}
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 className='bg-yellow-400 dark:bg-yellow-500'>
+ <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'>
+ {t('dashboard.lowEventsTitle')}
+ </span>
+ <span className='hidden md:inline'>
+ {t('dashboard.eventsXPercUsed', {
+ amount: SHOW_BANNER_AT_PERC,
+ })}
+ </span>
+ </p>
+ </div>
+ <div className='order-3 mt-2 flex-shrink-0 w-full sm:order-2 sm:mt-0 sm:w-auto'>
+ <span
+ onClick={() => setShowModeInfoModal(true)}
+ className='flex items-center justify-center cursor-pointer px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-yellow-600 bg-gray-50 hover:bg-yellow-50 dark:text-gray-50 dark:bg-gray-700 dark:hover:bg-gray-600'
+ >
+ {t('common.learnMore')}
+ </span>
+ </div>
+ <div className='order-2 flex-shrink-0 sm:order-3 sm:ml-3'>
+ <button
+ type='button'
+ onClick={closeHandler}
+ 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'>
+ {t('common.close')}
+ </span>
+ <XIcon className='h-6 w-6 text-black' aria-hidden='true' />
+ </button>
+ </div>
</div>
</div>
</div>
- </div>
+ <Modal
+ onClose={() => setShowModeInfoModal(false)}
+ onSubmit={() => setShowModeInfoModal(false)}
+ submitText={t('common.gotIt')}
+ title={t('dashboard.lowEventsTitle')}
+ message={t('dashboard.lowEventsDesc')}
+ type='warning'
+ isOpened={showModeInfoModal}
+ />
+ </>
)
}
diff --git a/src/pages/Project/View/ViewProject.jsx b/src/pages/Project/View/ViewProject.jsx
index 556338cef..88004a415 100644
--- a/src/pages/Project/View/ViewProject.jsx
+++ b/src/pages/Project/View/ViewProject.jsx
@@ -32,6 +32,7 @@ import _truncate from 'lodash/truncate'
import PropTypes from 'prop-types'
import Title from 'components/Title'
+import EventsRunningOutBanner from 'components/EventsRunningOutBanner'
import {
tbPeriodPairs, tbsFormatMapper, getProjectCacheKey, LIVE_VISITORS_UPDATE_INTERVAL,
} from 'redux/constants'
@@ -441,6 +442,7 @@ const ViewProject = ({
if (!isLoading) {
return (
<Title title={name}>
+ <EventsRunningOutBanner />
<div className={cx('bg-gray-50 dark:bg-gray-800 py-6 px-4 sm:px-6 lg:px-8', {
'min-h-min-footer': authenticated,
'min-h-min-footer-ad': !authenticated,
diff --git a/src/redux/constants/index.js b/src/redux/constants/index.js
index d67858d70..f2f17ebcb 100644
--- a/src/redux/constants/index.js
+++ b/src/redux/constants/index.js
@@ -41,6 +41,8 @@ export const reportFrequencies = ['weekly', 'monthly', 'never']
export const GDPR_EXPORT_TIMEFRAME = 14 // days
+export const SHOW_BANNER_AT_PERC = 85 // show banner when 85% of events in tier are used
+
export const TOKEN = 'access_token'
export const TITLE_SUFFIX = '| Swetrix'
@@ -68,6 +70,7 @@ export const getProjectCacheKey = (period, timeBucket) => `${period}${timeBucket
// Cookies
export const GDPR_REQUEST = 'gdpr_request'
export const CONFIRMATION_TIMEOUT = 'confirmation_timeout'
+export const LOW_EVENTS_WARNING = 'low_events_warning'
// List of languages with translations available
export const whitelist = ['en', 'uk', 'de', 'sv', 'el', 'ru', 'hi', 'zh']
diff --git a/src/redux/reducers/ui/misc.js b/src/redux/reducers/ui/misc.js
index bfd9ed1f4..2880c472c 100644
--- a/src/redux/reducers/ui/misc.js
+++ b/src/redux/reducers/ui/misc.js
@@ -1,4 +1,7 @@
import { types } from 'redux/actions/ui/types'
+import { LOW_EVENTS_WARNING } from 'redux/constants'
+import { setCookie } from 'utils/cookie'
+import { secondsTillNextMonth } from 'utils/generic'
const initialState = {
stats: {
@@ -39,7 +42,8 @@ const miscReducer = (state = initialState, { type, payload }) => {
const { showNoEventsLeftBanner } = payload
if (!showNoEventsLeftBanner) {
- // set cookie
+ const maxAge = secondsTillNextMonth() + 86400
+ setCookie(LOW_EVENTS_WARNING, 1, maxAge)
}
return {
diff --git a/src/redux/sagas/ui/worker_is_events_available.js b/src/redux/sagas/ui/worker_is_events_available.js
index 4407c7622..0c0d8e9d6 100644
--- a/src/redux/sagas/ui/worker_is_events_available.js
+++ b/src/redux/sagas/ui/worker_is_events_available.js
@@ -2,14 +2,26 @@ import { put, delay, select } from 'redux-saga/effects'
import _isNull from 'lodash/isNull'
import UIActions from 'redux/actions/ui'
+import { SHOW_BANNER_AT_PERC, LOW_EVENTS_WARNING } from 'redux/constants'
+import { getCookie } from 'utils/cookie'
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
+const DATA_REQUEST_ATTEMPTS = 20
export default function* checkEventsAvailable() {
+ const lowEvents = getCookie(LOW_EVENTS_WARNING)
+
+ if (lowEvents) {
+ return
+ }
+
+ const dataRequests = 0
+
while (true) {
- // todo: check banner cookie
+ if (dataRequests > DATA_REQUEST_ATTEMPTS) {
+ return
+ }
const isAuthenticated = yield select(state => state.auth.authenticated)
@@ -21,16 +33,15 @@ export default function* checkEventsAvailable() {
const totalMonthlyEvents = yield select(state => state.ui.projects.totalMonthlyEvents)
if (_isNull(totalMonthlyEvents)) {
+ dataRequests++
yield delay(NO_DATA_YET)
continue
}
- const maxEventsCount = 5000 // yield select(state => state.auth.user.maxEventsCount)
+ const maxEventsCount = 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))
}
diff --git a/src/utils/cookie.js b/src/utils/cookie.js
index 8c37e7cf9..a755c69a8 100644
--- a/src/utils/cookie.js
+++ b/src/utils/cookie.js
@@ -3,6 +3,6 @@ export const getCookie = key => {
if (match) return match[2]
}
-export const setCookie = (key, value, maxAge = 3600) => {
- document.cookie = `${key}=${value}; max-age=${maxAge}; path=/`
+export const setCookie = (key, value, maxAge = 3600, sameSite = 'strict') => {
+ document.cookie = `${key}=${value}; max-age=${maxAge}; path=/; SameSite=${sameSite}`
}
diff --git a/src/utils/generic.js b/src/utils/generic.js
index bb1897994..f5b39beed 100644
--- a/src/utils/generic.js
+++ b/src/utils/generic.js
@@ -11,3 +11,14 @@ export const nFormatter = (num, digits) => {
return item ? (num / item.value).toFixed(digits).replace(rx, '$1') + item.symbol : '0'
}
+
+export const secondsTillNextMonth = () => {
+ const now = new Date()
+ const date = new Date()
+
+ date.setMonth(date.getMonth() + 1)
+ date.setDate(1)
+ date.setHours(0, 0, 0, 0)
+
+ return 0 | (date - now) / 1000
+}