Adding onClick button event code



I need help with figuring out how to add onclick button event code to a submit button on the HubSpot form. I noticed that I can't access the button with JS. I tried adding an attribute of onClick but it didn't work for items within the shortcode - like submit button.

I found this conversation: Trigger Google Analytics Event with Form Submission and I tried the below code but it didnt work.

The onclick button event code I have looks like this:
onclick="dataLayer.push({'event': 'Newsletter', 'eventCategory': 'email', 'eventAction': 'submit'})"

Please help. I have no idea what I need to do here.

Any suggestions will be appreciated.



Hi @Helena, since we can't alter the HTML of the submit button directly, we should be able to use the onFormSubmit callback function that's built into HubSpot forms. Reading through some docs, it looks like this would be your best method:

ga('send', 'event', [eventCategory], [eventAction], [eventLabel], [eventValue], [fieldsObject]);

That code would go inside of the onFormSubmit callback like this:

    css: ‘’,
    portalId: ‘XXXXXXX’,
    onFormSubmit: function($form) {
        ga('send', 'event', [eventCategory], [eventAction], [eventLabel], [eventValue], [fieldsObject]);

Take a look at this thread for a bit more detail: Trigger Google Analytics Event with Form Submission


This is very helpful. Thank you!