# How to show a download count on files uploaded to a post (Pro hosting, theme component only)?

**URL:** <https://meta.discourse.org/t/how-to-show-a-download-count-on-files-uploaded-to-a-post-pro-hosting-theme-component-only/417810>\
**Category:** Extras\
**Created:** [19 בנובמבר,‏ 2026,‏ 6:51am UTC](https://meta.discourse.org/t/how-to-show-a-download-count-on-files-uploaded-to-a-post-pro-hosting-theme-component-only/417810 "2026-11-19T06:51:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jessica\_S](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jessica_s/32/584823_2.png) [@Jessica\_S](https://meta.discourse.org/u/Jessica_S)\
**Post date:** [19 בנובמבר,‏ 2026,‏ 6:51am UTC](https://meta.discourse.org/t/how-to-show-a-download-count-on-files-uploaded-to-a-post-pro-hosting-theme-component-only/417810/1 "2026-11-19T06:51:17Z")

</div>

Hi everyone,

We run a community on Discourse (Pro hosting, so no custom plugins, only themes and theme components). We have a category where members share downloadable files (BIOS packages, scripts, config packs). These files are uploaded **directly to Discourse** , not hosted on GitHub or elsewhere.

**What we want**  
A visible **download count** on each file, similar to the reference screenshot below (a skills marketplace showing favorites, installs and a download button). Ideally:

- a count that increases each time someone downloads the attachment,
- shown in the topic and, if possible, on topic list cards,
- sortable or filterable later if the data is available.

**What we tried**  
We built a theme component that adds a download button and tries to read the attachment’s click count. The count always stays at **0** , even after several test downloads from different accounts.

**What we need to know**

1. Does Discourse track downloads of uploaded attachments out of the box? If yes, where is that data stored, and is it exposed in the topic/post JSON, or the [Data Explorer](https://meta.discourse.org/t/32566?silent=true)?
2. If it is tracked as link clicks, are attachment clicks counted when the file is opened via the `/uploads/short-url/...` link, and does that count depend on any site setting (for example, tracking clicks for internal links)?
3. If it isn’t tracked, what’s the recommended approach on a hosted plan? For example, a tracking redirect, an API-based counter, or a theme component that reports to an external endpoint.
4. Does the count behave differently for files added through a Form Template “upload” field compared with files added in the normal composer?

**Environment**

- Discourse Pro hosted, Foundation theme
- Files uploaded as post attachments (not external links)
- Version: [paste from /admin]

**Reference (what we’d like to build)**

 ![image](https://global.discourse-cdn.com/meta/original/4X/e/3/a/e3a2e2f88b0985f186363e91e101bdde6804c9ea.jpeg)

Shows the download button, favorites count and install/download count on a resource page.

Any pointers, example code, or existing components would be really appreciated. Thank you!

---

<div class="post-metadata">

**Author:** ![Rajat\_99](https://avatars.discourse-cdn.com/v4/letter/r/b5a626/32.png) [@Rajat\_99](https://meta.discourse.org/u/Rajat_99)\
**Post date:** [19 בנובמבר,‏ 2026,‏ 7:31am UTC](https://meta.discourse.org/t/how-to-show-a-download-count-on-files-uploaded-to-a-post-pro-hosting-theme-component-only/417810/2 "2026-11-19T07:31:22Z")

</div>

> [@Jessica\_S](#):
>
> Shows the download button, favorites count and install/download count on a resource page.
> 
> Any pointers, example code, or existing components would be really appreciated. Thank you!

@Jessica_S Interesting requirement! A few questions:

1. Do you need total downloads or unique downloads per user?

2. Should downloads from guests also be counted?

3. Are you open to an external API for tracking if Discourse doesn’t expose attachment download events?

---

<div class="post-metadata">

**Author:** ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)\
**Post date:** [19 בנובמבר,‏ 2026,‏ 7:44am UTC](https://meta.discourse.org/t/how-to-show-a-download-count-on-files-uploaded-to-a-post-pro-hosting-theme-component-only/417810/3 "2026-11-19T07:44:03Z")

</div>

> [@Rajat\_99](#):
>
> 1. Are you open to an external API for tracking if Discourse doesn’t expose attachment download events?

I would expect someone with 10+ years of experience in Discourse theme development to be able to figure out that Discourse exposes those counts in the post stream json 😉

Eg. the number of clicks on this link will be counted [https://www.rd.usda.gov/sites/default/files/pdf-sample\_0.pdf](https://www.rd.usda.gov/sites/default/files/pdf-sample_0.pdf)

Then look at [https://meta.discourse.org/t/how-to-show-a-download-count-on-files-uploaded-to-a-post-pro-hosting-theme-component-only/417810.json:](https://meta.discourse.org/t/how-to-show-a-download-count-on-files-uploaded-to-a-post-pro-hosting-theme-component-only/417810.json:)

 ![image](https://global.discourse-cdn.com/meta/original/4X/4/c/9/4c94dd9c5e8cb710758818bfe0c2936e56cb87ba.png)

and in Javascript you get a `post.link_counts` array.

---

<div class="post-metadata">

**Author:** ![Rajat\_99](https://avatars.discourse-cdn.com/v4/letter/r/b5a626/32.png) [@Rajat\_99](https://meta.discourse.org/u/Rajat_99)\
**Post date:** [19 בנובמבר,‏ 2026,‏ 8:43am UTC](https://meta.discourse.org/t/how-to-show-a-download-count-on-files-uploaded-to-a-post-pro-hosting-theme-component-only/417810/4 "2026-11-19T08:43:09Z")

</div>

> [@RGJ](#):
>
> and in Javascript you get a `post.link_counts` array.

@RGJ Thanks for pointing this out. You’re right, I appreciate the clarification. I’ll look into the `post.link_counts` data in the post stream JSON and how it can be used to display attachment download counts through a Discourse theme component.

---

<div class="post-metadata">

**Author:** ![Jessica\_S](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jessica_s/32/584823_2.png) [@Jessica\_S](https://meta.discourse.org/u/Jessica_S)\
**Post date:** [19 בנובמבר,‏ 2026,‏ 8:58am UTC](https://meta.discourse.org/t/how-to-show-a-download-count-on-files-uploaded-to-a-post-pro-hosting-theme-component-only/417810/5 "2026-11-19T08:58:46Z")

</div>

![image](https://global.discourse-cdn.com/meta/original/4X/d/3/e/d3e461e1a2f83b0bf055bbf70549e6c4fe1708f0.png)

I’ve added the code to my theme, but it isn’t being picked up automatically—I still can’t see the download count.

---

<div class="post-metadata">

**Author:** ![Moin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/moin/32/554653_2.png) [@Moin](https://meta.discourse.org/u/Moin)\
**Post date:** [19 בנובמבר,‏ 2026,‏ 9:01am UTC](https://meta.discourse.org/t/how-to-show-a-download-count-on-files-uploaded-to-a-post-pro-hosting-theme-component-only/417810/6 "2026-11-19T09:01:42Z")

</div>

Which code did you add?

---

<div class="post-metadata">

**Author:** ![Jessica\_S](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jessica_s/32/584823_2.png) [@Jessica\_S](https://meta.discourse.org/u/Jessica_S)\
**Post date:** [19 בנובמבר,‏ 2026,‏ 9:15am UTC](https://meta.discourse.org/t/how-to-show-a-download-count-on-files-uploaded-to-a-post-pro-hosting-theme-component-only/417810/7 "2026-11-19T09:15:10Z")

</div>

JS:

import { apiInitializer } from “discourse/lib/api”;  
import { iconHTML } from “discourse/lib/icon-library”;

// 1234 → 1.2k, 15000 → 15k  
const formatCount = (n) =\> {  
if (n \< 1000) return `${n}`;  
const k = n / 1000;  
return `${k >= 10 ? Math.round(k) : k.toFixed(1).replace(/\.0$/, "")}k`;  
};

export default apiInitializer(“1.8.0”, (api) =\> {  
api.decorateCookedElement(  
(el, helper) =\> {  
const post = helper?.getModel?.();  
if (!post || post.post\_number !== 1) return; // first post only

```plaintext
  const links = post.link_counts || [];

  el.querySelectorAll("a.attachment").forEach((a) => {
    if (a.dataset.dlDecorated) return;
    a.dataset.dlDecorated = "1";

    // Look up the download count for this file
    const path = new URL(a.href, location.origin).pathname;
    const match = links.find((l) => l.url && l.url.includes(path));
    const downloads = match?.clicks || 0;

    // Rebuild the button: [icon] filename | count
    const fileName = a.textContent.trim();
    a.textContent = "";
    a.classList.add("gm-download-btn");
    a.insertAdjacentHTML("afterbegin", iconHTML("download"));

    const name = document.createElement("span");
    name.className = "gm-download-name";
    name.textContent = fileName;
    a.appendChild(name);

    const count = document.createElement("span");
    count.className = "gm-download-count";
    count.textContent = formatCount(downloads);
    count.title = `${downloads} download${downloads === 1 ? "" : "s"}`;
    a.appendChild(count);
  });
},
{ id: "gm-download-button", onlyStream: true }

```

);  
});

CSS：.gm-download-btn {  
display: inline-flex;  
align-items: center;  
gap: 0.6em;  
padding: 0.55em 0.55em 0.55em 1.2em;  
border-radius: 999px;  
background: var(–tertiary);  
color: var(–secondary) !important;  
font-weight: 600;  
text-decoration: none;

&:hover {  
background: var(–tertiary-hover);  
color: var(–secondary) !important;  
}

.d-icon {  
color: var(–secondary);  
}

.gm-download-count {  
margin-left: 0.4em;  
padding: 0.15em 0.8em;  
border-radius: 999px;  
background: rgba(255, 255, 255, 0.22);  
font-size: var(–font-down-1);  
font-variant-numeric: tabular-nums;  
}  
}

---

<div class="post-metadata">

**Author:** ![Moin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/moin/32/554653_2.png) [@Moin](https://meta.discourse.org/u/Moin)\
**Post date:** [19 בנובמבר,‏ 2026,‏ 10:00am UTC](https://meta.discourse.org/t/how-to-show-a-download-count-on-files-uploaded-to-a-post-pro-hosting-theme-component-only/417810/8 "2026-11-19T10:00:30Z")

</div>

Did you check the json for that post manually? Is the `link_count` you are looking for included?
