Tracking Download Clicks getCurrentItem() Error

I’m following this tutorial trying to add tracking to a download button in a repeater dataset. I can’t figure out why I’m getting this error on the bottom line getCurrentItem(). Any help would be appreciated.

import wixWindow from ‘wix-window’ ;

export function downloadButton_click ( event ) {
wixWindow . trackEvent ( “CustomEvent” , {
“event” : “Document Download” ,
“eventCategory” : “Downloads” ,
“eventAction” : “Download” ,
“eventLabel” : $w ( ‘#DownloadWPButton’ ). getCurrentItem (). title
} );
}

Screenshot:

The getCurrentItem() only works on a Dataset element on the page.

https://www.wix.com/velo/reference/wix-dataset/dataset/getcurrentitem

Also is this button in a repeater? If so this would have to be done with the onItemReady for the repeater and then set the eventLabel to itemData.title.

https://www.wix.com/velo/reference/$w/repeater/onitemready

1 Like

I’m confused. Isn’t my button a dataset item on the page? It allows the person to download a file that’s in a dataset. This is on the child page of a dynamic page and is repeated across all other child pages. Thanks for any additional help.

@daniel3614

Like Jarod already mentioned → getCurrentItem only works on DATASETS!
You are doing it on a → BUTTON! ← That will not work!

"eventLabel":$w('#DownloadWPButton').getCurrentItem().title

Comparing the code from the tutorial:

import wixWindow from 'wix-window';

export function downloadButton_click(event) {
  wixWindow.trackEvent("CustomEvent", {
    "event": "Document Download",
    "eventCategory": "Downloads",
    "eventAction": "Download",
    "eventLabel": $w('#dataset1').getCurrentItem().title
  } );
}

With your code → you surely recognize the difference:

import wixWindow from 'wix-window';

export function downloadButton_click(event) {
  wixWindow.trackEvent("CustomEvent", {
    "event": "Document Download",
    "eventCategory": "Downloads",
    "eventAction": "Download",
    "eventLabel": $w('#DownloadWPButton').getCurrentItem().title
  } );
}

Delete you event-connection of your button in the property-panel & try this one…

import wixWindow from 'wix-window';

$w.onReady(async function() {console.log("Page is ready!");
   $w('#dataset1').onReady(()=>{console.log("Dataset is ready!");
        $w('#downloadButton').onClick((event)=>{
            console.log("Button " + event.target.id + " clicked!");
            start_Tracking();
         });
    });
});

function start_Tracking() {
    wixWindow.trackEvent("CustomEvent", {
        "event": "Document Download",
        "eventCategory": "Downloads",
        "eventAction": "Download",
        "eventLabel": $w('#dataset1').getCurrentItem().title
    });
    
}

But even, with this correction, it seems that still is somwthing not right or missing!
We have here a normal → DATASET ← and we are using → getCurrentItem().

Normaly getCurrentItem-methods are used on → DYNAMIC-DATASETS.
In This case your REPEATER must be connected in the property-panel with the DATASET, if getCurrentItem() shall work like expected.

Another version would be to go the CODING way → using REPEATERS → onItemReady

import wixWindow from 'wix-window';

$w.onReady(async function() {console.log("Page is ready!");
   $w('#REPEATER').onItemReady(($item, itemData, index)=>{
        $w('#downloadButton').onClick((event)=>{
            console.log("Button " + event.target.id + " clicked!");
            wixWindow.trackEvent("CustomEvent", {
                "event": "Document Download",
                "eventCategory": "Downloads",
                "eventAction": "Download",
                "eventLabel": itemData.title
            });
        });
    });
});

This was also mentioned by Jarod already.

@Jarod Dykstra → to your question…
Also is this button in a repeater? If so this would have to be done with the onItemReady for the repeater and then set the eventLabel to itemData.title.

I think you will already know the answer → Because no → DYNAMIC-DATASET exists, the button must be inside REPEATER, else this would not work at all.

There multiple possible scenarios…

  1. Using dynamic dataset.
  2. Using a repeater.
  3. Using a table.
  4. Using a gallary.
    --------------------------------------------- not sure-----------------------------------------------------------------
  5. Even the usage of Multi-State-Boxes & Slide-Shows would be possible…but this for you may would have to use the next provided function → getItems().
    --------------------------------------------- not sure-----------------------------------------------------------------

Another possible solution could also be the usage of this one…

1 Like

Here is my code now which appears to work. Thank you for the long explanation. Very helpful! I do have a dynamic dataset, it’s literally called dynamicDataset. I was confused by the tutorial I was following thinking that because the button is linked to a dataset it would work to use DownloadWPButton, but that button is already linked to the dataset so by simply changing it to dynamicDataset it’s collecting what is linked to that button. Again, thank you for the schooling. Very clear! @jarod @russian-dima

import wixWindow from ‘wix-window’ ;

export function downloadButton_click ( event ) {
wixWindow . trackEvent ( “Whitepaper Download” , {
“event” : “Document Download” ,
“eventCategory” : “Downloads” ,
“eventAction” : “Download” ,
“eventLabel” : $w ( ‘#dynamicDataset’ ). getCurrentItem (). title
} );
}

1 Like

One last tip → always use a CODE-BLOCK to show your CODE.
Why? → Better code-readebility, fater code-recogniction, better format, lower space-consumption & more OVERVIEW & at least → more efficient.

Your format:
import wixWindow from ‘wix-window’ ;

export function downloadButton_click ( event ) {
wixWindow . trackEvent ( “Whitepaper Download” , {
“event” : “Document Download” ,
“eventCategory” : “Downloads” ,
“eventAction” : “Download” ,
“eventLabel” : $w ( ‘#dynamicDataset’ ). getCurrentItem (). title
} );
}

CODE-BLOCK-CODE:

import wixWindow from 'wix-window';

export function downloadButton_click(event) {
  wixWindow.trackEvent("Whitepaper Download", {
    "event": "Document Download",
    "eventCategory": "Downloads",
    "eventAction": "Download",
    "eventLabel": $w('#dynamicDataset').getCurrentItem().title
  } );
}

Do you feel the difference? A real CODER emmidiately feels the difference between this two demonstrated code-formats.:wink:

1 Like