FIX: Don't track category-badges links.

This commit is contained in:
Guo Xiang Tan 2016-01-19 15:54:49 +08:00
parent 7303f8f309
commit 9652c3df4f
3 changed files with 32 additions and 19 deletions

View File

@ -1,5 +1,11 @@
import DiscourseURL from 'discourse/lib/url';
export function isValidLink($link) {
return ($link.hasClass("track-link") ||
($link.closest('.onebox-result,.onebox-body').length === 0 &&
$link.has('.badge-category').length === 0))
};
export default {
trackClick(e) {
// cancel click if triggered as part of selection.
@ -32,8 +38,7 @@ export default {
var $badge = $('span.badge', $link);
if ($badge.length === 1) {
// don't update counts in category badge nor in oneboxes (except when we force it)
if ($link.hasClass("track-link") ||
$link.closest('.badge-category,.onebox-result,.onebox-body').length === 0) {
if (isValidLink($link)) {
var html = $badge.html();
if (/^\d+$/.test(html)) { $badge.html(parseInt(html, 10) + 1); }
}

View File

@ -4,6 +4,7 @@ import DiscourseURL from 'discourse/lib/url';
import { default as computed, on } from 'ember-addons/ember-computed-decorators';
import { fmt } from 'discourse/lib/computed';
import { decorateLinks as decorateCategoryHashtagLinks } from 'discourse/lib/category-hashtags';
import { isValidLink } from 'discourse/lib/click-track';
const DAY = 60 * 50 * 1000;
@ -194,8 +195,7 @@ const PostView = Discourse.GroupedView.extend(Ember.Evented, {
if (valid) {
// don't display badge counts on category badge & oneboxes (unless when explicitely stated)
if ($link.hasClass("track-link") ||
$link.closest('.badge-category,.onebox-result,.onebox-body').length === 0) {
if (isValidLink($link)) {
$link.append("<span class='badge badge-notification clicks' title='" + I18n.t("topic_map.clicks", {count: lc.clicks}) + "'>" + number(lc.clicks) + "</span>");
}
}

View File

@ -16,21 +16,25 @@ module("lib:click-track", {
windowOpen = sandbox.stub(window, "open").returns(win);
sandbox.stub(win, "focus");
fixture().html([
'<div id="topic" id="1337">',
' <article data-post-id="42" data-user-id="3141">',
' <a href="http://www.google.com">google.com</a>',
' <a class="lightbox back quote-other-topic" href="http://www.google.com">google.com</a>',
' <a id="with-badge" data-user-id="314" href="http://www.google.com">google.com<span class="badge">1</span></a>',
' <a id="with-badge-but-not-mine" href="http://www.google.com">google.com<span class="badge">1</span></a>',
' <div class="onebox-result">',
' <a id="inside-onebox" href="http://www.google.com">google.com<span class="badge">1</span></a>',
' <a id="inside-onebox-forced" class="track-link" href="http://www.google.com">google.com<span class="badge">1</span></a>',
' </div>',
' <a id="same-site" href="http://discuss.domain.com">forum</a>',
' <a class="attachment" href="http://discuss.domain.com/uploads/default/1234/1532357280.txt">log.txt</a>',
' </article>',
'</div>'].join("\n"));
fixture().html(
`<div id="topic" id="1337">
<article data-post-id="42" data-user-id="3141">
<a href="http://www.google.com">google.com</a>
<a class="lightbox back quote-other-topic" href="http://www.google.com">google.com</a>
<a id="with-badge" data-user-id="314" href="http://www.google.com">google.com<span class="badge">1</span></a>
<a id="with-badge-but-not-mine" href="http://www.google.com">google.com<span class="badge">1</span></a>
<div class="onebox-result">
<a id="inside-onebox" href="http://www.google.com">google.com<span class="badge">1</span></a>
<a id="inside-onebox-forced" class="track-link" href="http://www.google.com">google.com<span class="badge">1</span></a>
</div>
<a id="same-site" href="http://discuss.domain.com">forum</a>
<a class="attachment" href="http://discuss.domain.com/uploads/default/1234/1532357280.txt">log.txt</a>
<a class="badge-wrapper bullet" href="http://discuss.domain.com">
<span class="badge-category-bg"></span>
<span class="badge-category"></span>
</a>
</article>
</div>`);
}
});
@ -64,6 +68,10 @@ test("does not track clicks on quote buttons", function() {
ok(track(generateClickEventOn('.quote-other-topic')));
});
test("does not track clicks on category badges", () => {
ok(!track(generateClickEventOn('.badge-wrapper')));
});
test("removes the href and put it as a data attribute", function() {
track(generateClickEventOn('a'));