Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.