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 public dashboards feature

Blue Mouse2baf51e6eeac
diff --git a/src/i18n/en.json b/src/i18n/en.json
index d8cb1f309..520650bc9 100644
--- a/src/i18n/en.json
+++ b/src/i18n/en.json
@@ -267,7 +267,9 @@
"origins": "Allowed origins",
"originsHint": "A list of origins (domains) which are allowed to use script with your ProjectID, separated by commas.\nLeave it empty to allow all origins (default setting).\nExample: cornell.edu, app.example.com, ssu.gov.ua",
"enabled": "Project enabled",
+ "public": "Dashboard is public",
"enabledHint": "Disabled projects will not count any newly incoming events or pageviews.\nYou will still be able to access the project's analytics in dashboard.",
+ "publicHint": "By checking this, anyone on the internet (including Google) would be able to see this project's Dashboard.\nOnly project's administrator can edit the settings.",
"createHint": "*you will be able to set up your project more thoroughly after you have created it",
"delete": "Delete project",
"qDelete": "Delete the project?",
diff --git a/src/index.js b/src/index.js
index cb81f06ed..e32b4f080 100644
--- a/src/index.js
+++ b/src/index.js
@@ -40,7 +40,7 @@ const options = {
}
if (process.env.NODE_ENV !== 'production') {
- localStorage.debug = 'analytics:*'
+ localStorage.debug = 'swetrix:*'
}
ReactDOM.render(
diff --git a/src/pages/Dashboard/Dashboard.jsx b/src/pages/Dashboard/Dashboard.jsx
index d23bfa1c8..1297c8f67 100644
--- a/src/pages/Dashboard/Dashboard.jsx
+++ b/src/pages/Dashboard/Dashboard.jsx
@@ -6,6 +6,7 @@ import PropTypes from 'prop-types'
import _isEmpty from 'lodash/isEmpty'
import _isNumber from 'lodash/isNumber'
import _map from 'lodash/map'
+import _filter from 'lodash/filter'
import { useTranslation } from 'react-i18next'
import { EyeIcon } from '@heroicons/react/outline'
import { CalendarIcon } from '@heroicons/react/outline'
@@ -163,7 +164,7 @@ const Dashboard = ({ projects, isLoading, error, user }) => {
) : (
<div className='bg-white shadow overflow-hidden sm:rounded-md mt-10'>
<ul className='divide-y divide-gray-200'>
- {_map(projects, ({ name, id, created, active, overall, live }) => (
+ {_map(_filter(projects, ({ uiHidden }) => !uiHidden), ({ name, id, created, active, overall, live }) => (
<ProjectCart
key={id}
t={t}
diff --git a/src/pages/Project/Settings/ProjectSettings.jsx b/src/pages/Project/Settings/ProjectSettings.jsx
index dc6ae2632..f2efe84a2 100644
--- a/src/pages/Project/Settings/ProjectSettings.jsx
+++ b/src/pages/Project/Settings/ProjectSettings.jsx
@@ -36,6 +36,7 @@ const ProjectSettings = ({
name: '',
id: id || nanoid(),
origins: '', // origins string, ',' is used as a separator. converted to an array on the backend side.
+ public: false,
})
const [validated, setValidated] = useState(false)
const [errors, setErrors] = useState({})
@@ -51,7 +52,7 @@ const ProjectSettings = ({
}
if (!isLoading && isSettings && !projectDeleting) {
- if (_isEmpty(project)) {
+ if (_isEmpty(project) || project?.uiHidden) {
showError(t('project.noExist'))
history.push(routes.dashboard)
} else {
@@ -213,6 +214,15 @@ const ProjectSettings = ({
label={t('project.settings.enabled')}
hint={t('project.settings.enabledHint')}
/>
+ <Checkbox
+ checked={Boolean(form.public)}
+ onChange={handleInput}
+ name='public'
+ id='public'
+ className='mt-4'
+ label={t('project.settings.public')}
+ hint={t('project.settings.publicHint')}
+ />
</>
) : (
<p className='text-gray-500 italic mt-2 text-sm'>
diff --git a/src/pages/Project/View/ViewProject.container.jsx b/src/pages/Project/View/ViewProject.container.jsx
index e6ca356ec..898109e05 100644
--- a/src/pages/Project/View/ViewProject.container.jsx
+++ b/src/pages/Project/View/ViewProject.container.jsx
@@ -8,7 +8,6 @@ const mapStateToProps = (state) => ({
isLoading: state.ui.projects.isLoading,
cache: state.ui.cache.analytics,
projectViewPrefs: state.ui.cache.projectViewPrefs,
- user: state.auth.user,
})
const mapDispatchToProps = (dispatch) => ({
@@ -21,9 +20,12 @@ const mapDispatchToProps = (dispatch) => ({
setProjectViewPrefs: (pid, period, timeBucket) => {
dispatch(UIActions.setProjectViewPrefs(pid, period, timeBucket))
},
- setProject: (project) => {
- dispatch(UIActions.setProject(project))
+ setPublicProject: (project) => {
+ dispatch(UIActions.setPublicProject(project))
},
+ setLiveStatsForProject: (id, count) => {
+ dispatch(UIActions.setLiveStatsForProject(id, count))
+ }
})
export default connect(mapStateToProps, mapDispatchToProps)(ViewProject)
diff --git a/src/pages/Project/View/ViewProject.jsx b/src/pages/Project/View/ViewProject.jsx
index 3f2e5f08d..246107a0d 100644
--- a/src/pages/Project/View/ViewProject.jsx
+++ b/src/pages/Project/View/ViewProject.jsx
@@ -21,10 +21,9 @@ import PropTypes from 'prop-types'
import Title from 'components/Title'
import {
- tbPeriodPairs, tbsFormatMapper, getProjectCacheKey,
+ tbPeriodPairs, tbsFormatMapper, getProjectCacheKey, LIVE_VISITORS_UPDATE_INTERVAL,
} from 'redux/constants'
import { isAuthenticated } from 'hoc/protected'
-import { isAdmin } from 'utils/validator'
import Button from 'ui/Button'
import Loader from 'ui/Loader'
import Dropdown from 'ui/Dropdown'
@@ -33,7 +32,9 @@ import {
Panel, Overview, CustomEvents,
} from './Panels'
import routes from 'routes'
-import { getProjectData, getProject } from 'api'
+import {
+ getProjectData, getProject, getOverallStats, getLiveVisitors,
+} from 'api'
countries.registerLocale(countriesEn)
@@ -142,7 +143,7 @@ const NoEvents = ({ t }) => (
)
const ViewProject = ({
- projects, isLoading, showError, cache, setProjectCache, projectViewPrefs, setProjectViewPrefs, user, setProject,
+ projects, isLoading, showError, cache, setProjectCache, projectViewPrefs, setProjectViewPrefs, setPublicProject, setLiveStatsForProject,
}) => {
const { t } = useTranslation('common')
const periodPairs = tbPeriodPairs(t)
@@ -151,6 +152,7 @@ const ViewProject = ({
const { id } = useParams()
const history = useHistory()
const project = useMemo(() => _find(projects, p => p.id === id) || {}, [projects, id])
+ const [isProjectPublic, setIsProjectPublic] = useState(false)
const [panelsData, setPanelsData] = useState({})
const [analyticsLoading, setAnalyticsLoading] = useState(true)
const [period, setPeriod] = useState(projectViewPrefs[id]?.period || periodPairs[1].period)
@@ -167,6 +169,11 @@ const ViewProject = ({
const { name } = project
+ const onErrorLoading = () => {
+ showError(t('project.noExist'))
+ history.push(routes.dashboard)
+ }
+
const loadAnalytics = async () => {
if (!isLoading && !_isEmpty(project)) {
try {
@@ -225,6 +232,50 @@ const ViewProject = ({
loadAnalytics()
}, [project, period, timeBucket]) // eslint-disable-line
+ useEffect(() => {
+ let interval
+ if (project.uiHidden) {
+ interval = setInterval(async () => {
+ const { id } = project
+ const result = await getLiveVisitors([id])
+
+ setLiveStatsForProject(id, result[id])
+ }, LIVE_VISITORS_UPDATE_INTERVAL)
+ }
+
+ return () => clearInterval(interval)
+ }, [project])
+
+ useEffect(() => {
+ if (!isLoading && _isEmpty(project)) {
+ getProject(id)
+ .then(projectRes => {
+ if (!_isEmpty(projectRes) && projectRes?.public) {
+ getOverallStats([id])
+ .then(res => {
+ setPublicProject({
+ ...projectRes,
+ overall: res[id],
+ live: 'N/A',
+ })
+ })
+ .catch(e => {
+ console.error(e)
+ onErrorLoading()
+ })
+
+ setIsProjectPublic(true)
+ } else {
+ onErrorLoading()
+ }
+ })
+ .catch(e => {
+ console.error(e)
+ onErrorLoading()
+ })
+ }
+ }, [isLoading, project])
+
const updatePeriod = (newPeriod) => {
const newPeriodFull = _find(periodPairs, (el) => el.period === newPeriod)
let tb = timeBucket
@@ -244,17 +295,6 @@ const ViewProject = ({
setProjectViewPrefs(id, period, newTimebucket)
}
- if (!isLoading && _isEmpty(project)) {
- if (isAdmin(user)) {
- getProject(id)
- .then(setProject)
- .catch(console.error)
- } else {
- showError(t('project.noExist'))
- history.push(routes.dashboard)
- }
- }
-
const openSettingsHandler = () => {
history.push(_replace(routes.project_settings, ':id', id))
}
@@ -307,11 +347,13 @@ const ViewProject = ({
keyExtractor={pair => pair.label}
onSelect={pair => updatePeriod(pair.period)}
/>
- <div className='h-full ml-3'>
- <Button onClick={openSettingsHandler} className='py-2.5 px-3 md:px-4 text-sm' secondary>
- {t('common.settings')}
- </Button>
- </div>
+ {!isProjectPublic && (
+ <div className='h-full ml-3'>
+ <Button onClick={openSettingsHandler} className='py-2.5 px-3 md:px-4 text-sm' secondary>
+ {t('common.settings')}
+ </Button>
+ </div>
+ )}
</div>
</div>
{isPanelsDataEmpty && (
@@ -410,7 +452,8 @@ ViewProject.propTypes = {
setProjectViewPrefs: PropTypes.func.isRequired,
isLoading: PropTypes.bool.isRequired,
user: PropTypes.object.isRequired,
- setProject: PropTypes.func.isRequired,
+ setPublicProject: PropTypes.func.isRequired,
+ setLiveStatsForProject: PropTypes.func.isRequired,
}
export default isAuthenticated(memo(ViewProject))
diff --git a/src/redux/actions/ui/index.js b/src/redux/actions/ui/index.js
index 352c0921a..9ffaf04f4 100644
--- a/src/redux/actions/ui/index.js
+++ b/src/redux/actions/ui/index.js
@@ -18,8 +18,15 @@ const setLiveStats = (data) => ({
},
})
-const setProject = (project) => ({
- type: types.SET_PROJECT,
+const setLiveStatsForProject = (id, count) => ({
+ type: types.SET_LIVE_STATS_PROJECT,
+ payload: {
+ id, count,
+ },
+})
+
+const setPublicProject = (project) => ({
+ type: types.SET_PUBLIC_PROJECT,
payload: {
project,
},
@@ -76,7 +83,8 @@ const UIActions = {
setProjectCache,
deleteProjectCache,
setProjectViewPrefs,
- setProject,
+ setPublicProject,
+ setLiveStatsForProject,
setLiveStats,
}
diff --git a/src/redux/actions/ui/types.js b/src/redux/actions/ui/types.js
index 9d12ba5e0..ff6b0fe6c 100644
--- a/src/redux/actions/ui/types.js
+++ b/src/redux/actions/ui/types.js
@@ -8,5 +8,6 @@ export const types = {
SET_PROJECT_VIEW_PREFS: 'UI_SET_PROJECT_VIEW_PREFS',
SET_PROJECTS: 'UI_SET_PROJECTS',
SET_LIVE_STATS: 'UI_SET_LIVE_STATS',
- SET_PROJECT: 'UI_SET_PROJECT',
+ SET_LIVE_STATS_PROJECT: 'UI_SET_LIVE_STATS_PROJECT',
+ SET_PUBLIC_PROJECT: 'UI_SET_PUBLIC_PROJECT',
}
diff --git a/src/redux/constants/index.js b/src/redux/constants/index.js
index f99ed9152..1f95c3180 100644
--- a/src/redux/constants/index.js
+++ b/src/redux/constants/index.js
@@ -45,6 +45,8 @@ export const TOKEN = 'access_token'
export const TITLE_SUFFIX = process.env.REACT_APP_TITLE_SUFFIX || '| Swetrix'
+export const LIVE_VISITORS_UPDATE_INTERVAL = 40000
+
// Functions
export const getProjectCacheKey = (period, timeBucket) => `${period}${timeBucket}`
diff --git a/src/redux/reducers/ui/projects.js b/src/redux/reducers/ui/projects.js
index 3c2b11aea..9cbb35962 100644
--- a/src/redux/reducers/ui/projects.js
+++ b/src/redux/reducers/ui/projects.js
@@ -34,11 +34,37 @@ const projectsReducer = (state = getInitialState(), { type, payload }) => {
}
}
- case types.SET_PROJECT: {
+ case types.SET_LIVE_STATS_PROJECT: {
+ const { id, count } = payload
+
+ return {
+ ...state,
+ projects: _map(state.projects, res => {
+ if (res.id === id) {
+ return {
+ ...res,
+ live: count,
+ }
+ } else {
+ return res
+ }
+ })
+ }
+ }
+
+ case types.SET_PUBLIC_PROJECT: {
const { project } = payload
return {
...state,
- projects: _findIndex(state.projects, (el) => el.id === project.id) >= 0 ? state.projects : [...state.projects, project],
+ projects: _findIndex(state.projects, (el) => el.id === project.id) >= 0
+ ? state.projects
+ : [
+ ...state.projects,
+ {
+ ...project,
+ uiHidden: true,
+ },
+ ],
}
}
diff --git a/src/redux/sagas/ui/worker_live_visitors.js b/src/redux/sagas/ui/worker_live_visitors.js
index 25ab4e8fd..1e21737eb 100644
--- a/src/redux/sagas/ui/worker_live_visitors.js
+++ b/src/redux/sagas/ui/worker_live_visitors.js
@@ -1,11 +1,11 @@
import { put, call, delay, select } from 'redux-saga/effects'
import _map from 'lodash/map'
+import _filter from 'lodash/filter'
import _isEmpty from 'lodash/isEmpty'
import { getLiveVisitors } from 'api'
import UIActions from 'redux/actions/ui'
-
-const LIVE_VISITORS_UPDATE_INTERVAL = 40000
+import { LIVE_VISITORS_UPDATE_INTERVAL } from 'redux/constants'
export default function* liveVisitors() {
while (true) {
@@ -15,7 +15,7 @@ export default function* liveVisitors() {
continue
}
- const pids = _map(projects, project => project.id)
+ const pids = _map(_filter(projects, ({ uiHidden }) => !uiHidden), project => project.id)
const liveStats = yield call(getLiveVisitors, pids)
yield put(UIActions.setLiveStats(liveStats))
diff --git a/src/utils/validator.js b/src/utils/validator.js
index 064461768..78514723a 100644
--- a/src/utils/validator.js
+++ b/src/utils/validator.js
@@ -1,9 +1,5 @@
-import _includes from 'lodash/includes'
-
export const MIN_PASSWORD_CHARS = 8
export const isValidEmail = text => text.match(/^\w+([.+-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,7})+$/)
export const isValidPassword = text => text.length >= MIN_PASSWORD_CHARS
-
-export const isAdmin = user => _includes(user.roles, 'admin')