Reputation: 35
I want to call below code (Google Tracking code) if the form is validated successfully.
onclick="dataLayer.push({'event': 'NewsletterSignup'});"
If I add it to "onclick" event, it fires every time without considering the validation.
please let me know how should I call it only after validating the form. I searched in Google Tag Manager developer guide but could not find any help regarding this.
Please advise.
Upvotes: 0
Views: 3523
Reputation: 141
I think all you need is to change your onclick handler to include the validation. Something like this:
onclick="function() { if(validate_form()) dataLayer.push({'event':'NewsletterSignup'}); }"
I'm not a Javascript programmer so I hope that's the correct syntax. All I've done is to introduce the validation check into the onclick. If the check passes we push the event to the data layer (I assume that validate_form()
returns a boolean).
Your idea of firing the tracking code on the new page after the form is submitted is another possibility. That could work. You could just add a rule in GTM for that tag (tracking code) that causes it to fire on the new page (assuming that the only time that the new page is loaded is after a newsletter signup has occurred).
I think using the onclick handler above is the simplest solution.
Upvotes: 1