diff --git a/app/site.rb b/app/site.rb index bb85b30e..5600f8fc 100644 --- a/app/site.rb +++ b/app/site.rb @@ -32,6 +32,50 @@ get '/site/:username/?' do |username| erb :'site', locals: {site: site, is_current_site: site == current_site} end +get '/site/:username/stats' do + @site = Site[username: params[:username]] + not_found if @site.nil? + + @stats = {} + + %i{referrers locations paths}.each do |stat| + @stats[stat] = @site.send("stat_#{stat}_dataset".to_sym).order(:views.desc).limit(100).all + end + + @stats[:locations].collect! do |location| + location_name = '' + + location_name += location.city_name if location.city_name + + if location.region_name + # Some of the region names are numbers for some reason. + begin + Integer(location.region_name) + rescue + location_name += ', ' unless location_name == '' + location_name += location.region_name + end + end + + if location.country_code2 && !$country_codes[location.country_code2].nil? + location_name += ', ' unless location_name == '' + location_name += $country_codes[location.country_code2] + end + + location_hash = {name: location_name, views: location.views} + if location.latitude && location.longitude + location_hash.merge! latitude: location.latitude, longitude: location.longitude + end + location_hash + end + + @stats[:stat_days] = @site.stats_dataset.order(:created_at.desc).limit(7).all.reverse + + @multi_tooltip_template = "<%= datasetLabel %> - <%= value %>" + + erb :'site/stats', locals: {site: @site} +end + post '/site/:username/set_editor_theme' do require_login current_site.editor_theme = params[:editor_theme] diff --git a/environment.rb b/environment.rb index ccff05f4..35a7e704 100644 --- a/environment.rb +++ b/environment.rb @@ -131,3 +131,11 @@ if ENV['RACK_ENV'] != 'development' # Sass::Plugin.options[:never_update] = true Sass::Plugin.options[:full_exception] = false end + +require 'csv' + +$country_codes = {} + +CSV.foreach("./files/country_codes.csv") do |row| + $country_codes[row.last] = row.first +end diff --git a/views/site/stats.erb b/views/site/stats.erb new file mode 100644 index 00000000..61220943 --- /dev/null +++ b/views/site/stats.erb @@ -0,0 +1,310 @@ +
+
+
+

Your Stats

+
+ <% if !@events.empty? && (site.followings_dataset.count > 0) %> + class="selected"<% end %>>All + class="selected"<% end %>>Profile Activity + <% end %> + Global Activity +
+
+
+

Your Site

+ Edit Site +
+
+
+ +
+
+
+
+
+ +
+

Latest Visitors

+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ San Francisco, CA + neocities.org + + 7:11PM + /index, /links, /art +
+ Portland, OR + Google search + + 7:11PM + /index +
+ London, UK + Twitter URL + + 7:11PM + /index, /about +
+ Hong Kong, China + Google search + + 7:11PM + /index, /links, /art, /music +
+ San Francisco, CA + Facebook URL + + 7:11PM - 4/27/15 + /index +
+ San Francisco, CA + neocities.org + + 7:11PM - 4/27/15 + /index, /links, /art, /music, /about +
+ Portland, OR + Google search + + 7:11PM - 4/27/15 + /index +
+ London, UK + Twitter URL + + 7:11PM - 4/27/15 + /index, /about +
+ Hong Kong, China + Google search + + 7:11PM - 4/27/15 + /index, /links, /art, /music, /tech, /about +
+ Hong Kong, China + Google search + + 7:11PM - 4/27/15 + /index, /links, /art, /music +
+
+
+ +

Last 7 Days

+

(Upgrade to see up to see stats for all time)

+ + + + +
+
+

Top Paths

+ + + + + + + + + <% @stats[:paths].each do |path| %> + + + + + <% end %> + +
PathVisits
<%= path.name.gsub(/\?.+/i, '') %><%= path.views %>
+ +

Top Locations

+ + + + + + + + + <% @stats[:locations].each do |location| %> + + + + + <% end %> + +
CityVisits
+ <%= location[:name] %> + <%= location[:views] %>
+
+ +
+ +

Top Referrers

+ + + + + + + + + <% @stats[:referrers].each do |referrer| %> + + + + + <% end %> + +
ReferrerVisits
<%= referrer.url %><%= referrer.views %>
+
+
+
+ +
+
+

<%= site.title %>

+
+
+ <% if site.updated_at %> + Last updated
<%= site.updated_at.ago %> + <% else %> + Your new website!
Start Building + <% end %> +
+
+
<%= site.views.format_large_number %> views
+ <% follows_count = site.follows_dataset.count %> +
<%= follows_count.format_large_number %> follower<%= follows_count == 1 ? '' : 's' %>
+
+
+
+ + + + + + <%== erb :'_follows', layout: false, locals: {site: site, is_current_site: site == current_site} %> + + <%== erb :'_tags', layout: false, locals: {site: site, is_current_site: site == current_site} %> +
+
+
+
+ + + +