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.

Setup at a glance
StepWhereWhat you do
1. Create a containertagmanager.google.comChoose Web as the platform and note the GTM- ID
2. Install the snippetsYour site's HTML or theme settingsHead snippet high in <head>, noscript snippet right after <body>
3. Add a tagTag Manager workspaceFor Analytics: tag type Google Tag with your G- ID
4. TestPreview buttonTag Assistant connects to your site and shows which tags fired
5. PublishSubmit in the workspaceNothing 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

Common Tag Manager triggers
TriggerFires whenTypical use
InitializationBefore all other triggers except Consent InitializationGoogle Tag for Analytics
Page ViewAs the browser begins to load a pageMost other page-level tags
History ChangeThe URL fragment changes or the page calls pushStatePage views in single-page apps
Custom EventYour code pushes a matching event to the data layerForm sends, sign-ups, bookings
ClickA visitor clicks an element or linkPhone 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

  1. Enter your container ID. The builder checks that it looks like GTM- followed by letters and numbers.
  2. Copy the head snippet into the <head> of every page, as high as you can.
  3. Copy the body snippet to the line right after the opening <body> tag.
  4. Open Tag Manager, click Preview, enter your site's address and confirm Tag Assistant says Connected.

Tips

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.

Start for free or see all free tools. We.Inc is free to start, with no card needed. AI building is on paid plans from $20 a month.

More in Guides

We.Inc is an AI-powered website builder you can resell under your own brand. Launch a branded client dashboard, bill on Stripe Connect, and deliver AI-generated websites in minutes. White-label plans start at $99 a month for 25 client sites, with a 7-day free trial and no per-site fees.

Product

Who It's For

Features

Resources

Company

View Sitemap