-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated to start using the tasks that fetch the statistics from googl…
…e analytics and populate the table 'statistic'.
- Loading branch information
Showing
8 changed files
with
132 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,4 @@ tmp/* | |
db/data.yml | ||
pids/* | ||
doc/app/* | ||
config/analytics_conf.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
user: [email protected] | ||
passwd: my-password | ||
agent: UA-12345678-0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
require 'tasks/support/statistics_helper' | ||
require 'garb' | ||
require 'yaml' | ||
|
||
namespace :statistics do | ||
|
||
# matched urls will be grouped using the first capture (the content | ||
# matched inside the parenthesis) | ||
RULES = [ '(^/bigbluebutton/servers/[^/]+)', | ||
'(^/bigbluebutton/servers/[^/]+/rooms/[^/]+)', | ||
'(^/feedback)', | ||
'(^/invitations)', | ||
'(^/invite)', | ||
'(^/spaces/[^/]+)', | ||
'(^/users/[^/]+)' ] | ||
|
||
desc "Resets the Statistics table with data from google analytics" | ||
task :init => :environment do | ||
Statistic.destroy_all | ||
stats = StatisticsHelper.get_statistics(Date.parse("10/01/2009"), Date.today, RULES) | ||
StatisticsHelper.update_statistics_table(stats) | ||
end | ||
|
||
desc "Updates the Statistics table with data from google analytics" | ||
task :update => :environment do | ||
stats = StatisticsHelper.get_statistics(Date.yesterday, Date.today, RULES) | ||
StatisticsHelper.update_statistics_table(stats) | ||
end | ||
|
||
desc "Updates the Statistics table with data from google analytics" | ||
task :print => :environment do | ||
puts "Current statistics (page views):" | ||
puts " Total (page views): " + Statistic.sum("unique_pageviews").to_s | ||
puts " Average (page views): " + Statistic.average("unique_pageviews").to_s | ||
puts " Minimum (page views): " + Statistic.minimum("unique_pageviews").to_s | ||
puts " Maximum (page views): " + Statistic.maximum("unique_pageviews").to_s | ||
puts " URLs: " + Statistic.count.to_s | ||
puts | ||
puts "Top spaces:" | ||
Statistic.where(['url LIKE ?', '/spaces/%']).order('unique_pageviews desc').first(5).each do |rec| | ||
puts " " + rec.url + " : " + rec.unique_pageviews.to_s | ||
end | ||
puts | ||
puts "Top users:" | ||
Statistic.where(['url LIKE ?', '/users/%']).order('unique_pageviews desc').first(5).each do |rec| | ||
puts " " + rec.url + " : " + rec.unique_pageviews.to_s | ||
end | ||
puts | ||
puts "Top webconf rooms:" | ||
Statistic.where(['url LIKE ?', '%/rooms/%']).order('unique_pageviews desc').first(5).each do |rec| | ||
puts " " + rec.url + " : " + rec.unique_pageviews.to_s | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
class StatisticsHelper | ||
|
||
# Gets the data from google analytics and returns a hash with { 'url' => views } | ||
def self.get_statistics(start_date, end_date, rules) | ||
profile = garb_login | ||
|
||
# we ask for 1.000.000 last visits, if we have more we should use a higher number in limit | ||
report = Garb::Report.new(profile, :start_date => start_date, :end_date => end_date, :limit => 100000) | ||
report.metrics :unique_pageviews | ||
report.dimensions :page_path | ||
filter_results(report.results, rules) | ||
end | ||
|
||
# Updates the db table with the incremental results in 'final_results' | ||
def self.update_statistics_table(final_results) | ||
final_results.each do |key,value| | ||
sta = Statistic.find_by_url(key) | ||
if sta | ||
sta.unique_pageviews = sta.unique_pageviews + value | ||
else | ||
sta = Statistic.new | ||
sta.url = key | ||
sta.unique_pageviews = value | ||
end | ||
sta.save | ||
end | ||
end | ||
|
||
private | ||
|
||
# method to add the different urls to sum up the visits | ||
def self.filter_results(results, rules) | ||
final_hash = Hash.new | ||
|
||
# try to match each result with each rule | ||
# the key in the hash is the first capture in the regex match | ||
# this will group several urls into a single key | ||
results.each do |res| | ||
path = res.page_path | ||
views = res.unique_pageviews.to_i | ||
|
||
rules.each do |rule| | ||
if path.match(rule) | ||
key = path.match(rule)[1] | ||
final_hash[key] = final_hash[key] ? (final_hash[key] + views) : views | ||
end | ||
end | ||
end | ||
|
||
final_hash | ||
end | ||
|
||
# Creates the garb profile used to access google analytics | ||
def self.garb_login | ||
myhash = YAML.load_file("#{Rails.root.to_s}/config/analytics_conf.yml") | ||
user = myhash["user"] | ||
pass = myhash["passwd"] | ||
agent = myhash["agent"] | ||
Garb::Session.login(user, pass) | ||
profile = Garb::Profile.first(agent) | ||
end | ||
|
||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters