Skip to content

Instantly share code, notes, and snippets.

@fernandiez
Last active December 20, 2017 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fernandiez/585d2402b7325d31f77434a4231251e9 to your computer and use it in GitHub Desktop.
Save fernandiez/585d2402b7325d31f77434a4231251e9 to your computer and use it in GitHub Desktop.
Simple Google Analytics WordPress plugin
<?php
/**
*
* Simple Google Analytics WordPress plugin
*
* @link https://www.fernan.com.es/
* @since 1.0.0
*
* Plugin Name: Fernan Google Analytics
* Plugin URI: https://www.fernan.com.es/
* Description: Plugin para añadir Google Analytics en el sitio web.
* Version: 1.0.0
* Author: Fernan Díez
* Author URI: https://www.fernan.com.es/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: google-analytics-plugin
* Domain Path: /languages
*/
// Adds Google Analytics code to the header
function fernan_analytics_code() { ?>
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXX-1');
</script>
<?php }
add_action('wp_head', 'fernan_analytics_code');
@fernandiez
Copy link
Author

fernandiez commented Dec 20, 2017

Important reminder!

Replace in both places:
"UA-XXXXXXXX-1"
with your tracking code:
"Your Google Analytics own tracking code"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment