# Install via Google Tag Manager

Canonical: https://sourcetag.io/docs/install-gtm

If you manage your site's scripts through Google Tag Manager (GTM), you can add SourceTag as a Custom HTML tag. This keeps all your tracking scripts in one place.

## Step 1: Copy your script tag

In your SourceTag dashboard, go to your site's detail page and copy the script tag. It looks like:

```html
<script src="https://cdn.sourcetag.io/scripts/YOUR_SITE_ID/st.js"></script>
```

## Step 2: Create a new tag

1. Open your GTM container at [tagmanager.google.com](https://tagmanager.google.com)
2. Go to **Tags** in the left sidebar
3. Click **New**
4. Name the tag something clear, like "SourceTag Attribution Script"


## Step 3: Configure the tag

1. Click **Tag Configuration**
2. Select **Custom HTML**
3. Paste the script tag in the HTML field:

```html
<script src="https://cdn.sourcetag.io/scripts/YOUR_SITE_ID/st.js"></script>
```

## Step 4: Set the trigger

1. Click **Triggering**
2. Select **All Pages** (the built-in Page View trigger)
3. Click **Save**

This fires the tag on every page load across your site.


## Step 5: Publish the container

1. Click **Submit** in the top right corner
2. Add a version name like "Added SourceTag"
3. Click **Publish**

The tag is now live. GTM injects the SourceTag script on every page where your GTM container is installed.

## Step 6: Test

You can test before publishing using GTM's Preview mode:

1. Click **Preview** in GTM
2. Enter your site URL with UTM parameters:

```text
https://yoursite.com/?utm_source=test&utm_medium=cpc&utm_campaign=gtm-test
```

3. In the GTM debug panel, confirm the "SourceTag Attribution Script" tag fired on the Page View event
4. Open your browser's developer tools and check **Application > Cookies** for a `_sourcetag` cookie
5. Submit a form on your site and check the submission for attribution data

## Important: tag firing order

SourceTag needs to run before any form submission happens. The **All Pages** trigger fires on the Page View event, which is early enough for most forms. However, if you have forms that load very early or via AJAX, be aware that:

- SourceTag uses a MutationObserver to detect forms added to the page after initial load, so dynamically loaded forms are handled
- If you're using GTM's built-in form submission tracking, SourceTag's hidden fields will be populated before GTM captures the form data

## Notes

- This works with any website that has GTM installed, regardless of platform.
- If you're also using GTM to manage cookie consent, you can add a consent condition to the SourceTag tag's trigger. For example, fire only after the visitor has accepted marketing cookies.
- Browser cookies are limited to 7 days when the script is loaded via GTM (since it's still a JavaScript cookie). For longer persistence, use the [WordPress plugin](https://sourcetag.io/docs/install-wordpress) or the [Cloudflare Worker](https://sourcetag.io/docs/cloudflare-worker-cookies) for server-side cookies.
- If you have multiple environments in GTM (staging, production), make sure to publish to the correct environment.
