Jump to content

Trying to add additional behavior to the onclick handler of a submission button

Recommended Posts

Hello,

I'm in the process of adding Amplitude tracking to our site and ran into a small problem. I would like to add the following behavior to the existing onclick handler of the submission button (in this example the newsletter submission button):

   const newsletterSubmissionHandler = () => {
      const eventProperties = {
       page: window.location.href
     };
       amplitude.track('Form Submitted - Newsletter', eventProperties);
    }

So far I've tried the following code injection without success: 

   const newsletterButton = document.getElementsByClassName("newsletter-form-button")[0];
   const newsletterSubmissionHandler = () => {
      const eventProperties = {
       page: window.location.href
     };
       amplitude.track('Form Submitted - Newsletter', eventProperties);
    }
    addEvent(newsletterButton, "click", newsletterSubmissionHandler);

I'm wondering what's proper way to do this? I haven't had much success with searching online since adding onclick behaviors dynamically seems like a niche case. 

Link to comment
  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.