settings menu */ add_action('admin_menu', array($this, 'add_to_settings_menu')); /* Show warning if debug mode is on */ add_action('admin_notices', array($this, 'show_admin_notice')); } /** * Function to instantiate our class and make it a singleton */ public static function get_instance() { if (is_null(self::$instance)) { self::$instance = new self(); } return self::$instance; } /* * Save default settings upon plugin activation */ function do_upon_plugin_activation() { //delete transient upon activation or update $this->delete_transient_js(); //if options already exists then return early if (get_option($this->option_name)!==false) return; update_option($this->option_name, $this->get_default_options()); } /*Register our settings, using WP settings API*/ function register_plugin_settings() { register_setting('asga_plugin_options', $this->option_name, array($this, 'ASGA_validate_options')); } /** * Adds a 'Settings' link for this plugin on plugin listing page * * @param $links * @param $file * @return array Links array */ function add_plugin_actions_links($links, $file) { if (current_user_can('manage_options')) { $build_url = add_query_arg('page', $this->plugin_slug, 'options-general.php'); array_unshift( $links, sprintf('%s', $build_url, __('Settings')) ); } return $links; } /* * Adds link to Plugin Option page + do related stuff */ function add_to_settings_menu() { $page_hook_suffix = add_submenu_page('options-general.php', 'Ank Simplified Google Analytics', 'Ank Simplified GA', 'manage_options', $this->plugin_slug, array($this, 'ASGA_options_page')); /*add help stuff via tab*/ add_action("load-$page_hook_suffix", array($this, 'add_help_menu_tab')); /*we can load additional css/js to our option page here */ } /** * Return all roles plus superAdmin if multi-site is enabled * @return array */ private function get_all_roles() { global $wp_roles; if (!isset($wp_roles)) { $wp_roles = new WP_Roles(); } $role_list = $wp_roles->get_names(); //append a custom role if multi-site is enabled if (is_multisite()) { $role_list['networkAdmin'] = 'Network Admin'; } return $role_list; } /** * Return default options for this plugin * @return array */ private function get_default_options() { $defaults = array( 'plugin_ver' => ASGA_PLUGIN_VER, 'ga_id' => '', 'js_location' => 1, 'js_load_later' => 0, 'js_priority' => 10, 'log_404' => 0, 'log_search' => 0, 'log_user_engagement' => 0, 'ua_enabled' => 1, 'displayfeatures' => 0, 'ga_ela' => 0, 'anonymise_ip' => 0, 'ga_domain' => '', 'debug_mode' => 0, 'force_ssl' => 1, ); //store roles as well foreach ($this->get_all_roles() as $role => $role_info) { $defaults = array_merge($defaults, array('ignore_role_' . $role => 0)); } return $defaults; } /** * Callback Function to handle and validate the form data * * @param array $in - POST array * @returns array - Validated array */ function ASGA_validate_options($in) { $out = array(); //always store plugin version to db $out['plugin_ver'] = ASGA_PLUGIN_VER; // Get the actual tracking ID if (!preg_match('|^UA-\d{4,}-\d+$|', (string)$in['ga_id'])) { $out['ga_id'] = ''; //warn user that the entered id is not valid add_settings_error($this->option_name, 'ga_id', 'Your GA tracking ID seems invalid. Please check.'); } else { $out['ga_id'] = esc_html($in['ga_id']); } $radio_items = array('js_location','js_load_later'); foreach($radio_items as $item){ $out[$item] = absint($in[$item]); } $out['js_priority'] = ($in['js_priority'] == '') ? 10 : absint($in['js_priority']); $out['ga_domain'] = esc_html($in['ga_domain']); $checkbox_items = array('ua_enabled', 'anonymise_ip', 'displayfeatures', 'ga_ela', 'log_404', 'log_search','log_user_engagement','debug_mode','force_ssl'); //add rolls to checkbox_items array foreach ($this->get_all_roles() as $role => $role_info) { $checkbox_items[] = 'ignore_role_' . $role; } foreach ($checkbox_items as $item) { if (isset($in[$item]) && '1' == $in[$item]) $out[$item] = 1; else $out[$item] = 0; } //delete transient upon change in settings $this->delete_transient_js(); return $out; } /** * Function will print our option page form */ function ASGA_options_page() { if (!current_user_can('manage_options')) { wp_die(__('You do not have sufficient permissions to access this page.')); } ?>

Ank Simplified Google Analytics (v)

get_safe_options(); //wp inbuilt nonce field , etc settings_fields('asga_plugin_options'); ?>
Google Analytics tracking ID :

Paste your Google Analytics tracking ID (e.g. "UA-XXXXXXXX-X")

Enable Universal GA :

Un-check this, If you are using Classic GA . OR upgrade.

Set domain : %s

',get_blogaddress_by_id(get_current_blog_id())) ; } ?>
Enable Display Features :

Remarketing | Demographics and Interest Reporting

Enhanced Link Attribution :
Anonymize IP's :
Force SSL :
Code Location :

Code Execution :

Action Priority :

0 means highest priority, default is 10

Disable Tracking when : get_all_roles() as $id => $label) { echo '
'; } ?>
Event Tracking :
'Log 404 errors as events', 'log_search' => 'Log searched items as page views', 'log_user_engagement' => 'Log user engagement as events' ); //loop through each event item foreach ($events as $event => $label) { echo '
'; } ?>
Troubleshoot :

This should only be used temporarily or during development, don't forget to disable it in production.

Debug database options :

Developed by- Ankur Kumar | Fork on GitHub | Rate this on WordPress

add_help_tab( array( 'id' => 'asga-overview', 'title' => 'Basic', 'content' => '

Do you have a Google Analytics Account ?
' . 'In order to use this plugin you need to have a Google Analytics Account. Create an account here. It is FREE.
' . 'How do i find my Google Analytics ID ?
' . 'Please check out this link
' . 'How do i view my stats ?
' . 'Login to Google Analytics Account with your G-Mail ID to view stats.' . '

' ) ); $curr_screen->add_help_tab( array( 'id' => 'asga-troubleshoot', 'title' => 'Troubleshoot', 'content' => '

Things to remember
' . '

'. '

' ) ); $curr_screen->add_help_tab( array( 'id' => 'asga-more-info', 'title' => 'More', 'content' => '

Need more information ?
' . 'A brief FAQ is available to solve your common issues, ' . 'click here for more.
' . 'Support is only available on WordPress Forums, click here to ask anything about this plugin.
' . 'You can also browse the source code of this plugin on GitHub. ' . '

' ) ); /*add a help sidebar with links */ $curr_screen->set_help_sidebar( '

Quick Links

' . '

Plugin FAQ

' . '

Plugin Home

' ); } /** * Show a warning notice on all screens if debug mode is on */ function show_admin_notice() { //notice will be shown to admin user only if ($this->check_admin_notice()) { ?>

Google Analytics debug mode is set to on. Don't leave this option enabled in production. id, $this->plugin_slug) === false) { ?> Settings

get_safe_options(); //id ga id is not set return early if (empty($options['ga_id'])) return false; //if current user is not admin then return early if (!current_user_can('manage_options')) return false; //if debug mode is off return early if ($options['debug_mode'] == 0) return false; //else return true return true; } /** * Get fail safe options * @return array */ private function get_safe_options() { //get fresh options from db $options = get_option($this->option_name); //if options not exists in db then init with defaults , also always append default options to existing options $options = empty($options) ? $this->get_default_options() : $options + $this->get_default_options(); return $options; } /** * Delete cache version of tracking code */ private function delete_transient_js(){ delete_transient($this->transient_name); } } //end class