FIX: Support links with google analytics tracking and hashes

This commit is contained in:
Robin Ward 2016-08-23 12:08:37 -04:00
parent e06be6561d
commit 1468616465
4 changed files with 38 additions and 2 deletions

View File

@ -27,7 +27,12 @@ class TopicLinkClick < ActiveRecord::Base
end
urls << UrlHelper.absolute_without_cdn(url)
urls << uri.path if uri.try(:host) == Discourse.current_hostname
urls << url.sub(/\?.*$/, '') if url.include?('?')
query = url.index('?')
unless query.nil?
endpos = url.index('#') || url.size
urls << url[0..query-1] + url[endpos..-1]
end
# add a cdn link
if uri

View File

@ -114,6 +114,7 @@ And a link to google: http://google.com
And a secure link to google: https://google.com
And a markdown link: [forumwarz](http://forumwarz.com)
And a markdown link with a period after it [codinghorror](http://www.codinghorror.com/blog).
And one with a hash http://discourse.org#faq
"
end

View File

@ -177,6 +177,36 @@ describe TopicLinkClick do
end
end
context 'with a google analytics tracking code' do
before do
@url = TopicLinkClick.create_from(url: 'http://twitter.com?_ga=1.16846778.221554446.1071987018',
topic_id: @topic.id,
ip: '127.0.0.3')
@click = TopicLinkClick.last
end
it 'creates a click' do
expect(@click).to be_present
expect(@click.topic_link).to eq(@topic_link)
expect(@url).to eq('http://twitter.com?_ga=1.16846778.221554446.1071987018')
end
end
context 'with a google analytics tracking code and a hash' do
before do
@url = TopicLinkClick.create_from(url: 'http://discourse.org?_ga=1.16846778.221554446.1071987018#faq',
topic_id: @topic.id,
ip: '127.0.0.3')
@click = TopicLinkClick.last
end
it 'creates a click' do
expect(@click).to be_present
expect(@url).to eq('http://discourse.org?_ga=1.16846778.221554446.1071987018#faq')
end
end
context 'with a valid url and topic_id' do
before do
@url = TopicLinkClick.create_from(url: @topic_link.url, topic_id: @topic.id, ip: '127.0.0.3')

View File

@ -304,7 +304,7 @@ http://b.com/#{'a'*500}
expect(counts_for[post.id].first[:clicks]).to eq(1)
array = TopicLink.topic_map(Guardian.new, post.topic_id)
expect(array.length).to eq(5)
expect(array.length).to eq(6)
expect(array[0]["clicks"]).to eq("1")
end