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!

integrated sdk with the marketplace

Blue Mouse54ab06563c34
diff --git a/.env.example b/.env.example
index aad8e1f1d..ffa30e09b 100644
--- a/.env.example
+++ b/.env.example
@@ -1,4 +1,5 @@
REACT_APP_API_URL=https://api.swetrix.com/
+REACT_APP_CDN_URL=https://cdn.swetrix.com/
REACT_APP_BLOG_URL=https://blog.swetrix.com/
CHOKIDAR_USEPOLLING=true
GENERATE_SOURCEMAP=false
diff --git a/public/assets/export_as_json.js b/public/assets/export_as_json.js
deleted file mode 100644
index b7a787bc1..000000000
--- a/public/assets/export_as_json.js
+++ /dev/null
@@ -1,36 +0,0 @@
-(async (sdk) => {
- let data = {}
- let pid = null
-
- sdk.addExportDataRow('As JSON', () => {
- const _data = {
- ...data,
- exportDate: new Date().toString(),
- exportDateISO: new Date().toISOString(),
- }
-
- const blob = new Blob([JSON.stringify(_data, null, 2)], { type: 'application/json' })
- const url = URL.createObjectURL(blob)
- const a = document.createElement('a')
- a.href = url
- // swetrix-YYYY-MM-DDTHH-mm-ss.json
- a.download = `swetrix-export-${new Date().toISOString().replace(/:/g, '-').replace(/\..+/, '')}.json`
- a.click()
- })
-
- sdk.addEventListener('projectinfo', ({ id }) => {
- pid = id
- })
-
- sdk.addEventListener('load', ({ period, timeBucket, from, to, params, filters, customs }) => {
- data = {
- period,
- timeBucket,
- from,
- to,
- data: params,
- filters,
- customs,
- }
- })
-})
diff --git a/public/assets/test_extension.js b/public/assets/test_extension.js
deleted file mode 100644
index 26f1a2d6b..000000000
--- a/public/assets/test_extension.js
+++ /dev/null
@@ -1,26 +0,0 @@
-(async (sdk) => {
- // console.log('test_extension.js', sdk)
- // console.log('--------------------')
-
- // sdk.addExportDataRow('As blahblahblah', () => {
- // console.log('blahblahblah')
- // })
-
- // sdk.addExportDataRow('As test', () => {
- // console.log('test')
- // })
-
- // sdk.addPanelTab('pg', 'test', () => {})
-
- // sdk.addEventListener('timeupdate', (data) => {
- // console.log('timeupdate', data)
- // })
-
- // sdk.addEventListener('filtersupdate', (data) => {
- // console.log('filtersupdate', data)
- // })
-
- // sdk.addEventListener('load', (data) => {
- // console.log('load', data)
- // })
-})
diff --git a/src/api/index.js b/src/api/index.js
index 3f115bb03..67f20aebb 100644
--- a/src/api/index.js
+++ b/src/api/index.js
@@ -413,3 +413,14 @@ export const deleteApiKey = () =>
? error.response.data
: error.response.data.message
})
+
+export const getInstalledExtensions = (limit = 100, offset = 0) =>
+ api
+ .get(`/extensions/installed?limit=${limit}&offset=${offset}`)
+ .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/Project/View/ViewProject.container.jsx b/src/pages/Project/View/ViewProject.container.jsx
index 78df864d3..125afb072 100644
--- a/src/pages/Project/View/ViewProject.container.jsx
+++ b/src/pages/Project/View/ViewProject.container.jsx
@@ -16,6 +16,7 @@ const mapStateToProps = (state) => {
authenticated: state.auth.authenticated,
timezone: state.auth.user.timezone,
isPaidTierUsed: state.auth.isPaidTierUsed,
+ extensions: state.ui.misc.extensions,
user: state.auth.user,
}
}
@@ -29,6 +30,7 @@ const mapStateToProps = (state) => {
authenticated: state.auth.authenticated,
timezone: state.auth.user.timezone,
isPaidTierUsed: state.auth.isPaidTierUsed,
+ extensions: state.ui.misc.extensions,
user: state.auth.user,
}
}
diff --git a/src/pages/Project/View/ViewProject.jsx b/src/pages/Project/View/ViewProject.jsx
index 9543119a7..e0565a51e 100644
--- a/src/pages/Project/View/ViewProject.jsx
+++ b/src/pages/Project/View/ViewProject.jsx
@@ -29,7 +29,7 @@ import { SWETRIX_PID } from 'utils/analytics'
import Title from 'components/Title'
import EventsRunningOutBanner from 'components/EventsRunningOutBanner'
import {
- tbPeriodPairs, getProjectCacheKey, LIVE_VISITORS_UPDATE_INTERVAL, DEFAULT_TIMEZONE,
+ tbPeriodPairs, getProjectCacheKey, LIVE_VISITORS_UPDATE_INTERVAL, DEFAULT_TIMEZONE, CDN_URL, isDevelopment,
timeBucketToDays, getProjectCacheCustomKey, roleViewer, MAX_MONTHS_IN_PAST, MAX_MONTHS_IN_PAST_FREE,
} from 'redux/constants'
import Button from 'ui/Button'
@@ -57,7 +57,7 @@ import './styles.css'
const ViewProject = ({
projects, isLoading: _isLoading, showError, cache, setProjectCache, projectViewPrefs, setProjectViewPrefs, setPublicProject,
- setLiveStatsForProject, authenticated, timezone, user, sharedProjects, isPaidTierUsed,
+ setLiveStatsForProject, authenticated, timezone, user, sharedProjects, isPaidTierUsed, extensions,
}) => {
const { t, i18n: { language } } = useTranslation('common')
const [periodPairs, setPeriodPairs] = useState(tbPeriodPairs(t))
@@ -313,14 +313,20 @@ const ViewProject = ({
// Initialising Swetrix SDK instance
useEffect(() => {
- const sdk = new SwetrixSDK([{
- cdnURL: 'http://localhost:3000/assets/test_extension.js',
- id: 'test-extension',
- }, {
- cdnURL: 'http://localhost:3000/assets/export_as_json.js',
- id: 'export-as-json',
- }], {
- debug: true,
+ if (_isEmpty(extensions)) {
+ return null
+ }
+
+ const processedExtensions = _map(extensions, (ext) => {
+ const { id: extId, fileURL } = ext
+ return {
+ id: extId,
+ cdnURL: `${CDN_URL}file/${fileURL}`,
+ }
+ })
+
+ const sdk = new SwetrixSDK(processedExtensions, {
+ debug: isDevelopment,
}, {
onAddExportDataRow: (label, onClick) => {
setCustomExportTypes((prev) => [
@@ -354,7 +360,7 @@ const ViewProject = ({
return () => {
sdk._destroy()
}
- }, [])
+ }, [extensions])
// Supplying 'timeupdate' event to the SDK after loading
useEffect(() => {
@@ -979,6 +985,7 @@ ViewProject.propTypes = {
setPublicProject: PropTypes.func.isRequired,
setLiveStatsForProject: PropTypes.func.isRequired,
authenticated: PropTypes.bool.isRequired,
+ extensions: PropTypes.arrayOf(PropTypes.object).isRequired,
isPaidTierUsed: PropTypes.bool.isRequired,
timezone: PropTypes.string,
}
diff --git a/src/redux/actions/ui/index.js b/src/redux/actions/ui/index.js
index 1ed6a2029..b3d81c254 100644
--- a/src/redux/actions/ui/index.js
+++ b/src/redux/actions/ui/index.js
@@ -18,6 +18,17 @@ const setProjects = (projects, shared) => ({
},
})
+const setExtensions = (extensions) => ({
+ type: types.SET_EXTENSIONS,
+ payload: {
+ extensions,
+ },
+})
+
+const loadExtensions = () => ({
+ type: types.LOAD_EXTENSIONS,
+})
+
const setTotalMonthlyEvents = (totalMonthlyEvents) => ({
type: types.SET_TOTAL_MONTHLY_EVENTS,
payload: {
@@ -190,6 +201,8 @@ const UIActions = {
setProjectsShareData,
shareVerifyAsync,
setDashboardTabs,
+ setExtensions,
+ loadExtensions,
setLastBlogPost,
}
diff --git a/src/redux/actions/ui/types.js b/src/redux/actions/ui/types.js
index b38bcad45..71911958e 100644
--- a/src/redux/actions/ui/types.js
+++ b/src/redux/actions/ui/types.js
@@ -7,6 +7,8 @@ export const types = {
DELETE_PROJECT_CACHE: 'UI_DELETE_PROJECT_CACHE',
SET_PROJECT_VIEW_PREFS: 'UI_SET_PROJECT_VIEW_PREFS',
SET_PROJECTS: 'UI_SET_PROJECTS',
+ SET_EXTENSIONS: 'UI_SET_EXTENSIONS',
+ LOAD_EXTENSIONS: 'UI_LOAD_EXTENSIONS',
SET_LIVE_STATS: 'UI_SET_LIVE_STATS',
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 1459c588a..7af2e9c8f 100644
--- a/src/redux/constants/index.js
+++ b/src/redux/constants/index.js
@@ -108,6 +108,9 @@ export const TWITTER_URL = 'https://twitter.com/swetrix'
export const STATUSPAGE_URL = 'https://stats.uptimerobot.com/33rvmiXXEz'
export const BLOG_URL = 'https://blog.swetrix.com'
export const UTM_GENERATOR_URL = 'https://url.swetrix.com'
+export const CDN_URL = process.env.REACT_APP_CDN_URL
+
+export const isDevelopment = !process.env.NODE_ENV || process.env.NODE_ENV === 'development'
export const SUPPORTED_THEMES = ['light', 'dark']
diff --git a/src/redux/reducers/ui/misc.js b/src/redux/reducers/ui/misc.js
index 2fea823fb..9bb1388f7 100644
--- a/src/redux/reducers/ui/misc.js
+++ b/src/redux/reducers/ui/misc.js
@@ -14,6 +14,7 @@ const initialState = {
},
showNoEventsLeftBanner: false,
lastBlogPost: {},
+ extensions: [],
}
// eslint-disable-next-line default-param-last
@@ -54,6 +55,15 @@ const miscReducer = (state = initialState, { type, payload }) => {
}
}
+ case types.SET_EXTENSIONS: {
+ const { extensions } = payload
+
+ return {
+ ...state,
+ extensions,
+ }
+ }
+
case types.SET_LAST_BLOG_POST: {
const { lastBlogPost } = payload
diff --git a/src/redux/sagas/ui/index.js b/src/redux/sagas/ui/index.js
index d3ca255d5..6ab59339f 100644
--- a/src/redux/sagas/ui/index.js
+++ b/src/redux/sagas/ui/index.js
@@ -9,6 +9,7 @@ import generalStats from './worker_general_stats'
import setProjectViewPrefs from './set_project_view_prefs'
import isEventsAvailable from './worker_is_events_available'
import shareVerify from './share_verify'
+import loadExtensions from './load_extensions'
import logout from './logout'
import loadSharedProjects from './load_shared_projects'
@@ -20,6 +21,7 @@ function* mainUISaga() {
yield takeEvery(types.SET_PROJECT_VIEW_PREFS, setProjectViewPrefs)
yield takeEvery(types.LOAD_PROJECTS, loadProjects)
yield takeEvery(types.LOAD_SHARED_PROJECTS, loadSharedProjects)
+ yield takeEvery(types.LOAD_EXTENSIONS, loadExtensions)
yield takeEvery(authTypes.LOGOUT, logout)
yield takeLatest(types.SHARE_VERIFY_ASYNC, shareVerify)
}
diff --git a/src/redux/sagas/ui/initialise.js b/src/redux/sagas/ui/initialise.js
index 1c859b61b..2c2bb618d 100644
--- a/src/redux/sagas/ui/initialise.js
+++ b/src/redux/sagas/ui/initialise.js
@@ -14,6 +14,7 @@ export default function* initialise() {
if (token) {
yield put(UIActions.loadProjects())
yield put(UIActions.loadSharedProjects())
+ yield put(UIActions.loadExtensions())
}
const lastBlogPost = yield call(getLastPost)
diff --git a/src/redux/sagas/ui/load_extensions.js b/src/redux/sagas/ui/load_extensions.js
new file mode 100644
index 000000000..1d49b5672
--- /dev/null
+++ b/src/redux/sagas/ui/load_extensions.js
@@ -0,0 +1,23 @@
+import { put, call } from 'redux-saga/effects'
+import Debug from 'debug'
+
+import UIActions from 'redux/actions/ui'
+
+import {
+ getInstalledExtensions,
+} from '../../../api'
+
+const debug = Debug('swetrix:rx:s:load-extensions')
+
+export default function* loadExtensions() {
+ try {
+ const { extensions } = yield call(getInstalledExtensions)
+
+ yield put(UIActions.setExtensions(extensions))
+ } catch ({ message }) {
+ // if (_isString(message)) {
+ // yield put(UIActions.setProjectsError(message))
+ // }
+ debug('failed to load extensions: %s', message)
+ }
+}