To set up Google Analytics in Webflow to track only your production site and trigger an event on successful form submissions, follow these steps:
1. Add Google Analytics to Your Webflow Site
- Go to Project Settings in your Webflow dashboard.
- Navigate to the Integrations tab.
- Locate the Google Analytics section and enter your Google Analytics tracking ID (e.g., UA-XXXXXXXXX-X for Universal Analytics or G-XXXXXXXXXX for GA4).
2. Limit Tracking to Production Site
- Go to the Hosting tab in Project Settings.
- Scroll down to Custom Code and Open Head Code.
- Insert conditional JavaScript to check if the site is on the production domain:
if (window.location.hostname === 'www.yourdomain.com') { / Your Google Analytics script here / }.
3. Set Up Event Tracking for Form Submissions
- In the Custom Code section under Before Body Tag, add a script to track form submissions:
- For Universal Analytics:
```javascript
<script>
document.addEventListener('submit', function(e) {
if (e.target.matches('form')) {
gtag('event', 'form_submit', {
'event_category': 'Form',
'event_label': e.target.getAttribute('name')
});
}
});
</script>
```
- Ensure this script is within the conditional statement if only required for production.
4. Publish Your Site
- Publish your Webflow site to the custom domain.
- Ensure all scripts and conditional checks are functioning by visiting the domain and testing a form submission.
Summary
To set up Google Analytics on your Webflow site for tracking on a production domain and capturing form submission events, you need to add your tracking ID to the project, ensure tracking runs only on the production domain, and append a form submission tracking script with conditional logic.