Created
June 27, 2012 05:52
-
-
Save trepmal/3001766 to your computer and use it in GitHub Desktop.
Basic Google Analytics - updated here: https://gist.github.com/trepmal/3389856
This file contains hidden or 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: Basic Google Analytics | |
new Basic_Google_Analytics(); | |
class Basic_Google_Analytics { | |
function __construct( ) { | |
add_action( 'admin_init' , array( &$this , 'register_fields' ) ); | |
add_action( 'init' , array( &$this , 'load_after' ) ); | |
} | |
function load_after() { | |
if ( ! is_super_admin() ) | |
add_action( 'wp_head', array( &$this, 'google_analytics' ) ); | |
} | |
function register_fields() { | |
register_setting( 'general', 'ga_number', 'esc_attr' ); | |
add_settings_field('ga_number', '<label for="ga_number">'.__('Google Analytics Number' , 'ga_number' ).'</label>' , array(&$this, 'fields_html') , 'general' ); | |
} | |
function fields_html() { | |
$value = get_option( 'ga_number', '' ); | |
echo '<input type="text" id="ga_number" name="ga_number" value="' . $value . '" />'; | |
} | |
function google_analytics() { | |
$value = get_option( 'ga_number', false ); | |
if ( ! $value ) return; | |
?> | |
<script type="text/javascript"> | |
var _gaq = _gaq || []; | |
_gaq.push(['_setAccount', '<?php echo $value; ?>']); | |
_gaq.push(['_trackPageview']); | |
(function() { | |
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; | |
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; | |
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); | |
})(); | |
</script> | |
<?php | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment