Added Analytics function to track user hits by ip address, referrer and user agent

This commit is contained in:
John Poulin
2014-04-08 17:24:57 -04:00
committed by cktricky
parent 5056f77395
commit 3f63480022
7 changed files with 95 additions and 3 deletions
+16
View File
@@ -6,6 +6,22 @@ class AdminController < ApplicationController
def dashboard
end
def analytics
if params[:field].nil?
fields = "*"
else
fields = params[:field].map {|k,v| k}.join(",")
end
if params[:ip]
@analytics = Analytics.hits_by_ip(params[:ip], fields)
else
@analytics = Analytics.all
end
render "layouts/admin/_analytics"
end
def get_all_users
@users = User.all
render :partial => "layouts/admin/get_all_users"
+5 -1
View File
@@ -1,6 +1,6 @@
class ApplicationController < ActionController::Base
before_filter :authenticated, :has_info
before_filter :authenticated, :has_info, :create_analytic
helper_method :current_user, :is_admin?, :sanitize_font
# Our security guy keep talking about sea-surfing, cool story bro.
@@ -45,6 +45,10 @@ class ApplicationController < ActionController::Base
redirect_to home_dashboard_index_path if redirect
end
def create_analytic
Analytics.create({ :ip_address => request.remote_ip, :referrer => request.referrer, :user_agent => request.user_agent})
end
def sanitize_font(css)
css
# css if css.match(/\A[0-9]+([\%]|pt)\z/)
+9
View File
@@ -0,0 +1,9 @@
class Analytics < ActiveRecord::Base
attr_accessible :ip_address, :referrer, :user_agent
scope :hits_by_ip, ->(ip,col="*") { select("#{col}").where("ip_address = '#{ip}'")}
def self.count_by_col(col)
calculate(:count, col)
end
end
@@ -0,0 +1,44 @@
<form action="">
Search by IP: <input type="text" name="ip"><br />
<input type="checkbox" value="" name="field[ip_address]"> IP Address<br />
<input type="checkbox" value="" name="field[referrer]"> Referrer<br />
<input type="checkbox" value="" name="field[user_agent]"> User Agent
</form>
<div id="dt_example" class="example_alt_pagination">
<table class="table table-striped table-hover table-bordered pull-left" id="data-table">
<thead>
<tr>
<% params[:field].count.times do %>
<td>&nbsp;</td>
<% end %>
</tr>
</thead>
<tbody>
<% @analytics.each do |a|%>
<tr>
<% a.attributes.each do |k,v| %>
<td><%= v %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<div id="editAcct" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel1" aria-hidden="true">
</div>
<div class="clearfix">
</div>
</div>
</div>
<script type="text/javascript">
function dataTablePagination(){
$('#data-table').dataTable({
"sPaginationType": "full_numbers"
});
};
$(document).ready(dataTablePagination());
</script>
+1
View File
@@ -82,6 +82,7 @@ Railsgoat::Application.routes.draw do
post "delete_user"
put "update_user"
get "get_all_users"
get "analytics"
end
resources :dashboard do
@@ -0,0 +1,10 @@
class CreateAnalytics < ActiveRecord::Migration
def change
create_table :analytics do |t|
t.string :ip_address
t.string :referrer
t.string :user_agent
t.timestamps
end
end
end
+9 -1
View File
@@ -11,7 +11,15 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20140315002730) do
ActiveRecord::Schema.define(:version => 20140408185601) do
create_table "analytics", :force => true do |t|
t.string "ip_address"
t.string "referrer"
t.string "user_agent"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "benefits", :force => true do |t|
t.datetime "created_at", :null => false