Last active
March 9, 2019 00:42
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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