# Improve Onebox click counting

**URL:** https://meta.discourse.org/t/improve-onebox-click-counting/130409
**Category:** Feature
**Created:** [October 7, 2019, 1:52pm UTC](https://meta.discourse.org/t/improve-onebox-click-counting/130409 "2019-10-07T13:52:14Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![jtbayly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jtbayly/32/119510_2.png) [@jtbayly](https://meta.discourse.org/u/jtbayly)
#### Post date: [October 7, 2019, 1:52pm UTC](https://meta.discourse.org/t/improve-onebox-click-counting/130409/1 "2019-10-07T13:52:15Z")

</div>

If I put a link into my posts, I see how many people have clicked on the link. But if I put the link on one line, and it gets one-boxed, then I never see any count of the clicks.

This post describes the issue further:

> [@How to educate users about oneboxing](https://meta.discourse.org/t/how-to-educate-users-about-oneboxing/66231/8):
>
> The count for links not Oneboxed are showing now, at least on those that I’ve just seen, but there are still no counts showing for those links which get Oneboxed. I just clicked on the Onebox link in the below screenshot and no number came up. And I do know for a fact that several users have also clicked on the link. It works okay here on Meta, but not on our forum (Discourse hosted). As of yet, no one has complained at all. I don’t even think they realize there is supposed to be a counter to sh…

There are two parts to the answer.

First, you must enable opening links in an external tab. (I don’t understand this, technically, as other links are counted without turning this option on.)

Second…

> [@How to educate users about oneboxing](https://meta.discourse.org/t/how-to-educate-users-about-oneboxing/66231/9):
>
> Please note that you need to click on the first link ( `snopes.com` ) and not on the title ( `Does this photograph...` ) to see the number of clicks change.

So, the main link people will click on will never be counted. Again, I don’t understand the technical difference between the links and why one would be counted and not the other.

Regardless of my lack of understanding of what’s going on technically, I’d love to see Onebox link clicks counted consistently.

---

<div class="post-metadata">

### Author: ![JimPas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jimpas/32/148179_2.png) [@JimPas](https://meta.discourse.org/u/JimPas)
#### Post date: [October 7, 2019, 3:53pm UTC](https://meta.discourse.org/t/improve-onebox-click-counting/130409/2 "2019-10-07T15:53:40Z")

</div>

I have gone through all the settings in my Dashboard and I have no check boxes to enable opening links in an external tab… but the links do open in another tab already.  
We have tried clicking on both the title of an article **and** the link itself - no counter for either.  
Here’s a stickler… for a day or two, the counter suddenly worked, then stopped again. Nothing was changed at all. I’ve also asked a mod and several users if they see any link counts next to the links posted. None have seen them so I know it’s not just me being an Admin.  
Like you, I would like to see the Onebox link clicks counted consistently.

Edit: I should point out for link posted after a blank space (so they don’t get Oneboxed), the counter works… just not for Onebox.

---

<div class="post-metadata">

### Author: ![dax](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dax/32/244677_2.png) [@dax](https://meta.discourse.org/u/dax)
#### Post date: [October 11, 2019, 5:43pm UTC](https://meta.discourse.org/t/improve-onebox-click-counting/130409/3 "2019-10-11T17:43:14Z")

</div>

> [@JimPas](#):
>
> I have gone through all the settings in my Dashboard and I have no check boxes to enable opening links in an external tab… but the links do open in another tab already.

I already enabled the setting last week, search for `default other external links in new tab`. This setting will be applied to all new users who have registered on your site after the site setting has been enabled. Old users can activate it from their profiles (in case we can bulk enable the setting even for old users via console).

> [@JimPas](#):
>
> We have tried clicking on both the title of an article **and** the link itself - no counter for either

I tried to click on the two oneboxes in the first two posts of this topic [https://forum.nodders.net/t/2019-plastic-waste-and-recycling/167](https://forum.nodders.net/t/2019-plastic-waste-and-recycling/167) and the count has increased from 0 to 1. It is possible that the user who did the test was an old user and did not enable the setting for external tab on the profile, or that the user tried to open the link in a new window directly by right-clicking on the link itself.

---

<div class="post-metadata">

### Author: ![JimPas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jimpas/32/148179_2.png) [@JimPas](https://meta.discourse.org/u/JimPas)
#### Post date: [October 11, 2019, 6:41pm UTC](https://meta.discourse.org/t/improve-onebox-click-counting/130409/4 "2019-10-11T18:41:45Z")

</div>

> [@dax](#):
>
> It is possible that the user who did the test was an old user and did not enable the setting for external tab on the profile

That’s what probably is happening. I’ll inform everyone that they will have to make the change in their profile… as I. 😉 Thank you.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [October 12, 2019, 10:07pm UTC](https://meta.discourse.org/t/improve-onebox-click-counting/130409/5 "2019-10-12T22:07:11Z")

</div>

Also note that clicking links in **your own post** will not increment the counters..

---

<div class="post-metadata">

### Author: ![JimPas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jimpas/32/148179_2.png) [@JimPas](https://meta.discourse.org/u/JimPas)
#### Post date: [October 17, 2019, 6:40pm UTC](https://meta.discourse.org/t/improve-onebox-click-counting/130409/6 "2019-10-17T18:40:21Z")

</div>

@dax, @codinghorror, I found out why the link counter was working for me and others on our forum. In \_Preferences/Interface/Other, although I did have the _Open all external links in another tab_ checked, I also had the _Show counts on browser icon_ checked. Apparently the latter overrides the first box. I unchecked the _Show counts on browser icon_, saved, then checked a post… the counter showed up!

I also found one little nuance. When a user posts a link and OneBox tries and fails to OneBox it - leaving the link as is - the link click counter will not work if the link isn’t preceded with a blank space. I just edited a user’s post where this just occurred, adding the space, and the counter began showing. 😉

---

<div class="post-metadata">

### Author: ![adamengst](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/adamengst/32/116607_2.png) [@adamengst](https://meta.discourse.org/u/adamengst)
#### Post date: [January 8, 2020, 4:22pm UTC](https://meta.discourse.org/t/improve-onebox-click-counting/130409/7 "2020-01-08T16:22:22Z")

</div>

> [@jtbayly](#):
>
> First, you must enable opening links in an external tab. (I don’t understand this, technically, as other links are counted without turning this option on.)

@dax Any thoughts on why this setting is required for the onebox click badges to appear? I could change it for my site, but for it to be useful, I’d have to bulk-change the setting for 7400+ users, which I’d rather not do, since it will just cause confusion.

---

<div class="post-metadata">

### Author: ![jtbayly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jtbayly/32/119510_2.png) [@jtbayly](https://meta.discourse.org/u/jtbayly)
#### Post date: [January 8, 2020, 4:48pm UTC](https://meta.discourse.org/t/improve-onebox-click-counting/130409/8 "2020-01-08T16:48:36Z")

</div>

I also don’t know why you’d bother, since the one box clicks won’t be counted anyway except for the few people who click the least-obvious link. This means that instead of displaying no data on how many times it was clicked, you’ll display incorrect data (artificially low), which is much worse in my opinion.

---

<div class="post-metadata">

### Author: ![adamengst](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/adamengst/32/116607_2.png) [@adamengst](https://meta.discourse.org/u/adamengst)
#### Post date: [January 8, 2020, 5:13pm UTC](https://meta.discourse.org/t/improve-onebox-click-counting/130409/9 "2020-01-08T17:13:58Z")

</div>

A good point—ideally, the onebox would also count clicks on its headline as well.

---

<div class="post-metadata">

### Author: ![evantill](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/evantill/32/116219_2.png) [@evantill](https://meta.discourse.org/u/evantill)
#### Post date: [March 16, 2020, 11:21pm UTC](https://meta.discourse.org/t/improve-onebox-click-counting/130409/10 "2020-03-16T23:21:20Z")

</div>

I have made a theme component in order to count clicks on its headline as well.

[https://github.com/evantill/discourse-onebox-track-url](https://github.com/evantill/discourse-onebox-track-url)

---

<div class="post-metadata">

### Author: ![rizka](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rizka/32/79717_2.png) [@rizka](https://meta.discourse.org/u/rizka)
#### Post date: [March 16, 2020, 11:58pm UTC](https://meta.discourse.org/t/improve-onebox-click-counting/130409/11 "2020-03-16T23:58:55Z")

</div>

This really used to work. Onebox clicks were counted on our site. Then at some point it didn’t work anymore. Can’t remember when exactly but I think it’s more than a year ago. I say this is a regression, not a real feature request.

Could you elaborate what your component does exactly, @evantill?

---

<div class="post-metadata">

### Author: ![jtbayly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jtbayly/32/119510_2.png) [@jtbayly](https://meta.discourse.org/u/jtbayly)
#### Post date: [March 17, 2020, 12:39am UTC](https://meta.discourse.org/t/improve-onebox-click-counting/130409/12 "2020-03-17T00:39:03Z")

</div>

It looks like a tiny bit of JavaScript that could simply be added to core, assuming it works.

---

<div class="post-metadata">

### Author: ![evantill](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/evantill/32/116219_2.png) [@evantill](https://meta.discourse.org/u/evantill)
#### Post date: [March 17, 2020, 9:14am UTC](https://meta.discourse.org/t/improve-onebox-click-counting/130409/13 "2020-03-17T09:14:36Z")

</div>

Just a simple script that replace a click on an Url inside the onebox by a click on the url of the header.

```javascript
    //if click inside onebox body
    $('.onebox-body a').on('click.onebox-track-url',function(event){      
      event.preventDefault(); //->ignore click event inside onebox
      var headerUrl = $(event.target).closest('.onebox').find('header a'); //select the header url
      //click instead on header url to update tracking counter
      if(headerUrl.length>0){
        headerUrl[0].click(); //-> simulate click on the head url
      }
      });
    });
</script>

```

---

<div class="post-metadata">

### Author: ![evantill](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/evantill/32/116219_2.png) [@evantill](https://meta.discourse.org/u/evantill)
#### Post date: [March 17, 2020, 9:27am UTC](https://meta.discourse.org/t/improve-onebox-click-counting/130409/14 "2020-03-17T09:27:19Z")

</div>

It seems that links inside onebox body are not tracked by choice :

`isValidLink()` return false if link is in `.onebox-body`

> <https://github.com/discourse/discourse/blob/c61ebc9ba67d6588c09c2b93205865e0fd388f51/app/assets/javascripts/discourse/lib/click-track.js#L23>

---

<div class="post-metadata">

### Author: ![evantill](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/evantill/32/116219_2.png) [@evantill](https://meta.discourse.org/u/evantill)
#### Post date: [March 17, 2020, 2:13pm UTC](https://meta.discourse.org/t/improve-onebox-click-counting/130409/15 "2020-03-17T14:13:58Z")

</div>

A solution could be to add a new setting on onebox to enable tracking on links inside oneboxes  
and in this case add class `track-link` on links inside oneboxes ?

---

<div class="post-metadata">

### Author: ![jtbayly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jtbayly/32/119510_2.png) [@jtbayly](https://meta.discourse.org/u/jtbayly)
#### Post date: [March 17, 2020, 2:20pm UTC](https://meta.discourse.org/t/improve-onebox-click-counting/130409/16 "2020-03-17T14:20:25Z")

</div>

It’s not clear to me from this thread whether it was an intentional decision to stop tracking onebox clicks or if this is simply a bug that you could create a pull request for.

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [March 17, 2020, 4:14pm UTC](https://meta.discourse.org/t/improve-onebox-click-counting/130409/17 "2020-03-17T16:14:30Z")

</div>

Would it be correct to describe the proposed fix as this?

“If the link is in a onebox body, find the URL of the onebox. If that URL is the same as the link, count the click. Otherwise, ignore”

---

<div class="post-metadata">

### Author: ![jtbayly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jtbayly/32/119510_2.png) [@jtbayly](https://meta.discourse.org/u/jtbayly)
#### Post date: [March 17, 2020, 4:23pm UTC](https://meta.discourse.org/t/improve-onebox-click-counting/130409/19 "2020-03-17T16:23:39Z")

</div>

Is it even possible for the onebox to have a link in it other than the url being oneboxed? I wouldn’t have thought so.

---

<div class="post-metadata">

### Author: ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)
#### Post date: [April 2, 2020, 6:41pm UTC](https://meta.discourse.org/t/improve-onebox-click-counting/130409/22 "2020-04-02T18:41:36Z")

</div>

> [@riking](#):
>
> If the link is in a onebox body, find the URL of the onebox. If that URL is the same as the link, count the click. Otherwise, ignore

I added this functionality in below commit

[https://github.com/discourse/discourse/commit/e7e931b70b5790a71d41893834d5e8c9945c92df](https://github.com/discourse/discourse/commit/e7e931b70b5790a71d41893834d5e8c9945c92df)
