Globe Boss logo with tagline 'Rising to the Top'.

Track ajax forms with google tag manager and google analytics 4

Computer displaying AJAX Tag Manager dashboard and analytics.

Tracking AJAX Forms with Google Tag Manager and Google Analytics 4

Tracking AJAX forms can pose unique challenges, particularly because these forms often submit data without reloading the page. This means that traditional form submission tracking methods may not work as expected. Fortunately, Google Tag Manager (GTM) and Google Analytics 4 (GA4) offer powerful tools to easily set up tracking for AJAX forms. Below, we’ll walk through the process step-by-step.

Understanding AJAX Forms and the Need for Tracking

AJAX (Asynchronous JavaScript and XML) allows web forms to send and retrieve data asynchronously, meaning users can interact with the page without interruption. While this enhances user experience, it can complicate tracking user actions in analytics platforms. Tracking form submissions is essential for measuring performance, understanding user behavior, and optimizing conversions—hence the need for a robust solution like GTM and GA4.

Step-by-Step Guide to Setup

1. Prepare Your AJAX Form

Before you set up tracking in GTM, ensure your AJAX form is fully functional. When the form is submitted, it should trigger a specific event in the browser. For this guide, we will create a custom event that GTM can listen to.

Here’s an example of how you might define an event in your JavaScript code upon form submission:

javascript
document.getElementById(‘myAjaxForm’).addEventListener(‘submit’, function(e) {
e.preventDefault(); // Prevent default form submission

// AJAX form submission code here...
// Trigger Google Tag Manager event
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'ajaxFormSubmit',
formId: 'myAjaxForm'
});

});

In this snippet, the dataLayer.push() method creates a custom event called ajaxFormSubmit, which includes an identifier for the form.

2. Configuring Google Tag Manager

Once your form is set up to push events to the dataLayer, the next step is to configure GTM.

  • Create a New Trigger:

    1. Log into GTM and navigate to Triggers.
    2. Click on “New” and select “Custom Event”.
    3. Name your trigger (e.g., “AJAX Form Submission”).
    4. Set the event name to match the one defined in your JavaScript (ajaxFormSubmit).
    5. Optionally, configure conditions to limit when this trigger fires on specific forms or use the form ID as a specified condition.

  • Create a New Tag:

    1. Head to Tags and click on “New”.
    2. Select “Google Analytics: GA4 Event” as the tag type.
    3. Choose your GA4 configuration variable (or create one if you haven’t already).
    4. Set the Event Name (e.g., form_submission).
    5. Add parameters to the event if needed, such as formId to track which form was submitted. This could mean adding a parameter like { "form_id": "{{Trigger Form ID Variable}}" }.

  • Publish Your Changes:
    After configuring both the trigger and the tag, don’t forget to publish your changes in GTM.

3. Setting Up Google Analytics 4 to Receive Events

With GTM configured to push event data to GA4, the final step is to ensure GA4 is set up to receive and process this event data.

  • Configure Events in GA4:

    1. Log into GA4 and navigate to “Events”.
    2. Check to see if your event (form_submission) is listed. It will appear here after a trigger has been fired.
    3. If it appears, you can click on it to set up conversions if you wish to measure form submissions as conversions.
    4. If you haven’t seen an event yet, it’s a good idea to test the setup by submitting the AJAX form and confirming that the event appears in real-time data.

4. Testing and Debugging

Testing is a crucial step before going live. Use the GTM Preview mode to validate that your triggers fire as expected when the AJAX form is submitted. You can view incoming events in real-time in GA4 as well to ensure data is being tracked accurately.

To troubleshoot any issues, check the console for JavaScript errors and ensure that the GTM container is correctly set up and published.

Best Practices for AJAX Form Tracking

  • Utilize Descriptive Event Names: Use clear and descriptive names for your events to make them easily identifiable within GA4.

  • Leverage Parameters: Include additional parameters for more granular tracking, such as form field values or user segments.

  • Maintain User Privacy: Be mindful of user data and privacy considerations when tracking form submissions.

By effectively tracking AJAX forms through Google Tag Manager and Google Analytics 4, you can unlock detailed insights into user interactions, helping refine marketing strategies and improve user experience.