Skip to content

Commit

Permalink
Merge pull request #8 from Qbatch/xpath-fixed
Browse files Browse the repository at this point in the history
Xpath fixed
  • Loading branch information
hassanakram authored Sep 12, 2018
2 parents 14b5f9e + 4d8d7d3 commit f44e75b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ $ rake db:migrate

5. Include where to show the heatmap:
```erb
<%= show_heatmap(request.path) %>
<%= show_heatmap %>
```
6. Before adding headmap.js in the application install **jquery-rails** gem and require it in application.js file
```js
Expand All @@ -68,14 +68,14 @@ $ rake db:migrate
## Viewing Heat Maps
Use the helper
```erb
<%= show_heatmap(request.path) %>
<%= show_heatmap %>
```
The argument is the path of current page. This way the helper will only display the respective heatmap.
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:

```erb
<% if admin_user_signed_in? %>
<%= show_heatmap(request.path) %>
<%= show_heatmap %>
<% end %>
```

Expand All @@ -89,7 +89,7 @@ You can use:

```erb
<% if request.query_parameters.include?("see_heatmap") %>
<%= show_heatmap(request.path) %>
<%= show_heatmap %>
<% end %>
```

Expand Down
30 changes: 15 additions & 15 deletions lib/heatmap/rails/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
module Heatmap
module Helper

def save_heatmap(options = {})
def exact_route
"#{params[:controller]}/#{params[:action]}"
end

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]
Expand Down Expand Up @@ -38,7 +41,7 @@ def save_heatmap(options = {})
var element_height= event.target.getBoundingClientRect().height;
offset_x_element = event.offsetX / element_width;
offset_y_element = event.offsetY / element_height;
var pageCoords = { path: window.location.pathname, type: 'scroll', xpath: xpath_element, offset_x: offset_x_element , offset_y: offset_y_element, };
var pageCoords = { path: "#{exact_route}", type: 'scroll', xpath: xpath_element, offset_x: offset_x_element , offset_y: offset_y_element, };
scroll_array.push(pageCoords);
if (scroll_array.length >= parseInt(#{scroll})) {
Expand All @@ -55,7 +58,7 @@ def save_heatmap(options = {})
var element_height= ev.target.getBoundingClientRect().height;
offset_x_element = ev.offsetX / element_width;
offset_y_element = ev.offsetY / element_height;
var pageCoords = { path: window.location.pathname, type: 'move', xpath: xpath_element, offset_x: offset_x_element , offset_y: offset_y_element, };
var pageCoords = { path: "#{exact_route}", type: 'move', xpath: xpath_element, offset_x: offset_x_element , offset_y: offset_y_element, };
var obj = move_array.find(function (obj) { return obj.xpath === xpath_element; });
if (obj == null){
Expand All @@ -78,7 +81,7 @@ def save_heatmap(options = {})
var element_height= ev.target.getBoundingClientRect().height;
offset_x_element = ev.offsetX / element_width;
offset_y_element = ev.offsetY / element_height;
var pageCoords = { path: window.location.pathname, type: 'click', xpath: xpath_element, offset_x: offset_x_element , offset_y: offset_y_element, };
var pageCoords = { path: "#{exact_route}", type: 'click', xpath: xpath_element, offset_x: offset_x_element , offset_y: offset_y_element, };
click_array.push(pageCoords);
if (click_array.length >= parseInt(#{click}))
{
Expand Down Expand Up @@ -153,14 +156,14 @@ def save_heatmap(options = {})
html.respond_to?(:html_safe) ? html.html_safe : html
end

def show_heatmap(path,type = false)
def show_heatmap(type = false)
if type
heatmap = HeatMap.where(path: path.to_s , click_type: type)
heatmap_count = HeatMap.where(path: path.to_s , click_type: type).count
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: path.to_s)
heatmap_count = HeatMap.where(path: path.to_s).count
heatmap = HeatMap.where(path: exact_route.to_s)
heatmap_count = HeatMap.where(path: exact_route.to_s).count
type = 'heatmaps'
end
@data_points = []
Expand Down Expand Up @@ -222,7 +225,7 @@ def show_heatmap(path,type = false)
height = element.getBoundingClientRect().height;
var x_coord = getOffset(path.xpath).x+ (width * path.offset_x);
var y_coord = getOffset(path.xpath).y+ (height * path.offset_y);
delete path["xpath"];
delete path["xpath_current"];
delete path["offset_x"];
delete path["offset_y"];
path.x = Math.ceil(parseFloat(x_coord));
Expand All @@ -237,11 +240,8 @@ def show_heatmap(path,type = false)
heatmapInstance.addData(data_xpath);
var scroll = JSON.parse('#{raw(@scroll_data.to_json.html_safe)}');
var scroll_data = scroll.map(function(element){
var element = getElement(element.xpath);
if (!element)
return;
width = element.getBoundingClientRect().width;
height = element.getBoundingClientRect().height;
width = getElement(element.xpath).getBoundingClientRect().width;
height = getElement(element.xpath).getBoundingClientRect().height;
dot = document.createElement('div');
dot.className = "dot";
dot.style.left = (getOffset(element.xpath).x+ (width * element.offset_x)) + "px";
Expand Down
2 changes: 1 addition & 1 deletion lib/heatmap/rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Heatmap
module Rails
VERSION = "0.1.4"
VERSION = "0.2.0"
end
end

0 comments on commit f44e75b

Please sign in to comment.