Module: Heatmap::Helper

Defined in:
lib/heatmap/rails/helper.rb

Instance Method Summary collapse

Instance Method Details

#exact_routeObject



6
7
8
# File 'lib/heatmap/rails/helper.rb', line 6

def exact_route
  "#{params[:controller]}/#{params[:action]}"
end

#save_heatmap(options = {}) ⇒ Object



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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/heatmap/rails/helper.rb', line 10

def save_heatmap(options = {})
  click = options[:click] || Heatmap::Rails.options[:click]
  move = options[:move] || Heatmap::Rails.options[:move]
  scroll = options[:scroll] || Heatmap::Rails.options[:scroll]
  html_element = options[:html_element] || Heatmap::Rails.options[:html_element]
  html = ""

  js = "<script type=\"text/javascript\">\n$( document ).ready(function() {\n  var move_array = [];\n  var scroll_array = [];\n\n  (function() {\ndocument.onwheel = handleWheelMove;\nfunction handleWheelMove(event) {\n  var dot, eventDoc, doc, body, offsetX, offsetY;\n  event = event || window.event;\n  if (event.offsetX == null && event.clientX != null) {\n    eventDoc = (event.target && event.target.ownerDocument) || document;\n    doc = eventDoc.documentElement;\n    body = eventDoc.body;\n    event.offsetX = event.clientX +\n    (doc && doc.scrollLeft || body && body.scrollLeft || 0) -\n    (doc && doc.clientLeft || body && body.clientLeft || 0);\n    event.offsetY = event.clientY +\n    (doc && doc.scrollTop  || body && body.scrollTop  || 0) -\n    (doc && doc.clientTop  || body && body.clientTop  || 0 );\n  }\n  var xpath_element =  xpathstring(event);\n  var element_width = event.target.getBoundingClientRect().width;\n  var element_height= event.target.getBoundingClientRect().height;\n  offset_x_element = event.offsetX / element_width;\n  offset_y_element = event.offsetY /  element_height;\n  var pageCoords = { path: \"\#{exact_route}\",  type: 'scroll', xpath: xpath_element, offset_x: offset_x_element ,  offset_y: offset_y_element, };\n\n  scroll_array.push(pageCoords);\n  if (scroll_array.length >= parseInt(\#{scroll})) {\n    var coordinates = scroll_array;\n    sendRequest({'scroll_data': coordinates, 'total_scrolls': \#{scroll} });\n    scroll_array = [];\n  }\n}\n  })();\n\n  document.querySelector('\#{html_element}').onmousemove = function(ev) {\nvar xpath_element =  xpathstring(ev);\nvar element_width = ev.target.getBoundingClientRect().width;\nvar element_height= ev.target.getBoundingClientRect().height;\noffset_x_element = ev.offsetX / element_width;\noffset_y_element = ev.offsetY /  element_height;\nvar pageCoords = { path: \"\#{exact_route}\",  type: 'move', xpath: xpath_element, offset_x: offset_x_element ,  offset_y: offset_y_element, };\n\nvar obj = move_array.find(function (obj) { return obj.xpath === xpath_element; });\nif (obj == null){\n move_array.push(pageCoords);\n}\nif (move_array.length >= parseInt(\#{move}))\n{\n  var coordinates = move_array;\n  sendRequest({'move_data': coordinates,'total_moves': \#{move} });\n  move_array = [];\n}\n\n\n  };\n  var click_array = [];\n  document.querySelector('\#{html_element}').onclick = function(ev) {\n\nvar xpath_element=  xpathstring(ev);\nvar element_width = ev.target.getBoundingClientRect().width;\nvar element_height= ev.target.getBoundingClientRect().height;\noffset_x_element = ev.offsetX / element_width;\noffset_y_element = ev.offsetY /  element_height;\nvar pageCoords = { path: \"\#{exact_route}\",  type: 'click', xpath: xpath_element, offset_x: offset_x_element ,  offset_y: offset_y_element, };\nclick_array.push(pageCoords);\nif (click_array.length >= parseInt(\#{click}))\n{\n  var coordinates = click_array;\n  sendRequest({'click_data': coordinates, 'total_clicks': \#{click} });\n  click_array = [];\n\n}\n  };\n  function sendRequest(coordinates_data){\n$.ajax({\n   method: \"POST\",\n   url: '/points',\n   data: coordinates_data,\n   dataType: 'application/json'\n});\n  }\n});\n\nfunction xpathstring(event) {\n  var e = event.srcElement || event.originalTarget,\n  path = xpath(e, '');\n  return path\n}\nfunction xpath(element, suffix) {\n  var parent, child_index, node_name;\n  parent = element.parentElement;\n  if (parent) {\n  node_name = element.nodeName.toLowerCase();\n  child_index = nodeindex(element, parent.children) + 1;\n  return xpath(parent, '/' + node_name + '[' + child_index + ']' + suffix);\n  } else {\n  return '//html[1]' + suffix;\n  }\n}\nfunction nodeindex(element, array) {\n  var i,\n  found = -1,\n  element_name = element.nodeName.toLowerCase(),\n  matched\n ;\n\n  for (i = 0; i != array.length; ++i) {\n  matched = array[i];\n  if (matched.nodeName.toLowerCase() === element_name) {\n      ++found;\n\n\n  if (matched === element) {\n      return found;\n  }\n  }\n  }\n\n  return -1;\n}\nfunction getOffset( path ) {\nel = document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;\nvar _x = 0;\nvar _y = 0;\nwhile( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {\n    _x += el.offsetLeft - el.scrollLeft;\n    _y += el.offsetTop - el.scrollTop;\n    el = el.offsetParent;\n}\nreturn { y: _y, x: _x };\n}\n </script>\n"

  html += js
  html.respond_to?(:html_safe) ? html.html_safe : html
