Created
August 31, 2017 19:15
-
-
Save joehand/fbbda1320cb9a3117edfab12b7acafbb to your computer and use it in GitHub Desktop.
analytics
This file contains hidden or 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
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