From e6e280c73bf9f1e95ddf2213e08e10afe3db0c4d Mon Sep 17 00:00:00 2001 From: rulingcom Date: Wed, 29 Jul 2026 18:08:04 +0800 Subject: [PATCH] Add click tracking and display period to web resource links --- .../admin/web_resources_controller.rb | 2 +- app/controllers/web_resources_controller.rb | 15 +++++++++++++-- app/models/web_link.rb | 9 ++++++--- app/views/admin/web_resources/_form.html.erb | 18 +++++++++++++++++- app/views/admin/web_resources/_index.html.erb | 4 +++- config/routes.rb | 2 ++ 6 files changed, 42 insertions(+), 8 deletions(-) diff --git a/app/controllers/admin/web_resources_controller.rb b/app/controllers/admin/web_resources_controller.rb index ab03f6a..a37345d 100644 --- a/app/controllers/admin/web_resources_controller.rb +++ b/app/controllers/admin/web_resources_controller.rb @@ -6,7 +6,7 @@ class Admin::WebResourcesController < OrbitAdminController @tags = @module_app.tags @categories = @module_app.categories.enabled @filter_fields = filter_fields(@categories, @tags) - @table_fields = [:status, :category, :title] + @table_fields = [:status, :category, :title, "ad_banner.duration", "click_count"] if params[:sort].blank? s = {:order_position => "asc"} else diff --git a/app/controllers/web_resources_controller.rb b/app/controllers/web_resources_controller.rb index decb14c..8ee4d69 100644 --- a/app/controllers/web_resources_controller.rb +++ b/app/controllers/web_resources_controller.rb @@ -1,5 +1,6 @@ class WebResourcesController < ApplicationController require 'nokogiri' + skip_before_action :verify_authenticity_token, only: [:track_click] def index OrbitHelper.render_css_in_head(["web_resources_front"]) links = WebLink.where(:title.ne => "").can_display.filter_by_categories.filter_by_tags @@ -33,7 +34,8 @@ class WebResourcesController < ApplicationController "statuses" => statuses, "category" => link.category.title, "link_to_show" => link.url, - "target" => target + "target" => target, + "onclick" => "if(navigator.sendBeacon){navigator.sendBeacon('/web_resources/track_click/#{link.id}');}" } end { @@ -79,7 +81,8 @@ class WebResourcesController < ApplicationController "context" => nl2br(link.context.to_s), "statuses" => statuses, "link_to_show" => link.url, - "target" => target + "target" => target, + "onclick" => "if(navigator.sendBeacon){navigator.sendBeacon('/web_resources/track_click/#{link.id}');}" } end { @@ -87,4 +90,12 @@ class WebResourcesController < ApplicationController "extras" => {"widget-title"=>t(:web_resource),"more_url" => OrbitHelper.widget_more_url} } end + + def track_click + link = WebLink.find(params[:id]) + link.inc(click_count: 1) + head :ok + rescue Mongoid::Errors::DocumentNotFound + head :not_found + end end diff --git a/app/models/web_link.rb b/app/models/web_link.rb index 375e02d..b656ee2 100644 --- a/app/models/web_link.rb +++ b/app/models/web_link.rb @@ -18,12 +18,15 @@ class WebLink field :create_user_id field :update_user_id field :rss2_id, type: String + field :click_count, type: Integer, default: 0 + field :postdate, type: DateTime, default: Time.now + field :deadline, type: DateTime field :link_open, type: String LINK_OPEN_TYPES = ["local", "new_window"] - scope :can_display, ->{where(is_hidden: false).order_by([:is_top,:desc],[:order_position,:asc])} - + scope :can_display, ->{where(is_hidden: false).and(WebLink.unscoped.or({:postdate.lte=>Time.now},{:postdate=>nil}).selector,WebLink.unscoped.or({:deadline.gte=>Time.now},{:deadline=>nil}).selector).order_by([:is_top,:desc],[:order_position,:asc])} + before_save :add_http protected @@ -41,4 +44,4 @@ class WebLink self.url_translations = temp_url end -end \ No newline at end of file +end diff --git a/app/views/admin/web_resources/_form.html.erb b/app/views/admin/web_resources/_form.html.erb index 93bceff..2df9ce9 100644 --- a/app/views/admin/web_resources/_form.html.erb +++ b/app/views/admin/web_resources/_form.html.erb @@ -33,6 +33,22 @@ <%= select_category(f, @module_app) %> + + +
+ +
+ <%= f.datetime_picker :postdate, :no_label => true, :new_record => @link.new_record? %> +
+
+ + +
+ +
+ <%= f.datetime_picker :deadline, :no_label => true, :new_record => @link.new_record? %> +
+
@@ -254,4 +270,4 @@ }); }); - \ No newline at end of file + diff --git a/app/views/admin/web_resources/_index.html.erb b/app/views/admin/web_resources/_index.html.erb index 2b31329..752027f 100644 --- a/app/views/admin/web_resources/_index.html.erb +++ b/app/views/admin/web_resources/_index.html.erb @@ -22,6 +22,8 @@ + <%= format_value link.postdate %> ~ <%= format_value link.deadline %> + <%= link.click_count %> <% end %> @@ -31,4 +33,4 @@ content_tag(:div, paginate(@links), class: "pagination pagination-centered") + content_tag(:div, link_to(t(:new_),new_admin_web_resource_path, :class=>"btn btn-primary"), class: "pull-right") end -%> \ No newline at end of file +%> diff --git a/config/routes.rb b/config/routes.rb index e82c7dc..8307d89 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,7 @@ Rails.application.routes.draw do +post 'web_resources/track_click/:id', to: 'web_resources#track_click', as: 'web_resource_track_click' + locales = Site.find_by(site_active: true).in_use_locales rescue I18n.available_locales scope "(:locale)", locale: Regexp.new(locales.join("|")) do