Add click tracking and display period to web resource links

This commit is contained in:
rulingcom 2026-07-29 18:08:04 +08:00
parent 76d317d9bb
commit e6e280c73b
6 changed files with 42 additions and 8 deletions

View File

@ -6,7 +6,7 @@ class Admin::WebResourcesController < OrbitAdminController
@tags = @module_app.tags @tags = @module_app.tags
@categories = @module_app.categories.enabled @categories = @module_app.categories.enabled
@filter_fields = filter_fields(@categories, @tags) @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? if params[:sort].blank?
s = {:order_position => "asc"} s = {:order_position => "asc"}
else else

View File

@ -1,5 +1,6 @@
class WebResourcesController < ApplicationController class WebResourcesController < ApplicationController
require 'nokogiri' require 'nokogiri'
skip_before_action :verify_authenticity_token, only: [:track_click]
def index def index
OrbitHelper.render_css_in_head(["web_resources_front"]) OrbitHelper.render_css_in_head(["web_resources_front"])
links = WebLink.where(:title.ne => "").can_display.filter_by_categories.filter_by_tags links = WebLink.where(:title.ne => "").can_display.filter_by_categories.filter_by_tags
@ -33,7 +34,8 @@ class WebResourcesController < ApplicationController
"statuses" => statuses, "statuses" => statuses,
"category" => link.category.title, "category" => link.category.title,
"link_to_show" => link.url, "link_to_show" => link.url,
"target" => target "target" => target,
"onclick" => "if(navigator.sendBeacon){navigator.sendBeacon('/web_resources/track_click/#{link.id}');}"
} }
end end
{ {
@ -79,7 +81,8 @@ class WebResourcesController < ApplicationController
"context" => nl2br(link.context.to_s), "context" => nl2br(link.context.to_s),
"statuses" => statuses, "statuses" => statuses,
"link_to_show" => link.url, "link_to_show" => link.url,
"target" => target "target" => target,
"onclick" => "if(navigator.sendBeacon){navigator.sendBeacon('/web_resources/track_click/#{link.id}');}"
} }
end end
{ {
@ -87,4 +90,12 @@ class WebResourcesController < ApplicationController
"extras" => {"widget-title"=>t(:web_resource),"more_url" => OrbitHelper.widget_more_url} "extras" => {"widget-title"=>t(:web_resource),"more_url" => OrbitHelper.widget_more_url}
} }
end 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 end

View File

@ -18,12 +18,15 @@ class WebLink
field :create_user_id field :create_user_id
field :update_user_id field :update_user_id
field :rss2_id, type: String 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 field :link_open, type: String
LINK_OPEN_TYPES = ["local", "new_window"] 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 before_save :add_http
protected protected
@ -41,4 +44,4 @@ class WebLink
self.url_translations = temp_url self.url_translations = temp_url
end end
end end

View File

@ -33,6 +33,22 @@
<%= select_category(f, @module_app) %> <%= select_category(f, @module_app) %>
</div> </div>
</div> </div>
<!-- Date Time Picker -->
<div class="control-group">
<label class="control-label muted"><%= t(:start_date) %></label>
<div class="controls">
<%= f.datetime_picker :postdate, :no_label => true, :new_record => @link.new_record? %>
</div>
</div>
<!-- Date Time Picker -->
<div class="control-group">
<label class="control-label muted"><%= t(:end_date) %></label>
<div class="controls">
<%= f.datetime_picker :deadline, :no_label => true, :new_record => @link.new_record? %>
</div>
</div>
</div> </div>
@ -254,4 +270,4 @@
}); });
}); });
</script> </script>
</div> </div>

View File

@ -22,6 +22,8 @@
</ul> </ul>
</div> </div>
</td> </td>
<td><%= format_value link.postdate %> ~ <%= format_value link.deadline %></td>
<td><%= link.click_count %></td>
</tr> </tr>
<% end %> <% end %>
</tbody> </tbody>
@ -31,4 +33,4 @@
content_tag(:div, paginate(@links), class: "pagination pagination-centered") + 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") content_tag(:div, link_to(t(:new_),new_admin_web_resource_path, :class=>"btn btn-primary"), class: "pull-right")
end end
%> %>

View File

@ -1,5 +1,7 @@
Rails.application.routes.draw do 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 locales = Site.find_by(site_active: true).in_use_locales rescue I18n.available_locales
scope "(:locale)", locale: Regexp.new(locales.join("|")) do scope "(:locale)", locale: Regexp.new(locales.join("|")) do