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.

Hi,
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
backend/data.js
change the code to this code:

 export async function EbookDownloads_beforeInsert(item, context) {
let incrementalNr;
 await wixData.query("EbookDownloads")
    .descending("totalDownloads")
    .find()
    .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.
 
        }
 
    })
 //console.log(incrementalNr)
 //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.
https://www.youtube.com/watch?v=MTBVyk0YGv0&feature=emb_title

As for doing this, you can go through the actual Wix tutorial here.
https://support.wix.com/en/article/corvid-tutorial-sending-tracking-and-analytics-events
https://www.wix.com/corvid/reference/wix-window.html#trackEvent
https://www.wix.com/corvid/reference/wix-window.trackingParameters.html#ClickProductEvent
https://www.wix.com/corvid/reference/wix-window.trackingParameters.html#CustomEvent

Or with GTM.
https://support.wix.com/en/article/tracking-click-events-with-google-tag-manager

There were previous forum posts that showed how to do this.
https://www.wix.com/corvid/forum/community-discussion/is-there-a-way-to-make-a-button-add-1-to-a-collection-field-everytime-its-pressed
https://www.wix.com/corvid/forum/community-discussion/increment-button-with-1

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