📈 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 analytics data & project view settings caching, fixed crash with non-string value supplied to Title
Blue Mouseaaafb2725f03
diff --git a/src/components/Title.jsx b/src/components/Title.jsxindex b9069f928..588b298c6 100644--- a/src/components/Title.jsx+++ b/src/components/Title.jsx@@ -1,14 +1,39 @@import React from 'react'import { Helmet } from 'react-helmet'+import _isEmpty from 'lodash/isEmpty'+import PropTypes from 'prop-types'+import { TITLE_SUFFIX } from 'redux/constants'-const Title = ({ title, children }) => (- <>- <Helmet>- <title>{title} {TITLE_SUFFIX}</title>- </Helmet>- {children}- </>-)+const Title = ({ title, children }) => {+ if (_isEmpty(title)) {+ return (+ <>+ <Helmet>+ <title>Swetrix Analytics</title>+ </Helmet>+ {children}+ </>+ )+ }++ return (+ <>+ <Helmet>+ <title>{title} {TITLE_SUFFIX}</title>+ </Helmet>+ {children}+ </>+ )+}++Title.propTypes = {+ children: PropTypes.node.isRequired,+ title: PropTypes.string,+}++Title.defaultProps = {+ title: '',+}export default Titlediff --git a/src/pages/Project/View/ViewProject.container.jsx b/src/pages/Project/View/ViewProject.container.jsxindex 33443d213..0f3a17909 100644--- a/src/pages/Project/View/ViewProject.container.jsx+++ b/src/pages/Project/View/ViewProject.container.jsx@@ -1,16 +1,25 @@import { connect } from 'react-redux'import { errorsActions } from 'redux/actions/errors'+import UIActions from 'redux/actions/ui'import ViewProject from './ViewProject'const mapStateToProps = (state) => ({projects: state.ui.projects.projects,isLoading: state.ui.projects.isLoading,+ cache: state.ui.cache.analytics,+ projectViewPrefs: state.ui.cache.projectViewPrefs,})const mapDispatchToProps = (dispatch) => ({showError: (message) => {dispatch(errorsActions.genericError(message))},+ setProjectCache: (pid, period, timeBucket, data) => {+ dispatch(UIActions.setProjectCache(pid, period, timeBucket, data))+ },+ setProjectViewPrefs: (pid, period, timeBucket) => {+ dispatch(UIActions.setProjectViewPrefs(pid, period, timeBucket))+ },})export default connect(mapStateToProps, mapDispatchToProps)(ViewProject)diff --git a/src/pages/Project/View/ViewProject.jsx b/src/pages/Project/View/ViewProject.jsxindex 2e9efd216..6eaed38a8 100644--- a/src/pages/Project/View/ViewProject.jsx+++ b/src/pages/Project/View/ViewProject.jsx@@ -14,7 +14,9 @@ import _find from 'lodash/find'import PropTypes from 'prop-types'import Title from 'components/Title'-import { tbPeriodPairs, tbsFormatMapper } from 'redux/constants'+import {+ tbPeriodPairs, tbsFormatMapper, getProjectCacheKey,+} from 'redux/constants'import Button from 'ui/Button'import Loader from 'ui/Loader'import Dropdown from 'ui/Dropdown'@@ -52,12 +54,12 @@ const getSettings = (chart, timeBucket, showTotal = true) => ({tooltip: {format: {title: (x) => d3.timeFormat(tbsFormatMapper[timeBucket])(x),- }+ },},point: {focus: {only: true,- }+ },},bindto: '#dataChart',})@@ -85,17 +87,17 @@ const NoEvents = () => ()const ViewProject = ({- projects, isLoading, showError,+ projects, isLoading, showError, cache, setProjectCache, projectViewPrefs, setProjectViewPrefs,}) => {const { id } = useParams()const history = useHistory()const project = useMemo(() => _find(projects, p => p.id === id) || {}, [projects, id])const [panelsData, setPanelsData] = useState({})const [analyticsLoading, setAnalyticsLoading] = useState(true)- const [period, setPeriod] = useState(tbPeriodPairs[1].period)- const [timeBucket, setTimebucket] = useState(tbPeriodPairs[1].tbs[1])+ const [period, setPeriod] = useState(projectViewPrefs[id]?.period || tbPeriodPairs[1].period)+ const [timeBucket, setTimebucket] = useState(projectViewPrefs[id]?.timeBucket || tbPeriodPairs[1].tbs[1])const activePeriod = useMemo(() => _find(tbPeriodPairs, p => p.period === period), [period])- const [showTotal, setShowTotal] = useState(false) // todo: put a string here with other metrics like loadTime, bounce rate, etc.+ const [showTotal, setShowTotal] = useState(false)const [chartData, setChartData] = useState({})const [mainChart, setMainChart] = useState(null)@@ -104,7 +106,15 @@ const ViewProject = ({const loadAnalytics = async () => {if (!isLoading && !_isEmpty(project)) {try {- const data = await getProjectData(id, timeBucket, period)+ let data+ const key = getProjectCacheKey(period, timeBucket)++ if (!_isEmpty(cache[id]) && !_isEmpty(cache[id][key])) {+ data = cache[id][key]+ } else {+ data = await getProjectData(id, timeBucket, period)+ setProjectCache(id, period, timeBucket, data || {})+ }if (_isEmpty(data)) {setAnalyticsLoading(false)@@ -148,13 +158,21 @@ const ViewProject = ({const updatePeriod = (newPeriod) => {const newPeriodFull = _find(tbPeriodPairs, (el) => el.period === newPeriod)+ let tb = timeBucketif (_isEmpty(newPeriodFull)) returnif (!_includes(newPeriodFull.tbs, timeBucket)) {- setTimebucket(_last(newPeriodFull.tbs))+ tb = _last(newPeriodFull.tbs)+ setTimebucket(tb)}setPeriod(newPeriod)+ setProjectViewPrefs(id, newPeriod, tb)+ }++ const updateTimebucket = (newTimebucket) => {+ setTimebucket(newTimebucket)+ setProjectViewPrefs(id, period, newTimebucket)}if (!isLoading && _isEmpty(project)) {@@ -179,7 +197,7 @@ const ViewProject = ({<buttonkey={tb}type='button'- onClick={() => setTimebucket(tb)}+ onClick={() => updateTimebucket(tb)}className={cx('relative capitalize inline-flex items-center px-4 py-2 border border-gray-300 bg-white text-sm font-medium text-gray-700 hover:bg-gray-50 focus:z-10 focus:outline-none focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500', {'-ml-px': index > 0,'rounded-l-md': index === 0,@@ -239,7 +257,11 @@ const ViewProject = ({ViewProject.propTypes = {projects: PropTypes.arrayOf(PropTypes.object).isRequired,+ cache: PropTypes.objectOf(PropTypes.object).isRequired,+ projectViewPrefs: PropTypes.objectOf(PropTypes.object).isRequired,showError: PropTypes.func.isRequired,+ setProjectCache: PropTypes.func.isRequired,+ setProjectViewPrefs: PropTypes.func.isRequired,isLoading: PropTypes.bool.isRequired,}diff --git a/src/redux/actions/ui/index.js b/src/redux/actions/ui/index.jsindex 091efd7c7..2bd0fc164 100644--- a/src/redux/actions/ui/index.js+++ b/src/redux/actions/ui/index.js@@ -8,7 +8,7 @@ const setProjects = (projects) => ({type: types.SET_PROJECTS,payload: {projects,- }+ },})const removeProject = (pid) => ({@@ -22,14 +22,35 @@ const setProjectsError = (error) => ({type: types.SET_PROJECTS_ERROR,payload: {error,- }+ },})const setProjectsLoading = (isLoading) => ({type: types.SET_PROJECTS_LOADING,payload: {isLoading,- }+ },+})++const setProjectCache = (pid, period, timeBucket, data) => ({+ type: types.SET_PROJECT_CACHE,+ payload: {+ pid, period, timeBucket, data,+ },+})++const deleteProjectCache = (pid, period, timeBucket) => ({+ type: types.DELETE_PROJECT_CACHE,+ payload: {+ pid, period, timeBucket,+ },+})++const setProjectViewPrefs = (pid, period, timeBucket) => ({+ type: types.SET_PROJECT_VIEW_PREFS,+ payload: {+ pid, period, timeBucket,+ },})const UIActions = {@@ -38,6 +59,9 @@ const UIActions = {setProjectsError,setProjectsLoading,removeProject,+ setProjectCache,+ deleteProjectCache,+ setProjectViewPrefs,}export default UIActionsdiff --git a/src/redux/actions/ui/types.js b/src/redux/actions/ui/types.jsindex 610e807cb..45930a8df 100644--- a/src/redux/actions/ui/types.js+++ b/src/redux/actions/ui/types.js@@ -1,10 +1,10 @@export const types = {- // SyncLOAD_PROJECTS: 'LOAD_PROJECTS',SET_PROJECTS_ERROR: 'SET_PROJECTS_ERROR',SET_PROJECTS_LOADING: 'SET_PROJECTS_LOADING',REMOVE_PROJECT: 'REMOVE_PROJECT',-- // Async+ SET_PROJECT_CACHE: 'SET_PROJECT_CACHE',+ DELETE_PROJECT_CACHE: 'DELETE_PROJECT_CACHE',+ SET_PROJECT_VIEW_PREFS: 'SET_PROJECT_VIEW_PREFS',SET_PROJECTS: 'SET_PROJECTS',}diff --git a/src/redux/constants/index.js b/src/redux/constants/index.jsindex 05955a825..86c8eebe8 100644--- a/src/redux/constants/index.js+++ b/src/redux/constants/index.js@@ -40,3 +40,6 @@ export const tbsFormatMapper = {export const TOKEN = 'access_token'export const TITLE_SUFFIX = process.env.REACT_APP_TITLE_SUFFIX++// Functions+export const getProjectCacheKey = (period, timeBucket) => `${period}${timeBucket}`diff --git a/src/redux/reducers/ui/cache.js b/src/redux/reducers/ui/cache.jsnew file mode 100644index 000000000..dfaf6595e--- /dev/null+++ b/src/redux/reducers/ui/cache.js@@ -0,0 +1,75 @@+import { types } from 'redux/actions/ui/types'+import _filter from 'lodash/filter'+import _isEmpty from 'lodash/isEmpty'++import { getProjectCacheKey } from 'redux/constants'++const getInitialState = () => {+ return {+ // { pid: { 'period + timeBucket': { ... }, ... }, ... }+ // example: { 'FSMaC9V4HFLA': { '4wday': { }, '7dhour': { } } }+ analytics: {},++ // { pid: { period: '7d', timeBucket: 'day' }, ... }+ projectViewPrefs: {},+ }+}++const cacheReducer = (state = getInitialState(), { type, payload }) => {+ switch (type) {+ case types.SET_PROJECT_CACHE: {+ const { pid, period, timeBucket, data } = payload+ const key = getProjectCacheKey(period, timeBucket)++ return {+ ...state,+ analytics: {+ ...state.analytics,+ [pid]: {+ ...state.analytics[pid],+ [key]: data,+ },+ },+ }+ }++ case types.DELETE_PROJECT_CACHE: {+ const { pid, period, timeBucket } = payload+ const key = getProjectCacheKey(period, timeBucket)++ if (_isEmpty(period) || _isEmpty(timeBucket)) {+ return {+ ...state,+ analytics: _filter(state.analytics, (project) => project !== pid),+ }+ }++ return {+ ...state,+ analytics: {+ ...state.analytics,+ [pid]: _filter(state.analytics[pid], (ckey) => ckey !== key),+ },+ }+ }++ case types.SET_PROJECT_VIEW_PREFS: {+ const { pid, period, timeBucket } = payload++ return {+ ...state,+ projectViewPrefs: {+ ...state.projectViewPrefs,+ [pid]: {+ period, timeBucket,+ },+ },+ }+ }++ default:+ return state+ }+}++export default cacheReducerdiff --git a/src/redux/reducers/ui/index.js b/src/redux/reducers/ui/index.jsindex e6c4a9794..7f9bbedd1 100644--- a/src/redux/reducers/ui/index.js+++ b/src/redux/reducers/ui/index.js@@ -1,4 +1,5 @@import { combineReducers } from 'redux'import projects from './projects'+import cache from './cache'-export default combineReducers({ projects })+export default combineReducers({ projects, cache })