Skip to content

Commit

Permalink
feat: add analytics (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonreshetov committed Apr 9, 2022
1 parent 7762315 commit d73856c
Show file tree
Hide file tree
Showing 21 changed files with 239 additions and 188 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@iconscout/unicons": "^4.0.1",
"@masscode/json-server": "^0.18.0",
"@sipec/vue3-tags-input": "^3.0.4",
"@types/universal-analytics": "^0.4.5",
"@vueuse/core": "^8.1.2",
"ace-builds": "^1.4.14",
"electron-store": "^8.0.1",
Expand All @@ -41,6 +42,7 @@
"mitt": "^3.0.0",
"nanoid": "^3.3.1",
"pinia": "^2.0.12",
"universal-analytics": "^0.5.3",
"vue": "^3.2.26",
"vue-router": "^4.0.12",
"vue3-perfect-scrollbar": "^1.6.0"
Expand Down
14 changes: 14 additions & 0 deletions src/main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { contextBridge, ipcRenderer } from 'electron'
import { isDbExist, migrate, move } from './services/db'
import { store } from './store'
import type { ElectronBridge } from '@shared/types/main'
import { version } from '../../package.json'
import type { TrackEvents } from '@shared/types/main/analytics'
import { analytics } from './services/analytics'

const isDev = process.env.NODE_ENV === 'development'

contextBridge.exposeInMainWorld('electron', {
ipc: {
Expand All @@ -25,5 +30,14 @@ contextBridge.exposeInMainWorld('electron', {
migrate: path => migrate(path),
move: (from, to) => move(from, to),
isExist: path => isDbExist(path)
},
track: (event: TrackEvents, payload?: string) => {
if (isDev) return

const path = payload
? `${version}/${event}/${payload}`
: `${version}/${event}`

analytics.pageview(path).send()
}
} as ElectronBridge)
4 changes: 4 additions & 0 deletions src/main/services/analytics/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import ua from 'universal-analytics'
const analytics = ua('UA-56182454-13')

export { analytics }
2 changes: 2 additions & 0 deletions src/renderer/components/editor/TheEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import type { Language } from '@shared/types/renderer/editor'
import { languages } from './languages'
import { useAppStore } from '@/store/app'
import { useSnippetStore } from '@/store/snippets'
import { track } from '@/electron'
interface Props {
lang: Language
Expand Down Expand Up @@ -143,6 +144,7 @@ const setValue = () => {
const setLang = () => {
editor.session.setMode(`ace/mode/${localLang.value}`)
track('snippets/set-language', localLang.value)
}
const setTheme = () => {
Expand Down
5 changes: 4 additions & 1 deletion src/renderer/components/preferences/Storage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
</template>

<script setup lang="ts">
import { ipc, store, db } from '@/electron'
import { ipc, store, db, track } from '@/electron'
import { useFolderStore } from '@/store/folders'
import { useSnippetStore } from '@/store/snippets'
import type { MessageBoxRequest } from '@shared/types/main'
Expand All @@ -53,6 +53,7 @@ const onClickMove = async () => {
await db.move(storagePath.value, path)
console.log('aas')
setStorageAndRestartApi(path)
track('app/move-storage')
} catch (err) {
const e = err as Error
ipc.invoke('main:notification', {
Expand All @@ -69,6 +70,7 @@ const onClickOpen = async () => {
if (isExist) {
setStorageAndRestartApi(path, true)
snippetStore.getSnippets()
track('app/open-storage')
} else {
const message = 'Folder not contain "db.json".'
ipc.invoke('main:notification', {
Expand Down Expand Up @@ -99,6 +101,7 @@ const onClickMigrate = async () => {
body: 'DB successfully migrated.'
})
snippetStore.getSnippets()
track('app/migrate')
} catch (err) {
const e = err as Error
ipc.invoke('main:notification', {
Expand Down
10 changes: 6 additions & 4 deletions src/renderer/components/sidebar/SidebarListItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { ref } from 'vue'
import Folder from '~icons/unicons/folder'
import AngleRight from '~icons/unicons/angle-right'
import { onClickOutside } from '@vueuse/core'
import { ipc } from '@/electron'
import { ipc, track } from '@/electron'
import type {
ContextMenuPayload,
ContextMenuResponse,
Expand Down Expand Up @@ -80,16 +80,17 @@ const onClickContextMenu = async () => {
if (action === 'new') {
await folderStore.addNewFolder()
track('folders/add-new')
}
if (action === 'delete') {
await folderStore.deleteFoldersById(props.id!)
console.log(action, type)
track('folders/delete')
}
if (action === 'update:language') {
console.log(data)
await folderStore.patchFoldersById(props.id!, { defaultLanguage: data })
track('folders/set-language', data)
}
}
Expand All @@ -104,9 +105,9 @@ const onClickContextMenu = async () => {
})
if (action === 'delete') {
console.log('trash delete')
snippetStore.emptyTrash()
await snippetStore.getSnippets()
track('app/empty-trash')
if (folderStore.selectedAlias === 'trash') {
snippetStore.setSnippetsByAlias('trash')
Expand All @@ -126,6 +127,7 @@ const onClickContextMenu = async () => {
if (action === 'delete') {
await tagStore.deleteTagById(props.id!)
track('tags/delete')
if (props.id === tagStore.selectedId) {
tagStore.selectedId = undefined
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/components/sidebar/TheSidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ import Trash from '~icons/unicons/trash'
import LabelAlt from '~icons/unicons/label-alt'
import { useFolderStore } from '@/store/folders'
import { useSnippetStore } from '@/store/snippets'
import { ipc } from '@/electron'
import { ipc, track } from '@/electron'
import { useTagStore } from '@/store/tags'
import { emitter } from '@/composable'
Expand Down Expand Up @@ -153,6 +153,7 @@ const contextMenuHandler = () => {
const onAddNewFolder = async () => {
await folderStore.addNewFolder()
track('folders/add-new')
}
const onUpdate = async () => {
Expand Down
4 changes: 3 additions & 1 deletion src/renderer/components/snippets/SnippetHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

<script setup lang="ts">
import { emitter } from '@/composable'
import { ipc } from '@/electron'
import { ipc, track } from '@/electron'
import { useSnippetStore } from '@/store/snippets'
import { useClipboard, useDebounceFn } from '@vueuse/core'
import { computed, ref } from 'vue'
Expand All @@ -53,6 +53,7 @@ const name = computed({
const onAddNewFragment = () => {
snippetStore.addNewFragmentToSnippetsById(snippetStore.selectedId!)
snippetStore.fragment = snippetStore.fragmentCount!
track('snippets/add-fragment')
}
const onCopySnippet = () => {
Expand All @@ -61,6 +62,7 @@ const onCopySnippet = () => {
ipc.invoke<any, NotificationPayload>('main:notification', {
body: 'Snippet copied'
})
track('snippets/copy')
}
emitter.on('focus:snippet-name', () => {
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/components/snippets/SnippetListHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { useFolderStore } from '@/store/folders'
import { useSnippetStore } from '@/store/snippets'
import { useDebounceFn } from '@vueuse/core'
import { computed } from 'vue'
import { track } from '@/electron'
const snippetStore = useSnippetStore()
const folderStore = useFolderStore()
Expand All @@ -38,6 +39,7 @@ const query = computed({
snippetStore.searchQuery = v
snippetStore.setSnippetsByAlias('all')
snippetStore.search(v!)
track('snippets/search')
}, 300)
})
Expand All @@ -50,6 +52,7 @@ const onAddNewSnippet = async () => {
await snippetStore.getSnippets()
emitter.emit('focus:snippet-name', true)
track('snippets/add-new')
}
const onReset = () => {
Expand Down
14 changes: 12 additions & 2 deletions src/renderer/components/snippets/SnippetListItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</template>

<script setup lang="ts">
import { ipc } from '@/electron'
import { ipc, track } from '@/electron'
import { useFolderStore } from '@/store/folders'
import { useSnippetStore } from '@/store/snippets'
import type {
Expand Down Expand Up @@ -146,7 +146,10 @@ const onClickContextMenu = async () => {
}
if (action === 'delete') {
if (type === 'folder') await moveToTrash()
if (type === 'folder') {
await moveToTrash()
track('snippets/move-to-trash')
}
if (type === 'favorites' || type === 'all' || type === 'inbox') {
await moveToTrash(type)
Expand All @@ -165,6 +168,7 @@ const onClickContextMenu = async () => {
if (action === 'duplicate') {
await snippetStore.duplicateSnippetById(props.id)
track('snippets/duplicate')
if (type === 'folder') {
await snippetStore.getSnippetsByFolderIds(folderStore.selectedIds!)
Expand All @@ -181,6 +185,12 @@ const onClickContextMenu = async () => {
isFavorites: data
})
if (data) {
track('snippets/add-to-favorites')
} else {
track('snippets/delete-from-favorites')
}
if (type === 'folder') {
await snippetStore.getSnippetsByFolderIds(folderStore.selectedIds!)
}
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/components/snippets/SnippetsFragmentsInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import { computed, nextTick, ref, watch } from 'vue'
import { useSnippetStore } from '@/store/snippets'
import { onClickOutside, useDebounceFn } from '@vueuse/core'
import { ipc } from '@/electron'
import { ipc, track } from '@/electron'
import type {
ContextMenuPayload,
ContextMenuResponse
Expand Down Expand Up @@ -74,6 +74,7 @@ const onClickContext = async () => {
if (action === 'delete') {
await snippetStore.deleteCurrentSnippetFragmentByIndex(props.index)
snippetStore.fragment = snippetStore.fragmentCount! - 1
track('snippets/delete-fragment')
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/components/snippets/SnippetsTags.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</template>

<script setup lang="ts">
import { track } from '@/electron'
import { useAppStore } from '@/store/app'
import { useSnippetStore } from '@/store/snippets'
import { useTagStore } from '@/store/tags'
Expand All @@ -24,6 +25,7 @@ const tags = computed({
if (tag) {
tagsIds.push(tag.id)
track('snippets/add-tag')
} else {
if (!i.text) return
Expand All @@ -37,6 +39,8 @@ const tags = computed({
} else {
snippetStore.selected!.tags = [newTag]
}
track('tags/add-new')
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/electron.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const { ipc, store, db } = window.electron
const { ipc, store, db, track } = window.electron

export { ipc, store, db }
export { ipc, store, db, track }
2 changes: 1 addition & 1 deletion src/renderer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite App</title>
<title>massCode</title>
</head>
<body>
<div id="app"></div>
Expand Down
4 changes: 3 additions & 1 deletion src/renderer/store/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { State } from '@shared/types/renderer/store/app'
import { defineStore } from 'pinia'
import { version } from '../../../package.json'

export const useAppStore = defineStore('app', {
state: (): State => ({
Expand All @@ -14,6 +15,7 @@ export const useAppStore = defineStore('app', {
tagsHeight: 40,
footerHeight: 30
}
}
},
version
})
})
3 changes: 2 additions & 1 deletion src/renderer/views/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</template>

<script setup lang="ts">
import { store } from '@/electron'
import { store, track } from '@/electron'
import { useFolderStore } from '@/store/folders'
import { useSnippetStore } from '@/store/snippets'
import { useTagStore } from '@/store/tags'
Expand Down Expand Up @@ -53,6 +53,7 @@ const addDevtoolsHost = () => {
addDevtoolsHost()
init()
track('main')
</script>

<style scoped lang="scss">
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/views/Preferences.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
</template>

<script setup lang="ts">
import { track } from '@/electron'
import router from '@/router'
import { ref } from 'vue'
Expand All @@ -28,6 +29,8 @@ const activeMenu = ref('storage')
const toHome = () => {
router.push('/')
}
track('preferences')
</script>

<style lang="scss" scoped>
Expand Down
36 changes: 36 additions & 0 deletions src/shared/types/main/analytics.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
type CombineWith<T extends string, U extends string> = `${U}/${T}`

type SnippetEvents =
| 'add-fragment'
| 'add-new'
| 'add-tag'
| 'add-to-favorites'
| 'copy'
| 'delete-fragment'
| 'delete-from-favorites'
| 'delete'
| 'duplicate'
| 'move-to-trash'
| 'set-language'
| 'search'
type FolderEvents = 'add-new' | 'delete' | 'set-language'
type TagEvents = 'add-new' | 'delete'
type AppEvents =
| 'move-storage'
| 'open-storage'
| 'migrate'
| 'update'
| 'empty-trash'

type TrackSnippetEvents = CombineWith<SnippetEvents, 'snippets'>
type TrackFolderEvents = CombineWith<FolderEvents, 'folders'>
type TrackTagEvents = CombineWith<TagEvents, 'tags'>
type TrackAppEvents = CombineWith<AppEvents, 'app'>

export type TrackEvents =
| TrackSnippetEvents
| TrackFolderEvents
| TrackTagEvents
| TrackAppEvents
| 'main'
| 'preferences'

0 comments on commit d73856c

Please sign in to comment.