diff --git a/app/models/topic_link_click.rb b/app/models/topic_link_click.rb index 85168975a51..1e98ec4615a 100644 --- a/app/models/topic_link_click.rb +++ b/app/models/topic_link_click.rb @@ -34,6 +34,12 @@ class TopicLinkClick < ActiveRecord::Base urls << url[0..query - 1] + url[endpos..-1] end + # link can have query params, and analytics can add more to the end: + i = url.length + while i = url.rindex('&', i-1) + urls << url[0...i] + end + # add a cdn link if uri if Discourse.asset_host.present? diff --git a/spec/models/topic_link_click_spec.rb b/spec/models/topic_link_click_spec.rb index 3499c63c106..f4953f014d3 100644 --- a/spec/models/topic_link_click_spec.rb +++ b/spec/models/topic_link_click_spec.rb @@ -192,6 +192,32 @@ describe TopicLinkClick do end end + context 'with a query param and google analytics' do + before do + @topic = Fabricate(:topic) + @post = Fabricate(:post, + topic: @topic, + user: @topic.user, + raw: "Here's a link to twitter: http://twitter.com?ref=forum" + ) + TopicLink.extract_from(@post) + @topic_link = @topic.topic_links.first + end + + it 'creates a click' do + url = TopicLinkClick.create_from( + url: 'http://twitter.com?ref=forum&_ga=1.16846778.221554446.1071987018', + topic_id: @topic.id, + post_id: @post.id, + ip: '127.0.0.3' + ) + click = TopicLinkClick.last + expect(click).to be_present + expect(click.topic_link).to eq(@topic_link) + expect(url).to eq('http://twitter.com?ref=forum&_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',