diff --git a/README.md b/README.md index 1a8787e..03a9890 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 %> ``` @@ -89,7 +89,7 @@ You can use: ```erb <% if request.query_parameters.include?("see_heatmap") %> - <%= show_heatmap(request.path) %> + <%= show_heatmap %> <% end %> ``` diff --git a/lib/heatmap/rails/helper.rb b/lib/heatmap/rails/helper.rb index 8034650..e931b3b 100644 --- a/lib/heatmap/rails/helper.rb +++ b/lib/heatmap/rails/helper.rb @@ -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] @@ -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})) { @@ -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){ @@ -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})) { @@ -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 = [] @@ -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)); @@ -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"; diff --git a/lib/heatmap/rails/version.rb b/lib/heatmap/rails/version.rb index 6cac560..f359e49 100644 --- a/lib/heatmap/rails/version.rb +++ b/lib/heatmap/rails/version.rb @@ -1,5 +1,5 @@ module Heatmap module Rails - VERSION = "0.1.4" + VERSION = "0.2.0" end end