From 1468616465b720269225d3be590b6944c78aa70d Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 23 Aug 2016 12:08:37 -0400 Subject: [PATCH] FIX: Support links with google analytics tracking and hashes --- app/models/topic_link_click.rb | 7 ++++++- spec/fabricators/post_fabricator.rb | 1 + spec/models/topic_link_click_spec.rb | 30 ++++++++++++++++++++++++++++ spec/models/topic_link_spec.rb | 2 +- 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/app/models/topic_link_click.rb b/app/models/topic_link_click.rb index e6f4d59ea07..6bd932c796f 100644 --- a/app/models/topic_link_click.rb +++ b/app/models/topic_link_click.rb @@ -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 diff --git a/spec/fabricators/post_fabricator.rb b/spec/fabricators/post_fabricator.rb index 520271a6cfd..e462230efc6 100644 --- a/spec/fabricators/post_fabricator.rb +++ b/spec/fabricators/post_fabricator.rb @@ -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 diff --git a/spec/models/topic_link_click_spec.rb b/spec/models/topic_link_click_spec.rb index 0709a0a0b34..6200c24a079 100644 --- a/spec/models/topic_link_click_spec.rb +++ b/spec/models/topic_link_click_spec.rb @@ -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') diff --git a/spec/models/topic_link_spec.rb b/spec/models/topic_link_spec.rb index 314276a26dd..6eb802dd0cd 100644 --- a/spec/models/topic_link_spec.rb +++ b/spec/models/topic_link_spec.rb @@ -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