end

#show_heatmap(type = false) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/heatmap/rails/helper.rb', line 159

def show_heatmap(type = false)
  if type
    heatmap = HeatMap.where(path: exact_route.to_s , click_type: type)
    heatmap_count = HeatMap.where(path: exact_route.to_s , click_type: type).count
    type = type + 's'
  else
    heatmap = HeatMap.where(path: exact_route.to_s)
    heatmap_count = HeatMap.where(path: exact_route.to_s).count
    type = 'heatmaps'
  end
  @data_points = []
  @data_xpaths = []
  @scroll_data = []
  heatmap.each do |coordinate|
    if (coordinate.click_type == "scroll")
      @scroll_data.push({xpath: coordinate.xpath, offset_x: coordinate.offset_x, offset_y:coordinate.offset_y})
    else
      @data_xpaths.push({xpath: coordinate.xpath, offset_x: coordinate.offset_x, offset_y:coordinate.offset_y, value: 100})
    end
  end
  html = ""
  js = "<script type=\"text/javascript\">\n  var heatmapInstance = h337.create({\ncontainer: document.querySelector('body'),\nradius: 40\n  });\n  window.onload = function() {\nvar parent_div = document.createElement(\"div\");\nvar text_div = document.createElement(\"span\");\nparent_div.style.padding= \"14px\";\nparent_div.style.position = \"absolute\";\nparent_div.style.top = \"0\";\nparent_div.style.right = \"0\";\nparent_div.style.background =\"rgba(0, 0, 0, 0.7)\";\nparent_div.style.color =\"white\";\nparent_div.style.textAlign =\"center\";\nvar text_node = document.createTextNode(\"\#{type.capitalize} Recorded\");\ntext_div.appendChild(text_node);\nparent_div.appendChild(text_div);\nvar numeric_div = document.createElement(\"h1\");\nvar numeric_node =  document.createTextNode(\"\#{heatmap_count}\");\nnumeric_div.appendChild(numeric_node);\nparent_div.appendChild(numeric_div);\ndocument.body.appendChild(parent_div);\n  }\n  function getOffset( path ) {\nel = document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;\nvar _x = 0;\nvar _y = 0;\nwhile( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {\n    _x += el.offsetLeft - el.scrollLeft;\n    _y += el.offsetTop - el.scrollTop;\n    el = el.offsetParent;\n}\nreturn { y: _y, x: _x };\n  }\n  function getElement(xpath){\n return document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;\n  }\n  var xpath_current = JSON.parse('\#{raw(@data_xpaths.to_json.html_safe)}');\n  var data_xpath = xpath_current.map(function(path){\nif (path != null) {\n  element = getElement(path.xpath);\n  if (element != null){\n    width = element.getBoundingClientRect().width;\n    height = element.getBoundingClientRect().height;\n    var x_coord = getOffset(path.xpath).x+  (width * path.offset_x);\n    var y_coord = getOffset(path.xpath).y+  (height * path.offset_y);\n    delete path[\"xpath_current\"];\n    delete path[\"offset_x\"];\n    delete path[\"offset_y\"];\n    path.x = Math.ceil(parseFloat(x_coord));\n    path.y = Math.ceil(parseFloat(y_coord));\n    return path;\n  }\n}\n  });\n  // Removed: Null Xpath(s)\n  var data_xpath = data_xpath.filter(function(val){ return val!==undefined; });\n\n  heatmapInstance.addData(data_xpath);\n  var scroll = JSON.parse('\#{raw(@scroll_data.to_json.html_safe)}');\n  var scroll_data = scroll.map(function(element){\nwidth = getElement(element.xpath).getBoundingClientRect().width;\nheight = getElement(element.xpath).getBoundingClientRect().height;\ndot = document.createElement('div');\ndot.className = \"dot\";\ndot.style.left = (getOffset(element.xpath).x+  (width * element.offset_x)) + \"px\";\ndot.style.top = (getOffset(element.xpath).y+  (height * element.offset_y) )+ \"px\";\ndelete element[\"xpath\"];\ndelete element[\"offset_x\"];\ndelete element[\"offset_y\"];\ndot.style.backgroundColor =\"white\";\ndot.style.position =\"absolute\";\ndot.style.borderWidth =\"8px\";\ndot.style.borderStyle =\"solid\";\nvar colors = Array('#ee3e32', '#f68838' ,'#fbb021', '#1b8a5a','#1d4877');\nvar color = colors[Math.floor(Math.random()*colors.length)];\ndot.style.borderColor = color;\ndot.style.borderRadius =\"50%\";\ndot.style.opacity =\"0.7\";\nvar arrow_node = document.createTextNode(\"\\u21C5\");\ndot.appendChild(arrow_node);\ndocument.body.appendChild(dot);\n  });\n\n  scroll_data = scroll_data.filter(function(val){ return val!==undefined; });\n</script>\n"

  html += js
  html.respond_to?(:html_safe) ? html.html_safe : html
end