Integrating Personalize with Google Tag Manager (GTM) allows you to unlock advanced personalization features while streamlining your tracking and data collection process. GTM simplifies the implementation of tracking codes and event triggers, and when paired with Personalize, it helps deliver highly personalized content based on user behavior.
This guide provides step-by-step instructions to add the Personalize Edge SDK to your website using the Contentstack Personalize Actions GTM Template.
For an example Stack, Personalize project, and GTM container, see the Reference Project section at the bottom of this page.How to create a GTM account and container and install it on your site.
How to create tags for the Initialize, Trigger Impressions, Trigger Event, Set UserId, and Set Attributes actions using the Contentstack Personalize Actions template.
How to preview and publish your GTM container.
To get started with Google Tag Manager, you first need to create a Google Tag Manager account that will serve as the topmost level of organization for your company. Then, create one Tag Manager container for each of your company's websites and apps. Please refer to the official GTM documentation to create an account and container.
Note: Before you begin, ensure you have access to your website code to add the code snippets.
After you have created your account and container, follow these steps to install the necessary code snippets for your website's Google Tag Manager container:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':<p></p>
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],<p></p>
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=<p></p>
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);<p></p>
})(window,document,'script','dataLayer',''GTM-N2*****G');</script><p></p>
<!-- End Google Tag Manager -->
<title>Example Webpage</title><p></p>
</head>
<body>
<!-- Content of your website -->
<h1>Welcome to My Website</h1>
<p>This is a sample paragraph on my webpage.
</body>
</html><!DOCTYPE html>
<html lang="en">
<head>
<!-- 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-N2*****G');</script>
<!-- End Google Tag Manager -->
<title>Example Webpage</title>
</head>
<body>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id="GTM-N2*****G" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<!-- Content of your website -->
<h1>Welcome to My Website</h1>
<p>This is a sample paragraph on my webpage.</p>
</body>
</html>Note: If you are using a web framework, there may be an easier way to install the GTM tag. For example, Next.js provides a custom integration for GTM: Google Tag Manager.
This adds the code snippets to your website. Next, we use Google Tag Manager to set up and manage tags.
For this guide, we will be creating 5 tags for the Trigger Impressions, Trigger Event, Set Attributes, Set UserId, and the Initialize actions:
Use the following steps to set up the tag for the Initialize action in GTM.




Use the following steps to set up the tag for the Trigger Impressions action in GTM.



Use the following steps to set up the tag for the Trigger Event action in GTM.


This trigger fires on: Select Some Clicks.
This adds your tag for the Trigger Event action in GTM.
Use the following steps to set up the tag for the Set UserId action in GTM.


This adds your tag for the Set UserId action in GTM.
Use the following steps to set up the tag for the Set Attributes action in GTM.
This trigger fires on: Select Some Clicks.
After setting up your GTM Tags, you can preview and test your container configuration before publishing. Google Tag Manager’s preview and debug mode lets you test a container configuration on a site before publishing. It connects to Tag Assistant, allowing you to see which tags fired and their order. Please refer to the official GTM documentation for more information on Preview and Debug Mode.
After previewing and ensuring everything is working as expected, navigate back to your Workspace where we created the Container and then click the Submit button to publish a container with the tags you created.

This completes the set up for the GTM integration for Personalize.
Additional Resource: For more information on the Publishing, versions, and approvals, please refer to the official GTM documentation.
Common Issues
Debugging Tips

For further assistance, refer to the Google Tag Manager documentation or contact support.
You can refer to the following project for a reference implementation: https://github.com/contentstack-personalize-examples/nextjs-example-launch-gtm. It is built using the Next.js framework and is hosted on Contentstack Launch here: https://personalize-demo-with-gtm.contentstackapps.com.
You can find the Google Tag Manager container available in the GitHub repository as the gtm-container.json file. You can import the same into your Tag Manager account to get started with the same example.