Add click tracking for banner links

This commit is contained in:
rulingcom 2026-07-29 16:17:05 +08:00
parent 6bfcc946a4
commit 06c6a100ce
7 changed files with 18 additions and 5 deletions

View File

@ -1,4 +1,5 @@
class AdBannersController < ApplicationController
skip_before_action :verify_authenticity_token, only: [:track_click]
def self.custom_widget_data
ac = ActionController::Base.new
ac.instance_variable_set(:@custom_data_field, @custom_data_field)
@ -15,6 +16,14 @@ class AdBannersController < ApplicationController
end
end
def track_click
ad_image = AdImage.find(params[:id])
ad_image.inc(click_count: 1)
head :ok
rescue Mongoid::Errors::DocumentNotFound
head :not_found
end
private
def image_widget(adbanner)
@ -34,7 +43,7 @@ class AdBannersController < ApplicationController
target = ""
end
if !b.out_link.blank?
image_link += "\" style=\"cursor:pointer;\" onload=\"var el=this;(function(){if((el.parentElement==undefined||el.parentElement.tagName!='A')&&$('<p>'+$(el).parents('.w-ba-banner__slide').eq(0).attr('data-overlay-template')+'</p>').find('a').length==0){el.outerHTML=('<a href=#{b.out_link} target=#{target}>'+el.outerHTML+'</a>');}})()\""
image_link += "\" style=\"cursor:pointer;\" onclick=\"if(navigator.sendBeacon){navigator.sendBeacon('/ad_banners/track_click/#{b.id}');}\" onload=\"var el=this;(function(){if((el.parentElement==undefined||el.parentElement.tagName!='A')&&$('<p>'+$(el).parents('.w-ba-banner__slide').eq(0).attr('data-overlay-template')+'</p>').find('a').length==0){el.outerHTML=('<a href=#{b.out_link} target=#{target}>'+el.outerHTML+'</a>');}})()\""
end
context = (b.context.tr('"',"'") rescue "")
images << {

View File

@ -24,7 +24,7 @@ class Admin::AdBannersController < OrbitAdminController
else
@images = @banner.ad_images.not_expired.order_by(is_top: -1, sort_number: 1).page(params[:page]).per(10)
end
@table_fields = [:banner, :title, "ad_banner.duration", :link]
@table_fields = [:banner, :title, "ad_banner.duration", :link, "click_count"]
end
def new

View File

@ -60,4 +60,4 @@ class Admin::AdImagesController < Admin::AdBannersController
def setup_vars
@module_app = ModuleApp.where(:key => "ad_banner").first
end
end
end

View File

@ -22,6 +22,7 @@ class AdImage
field :is_top, :type => Boolean, :default => false
field :language_enabled, :type => Array, :default => ["en","zh_tw"]
field :exchange_item, :default => "1"
field :click_count, type: Integer, default: 0
LINK_OPEN_TYPES = ["local", "new_window"]
belongs_to :banner

View File

@ -94,7 +94,8 @@
<% if image.out_link != "" %>
<a href="<%= image.out_link rescue "#" %>" target="_blank">Link</a>
<% end %>
</td>
</td>
<td><%= image.click_count %></td>
</tr>
<% end %>
</tbody>

View File

@ -46,4 +46,5 @@ zh_tw:
type: 圖片 或 影片
choose_type: 請先選擇類型
select: 請選擇
video: 影片
click_count: "點擊次數"
video: 影片

View File

@ -1,6 +1,7 @@
Rails.application.routes.draw do
require 'fileutils'
get 'ad_banners/widget'
post 'ad_banners/track_click/:id', to: 'ad_banners#track_click', as: 'ad_banner_track_click'
if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console')
Thread.new do
s = Site.first