• Skip to main navigation
  • Skip to main content
  • Jump to the main sidebar

Carlosmdh

  • Migra ahora a Google Analytics 4
  • Blog
  • Snippets
  • Tutorials
  • Q&A GA4
  • About Me
  • Contact Me

Adding Google Tag Manager to WordPress

5 junio, 2021

Snippet for adding the Google Tag Manager container script to a WordPress installation.

Puedes ver como hacerlo en el siguiente tutorial: «Cómo añadir el código de Google Tag Manager a WordPress».

Mi recomendación es que incluyas este snippet en un plugin de funciones personalizadas.

<?php
/*
Plugin Name: Insertar Google Tag Manager en WordPress
Plugin URI: https://carlosmdh.es
Description: Plugin para insertar los Scripts de Google Tag Manager
Version: 1.0.0
Author: carlosmdh
License: GPL 2+
License URI:
*/
// Añadir el código de Google Tag Manager en el <head>
/* Debes sustituir el texto AGREGA AQUÍ EL ID DEL CONTENEDOR DE GTM con el identificador de 
tu contenedor de GTM que encontrarás en el apartado de “instalar Google Tag Manager de la 
sección “Contenedor” del Administrador de Google Tag Manager */
add_action( 'wp_head', 'carlosmdh_google_tag_manager1' );
function carlosmdh_google_tag_manager1() { ?>
	<!-- 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','AGREGA AQUÍ EL ID DEL CONTENEDOR DE GTM');</script>
	<!-- End Google Tag Manager -->
<?php }
// Añadir el código de Google Tag Manager code justo  debajo de la apertura de la etiqueta <body>
add_action( 'wp_footer', 'carlosmdh_google_tag_manager2' );
function carlosmdh_google_tag_manager2() { ?>
	<!-- Google Tag Manager (noscript) -->
	<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=AGREGA AQUÍ EL ID DEL CONTENEDOR DE GTM"
	height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
	<!-- End Google Tag Manager (noscript) -->
<?php }

I invite you to leave your impressions and/or doubts in the contact form and to suggest new topics that you would like me to cover in these tutorials. I will be happy to answer you by email and write in this blog.

No category

Sidebar

Sidebar-Alt

Blog - Snippets - Tutorials

Privacy Policy - Cookie Policy - Change your Consent


carlosmdh © 2023 · Made with by carlosmdh

We use cookies to give you the best experience on our website.

You can learn more about which cookies we use or disable them in the .

Carlosmdh
Powered by  GDPR Cookie Compliance
Privacy summary

This website uses cookies so that we can provide you with the best possible user experience. Cookie information is stored in your browser and performs functions such as recognising you when you return to our site or helping our team understand which parts of the site you find most interesting and useful.

Strictly necessary cookies

Strictly necessary cookies must always be enabled so that we can save your cookie settings preferences.

If you disable this cookie we will not be able to save your preferences. This means that each time you visit this website you will have to enable or disable cookies again.

Cookies de terceros

Esta web utiliza Google Analytics para recopilar información anónima tal como el número de visitantes del sitio, o las páginas más populares.

Dejar esta cookie activa nos permite mejorar nuestra web.

¡Por favor, activa primero las cookies estrictamente necesarias para que podamos guardar tus preferencias!

Cookies adicionales

Esta web utiliza las siguientes cookies adicionales:

(Lista aquí las cookies que estás utilizando en la web.)

¡Por favor, activa primero las cookies estrictamente necesarias para que podamos guardar tus preferencias!

  • Español
  • Français