Skip to content

Commit

Permalink
Location analytics initial
Browse files Browse the repository at this point in the history
  • Loading branch information
cykod committed Aug 24, 2010
1 parent b913269 commit b1db783
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 19 deletions.
8 changes: 8 additions & 0 deletions app/controllers/module_app_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,21 @@ def handle_page
cls = req[0].constantize.new(self)
cls.post_process @output
end
include_stat_capture if @capture_location

@cms_site_node_engine = engine
set_robots!
return true
end

end


def include_stat_capture
@output.includes[:js] ||= []
@output.includes[:js] << "http#{'s' if request.ssl?}://www.google.com/jsapi"
@output.includes[:js] << "/javascripts/webalytics.js"
end

def process_logging #:nodoc:
if Configuration.logging
Expand Down
10 changes: 7 additions & 3 deletions app/controllers/page_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class PageController < ModuleAppController

skip_before_filter :validate_module

skip_before_filter :handle_page,:only => :paragraph
skip_after_filter :process_logging, :only => :paragraph
skip_before_filter :handle_page,:only => [ :paragraph, :webalytics ]
skip_after_filter :process_logging, :only => [ :paragraph, :webalytics ]

before_filter :cache_force

Expand Down Expand Up @@ -49,6 +49,11 @@ def index #:nodoc:
end


def webalytics
DomainLogVisitor.log_location(cookies,session,params[:loc])
render :nothing => true
end

protected

# Necessary to prevent repeated authenticity token errors
Expand Down Expand Up @@ -80,6 +85,5 @@ def renderer_test #:nodoc:
skip_before_filter :handle_page,:only => :renderer_test

end


end
26 changes: 26 additions & 0 deletions app/helpers/page_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,32 @@
module PageHelper


def webiva_javascript_tags(js_includes,js_header)
if js_includes || js_header
if js_header; js_includes ||= []; js_includes += js_header; end

js_includes.uniq.each do |js|
if js.to_s[0] != '/'
concat(" <script src=\"#{vh js}\" type='text/javascript'></script>\n")
else
concat(" " + javascript_include_tag(js) + "\n")
end
end
end
nil
end

def webiva_css_tags(css_includes,css_header)
if css_includes || css_header
if css_header; css_includes ||= []; css_includes += css_header; end
css_includes.uniq.each do |css|
concat(" " + stylesheet_link_tag(css) + "\n")
end
end
nil
end


