# Add Google Tag Manager

Source: https://help.deftform.com/nice-to-know/add-google-tag-manager-to-your-forms

If you want to add Google Tag Manager to any of your forms, you can do this by creating a script. Log-in to your account, then visit the “Scripts” settings at <https://deftform.com/settings/scripts>.

### Add the script

Click the “Add Script" button and paste the GTM script into the textarea. You should also give the script a unique name; you’ll need it for the next step.

### Attach the script to one or more forms.

Next, edit your form and open the form settings. In the “General” section, scroll down to the “Form scripts” section and click “Add.” Select your script and **be sure to click “Attach script”**.

![Screenshot 2025-11-20 at 09.56.46.png](https://cdn.eniston.com/bases/6a64050d-dfb7-47ff-a200-5c7f8d9dcda2/attachments/1763629508_691ed9c4b65eb.png)

### Initialize on load or after form submit

After you attach the script, you have the option to determine if the script should load when the page loads or after the form has been submitted. For the initial GTM script, you can leave it with the default setting (on page load).

![Screenshot 2025-11-20 at 09.57.55.png](https://cdn.eniston.com/bases/6a64050d-dfb7-47ff-a200-5c7f8d9dcda2/attachments/1763629226_691ed8aaafaa8.png)

For any events you want to submit to the GTM container *after* the form has been submitted, repeat the process, but make sure to click the “after submit” icon (from the screenshot above). When it’s green, the script will be executed after a form has been successfully submitted.

### Adding the main GTM script

In order for any events to be tracked, you will, of course, need to add the main GTM script to your form as well. This should load on page load (not after form submission). The script looks something like this:

```javascript
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXXXX');</script>
<!-- End Google Tag Manager -->
```

Make sure to replace `GTM-XXXXXXXX` with your own ID.

---

### Example tracking scripts

**Basic Event Tracking**

```javascript
dataLayer.push({
  'event': 'custom_event',
  'event_category': 'engagement',
  'event_action': 'visit',
  'event_label': 'demo_form'
});
```

**Form Submission Tracking**

```javascript
dataLayer.push({
  'event': 'form_submit',
  'event_category': 'form',
  'event_action': 'submit',
  'event_label': 'contact_form',
  'form_id': 'contact-us',
  'form_name': 'Contact Form'
});
```
