To test Google Tag Manager (GTM) locally, you can follow the steps below:
- Set up a local development environment: You can use a local server software like XAMPP or MAMP to set up a local development environment on your computer.
- Create a local HTML file: Create a simple HTML file on your local server, which will serve as your test page. You can create a basic HTML file using any text editor, and save it with a .html extension.
- Generate a preview mode snippet in GTM: Login to your GTM account and navigate to the desired container. In the container panel, click on the "Preview" button at the top right corner. Then, copy the provided preview mode snippet.
- Add GTM snippet to your local HTML file: Open the local HTML file you created earlier and paste the GTM preview mode snippet just before the closing tag. Save the HTML file.
- Publish GTM container: In GTM, click on "Submit" at the top right corner to publish your container changes. This step is necessary to make sure the container is in the "Published" state.
- Start the local server: Start your local server (e.g., XAMPP or MAMP) to host your HTML file.
- Access your test page: Open a web browser and navigate to your local test page by entering the URL provided by your local server (e.g., http://localhost/testpage.html).
- Open GTM Preview mode: While on your local test page, open a new tab in the same browser and navigate to your GTM account. Click on the "Preview" button. This will enable GTM preview mode for the current browser session.
- Test GTM functionality: With the GTM preview mode open and your local test page visible, you can now test the functionality of your GTM setup. You can check tag firing, custom events, data layer interactions, and debug your GTM configuration.
Remember to remove the GTM preview mode snippet and republish your container before deploying any live changes.
Note: Testing GTM locally may have limitations, especially if you have integrations with external services or rely on real-time user data. It is advisable to thoroughly test GTM in the actual production environment as well.