Skip to content

Commit

Permalink
add google analytics module
Browse files Browse the repository at this point in the history
  • Loading branch information
littlewin committed Nov 29, 2017
1 parent f17b6ee commit 976f1a5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion config/env.js
Expand Up @@ -24,7 +24,8 @@ const INFO = {
title: 'littlewin.server',
version: '1.0.0',
author: 'littlewin',
site: 'http://littlewin.wang',
site: 'https://littlewin.wang',
GA: 'UA-108992108-1',
with: ['Node.js', 'MongoDB', 'Koa2', 'Nginx']
}

Expand Down
33 changes: 33 additions & 0 deletions controllers/ga.js
@@ -0,0 +1,33 @@
/**
* @file Google Analytics控制
* @author littlewin(littlewin.wang@gmail.com)
*/

const request = require('request')
const config = require('config/env')[process.env.NODE_ENV || 'development']

class GA {
static async post (ctx) {
let { query } = ctx.request.body

if (!query) {
ctx.throw(404, "谷歌分析提交参数为空")
}

const ip = (ctx.header['x-forwarded-for'] || ctx.header['x-real-ip'] || ctx.ip || ctx.ips[0])

query += `&dh=${config.INFO.site}&tid=${config.INFO.GA}&uip=${ip}`

ctx.status = 200,
ctx.body = {
success: true,
message: "谷歌分析已提交"
}

request.get({
url: `https://www.google-analytics.com/collect${query}`,
})
}
}

module.exports = GA
3 changes: 3 additions & 0 deletions routes/index.js
Expand Up @@ -20,6 +20,7 @@ const Like = require('controllers/like')
const Event = require('controllers/event')
const Message = require('controllers/message')
const Spotify = require('controllers/spotify')
const GA = require('controllers/ga')
const middleware = require('middlewares')

router
Expand Down Expand Up @@ -92,4 +93,6 @@ router

.get('/spotify', Spotify.getToken)

.post('/ga', GA.post)

module.exports = router

0 comments on commit 976f1a5

Please sign in to comment.