En este tutorial voy a mostraros como añadir la etiqueta de Google Analytics a nuestra instalación de WordPress si necesidad de añadir ningún plugin adicional y haciendo uso de Google Tag Manager, el cual ya vimos como agregarlo en un tutorial anterior.
Let'S Watch three videos three possible varieties to add this tag: The one that Google Tag Manager offers us by default to install Universal Analytics, an altered way to add this tag and finally, how to add the new version that has released ago Little Google and the one that has called Global Analytics.
Add Universal Analytics
This is the simple and "default" way that Google Tag Manager offers to add the Google analytics tag.
To do so, once created our container in Google Tag Manager, we will have to create a escogiéndola tag of the templates that Tag Manager provides us (in this case that of Universal Analytics), a variable that collects the identifier of the property of Google Analytics to which we want to send the data of our Web and an activator that, initially, will be all the pages. But the best thing is to see it in the next video.
As you can see, it's very easy to add. Let'S move now to another less standard way to add this tag.
Alternative method for adding Universal Analytics
We will use in this case a label format that is defined in Google Tag Manager and that is a "wildcard" that will serve us to create not only the analytics, but in future videos we see that is very useful to create other labels that , by default, are not predefined in this tool, the HTML tag. As in the previous case, it's best to see it in the next video.
As you can see, the procedure is identical to the previous one, with the only difference that instead of using the default tag, we use an HTML tag in which we add the JavaScript Code of Google Analytics and personalize it to point to the property In which we want to send the data from our Website.
Como os comente al principio, Analytics ha cambiado el script, si deseáis seguir usando el código de Universal Analytics, lo podéis copiar este código a continuación. Eso si, cambiad la frase INSERTA AQUI EL IDENTIFICADOR DE TU PROPIEDAD DE ANALYTICS por el identificador que os proporciona Analytics, y que podréis encontrar en la sección de administración de vuestra propiedad de dicha herramienta. Su sintaxis es UA-XXXXXXXXX-X
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'INSERTA AQUI EL IDENTIFICADOR DE TU PROPIEDAD DE ANALYTICS', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
Add Global Analytics
At present, if enter to the Analytics management tool, you will find that the label you offer to insert is NO longer the Universal version, but it is a new version called Global Analytics.
At the time of writing this tutorial (March 2018) Google Tag Manager does not offer a predefined tag for Global Analytics, so if we wanted to insert the new tag on our Website, the option goes through using the method I have commented before, but in this case , inserting the Global Analytics code into the HTML tag. As always, let's look at it better with a video.
<?php
/*
Plugin Name: Insertar Gtag.js en WordPress
Plugin URI: https://carlosmdh.es
Description: Plugin para insertar los Scripts de Global Analytics
Version: 1.0.1
Author: carlosmdh
License: GPL 2+
License URI:https://carlosmdh.es
*/
// Añadir el código de Global Analytics en el <head>
add_action( 'wp_head', 'carlosmdh_global_analytics' );
function carlosmdh_global_analytics() { ?>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=INSERTA AQUI EL IDENTIFICADOR DE TU PROPIEDAD DE ANALYTICS"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'INSERTA AQUI EL IDENTIFICADOR DE TU PROPIEDAD DE UNIVERSAL ANALYTICS UA-XXXXXXXXX-Y', { 'anonymize_ip': true }); // Identificador de Google Analytics 3 (Universal Analytics
gtag('config', 'INSERTA AQUI EL IDENTIFICADOR DE TU PROPIEDAD DE GA4 G-XXXXXXXXXX'); //Identificador de Google Analytics 4 (GA4)
</script>
<!--End Global site tag (gtag.js) - Google Analytics -->
<?php }
Conclusion
Any of the three methods is valid to add the tracking code and start to follow and analyze what happens on your website. As you have seen in the videos, they are very easy to implement.
Como siempre, cualquier duda que os pueda surgir, puedes enviármela a través del formulario de contacto.