How to send a custom event to Google Analytics 4 using Velo code?

Question:
How to send a custom event to Google Analytics 4 using Velo code?

Product:
Studio, WIX Velo

What are you trying to achieve:
I want to send a custom event to GA4 using WIX Velo. For example, if a user clicks on an element, I want to track that event and have it in my GA4 property. I already installed my gtag on my website.

The documentation for trackEvent (https://www.wix.com/velo/reference/wix-window-frontend/trackevent) only supports events for Universal Analytics (with the old configuration of eventCategory, eventValue, etc).

What have you already tried:
I’ve tried using code from Stackoverflow, but WIX does not seem to support window.datalayer (javascript - How to programatically send an event to GA4 without Google Tag Manager? - Stack Overflow)

I do not want to use GTM. I want to send the event from Velo code.

Can someone help me with that? Thank you!

If you need to run arbitrary JavaScript you can use a Custom Element to do so: About Custom Elements You can pass messages back and forth from your page code to the Custom Element as well so then you’ll be able to fire off custom events.

If you’d like Wix to support newer and custom events with trackEvents then submit a feature request on the roadmap: Product Roadmap

I’m just now having some luck with this example.

I wrapped it as a function that is called on onReady() and I seem to be seeing stuff come through. Is that what you’re after @vinnysantini ?

I decided to go with Google Tag Manager instead because I was unable to send the events to GA4 using the trackEvent function since it does not allow sending custom properties for events for GA4 (it only supports Universal Analytics properties, like eventCategory, eventAction, eventLabel and eventValue).

For example, I need to be able to send an event on a successful form submission, and send the value of a field as a custom property in the event. I was unable to achieve that with trackEvent for GA4.

While I’m not familiar with different G4A/GTM versions it does seem that in the versions that trackEvent supports according to the docs it’s possible to provide any number of arbitrary properties:

If this isn’t working correctly then can you share your code and a bug report with customer care: Contact Wix ?