How to Add Google Tag Manager to Your Website
Create a container in Tag Manager to get an ID like GTM-ABCD123. Paste Google's first snippet as high in the <head> as possible and the second, a noscript iframe, immediately after the opening <body> tag on every page. Then add tags, such as your Google Analytics tag, inside Tag Manager and publish.
| Step | Where | What you do |
|---|---|---|
| 1. Create a container | tagmanager.google.com | Choose Web as the platform and note the GTM- ID |
| 2. Install the snippets | Your site's HTML or theme settings | Head snippet high in <head>, noscript snippet right after <body> |
| 3. Add a tag | Tag Manager workspace | For Analytics: tag type Google Tag with your G- ID |
| 4. Test | Preview button | Tag Assistant connects to your site and shows which tags fired |
| 5. Publish | Submit in the workspace | Nothing goes live on your site until you publish |
Updated
Google Tag Manager is a free container for tracking codes. Instead of pasting each tool's script into your site, you install one container once, then add, change or remove tags (small scripts for analytics, ads and similar tools) from Tag Manager's website. Type your container ID into the builder to get both snippets ready to paste.
Snippet builder
Type your container ID from the top of your Tag Manager workspace. Everything is built in your browser.
Container ID: GTM-ABCD123
Looks right. Paste both snippets on every page.
1. Paste as high in the <head> as possible
<!-- 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-ABCD123');</script>
<!-- End Google Tag Manager -->2. Paste immediately after the opening <body> tag
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-ABCD123"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->Push an event to the data layer
Use this when something happens that Tag Manager cannot see by itself, such as a form that sends without leaving the page.
Newsletter sign-up event
Create a Custom Event trigger named newsletter_signup in Tag Manager to fire tags on it.
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: "newsletter_signup",
signup_location: "footer"
});Triggers you will use first
| Trigger | Fires when | Typical use |
|---|---|---|
| Initialization | Before all other triggers except Consent Initialization | Google Tag for Analytics |
| Page View | As the browser begins to load a page | Most other page-level tags |
| History Change | The URL fragment changes or the page calls pushState | Page views in single-page apps |
| Custom Event | Your code pushes a matching event to the data layer | Form sends, sign-ups, bookings |
| Click | A visitor clicks an element or link | Phone and email link clicks |
Testing with Preview mode
In your workspace, click Preview at the top right. Tag Assistant opens in a new tab: enter your site's address and click Connect. Your site opens in a new window and shows Connected in the corner.
Back in the Tag Assistant tab, click Continue to see each event and which tags fired. Fix anything that did not fire before you publish.
Sending your own events
The data layer is a JavaScript array that passes information to Tag Manager. Push an object with an event key, like a newsletter sign-up, and build a Custom Event trigger with the same name.
Google's guide says the data layer is case sensitive, so dataLayer works and datalayer does not, and that you should only ever push to it, never reassign it.
How it works
- Enter your container ID. The builder checks that it looks like GTM- followed by letters and numbers.
- Copy the head snippet into the <head> of every page, as high as you can.
- Copy the body snippet to the line right after the opening <body> tag.
- Open Tag Manager, click Preview, enter your site's address and confirm Tag Assistant says Connected.
Tips
- Install one container per site. Google's data layer guide warns that more than one data layer per page makes tags misfire.
- If a page sets data for tags, put window.dataLayer = window.dataLayer || []; and your push calls above the container snippet.
- Remove any Analytics code you pasted directly before adding the same tag in Tag Manager, or every visit is counted twice.
- Many website builders have a field for a GTM ID. Use it if yours does, and paste nothing by hand.
Frequently asked questions
Where exactly does the Google Tag Manager code go?
Google's install help says to paste the first block as high in the <head> tag as possible and the second block immediately after the opening <body> tag, on every page of the site.
Do I still need the noscript part?
Google includes it in the install code. It is an iframe wrapped in a noscript element, which browsers only use when JavaScript is turned off. Paste it exactly as Google gives it to you.
How do I add Google Analytics 4 through Tag Manager?
In Tag Manager, add a new tag, choose Google Tag as the type and enter your tag ID starting with G-. Pick an initialization trigger, save, test it in Preview and then publish.
Why does Tag Manager not track page changes in my single-page app?
Apps that change pages without reloading do not trigger a fresh page load. Use the History Change trigger, which Google says fires when the URL fragment changes or when the site uses the HTML5 pushState API.
Is Google Tag Manager free?
The standard version is free to use. Google also sells Tag Manager 360 as part of its enterprise products.
Related: How to set up Google Analytics, Google Search Console guide, How to view page source, How to get your business on Google.
Build your site free or see all free tools. On We.Inc you can build your site free, with no card and no trial needed to start. AI building is on paid plans from $19 a month.
More in Guides
- How to Add Google Translate to Your Website
- How to Add Hotjar to Your Website
- How to Add HubSpot Chat to Your Website
- How to Add an Image Gallery to Your Website
- How to Add Intercom to Your Website
- How to Add the LinkedIn Insight Tag to Your Website
- How to Add Matomo to Your Website
- How to Add the Meta Pixel (Facebook Pixel) to Your Website
Product
Who It's For
- For Agencies
- For Freelancers
- For Hosting Providers
- For SaaS Platforms
- For Startups
- For Financial Services
- For Healthcare
Features
Resources
- Blog
- Help Center
- Free Business Tools
- Free UI Components
- Website Templates
- Template Collections
- Website Examples
- Guides
- Comparisons
- Alternatives
- Best Lists
- Reviews
- Pricing Guides
- Use Cases
- Glossary & Guides
- Integrations
- Developers
- Benchmark
- Windows App