-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#21] Track events on Google Analytics
- Loading branch information
1 parent
8f9c8dc
commit 1e54fd0
Showing
6 changed files
with
84 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict'; | ||
|
||
var ua = require('universal-analytics'); | ||
var url = require('url'); | ||
var extend = require('extend'); | ||
var config = require('./config'); | ||
|
||
function pageview(options) { | ||
return function(req, res, next) { | ||
var defaults = { | ||
dp: req.path, | ||
dh: _getUrl(req), | ||
dr: req.get('Referer'), | ||
uip: req.ip, | ||
ua: req.get('User-Agent'), | ||
}; | ||
|
||
if (req.visitor) { | ||
req.visitor | ||
.pageview(extend(defaults, options)) | ||
.send(); | ||
} | ||
|
||
next(); | ||
}; | ||
} | ||
|
||
function sendEvent(req, category, action, label, value, options) { | ||
var defaults = { | ||
t: 'event', | ||
ni: true, | ||
ec: category, | ||
ea: action, | ||
el: label, | ||
ev: value, | ||
}; | ||
|
||
pageview(extend(defaults, options))(req, {}, function(){}); | ||
} | ||
|
||
function _getUrl(req) { | ||
return url.format({ | ||
protocol: req.protocol, | ||
host: req.get('host'), | ||
}); | ||
} | ||
|
||
module.exports = { | ||
trackingId: config.analytics.tracking_id, | ||
middleware: ua.middleware, | ||
pageview: pageview, | ||
sendEvent: sendEvent, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
'use strict'; | ||
|
||
var express = require('express'); | ||
var analytics = require('../analytics'); | ||
var router = express.Router(); | ||
|
||
/* GET home page. */ | ||
router.get('/', function(req, res, next) { | ||
res.render('index', { title: 'Express' }); | ||
}); | ||
res.render('index'); | ||
next(); | ||
}, analytics.pageview()); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters