Permalink
Browse files

add tracking of blocked cookies

  • Loading branch information...
1 parent a36aae2 commit d519897cd854abad23b79fe7b010266c5bb52c9a @lukebaker lukebaker committed Jun 27, 2013
@@ -87,6 +87,7 @@ def record_action
:user_agent => request.env["HTTP_USER_AGENT"],
:white_label_request => white_label_request?,
:visitor_id => visitor.id)
+ @user_session = user_session
sql = ActiveRecord::Base.send(:sanitize_sql_array, ["INSERT INTO `clicks` (`url`, `controller`, `action`, `user_id`, `referrer`, `session_info_id`, `created_at`, `updated_at`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", request.url, controller_name, action_name, current_user.try(:id), request.referrer, user_session.try(:id), Time.now.utc, Time.now.utc])
ActiveRecord::Base.connection.execute(sql)
@@ -4,6 +4,8 @@ class HomeController < ApplicationController
before_filter :authenticate, :only => [:admin]
before_filter :admin_only, :only => [:no_google_tracking]
+ skip_before_filter :initialize_session, :set_session_timestamp, :record_action, :view_filter, :set_pairwise_credentials, :set_locale, :set_p3p_header, :only => [:cookies_blocked]
+
def index
@example_earl = 'planyc_example'
Question.timeout = 0.5
@@ -18,6 +20,12 @@ def index
def no_google_tracking
end
+ def cookies_blocked
+ BlockedCookie.create(:ip_addr => request.remote_ip, :question_id => params[:question_id], :referrer => params[:referrer], :source => request.referrer, :user_agent => request.env["HTTP_USER_AGENT"], :session_id => params[:session_id])
+ # send 1x1 gif in response
+ send_data(Base64.decode64('R0lGODlhAQABAAAAADs='), :type => "image/gif", :disposition => "inline")
+ end
+
def example
redirect_to("/planyc_example") and return
end
@@ -48,6 +56,8 @@ def admin
@available_charts['user_submitted_ideas'] = { :title => "Number of all submitted ideas over time"}
@available_charts['user_sessions'] = { :title => "Number of all user sessions per day"}
@available_charts['unique_users'] = { :title => "Number of all unique users per day"}
+
+ @blocked_cookies = BlockedCookie.today.group_by(&:question_id)
else
@earls = current_user.earls.sort_by {|x| [(!x.active).to_s, x.name]}
@questions = Question.find(:all, :params => {
@@ -0,0 +1,3 @@
+class BlockedCookie < ActiveRecord::Base
+ named_scope :today, :conditions => ["created_at >= ?", Time.now.beginning_of_day]
+end
@@ -54,6 +54,18 @@
%p
= link_to("See all", admin_path(:all => 'true'), :class => 'btn btn-inverse')
-if current_user.admin?
+ -if @blocked_cookies && @blocked_cookies.count > 0
+ %h4 Today's Blocked Cookies
+ %table.table
+ %thead
+ %tr
+ %th Survey
+ %th Count
+ %tbody
+ - @blocked_cookies.each do |question_id, bc|
+ %tr
+ %td= "/#{Earl.find_by_question_id(question_id).name}"
+ %td= bc.count
%table.table
%thead
%tr
@@ -55,6 +55,8 @@
var cookies_failed = document.getElementById('cookies-failed');
if (cookies_failed) {
cookies_failed.style.display = 'block';
+ var img = new Image();
+ img.src = '/cookies_blocked.gif?question_id=#{@earl.question_id}&session_id=#{@user_session.session_id}&referrer=' + encodeURIComponent(document.referrer);
}
}
else {
View
@@ -41,7 +41,7 @@
#map.connect '/questions/:question_id/choices/:id', :controller => 'choices', :action => 'show'
map.toggle_choice_status '/questions/:earl_id/choices/:id/toggle.:format', :controller => 'choices', :action => 'toggle', :conditions => { :method => :post }
- map.about '/redirect', :controller => 'home', :action => 'redirect'
+ map.cookies_blocked '/cookies_blocked.gif', :controller => 'home', :action => 'cookies_blocked'
map.about '/about', :controller => 'home', :action => 'about'
map.admin '/admin', :controller => 'home', :action => 'admin'
map.privacy '/privacy', :controller => 'home', :action => 'privacy'
@@ -0,0 +1,18 @@
+class CreateBlockedCookies < ActiveRecord::Migration
+ def self.up
+ create_table :blocked_cookies do |table|
+ table.string :referrer, :default => ""
+ table.integer :question_id
+ table.string :user_agent, :default => ""
+ table.string :ip_addr, :default => ""
+ table.string :source, :default => ""
+ table.string :session_id, :default => ""
+ table.timestamps
+ end
+
+ end
+
+ def self.down
+ drop_table :blocked_cookies
+ end
+end
@@ -0,0 +1,12 @@
+class AddIndexesToBlockedCookies < ActiveRecord::Migration
+ def self.up
+ change_table :blocked_cookies do |t|
+ t.index :question_id
+ t.index :ip_addr
+ t.index :created_at
+ end
+ end
+
+ def self.down
+ end
+end
View
@@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110304211014) do
+ActiveRecord::Schema.define(:version => 20130627190740) do
create_table "alternatives", :force => true do |t|
t.integer "experiment_id"
@@ -23,6 +23,21 @@
add_index "alternatives", ["experiment_id"], :name => "index_alternatives_on_experiment_id"
add_index "alternatives", ["lookup"], :name => "index_alternatives_on_lookup"
+ create_table "blocked_cookies", :force => true do |t|
+ t.string "referrer", :default => ""
+ t.integer "question_id"
+ t.string "user_agent", :default => ""
+ t.string "ip_addr", :default => ""
+ t.string "source", :default => ""
+ t.string "session_id", :default => ""
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ add_index "blocked_cookies", ["created_at"], :name => "index_blocked_cookies_on_created_at"
+ add_index "blocked_cookies", ["ip_addr"], :name => "index_blocked_cookies_on_ip_addr"
+ add_index "blocked_cookies", ["question_id"], :name => "index_blocked_cookies_on_question_id"
+
create_table "clicks", :force => true do |t|
t.integer "user_id"
t.string "controller"
@@ -0,0 +1,7 @@
+Factory.define :blocked_cookies do |blocked_cookies|
+ blocked_cookies.referrer { 'string' }
+ blocked_cookies.question_id { 1 }
+ blocked_cookies.user_agent { 'string' }
+ blocked_cookies.ip_addr { 'string' }
+ blocked_cookies.source { 'string' }
+end
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class BlockedCookiesTest < ActiveSupport::TestCase
+ should "be valid with factory" do
+ assert_valid Factory.build(:blocked_cookies)
+ end
+end

0 comments on commit d519897

Please sign in to comment.