Count number of downloads (or button clicks) from a dynamic page dataset

I’ve seen dozens of explanations here but none worked, and some I couldn’t even understand.

I am NOT tech-savvy, could someone please tell me how I could use Wix VELO to count how many times an user clicked on the download button in a dynamic page? in a simple and straightforward way?

I use dynamic pages to list e-books to be downloaded for free and I want to know how many downloads each one has.

Thanks :slight_smile:

  1. Find the button on your dynamic-page you want to count the downloads.
  2. Create a new Field in your database, called something like Click-Count or similar, where you will store all the counter for all your items inside your database.
  3. Generate some code like…

Qhick example-code without any testings…


$w.onReady(()=>{let myNewCount;
    $w('#myDatasetIdHere').onReady(()=>{
        let myCurrentItem = $w("#myDatasetIdHere").getCurrentItem();
        console.log("Cur-Item: ", myCurrentItem);      
 
        $w('#myButtonIdHere').onClick(()=>{
             //adding +1 to the counter ...
             myNewCount = myCurrentItem.clickCount+1;  console.log(myNewCount);
              $w("#myDatasetIdHere").save()
              .then( (item) => {let fieldValue = item.fieldName;} )
              .catch( (err) => {let errMsg = err; } );
        });
        
          $w("#myDatasetIdHere").onBeforeSave(() => {           
                   $w("#myDatasetIdHere").setFieldValue("clickCounter", myNewCount);
           });
    });
});

It worked like a charm, when it comes to counting, thanks so much! I love your posts, by the way. You’re always so helpful!

But now, the button isn’t downloading the document stored in the database anymore. Do you have any workaround or did I do something stupid (most probably)?

1 Like

What does show the console ??? Which log-entries do you see inside your console?
Do you work with → CONSOLE <— ??? If not, than it is the best point to start doing this.
Remember → CONSOLE is your BEST-FRIEND <----
Who can work with the CONSOLE the right way → won’t need much help anymore when it comes to debuging.

Use logs like…

$w.onReady(()=>{ console.log('Page ready...');
    $w(‘#myDatasetIdHere’).onReady(()=>{console.log('Dataset ready...');
         $w('#myButtonIdHere').onClick(()=>{console.log('click');

         });
    });
});

Those simple console logs will help you to find the error-part of your code.
All you have to do is to follow those logs inside your console, and take a look where it do stop at what step does break your code, or does your code break at all.

Then you try to think logical step by step.
Step-1: What happens?
Step-1: Is my first step complete?
Step-1: Or do i already have errors in first step?
If STEP-1 is ok → check next step… —> Jump to next log…

Step-2: What happens?
Step-2: Is my second step complete?
Step-2: Or do i already have errors in second step?
If STEP-2 is ok → check next step… —> Jump to next log…

…and so on…

You can use either the integrated CONSOLE inside the Wix-Editor or you do it on your live-site using your browser.

I would recommend to do it on the live-site (Press-F12) on your BROWSER and navigate to CONSOLE. Check for all the generated logs and find the error.

If no errors found and your code is working fine, but still something is missing, start brainstorming again.

-What is missing?
-Synchronous or Asynchronous running code? Do i have to wait for something?
-Did i change the buttons behaviour?
-Did i changed some connections inside my DATASET ?

Maybe i should test my old code one more time and take a look what has been changed and compare.

All these steps will guide to to your solution.

If you are still not able to get your solution at the end, post here again, showing all the related code to your issue…