Adding Google Tag Manager to your Salesforce site is easy. You just need to add two lines of code to the header of your site.
First, create a Google Tag Manager account and get your GTM code. Then, add the following code to the header of your Salesforce site:
<!-- 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-XXXXXXX');</script>
<!-- End Google Tag Manager -->
Replace "GTM-XXXXXXX" with your Google Tag Manager code.
You can also add Google Tag Manager code to specific pages on your site using the "Page Layout" editor in Salesforce. This is useful if you want to track specific events on certain pages, or if you want to use different Google Analytics tags for different pages on your site.
To do this, edit the page layout for the page you want to track and add the following code to the "HTML Header" section:
<!-- 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-XXXXXXX');</script>
<!-- End Google Tag Manager -->
Replace "GTM-XXXXXXX" with your Google Tag Manager code.
You can also add Google Tag Manager code to specific pages using Salesforce's Content Management System (CMS). This is useful if you want to track individual pieces of content, or if you want to use different Google Analytics tags for different pieces of content on your site.
To do this, edit the page or piece of content you want to track and add the following code to the "HTML Body" section:
<!-- 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-XXXXXXX');</script>
<!-- End Google Tag Manager -->
Replace "GTM-XXXXXXX" with your Google Tag Manager code.
You can find more information about adding Google Tag Manager to Salesforce sites here:
https://help.salesforce.com/articleView?id=000331207&type=1&mode=1