From 9883cd3a83a20ef355fa26c106ca7d561484ae6a Mon Sep 17 00:00:00 2001 From: Kylart Date: Tue, 18 Jun 2019 15:37:20 +0200 Subject: [PATCH 1/5] Made basic service to send analytics to kawanime --- .../server/services/analytics/index.js | 23 +++++++++++++++++++ src/background/server/services/index.js | 4 +++- src/vendor/events.js | 3 ++- 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 src/background/server/services/analytics/index.js diff --git a/src/background/server/services/analytics/index.js b/src/background/server/services/analytics/index.js new file mode 100644 index 0000000..825d87c --- /dev/null +++ b/src/background/server/services/analytics/index.js @@ -0,0 +1,23 @@ +import { eventsList } from '../../../../vendor' +import { Logger, https } from '../../utils' + +const events = eventsList.analytics +const ANALYTICS_URL = 'https://kawanime.com/api/v1/analytics' + +const logger = new Logger('Analytics') + +async function send (event, { eventName, data }) { + https.post(ANALYTICS_URL, { + eventName, + data + }) + .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/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 = [ From 6bb49934f3cd497b0e64bf1fc328ce17d7f52fa9 Mon Sep 17 00:00:00 2001 From: Kylart Date: Tue, 25 Jun 2019 15:29:11 +0200 Subject: [PATCH 2/5] Added settings to disable analytics --- src/background/startUp/templates/config.js | 3 ++- src/components/settings/sections/system.vue | 22 ++++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) 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 From 2caf50c4ef652bb20d557fff4387f9c70058f3ac Mon Sep 17 00:00:00 2001 From: Kylart Date: Tue, 25 Jun 2019 15:29:39 +0200 Subject: [PATCH 3/5] Added KawAnime secret to authenticate on analytics server --- .env.example | 2 ++ src/background/server/services/analytics/index.js | 10 ++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index 33b26dd..2bf4a5b 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,5 @@ +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 index 825d87c..1473247 100644 --- a/src/background/server/services/analytics/index.js +++ b/src/background/server/services/analytics/index.js @@ -1,16 +1,22 @@ +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 = 'https://kawanime.com/api/v1/analytics' +const userToken = readFileSync(localFiles.getPath('_token')) const logger = new Logger('Analytics') async function send (event, { eventName, data }) { https.post(ANALYTICS_URL, { eventName, - data - }) + data, + userToken + }, [], { Authorization: secretKey }) .catch(() => { // We ignore errors logger.error(`Could not send ${eventName} analytics event.`) From cccfc8db31eedbaad48ab6c11b456be05c3fee1f Mon Sep 17 00:00:00 2001 From: Kylart Date: Mon, 1 Jul 2019 15:53:21 +0200 Subject: [PATCH 4/5] Now sending the analytic appOpen event when config loaded succesfully Fix authentication header for analytics --- src/background/server/services/analytics/index.js | 4 ++-- src/store/actions.js | 8 ++++++++ src/store/modules/config/handlers.js | 2 ++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/background/server/services/analytics/index.js b/src/background/server/services/analytics/index.js index 1473247..2fc493e 100644 --- a/src/background/server/services/analytics/index.js +++ b/src/background/server/services/analytics/index.js @@ -6,7 +6,7 @@ import { localFiles } from '../../externals' const events = eventsList.analytics const secretKey = process.env.VUE_APP_KAWANIME_SECRET -const ANALYTICS_URL = 'https://kawanime.com/api/v1/analytics' +const ANALYTICS_URL = 'https://api.kawanime.com/v1/analytics' const userToken = readFileSync(localFiles.getPath('_token')) const logger = new Logger('Analytics') @@ -16,7 +16,7 @@ async function send (event, { eventName, data }) { eventName, data, userToken - }, [], { Authorization: secretKey }) + }, [], { Authorization: `Bearer ${secretKey}` }) .catch(() => { // We ignore errors logger.error(`Could not send ${eventName} analytics event.`) 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) From 0000f9ae6636ad91249f9057d958d31f3ae2367d Mon Sep 17 00:00:00 2001 From: Kylart Date: Sat, 13 Jul 2019 15:31:07 +0200 Subject: [PATCH 5/5] Added version to send to analytics --- .env.example | 1 + src/background/server/services/analytics/index.js | 8 +++++--- vue.config.js | 2 ++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/.env.example b/.env.example index 2bf4a5b..78b4096 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,4 @@ +VUE_APP_KAWANIME_API_URL= VUE_APP_KAWANIME_SECRET= VUE_APP_ANILIST_CLIENT_ID= diff --git a/src/background/server/services/analytics/index.js b/src/background/server/services/analytics/index.js index 2fc493e..31764d3 100644 --- a/src/background/server/services/analytics/index.js +++ b/src/background/server/services/analytics/index.js @@ -6,16 +6,18 @@ import { localFiles } from '../../externals' const events = eventsList.analytics const secretKey = process.env.VUE_APP_KAWANIME_SECRET -const ANALYTICS_URL = 'https://api.kawanime.com/v1/analytics' +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, { + https.post(`${ANALYTICS_URL}/analytics`, { eventName, data, - userToken + userToken, + version: VERSION }, [], { Authorization: `Bearer ${secretKey}` }) .catch(() => { // We ignore errors diff --git a/vue.config.js b/vue.config.js index 4838c4a..a0f55de 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 *