Skip to content

Commit d73856c

Browse files
feat: add analytics (#12)
1 parent 7762315 commit d73856c

File tree

21 files changed

+239
-188
lines changed

21 files changed

+239
-188
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"@iconscout/unicons": "^4.0.1",
3434
"@masscode/json-server": "^0.18.0",
3535
"@sipec/vue3-tags-input": "^3.0.4",
36+
"@types/universal-analytics": "^0.4.5",
3637
"@vueuse/core": "^8.1.2",
3738
"ace-builds": "^1.4.14",
3839
"electron-store": "^8.0.1",
@@ -41,6 +42,7 @@
4142
"mitt": "^3.0.0",
4243
"nanoid": "^3.3.1",
4344
"pinia": "^2.0.12",
45+
"universal-analytics": "^0.5.3",
4446
"vue": "^3.2.26",
4547
"vue-router": "^4.0.12",
4648
"vue3-perfect-scrollbar": "^1.6.0"

src/main/preload.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import { contextBridge, ipcRenderer } from 'electron'
22
import { isDbExist, migrate, move } from './services/db'
33
import { store } from './store'
44
import type { ElectronBridge } from '@shared/types/main'
5+
import { version } from '../../package.json'
6+
import type { TrackEvents } from '@shared/types/main/analytics'
7+
import { analytics } from './services/analytics'
8+
9+
const isDev = process.env.NODE_ENV === 'development'
510

611
contextBridge.exposeInMainWorld('electron', {
712
ipc: {
@@ -25,5 +30,14 @@ contextBridge.exposeInMainWorld('electron', {
2530
migrate: path => migrate(path),
2631
move: (from, to) => move(from, to),
2732
isExist: path => isDbExist(path)
33+
},
34+
track: (event: TrackEvents, payload?: string) => {
35+
if (isDev) return
36+
37+
const path = payload
38+
? `${version}/${event}/${payload}`
39+
: `${version}/${event}`
40+
41+
analytics.pageview(path).send()
2842
}
2943
} as ElectronBridge)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import ua from 'universal-analytics'
2+
const analytics = ua('UA-56182454-13')
3+
4+
export { analytics }

src/renderer/components/editor/TheEditor.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import type { Language } from '@shared/types/renderer/editor'
3636
import { languages } from './languages'
3737
import { useAppStore } from '@/store/app'
3838
import { useSnippetStore } from '@/store/snippets'
39+
import { track } from '@/electron'
3940
4041
interface Props {
4142
lang: Language
@@ -143,6 +144,7 @@ const setValue = () => {
143144
144145
const setLang = () => {
145146
editor.session.setMode(`ace/mode/${localLang.value}`)
147+
track('snippets/set-language', localLang.value)
146148
}
147149
148150
const setTheme = () => {

src/renderer/components/preferences/Storage.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
</template>
3434

3535
<script setup lang="ts">
36-
import { ipc, store, db } from '@/electron'
36+
import { ipc, store, db, track } from '@/electron'
3737
import { useFolderStore } from '@/store/folders'
3838
import { useSnippetStore } from '@/store/snippets'
3939
import type { MessageBoxRequest } from '@shared/types/main'
@@ -53,6 +53,7 @@ const onClickMove = async () => {
5353
await db.move(storagePath.value, path)
5454
console.log('aas')
5555
setStorageAndRestartApi(path)
56+
track('app/move-storage')
5657
} catch (err) {
5758
const e = err as Error
5859
ipc.invoke('main:notification', {
@@ -69,6 +70,7 @@ const onClickOpen = async () => {
6970
if (isExist) {
7071
setStorageAndRestartApi(path, true)
7172
snippetStore.getSnippets()
73+
track('app/open-storage')
7274
} else {
7375
const message = 'Folder not contain "db.json".'
7476
ipc.invoke('main:notification', {
@@ -99,6 +101,7 @@ const onClickMigrate = async () => {
99101
body: 'DB successfully migrated.'
100102
})
101103
snippetStore.getSnippets()
104+
track('app/migrate')
102105
} catch (err) {
103106
const e = err as Error
104107
ipc.invoke('main:notification', {

src/renderer/components/sidebar/SidebarListItem.vue

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { ref } from 'vue'
3030
import Folder from '~icons/unicons/folder'
3131
import AngleRight from '~icons/unicons/angle-right'
3232
import { onClickOutside } from '@vueuse/core'
33-
import { ipc } from '@/electron'
33+
import { ipc, track } from '@/electron'
3434
import type {
3535
ContextMenuPayload,
3636
ContextMenuResponse,
@@ -80,16 +80,17 @@ const onClickContextMenu = async () => {
8080
8181
if (action === 'new') {
8282
await folderStore.addNewFolder()
83+
track('folders/add-new')
8384
}
8485
8586
if (action === 'delete') {
8687
await folderStore.deleteFoldersById(props.id!)
87-
console.log(action, type)
88+
track('folders/delete')
8889
}
8990
9091
if (action === 'update:language') {
91-
console.log(data)
9292
await folderStore.patchFoldersById(props.id!, { defaultLanguage: data })
93+
track('folders/set-language', data)
9394
}
9495
}
9596
@@ -104,9 +105,9 @@ const onClickContextMenu = async () => {
104105
})
105106
106107
if (action === 'delete') {
107-
console.log('trash delete')
108108
snippetStore.emptyTrash()
109109
await snippetStore.getSnippets()
110+
track('app/empty-trash')
110111
111112
if (folderStore.selectedAlias === 'trash') {
112113
snippetStore.setSnippetsByAlias('trash')
@@ -126,6 +127,7 @@ const onClickContextMenu = async () => {
126127
127128
if (action === 'delete') {
128129
await tagStore.deleteTagById(props.id!)
130+
track('tags/delete')
129131
130132
if (props.id === tagStore.selectedId) {
131133
tagStore.selectedId = undefined

src/renderer/components/sidebar/TheSidebar.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ import Trash from '~icons/unicons/trash'
9191
import LabelAlt from '~icons/unicons/label-alt'
9292
import { useFolderStore } from '@/store/folders'
9393
import { useSnippetStore } from '@/store/snippets'
94-
import { ipc } from '@/electron'
94+
import { ipc, track } from '@/electron'
9595
import { useTagStore } from '@/store/tags'
9696
import { emitter } from '@/composable'
9797
@@ -153,6 +153,7 @@ const contextMenuHandler = () => {
153153
154154
const onAddNewFolder = async () => {
155155
await folderStore.addNewFolder()
156+
track('folders/add-new')
156157
}
157158
158159
const onUpdate = async () => {

src/renderer/components/snippets/SnippetHeader.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
<script setup lang="ts">
2929
import { emitter } from '@/composable'
30-
import { ipc } from '@/electron'
30+
import { ipc, track } from '@/electron'
3131
import { useSnippetStore } from '@/store/snippets'
3232
import { useClipboard, useDebounceFn } from '@vueuse/core'
3333
import { computed, ref } from 'vue'
@@ -53,6 +53,7 @@ const name = computed({
5353
const onAddNewFragment = () => {
5454
snippetStore.addNewFragmentToSnippetsById(snippetStore.selectedId!)
5555
snippetStore.fragment = snippetStore.fragmentCount!
56+
track('snippets/add-fragment')
5657
}
5758
5859
const onCopySnippet = () => {
@@ -61,6 +62,7 @@ const onCopySnippet = () => {
6162
ipc.invoke<any, NotificationPayload>('main:notification', {
6263
body: 'Snippet copied'
6364
})
65+
track('snippets/copy')
6466
}
6567
6668
emitter.on('focus:snippet-name', () => {

src/renderer/components/snippets/SnippetListHeader.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { useFolderStore } from '@/store/folders'
2828
import { useSnippetStore } from '@/store/snippets'
2929
import { useDebounceFn } from '@vueuse/core'
3030
import { computed } from 'vue'
31+
import { track } from '@/electron'
3132
3233
const snippetStore = useSnippetStore()
3334
const folderStore = useFolderStore()
@@ -38,6 +39,7 @@ const query = computed({
3839
snippetStore.searchQuery = v
3940
snippetStore.setSnippetsByAlias('all')
4041
snippetStore.search(v!)
42+
track('snippets/search')
4143
}, 300)
4244
})
4345
@@ -50,6 +52,7 @@ const onAddNewSnippet = async () => {
5052
await snippetStore.getSnippets()
5153
5254
emitter.emit('focus:snippet-name', true)
55+
track('snippets/add-new')
5356
}
5457
5558
const onReset = () => {

src/renderer/components/snippets/SnippetListItem.vue

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
</template>
3333

3434
<script setup lang="ts">
35-
import { ipc } from '@/electron'
35+
import { ipc, track } from '@/electron'
3636
import { useFolderStore } from '@/store/folders'
3737
import { useSnippetStore } from '@/store/snippets'
3838
import type {
@@ -146,7 +146,10 @@ const onClickContextMenu = async () => {
146146
}
147147
148148
if (action === 'delete') {
149-
if (type === 'folder') await moveToTrash()
149+
if (type === 'folder') {
150+
await moveToTrash()
151+
track('snippets/move-to-trash')
152+
}
150153
151154
if (type === 'favorites' || type === 'all' || type === 'inbox') {
152155
await moveToTrash(type)
@@ -165,6 +168,7 @@ const onClickContextMenu = async () => {
165168
166169
if (action === 'duplicate') {
167170
await snippetStore.duplicateSnippetById(props.id)
171+
track('snippets/duplicate')
168172
169173
if (type === 'folder') {
170174
await snippetStore.getSnippetsByFolderIds(folderStore.selectedIds!)
@@ -181,6 +185,12 @@ const onClickContextMenu = async () => {
181185
isFavorites: data
182186
})
183187
188+
if (data) {
189+
track('snippets/add-to-favorites')
190+
} else {
191+
track('snippets/delete-from-favorites')
192+
}
193+
184194
if (type === 'folder') {
185195
await snippetStore.getSnippetsByFolderIds(folderStore.selectedIds!)
186196
}

0 commit comments

Comments
 (0)