FEATURE: Always track clicks using AJAX. (#7373)

FEATURE: Always track clicks using AJAX. (#7373)

diff --git a/app/assets/javascripts/discourse/components/discourse-topic.js.es6 b/app/assets/javascripts/discourse/components/discourse-topic.js.es6
index 4687203..4e7496d 100644
--- a/app/assets/javascripts/discourse/components/discourse-topic.js.es6
+++ b/app/assets/javascripts/discourse/components/discourse-topic.js.es6
@@ -3,7 +3,6 @@ import AddArchetypeClass from "discourse/mixins/add-archetype-class";
 import ClickTrack from "discourse/lib/click-track";
 import Scrolling from "discourse/mixins/scrolling";
 import MobileScrollDirection from "discourse/mixins/mobile-scroll-direction";
-import { selectedText } from "discourse/lib/utilities";
 import { observes } from "ember-addons/ember-computed-decorators";
 
 const MOBILE_SCROLL_DIRECTION_CHECK_THROTTLE = 300;
@@ -82,17 +81,9 @@ export default Ember.Component.extend(
       $(window).on("resize.discourse-on-scroll", () => this.scrolled());
 
       this.$().on(
-        "mouseup.discourse-redirect",
+        "click.discourse-redirect",
         ".cooked a, a.track-link",
         function(e) {
-          // bypass if we are selecting stuff
-          const selection = window.getSelection && window.getSelection();
-          if (selection.type === "Range" || selection.rangeCount > 0) {
-            if (selectedText() !== "") {
-              return true;
-            }
-          }
-
           const $target = $(e.target);
           if (
             $target.hasClass("mention") ||
@@ -116,7 +107,7 @@ export default Ember.Component.extend(
       $(window).unbind("resize.discourse-on-scroll");
 
       // Unbind link tracking
-      this.$().off("mouseup.discourse-redirect", ".cooked a, a.track-link");
+      this.$().off("click.discourse-redirect", ".cooked a, a.track-link");
 
       this.resetExamineDockCache();
 
diff --git a/app/assets/javascripts/discourse/components/links-redirect.js.es6 b/app/assets/javascripts/discourse/components/links-redirect.js.es6
index da28b82..a84a140 100644
--- a/app/assets/javascripts/discourse/components/links-redirect.js.es6
+++ b/app/assets/javascripts/discourse/components/links-redirect.js.es6
@@ -1,19 +1,10 @@
 import ClickTrack from "discourse/lib/click-track";
-import { selectedText } from "discourse/lib/utilities";
 
 export default Ember.Component.extend({
   didInsertElement() {
     this._super(...arguments);
 
-    this.$().on("mouseup.discourse-redirect", "#revisions a", function(e) {
-      // bypass if we are selecting stuff
-      const selection = window.getSelection && window.getSelection();
-      if (selection.type === "Range" || selection.rangeCount > 0) {
-        if (selectedText() !== "") {
-          return true;
-        }
-      }
-
+    this.$().on("click.discourse-redirect", "#revisions a", function(e) {
       const $target = $(e.target);
       if (
         $target.hasClass("mention") ||
@@ -28,6 +19,6 @@ export default Ember.Component.extend({
 
   willDestroyElement() {
     this._super(...arguments);
-    this.$().off("mouseup.discourse-redirect", "#revisions a");
+    this.$().off("click.discourse-redirect", "#revisions a");
   }
 });
diff --git a/app/assets/javascripts/discourse/components/user-stream.js.es6 b/app/assets/javascripts/discourse/components/user-stream.js.es6
index be0ba24..8b16ea6 100644
--- a/app/assets/javascripts/discourse/components/user-stream.js.es6
+++ b/app/assets/javascripts/discourse/components/user-stream.js.es6
@@ -1,6 +1,5 @@
 import LoadMore from "discourse/mixins/load-more";
 import ClickTrack from "discourse/lib/click-track";
-import { selectedText } from "discourse/lib/utilities";
 import Post from "discourse/models/post";
 import DiscourseURL from "discourse/lib/url";
 import Draft from "discourse/models/draft";
@@ -32,15 +31,7 @@ export default Ember.Component.extend(LoadMore, {
     $(window).on("resize.discourse-on-scroll", () => this.scrolled());
 
     this.$().on("click.details-disabled", "details.disabled", () => false);
-    this.$().on("mouseup.discourse-redirect", ".excerpt a", function(e) {
-      // bypass if we are selecting stuff
-      const selection = window.getSelection && window.getSelection();
-      if (selection.type === "Range" || selection.rangeCount > 0) {
-        if (selectedText() !== "") {
-          return true;
-        }
-      }
-
+    this.$().on("click.discourse-redirect", ".excerpt a", function(e) {
       const $target = $(e.target);
       if (
         $target.hasClass("mention") ||
@@ -60,7 +51,7 @@ export default Ember.Component.extend(LoadMore, {
     this.$().off("click.details-disabled", "details.disabled");
 
     // Unbind link tracking
-    this.$().off("mouseup.discourse-redirect", ".excerpt a");
+    this.$().off("click.discourse-redirect", ".excerpt a");
   }.on("willDestroyElement"),
 
   actions: {
diff --git a/app/assets/javascripts/discourse/lib/click-track.js.es6 b/app/assets/javascripts/discourse/lib/click-track.js.es6
index 1da282b..f3468b6 100644
--- a/app/assets/javascripts/discourse/lib/click-track.js.es6
+++ b/app/assets/javascripts/discourse/lib/click-track.js.es6
@@ -4,9 +4,24 @@ import { wantsNewWindow } from "discourse/lib/intercept-click";
 import { selectedText } from "discourse/lib/utilities";
 
 export function isValidLink($link) {
+  // Do not track:
+  //  - lightboxes
+  //  - group mentions
+  //  - links with disabled tracking
+  //  - category links
+  //  - quote back button
+  if ($link.is(".lightbox, .mention-group, .no-track-link, .hashtag, .back")) {
+    return false;
+  }
+
+  // Do not track links in quotes or in elided part
+  if ($link.parents("aside.quote, .elided").length !== 0) {
+    return false;
+  }
+
   return (
     $link.hasClass("track-link") ||
-    $link.closest(".hashtag,.badge-category,.onebox-result,.onebox-body")
+    $link.closest(".hashtag, .badge-category, .onebox-result, .onebox-body")
       .length === 0
   );
 }
@@ -18,34 +33,36 @@ export default {
       return true;
     }
 
-    // cancel click if triggered as part of selection.
-    if (selectedText() !== "") {
-      return false;
+    // Cancel click if triggered as part of selection.
+    const selection = window.getSelection();
+    if (selection.type === "Range" || selection.rangeCount > 0) {
+      if (selectedText() !== "") {
+        return true;
+      }
     }
 
     const $link = $(e.currentTarget);
-
-    // don't track
-    //   - lightboxes
-    //   - group mentions
-    //   - links with disabled tracking
-    //   - category links
-    //   - quote back button
-    if (
-      $link.is(".lightbox, .mention-group, .no-track-link, .hashtag, .back")
-    ) {
+    if (!isValidLink($link)) {
       return true;
     }
 
-    // don't track links in quotes or in elided part
-    let tracking = $link.parents("aside.quote, .elided").length === 0;
+    if ($link.hasClass("attachment")) {
+      // Warn the user if they cannot download the file.
+      if (
+        Discourse.SiteSettings.prevent_anons_from_downloading_files &&
+        !Discourse.User.current()
+      ) {
+        bootbox.alert(I18n.t("post.errors.attachment_download_requires_login"));
+        return false;
+      }
 
-    let href = $link.attr("href") || $link.data("href");
+      return true;
+    }
 
-    if (!href || href.trim().length === 0) {
+    let href = ($link.attr("href") || $link.data("href")).trim();
+    if (!href) {
       return false;
-    }
-    if (href.indexOf("mailto:") === 0) {
+    } else if (href.indexOf("mailto:") === 0) {
       return true;
     }
 
@@ -57,119 +74,64 @@ export default {
     const userId = $link.data("user-id") || $article.data("user-id");
     const ownLink = userId && userId === Discourse.User.currentProp("id");
 
-    let destUrl = href;
-
-    if (tracking) {
-      destUrl = Discourse.getURL(
-        "/clicks/track?url=" + encodeURIComponent(href)
-      );
-
-      if (postId && !$link.data("ignore-post-id")) {
-        destUrl += "&post_id=" + encodeURI(postId);
-      }
-      if (topicId) {
-        destUrl += "&topic_id=" + encodeURI(topicId);

[... diff too long, it was truncated ...]

GitHub sha: 1867f2dd

I think this broke mentions. Clicking on the mentioned username doesn’t show the user card anymore.

3 Likes

It’s been fixed by

1 Like