chore: send stats to google analytics

pull/172/head
Amio 2018-09-29 15:58:10 +08:00
rodzic 09e90025da
commit 399fc6946f
3 zmienionych plików z 29 dodań i 1 usunięć

Wyświetl plik

@ -1,6 +1,7 @@
// Cache ongoing fetching, prevent redundant request
const pool = require('./live-pool.js')
const raven = require('./raven.js')
const sendStats = require('./send-stats.js')
module.exports = async (service, fn, paramsPath) => {
const fetchStart = new Date()
@ -11,6 +12,7 @@ module.exports = async (service, fn, paramsPath) => {
const fetcher = fn(...paramsPath.split('/')).then(
result => {
console.log(timeSince(fetchStart), fetchKey)
sendStats('stats', service, fetchKey, new Date() - fetchStart)
return typeof result === 'object' ? result : { failed: true }
},
err => gotErrorHandler(service, paramsPath, err)
@ -41,6 +43,7 @@ const gotErrorHandler = (service, paramsPath, err) => {
logError(serviceKey, status, err)
sendError(serviceKey, status, err)
sendStats('error', status, serviceKey)
return { status, failed: true }
}
@ -57,7 +60,9 @@ const logError = (serviceKey, status, err) => {
// send error to sentry
const sendError = (serviceKey, status, err) => {
status === 'unknown' && raven && raven.captureException(err, {
if (status !== 'unknown') return
raven && raven.captureException(err, {
tags: {
serviceKey,
url: err.url

21
libs/send-stats.js 100644
Wyświetl plik

@ -0,0 +1,21 @@
const got = require('got')
const { TRACKING_GA, NOW_URL } = process.env
// Send stats to google analytics
module.exports = (category, action, label, value) => {
if (!TRACKING_GA) return
got.get('https://www.google-analytics.com/collect', {
query: {
v: '1',
tid: TRACKING_GA,
cid: NOW_URL || '000',
t: 'event',
ec: category,
ea: action,
el: label,
ev: value
}
})
}

Wyświetl plik

@ -1,4 +1,5 @@
const serveBadge = require('./serve-badge.js')
const sendStats = require('./send-stats.js')
module.exports = (req, res) => {
req.params = {
@ -7,4 +8,5 @@ module.exports = (req, res) => {
color: 'orange'
}
serveBadge(req, res, { code: 404 })
sendStats('invalid', '404', req.url)
}