summaryrefslogtreecommitdiff
path: root/assets/scripts/metrics.coffee
blob: 9d246d23054b67b3d23f7487cd1d8c72b64256ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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