Count Button Clicks

I have an E-Book that I am giving away for free. I have attached the e-book to a button on one of my web pages. I would like to know how many times the e-book is downloaded. Is there some way to record the number of times the button is clicked.

You can create an onClick event that update a record in the database which represented the number of clicks in the button.

Good luck :slight_smile:

Appreciate your help.

This video explains it pretty well

The video is helpful, but only up to a point. I just can’t read what it is that is typed into the code panel? Can anyone help me please? Thanks.

hi tony.
Make a database named “EbookDownloads”
add a row to it named “totalDownloads”
on your button click

In onClick function :

 let toInsert =  await{
 "totalDownloads" : 1

await wixData.insert("EbookDownloads", toInsert)
    .catch( (err) => {
 let errorMsg = err;
    } );

if you click on your database and add hook add a beforeinsert hook.
this will create a function named Edownloads_beforeInsert() in
change the code to this code:

 export async function EbookDownloads_beforeInsert(item, context) {
let incrementalNr;
 await wixData.query("EbookDownloads")
    .then((result) => {
 let r = result.items;
 if (r[0] === undifined)
 //do nothing, number 1 will be added as said in the page code
 else {
incrementalNr = r[0].totalDownloads; // adds the founded //number to incrementalNr
 item.totalDownloads = incrementalNr + 1 //adds 1 to it.
 //TODO: write your code here...
 return item;

Hi Kevin-- I tried using your code, and am receiving the error message " Cannot use keyword ‘await’ outside an async function ". Any suggestions on how to fix this?

The code for the Wix YouTube video was already in the comments itself.

As for doing this, you can go through the actual Wix tutorial here.

Or with GTM.

There were previous forum posts that showed how to do this.

Finally, please add a new post if you require more help with your own issue, rather than bumping up an old thread from 2018.