How to implement the JavaScript code for tracking form submissions as conversions in Google Ads.

Phone showing analytics or conversion data

To implement the JavaScript code for tracking form submissions as conversions in Google Ads, you’ll need to place it in the right place on your website. Here’s how and where to put the JavaScript code:

Where to Place the JavaScript Code:


On the Form Page:

You need to add the JavaScript code to the same page where the form is located.

Typically, you place the code just before the closing </body> tag of your page. This ensures that the form is fully loaded before the script runs.

Steps to Add the JavaScript Code:

Identify the Form: Find the HTML form element on your page. It may look something like this:


<form id=”yourFormId” action=”/submit” method=”POST”>

  <!– Form fields go here –>

  <input type=”submit” value=”Submit”>

</form>

Place the JavaScript Code: Right before the </body> tag (at the bottom of the HTML page), add the JavaScript code that triggers the Google Ads conversion tracking after the form submission.
Here’s the full code to add:
<script>

  document.getElementById(‘yourFormId’).addEventListener(‘submit’, function(event) {

    // Prevent the default form submission behavior

    event.preventDefault();

   

    // Your form submission code here (AJAX or form submit logic)

   

    // Fire the Google Ads conversion tracking event

    gtag(‘event’, ‘conversion’, {

      ‘send_to’: ‘AW-XXXXXXXXX/YYYYYYYYYY’ // Replace with your actual conversion ID and label

    });

    // After firing

Leave a Reply

Your email address will not be published. Required fields are marked *