mirror of
https://github.com/discourse/discourse.git
synced 2025-06-06 23:07:28 +08:00
FIX: Count clicks on links with query params (#15969)
This did not work sometimes if a topic had the same URL with and without query params because it did not try to select the best matching URL.
This commit is contained in:
@ -70,17 +70,17 @@ class TopicLinkClick < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
link = TopicLink.select([:id, :user_id])
|
|
||||||
|
|
||||||
# test for all possible URLs
|
# test for all possible URLs
|
||||||
link = link.where(Array.new(urls.count, "url = ?").join(" OR "), *urls)
|
link = TopicLink.where(url: urls)
|
||||||
|
|
||||||
# Find the forum topic link
|
# Find the forum topic link
|
||||||
link = link.where(post_id: args[:post_id]) if args[:post_id].present?
|
link = link.where(post_id: args[:post_id]) if args[:post_id].present?
|
||||||
|
|
||||||
# If we don't have a post, just find the first occurrence of the link
|
# If we don't have a post, just find the first occurrence of the link
|
||||||
link = link.where(topic_id: args[:topic_id]) if args[:topic_id].present?
|
link = link.where(topic_id: args[:topic_id]) if args[:topic_id].present?
|
||||||
link = link.first
|
|
||||||
|
# select the TopicLink associated to first url
|
||||||
|
link = link.order("array_position(ARRAY[#{urls.map { |s| "#{ActiveRecord::Base.connection.quote(s)}" }.join(',')}], url::text)").first
|
||||||
|
|
||||||
# If no link is found...
|
# If no link is found...
|
||||||
unless link.present?
|
unless link.present?
|
||||||
|
@ -238,6 +238,27 @@ describe TopicLinkClick do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'same base URL with different query' do
|
||||||
|
it 'are handled differently' do
|
||||||
|
post = Fabricate(:post, raw: <<~RAW)
|
||||||
|
no query param: http://example.com/a
|
||||||
|
with query param: http://example.com/a?b=c
|
||||||
|
with two query params: http://example.com/a?b=c&d=e
|
||||||
|
RAW
|
||||||
|
|
||||||
|
TopicLink.extract_from(post)
|
||||||
|
|
||||||
|
TopicLinkClick.create_from(url: 'http://example.com/a', post_id: post.id, ip: '127.0.0.1', user: Fabricate(:user))
|
||||||
|
TopicLinkClick.create_from(url: 'http://example.com/a?b=c', post_id: post.id, ip: '127.0.0.2', user: Fabricate(:user))
|
||||||
|
TopicLinkClick.create_from(url: 'http://example.com/a?b=c&d=e', post_id: post.id, ip: '127.0.0.3', user: Fabricate(:user))
|
||||||
|
TopicLinkClick.create_from(url: 'http://example.com/a?b=c', post_id: post.id, ip: '127.0.0.4', user: Fabricate(:user))
|
||||||
|
|
||||||
|
expect(TopicLink.where("url LIKE '%example.com%'").pluck(:url, :clicks)).to contain_exactly(
|
||||||
|
['http://example.com/a', 1], ['http://example.com/a?b=c', 2], ['http://example.com/a?b=c&d=e', 1]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'with a google analytics tracking code and a hash' do
|
context 'with a google analytics tracking code and a hash' do
|
||||||
before do
|
before do
|
||||||
@url = TopicLinkClick.create_from(url: 'http://discourse.org?_ga=1.16846778.221554446.1071987018#faq',
|
@url = TopicLinkClick.create_from(url: 'http://discourse.org?_ga=1.16846778.221554446.1071987018#faq',
|
||||||
|
Reference in New Issue
Block a user