Skip to content
HomeTraining

Send a form event

Correct a dataLayer.push and check its fields and types.

All exercisesSaved on this device

Your task

Implement trackLead(form): emit generate_lead only for a valid form. Take form_id from the input and set lead_source to website_form, value to the number 50 and currency to USD.

  • Uses the actual form ID
  • No event for invalid input
  • No name or email in the event

Practice site

Simulation
Field Studio

Discuss a project

A practice contact form. Clear a field to test invalid input.

Use test data. The form does not send a real request.

trackLead(form)
{
  "id": "contact_simple",
  "name": "Demo",
  "email": "learner@example.test",
  "valid": true
}

Run your solution to enable actions.

Your solution

JavaScript
function trackLead(form) {
// Send the event only when form.valid is true.
dataLayer.push({
event: 'generate_lead',
form_id: 'contact_main',
lead_source: 'website_form',
value: '50',
currency: 'USD'
});
}

Ctrl / ⌘ + Enter to run. Tab moves to the next control.

Run to apply your changes.
Hints and explanation

dataLayer.push adds a message to the local queue. The event field names the action. Other fields describe it. A string "50" and the number 50 are different values.

    Result

    Local output

    Run the solution, then try an action on the practice site.

    What this lab supports

    The lab supports dataLayer.push, Data Layer / Constant / Event / Custom JavaScript variables, Custom Event conditions and a local GA4 Event output. Custom HTML supports one inline script and the small DOM described in that exercise. No container, Preview connection, consent engine, templates, external scripts or GA4 delivery are included.

    Messages are processed in order. Objects merge, arrays replace previous arrays, and null clears a value. Dotted paths read the state at that event. This is a documented subset, not full GTM Data Layer Variable v2 behavior. {{Variable}} references work outside strings and comments; references inside template literals and regular expressions are unsupported.

    The JSON export is a lab project, not a GTM container. To transfer a solution, create the corresponding variables, trigger and tag in GTM and test them on your own site. Student code has no access to this site’s cookies, storage, network or production dataLayer.

    Google: the data layer