I need to track an event from Google Analytics. Its just a submit button to track each time a user fills up the contact form.
Ive got this code but it doesnt work:
import wixWindow from ‘wix-window’;
export function button6_click(event) {
wixWindow.trackEvent(“CustomEvent”, {
“event”: “ctozacion”,
“eventCategory”: “form”,
“eventAction”: “Click”,
“eventLabel”: $w(‘#dataset1’).getCurrentItem().title
} );
}
The line “eventLabel”: $w(‘#dataset1’).getCurrentItem().title it gives me an errror that says
#dataset1 is not a valid parameter.
Does anybody know how this code works or what do I have to fix?
Check that your dataset is actually called dataset1. Just right click on the dataset element > view properties > ID field
Thanks for the anwer! Where do I access the dataset setting?
Just hover over the dataset element on your page and it will show the dataset’s id name on the top left.
Or just simply right click the dataset and choose properties and view the dataset’s id name in the properties panel.
If you haven’t got a dataset on your page, then read how to do it here:
https://support.wix.com/en/article/corvid-tutorial-displaying-database-content-on-a-regular-page#6-add-and-configure-a-dataset231
thanks very much, so each time I create a contact form, I have to link a database to it=?
@vonsteffan
You will need a dataset for any form that you use:
https://support.wix.com/en/article/creating-a-form-with-user-input-elements
Even if you just use Wix Forms to do it all for you, Wix will still put a dataset in your site structure automatically.
https://support.wix.com/en/article/creating-a-wix-forms-submissions-table
@givemeawhisky thanks for your help! but it doesnt work. In google analytics, my event name is cotizacion, and I only filled two fields, category:form and event: click
and my code in wix is:

and my code in wix is:
import wixWindow from ‘wix-window’;
export function button6_click(event) {
wixWindow.trackEvent(“CustomEvent”, {
“event”: “ctozacion”,
“eventCategory”: “form”,
“eventAction”: “Click”, “eventLabel”: $w(’ #dataset1 ').getCurrentItem().title } ); }
and my form is:

the database name in code is correct
If you create any user input form in Wix then you will need a dataset on the page that the form is linked to, this is where all the user inputs are collected, as in your case with your contact form it would be the messages that get saved in the collection.
Have you actually added Google Analytics to Wix?
https://support.wix.com/en/marketing-tools-analytics/google-analytics
Pllus, I am assuming that you are not using a free Wix site as Google Analytics will not work.
https://support.wix.com/en/article/using-google-analytics-on-free-wix-sites
I think you are following this tutorial here.
https://support.wix.com/en/article/corvid-tutorial-sending-tracking-and-analytics-events
For help with adding event handlers.
https://support.wix.com/en/article/corvid-reacting-to-user-actions-using-events
If you are then make sure that you use all of the code correctly from the tutorial:
import wixWindow from 'wix-window';
$w.onReady(function () {
export function button6_click(event) {
wixWindow.trackEvent("CustomEvent", {
"event": "cotizacion",
"eventCategory": "form",
"eventAction": "click",
"eventLabel": $w('#dataset1').getCurrentItem().title
} );
}
You might find using Google Tag Manager easier.
https://support.wix.com/en/article/tracking-click-events-with-google-tag-manager
Connecting Your Google Tag Manager Account to Your Wix Site | Help Center | Wix.com
Finally, just a thought, how are you using the submit button as it will affect how your code runs.
- Add a regular button and use onClick event handler.
From there you can submit the form bound to a dataset using
$('#dataset1').save() //(replace #dataset1 with actual ID of your dataset)
You can add event handlers by clicking + in the properties panel for the element.
- Submit the form using regular button bound to save action and perform additional actions when form is submitted in onAfterSave handler on a DataSet.
As in like Step 6 from this tutorial.
CMS: Creating a Custom Form | Help Center | Wix.com
Like I have done with my own contact form…
$w.onReady(function () {
$w("#PublicContactUs").onAfterSave(() => {
thanks a lot for your help, I ll check those links and see what I can do. Thanks!