summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/ui/reader/Tracker.js
blob: d2dec39beeeab593b809046dea5d478e9ae5e7f2 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
if (window.location.host.indexOf("lvh.me") === -1) {
	(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
	(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
	m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
	})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
}
else {
	ga = function(){}
}
ga('create', 'UA-56883705-1', 'auto');
ga('send', 'pageview');

var Tracker = Fiber.extend(function(base){

	var exports = {
		init: function(opt){
		  this.wall_id = null
		  this.scenery_id = null
		  this.clicks = 0
		  
		  this.wallTimer = new Timer ()
		  this.roomTimer = new Timer ()
		  this.sceneryTimer = new Timer ()

		  this.bind()
		  // this.trackPageview(opt)
		},
		
		bind: function () {
		  window.addEventListener("click", this.trackClick.bind(this), true)
		  app.on("change-wall", this.changeWall.bind(this))
		  app.on("pick-scenery", this.pickScenery.bind(this))
		  app.on("close-scenery", this.trackScenery.bind(this))
		  app.on("change-room", this.changeRoom.bind(this))
		},

		pushEvent: function(event){
		  // this.events.push(event)
		  event.unshift("send")
		  ga.apply( ga, event )
		},
		
		trackPageview: function(opt){
      // this.pushEvent([ "view", opt.mode ])
		},

    //
    // how long they spend in front of each wall

		changeWall: function(opt){
		  var duration = this.wallTimer.currentTime()
		  if (this.wall_id && duration > 5000) {
        this.pushEvent([ "wall", this.wall_id, duration ])
		  }
      this.wall_id = opt.wall.id
      this.wallTimer.start()
		},
		
    //
    // how long the user spends on each item they click
    
		pickScenery: function(opt){
		  if (this.scenery_id && opt.scenery.id !== this.scenery_id) {
		    this.trackScenery()
		  }
		  else {
        this.sceneryTimer.start()
		  }
		  this.scenery_id = opt.scenery.id
		},
		
		trackScenery: function(){
		  var duration = this.sceneryTimer.currentTime()
		  if (this.scenery_id && duration > 1000) {
        this.pushEvent([ "scenery", this.scenery_id, duration ])
		  }
      this.scenery_id = null
      this.sceneryTimer.reset()
		},

		//
    // how long they spend in the room

		changeRoom: function(opt){
		  var duration = this.roomTimer.currentTime()
		  if (this.room_id !== opt.room.id) {
		    if (this.room_id && duration > 5000) {
          this.pushEvent([ "room", this.room_id, duration ])
        }
        this.roomTimer.start()
        this.room_id = opt.room.id
		  }
		},
		
    //
    // how many clicks per room

		trackClick: function(opt){
      this.clicks += 1
		},
		
		save: function () {
		  // possibly just push to google analytics
		},
		
  }
  
  return exports
})


var Timer = Fiber.extend(function(base){
  var exports = {

    init: function(opt){
      this.time = 0
    },
    
    reset: function(){
      this.time = 0
    },
    
    start: function(){
      this.time = Date.now()
    },

    currentTime: function(){
      return this.time ? Date.now() - this.time : 0
    },
  }
  
  return exports
})