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.
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.
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.
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.
The page code is very simple and straightforward. Add an onClick function to your download button.
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.