Skip to content

Instantly share code, notes, and snippets.

@trepmal
Last active October 8, 2015 21:08
Show Gist options
  • Save trepmal/3389856 to your computer and use it in GitHub Desktop.
Save trepmal/3389856 to your computer and use it in GitHub Desktop.
Basic Google Analytics
<?php
/*
Plugin Name: Basic Google Analytics
Plugin URI: trepmal.com
Description: Adds very basic Google Analytics code. Need complex tracking? This isn't for you.
Version: 1
Author: Kailey Lampert
Author URI: kaileylampert.com
*/
$basic_google_analytics = new Basic_Google_Analytics();
class Basic_Google_Analytics {
function __construct( ) {
add_filter( 'plugin_action_links_'. plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' ), 10, 4 );
add_action( 'admin_init' , array( $this, 'admin_init' ) );
add_action( 'wp_head', array( $this, 'wp_head' ) );
}
function plugin_action_links( $actions, $plugin_file, $plugin_data, $context ) {
if ( is_plugin_active( $plugin_file ) ) {
$actions[] = '<a href="' . admin_url('options-general.php#bga-google_analytics_id') . '">' . __( 'Setup', 'basic-google-analytics' ) . '</a>';
}
return $actions;
}
function admin_init() {
register_setting( 'general', 'bga-google_analytics_id', array( $this, 'sanitize_validate' ) );
add_settings_field( 'bga-google_analytics_id', '<label for="bga-google_analytics_id">' . __( 'Google Analytics ID', 'basic-google-analytics' ) . '</label>' , array( $this, 'fields_html') , 'general' );
}
function fields_html() {
$ga_id = esc_attr( get_option( 'bga-google_analytics_id', '' ) );
echo "<input type='text' id='bga-google_analytics_id' name='bga-google_analytics_id' value='$ga_id' /><p class='description'>UA-XXXXXXX-YY</p>";
}
function sanitize_validate( $given_id ) {
$saved_id = get_option('bga-google_analytics_id', '');
$clean_id = strtoupper( strip_tags( trim( $given_id ) ) ); //original, cleaned input
// if no change, carry on
if ( $saved_id == $clean_id ) {
return $saved_id;
}
// has the id been removed entirely?
if ( empty( $clean_id ) ) {
add_settings_error('', '', __( 'Your Google Analytics ID number has been removed.', 'basic-google-analytics' ), 'updated' );
return '';
}
$almost_ready_id = 'UA-'. trim( $clean_id, 'UA-' ); // account for variance, make sure number is prefixed with "UA-"
preg_match( '/(UA-\d+-\d+)$/', $almost_ready_id, $match ); // expects: UA-XXXXXXX-YY
// if empty, then the regex failed to find the correct pattern
if ( empty( $match ) ) {
add_settings_error('', '', sprintf( __( '%s is not a valid ID number', 'basic-google-analytics' ), $almost_ready_id ), 'error' );
return '';
} else {
$ready_id = $almost_ready_id;
}
// if we've made it this far, we have a good id
add_settings_error('', '', __( 'Your Google Analytics ID number has been saved.', 'basic-google-analytics' ), 'updated' );
return $ready_id;
}
function wp_head() {
if ( is_super_admin() ) return; //if not MS, will return true if user can delete other users
// if ( is_user_logged_in() ) return; //or ignore any logged in user
$ga_id = get_option( 'bga-google_analytics_id', '' );
if ( empty( $ga_id ) ) return;
?>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '<?php echo esc_js( $ga_id ); ?>']);
_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