How to add custom code for GTM and GA4 (dataLayer push) to Wix?

Product:
Wix Editor

What are you trying to achieve?
We need a way to track user interactions (like clicks, form submissions, purchases) and send them to GTM in a structured way. Instead of hardcoding individual events, we’re looking for a flexible system where we can trigger events dynamically from the frontend and pass them to GTM without constantly updating backend code.

What have you already tried?

  • We set up window.dataLayer in the frontend, but sometimes it’s undefined, making it unreliable.
  • We looked into Wix’s wix-analytics API but couldn’t import it successfully.
  • We tried using a Wix web module (.web.js) to send events from the frontend to the backend, which seems promising but still needs refining.
  • Checked Wix documentation and community posts but haven’t found a complete solution for this.

Additional information:

  • We deployed a backend function that receives event data and returns it to GTM.
  • We’re testing this with a simple phone number click event, but eventually, we want to track multiple interactions without rewriting backend code for each new event.
  • If anyone has experience setting up a scalable event tracking system in Wix, we’d love to hear your approach!