Track Web Form submissions

Listen for the submission event from an embedded LiveSwitch Web Form and send it to Google Analytics, Google Tag Manager, ad platforms, or your own code.

When a person submits a LiveSwitch Web Form that is embedded on your website, the form sends a browser event to your page. Your page can listen for this event and use it to:

  • Count form submissions in Google Analytics 4 (GA4) or Google Tag Manager (GTM).
  • Record a conversion in Google Ads, Meta, LinkedIn, or Microsoft Advertising.
  • Connect a submission to the campaign (UTM values) that brought the visitor to your page.
  • Start your own code, for example show a message or open a different page.

This is optional. The form works without it.

📘

Before you start

  • The form must be embedded on your page with the embed code from LiveSwitch (an <iframe>). To get the code, open your form in LiveSwitch and click Show Embed Code. For WordPress, see Add a LiveSwitch Web Form to your website.
  • The event is available for forms made with the current form builder. Older forms do not send it. If your form shows View Embed Documentation instead of Show Embed Code, it is an older form. Make the form again to get the event.
  • You need access to your website's HTML, or to your tag manager.

How the event works

The form sends the event with the browser's postMessage function. The event goes from the form (inside the <iframe>) to the page that contains it.

  • The form sends the event only after LiveSwitch accepts the submission. If the submission fails, there is no event.
  • The form sends one event for each submission.
  • The form sends the event to any page that embeds it. Always check where the event comes from (see the code below).

Event data

The event's data is an object with these properties:

PropertyTypeDescription
typestringAlways "LS_FORM_SUBMIT". Use it to find this event among other messages.
formIdstringThe ID of the form.
formNamestringThe form's title.
fieldsarrayThe submitted answers, one item for each field. See the table below.
timestampstringThe time of the submission, in ISO 8601 format (2026-11-23T15:04:05.123Z).

Each item in fields has these properties:

PropertyTypeDescription
namestringThe field's label on the form, for example Email.
typestringThe kind of value: Text, Name, Email, Phone, Number, Date, or DateTime.
valuestringThe answer. Choices from checkboxes come as one string, separated by commas. An address comes as one line of text.

Example:

{
  "type": "LS_FORM_SUBMIT",
  "formId": "02523eac-c154-4d79-8f1c-64539f320235",
  "formName": "Get a Free Quote",
  "fields": [
    { "name": "Name", "type": "Name", "value": "Jane Doe" },
    { "name": "Email", "type": "Email", "value": "[email protected]" },
    { "name": "Services", "type": "Text", "value": "Moving, Packing" }
  ],
  "timestamp": "2026-11-23T15:04:05.123Z"
}
🚧

Do not send personal data to analytics tools

The fields array contains what the person typed, for example their name, email, and phone number. Google Analytics, Google Ads, and Meta do not allow you to send personal data in events. Send only formId and formName to these tools. Use fields only in your own code.


Step 1: Add the listener

Add this script to the page that contains the form. You can put it anywhere on the page. One script is sufficient for all LiveSwitch forms on the page.

<script>
window.addEventListener("message", function (event) {
  // Accept only events from LiveSwitch forms.
  if (event.origin !== "https://app.contact.liveswitch.com") return;
  if (!event.data || event.data.type !== "LS_FORM_SUBMIT") return;

  var formId = event.data.formId;
  var formName = event.data.formName;

  // Add your tracking code here (see the examples below).
  console.log("LiveSwitch form submitted:", formName, formId);
});
</script>

The first if line makes sure that the event comes from LiveSwitch. Do not remove it. If you remove it, other pages and scripts can send a false "submission" to your listener.


Step 2: Send the event to your tools

Put one or more of these examples where the listener says // Add your tracking code here. Each example first makes sure that the tool is on the page, so a missing tool does not cause an error.

Google Analytics 4 (gtag.js)

This example uses generate_lead, the event name that Google recommends for lead forms.

if (typeof gtag === "function") {
  gtag("event", "generate_lead", {
    form_id: formId,
    form_name: formName
  });
}

To see form_id and form_name in your GA4 reports, register them as custom dimensions (event scope) in GA4 Admin > Custom definitions. You can also mark generate_lead as a key event.

Google Tag Manager

Push the event to the data layer. Then use it in GTM.

window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
  event: "ls_form_submit",
  ls_form_id: formId,
  ls_form_name: formName
});

In GTM:

  1. Make a Custom Event trigger with the event name ls_form_submit.
  2. Make two Data Layer Variables: ls_form_id and ls_form_name.
  3. Use the trigger and the variables in your tags (GA4, Google Ads, Meta, and others).

If you use GTM, you can add the listener script from Step 1 to GTM as a Custom HTML tag that fires on All Pages.

Google Ads conversion

Replace AW-XXXXXXXXX/XXXXXXXXXXX with the send to value of your conversion action in Google Ads.

