Skip to content

Instantly share code, notes, and snippets.

@luclemo
Last active March 9, 2019 00:42
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 luclemo/0a1152c752be012612ca0135f7c4e16a to your computer and use it in GitHub Desktop.
Save luclemo/0a1152c752be012612ca0135f7c4e16a to your computer and use it in GitHub Desktop.
Adds a Google analytics tracking code to the <head> of your theme. #wordpress
<?php
/*
Plugin Name: THEME NAME Google Analytics Plugin
Description: Adds a Google analytics tracking code to the <head> of your theme, by hooking to wp_head.
Author: Lucas Lemonnier
*/
/*
* Replace all instances of XXXXXXXXX-X with your GA code
*/
function theme_google_analytics() { ?>
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-X"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXXX-X');
</script>
<?php }
add_action( 'wp_head', 'theme_google_analytics', 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment