Merge pull request #667 from ZogStriP/dont-track-links-in-oneboxes

don't track links in oneboxes
This commit is contained in:
Sam 2013-04-07 18:30:24 -07:00
commit 9ad320768e
13 changed files with 39 additions and 41 deletions

View File

@ -14,38 +14,39 @@ Discourse.ClickTrack = {
@param {jQuery.Event} e The click event that occurred
**/
trackClick: function(e) {
var $a, $article, $badge, count, destination, href, ownLink, postId, topicId, trackingUrl, userId;
$a = $(e.currentTarget);
if ($a.hasClass('lightbox')) {
return;
}
var $link = $(e.currentTarget);
if ($link.hasClass('lightbox')) return true;
e.preventDefault();
// We don't track clicks on quote back buttons
if ($a.hasClass('back') || $a.hasClass('quote-other-topic')) return true;
if ($link.hasClass('back') || $link.hasClass('quote-other-topic')) return true;
// We don't track clicks in oneboxes
// except when we force it with the "track-link" class
if ($link.closest('.onebox-result') && !$link.hasClass('track-link')) return true;
// Remove the href, put it as a data attribute
if (!$a.data('href')) {
$a.addClass('no-href');
$a.data('href', $a.attr('href'));
$a.attr('href', null);
if (!$link.data('href')) {
$link.addClass('no-href');
$link.data('href', $link.attr('href'));
$link.attr('href', null);
// Don't route to this URL
$a.data('auto-route', true);
$link.data('auto-route', true);
}
href = $a.data('href');
$article = $a.closest('article');
postId = $article.data('post-id');
topicId = $('#topic').data('topic-id');
userId = $a.data('user-id');
if (!userId) {
userId = $article.data('user-id');
}
ownLink = userId && (userId === Discourse.get('currentUser.id'));
var href = $link.data('href'),
$article = $link.closest('article'),
postId = $article.data('post-id'),
topicId = $('#topic').data('topic-id'),
userId = $link.data('user-id');
if (!userId) userId = $article.data('user-id');
var ownLink = userId && (userId === Discourse.get('currentUser.id'));
// Build a Redirect URL
trackingUrl = Discourse.getURL("/clicks/track?url=" + encodeURIComponent(href));
if (postId && (!$a.data('ignore-post-id'))) {
var trackingUrl = Discourse.getURL("/clicks/track?url=" + encodeURIComponent(href));
if (postId && (!$link.data('ignore-post-id'))) {
trackingUrl += "&post_id=" + encodeURI(postId);
}
if (topicId) {
@ -54,23 +55,21 @@ Discourse.ClickTrack = {
// Update badge clicks unless it's our own
if (!ownLink) {
$badge = $('span.badge', $a);
var $badge = $('span.badge', $link);
if ($badge.length === 1) {
count = parseInt($badge.html(), 10);
$badge.html(count + 1);
$badge.html(parseInt($badge.html(), 10) + 1);
}
}
// If they right clicked, change the destination href
if (e.which === 3) {
destination = Discourse.SiteSettings.track_external_right_clicks ? trackingUrl : href;
$a.attr('href', destination);
var destination = Discourse.SiteSettings.track_external_right_clicks ? trackingUrl : href;
$link.attr('href', destination);
return true;
}
// if they want to open in a new tab, do an AJAX request
if (e.metaKey || e.ctrlKey || e.which === 2) {
Discourse.ajax(Discourse.getURL("/clicks/track"), {
data: {
url: href,
@ -101,8 +100,7 @@ Discourse.ClickTrack = {
if (Discourse.get('currentUser.external_links_in_new_tab')) {
var win = window.open(trackingUrl, '_blank');
win.focus();
}
else {
} else {
window.location = trackingUrl;
}

View File

@ -1,6 +1,6 @@
<div class='onebox-result'>
{{#host}}
<a href='{{original_url}}' class='source' target="_blank">
<a href='{{original_url}}' class='source track-link' target="_blank">
{{#favicon}}<img class='favicon' src="{{favicon}}"> {{/favicon}}{{host}}
</a>
{{/host}}

View File

@ -1,6 +1,6 @@
<div class='onebox-result'>
{{#host}}
<a href='{{original_url}}' class='source' target="_blank">
<a href='{{original_url}}' class='source track-link' target="_blank">
{{#favicon}}<img class='favicon' src="{{favicon}}"> {{/favicon}}{{host}}
</a>
{{/host}}

View File

@ -2,7 +2,7 @@
{{#host}}
<div class="source">
<div class="info">
<a href="{{html_url}}" target="_blank">
<a href="{{html_url}}" class="track-link" target="_blank">
{{#favicon}}<img class="favicon" src="{{favicon}}"> {{/favicon}}{{host}}
</a>
</div>

View File

@ -2,7 +2,7 @@
{{#host}}
<div class="source">
<div class="info">
<a href='{{original_url}}' class='source' target="_blank">
<a href='{{original_url}}' class='source track-link' target="_blank">
{{#favicon}}<img class='favicon' src="{{favicon}}"> {{/favicon}}{{host}}
</a>
</div>

View File

@ -2,7 +2,7 @@
{{#host}}
<div class='source'>
<div class='info'>
<a href='{{original_url}}' target="_blank">
<a href='{{original_url}}' class="track-link" target="_blank">
{{#favicon}}<img class='favicon' src="{{favicon}}"> {{/favicon}}{{host}}
</a>
</div>

View File

@ -2,7 +2,7 @@
{{#host}}
<div class="source">
<div class="info">
<a href='{{original_url}}' target="_blank">
<a href='{{original_url}}' class="track-link" target="_blank">
{{#favicon}}<img class='favicon' src="{{favicon}}"> {{/favicon}}{{host}}
</a>
</div>

View File

@ -24,7 +24,7 @@ private
<div class='onebox-result'>
<div class='source'>
<div class='info'>
<a href='http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177' target="_blank">
<a href='http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177' class="track-link" target="_blank">
<img class='favicon' src="/assets/favicons/amazon.png"> amazon.com
</a>
</div>

View File

@ -20,7 +20,7 @@ private
<div class='onebox-result'>
<div class='source'>
<div class='info'>
<a href='https://play.google.com/store/apps/details?id=com.moosoft.parrot' target="_blank">
<a href='https://play.google.com/store/apps/details?id=com.moosoft.parrot' class="track-link" target="_blank">
<img class='favicon' src="/assets/favicons/google_play.png"> play.google.com
</a>
</div>

View File

@ -20,7 +20,7 @@ private
<div class='onebox-result'>
<div class='source'>
<div class='info'>
<a href='https://itunes.apple.com/us/app/minecraft-pocket-edition-lite/id479651754' target="_blank">
<a href='https://itunes.apple.com/us/app/minecraft-pocket-edition-lite/id479651754' class="track-link" target="_blank">
<img class='favicon' src="/assets/favicons/apple.png"> itunes.apple.com
</a>
</div>

View File

@ -24,7 +24,7 @@ private
<div class="onebox-result">
<div class="source">
<div class="info">
<a href="https://github.com/discourse/discourse/commit/ee76f1926defa8309b3a7ea64a25707519529a13" target="_blank">
<a href="https://github.com/discourse/discourse/commit/ee76f1926defa8309b3a7ea64a25707519529a13" class="track-link" target="_blank">
<img class="favicon" src="/assets/favicons/github.png"> github.com
</a>
</div>

View File

@ -39,7 +39,7 @@ private
<div class='onebox-result'>
<div class='source'>
<div class='info'>
<a href='http://en.wikipedia.org/wiki/Ruby' target="_blank">
<a href='http://en.wikipedia.org/wiki/Ruby' class="track-link" target="_blank">
<img class='favicon' src="/assets/favicons/wikipedia.png"> en.wikipedia.org
</a>
</div>