Skip to content

Instantly share code, notes, and snippets.

@trepmal
Created June 27, 2012 05:52
Show Gist options
  • Save trepmal/3001766 to your computer and use it in GitHub Desktop.
Save trepmal/3001766 to your computer and use it in GitHub Desktop.
Basic Google Analytics - updated here: https://gist.github.com/trepmal/3389856
<?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