diff options
author | Ben Burwell <ben@benburwell.com> | 2015-05-22 00:46:04 -0400 |
---|---|---|
committer | Ben Burwell <ben@benburwell.com> | 2015-05-22 00:46:04 -0400 |
commit | c5afc5400d656b5584380f3e3a74f2b6dd94e257 (patch) | |
tree | 8d864b7e556c805440ad7eea37c4e4c5ed13fec1 /assets | |
parent | a0eb7b9f5fbafc6d7fc0104b391949fb60b6fddd (diff) |
Add metrics and update privacy info
Diffstat (limited to 'assets')
-rw-r--r-- | assets/scripts/metrics.coffee | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/assets/scripts/metrics.coffee b/assets/scripts/metrics.coffee new file mode 100644 index 0000000..9d246d2 --- /dev/null +++ b/assets/scripts/metrics.coffee @@ -0,0 +1,104 @@ +--- +--- + +ENDPOINT_URL = 'https://metrics.benburwell.com/api/events' + +# Collects and reports browser metrics +class Metrics + + # Constructor for the Metrics class + constructor: -> + + # Shall we track detailed metrics? + @dnt = false + @dnt = true if window.navigator.doNotTrack is '1' + @dnt = true if window.navigator.doNotTrack is 'yes' + @dnt = true if window.doNotTrack is '1' + + # Initialize an object to store the metrics + @m = + timing_redirect: 0 + timing_appCache: 0 + timing_dns: 0 + timing_tcp: 0 + timing_request: 0 + timing_response: 0 + timing_processing: 0 + timing_onLoad: 0 + timing_click_to_interactive: 0 + timing_total: 0 + path: '[unknown]' + pageTitle: '[unknown]' + language: 'Unknown' + referring_domain: '[none]' + browser: 'Other' + + # Gather metric values and add them to the field + collect: -> + # We collect certain metrics regardless of + # the user's Do Not Track setting -- these + # reveal no information about the browser + # other than the fact that the DNT flag is + # set. + + t = window.performance?.timing + + @m.timing_redirect = t?.redirectEnd - t?.redirectStart + @m.timing_appCache = t?.domainLookupStart - t?.fetchStart + @m.timing_dns = t?.domainLookupEnd - t?.domainLookupStart + @m.timing_tcp = t?.connectEnd - t?.connectStart + @m.timing_request = t?.responseStart - t?.requestStart + @m.timing_response = t?.responseEnd - t?.responseStart + @m.timing_processing = t?.domComplete - t?.domLoading + @m.timing_onLoad = t?.loadEventEnd - t?.loadEventStart + @m.timing_click_to_interactive = t?.domInteractive - t?.navigationStart + @m.timing_total = t?.loadEventEnd - t?.navigationStart + + @m.path = window.location.pathname + @m.pageTitle = window.document.title + + if @dnt + # These metrics are _not_ collected if DNT + # flag is set, as they can potentially + # reveal information about the browser or + # the user + @m.referring_domain = 'Not Reported' if window.document.referrer + @m.browser = 'Not Reported' + @m.language = 'Not Reported' + else + # Since the DNT flag is not set, we are going + # to gather a small bit of info about the + # visitor + @m.language = window.navigator.language + @m.referring_domain = window.document.referrer?.match(/\/\/(.+)\//)?[1] + + # Now the ridiculous process of browser detection + # Ref: https://developer.mozilla.org/en-US/docs/Browser_detection_using_the_user_agent + ua = window.navigator.userAgent + + if /Firefox/.test(ua) and not /Seamonkey/.test(ua) + @m.browser = 'Firefox' + else if /Seamonkey/.test(ua) + @m.browser = 'Seamonkey' + else if /Chrome/.test(ua) and not /Chromium/.test(ua) + @m.browser = 'Chrome' + else if /Chromium/.test(ua) + @m.browser = 'Chromium' + else if /Safari/.test(ua) and not /Chrome/.test(ua) and not /Chromium/.test(ua) + @m.browser = 'Safari' + else if /OPR/.test(ua) or /Opera/.test(ua) + @m.browser = 'Opera' + else if /MSIE/.test(ua) + @m.browser = 'Internet Explorer' + + send: -> + http = new XMLHttpRequest() + http.open 'POST', ENDPOINT_URL + http.setRequestHeader 'Content-Type', 'application/json;charset=UTF-8' + http.send JSON.stringify({type:'benburwell-com.pageview', properties: @m}) + +setTimeout -> + m = new Metrics + m.collect() + m.send() +, 100 |