@sorca_marian I have asked Chatgtp for help and he gave me a code to help fire a trigger in GTM. This is the answer i got and it works. (mind you to not put the " " in the trigger description of GTM, just formSubmitSuccess is enough )
To set up a Google Tag Manager (GTM) trigger and a tag that reacts to the addition of the class "form-submitted-successfully" to the body element of your webpage, you can follow these steps:
Step 1: Set Up a Custom Event Trigger
In this case, it's more efficient to directly send a custom event from your JavaScript function to GTM, instead of waiting for the class to be added. This gives you more control and ensures the tag fires at exactly the right moment. Here’s how you can modify your function:
<script>
function onFormPostSubmit() {
// Add your JavaScript logic here
document.body.classList.add("form-submitted-successfully");
// Send an event to Google Tag Manager
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'event': 'formSubmitSuccess'
});
}
</script>
Step 2: Create a Trigger in GTM
Log in to your Google Tag Manager account.
Navigate to the 'Triggers' section and click 'New'.
Choose the trigger type 'Custom Event'.
Enter the event name that you used in your dataLayer push, in this case, formSubmitSuccess.
Set the trigger to fire on all Custom Events that match this name.
Step 3: Create a Tag to Send to GA4
Return to the GTM overview and select 'Tags' followed by 'New'.
Choose the tag type 'GA4 Event'. If you haven't set up a GA4 configuration tag yet, you'll need to do that first.
Give the event a name, for example, 'form_submission'.
Add any event parameters as necessary. This could be specific information you want to track about the form submission, such as the values filled out (be mindful of privacy regulations).
Select the trigger you just created, 'formSubmitSuccess', for triggering.
Step 4: Test and Publish
Use the 'Preview' mode in GTM to test if your trigger and tag are functioning correctly. This shows you in real-time whether the events are being sent when the form is submitted.
Once you have verified that everything works correctly, click 'Publish' to push your changes live.
With these steps, you can create a robust and accurate tracking setup for form submissions on Squarespace, using GTM and GA4.
.