I'm not exactly an expert coder. I'm actually quite the non-coder. For a non-coder, I run a lot of code on my website, thanks to this forum. I recently needed to add a download counter to a dynamic page. Wix, of course, doesn't have a vanilla feature with the available download buttons. Wix customer support says it can't be done. The solution? Code it.
There are dozens upon dozens of forum posts on this functionality. I found no straightforward solution, but I did find a lot of what does not work for so many. I compiled a solution based on several different forum posts. The solution counts buttons clicks, displays the count for the item in the repeater on the dynamic list page and displays the count on the dynamic item page where the download button lives. It's a rather simple two-part solution.
I have a data collection that contains files for user download. It's very simple and straightforward.
![](https://static.wixstatic.com/media/b1bb60_a2b8b27f046b458c926fe200233f9ee6~mv2.png/v1/fill/w_980,h_446,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/b1bb60_a2b8b27f046b458c926fe200233f9ee6~mv2.png)
A button on the dynamic title page is connected to the File field in the database. The Download Count field is where the download count is stored. On the item page and in the repeater on the list page a text element is connected to this Download Count field. Easy and straightforward.
![](https://static.wixstatic.com/media/b1bb60_5fccfcd6a8de4f7cb48720fda7e8f54d~mv2.png/v1/fill/w_891,h_807,al_c,q_90,enc_auto/b1bb60_5fccfcd6a8de4f7cb48720fda7e8f54d~mv2.png)
When the Download File button is clicked, it's counted as one download, and the count display updates. There are several mentions in the forum of this number refreshing after returning to the list page or after refreshing the browser. The solution for this resides with database and dataset permissions. Database permissions must be set to Anyone for "Can add content," and permissions for the dataset on the title page must be set to Read & Write. These settings allow the download button click to be recorded when any user clicks the button. I ran into an issue where everything worked in Preview mode but not on the live site. Setting the permissions as described here solved that issue.
![](https://static.wixstatic.com/media/b1bb60_7a420af8b1f84268bd334a8cb3137c9f~mv2.png/v1/fill/w_738,h_807,al_c,q_90,enc_auto/b1bb60_7a420af8b1f84268bd334a8cb3137c9f~mv2.png)
![](https://static.wixstatic.com/media/b1bb60_287c2daa508e4168be235347a19161a5~mv2.png/v1/fill/w_495,h_735,al_c,q_90,enc_auto/b1bb60_287c2daa508e4168be235347a19161a5~mv2.png)
The code is two part. The first part is the datahook that ensures a progressive incremental count that will not reset for the user after the user leaves the site. The datahook code was found here in the forum and is compiled from between several different posts.
import wixData from 'wix-data'; export function Downloads_beforeInsert(item, context) { let incrementalNr; wixData.query("Downloads").descending("downloadCount").find().then((result) => { //"Downloads" is the name of the database, "downladCount" is the field name let r = result.items; if (r[0] === undefined) { //do nothing, number 1 will be added as said in the page code } else { incrementalNr = r[0].downloadCount; // adds the founded //number to incrementalNr item.Downloads = incrementalNr + 1 //adds 1 to it. } }) return item; } export function Downloads_beforeUpdate(item, context) { let incrementalNr; wixData.query("Downloads").descending("downloadCount").find().then((result) => { let r = result.items; if (!item.updateScore) { return item; } return wixData.get("Downloads", item._id) .then((res) => { if (!res.downloadCount) { item.downloadCount = 1; } else { item.downloadCount = res.downloadCount + 1; } delete item.updateScore; return item; }) }) }
To add a datahook to your database, click on the "..." and select Add/Remove Hooks. Then, paste the above code, changing the database and field names to match yours.
![](https://static.wixstatic.com/media/b1bb60_0788175fd4034fe9a2b4ae2c7c61981e~mv2.png/v1/fill/w_980,h_449,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/b1bb60_0788175fd4034fe9a2b4ae2c7c61981e~mv2.png)
The page code is very simple and straightforward. Add an onClick function to your download button.
![](https://static.wixstatic.com/media/b1bb60_5dc2ff60960b4620a53e1e86f36884be~mv2.png/v1/fill/w_486,h_582,al_c,q_85,enc_auto/b1bb60_5dc2ff60960b4620a53e1e86f36884be~mv2.png)
Then, paste in the following code, making necessary changes for the names of your database, field names, dataset, and page element names.
import wixData from 'wix-data'; $w.onReady(function () { }); export function downloadFile_click(event) { const currentItem = $w("#itemsDataset").getCurrentItem(); //itemsDataset is the dataset for the page let clickCount = currentItem.downloadCount + 1; $w("#itemsDataset").setFieldValue("downloadCount", clickCount); //"downloadCount" is the database field key" $w('#itemsDataset').save(); wixData.update("Downloads", clickCount).then(() => { //"Downloads is the database name" $w("#itemsDataset").refresh(); } ) }
If you use Sandbox in your database, make sure to sync to live before you publish and make this code live. Missing this step will have you banging your head against your screen.
Is there a less clunky solution? Possibly. However, the download counter on my website runs smoothly with this solution. Live site: expertsincmt . com/downloads
If anybody can help me out with coding for the download button to trigger a Save As dialog pop up, I would be forever grateful. I haven't found a solution yet and the examples from the incomparable Code Queen that are referenced throughout the community don't work for me.