Skip to content

Instantly share code, notes, and snippets.

@joehand
Created August 31, 2017 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joehand/fbbda1320cb9a3117edfab12b7acafbb to your computer and use it in GitHub Desktop.
Save joehand/fbbda1320cb9a3117edfab12b7acafbb to your computer and use it in GitHub Desktop.
analytics
var fairAnalytics = require('fair-analytics-client-api')
module.exports = analytics
function analytics (state, emitter) {
// create a fa instance
var fa = fairAnalytics({
url: `http://analytics.domain.com` // Fair Analytics server
})
var start = null
var pgCount = 0
emitter.on(state.events.DOMCONTENTLOADED, trackEvent)
emitter.on(state.events.NAVIGATE, trackEvent)
function trackEvent () {
if (window.location.protocol !== 'dat:' && window.location.hostname !== domain) return
// track events
if (!start) start = new Date()
var eventTime = new Date()
fa.send({
sessionDuration: eventTime.getTime() - start.getTime(),
sessionPageCount: pgCount++,
event: 'pageView', // event is mandatory and can be anything
pathname: window.location.pathname
})
.then(res => {
if (res.ok) {
console.log('success')
}
})
.catch(err => {
console.error(err.message)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment