diff --git a/.env.example b/.env.example index 33b26dd..78b4096 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,6 @@ +VUE_APP_KAWANIME_API_URL= +VUE_APP_KAWANIME_SECRET= + VUE_APP_ANILIST_CLIENT_ID= VUE_APP_ANILIST_CLIENT_SECRET= diff --git a/src/background/server/services/analytics/index.js b/src/background/server/services/analytics/index.js new file mode 100644 index 0000000..31764d3 --- /dev/null +++ b/src/background/server/services/analytics/index.js @@ -0,0 +1,31 @@ +import { readFileSync } from 'fs' + +import { eventsList } from '../../../../vendor' +import { Logger, https } from '../../utils' +import { localFiles } from '../../externals' + +const events = eventsList.analytics +const secretKey = process.env.VUE_APP_KAWANIME_SECRET +const ANALYTICS_URL = process.env.VUE_APP_KAWANIME_API_URL +const VERSION = process.env.KAWANIME_VERSION +const userToken = readFileSync(localFiles.getPath('_token')) + +const logger = new Logger('Analytics') + +async function send (event, { eventName, data }) { + https.post(`${ANALYTICS_URL}/analytics`, { + eventName, + data, + userToken, + version: VERSION + }, [], { Authorization: `Bearer ${secretKey}` }) + .catch(() => { + // We ignore errors + logger.error(`Could not send ${eventName} analytics event.`) + }) +} + +export default { + eventName: events.main, + handler: send +} diff --git a/src/background/server/services/index.js b/src/background/server/services/index.js index c4d123a..baf9278 100644 --- a/src/background/server/services/index.js +++ b/src/background/server/services/index.js @@ -13,6 +13,7 @@ import isOnline from './isOnline' import vault from './vault' import watchLists from './watchLists' import registerService from './registerService' +import analytics from './analytics' const services = [ ...config, @@ -29,7 +30,8 @@ const services = [ news, seasons, episodes, - isOnline + isOnline, + analytics ] // auto update diff --git a/src/background/startUp/templates/config.js b/src/background/startUp/templates/config.js index bcd338c..461e3ca 100644 --- a/src/background/startUp/templates/config.js +++ b/src/background/startUp/templates/config.js @@ -32,7 +32,8 @@ export default { darkTheme: true, autoStart: false, toTray: false, - center: true + center: true, + analytics: true }, bounds: { height: null, diff --git a/src/components/settings/sections/system.vue b/src/components/settings/sections/system.vue index b2a3fce..9d889db 100644 --- a/src/components/settings/sections/system.vue +++ b/src/components/settings/sections/system.vue @@ -6,7 +6,7 @@ v-card-text v-container(grid-list-lg, pa-0, pb-2) v-layout(row, wrap, justify-space-around, align-center) - v-flex(xs12, sm4) + v-flex(xs12, sm3) v-switch.mt-0( v-model='tray', color='primary', @@ -14,7 +14,7 @@ persistent-hint, hint='*Should KawAnime be in your tray?' ) - v-flex(xs12, sm4) + v-flex(xs12, sm3) v-switch.mt-0( v-model='autoStart', color='primary', @@ -22,7 +22,7 @@ persistent-hint, hint='*Should KawAnime start on boot?' ) - v-flex(xs12, sm4) + v-flex(xs12, sm3) v-switch.mt-0( v-model='darkTheme', color='primary', @@ -30,6 +30,14 @@ persistent-hint, hint='Theme to apply' ) + v-flex(xs12, sm3) + v-switch.mt-0( + v-model='analytics', + color='primary', + :label="analytics ? 'Ok' : 'Nope'" + persistent-hint, + hint='Allows the app to send events to our server (mainly to know how many users we have)' + ) v-container(grid-list-lg, pa-0, mt-3) v-layout(row, wrap, justify-space-around, align-center) @@ -144,6 +152,14 @@ export default { this.setDeepValue('system.darkTheme', val) } }, + analytics: { + get () { + return this.system.analytics + }, + set (val) { + this.setDeepValue('system.analytics', val) + } + }, height: { get () { return this.bounds.height diff --git a/src/store/actions.js b/src/store/actions.js index b39aade..a2e5cab 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -32,6 +32,14 @@ export default { checkOnlineStatus () { ipcRenderer.send(eventsList.isOnline.main) }, + analytics ({ state }, args) { + const { config: { system: { analytics } } } = state.config + + if (analytics) { + ipcRenderer.send(eventsList.analytics.main, args) + } + }, + setEvents ({ state, commit, dispatch }) { ipcRenderer.on(eventsList.isOnline.success, (e) => { const isConnected = state.isConnected diff --git a/src/store/modules/config/handlers.js b/src/store/modules/config/handlers.js index e85f7f3..195d726 100644 --- a/src/store/modules/config/handlers.js +++ b/src/store/modules/config/handlers.js @@ -4,6 +4,8 @@ export const get = { success ({ commit, dispatch }, data) { commit('set', data.config) + dispatch('analytics', { eventName: 'appOpen' }, isRoot) + // Setting defaults dispatch('player/setUp', null, isRoot) commit('localFiles/setDir', data.config.localPath, isRoot) diff --git a/src/vendor/events.js b/src/vendor/events.js index eabaa3e..48856b4 100644 --- a/src/vendor/events.js +++ b/src/vendor/events.js @@ -19,7 +19,8 @@ const getOnly = [ // Internal 'env', 'isOnline', - 'externalOpen' + 'externalOpen', + 'analytics' ] const updatable = [ diff --git a/vue.config.js b/vue.config.js index f244255..af1b5fd 100644 --- a/vue.config.js +++ b/vue.config.js @@ -1,6 +1,8 @@ const fs = require('fs') const path = require('path') +process.env.KAWANIME_VERSION = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'package.json'))).version + /** * Taken from https://gist.github.com/kethinov/6658166#gistcomment-2389484 *