Commit 4aee14572c1191fa09a890f0550756603d9cbb92
1 parent
cd9c0b07
Exists in
master
and in
29 other branches
Add analytics plugin
Showing
13 changed files
with
386 additions
and
0 deletions
Show diff stats
plugins/analytics/controllers/profile/analytics_plugin/time_on_page_controller.rb
0 → 100644
| @@ -0,0 +1,30 @@ | @@ -0,0 +1,30 @@ | ||
| 1 | +class AnalyticsPlugin::TimeOnPageController < ProfileController | ||
| 2 | + | ||
| 3 | + before_filter :skip_page_view | ||
| 4 | + | ||
| 5 | + def page_load | ||
| 6 | + # to avoid concurrency problems with the original deferred request, also defer this | ||
| 7 | + Scheduler::Defer.later do | ||
| 8 | + page_view = profile.page_views.where(request_id: params[:id]).first | ||
| 9 | + page_view.request = request | ||
| 10 | + page_view.page_load! | ||
| 11 | + end | ||
| 12 | + | ||
| 13 | + render nothing: true | ||
| 14 | + end | ||
| 15 | + | ||
| 16 | + def report | ||
| 17 | + page_view = profile.page_views.where(request_id: params[:id]).first | ||
| 18 | + page_view.request = request | ||
| 19 | + page_view.increase_time_on_page! | ||
| 20 | + | ||
| 21 | + render nothing: true | ||
| 22 | + end | ||
| 23 | + | ||
| 24 | + protected | ||
| 25 | + | ||
| 26 | + def skip_page_view | ||
| 27 | + @analytics_skip_page_view = true | ||
| 28 | + end | ||
| 29 | + | ||
| 30 | +end |
plugins/analytics/db/migrate/20150715001149_init_analytics_plugin.rb
0 → 100644
| @@ -0,0 +1,47 @@ | @@ -0,0 +1,47 @@ | ||
| 1 | +class InitAnalyticsPlugin < ActiveRecord::Migration | ||
| 2 | + | ||
| 3 | + def up | ||
| 4 | + create_table :analytics_plugin_visits do |t| | ||
| 5 | + t.integer :profile_id | ||
| 6 | + end | ||
| 7 | + | ||
| 8 | + create_table :analytics_plugin_page_views do |t| | ||
| 9 | + t.string :type | ||
| 10 | + t.integer :visit_id | ||
| 11 | + t.integer :track_id | ||
| 12 | + t.integer :referer_page_view_id | ||
| 13 | + t.string :request_id | ||
| 14 | + | ||
| 15 | + t.integer :user_id | ||
| 16 | + t.integer :session_id | ||
| 17 | + t.integer :profile_id | ||
| 18 | + | ||
| 19 | + t.text :url | ||
| 20 | + t.text :referer_url | ||
| 21 | + | ||
| 22 | + t.text :user_agent | ||
| 23 | + t.string :remote_ip | ||
| 24 | + | ||
| 25 | + t.datetime :request_started_at | ||
| 26 | + t.datetime :request_finished_at | ||
| 27 | + t.datetime :page_loaded_at | ||
| 28 | + t.integer :time_on_page, default: 0 | ||
| 29 | + | ||
| 30 | + t.text :data, default: {}.to_yaml | ||
| 31 | + end | ||
| 32 | + add_index :analytics_plugin_page_views, :request_id | ||
| 33 | + add_index :analytics_plugin_page_views, :referer_page_view_id | ||
| 34 | + | ||
| 35 | + add_index :analytics_plugin_page_views, :user_id | ||
| 36 | + add_index :analytics_plugin_page_views, :session_id | ||
| 37 | + add_index :analytics_plugin_page_views, :profile_id | ||
| 38 | + add_index :analytics_plugin_page_views, :url | ||
| 39 | + add_index :analytics_plugin_page_views, [:user_id, :session_id, :profile_id, :url], name: :analytics_plugin_referer_find | ||
| 40 | + end | ||
| 41 | + | ||
| 42 | + def down | ||
| 43 | + drop_table :analytics_plugin_visits | ||
| 44 | + drop_table :analytics_plugin_page_views | ||
| 45 | + end | ||
| 46 | + | ||
| 47 | +end |
| @@ -0,0 +1,15 @@ | @@ -0,0 +1,15 @@ | ||
| 1 | +module AnalyticsPlugin | ||
| 2 | + | ||
| 3 | + TimeOnPageUpdateInterval = 2.minutes * 1000 | ||
| 4 | + | ||
| 5 | + extend Noosfero::Plugin::ParentMethods | ||
| 6 | + | ||
| 7 | + def self.plugin_name | ||
| 8 | + I18n.t'analytics_plugin.lib.plugin.name' | ||
| 9 | + end | ||
| 10 | + | ||
| 11 | + def self.plugin_description | ||
| 12 | + I18n.t'analytics_plugin.lib.plugin.description' | ||
| 13 | + end | ||
| 14 | + | ||
| 15 | +end |
| @@ -0,0 +1,43 @@ | @@ -0,0 +1,43 @@ | ||
| 1 | + | ||
| 2 | +class AnalyticsPlugin::Base < Noosfero::Plugin | ||
| 3 | + | ||
| 4 | + def body_ending | ||
| 5 | + return unless profile and profile.analytics_enabled? | ||
| 6 | + lambda do | ||
| 7 | + render 'analytics_plugin/body_ending' | ||
| 8 | + end | ||
| 9 | + end | ||
| 10 | + | ||
| 11 | + def js_files | ||
| 12 | + ['analytics'].map{ |j| "javascripts/#{j}" } | ||
| 13 | + end | ||
| 14 | + | ||
| 15 | + def application_controller_filters | ||
| 16 | + [{ | ||
| 17 | + type: 'around_filter', options: {}, block: -> &block do | ||
| 18 | + request_started_at = Time.now | ||
| 19 | + block.call | ||
| 20 | + request_finished_at = Time.now | ||
| 21 | + | ||
| 22 | + return if @analytics_skip_page_view | ||
| 23 | + return unless profile and profile.analytics_enabled? | ||
| 24 | + | ||
| 25 | + Scheduler::Defer.later 'analytics: register page view' do | ||
| 26 | + page_view = profile.page_views.build request: request, profile_id: profile, | ||
| 27 | + request_started_at: request_started_at, request_finished_at: request_finished_at | ||
| 28 | + | ||
| 29 | + unless profile.analytics_anonymous? | ||
| 30 | + # FIXME: use session.id in Rails 4 | ||
| 31 | + session_id = Marshal.load(Base64.decode64 request['_session_id'])['session_id'] rescue nil | ||
| 32 | + #session_id = request.session_options[:id] | ||
| 33 | + page_view.user = user | ||
| 34 | + page_view.session_id = session_id | ||
| 35 | + end | ||
| 36 | + | ||
| 37 | + page_view.save! | ||
| 38 | + end | ||
| 39 | + end, | ||
| 40 | + }] | ||
| 41 | + end | ||
| 42 | + | ||
| 43 | +end |
| @@ -0,0 +1,30 @@ | @@ -0,0 +1,30 @@ | ||
| 1 | +require_dependency 'profile' | ||
| 2 | +require_dependency 'community' | ||
| 3 | + | ||
| 4 | +([Profile] + Profile.descendants).each do |subclass| | ||
| 5 | +subclass.class_eval do | ||
| 6 | + | ||
| 7 | + has_many :visits, foreign_key: :profile_id, class_name: 'AnalyticsPlugin::Visit' | ||
| 8 | + has_many :page_views, foreign_key: :profile_id, class_name: 'AnalyticsPlugin::PageView' | ||
| 9 | + | ||
| 10 | +end | ||
| 11 | +end | ||
| 12 | + | ||
| 13 | +class Profile | ||
| 14 | + | ||
| 15 | + def analytics_settings attrs = {} | ||
| 16 | + @analytics_settings ||= Noosfero::Plugin::Settings.new self, AnalyticsPlugin, attrs | ||
| 17 | + attrs.each{ |a, v| @analytics_settings.send "#{a}=", v } | ||
| 18 | + @analytics_settings | ||
| 19 | + end | ||
| 20 | + alias_method :analytics_settings=, :analytics_settings | ||
| 21 | + | ||
| 22 | + def analytics_enabled? | ||
| 23 | + self.analytics_settings.enabled | ||
| 24 | + end | ||
| 25 | + | ||
| 26 | + def analytics_anonymous? | ||
| 27 | + self.analytics_settings.anonymous | ||
| 28 | + end | ||
| 29 | + | ||
| 30 | +end |
| @@ -0,0 +1,67 @@ | @@ -0,0 +1,67 @@ | ||
| 1 | +class AnalyticsPlugin::PageView < ActiveRecord::Base | ||
| 2 | + | ||
| 3 | + serialize :data | ||
| 4 | + | ||
| 5 | + attr_accessible *self.column_names | ||
| 6 | + attr_accessible :user, :profile | ||
| 7 | + | ||
| 8 | + attr_accessor :request | ||
| 9 | + attr_accessible :request | ||
| 10 | + | ||
| 11 | + acts_as_having_settings field: :options | ||
| 12 | + | ||
| 13 | + belongs_to :visit, class_name: 'AnalyticsPlugin::Visit' | ||
| 14 | + belongs_to :referer_page_view, class_name: 'AnalyticsPlugin::PageView' | ||
| 15 | + | ||
| 16 | + belongs_to :user, class_name: 'Person' | ||
| 17 | + belongs_to :session, primary_key: :session_id, foreign_key: :session_id, class_name: 'Session' | ||
| 18 | + belongs_to :profile | ||
| 19 | + | ||
| 20 | + validates_presence_of :visit | ||
| 21 | + validates_presence_of :request, on: :create | ||
| 22 | + validates_presence_of :url | ||
| 23 | + | ||
| 24 | + before_validation :extract_request_data, on: :create | ||
| 25 | + before_validation :fill_referer_page_view, on: :create | ||
| 26 | + before_validation :fill_visit, on: :create | ||
| 27 | + | ||
| 28 | + def request_duration | ||
| 29 | + self.request_finished_at - self.request_started_at | ||
| 30 | + end | ||
| 31 | + | ||
| 32 | + def page_load! | ||
| 33 | + self.page_loaded_at = Time.now | ||
| 34 | + self.update_column :page_loaded_at, self.page_loaded_at | ||
| 35 | + end | ||
| 36 | + | ||
| 37 | + def increase_time_on_page! | ||
| 38 | + now = Time.now | ||
| 39 | + initial_time = self.page_loaded_at || self.request_finished_at | ||
| 40 | + return unless now > initial_time | ||
| 41 | + | ||
| 42 | + self.time_on_page = now - initial_time | ||
| 43 | + self.update_column :time_on_page, self.time_on_page | ||
| 44 | + end | ||
| 45 | + | ||
| 46 | + protected | ||
| 47 | + | ||
| 48 | + def extract_request_data | ||
| 49 | + self.url = self.request.url.sub /\/+$/, '' | ||
| 50 | + self.referer_url = self.request.referer | ||
| 51 | + self.user_agent = self.request.headers['User-Agent'] | ||
| 52 | + self.request_id = self.request.env['action_dispatch.request_id'] | ||
| 53 | + self.remote_ip = self.request.remote_ip | ||
| 54 | + end | ||
| 55 | + | ||
| 56 | + def fill_referer_page_view | ||
| 57 | + self.referer_page_view = AnalyticsPlugin::PageView.order('request_started_at DESC'). | ||
| 58 | + where(url: self.referer_url, session_id: self.session_id, user_id: self.user_id, profile_id: self.profile_id).first if self.referer_url.present? | ||
| 59 | + end | ||
| 60 | + | ||
| 61 | + def fill_visit | ||
| 62 | + self.visit = self.referer_page_view.visit if self.referer_page_view | ||
| 63 | + self.visit ||= AnalyticsPlugin::Visit.new profile: profile | ||
| 64 | + end | ||
| 65 | + | ||
| 66 | +end | ||
| 67 | + |
| @@ -0,0 +1,11 @@ | @@ -0,0 +1,11 @@ | ||
| 1 | +class AnalyticsPlugin::Visit < ActiveRecord::Base | ||
| 2 | + | ||
| 3 | + attr_accessible *self.column_names | ||
| 4 | + attr_accessible :profile | ||
| 5 | + | ||
| 6 | + default_scope -> { includes :page_views } | ||
| 7 | + | ||
| 8 | + belongs_to :profile | ||
| 9 | + has_many :page_views, class_name: 'AnalyticsPlugin::PageView', dependent: :destroy | ||
| 10 | + | ||
| 11 | +end |
| @@ -0,0 +1,29 @@ | @@ -0,0 +1,29 @@ | ||
| 1 | +# translation of analytic.po to portuguese | ||
| 2 | +# Krishnamurti Lelis Lima Vieira Nunes <krishna@colivre.coop.br>, 2007. | ||
| 3 | +# noosfero - Brazilian Portuguese translation | ||
| 4 | +# Copyright (C) 2007, | ||
| 5 | +# Forum Brasileiro de Economia Solidaria <http://www.fbes.org.br/> | ||
| 6 | +# Copyright (C) 2007, | ||
| 7 | +# Ynternet.org Foundation <http://www.ynternet.org/> | ||
| 8 | +# This file is distributed under the same license as noosfero itself. | ||
| 9 | +# Joenio Costa <joenio@colivre.coop.br>, 2008. | ||
| 10 | +# | ||
| 11 | +# | ||
| 12 | +msgid "" | ||
| 13 | +msgstr "" | ||
| 14 | +"Project-Id-Version: 1.0-690-gcb6e853\n" | ||
| 15 | +"POT-Creation-Date: 2015-03-05 12:10-0300\n" | ||
| 16 | +"PO-Revision-Date: 2015-07-21 09:23-0300\n" | ||
| 17 | +"Last-Translator: Michal Čihař <michal@cihar.com>\n" | ||
| 18 | +"Language-Team: Portuguese <https://hosted.weblate.org/projects/noosfero" | ||
| 19 | +"/plugin-solr/pt/>\n" | ||
| 20 | +"Language: pt\n" | ||
| 21 | +"MIME-Version: 1.0\n" | ||
| 22 | +"Content-Type: text/plain; charset=UTF-8\n" | ||
| 23 | +"Content-Transfer-Encoding: 8bit\n" | ||
| 24 | +"Plural-Forms: nplurals=2; plural=n != 1;\n" | ||
| 25 | +"X-Generator: Weblate 2.3-dev\n" | ||
| 26 | + | ||
| 27 | +msgid "Select the set of communities and users to track" | ||
| 28 | +msgstr "Seleciona o conjunto de comunidades e usuários para rastrear" | ||
| 29 | + |
| @@ -0,0 +1,39 @@ | @@ -0,0 +1,39 @@ | ||
| 1 | +analytics = { | ||
| 2 | + requestId: '', | ||
| 3 | + | ||
| 4 | + timeOnPage: { | ||
| 5 | + updateInterval: 0, | ||
| 6 | + baseUrl: '', | ||
| 7 | + | ||
| 8 | + report: function() { | ||
| 9 | + $.ajax(analytics.timeOnPage.baseUrl+'/report', { | ||
| 10 | + type: 'POST', data: {id: analytics.requestId}, | ||
| 11 | + success: function(data) { | ||
| 12 | + | ||
| 13 | + analytics.timeOnPage.poll() | ||
| 14 | + }, | ||
| 15 | + }) | ||
| 16 | + }, | ||
| 17 | + | ||
| 18 | + poll: function() { | ||
| 19 | + if (analytics.timeOnPage.updateInterval) | ||
| 20 | + setTimeout(analytics.timeOnPage.report, analytics.timeOnPage.updateInterval) | ||
| 21 | + }, | ||
| 22 | + }, | ||
| 23 | + | ||
| 24 | + init: function() { | ||
| 25 | + analytics.timeOnPage.poll() | ||
| 26 | + }, | ||
| 27 | + | ||
| 28 | + pageLoad: function() { | ||
| 29 | + $.ajax(analytics.timeOnPage.baseUrl+'/page_load', { | ||
| 30 | + type: 'POST', data: {id: analytics.requestId}, | ||
| 31 | + success: function(data) { | ||
| 32 | + }, | ||
| 33 | + }); | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | +}; | ||
| 37 | + | ||
| 38 | +$(document).ready(analytics.pageLoad) | ||
| 39 | + |
plugins/analytics/test/functional/content_viewer_controller_test.rb
0 → 100644
| @@ -0,0 +1,48 @@ | @@ -0,0 +1,48 @@ | ||
| 1 | +require 'test_helper' | ||
| 2 | +require 'content_viewer_controller' | ||
| 3 | + | ||
| 4 | +class ContentViewerControllerTest < ActionController::TestCase | ||
| 5 | + | ||
| 6 | + def setup | ||
| 7 | + @controller = ContentViewerController.new | ||
| 8 | + @request = ActionController::TestRequest.new | ||
| 9 | + @response = ActionController::TestResponse.new | ||
| 10 | + | ||
| 11 | + @environment = Environment.default | ||
| 12 | + @environment.enabled_plugins += ['AnalyticsPlugin'] | ||
| 13 | + @environment.save! | ||
| 14 | + | ||
| 15 | + @user = create_user('testinguser').person | ||
| 16 | + login_as @user.identifier | ||
| 17 | + | ||
| 18 | + @community = build Community, identifier: 'testcomm', name: 'test' | ||
| 19 | + @community.analytics_settings.enabled = true | ||
| 20 | + @community.analytics_settings.anonymous = false | ||
| 21 | + @community.save! | ||
| 22 | + @community.add_member @user | ||
| 23 | + end | ||
| 24 | + | ||
| 25 | + should 'register page view correctly' do | ||
| 26 | + @request.env['HTTP_REFERER'] = 'http://google.com' | ||
| 27 | + first_url = 'http://test.host' | ||
| 28 | + get :view_page, profile: @community.identifier, page: [] | ||
| 29 | + assert_equal 1, @community.page_views.count | ||
| 30 | + assert_equal 1, @community.visits.count | ||
| 31 | + | ||
| 32 | + first_page_view = @community.page_views.order(:id).first | ||
| 33 | + assert_equal @request.referer, first_page_view.referer_url | ||
| 34 | + | ||
| 35 | + @request.env['HTTP_REFERER'] = first_url | ||
| 36 | + get :view_page, profile: @community.identifier, page: @community.articles.last.path.split('/') | ||
| 37 | + assert_equal 2, @community.page_views.count | ||
| 38 | + assert_equal 1, @community.visits.count | ||
| 39 | + | ||
| 40 | + second_page_view = @community.page_views.order(:id).last | ||
| 41 | + assert_equal first_page_view, second_page_view.referer_page_view | ||
| 42 | + | ||
| 43 | + assert_equal @user, second_page_view.user | ||
| 44 | + | ||
| 45 | + assert second_page_view.request_duration > 0 and second_page_view.request_duration < 1 | ||
| 46 | + end | ||
| 47 | + | ||
| 48 | +end |
plugins/analytics/views/analytics_plugin/_body_ending.html.slim
0 → 100644
| @@ -0,0 +1,6 @@ | @@ -0,0 +1,6 @@ | ||
| 1 | +javascript: | ||
| 2 | + analytics.timeOnPage.baseUrl = #{url_for(controller: 'analytics_plugin/time_on_page').to_json} | ||
| 3 | + analytics.timeOnPage.updateInterval = #{AnalyticsPlugin::TimeOnPageUpdateInterval.to_json} | ||
| 4 | + analytics.requestId = #{request.env['action_dispatch.request_id'].to_json} | ||
| 5 | + analytics.init() | ||
| 6 | + |