Pushing Form Event to GA with gtag.js?
I'm trying to get Marketo form submissions to log as events in GA. The script I have tried is not working, and I *think* I know why: We use Tag Manager, but GA is actually deployed separately as a Global Site Tag (gtag.js) independent of GTM. This is different from analytics.js too, which, in previous threads, here and here, @Sanford Whiteman already provided solutions.
This is the code I have put together (from previous community posts:
<script>
form.onSuccess(function(vals,tyURL){
gtag('send', {
hitType: 'event',
eventCategory: 'Marketo',
eventAction: 'Conversion',
eventLabel: 'Content Download',
hitCallback: function() {
document.location.href = tyURL;
}
});
return false;
});
</script>Note that I changed the callback from "ga" to "gtag" with the hopes that it would work (which it did not).
So I'm looking for help with a couple of questions:
- Is there a different way to push this event back to GA with gtag.js?
- If so, where should the script go?
- Could I push it through GTM?
- Or do I need to place it in MKTO templates above or below the form?
[Bonus Points] Is it possible to call the Marketo form id (or form name, which would be even better) for eventLabel? I am pretty bad at JS (thus this help request), but could that look something like this?
Example:
eventLabel: "mkto.form.id": form.getId(),Thanks for any help! Here's a test page that is currently failing miserably...