if (typeof gtag === "function") {
  gtag("event", "conversion", {
    send_to: "AW-XXXXXXXXX/XXXXXXXXXXX"
  });
}

Meta Pixel

if (typeof fbq === "function") {
  fbq("track", "Lead", {
    content_name: formName
  });
}

LinkedIn Insight Tag

Replace 1234567 with the conversion ID from LinkedIn Campaign Manager.

if (typeof window.lintrk === "function") {
  window.lintrk("track", { conversion_id: 1234567 });
}

Microsoft Advertising (UET)

window.uetq = window.uetq || [];
window.uetq.push("event", "submit_lead_form", {
  event_label: formName
});

Your own code

You can also do other actions after a submission, for example open a thank-you page on your website:

window.location.href = "/thank-you";

Track UTM values

UTM values are the utm_ parameters in the address of your page, for example:

https://example.com/quote?utm_source=google&utm_medium=cpc&utm_campaign=spring_sale

Most analytics tools record UTM values automatically when the visitor arrives on your page. Thus you usually do not have to do more. If you also want the UTM values in the submission event, add this script to your page. It saves the UTM values when the visitor arrives, and keeps them while the visitor goes to other pages of your website.

<script>
(function () {
  var keys = ["utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content"];
  var params = new URLSearchParams(window.location.search);
  var found = {};
  keys.forEach(function (key) {
    if (params.get(key)) found[key] = params.get(key);
  });
  try {
    if (Object.keys(found).length) {
      sessionStorage.setItem("ls_utm", JSON.stringify(found));
    }
  } catch (e) {
    // The browser does not allow storage. The UTM values are not saved.
  }
})();

function getSavedUtm() {
  try {
    return JSON.parse(sessionStorage.getItem("ls_utm")) || {};
  } catch (e) {
    return {};
  }
}
</script>

Then add the UTM values to the event in your listener. For example, with Google Tag Manager:

var utm = getSavedUtm();
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
  event: "ls_form_submit",
  ls_form_id: formId,
  ls_form_name: formName,
  utm_source: utm.utm_source,
  utm_medium: utm.utm_medium,
  utm_campaign: utm.utm_campaign
});

The UTM values stay on your website and in your analytics tools. LiveSwitch does not save them with the submission.


Full example

This example sends the submission to GA4 and GTM, adds the saved UTM values, and logs the event for testing. Use only the parts that you need.

<script>
window.addEventListener("message", function (event) {
  if (event.origin !== "https://app.contact.liveswitch.com") return;
  if (!event.data || event.data.type !== "LS_FORM_SUBMIT") return;

  var formId = event.data.formId;
  var formName = event.data.formName;
  var utm = typeof getSavedUtm === "function" ? getSavedUtm() : {};

  try {
    if (typeof gtag === "function") {
      gtag("event", "generate_lead", {
        form_id: formId,
        form_name: formName
      });
    }

    window.dataLayer = window.dataLayer || [];
    window.dataLayer.push({
      event: "ls_form_submit",
      ls_form_id: formId,
      ls_form_name: formName,
      utm_source: utm.utm_source,
      utm_medium: utm.utm_medium,
      utm_campaign: utm.utm_campaign
    });
  } catch (e) {
    // An error in a tracking tool must not stop the rest of the page.
    console.warn("Tracking error:", e);
  }

  console.log("LiveSwitch form submitted:", event.data);
});
</script>

Test your setup

  1. Open the page that contains the form.
  2. Open the browser's developer tools and go to the Console tab.
  3. Fill in the form and submit it.
  4. Make sure that you see LiveSwitch form submitted: in the console.
  5. Make sure that your tool received the event:
    • GA4: Admin > DebugView, or the Realtime report.
    • GTM: Preview mode (Tag Assistant). Look for ls_form_submit.
    • Meta: the Meta Pixel Helper browser extension, or Test events in Events Manager.

Each test submission is a real submission in LiveSwitch.


Troubleshooting

No event in the console

  • Make sure that the form is in an <iframe> on your page. If you open the form link directly, there is no page to receive the event.
  • Make sure that the form is not an older form (see Before you start).
  • Make sure that the submission was accepted. The form shows its thank-you message after a correct submission. If you see an error, there is no event.
  • Make sure that the address in event.origin is the same as the address in the form's embed code (https://app.contact.liveswitch.com).

The event shows in the console, but not in the analytics tool

  • Make sure that the tool's code (gtag, fbq, GTM, and others) is on the same page as the listener.
  • Ad blockers and privacy settings in the browser can stop analytics tools. Test in a browser without them.
  • GA4 and ad platforms can need some minutes to show new events in their normal reports. Use the test views in Test your setup for immediate results.

Events are missing on phones

If the form asks for photos or videos, LiveSwitch can open the upload page in the same browser tab on a phone, immediately after the event. Put your tracking calls first in the listener, and do not wait for slow work before you send them.


Need help? Contact Support at +1 844-587-9824 or [email protected].


Did this page help you?