summaryrefslogtreecommitdiff
path: root/assets/scripts/metrics.js
blob: 1c415f097e2330741ee8585499ce729a015babd5 (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
'use strict';

(function() {
  var client = new Keen({
    projectId: '56bfd55296773d74919d1035',
    writeKey: 'f59539f4db693c0d240435c13d273e1d' +
      '702cc524d3ecadc4095f13cc0e8ab251' +
      '96c8615c2c815a6d2f4b18ac08b99740' +
      '67b7b1740cd469a761fffd7632065554' +
      '730d6d1a07b32df59516f94144799791' +
      'b03e9776cb9b659b9128c0b79b6e396f'
  });

  setTimeout(function() {
    client.addEvent('pageviews', getMetrics(), function(err, res) {
      if (err) {
        console.error(err);
      }
    });
  }, 1000);

  var getMetrics = function() {
    var metrics = {
      timing: getTimingMetrics(),
      source: getSourceMetrics(),
      visitor: getVisitorMetrics(),
      page: getPageMetrics()
    };
    return metrics;
  };

  var getTimingMetrics = function() {
    if (window.performance && window.performance.timing) {
      var t = window.performance.timing;
      return {
        redirect: t.redirectEnd - t.redirectStart,
        appCache: t.domainLookupStart - t.fetchStart,
        dns: t.domainLookupEnd - t.domainLookupStart,
        tcp: t.connectEnd - t.connectStart,
        request: t.responseStart - t.requestStart,
        response: t.responseEnd - t.responseStart,
        processing: t.domComplete - t.domLoading,
        onLoad: t.loadEventEnd - t.loadEventStart,
        click_to_interactive: t.domInteractive - t.navigationStart,
        total: t.loadEventEnd - t.navigationStart
      };
    } else {
      return {};
    }
  };

  var getVisitorMetrics = function() {
    return {
      browser: getBrowser(),
      language: window.navigator.language
    };
  };

  var getSourceMetrics = function() {
    if (window.document.referrer) {
      var d = window.document.referrer.match(/\/\/(.+)\//);
      var referring_domain = d ? d[1] : '';
    } else {
      var referring_domain = '';
    }
    return {
      referring_domain: referring_domain,
      referrer: window.document.referrer
    };
  };

  var getBrowser = function() {
    var ua = window.navigator.userAgent;
    if (/Firefox/.test(ua) && !/Seamonkey/.test(ua)) {
      return 'Firefox';
    } else if (/Seamonkey/.test(ua)) {
      return 'Seamonkey';
    } else if (/Chrome/.test(ua) && !/Chromium/.test(ua)) {
      return 'Chrome';
    } else if (/Chromium/.test(ua)) {
      return 'Chromium';
    } else if (
      /Safari/.test(ua) && !/Chrome/.test(ua) && !/Chromium/.test(ua)
    ) {
      return 'Safari';
    } else if (/OPR/.test(ua) || /Opera/.test(ua)) {
      return 'Opera';
    } else if (/MSIE/.test(ua)) {
      return 'Internet Explorer';
    } else {
      return 'Unknown';
    }
  };

  var getPageMetrics = function() {
    return {
      path: window.location.pathname,
      title: window.document.title
    };
  };
})();