Objective:
Track successful footer form submissions on all pages of a Gatsby.js website and send the event generate_lead to Google Analytics 4 (GA4) using Google Tag Manager (GTM).
Step 1: Push a Custom Event to dataLayer on Form Submit
Update your form handling logic in Gatsby. After the form is successfully submitted (e.g. after a fetch or axios call completes), add the following code:
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: “generate_lead”,
form_location: “footer”
});
✅ This manually pushes a custom event (generate_lead) to GTM after the form is successfully submitted.
✅ The form_location parameter helps identify that the form was submitted from the footer.
Step 2: Create a Custom Event Trigger in GTM
- Go to your GTM container → Triggers
- Click New
- Name it: Custom – generate_lead
- Choose Trigger Type → Custom Event
- Event name: generate_lead
- Select “All Custom Events”
- Save the trigger
Step 3: Create a GA4 Event Tag
- Go to Tags → Click New
- Name it: GA4 – generate_lead
- Tag Configuration → Select GA4 Event
- Under Configuration Tag, select your GA4 config tag (or create one with your GA4 Measurement ID)
- Set Event Name to: generate_lead
- (Optional) Add event parameters: form_location : {{DLV – form_location}}
Step 4: Create a Data Layer Variable (Optional but Recommended)
- Go to Variables → Click New
- Name it: DLV – form_location
- Variable Type: Data Layer Variable
- Data Layer Variable Name: form_location
- Save
Step 5: Link Trigger to Tag
- In your GA4 – generate_lead tag, scroll to Triggering
- Select: Custom – generate_lead
- Save the tag
Step 6: Test in Preview Mode
- Click Preview in GTM
- Enter your site URL and connect
- Submit the footer form
- In Tag Assistant, check:
- generate_lead event appears in the event timeline
- Your GA4 – generate_lead tag Fires
- form_location value is passed correctly
Step 7: Publish
Click Submit → Publish in GTM when testing is complete
Result
Every time a user submits the footer form, a generate_lead event is fired and sent to GA4, with a clear label that it’s from the footer (form_location: “footer”)