Download tracker/count

Hi,
I have a page where i will be offering downloadable Ebooks but i would like to track them
I have read you can do this but i am struggling to get it working.
The files/Docs are stored on a data collection.

I have done a download button
and a text box to record the numbers.

My data collection looks like this
File doc is the actual ebook and the next column is ebook download count to store the numbers
(only 4 live at the moment and the numbers i just added myself)

My database called “StepbyStepGuides”
The counter field called “ebookDownloadCount”
Displayed counter text on page: “text65”
Button to down load ebookn: “button29”
Dataset name “dataset2”

and the coding I copied and just altered names is this below:

import wixData from ‘wix-data’;

let clicktitle = $w(‘#text65’).text;

export function button29_click(event) {wixData.query(“StepbyStepGuides”).eq(‘title’,clicktitle).find().then((result) => {

let answersitem = result.items[0]; 

let id = answersitem._id; 

answersitem.ebookDownloadCount++; 

wixData.update("StepbyStepGuides",answersitem).then(()=>{ 

	$w('#dataset2').refresh(); 

}); 

})

}

I am a total noob at coding, so any help would be grateful
Thanks in advance

Hey Dean,
Try to change the type of the field “ebookDownloadCount” to ‘number’.
I can see from your collection image that the type is ‘text’

Best

Binyamin

Hi Binyamin,

Thank you, I have tried that and it still only puts the counter into the first item

I have made sure the “Text field” was changed to “Number field” in both the sandbox and the live collections.

thank you

Hey Dean,
It updates only the first item because this is the item you selected on this line:

let answersitem = result.items[0];

In order to update the right item, you need to select the right item:
Also, check that the value of ‘answersitem.ebookDownloadCount’ isn’t null

export function button29_click(event) {
 const data = $w("#repeater1").data; ///<- put here the repeater id insted of 'repeater1'
 let answersitem = data.find(item => item._id === event.context.itemId);

 if (!answersitem.ebookDownloadCount) { // the default value is null , you need to check if null ->  
       answersitem.ebookDownloadCount = 1;
    } else {
        answersitem.ebookDownloadCount++; // increment field by 1
    }

    wixData.update("StepbyStepGuides", answersitem).then(() => {

        $w("#dataset2").refresh();
    });

}

@binyaminm

Thats amazing, works perfectly.

Thank you for your help, I was just about to give up.

@binyaminm
Sorry to bug you again, I have had a few hours playing with it to no luck

Is there a way I can use the same coding above but have the click attached to an image instead of a button? (The Blue Book Image)

Its all part of the same collection as before but its on a different page but i want to use a image instead of a button.

Thanks in advance

Hi everybody,

I have tried to apply the method described here, but failed.
No result appear on my page, and no change shows up in the database.

In my case,
My database called “Rapports”
The counter field called “compteurRef1”
Displayed counter text on page: “text365”
Button to down load ebookn: “button43”
Dataset name “dataset1”

Below is the code I used…

import wixData from ‘wix-data’ ;

let clicktitle = $w ( ‘#text365’ ). text ;

export function button43_click ( event ) {
const data = $w ( “#repeater15” ). data ;
let answersitem = data . find ( item => item . _id === event . context . itemId );

if (! answersitem . compteurRef1 ) {
answersitem . compteurRef1 = 1 ;
} else {
answersitem . CompteurRef1 ++;
}

wixData . update ( "Rapports" ,  answersitem ). then (() => { 

    $w ( "#dataset1" ). refresh (); 
}); 

}

What’s wrong? Should I change CountRef1’s properties in numero instead of Text? I tried and it did’nt solve the issue.

Additionally, I have only tried the preview mode… Is that the reason?

Kind regards,

Philippe