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 export csv

kruzhambus05a0934a743c
diff --git a/package.json b/package.json
index 7f26c2a1d..56c822665 100644
--- a/package.json
+++ b/package.json
@@ -20,6 +20,7 @@
"i18next": "^21.8.2",
"i18next-browser-languagedetector": "^6.1.4",
"i18next-http-backend": "^1.4.0",
+ "jszip": "^3.9.1",
"lodash": "^4.17.21",
"nanoid": "^3.3.4",
"prismjs": "^1.28.0",
@@ -58,7 +59,10 @@
"eslint:recommended",
"plugin:react/recommended"
],
- "plugins": ["lodash", "react-hooks"],
+ "plugins": [
+ "lodash",
+ "react-hooks"
+ ],
"rules": {
"arrow-body-style": "off",
"arrow-parens": "off",
@@ -71,7 +75,10 @@
"jsx-a11y/label-has-for": "off",
"jsx-a11y/no-noninteractive-element-interactions": "off",
"jsx-a11y/no-static-element-interactions": "off",
- "jsx-quotes": ["error", "prefer-single"],
+ "jsx-quotes": [
+ "error",
+ "prefer-single"
+ ],
"linebreak-style": "off",
"lines-between-class-members": "off",
"lodash/prefer-lodash-method": "warn",
@@ -85,7 +92,15 @@
"no-underscore-dangle": "off",
"no-unused-vars": "warn",
"react/display-name": "warn",
- "react/jsx-filename-extension": ["warn", { "extensions": [".js", ".jsx"] }],
+ "react/jsx-filename-extension": [
+ "warn",
+ {
+ "extensions": [
+ ".js",
+ ".jsx"
+ ]
+ }
+ ],
"react/jsx-props-no-spreading": "off",
"react/prefer-stateless-function": "off",
"react/prop-types": "off",
@@ -95,7 +110,10 @@
"react/react-in-jsx-scope": "off",
"react-hooks/exhaustive-deps": "warn",
"react/function-component-definition": "off",
- "semi": ["error", "never"]
+ "semi": [
+ "error",
+ "never"
+ ]
},
"settings": {
"import/resolver": {
diff --git a/src/pages/Project/View/ViewProject.helper.jsx b/src/pages/Project/View/ViewProject.helper.jsx
new file mode 100644
index 000000000..c1ff0dd15
--- /dev/null
+++ b/src/pages/Project/View/ViewProject.helper.jsx
@@ -0,0 +1,99 @@
+import { saveAs } from 'file-saver'
+import _forEach from 'lodash/forEach'
+import _map from 'lodash/map'
+import _split from 'lodash/split'
+import _replace from 'lodash/replace'
+import _isEmpty from 'lodash/isEmpty'
+import _keys from 'lodash/keys'
+import _round from 'lodash/round'
+import JSZip from 'jszip'
+
+import countries from 'i18n-iso-countries'
+import countriesEn from 'i18n-iso-countries/langs/en.json'
+import countriesDe from 'i18n-iso-countries/langs/de.json'
+import countriesEl from 'i18n-iso-countries/langs/el.json'
+import countriesHi from 'i18n-iso-countries/langs/hi.json'
+import countriesUk from 'i18n-iso-countries/langs/uk.json'
+import countriesZh from 'i18n-iso-countries/langs/zh.json'
+import countriesRu from 'i18n-iso-countries/langs/ru.json'
+import countriesSv from 'i18n-iso-countries/langs/sv.json'
+
+countries.registerLocale(countriesEn)
+countries.registerLocale(countriesDe)
+countries.registerLocale(countriesEl)
+countries.registerLocale(countriesRu)
+countries.registerLocale(countriesHi)
+countries.registerLocale(countriesUk)
+countries.registerLocale(countriesZh)
+countries.registerLocale(countriesSv)
+
+const getExportFilename = (suffix) => {
+ // turn something like 2022-03-02T19:31:00.100Z into 2022-03-02
+ const date = _split(_replace(_split(new Date().toISOString(), '.')[0], /:/g, '-'), 'T')[0]
+ return `${suffix}-${date}.zip`
+}
+
+function ConvertToCSV(objArray) {
+ const array = typeof objArray !== 'object' ? JSON.parse(objArray) : objArray
+ let str = 'name,value,perc\r\n'
+
+ for (let i = 0; i < array.length; i++) {
+ let line = ''
+
+ _forEach(array[i], (index) => {
+ if (line !== '') line += ','
+ line += index
+ })
+
+ str += `${line}\r\n`
+ }
+
+ return str
+}
+
+const onCSVExportClick = (data, t) => {
+ const typeNameMapping = {
+ cc: t('project.mapping.cc'),
+ pg: t('project.mapping.pg'),
+ lc: t('project.mapping.lc'),
+ ref: t('project.mapping.ref'),
+ dv: t('project.mapping.dv'),
+ br: t('project.mapping.br'),
+ os: t('project.mapping.os'),
+ so: 'utm_source',
+ me: 'utm_medium',
+ ca: 'utm_campaign',
+ lt: t('project.mapping.lt'),
+ }
+
+ const zip = new JSZip()
+
+ _forEach(data.types, (items) => {
+ if (!_isEmpty(data.data[items])) {
+ let total = 0
+
+ _forEach(_keys(data.data[items]), (e) => {
+ total += data.data[items][e]
+ })
+
+ const csvData = _map(_keys(data.data[items]), (e) => {
+ const perc = _round(((data.data[items][e] / total) * 100) || 0, 2)
+
+ if (items === 'cc') {
+ const name = countries.getName(e, 'en')
+ return [name, data.data[items][e], `${perc}%`]
+ }
+
+ return [e, data.data[items][e], `${perc}%`]
+ })
+
+ zip.file(`${typeNameMapping[items]}.csv`, ConvertToCSV(csvData))
+ }
+ })
+
+ zip.generateAsync({ type: 'blob' }).then((content) => {
+ saveAs(content, `${getExportFilename('panels-date')}.zip`)
+ })
+}
+
+export default onCSVExportClick
diff --git a/src/pages/Project/View/ViewProject.jsx b/src/pages/Project/View/ViewProject.jsx
index 56ae65f02..03b885f91 100644
--- a/src/pages/Project/View/ViewProject.jsx
+++ b/src/pages/Project/View/ViewProject.jsx
@@ -1,6 +1,6 @@
/* eslint-disable react/forbid-prop-types, react/no-unstable-nested-components, react/display-name */
import React, {
- useState, useEffect, useMemo, memo, useRef,
+ useState, useEffect, useMemo, memo, useRef, Fragment,
} from 'react'
import { useHistory, useParams, Link } from 'react-router-dom'
import domToImage from 'dom-to-image'
@@ -17,7 +17,7 @@ import countriesZh from 'i18n-iso-countries/langs/zh.json'
import countriesRu from 'i18n-iso-countries/langs/ru.json'
import countriesSv from 'i18n-iso-countries/langs/sv.json'
import {
- GlobeIcon, TranslateIcon, DocumentTextIcon, DeviceMobileIcon, ArrowCircleRightIcon, SearchIcon, ServerIcon,
+ GlobeIcon, TranslateIcon, DocumentTextIcon, DeviceMobileIcon, ArrowCircleRightIcon, SearchIcon, ServerIcon, DownloadIcon,
} from '@heroicons/react/outline'
import cx from 'clsx'
import * as d3 from 'd3'
@@ -54,6 +54,7 @@ import {
Panel, Overview, CustomEvents,
} from './Panels'
import './styles.css'
+import onCSVExportClick from './ViewProject.helper'
countries.registerLocale(countriesEn)
countries.registerLocale(countriesDe)
@@ -595,6 +596,11 @@ const ViewProject = ({
}
}
+ const exportTypes = [
+ { label: 'As image', onClick: exportAsImageHandler },
+ { label: 'As CSV', onClick: () => onCSVExportClick(panelsData, t) },
+ ]
+
if (!isLoading) {
return (
<Title title={name}>
@@ -666,13 +672,16 @@ const ViewProject = ({
onChange={(e) => setShowTotal(e.target.checked)}
/>
<div className={cx('h-full ml-3', { hidden: isPanelsDataEmpty || analyticsLoading })}>
- <Button
- onClick={exportAsImageHandler}
- className='py-2.5 px-3 md:px-4 text-sm dark:text-gray-50 dark:border-gray-800 dark:bg-gray-700 dark:hover:bg-gray-600'
- secondary
- >
- {t('project.exportImg')}
- </Button>
+ <Dropdown
+ items={exportTypes}
+ title={[
+ <DownloadIcon key='download-icon' className='w-5 h-5 mr-2' />,
+ <Fragment key='export-data'>Export data</Fragment>,
+ ]}
+ labelExtractor={item => item.label}
+ keyExtractor={item => item.label}
+ onSelect={item => item.onClick(panelsData, t)}
+ />
</div>
{!isProjectPublic && (
<div className='h-full ml-3'>