def ajax_url_for(rnd,options={})
opts = options.merge(:site_node => rnd.paragraph.page_revision ? rnd.paragraph.page_revision.revision_container_id : 0,
:page_revision => rnd.paragraph.page_revision_id,
Expand Down
2 changes: 1 addition & 1 deletion app/models/client_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def end_user
return @end_user if @end_user
@end_user = EndUser.find(:first,:conditions => [ 'client_user_id = ?',self.id ])
if(!@end_user)
@end_user = EndUser.new(:first_name => 'Administrative',:last_name => 'User', :hashed_password => 'Invalid', :registered => true )
@end_user = EndUser.new(:first_name => username, :hashed_password => 'Invalid', :registered => true, :email => email )
@end_user.user_class_id = UserClass.client_user_class_id
@end_user.client_user_id = self.id
@end_user.save(false)
Expand Down
3 changes: 2 additions & 1 deletion app/models/domain_log_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def self.start_session(user, session, request)
if !session[:domain_log_session] || session[:domain_log_session][:end_user_id] != user.id
tracking = Tracking.new(request)
session[:user_referrer] = tracking.referrer_domain if tracking.referrer_domain
ses = self.session(session[:domain_log_visitor],request.session_options[:id], user, request.remote_ip, true, tracking)
session[:domain_log_visitor] ||= {}
ses = self.session(session[:domain_log_visitor][:id],request.session_options[:id], user, request.remote_ip, true, tracking)
session[:domain_log_session] = { :id => ses.id, :end_user_id => user.id }
end
end
Expand Down
18 changes: 16 additions & 2 deletions app/models/domain_log_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def self.log_visitor(cookies,user,session,request)
cookies[:v] = nil
end
end
session[:domain_log_visitor] = dlv.id
session[:domain_log_visitor] = { :id => dlv.id, :loc => dlv.country }
else
cookies[:v] = nil
end
Expand All @@ -33,8 +33,22 @@ def self.log_visitor(cookies,user,session,request)
if !cookies[:v]
dlv = DomainLogVisitor.create(:ip_address => request.remote_ip, :end_user_id => user.id)
cookies[:v] = {:value => dlv.visitor_hash, :expires => 20.years.from_now.utc }
session[:domain_log_visitor] = dlv.id
session[:domain_log_visitor] = { :id => dlv.id, :loc => dlv.country }
end

# Need to capture the country and the location
session[:domain_log_visitor][:loc] ? false : true
end

def self.log_location(cookies,session,location = {})
if cookies[:v] && session[:domain_log_visitor][:id] && session[:domain_log_visitor][:loc].blank?
session[:domain_log_visitor] ||= {}
dlv = DomainLogVisitor.find_by_visitor_hash(cookies[:v])
if dlv
location[:country] = 'UN' if location[:country].blank?
dlv.update_attributes(location.slice(:latitude,:longitude,:country,:region,:city))
session[:domain_log_visitor][:loc] = location[:country]
end
end
end
end
14 changes: 2 additions & 12 deletions app/views/layouts/page.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,8 @@
<%= javascript_include_tag 'swfobject' %>
<%= @output.includes[:head_html].join("\n") if @output.includes[:head_html] -%>
<% if @output.includes[:js] || @js_header -%>
<% if @js_header; @output.includes[:js] ||= []; @output.includes[:js] += @js_header; end -%>
<% @output.includes[:js].uniq.each do |js| -%>
<%= javascript_include_tag js %>
<% end -%>
<% end -%>
<% if @output.includes[:css] || @css_header -%>
<% if @css_header; @output.includes[:css] ||= []; @output.includes[:css] += @css_header; end -%>
<% @output.includes[:css].uniq.each do |css| -%>
<%= stylesheet_link_tag css %>
<% end -%>
<% end -%>
<%= webiva_javascript_tags(@output.includes[:js],@js_header) -%>
<%= webiva_css_tags(@output.includes[:css],@css_header) -%>
<%= @output.includes[:extra_head_html].join("\n") if @output.includes[:extra_head_html] -%>
<%= @extra_header.uniq.join("\n") if @extra_header -%>
<link href="/stylesheet/<%= @output.css_site_template_id.to_s + ".css" %>" rel="Stylesheet" type="text/css" />
Expand Down
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
:controller => 'manage/access',
:action => 'login'

map.connect '/webalytics',
:controller => 'page', :action => 'webalytics'

map.connect '/mailing/view/:campaign_hash/:queue_hash',
:controller => 'campaigns', :action => 'view'
map.connect '/mailing/image/:campaign_hash/:queue_hash',
Expand Down
16 changes: 16 additions & 0 deletions public/javascripts/webalytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(function() {
var loc = document.createElement('script');
str = '/webalytics?';
var cloc = google.loader.ClientLocation;
if (cloc == null) { str += 'loc[country]=UN' }
else {
str += 'loc[city]='+encodeURIComponent(cloc.address.city);
str += '&loc[region]='+encodeURIComponent(cloc.address.region);
str += '&loc[country]='+encodeURIComponent(cloc.address.country_code);
str += '&loc[latitude]='+encodeURIComponent(cloc.latitude);
str += '&loc[longitude]='+encodeURIComponent(cloc.longitude);
}
loc.src = str;
document.documentElement.firstChild.appendChild(loc);
})();

0 comments on commit b1db783

Please sign in to comment.