Skip to content

Commit f44e75b

Browse files
authored
Merge pull request #8 from Qbatch/xpath-fixed
Xpath fixed
2 parents 14b5f9e + 4d8d7d3 commit f44e75b

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ $ rake db:migrate
5353

5454
5. Include where to show the heatmap:
5555
```erb
56-
<%= show_heatmap(request.path) %>
56+
<%= show_heatmap %>
5757
```
5858
6. Before adding headmap.js in the application install **jquery-rails** gem and require it in application.js file
5959
```js
@@ -68,14 +68,14 @@ $ rake db:migrate
6868
## Viewing Heat Maps
6969
Use the helper
7070
```erb
71-
<%= show_heatmap(request.path) %>
71+
<%= show_heatmap %>
7272
```
7373
The argument is the path of current page. This way the helper will only display the respective heatmap.
7474
The viewing can be done in multiple ways, for example if you want only the admin users to view heatmap, you can do something like:
7575

7676
```erb
7777
<% if admin_user_signed_in? %>
78-
<%= show_heatmap(request.path) %>
78+
<%= show_heatmap %>
7979
<% end %>
8080
```
8181

@@ -89,7 +89,7 @@ You can use:
8989

9090
```erb
9191
<% if request.query_parameters.include?("see_heatmap") %>
92-
<%= show_heatmap(request.path) %>
92+
<%= show_heatmap %>
9393
<% end %>
9494
```
9595

lib/heatmap/rails/helper.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
module Heatmap
44
module Helper
55

6-
def save_heatmap(options = {})
6+
def exact_route
7+
"#{params[:controller]}/#{params[:action]}"
8+
end
79

10+
def save_heatmap(options = {})
811
click = options[:click] || Heatmap::Rails.options[:click]
912
move = options[:move] || Heatmap::Rails.options[:move]
1013
scroll = options[:scroll] || Heatmap::Rails.options[:scroll]
@@ -38,7 +41,7 @@ def save_heatmap(options = {})
3841
var element_height= event.target.getBoundingClientRect().height;
3942
offset_x_element = event.offsetX / element_width;
4043
offset_y_element = event.offsetY / element_height;
41-
var pageCoords = { path: window.location.pathname, type: 'scroll', xpath: xpath_element, offset_x: offset_x_element , offset_y: offset_y_element, };
44+
var pageCoords = { path: "#{exact_route}", type: 'scroll', xpath: xpath_element, offset_x: offset_x_element , offset_y: offset_y_element, };
4245
4346
scroll_array.push(pageCoords);
4447
if (scroll_array.length >= parseInt(#{scroll})) {
@@ -55,7 +58,7 @@ def save_heatmap(options = {})
5558
var element_height= ev.target.getBoundingClientRect().height;
5659
offset_x_element = ev.offsetX / element_width;
5760
offset_y_element = ev.offsetY / element_height;
58-
var pageCoords = { path: window.location.pathname, type: 'move', xpath: xpath_element, offset_x: offset_x_element , offset_y: offset_y_element, };
61+
var pageCoords = { path: "#{exact_route}", type: 'move', xpath: xpath_element, offset_x: offset_x_element , offset_y: offset_y_element, };
5962
6063
var obj = move_array.find(function (obj) { return obj.xpath === xpath_element; });
6164
if (obj == null){
@@ -78,7 +81,7 @@ def save_heatmap(options = {})
7881
var element_height= ev.target.getBoundingClientRect().height;
7982
offset_x_element = ev.offsetX / element_width;
8083
offset_y_element = ev.offsetY / element_height;
81-
var pageCoords = { path: window.location.pathname, type: 'click', xpath: xpath_element, offset_x: offset_x_element , offset_y: offset_y_element, };
84+
var pageCoords = { path: "#{exact_route}", type: 'click', xpath: xpath_element, offset_x: offset_x_element , offset_y: offset_y_element, };
8285
click_array.push(pageCoords);
8386
if (click_array.length >= parseInt(#{click}))
8487
{
@@ -153,14 +156,14 @@ def save_heatmap(options = {})
153156
html.respond_to?(:html_safe) ? html.html_safe : html
154157
end
155158

156-
def show_heatmap(path,type = false)
159+
def show_heatmap(type = false)
157160
if type
158-
heatmap = HeatMap.where(path: path.to_s , click_type: type)
159-
heatmap_count = HeatMap.where(path: path.to_s , click_type: type).count
161+
heatmap = HeatMap.where(path: exact_route.to_s , click_type: type)
162+
heatmap_count = HeatMap.where(path: exact_route.to_s , click_type: type).count
160163
type = type + 's'
161164
else
162-
heatmap = HeatMap.where(path: path.to_s)
163-
heatmap_count = HeatMap.where(path: path.to_s).count
165+
heatmap = HeatMap.where(path: exact_route.to_s)
166+
heatmap_count = HeatMap.where(path: exact_route.to_s).count
164167
type = 'heatmaps'
165168
end
166169
@data_points = []
@@ -222,7 +225,7 @@ def show_heatmap(path,type = false)
222225
height = element.getBoundingClientRect().height;
223226
var x_coord = getOffset(path.xpath).x+ (width * path.offset_x);
224227
var y_coord = getOffset(path.xpath).y+ (height * path.offset_y);
225-
delete path["xpath"];
228+
delete path["xpath_current"];
226229
delete path["offset_x"];
227230
delete path["offset_y"];
228231
path.x = Math.ceil(parseFloat(x_coord));
@@ -237,11 +240,8 @@ def show_heatmap(path,type = false)
237240
heatmapInstance.addData(data_xpath);
238241
var scroll = JSON.parse('#{raw(@scroll_data.to_json.html_safe)}');
239242
var scroll_data = scroll.map(function(element){
240-
var element = getElement(element.xpath);
241-
if (!element)
242-
return;
243-
width = element.getBoundingClientRect().width;
244-
height = element.getBoundingClientRect().height;
243+
width = getElement(element.xpath).getBoundingClientRect().width;
244+
height = getElement(element.xpath).getBoundingClientRect().height;
245245
dot = document.createElement('div');
246246
dot.className = "dot";
247247
dot.style.left = (getOffset(element.xpath).x+ (width * element.offset_x)) + "px";

lib/heatmap/rails/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Heatmap
22
module Rails
3-
VERSION = "0.1.4"
3+
VERSION = "0.2.0"
44
end
55
end

0 commit comments

Comments
 (0)