From 4b59284f9b92c4ee259f87f461a849bc4f8086b4 Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Mon, 7 Nov 2016 15:00:00 -0300 Subject: [PATCH 01/43] Adds Google Analytics support using the wp-google-analytics plugin --- .../components/module-settings/index.jsx | 40 ++ .../module-settings/modules-per-tab-page.jsx | 5 +- _inc/client/engagement/index.jsx | 1 + _inc/lib/class.core-rest-api-endpoints.php | 17 + ...lass.jetpack-core-api-module-endpoints.php | 8 + modules/google-analytics.php | 23 + .../wp-google-analytics/README.md | 113 ++++ .../languages/wp-google-analytics.pot | 185 ++++++ .../wp-google-analytics/readme.txt | 133 ++++ .../wp-google-analytics.js | 29 + .../wp-google-analytics.php | 615 ++++++++++++++++++ .../wp-google-analytics/wpcom-helper.php | 49 ++ modules/module-headings.php | 5 + modules/module-info.php | 15 + 14 files changed, 1237 insertions(+), 1 deletion(-) create mode 100644 modules/google-analytics.php create mode 100644 modules/google-analytics/wp-google-analytics/README.md create mode 100644 modules/google-analytics/wp-google-analytics/languages/wp-google-analytics.pot create mode 100644 modules/google-analytics/wp-google-analytics/readme.txt create mode 100644 modules/google-analytics/wp-google-analytics/wp-google-analytics.js create mode 100644 modules/google-analytics/wp-google-analytics/wp-google-analytics.php create mode 100644 modules/google-analytics/wp-google-analytics/wpcom-helper.php diff --git a/_inc/client/components/module-settings/index.jsx b/_inc/client/components/module-settings/index.jsx index df0a74da8de62..afc08d58e57bc 100644 --- a/_inc/client/components/module-settings/index.jsx +++ b/_inc/client/components/module-settings/index.jsx @@ -855,3 +855,43 @@ export let WordAdsSettings = React.createClass( { } ); WordAdsSettings = moduleSettingsForm( WordAdsSettings ); + +export let GoogleAnalyticsSettings = React.createClass( { + render() { + return ( +
+ +

+ { + __( 'Enter your tracking Id value to track your blog with {{a}}Google Analytics{{/a}}.', { + components: { + a: + } + } ) + } +

+ +
+ + { __( 'Google Analytics Tracking Id' ) } + + +
+ + +
+ + ); + } +} ); + +GoogleAnalyticsSettings = moduleSettingsForm( GoogleAnalyticsSettings ); diff --git a/_inc/client/components/module-settings/modules-per-tab-page.jsx b/_inc/client/components/module-settings/modules-per-tab-page.jsx index 2da34a10d2c56..93334a73fb673 100644 --- a/_inc/client/components/module-settings/modules-per-tab-page.jsx +++ b/_inc/client/components/module-settings/modules-per-tab-page.jsx @@ -28,7 +28,8 @@ import { VerificationToolsSettings, SitemapsSettings, VideoPressSettings, - WordAdsSettings + WordAdsSettings, + GoogleAnalyticsSettings } from 'components/module-settings/'; import ExternalLink from 'components/external-link'; @@ -128,6 +129,8 @@ const AllModuleSettingsComponent = React.createClass( { return ( ); case 'wordads': return ( ); + case 'google-analytics': + return ( ); case 'gravatar-hovercards': case 'contact-form': case 'latex': diff --git a/_inc/client/engagement/index.jsx b/_inc/client/engagement/index.jsx index cb173bfe1f054..6bebc4c737017 100644 --- a/_inc/client/engagement/index.jsx +++ b/_inc/client/engagement/index.jsx @@ -79,6 +79,7 @@ export const Engagement = ( props ) => { [ 'sitemaps', getModule( 'sitemaps' ).name, sitemapsDesc, getModule( 'sitemaps' ).learn_more_button ], [ 'enhanced-distribution', getModule( 'enhanced-distribution' ).name, getModule( 'enhanced-distribution' ).description, getModule( 'enhanced-distribution' ).learn_more_button ], [ 'verification-tools', getModule( 'verification-tools' ).name, getModule( 'verification-tools' ).description, getModule( 'verification-tools' ).learn_more_button ], + [ 'google-analytics', getModule( 'google-analytics' ).name, getModule( 'google-analytics' ).description, getModule( 'google-analytics' ).learn_more_button ], ], nonAdminAvailable = [ 'publicize' ]; // Put modules available to non-admin user at the top of the list. diff --git a/_inc/lib/class.core-rest-api-endpoints.php b/_inc/lib/class.core-rest-api-endpoints.php index 29e2496fd97f9..8a80f771f4901 100644 --- a/_inc/lib/class.core-rest-api-endpoints.php +++ b/_inc/lib/class.core-rest-api-endpoints.php @@ -1546,6 +1546,16 @@ public static function get_updateable_data_list( $selector = '' ) { 'validate_callback' => __CLASS__ . '::validate_boolean', 'jp_group' => 'wordads', ), + + // Google Analytics + 'google_analytics_tracking_id' => array( + 'description' => esc_html__( 'Google Analytics', 'jetpack' ), + 'type' => 'string', + 'default' => '', + 'validate_callback' => __CLASS__ . '::validate_alphanum', + 'jp_group' => 'google-analytics', + ), + // Stats 'admin_bar' => array( 'description' => esc_html__( 'Put a chart showing 48 hours of views in the admin bar.', 'jetpack' ), @@ -2094,6 +2104,13 @@ public static function prepare_options_for_response( $module = '' ) { $options = self::split_options( $options, get_option( 'verification_services_codes' ) ); break; + case 'google-analytics': + $wga = get_option( 'wga' ); + if ( is_array( $wga ) && array_key_exists( 'code', $wga ) ) { + $options[ 'google_analytics_tracking_id' ][ 'current_value' ] = $wga[ 'code' ]; + } + break; + case 'sharedaddy': // It's local, but it must be broken apart since it's saved as an array. if ( ! class_exists( 'Sharing_Service' ) && ! @include( JETPACK__PLUGIN_DIR . 'modules/sharedaddy/sharing-service.php' ) ) { diff --git a/_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php b/_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php index a463bcde9e7b3..64b92ce8edf09 100644 --- a/_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php +++ b/_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php @@ -718,6 +718,14 @@ public function update_data( $data ) { $updated = get_option( $option ) != $value ? update_option( $option, (bool) $value ? 'letitsnow' : '' ) : true; break; + case 'google_analytics_tracking_id': + $grouped_options = $grouped_options_current = (array) get_option( 'wga' ); + $grouped_options[ 'code' ] = $value; + + // If option value was the same, consider it done. + $updated = $grouped_options_current != $grouped_options ? update_option( 'wga', $grouped_options ) : true; + break; + case 'wp_mobile_featured_images': case 'wp_mobile_excerpt': $value = ( 'enabled' === $value ) ? '1' : '0'; diff --git a/modules/google-analytics.php b/modules/google-analytics.php new file mode 100644 index 0000000000000..1fe924309ce2f --- /dev/null +++ b/modules/google-analytics.php @@ -0,0 +1,23 @@ +Google Analytics
to track your WordPress site statistics. Brought to you by Range + +## Description + +WP Google Analytics makes it easy to track your site's usage, with lots of +helpful additional data. + +Features: + +* Uses Google's asynchronous tracking method which is faster and more reliable. +* Automatically tracks site speed +* Option to log outgoing links as events +* Option to log 404 errors as events +* Use custom variables in Google Analytics to track additional data on pageviews including: + * Author + * Categories + * Tags + * Context (such as home, category, post, author, etc) + * Date + * Logged in + * Anything - Use the built-in filter to add your own! +* Allows you to ignore any user roles (administrators, editors, authors, etc) + +## Installation + +Use automatic installer. + +## Frequently Asked Questions + +**Where do I put my Google Analytics Code?** + +WP Google Analytics has a config page under the settings tab in the admin area +of your site. You can paste your tracking code from Google into the textarea on +this page. + +**How do I track searches?** + +WP Google Analytics used to help you track site searches before Google Analytics +started doing this natively. While we still support tracking searches for +backwards compatibility, this feature has been deprecated and will eventually be +removed. To track searches in Google Analytics follow this +Google support article. +WordPress uses 's' as the query parameter. + +**Can't I just paste the Google Analytics code into my template file?** + +Absolutely, however in order to get a better idea of what is going on with your +site, it is often nice to have your own activities ignored, track 404s, searches +and even where users go when they leave your site. WP Google Analytics lets you +easily do all these things. + +**What tokens are support for custom variables?** + +All the built-in tokens are described on the settings page. You can also add +your own using the 'wga_tokens' filter. + +## Changelog + +### 1.4.1 +* Fix undefined index notice for users upgrading from pre-1.3.0 + +### 1.4.0 +* Support for tokens in custom variables + +### 1.3.1 +* Fixed custom variables not being tracked + +### 1.3.0 +* Refactored to use settings API - Props danielbachhuber +* Convert to singleton and instatiate class +* Convert tracking code field to ID, keeping backwards compat - Props danielbachhuber +* Custom variable support - Props danielbachhuber +* Track outgoing links and 404s as events +* Made the whole plugin translatable +* Deprecated tracking searches + +### 1.2.5 +* Fixed some notices. Props westi +* Update all links + +### 1.2.4 +* Removed the optional anonymous statistics collection. Nothing is ever collected anymore. +* Changed & to & in some more places to fix validation problems. + +### 1.2.3 +* Changed & to & to fix validation problems. + +### 1.2.2 +* Fixed problem with code affecting Admin Javascript such as the TinyMCE editor + +### 1.2.1 +* Bug fix for the stats gathering + +### 1.2.0 +* No longer parses outgoing links in the admin section. +* Uses get_footer instead of wp_footer. Too many themes aren't adding the wp_footer call. +* Options page updated +* Added optional anonymous statistics collection + +### 1.1.0 +* Major revamp to work better with the new Google Tracking Code. It seems that outgoing links weren't being tracked properly. + +### 1.0.0 +* Added to wordpress.org repository + +### 0.2 +* Fixed problem with themes that do not call wp_footer(). If you are reading this and you are a theme developer, USE THE HOOKS! That's what they're there for! +* Updated how the admin section is handled + +### 0.1 +* Original Version diff --git a/modules/google-analytics/wp-google-analytics/languages/wp-google-analytics.pot b/modules/google-analytics/wp-google-analytics/languages/wp-google-analytics.pot new file mode 100644 index 0000000000000..3c045ab8e6c71 --- /dev/null +++ b/modules/google-analytics/wp-google-analytics/languages/wp-google-analytics.pot @@ -0,0 +1,185 @@ +# Copyright (C) 2012 WP Google Analytics +# This file is distributed under the same license as the WP Google Analytics package. +msgid "" +msgstr "" +"Project-Id-Version: WP Google Analytics 1.4.0\n" +"Report-Msgid-Bugs-To: http://wordpress.org/tag/wp-google-analytics\n" +"POT-Creation-Date: 2012-10-05 21:16:58+00:00\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"PO-Revision-Date: 2012-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" + +#: wp-google-analytics.php:78 +msgid "Post author for current view" +msgstr "" + +#: wp-google-analytics.php:79 +msgid "Post author's display name" +msgstr "" + +#: wp-google-analytics.php:94 +msgid "Categories assigned to a post" +msgstr "" + +#: wp-google-analytics.php:95 +msgid "Category names in a commma-separated list" +msgstr "" + +#: wp-google-analytics.php:109 +msgid "Which view the visitor is on" +msgstr "" + +#: wp-google-analytics.php:110 +msgid "Samples: 'home', 'category', 'post', 'author'" +msgstr "" + +#: wp-google-analytics.php:116 +msgid "Publication date for the current view" +msgstr "" + +#: wp-google-analytics.php:117 +msgid "Format specified by 'Date Format' in Settings -> General" +msgstr "" + +#: wp-google-analytics.php:132 +msgid "Tags assigned to a post" +msgstr "" + +#: wp-google-analytics.php:133 +msgid "Tag names in a commma-separated list" +msgstr "" + +#: wp-google-analytics.php:148 +msgid "Whether or not the viewer is logged in" +msgstr "" + +#: wp-google-analytics.php:149 +msgid "'true' or 'false'" +msgstr "" + +#: wp-google-analytics.php:160 +msgid "Google Analytics" +msgstr "" + +#: wp-google-analytics.php:171 +msgid "Google Analytics tracking ID:" +msgstr "" + +#: wp-google-analytics.php:172 +msgid "Additional items to log:" +msgstr "" + +#: wp-google-analytics.php:173 +msgid "Visits to ignore:" +msgstr "" + +#: wp-google-analytics.php:174 +msgid "Custom variables:" +msgstr "" + +#: wp-google-analytics.php:183 +msgid "" +"Use tokens in your custom variables to make your fields dynamic based on " +"context. Here are some of the tokens you can use:" +msgstr "" + +#: wp-google-analytics.php:185 +msgid "Token" +msgstr "" + +#: wp-google-analytics.php:185 +msgid "Description" +msgstr "" + +#: wp-google-analytics.php:185 +msgid "Return value" +msgstr "" + +#: wp-google-analytics.php:199 +msgid "" +"Paste your Google Analytics tracking ID (e.g. \"UA-XXXXXX-X\") into the " +"field." +msgstr "" + +#: wp-google-analytics.php:207 +msgid "Log 404 errors as events" +msgstr "" + +#: wp-google-analytics.php:208 +msgid "" +"Log searches as /search/{search}?referrer={referrer} (deprecated)" +msgstr "" + +#: wp-google-analytics.php:209 +msgid "Log outgoing links as events" +msgstr "" + +#: wp-google-analytics.php:227 +msgid "Default" +msgstr "" + +#: wp-google-analytics.php:228 +msgid "Visitor" +msgstr "" + +#: wp-google-analytics.php:229 +msgid "Session" +msgstr "" + +#: wp-google-analytics.php:230 +msgid "Page" +msgstr "" + +#: wp-google-analytics.php:236 +msgid "Name" +msgstr "" + +#: wp-google-analytics.php:239 +msgid "Value" +msgstr "" + +#: wp-google-analytics.php:242 +msgid "Scope" +msgstr "" + +#: wp-google-analytics.php:256 +msgid "Do not log anything in the admin area" +msgstr "" + +#: wp-google-analytics.php:260 +msgid "Do not log %s when logged in" +msgstr "" + +#: wp-google-analytics.php:321 +msgid "Google Analytics Options" +msgstr "" + +#: wp-google-analytics.php:326 +msgid "Update Options" +msgstr "" + +#. Plugin Name of the plugin/theme +msgid "WP Google Analytics" +msgstr "" + +#. Plugin URI of the plugin/theme +msgid "http://bluedogwebservices.com/wordpress-plugin/wp-google-analytics/" +msgstr "" + +#. Description of the plugin/theme +msgid "" +"Lets you use Google Analytics to " +"track your WordPress site statistics" +msgstr "" + +#. Author of the plugin/theme +msgid "Aaron D. Campbell" +msgstr "" + +#. Author URI of the plugin/theme +msgid "http://ran.ge/" +msgstr "" diff --git a/modules/google-analytics/wp-google-analytics/readme.txt b/modules/google-analytics/wp-google-analytics/readme.txt new file mode 100644 index 0000000000000..5c662820e4660 --- /dev/null +++ b/modules/google-analytics/wp-google-analytics/readme.txt @@ -0,0 +1,133 @@ +=== WP Google Analytics === +Contributors: aaroncampbell, PeteMall, range, saracannon +Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=CLXNQ3DVK6M48 +Tags: analytics, google, google analytics +Requires at least: 3.1 +Tested up to: 3.5 +Stable tag: 1.4.0 + +Lets you use Google Analytics to track your WordPress site statistics + +== Description == + +WP Google Analytics makes it easy to track your site's usage, with lots of +helpful additional data. + +Features: + +* Uses Google's asynchronous tracking method which is faster and more reliable. +* Automatically tracks site speed +* Option to log outgoing links as events +* Option to log 404 errors as events +* Use custom variables in Google Analytics to track additional data on pageviews including: + * Author + * Categories + * Tags + * Context (such as home, category, post, author, etc) + * Date + * Logged in + * Anything - Use the built-in filter to add your own! +* Allows you to ignore any user roles (administrators, editors, authors, etc) + +WP Google Analytics +Brought to you by Ran.ge + +== Installation == + +Use automatic installer. + +== Frequently Asked Questions == + += Where do I put my Google Analytics Code? = + +WP Google Analytics has a config page under the settings tab in the admin area +of your site. You can paste your tracking code from Google into the textarea on +this page. + += How do I track searches? = + +WP Google Analytics used to help you track site searches before Google Analytics +started doing this natively. While we still support tracking searches for +backwards compatibility, this feature has been deprecated and will eventually be +removed. To track searches in Google Analytics follow this +Google support article. +WordPress uses 's' as the query parameter. + += What tokens are support for custom variables? = + +All the built-in tokens are described on the settings page. You can also add +your own using the 'wga_tokens' filter. + += Can't I just paste the Google Analytics code into my template file? = + +Absolutely, however in order to get a better idea of what is going on with your +site, it is often nice to have your own activities ignored, track 404s, searches +and even where users go when they leave your site. WP Google Analytics lets you +easily do all these things. + +== Upgrade Notice == + += 1.4.0 = +Allow tokens in the custom variables + += 1.3.1 = +Fix for custom variables not being tracked + += 1.3.0 = +Move to using events for tracking 404s and outgoing links and add support for custom variables + +== Changelog == + += 1.4.1 = +* Fix undefined index notice for users upgrading from pre-1.3.0 + += 1.4.0 = +* Support for tokens in custom variables + += 1.3.1 = +* Fixed custom variables not being tracked + += 1.3.0 = +* Refactored to use settings API - Props danielbachhuber +* Convert to singleton and instatiate class +* Convert tracking code field to ID, keeping backwards compat - Props danielbachhuber +* Custom variable support - Props danielbachhuber +* Track outgoing links and 404s as events +* Made the whole plugin translatable +* Deprecated tracking searches + += 1.2.5 = +* Fixed some notices. Props westi +* Update all links + += 1.2.4 = +* Removed the optional anonymous statistics collection. Nothing is ever collected anymore. +* Changed & to & in some more places to fix validation problems. + += 1.2.3 = +* Changed & to & to fix validation problems. + += 1.2.2 = +* Fixed problem with code affecting Admin Javascript such as the TinyMCE editor + += 1.2.1 = +* Bug fix for the stats gathering + += 1.2.0 = +* No longer parses outgoing links in the admin section. +* Uses get_footer instead of wp_footer. Too many themes aren't adding the wp_footer call. +* Options page updated +* Added optional anonymous statistics collection + += 1.1.0 = +* Major revamp to work better with the new Google Tracking Code. It seems that outgoing links weren't being tracked properly. + += 1.0.0 = +* Added to wordpress.org repository + += 0.2 = +* Fixed problem with themes that do not call wp_footer(). If you are reading this and you are a theme developer, USE THE HOOKS! That's what they're there for! +* Updated how the admin section is handled + += 0.1 = +* Original Version diff --git a/modules/google-analytics/wp-google-analytics/wp-google-analytics.js b/modules/google-analytics/wp-google-analytics/wp-google-analytics.js new file mode 100644 index 0000000000000..6e1d5917e92ae --- /dev/null +++ b/modules/google-analytics/wp-google-analytics/wp-google-analytics.js @@ -0,0 +1,29 @@ +(function($){ // Open closure and map jQuery to $. + + // Adds :external for grabbing external links + $.expr[':'].external = function(obj) { + return obj.href && !obj.href.match(/^mailto\:/) && !obj.href.match(/^javascript\:/) && (obj.hostname != location.hostname); + }; + + // Document ready. + $( function() { + // Add 'external' class and _blank target to all external links + $('a:external').on( 'click.wp-google-analytics', function(e){ + try { + _gaq.push( [ '_trackEvent', 'Outbound Links', e.currentTarget.host, $(this).attr('href') ] ); + /** + * If this link is not opened in a new tab or window, we need to add + * a small delay so the event can fully fire. See: + * http://support.google.com/analytics/bin/answer.py?hl=en&answer=1136920 + * + * We're actually checking for modifier keys, middle-click, or pre-existing target=_blank attribute + */ + if ( ! ( e.metaKey || e.ctrlKey || 1 == e.button || '_blank' == $(this).attr('target') ) ) { + e.preventDefault(); + setTimeout('document.location = "' + $(this).attr('href') + '"', 100) + } + } catch(err) {} + }); + }); + +})( jQuery ); // Close closure. diff --git a/modules/google-analytics/wp-google-analytics/wp-google-analytics.php b/modules/google-analytics/wp-google-analytics/wp-google-analytics.php new file mode 100644 index 0000000000000..f2a9894f7fec8 --- /dev/null +++ b/modules/google-analytics/wp-google-analytics/wp-google-analytics.php @@ -0,0 +1,615 @@ +Google Analytics to track your WordPress site statistics + * Version: 1.4.0 + * Author: Aaron D. Campbell + * Author URI: http://ran.ge/ + * License: GPLv2 or later + * Text Domain: wp-google-analytics + */ + +define('WGA_VERSION', '1.4.0'); + +/* Copyright 2006 Aaron D. Campbell (email : wp_plugins@xavisys.com) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +/** + * wpGoogleAnalytics is the class that handles ALL of the plugin functionality. + * It helps us avoid name collisions + * http://codex.wordpress.org/Writing_a_Plugin#Avoiding_Function_Name_Collisions + */ +class wpGoogleAnalytics { + + /** + * @var wpGoogleAnalytics - Static property to hold our singleton instance + */ + static $instance = false; + + static $page_slug = 'wp-google-analytics'; + + var $tokens = array(); + + /** + * This is our constructor, which is private to force the use of get_instance() + * @return void + */ + private function __construct() { + add_filter( 'init', array( $this, 'init' ) ); + add_action( 'admin_init', array( $this, 'admin_init' ) ); + add_action( 'admin_menu', array( $this, 'admin_menu' ) ); + add_action( 'get_footer', array( $this, 'insert_code' ) ); + add_action( 'wp_enqueue_scripts', array( $this, 'track_outgoing' ) ); + add_filter( 'plugin_action_links', array( $this, 'add_plugin_page_links' ), 10, 2 ); + } + + /** + * Function to instantiate our class and make it a singleton + */ + public static function get_instance() { + if ( !self::$instance ) + self::$instance = new self; + + return self::$instance; + } + + public function init() { + load_plugin_textdomain( 'wp-google-analytics', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); + + $this->tokens = array( + array( + 'token' => '%the_author%', + 'callback' => 'get_the_author', + 'callback_returns' => 'string', + 'description' => __( 'Post author for current view', 'wp-google-analytics' ), + 'retval' => __( "Post author's display name", 'wp-google-analytics' ), + 'ignore_when' => array( + 'is_home', + 'is_front_page', + 'is_post_type_archive', + 'is_page', + 'is_date', + 'is_category', + 'is_tag', + ), + ), + array( + 'token' => '%the_category%', + 'callback' => array( $this, 'token_the_category' ), + 'callback_returns' => 'string', + 'description' => __( 'Categories assigned to a post', 'wp-google-analytics' ), + 'retval' => __( "Category names in a commma-separated list", 'wp-google-analytics' ), + 'ignore_when' => array( + 'is_home', + 'is_front_page', + 'is_page', + 'is_post_type_archive', + 'is_author', + 'is_tag', + ), + ), + array( + 'token' => '%context%', + 'callback' => array( $this, 'token_context' ), + 'callback_returns' => 'string', + 'description' => __( 'Which view the visitor is on', 'wp-google-analytics' ), + 'retval' => __( "Samples: 'home', 'category', 'post', 'author'" ), + ), + array( + 'token' => '%the_date%', + 'callback' => 'get_the_date', + 'callback_returns' => 'string', + 'description' => __( 'Publication date for the current view', 'wp-google-analytics' ), + 'retval' => __( "Format specified by 'Date Format' in Settings -> General", 'wp-google-analytics' ), + 'ignore_when' => array( + 'is_home', + 'is_front_page', + 'is_post_type_archive', + 'is_page', + 'is_author', + 'is_category', + 'is_tag', + ), + ), + array( + 'token' => '%the_tags%', + 'callback' => array( $this, 'token_the_tags' ), + 'callback_returns' => 'string', + 'description' => __( 'Tags assigned to a post', 'wp-google-analytics' ), + 'retval' => __( "Tag names in a commma-separated list", 'wp-google-analytics' ), + 'ignore_when' => array( + 'is_home', + 'is_front_page', + 'is_page', + 'is_post_type_archive', + 'is_date', + 'is_category', + 'is_author', + ), + ), + array( + 'token' => '%is_user_logged_in%', + 'callback' => 'is_user_logged_in', + 'callback_returns' => 'bool', + 'description' => __( 'Whether or not the viewer is logged in', 'wp-google-analytics' ), + 'retval' => __( "'true' or 'false'", 'wp-google-analytics' ), + ), + ); + + $this->tokens = apply_filters( 'wga_tokens', $this->tokens ); + } + + /** + * This adds the options page for this plugin to the Options page + */ + public function admin_menu() { + add_options_page(__('Google Analytics', 'wp-google-analytics'), __('Google Analytics', 'wp-google-analytics'), 'manage_options', self::$page_slug, array( $this, 'settings_view' ) ); + } + + /** + * Register our settings + */ + public function admin_init() { + + register_setting( 'wga', 'wga', array( $this, 'sanitize_general_options' ) ); + + add_settings_section( 'wga_general', false, '__return_false', 'wga' ); + add_settings_field( 'code', __( 'Google Analytics tracking ID:', 'wp-google-analytics' ), array( $this, 'field_code' ), 'wga', 'wga_general' ); + add_settings_field( 'additional_items', __( 'Additional items to log:', 'wp-google-analytics' ), array( $this, 'field_additional_items' ), 'wga', 'wga_general' ); + add_settings_field( 'do_not_track', __( 'Visits to ignore:', 'wp-google-analytics' ), array( $this, 'field_do_not_track' ), 'wga', 'wga_general' ); + add_settings_field( 'other_options', __( 'Other options:', 'wp-google-analytics' ), array( $this, 'field_other_options' ), 'wga', 'wga_general' ); + add_settings_field( 'custom_vars', __( 'Custom variables:', 'wp-google-analytics' ), array( $this, 'field_custom_variables' ), 'wga', 'wga_general' ); + } + + /** + * Where the user adds their Google Analytics code + */ + public function field_code() { + // Display the tokens in the right column of the page + echo '
'; + echo '' . __( 'Use tokens in your custom variables to make your fields dynamic based on context. Here are some of the tokens you can use:' ) . ''; + echo ''; + echo ''; + echo ''; + foreach( $this->tokens as $token ) { + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + } + echo ''; + echo '
' . __( 'Token', 'wp-google-analytics' ) . '' . __( 'Description', 'wp-google-analytics' ) . '' . __( 'Return value', 'wp-google-analytics' ) . '
' . esc_html( $token['token'] ) . '' . esc_html( $token['description'] ) . '' . esc_html( $token['retval'] ) . '
'; + echo '
'; + + echo ''; + echo '

' . __( 'Paste your Google Analytics tracking ID (e.g. "UA-XXXXXX-X") into the field.', 'wp-google-analytics' ) . '

'; + } + + /** + * Option to log additional items + */ + public function field_additional_items() { + $addtl_items = array( + 'log_404s' => __( 'Log 404 errors as events', 'wp-google-analytics' ), + 'log_searches' => sprintf( __( 'Log searches as /search/{search}?referrer={referrer} (deprecated)', 'wp-google-analytics' ), 'http://wordpress.org/extend/plugins/wp-google-analytics/faq/' ), + 'log_outgoing' => __( 'Log outgoing links as events', 'wp-google-analytics' ), + ); + foreach( $addtl_items as $id => $label ) { + echo '
'; + } + } + + /** + * Define custom variables to be included in your tracking code + */ + public function field_custom_variables() { + + $custom_vars = $this->_get_options( 'custom_vars' ); + + $scope_options = array( + 0 => __( 'Default', 'wp-google-analytics' ), + 1 => __( 'Visitor', 'wp-google-analytics' ), + 2 => __( 'Session', 'wp-google-analytics' ), + 3 => __( 'Page', 'wp-google-analytics' ), + ); + for ( $i = 1; $i <= 5; $i++ ) { + $name = ( isset( $custom_vars[$i]['name'] ) ) ? $custom_vars[$i]['name'] : ''; + $value = ( isset( $custom_vars[$i]['value'] ) ) ? $custom_vars[$i]['value'] : ''; + $scope = ( isset( $custom_vars[$i]['scope'] ) ) ? $custom_vars[$i]['scope'] : 0; + echo '  '; + echo '  '; + echo '
'; + } + + } + + public function field_do_not_track() { + $do_not_track = array( + 'ignore_admin_area' => __( 'Do not log anything in the admin area', 'wp-google-analytics' ), + ); + global $wp_roles; + foreach( $wp_roles->roles as $role => $role_info ) { + $do_not_track['ignore_role_' . $role] = sprintf( __( 'Do not log %s when logged in', 'wp-google-analytics' ), rtrim( $role_info['name'], 's' ) ); + } + foreach( $do_not_track as $id => $label ) { + echo '
'; + } + } + + /** + * Options that don't belong anywhere else. + */ + public function field_other_options() { + $other_options = array( + 'enable_display_advertising' => sprintf( __( 'Enable Display Advertising', 'wp-google-analytics' ), 'https://support.google.com/analytics/answer/2444872?hl=en&utm_id=ad' ), + ); + + foreach( $other_options as $id => $label ) { + echo '
'; + } + } + + /** + * Sanitize all of the options associated with the plugin + */ + public function sanitize_general_options( $in ) { + + $out = array(); + + // The actual tracking ID + if ( preg_match( '#UA-[\d-]+#', $in['code'], $matches ) ) + $out['code'] = $matches[0]; + else + $out['code'] = ''; + + $checkbox_items = array( + // Additional items you can track + 'log_404s', + 'log_searches', + 'log_outgoing', + 'enable_display_advertising', + // Things to ignore + 'ignore_admin_area', + ); + global $wp_roles; + foreach( $wp_roles->roles as $role => $role_info ) { + $checkbox_items[] = 'ignore_role_' . $role; + } + foreach( $checkbox_items as $checkbox_item ) { + if ( isset( $in[$checkbox_item] ) && 'true' == $in[$checkbox_item] ) + $out[$checkbox_item] = 'true'; + else + $out[$checkbox_item] = 'false'; + } + + // Custom variables + for( $i = 1; $i <= 5; $i++ ) { + foreach( array( 'name', 'value', 'scope' ) as $key ) { + if ( isset( $in['custom_vars'][$i][$key] ) ) + $out['custom_vars'][$i][$key] = sanitize_text_field( $in['custom_vars'][$i][$key] ); + else + $out['custom_vars'][$i][$key] = ''; + } + } + + return $out; + } + + /** + * This is used to display the options page for this plugin + */ + public function settings_view() { +?> +
+

+
+ +
+
+$value) { + if (strpos(strtolower($value), strtolower($site_url)) === 0) { + $track[$k] = substr($track[$k], strlen($site_url)); + } + if ($k == 'data') { + $track[$k] = preg_replace("/^https?:\/\/|^\/+/i", "", $track[$k]); + } + + //This way we don't lose search data. + if ($k == 'data' && $track['code'] == 'search') { + $track[$k] = urlencode($track[$k]); + } else { + $track[$k] = preg_replace("/[^a-z0-9\.\/\+\?=-]+/i", "_", $track[$k]); + } + + $track[$k] = trim($track[$k], '_'); + } + $char = (strpos($track['data'], '?') === false)? '?':'&'; + return str_replace("'", "\'", "/{$track['code']}/{$track['data']}{$char}referer=" . urlencode( isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '' ) ); + } + + /** + * Maybe output or return, depending on the context + */ + private function _output_or_return( $val, $maybe ) { + if ( $maybe ) + echo $val . "\r\n"; + else + return $val; + } + + /** + * This injects the Google Analytics code into the footer of the page. + * + * @param bool[optional] $output - defaults to true, false returns but does NOT echo the code + */ + public function insert_code( $output = true ) { + //If $output is not a boolean false, set it to true (default) + $output = ($output !== false); + + $tracking_id = $this->_get_options( 'code' ); + if ( empty( $tracking_id ) ) + return $this->_output_or_return( '', $output ); + + //get our plugin options + $wga = $this->_get_options(); + //If the user's role has wga_no_track set to true, return without inserting code + if ( is_user_logged_in() ) { + $current_user = wp_get_current_user(); + $role = array_shift( $current_user->roles ); + if ( 'true' == $this->_get_options( 'ignore_role_' . $role ) ) + return $this->_output_or_return( "", $output ); + } + + //If $admin is true (we're in the admin_area), and we've been told to ignore_admin_area, return without inserting code + if (is_admin() && (!isset($wga['ignore_admin_area']) || $wga['ignore_admin_area'] != 'false')) + return $this->_output_or_return( "", $output ); + + $custom_vars = array( + "_gaq.push(['_setAccount', '{$tracking_id}']);", + ); + + // Add custom variables specified by the user + foreach( $this->_get_options( 'custom_vars', array() ) as $i => $custom_var ) { + if ( empty( $custom_var['name'] ) || empty( $custom_var['value'] ) ) + continue; + + // Check whether a token was used with this custom var, and replace with value if so + $all_tokens = wp_list_pluck( $this->tokens, 'token' ); + if ( in_array( $custom_var['value'], $all_tokens ) ) { + $token = array_pop( wp_filter_object_list( $this->tokens, array( 'token' => $custom_var['value'] ) ) ); + + // Allow tokens to return empty values for specific contexts + $ignore = false; + if ( ! empty( $token['ignore_when'] ) ) { + foreach( (array)$token['ignore_when'] as $conditional ) { + if ( is_callable( $conditional ) ) { + $ignore = call_user_func( $conditional ); + if ( $ignore ) + break; + } + } + } + + // If we aren't set to ignore this context, possibly execute the callback + if ( ! $ignore && ! empty( $token['callback'] ) && is_callable( $token['callback'] ) ) + $replace = call_user_func( $token['callback'] ); + else + $replace = ''; + + if ( ! empty( $token['callback_returns'] ) && 'bool' == $token['callback_returns'] ) + $replace = ( $replace ) ? 'true' : 'false'; + + // Replace our token with the value + $custom_var['value'] = str_replace( $custom_var['value'], $replace, $custom_var['value'] ); + } + + $atts = array( + "'_setCustomVar'", + intval( $i ), + "'" . esc_js( $custom_var['name'] ) . "'", + "'" . esc_js( $custom_var['value'] ) . "'", + ); + if ( $custom_var['scope'] ) + $atts[] = intval( $custom_var['scope'] ); + $custom_vars[] = "_gaq.push([" . implode( ', ', $atts ) . "]);"; + } + + $track = array(); + if (is_404() && (!isset($wga['log_404s']) || $wga['log_404s'] != 'false')) { + // This is a 404 and we are supposed to track them + $custom_vars[] = "_gaq.push( [ '_trackEvent', '404', document.location.href, document.referrer ] );"; + } elseif (is_search() && (!isset($wga['log_searches']) || $wga['log_searches'] != 'false')) { + //Set track for searches, if it's a search, and we are supposed to + $track['data'] = $_REQUEST['s']; + $track['code'] = "search"; + } + + if ( ! empty( $track ) ) { + $track['url'] = $this->_get_url( $track ); + //adjust the code that we output, account for both types of tracking + $track['url'] = esc_js( str_replace( '&', '&', $track['url'] ) ); + $custom_vars[] = "_gaq.push(['_trackPageview','{$track['url']}']);"; + } else { + $custom_vars[] = "_gaq.push(['_trackPageview']);"; + } + + if ($wga['enable_display_advertising'] == 'true' ) { + $async_code = ""; + } else { + $async_code = ""; + + } + + $custom_vars_string = implode( "\r\n", $custom_vars ); + $async_code = str_replace( '%custom_vars%', $custom_vars_string, $async_code ); + + return $this->_output_or_return( $async_code, $output ); + + } + + /** + * Used to get one or all of our plugin options + * + * @param string[optional] $option - Name of options you want. Do not use if you want ALL options + * @return array of options, or option value + */ + private function _get_options( $option = null, $default = false ) { + + $o = get_option('wga'); + + if (isset($option)) { + + if (isset($o[$option])) { + if ( 'code' == $option ) { + if ( preg_match( '#UA-[\d-]+#', $o[$option], $matches ) ) + return $matches[0]; + else + return ''; + } else + return $o[$option]; + } else { + if ( 'ignore_role_' == substr( $option, 0, 12 ) ) { + global $wp_roles; + // Backwards compat for when the tracking information was stored as a cap + $maybe_role = str_replace( 'ignore_role_', '', $option ); + if ( isset( $wp_roles->roles[$maybe_role] ) ) { + if ( isset( $wp_roles->roles[$maybe_role]['capabilities']['wga_no_track'] ) && $wp_roles->roles[$maybe_role]['capabilities']['wga_no_track'] ) + return 'true'; + } + return false; + } + return $default; + } + } else { + return $o; + } + } + + /** + * If we track outgoing links, this will enqueue our javascript file + */ + public function track_outgoing() { + if ( 'true' == $this->_get_options( 'log_outgoing' ) && (!defined('XMLRPC_REQUEST') || !XMLRPC_REQUEST) && ( ! is_admin() || 'false' == $this->_get_options( 'ignore_admin_area' ) ) ) + wp_enqueue_script( 'wp-google-analytics', plugin_dir_url( __FILE__ ) . 'wp-google-analytics.js', array( 'jquery' ), '0.0.3' ); + } + + /** + * Callback for %the_category% token + */ + public function token_the_category() { + return implode( ', ', wp_list_pluck( (array)get_the_category(), 'name' ) ); + } + + /** + * Callback for %context% token + */ + public function token_context() { + if ( is_admin() ) { + return 'admin'; + } else if ( is_home() || is_front_page() ) { + return 'home'; + } else if ( is_tax() || is_tag() || is_category() ) { + return get_queried_object()->taxonomy; + } else if ( is_author() ) { + return 'author'; + } else if ( is_singular() || is_single() || is_page() ) { + return get_post_type(); + } else if ( is_search() ) { + return 'search'; + } else if ( is_date() ) { + return 'date'; + } else if ( is_archive() ) { + return 'archive'; + } else if ( is_404() ) { + return '404'; + } + } + + /** + * Callback for %the_tags% token + */ + public function token_the_tags() { + return implode( ', ', wp_list_pluck( (array)get_the_tags(), 'name' ) ); + } + + public function add_plugin_page_links( $links, $file ){ + if ( plugin_basename( __FILE__ ) == $file ) { + $link = '' . __( 'Settings', 'wp-google-analytics' ) . ''; + array_unshift( $links, $link ); + } + return $links; + } + +} + +global $wp_google_analytics; +$wp_google_analytics = wpGoogleAnalytics::get_instance(); diff --git a/modules/google-analytics/wp-google-analytics/wpcom-helper.php b/modules/google-analytics/wp-google-analytics/wpcom-helper.php new file mode 100644 index 0000000000000..3635f640e24f2 --- /dev/null +++ b/modules/google-analytics/wp-google-analytics/wpcom-helper.php @@ -0,0 +1,49 @@ + 'googleanalytics', + 'attributes' => array(), + 'config_data' => array( + 'vars' => array( + 'account' => $tracking_code, + ), + 'triggers' => array( + 'trackPageview' => array( + 'on' => 'visible', + 'request' => 'pageview', + 'vars' => array( + 'title' => get_the_title(), + 'ampdocUrl' => get_permalink(), + ), + ), + ), + ), + ); + + return $analytics; +} diff --git a/modules/module-headings.php b/modules/module-headings.php index cc2ceea33b2b7..0d4a0edde677c 100644 --- a/modules/module-headings.php +++ b/modules/module-headings.php @@ -49,6 +49,11 @@ function jetpack_get_module_i18n( $key ) { 'description' => _x( 'Increase reach and traffic.', 'Module Description', 'jetpack' ), ), + 'google-analytics' => array( + 'name' => _x( 'Google Analytics', 'Module Name', 'jetpack' ), + 'description' => _x( 'Collect traffic stats and insights with Google Analytics.', 'Module Description', 'jetpack' ), + ), + 'gravatar-hovercards' => array( 'name' => _x( 'Gravatar Hovercards', 'Module Name', 'jetpack' ), 'description' => _x( 'Enable pop-up business cards over commenters’ Gravatars.', 'Module Description', 'jetpack' ), diff --git a/modules/module-info.php b/modules/module-info.php index 6b7d538a9b1ae..70d24e053e8ea 100644 --- a/modules/module-info.php +++ b/modules/module-info.php @@ -641,3 +641,18 @@ function jetpack_wordads_more_info() { } add_action( 'jetpack_module_more_info_wordads', 'jetpack_wordads_more_info' ); // WordAds: STOP + +/** + * Google Analytics + */ +function jetpack_google_analytics_more_link() { + echo 'https://support.wordpress.com/google-analytics/'; +} +add_action( 'jetpack_learn_more_button_google-analytics', 'jetpack_google_analytics_more_link' ); + +function jetpack_google_analytics_more_info() { + esc_html_e( + 'Track website statistics with Google Analytics for a deeper understanding of your website visitors and customers.' + , 'jetpack' ); +} +add_action( 'jetpack_module_more_info_google-analytics', 'jetpack_google_analytics_more_info' ); From 36074703f48f7730463a1efc62956b6a4f0d7e8c Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Wed, 9 Nov 2016 22:46:47 -0300 Subject: [PATCH 02/43] Updates sort order of google analytics plugin --- modules/google-analytics.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/google-analytics.php b/modules/google-analytics.php index 1fe924309ce2f..6bb898740724d 100644 --- a/modules/google-analytics.php +++ b/modules/google-analytics.php @@ -4,7 +4,7 @@ * Module Name: Google Analytics * Module Description: MODULE DESCRIPTION COPY. * First Introduced: 4.4 - * Sort Order: 35 + * Sort Order: 37 * Requires Connection: No * Auto Activate: Yes * Feature: Engagement From 885fad4198bbe6e6b5084b93f150dfd08b305dd4 Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Fri, 11 Nov 2016 16:10:46 -0300 Subject: [PATCH 03/43] Adds pro detection and solves sync issue --- _inc/client/engagement/index.jsx | 9 ++-- _inc/client/pro-status/index.jsx | 12 +++++ _inc/lib/class.core-rest-api-endpoints.php | 4 +- ....wpcom-json-api-site-settings-endpoint.php | 6 ++- modules/google-analytics.php | 31 +++++++++--- .../{wp-google-analytics => }/README.md | 0 .../languages/wp-google-analytics.pot | 0 .../{wp-google-analytics => }/readme.txt | 0 .../wp-google-analytics.js | 0 .../wp-google-analytics.php | 0 .../wp-google-analytics/wpcom-helper.php | 49 ------------------- 11 files changed, 49 insertions(+), 62 deletions(-) rename modules/google-analytics/{wp-google-analytics => }/README.md (100%) rename modules/google-analytics/{wp-google-analytics => }/languages/wp-google-analytics.pot (100%) rename modules/google-analytics/{wp-google-analytics => }/readme.txt (100%) rename modules/google-analytics/{wp-google-analytics => }/wp-google-analytics.js (100%) rename modules/google-analytics/{wp-google-analytics => }/wp-google-analytics.php (100%) delete mode 100644 modules/google-analytics/wp-google-analytics/wpcom-helper.php diff --git a/_inc/client/engagement/index.jsx b/_inc/client/engagement/index.jsx index 6bebc4c737017..4ac0cfeeab09b 100644 --- a/_inc/client/engagement/index.jsx +++ b/_inc/client/engagement/index.jsx @@ -13,6 +13,8 @@ import analytics from 'lib/analytics'; /** * Internal dependencies */ +import QuerySite from 'components/data/query-site'; +import ProStatus from 'pro-status'; import { isModuleActivated as _isModuleActivated, activateModule, @@ -22,7 +24,6 @@ import { getModule as _getModule, getModules } from 'state/modules'; -import ProStatus from 'pro-status'; import { ModuleToggle } from 'components/module-toggle'; import { AllModuleSettings } from 'components/module-settings/modules-per-tab-page'; import { isUnavailableInDevMode } from 'state/connection'; @@ -42,7 +43,8 @@ export const Engagement = ( props ) => { toggleModule, isModuleActivated, isTogglingModule, - getModule + getModule, + sitePlan } = props, isAdmin = props.userCanManageModules, sitemapsDesc = getModule( 'sitemaps' ).description, @@ -92,6 +94,7 @@ export const Engagement = ( props ) => { } ); cards = cards.filter( ( element, index ) => cards.indexOf( element ) === index ); } + cards = cards.map( ( element ) => { if ( ! includes( moduleList, element[0] ) ) { return null; @@ -101,7 +104,7 @@ export const Engagement = ( props ) => { customClasses = unavailableInDevMode ? 'devmode-disabled' : '', toggle = '', adminAndNonAdmin = isAdmin || includes( nonAdminAvailable, element[0] ), - isPro = 'seo-tools' === element[0] || 'wordads' === element[0], + isPro = 'seo-tools' === element[0] ||'google-analytics' === element[0] || 'wordads' === element[0], proProps = { module: element[0], configure_url: '' diff --git a/_inc/client/pro-status/index.jsx b/_inc/client/pro-status/index.jsx index 85725426f2a3b..7c193b2c86607 100644 --- a/_inc/client/pro-status/index.jsx +++ b/_inc/client/pro-status/index.jsx @@ -121,6 +121,18 @@ const ProStatus = React.createClass( { ); } + if ( 'google-analytics' === feature && 'jetpack_business' !== sitePlan.product_slug ) { + return ( + + ); + } + if ( sitePlan.product_slug ) { let btnVals = {}; if ( 'jetpack_free' !== sitePlan.product_slug ) { diff --git a/_inc/lib/class.core-rest-api-endpoints.php b/_inc/lib/class.core-rest-api-endpoints.php index 8a80f771f4901..76250c087597a 100644 --- a/_inc/lib/class.core-rest-api-endpoints.php +++ b/_inc/lib/class.core-rest-api-endpoints.php @@ -2106,9 +2106,11 @@ public static function prepare_options_for_response( $module = '' ) { case 'google-analytics': $wga = get_option( 'wga' ); + $code = ''; if ( is_array( $wga ) && array_key_exists( 'code', $wga ) ) { - $options[ 'google_analytics_tracking_id' ][ 'current_value' ] = $wga[ 'code' ]; + $code = $wga[ 'code' ]; } + $options[ 'google_analytics_tracking_id' ][ 'current_value' ] = $code; break; case 'sharedaddy': diff --git a/json-endpoints/class.wpcom-json-api-site-settings-endpoint.php b/json-endpoints/class.wpcom-json-api-site-settings-endpoint.php index 589f7e8532072..b6b0d31f31a88 100644 --- a/json-endpoints/class.wpcom-json-api-site-settings-endpoint.php +++ b/json-endpoints/class.wpcom-json-api-site-settings-endpoint.php @@ -366,8 +366,10 @@ public function update_settings() { /** This action is documented in modules/widgets/social-media-icons.php */ do_action( 'jetpack_bump_stats_extras', 'google-analytics', $enabled_or_disabled ); - $business_plugins = WPCOM_Business_Plugins::instance(); - $business_plugins->activate_plugin( 'wp-google-analytics' ); + if ( ! $is_jetpack && defined( 'IS_WPCOM' ) && IS_WPCOM ) { + $business_plugins = WPCOM_Business_Plugins::instance(); + $business_plugins->activate_plugin( 'wp-google-analytics' ); + } break; case 'jetpack_testimonial': diff --git a/modules/google-analytics.php b/modules/google-analytics.php index 6bb898740724d..b283fa24efaa5 100644 --- a/modules/google-analytics.php +++ b/modules/google-analytics.php @@ -6,18 +6,35 @@ * First Introduced: 4.4 * Sort Order: 37 * Requires Connection: No - * Auto Activate: Yes + * Auto Activate: No * Feature: Engagement * Additional Search Queries: webmaster, google, analytics, console */ -function jetpack_load_google_analytics() { - include dirname( __FILE__ ) . "/google-analytics/wp-google-analytics/wp-google-analytics.php"; +/** + * Removes the Google Analytics plugin settings page + */ +function removeSettingsPage() { + remove_submenu_page( 'options-general.php', 'wp-google-analytics' ); } -function jetpack_google_analytics_loaded() { - Jetpack::enable_module_configurable( __FILE__ ); - Jetpack::module_configuration_load( __FILE__, 'jetpack_google_analytics_configuration_load' ); +/** + * Does the site have a Jetpack plan attached to it that includes Google Analytics? + * + * @return bool + */ +function isGoogleAnalyticsIncludedInJetpackPlan() { + $site_id = Jetpack_Options::get_option( 'id' ); + $result = Jetpack_Client::wpcom_json_api_request_as_blog( sprintf( '/sites/%d', $site_id ), '1.1' ); + + if ( is_wp_error( $result ) ) { + return false; + } + + $response = json_decode( $result['body'], true ); + + return $response['plan']['product_slug'] == 'jetpack_business'; } -jetpack_load_google_analytics(); +include dirname( __FILE__ ) . "/google-analytics/wp-google-analytics.php"; +add_action( 'admin_menu', 'removeSettingsPage' ); diff --git a/modules/google-analytics/wp-google-analytics/README.md b/modules/google-analytics/README.md similarity index 100% rename from modules/google-analytics/wp-google-analytics/README.md rename to modules/google-analytics/README.md diff --git a/modules/google-analytics/wp-google-analytics/languages/wp-google-analytics.pot b/modules/google-analytics/languages/wp-google-analytics.pot similarity index 100% rename from modules/google-analytics/wp-google-analytics/languages/wp-google-analytics.pot rename to modules/google-analytics/languages/wp-google-analytics.pot diff --git a/modules/google-analytics/wp-google-analytics/readme.txt b/modules/google-analytics/readme.txt similarity index 100% rename from modules/google-analytics/wp-google-analytics/readme.txt rename to modules/google-analytics/readme.txt diff --git a/modules/google-analytics/wp-google-analytics/wp-google-analytics.js b/modules/google-analytics/wp-google-analytics.js similarity index 100% rename from modules/google-analytics/wp-google-analytics/wp-google-analytics.js rename to modules/google-analytics/wp-google-analytics.js diff --git a/modules/google-analytics/wp-google-analytics/wp-google-analytics.php b/modules/google-analytics/wp-google-analytics.php similarity index 100% rename from modules/google-analytics/wp-google-analytics/wp-google-analytics.php rename to modules/google-analytics/wp-google-analytics.php diff --git a/modules/google-analytics/wp-google-analytics/wpcom-helper.php b/modules/google-analytics/wp-google-analytics/wpcom-helper.php deleted file mode 100644 index 3635f640e24f2..0000000000000 --- a/modules/google-analytics/wp-google-analytics/wpcom-helper.php +++ /dev/null @@ -1,49 +0,0 @@ - 'googleanalytics', - 'attributes' => array(), - 'config_data' => array( - 'vars' => array( - 'account' => $tracking_code, - ), - 'triggers' => array( - 'trackPageview' => array( - 'on' => 'visible', - 'request' => 'pageview', - 'vars' => array( - 'title' => get_the_title(), - 'ampdocUrl' => get_permalink(), - ), - ), - ), - ), - ); - - return $analytics; -} From 498b5f79735c44b93d2ddfe48d79e4684fe07693 Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Thu, 8 Dec 2016 15:59:18 -0300 Subject: [PATCH 04/43] Makes form legend translatable. --- _inc/client/components/module-settings/index.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_inc/client/components/module-settings/index.jsx b/_inc/client/components/module-settings/index.jsx index afc08d58e57bc..0d4fe8408e1c0 100644 --- a/_inc/client/components/module-settings/index.jsx +++ b/_inc/client/components/module-settings/index.jsx @@ -473,7 +473,7 @@ export let VerificationToolsSettings = React.createClass( {
- Google + __( 'Google' ) Date: Thu, 8 Dec 2016 16:02:22 -0300 Subject: [PATCH 05/43] Updates module description --- modules/google-analytics.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/google-analytics.php b/modules/google-analytics.php index b283fa24efaa5..4ec7091473cd1 100644 --- a/modules/google-analytics.php +++ b/modules/google-analytics.php @@ -2,10 +2,10 @@ /** * Module Name: Google Analytics - * Module Description: MODULE DESCRIPTION COPY. + * Module Description: Lets you use Google Analytics to track your WordPress site statistics. * First Introduced: 4.4 * Sort Order: 37 - * Requires Connection: No + * Requires Connection: Yes * Auto Activate: No * Feature: Engagement * Additional Search Queries: webmaster, google, analytics, console From b479e5b11f7bb658a4c2e68e31d9410a332a60cc Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Thu, 8 Dec 2016 16:11:02 -0300 Subject: [PATCH 06/43] Updates module coding style and removes business check function --- modules/google-analytics.php | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/modules/google-analytics.php b/modules/google-analytics.php index 4ec7091473cd1..059305da97b4a 100644 --- a/modules/google-analytics.php +++ b/modules/google-analytics.php @@ -14,27 +14,9 @@ /** * Removes the Google Analytics plugin settings page */ -function removeSettingsPage() { +function remove_settings_page() { remove_submenu_page( 'options-general.php', 'wp-google-analytics' ); } -/** - * Does the site have a Jetpack plan attached to it that includes Google Analytics? - * - * @return bool - */ -function isGoogleAnalyticsIncludedInJetpackPlan() { - $site_id = Jetpack_Options::get_option( 'id' ); - $result = Jetpack_Client::wpcom_json_api_request_as_blog( sprintf( '/sites/%d', $site_id ), '1.1' ); - - if ( is_wp_error( $result ) ) { - return false; - } - - $response = json_decode( $result['body'], true ); - - return $response['plan']['product_slug'] == 'jetpack_business'; -} - include dirname( __FILE__ ) . "/google-analytics/wp-google-analytics.php"; -add_action( 'admin_menu', 'removeSettingsPage' ); +add_action( 'admin_menu', 'remove_settings_page' ); From 94b9c1cac3cd9ecddba14fa2576b9a480ffed049 Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Fri, 9 Dec 2016 02:52:51 -0300 Subject: [PATCH 07/43] PR Feedback --- .jshintignore | 1 + .../components/module-settings/index.jsx | 8 +- _inc/client/engagement/index.jsx | 9 +- _inc/lib/class.core-rest-api-endpoints.php | 2 +- ...lass.jetpack-core-api-module-endpoints.php | 4 +- class.jetpack.php | 11 +- ....wpcom-json-api-site-settings-endpoint.php | 5 +- modules/google-analytics.php | 10 +- modules/google-analytics/README.md | 113 ----- .../languages/wp-google-analytics.pot | 185 -------- modules/google-analytics/readme.txt | 133 ------ .../google-analytics/wp-google-analytics.php | 418 ++++++------------ sync/class.jetpack-sync-defaults.php | 10 +- 13 files changed, 164 insertions(+), 745 deletions(-) delete mode 100644 modules/google-analytics/README.md delete mode 100644 modules/google-analytics/languages/wp-google-analytics.pot delete mode 100644 modules/google-analytics/readme.txt diff --git a/.jshintignore b/.jshintignore index b989b90adf04f..95a5db500d13c 100644 --- a/.jshintignore +++ b/.jshintignore @@ -9,6 +9,7 @@ modules/shortcodes/js/jmpress.js modules/shortcodes/js/jmpress.min.js modules/shortcodes/js/jquery.cycle.min.js modules/theme-tools/responsive-videos/responsive-videos.min.js +modules/google-analytics/wp-google-analytics.js todo: modules/infinite-scroll/infinity.js diff --git a/_inc/client/components/module-settings/index.jsx b/_inc/client/components/module-settings/index.jsx index 0d4fe8408e1c0..b41234550e33e 100644 --- a/_inc/client/components/module-settings/index.jsx +++ b/_inc/client/components/module-settings/index.jsx @@ -473,7 +473,7 @@ export let VerificationToolsSettings = React.createClass( {
- __( 'Google' ) + { __( 'Google' ) } - Bing + { __( 'Bing' ) } - Pinterest + { __( 'Pinterest' ) } - Yandex + { __( 'Yandex' ) } { toggleModule, isModuleActivated, isTogglingModule, - getModule, - sitePlan + getModule } = props, isAdmin = props.userCanManageModules, sitemapsDesc = getModule( 'sitemaps' ).description, @@ -94,7 +92,6 @@ export const Engagement = ( props ) => { } ); cards = cards.filter( ( element, index ) => cards.indexOf( element ) === index ); } - cards = cards.map( ( element ) => { if ( ! includes( moduleList, element[0] ) ) { return null; @@ -104,7 +101,7 @@ export const Engagement = ( props ) => { customClasses = unavailableInDevMode ? 'devmode-disabled' : '', toggle = '', adminAndNonAdmin = isAdmin || includes( nonAdminAvailable, element[0] ), - isPro = 'seo-tools' === element[0] ||'google-analytics' === element[0] || 'wordads' === element[0], + isPro = includes( [ 'seo-tools', 'wordads', 'google-analytics' ], element[0] ), proProps = { module: element[0], configure_url: '' diff --git a/_inc/lib/class.core-rest-api-endpoints.php b/_inc/lib/class.core-rest-api-endpoints.php index 76250c087597a..84c7bf22e53fd 100644 --- a/_inc/lib/class.core-rest-api-endpoints.php +++ b/_inc/lib/class.core-rest-api-endpoints.php @@ -2105,7 +2105,7 @@ public static function prepare_options_for_response( $module = '' ) { break; case 'google-analytics': - $wga = get_option( 'wga' ); + $wga = get_option( 'jetpack_wga' ); $code = ''; if ( is_array( $wga ) && array_key_exists( 'code', $wga ) ) { $code = $wga[ 'code' ]; diff --git a/_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php b/_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php index 64b92ce8edf09..d5833ccd01d62 100644 --- a/_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php +++ b/_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php @@ -719,11 +719,11 @@ public function update_data( $data ) { break; case 'google_analytics_tracking_id': - $grouped_options = $grouped_options_current = (array) get_option( 'wga' ); + $grouped_options = $grouped_options_current = (array) get_option( 'jetpack_wga' ); $grouped_options[ 'code' ] = $value; // If option value was the same, consider it done. - $updated = $grouped_options_current != $grouped_options ? update_option( 'wga', $grouped_options ) : true; + $updated = $grouped_options_current != $grouped_options ? update_option( 'jetpack_wga', $grouped_options ) : true; break; case 'wp_mobile_featured_images': diff --git a/class.jetpack.php b/class.jetpack.php index 3e6656c8f12a2..7f9a871522129 100644 --- a/class.jetpack.php +++ b/class.jetpack.php @@ -1217,14 +1217,14 @@ public static function get_active_plan() { // Set the default options if ( ! $plan ) { - $plan = array( - 'product_slug' => 'jetpack_free', - 'supports' => array(), + $plan = array( + 'product_slug' => 'jetpack_free', + 'supports' => array(), ); } // Define what paid modules are supported by personal plans - $personal_plans = array( + $personal_plans = array( 'jetpack_personal', 'jetpack_personal_monthly', ); @@ -1261,6 +1261,7 @@ public static function get_active_plan() { 'akismet', 'vaultpress', 'seo-tools', + 'google-analytics', ); } @@ -2292,7 +2293,7 @@ public static function get_translated_modules( $modules ) { */ public static function get_active_modules() { $active = Jetpack_Options::get_option( 'active_modules' ); - + if ( ! is_array( $active ) ) { $active = array(); } diff --git a/json-endpoints/class.wpcom-json-api-site-settings-endpoint.php b/json-endpoints/class.wpcom-json-api-site-settings-endpoint.php index b6b0d31f31a88..552893ead62a1 100644 --- a/json-endpoints/class.wpcom-json-api-site-settings-endpoint.php +++ b/json-endpoints/class.wpcom-json-api-site-settings-endpoint.php @@ -352,12 +352,13 @@ public function update_settings() { } break; case 'wga': + case 'jetpack_wga': if ( ! isset( $value['code'] ) || ! preg_match( '/^$|^UA-[\d-]+$/i', $value['code'] ) ) { return new WP_Error( 'invalid_code', 'Invalid UA ID' ); } - $wga = get_option( 'wga', array() ); + $wga = get_option( 'jetpack_wga', array() ); $wga['code'] = $value['code']; // maintain compatibility with wp-google-analytics - if ( update_option( 'wga', $wga ) ) { + if ( update_option( 'jetpack_wga', $wga ) ) { $updated[ $key ] = $value; } diff --git a/modules/google-analytics.php b/modules/google-analytics.php index 059305da97b4a..5704193d6fe7d 100644 --- a/modules/google-analytics.php +++ b/modules/google-analytics.php @@ -11,12 +11,6 @@ * Additional Search Queries: webmaster, google, analytics, console */ -/** - * Removes the Google Analytics plugin settings page - */ -function remove_settings_page() { - remove_submenu_page( 'options-general.php', 'wp-google-analytics' ); +if ( Jetpack::active_plan_supports( 'google-analytics' ) ) { + include dirname( __FILE__ ) . "/google-analytics/wp-google-analytics.php"; } - -include dirname( __FILE__ ) . "/google-analytics/wp-google-analytics.php"; -add_action( 'admin_menu', 'remove_settings_page' ); diff --git a/modules/google-analytics/README.md b/modules/google-analytics/README.md deleted file mode 100644 index dfb80fb2560f4..0000000000000 --- a/modules/google-analytics/README.md +++ /dev/null @@ -1,113 +0,0 @@ -# WP Google Analytics - -Lets you use Google Analytics to track your WordPress site statistics. Brought to you by Range - -## Description - -WP Google Analytics makes it easy to track your site's usage, with lots of -helpful additional data. - -Features: - -* Uses Google's asynchronous tracking method which is faster and more reliable. -* Automatically tracks site speed -* Option to log outgoing links as events -* Option to log 404 errors as events -* Use custom variables in Google Analytics to track additional data on pageviews including: - * Author - * Categories - * Tags - * Context (such as home, category, post, author, etc) - * Date - * Logged in - * Anything - Use the built-in filter to add your own! -* Allows you to ignore any user roles (administrators, editors, authors, etc) - -## Installation - -Use automatic installer. - -## Frequently Asked Questions - -**Where do I put my Google Analytics Code?** - -WP Google Analytics has a config page under the settings tab in the admin area -of your site. You can paste your tracking code from Google into the textarea on -this page. - -**How do I track searches?** - -WP Google Analytics used to help you track site searches before Google Analytics -started doing this natively. While we still support tracking searches for -backwards compatibility, this feature has been deprecated and will eventually be -removed. To track searches in Google Analytics follow this -Google support article. -WordPress uses 's' as the query parameter. - -**Can't I just paste the Google Analytics code into my template file?** - -Absolutely, however in order to get a better idea of what is going on with your -site, it is often nice to have your own activities ignored, track 404s, searches -and even where users go when they leave your site. WP Google Analytics lets you -easily do all these things. - -**What tokens are support for custom variables?** - -All the built-in tokens are described on the settings page. You can also add -your own using the 'wga_tokens' filter. - -## Changelog - -### 1.4.1 -* Fix undefined index notice for users upgrading from pre-1.3.0 - -### 1.4.0 -* Support for tokens in custom variables - -### 1.3.1 -* Fixed custom variables not being tracked - -### 1.3.0 -* Refactored to use settings API - Props danielbachhuber -* Convert to singleton and instatiate class -* Convert tracking code field to ID, keeping backwards compat - Props danielbachhuber -* Custom variable support - Props danielbachhuber -* Track outgoing links and 404s as events -* Made the whole plugin translatable -* Deprecated tracking searches - -### 1.2.5 -* Fixed some notices. Props westi -* Update all links - -### 1.2.4 -* Removed the optional anonymous statistics collection. Nothing is ever collected anymore. -* Changed & to & in some more places to fix validation problems. - -### 1.2.3 -* Changed & to & to fix validation problems. - -### 1.2.2 -* Fixed problem with code affecting Admin Javascript such as the TinyMCE editor - -### 1.2.1 -* Bug fix for the stats gathering - -### 1.2.0 -* No longer parses outgoing links in the admin section. -* Uses get_footer instead of wp_footer. Too many themes aren't adding the wp_footer call. -* Options page updated -* Added optional anonymous statistics collection - -### 1.1.0 -* Major revamp to work better with the new Google Tracking Code. It seems that outgoing links weren't being tracked properly. - -### 1.0.0 -* Added to wordpress.org repository - -### 0.2 -* Fixed problem with themes that do not call wp_footer(). If you are reading this and you are a theme developer, USE THE HOOKS! That's what they're there for! -* Updated how the admin section is handled - -### 0.1 -* Original Version diff --git a/modules/google-analytics/languages/wp-google-analytics.pot b/modules/google-analytics/languages/wp-google-analytics.pot deleted file mode 100644 index 3c045ab8e6c71..0000000000000 --- a/modules/google-analytics/languages/wp-google-analytics.pot +++ /dev/null @@ -1,185 +0,0 @@ -# Copyright (C) 2012 WP Google Analytics -# This file is distributed under the same license as the WP Google Analytics package. -msgid "" -msgstr "" -"Project-Id-Version: WP Google Analytics 1.4.0\n" -"Report-Msgid-Bugs-To: http://wordpress.org/tag/wp-google-analytics\n" -"POT-Creation-Date: 2012-10-05 21:16:58+00:00\n" -"MIME-Version: 1.0\n" -"Content-Type: text/plain; charset=UTF-8\n" -"Content-Transfer-Encoding: 8bit\n" -"PO-Revision-Date: 2012-MO-DA HO:MI+ZONE\n" -"Last-Translator: FULL NAME \n" -"Language-Team: LANGUAGE \n" - -#: wp-google-analytics.php:78 -msgid "Post author for current view" -msgstr "" - -#: wp-google-analytics.php:79 -msgid "Post author's display name" -msgstr "" - -#: wp-google-analytics.php:94 -msgid "Categories assigned to a post" -msgstr "" - -#: wp-google-analytics.php:95 -msgid "Category names in a commma-separated list" -msgstr "" - -#: wp-google-analytics.php:109 -msgid "Which view the visitor is on" -msgstr "" - -#: wp-google-analytics.php:110 -msgid "Samples: 'home', 'category', 'post', 'author'" -msgstr "" - -#: wp-google-analytics.php:116 -msgid "Publication date for the current view" -msgstr "" - -#: wp-google-analytics.php:117 -msgid "Format specified by 'Date Format' in Settings -> General" -msgstr "" - -#: wp-google-analytics.php:132 -msgid "Tags assigned to a post" -msgstr "" - -#: wp-google-analytics.php:133 -msgid "Tag names in a commma-separated list" -msgstr "" - -#: wp-google-analytics.php:148 -msgid "Whether or not the viewer is logged in" -msgstr "" - -#: wp-google-analytics.php:149 -msgid "'true' or 'false'" -msgstr "" - -#: wp-google-analytics.php:160 -msgid "Google Analytics" -msgstr "" - -#: wp-google-analytics.php:171 -msgid "Google Analytics tracking ID:" -msgstr "" - -#: wp-google-analytics.php:172 -msgid "Additional items to log:" -msgstr "" - -#: wp-google-analytics.php:173 -msgid "Visits to ignore:" -msgstr "" - -#: wp-google-analytics.php:174 -msgid "Custom variables:" -msgstr "" - -#: wp-google-analytics.php:183 -msgid "" -"Use tokens in your custom variables to make your fields dynamic based on " -"context. Here are some of the tokens you can use:" -msgstr "" - -#: wp-google-analytics.php:185 -msgid "Token" -msgstr "" - -#: wp-google-analytics.php:185 -msgid "Description" -msgstr "" - -#: wp-google-analytics.php:185 -msgid "Return value" -msgstr "" - -#: wp-google-analytics.php:199 -msgid "" -"Paste your Google Analytics tracking ID (e.g. \"UA-XXXXXX-X\") into the " -"field." -msgstr "" - -#: wp-google-analytics.php:207 -msgid "Log 404 errors as events" -msgstr "" - -#: wp-google-analytics.php:208 -msgid "" -"Log searches as /search/{search}?referrer={referrer} (deprecated)" -msgstr "" - -#: wp-google-analytics.php:209 -msgid "Log outgoing links as events" -msgstr "" - -#: wp-google-analytics.php:227 -msgid "Default" -msgstr "" - -#: wp-google-analytics.php:228 -msgid "Visitor" -msgstr "" - -#: wp-google-analytics.php:229 -msgid "Session" -msgstr "" - -#: wp-google-analytics.php:230 -msgid "Page" -msgstr "" - -#: wp-google-analytics.php:236 -msgid "Name" -msgstr "" - -#: wp-google-analytics.php:239 -msgid "Value" -msgstr "" - -#: wp-google-analytics.php:242 -msgid "Scope" -msgstr "" - -#: wp-google-analytics.php:256 -msgid "Do not log anything in the admin area" -msgstr "" - -#: wp-google-analytics.php:260 -msgid "Do not log %s when logged in" -msgstr "" - -#: wp-google-analytics.php:321 -msgid "Google Analytics Options" -msgstr "" - -#: wp-google-analytics.php:326 -msgid "Update Options" -msgstr "" - -#. Plugin Name of the plugin/theme -msgid "WP Google Analytics" -msgstr "" - -#. Plugin URI of the plugin/theme -msgid "http://bluedogwebservices.com/wordpress-plugin/wp-google-analytics/" -msgstr "" - -#. Description of the plugin/theme -msgid "" -"Lets you use Google Analytics to " -"track your WordPress site statistics" -msgstr "" - -#. Author of the plugin/theme -msgid "Aaron D. Campbell" -msgstr "" - -#. Author URI of the plugin/theme -msgid "http://ran.ge/" -msgstr "" diff --git a/modules/google-analytics/readme.txt b/modules/google-analytics/readme.txt deleted file mode 100644 index 5c662820e4660..0000000000000 --- a/modules/google-analytics/readme.txt +++ /dev/null @@ -1,133 +0,0 @@ -=== WP Google Analytics === -Contributors: aaroncampbell, PeteMall, range, saracannon -Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=CLXNQ3DVK6M48 -Tags: analytics, google, google analytics -Requires at least: 3.1 -Tested up to: 3.5 -Stable tag: 1.4.0 - -Lets you use Google Analytics to track your WordPress site statistics - -== Description == - -WP Google Analytics makes it easy to track your site's usage, with lots of -helpful additional data. - -Features: - -* Uses Google's asynchronous tracking method which is faster and more reliable. -* Automatically tracks site speed -* Option to log outgoing links as events -* Option to log 404 errors as events -* Use custom variables in Google Analytics to track additional data on pageviews including: - * Author - * Categories - * Tags - * Context (such as home, category, post, author, etc) - * Date - * Logged in - * Anything - Use the built-in filter to add your own! -* Allows you to ignore any user roles (administrators, editors, authors, etc) - -WP Google Analytics -Brought to you by Ran.ge - -== Installation == - -Use automatic installer. - -== Frequently Asked Questions == - -= Where do I put my Google Analytics Code? = - -WP Google Analytics has a config page under the settings tab in the admin area -of your site. You can paste your tracking code from Google into the textarea on -this page. - -= How do I track searches? = - -WP Google Analytics used to help you track site searches before Google Analytics -started doing this natively. While we still support tracking searches for -backwards compatibility, this feature has been deprecated and will eventually be -removed. To track searches in Google Analytics follow this -Google support article. -WordPress uses 's' as the query parameter. - -= What tokens are support for custom variables? = - -All the built-in tokens are described on the settings page. You can also add -your own using the 'wga_tokens' filter. - -= Can't I just paste the Google Analytics code into my template file? = - -Absolutely, however in order to get a better idea of what is going on with your -site, it is often nice to have your own activities ignored, track 404s, searches -and even where users go when they leave your site. WP Google Analytics lets you -easily do all these things. - -== Upgrade Notice == - -= 1.4.0 = -Allow tokens in the custom variables - -= 1.3.1 = -Fix for custom variables not being tracked - -= 1.3.0 = -Move to using events for tracking 404s and outgoing links and add support for custom variables - -== Changelog == - -= 1.4.1 = -* Fix undefined index notice for users upgrading from pre-1.3.0 - -= 1.4.0 = -* Support for tokens in custom variables - -= 1.3.1 = -* Fixed custom variables not being tracked - -= 1.3.0 = -* Refactored to use settings API - Props danielbachhuber -* Convert to singleton and instatiate class -* Convert tracking code field to ID, keeping backwards compat - Props danielbachhuber -* Custom variable support - Props danielbachhuber -* Track outgoing links and 404s as events -* Made the whole plugin translatable -* Deprecated tracking searches - -= 1.2.5 = -* Fixed some notices. Props westi -* Update all links - -= 1.2.4 = -* Removed the optional anonymous statistics collection. Nothing is ever collected anymore. -* Changed & to & in some more places to fix validation problems. - -= 1.2.3 = -* Changed & to & to fix validation problems. - -= 1.2.2 = -* Fixed problem with code affecting Admin Javascript such as the TinyMCE editor - -= 1.2.1 = -* Bug fix for the stats gathering - -= 1.2.0 = -* No longer parses outgoing links in the admin section. -* Uses get_footer instead of wp_footer. Too many themes aren't adding the wp_footer call. -* Options page updated -* Added optional anonymous statistics collection - -= 1.1.0 = -* Major revamp to work better with the new Google Tracking Code. It seems that outgoing links weren't being tracked properly. - -= 1.0.0 = -* Added to wordpress.org repository - -= 0.2 = -* Fixed problem with themes that do not call wp_footer(). If you are reading this and you are a theme developer, USE THE HOOKS! That's what they're there for! -* Updated how the admin section is handled - -= 0.1 = -* Original Version diff --git a/modules/google-analytics/wp-google-analytics.php b/modules/google-analytics/wp-google-analytics.php index f2a9894f7fec8..beeb3cb3e118d 100644 --- a/modules/google-analytics/wp-google-analytics.php +++ b/modules/google-analytics/wp-google-analytics.php @@ -1,18 +1,8 @@ Google Analytics to track your WordPress site statistics - * Version: 1.4.0 - * Author: Aaron D. Campbell - * Author URI: http://ran.ge/ - * License: GPLv2 or later - * Text Domain: wp-google-analytics - */ - -define('WGA_VERSION', '1.4.0'); +define( 'JETPACK_WGA_VERSION', '1.4.0' ); -/* Copyright 2006 Aaron D. Campbell (email : wp_plugins@xavisys.com) +/* + Copyright 2006 Aaron D. Campbell (email : wp_plugins@xavisys.com) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -30,54 +20,50 @@ */ /** - * wpGoogleAnalytics is the class that handles ALL of the plugin functionality. + * JetpackGoogleAnalytics is the class that handles ALL of the plugin functionality. * It helps us avoid name collisions * http://codex.wordpress.org/Writing_a_Plugin#Avoiding_Function_Name_Collisions */ -class wpGoogleAnalytics { +class JetpackGoogleAnalytics { /** - * @var wpGoogleAnalytics - Static property to hold our singleton instance + * @var JetpackGoogleAnalytics - Static property to hold our singleton instance */ static $instance = false; - static $page_slug = 'wp-google-analytics'; - var $tokens = array(); /** * This is our constructor, which is private to force the use of get_instance() + * * @return void */ private function __construct() { add_filter( 'init', array( $this, 'init' ) ); add_action( 'admin_init', array( $this, 'admin_init' ) ); - add_action( 'admin_menu', array( $this, 'admin_menu' ) ); add_action( 'get_footer', array( $this, 'insert_code' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'track_outgoing' ) ); - add_filter( 'plugin_action_links', array( $this, 'add_plugin_page_links' ), 10, 2 ); } - /** + /** * Function to instantiate our class and make it a singleton */ public static function get_instance() { - if ( !self::$instance ) + if ( ! self::$instance ) { self::$instance = new self; + } return self::$instance; } public function init() { - load_plugin_textdomain( 'wp-google-analytics', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); - $this->tokens = array( array( 'token' => '%the_author%', 'callback' => 'get_the_author', 'callback_returns' => 'string', - 'description' => __( 'Post author for current view', 'wp-google-analytics' ), - 'retval' => __( "Post author's display name", 'wp-google-analytics' ), + 'description' => __( 'Post author for current view', 'jetpack' ), + 'retval' => __( "Post author's display name", 'jetpack' ), 'ignore_when' => array( 'is_home', 'is_front_page', @@ -92,8 +78,8 @@ public function init() { 'token' => '%the_category%', 'callback' => array( $this, 'token_the_category' ), 'callback_returns' => 'string', - 'description' => __( 'Categories assigned to a post', 'wp-google-analytics' ), - 'retval' => __( "Category names in a commma-separated list", 'wp-google-analytics' ), + 'description' => __( 'Categories assigned to a post', 'jetpack' ), + 'retval' => __( 'Category names in a commma-separated list', 'jetpack' ), 'ignore_when' => array( 'is_home', 'is_front_page', @@ -107,15 +93,15 @@ public function init() { 'token' => '%context%', 'callback' => array( $this, 'token_context' ), 'callback_returns' => 'string', - 'description' => __( 'Which view the visitor is on', 'wp-google-analytics' ), + 'description' => __( 'Which view the visitor is on', 'jetpack' ), 'retval' => __( "Samples: 'home', 'category', 'post', 'author'" ), ), array( 'token' => '%the_date%', 'callback' => 'get_the_date', 'callback_returns' => 'string', - 'description' => __( 'Publication date for the current view', 'wp-google-analytics' ), - 'retval' => __( "Format specified by 'Date Format' in Settings -> General", 'wp-google-analytics' ), + 'description' => __( 'Publication date for the current view', 'jetpack' ), + 'retval' => __( "Format specified by 'Date Format' in Settings -> General", 'jetpack' ), 'ignore_when' => array( 'is_home', 'is_front_page', @@ -130,8 +116,8 @@ public function init() { 'token' => '%the_tags%', 'callback' => array( $this, 'token_the_tags' ), 'callback_returns' => 'string', - 'description' => __( 'Tags assigned to a post', 'wp-google-analytics' ), - 'retval' => __( "Tag names in a commma-separated list", 'wp-google-analytics' ), + 'description' => __( 'Tags assigned to a post', 'jetpack' ), + 'retval' => __( 'Tag names in a commma-separated list', 'jetpack' ), 'ignore_when' => array( 'is_home', 'is_front_page', @@ -146,144 +132,24 @@ public function init() { 'token' => '%is_user_logged_in%', 'callback' => 'is_user_logged_in', 'callback_returns' => 'bool', - 'description' => __( 'Whether or not the viewer is logged in', 'wp-google-analytics' ), - 'retval' => __( "'true' or 'false'", 'wp-google-analytics' ), + 'description' => __( 'Whether or not the viewer is logged in', 'jetpack' ), + 'retval' => __( "'true' or 'false'", 'jetpack' ), ), ); $this->tokens = apply_filters( 'wga_tokens', $this->tokens ); } - /** - * This adds the options page for this plugin to the Options page - */ - public function admin_menu() { - add_options_page(__('Google Analytics', 'wp-google-analytics'), __('Google Analytics', 'wp-google-analytics'), 'manage_options', self::$page_slug, array( $this, 'settings_view' ) ); - } - /** * Register our settings */ public function admin_init() { - register_setting( 'wga', 'wga', array( $this, 'sanitize_general_options' ) ); - - add_settings_section( 'wga_general', false, '__return_false', 'wga' ); - add_settings_field( 'code', __( 'Google Analytics tracking ID:', 'wp-google-analytics' ), array( $this, 'field_code' ), 'wga', 'wga_general' ); - add_settings_field( 'additional_items', __( 'Additional items to log:', 'wp-google-analytics' ), array( $this, 'field_additional_items' ), 'wga', 'wga_general' ); - add_settings_field( 'do_not_track', __( 'Visits to ignore:', 'wp-google-analytics' ), array( $this, 'field_do_not_track' ), 'wga', 'wga_general' ); - add_settings_field( 'other_options', __( 'Other options:', 'wp-google-analytics' ), array( $this, 'field_other_options' ), 'wga', 'wga_general' ); - add_settings_field( 'custom_vars', __( 'Custom variables:', 'wp-google-analytics' ), array( $this, 'field_custom_variables' ), 'wga', 'wga_general' ); - } - - /** - * Where the user adds their Google Analytics code - */ - public function field_code() { - // Display the tokens in the right column of the page - echo '
'; - echo '' . __( 'Use tokens in your custom variables to make your fields dynamic based on context. Here are some of the tokens you can use:' ) . ''; - echo ''; - echo ''; - echo ''; - foreach( $this->tokens as $token ) { - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - } - echo ''; - echo '
' . __( 'Token', 'wp-google-analytics' ) . '' . __( 'Description', 'wp-google-analytics' ) . '' . __( 'Return value', 'wp-google-analytics' ) . '
' . esc_html( $token['token'] ) . '' . esc_html( $token['description'] ) . '' . esc_html( $token['retval'] ) . '
'; - echo '
'; - - echo ''; - echo '

' . __( 'Paste your Google Analytics tracking ID (e.g. "UA-XXXXXX-X") into the field.', 'wp-google-analytics' ) . '

'; - } + register_setting( 'jetpack_wga', 'jetpack_wga', array( $this, 'sanitize_general_options' ) ); - /** - * Option to log additional items - */ - public function field_additional_items() { - $addtl_items = array( - 'log_404s' => __( 'Log 404 errors as events', 'wp-google-analytics' ), - 'log_searches' => sprintf( __( 'Log searches as /search/{search}?referrer={referrer} (deprecated)', 'wp-google-analytics' ), 'http://wordpress.org/extend/plugins/wp-google-analytics/faq/' ), - 'log_outgoing' => __( 'Log outgoing links as events', 'wp-google-analytics' ), - ); - foreach( $addtl_items as $id => $label ) { - echo '
'; - } } - /** - * Define custom variables to be included in your tracking code - */ - public function field_custom_variables() { - - $custom_vars = $this->_get_options( 'custom_vars' ); - $scope_options = array( - 0 => __( 'Default', 'wp-google-analytics' ), - 1 => __( 'Visitor', 'wp-google-analytics' ), - 2 => __( 'Session', 'wp-google-analytics' ), - 3 => __( 'Page', 'wp-google-analytics' ), - ); - for ( $i = 1; $i <= 5; $i++ ) { - $name = ( isset( $custom_vars[$i]['name'] ) ) ? $custom_vars[$i]['name'] : ''; - $value = ( isset( $custom_vars[$i]['value'] ) ) ? $custom_vars[$i]['value'] : ''; - $scope = ( isset( $custom_vars[$i]['scope'] ) ) ? $custom_vars[$i]['scope'] : 0; - echo '  '; - echo '  '; - echo '
'; - } - - } - - public function field_do_not_track() { - $do_not_track = array( - 'ignore_admin_area' => __( 'Do not log anything in the admin area', 'wp-google-analytics' ), - ); - global $wp_roles; - foreach( $wp_roles->roles as $role => $role_info ) { - $do_not_track['ignore_role_' . $role] = sprintf( __( 'Do not log %s when logged in', 'wp-google-analytics' ), rtrim( $role_info['name'], 's' ) ); - } - foreach( $do_not_track as $id => $label ) { - echo '
'; - } - } - - /** - * Options that don't belong anywhere else. - */ - public function field_other_options() { - $other_options = array( - 'enable_display_advertising' => sprintf( __( 'Enable Display Advertising', 'wp-google-analytics' ), 'https://support.google.com/analytics/answer/2444872?hl=en&utm_id=ad' ), - ); - - foreach( $other_options as $id => $label ) { - echo '
'; - } - } /** * Sanitize all of the options associated with the plugin @@ -292,165 +158,160 @@ public function sanitize_general_options( $in ) { $out = array(); - // The actual tracking ID - if ( preg_match( '#UA-[\d-]+#', $in['code'], $matches ) ) + // The actual tracking ID. + if ( preg_match( '#UA-[\d-]+#', $in['code'], $matches ) ) { $out['code'] = $matches[0]; - else + } else { $out['code'] = ''; + } $checkbox_items = array( - // Additional items you can track + // Additional items you can track. 'log_404s', 'log_searches', 'log_outgoing', 'enable_display_advertising', - // Things to ignore + // Things to ignore. 'ignore_admin_area', ); global $wp_roles; - foreach( $wp_roles->roles as $role => $role_info ) { + + foreach ( $wp_roles->roles as $role => $role_info ) { $checkbox_items[] = 'ignore_role_' . $role; } - foreach( $checkbox_items as $checkbox_item ) { - if ( isset( $in[$checkbox_item] ) && 'true' == $in[$checkbox_item] ) - $out[$checkbox_item] = 'true'; - else - $out[$checkbox_item] = 'false'; + + foreach ( $checkbox_items as $checkbox_item ) { + if ( isset( $in[ $checkbox_item ] ) && 'true' === $in[ $checkbox_item ] ) { + $out[ $checkbox_item ] = 'true'; + } else { + $out[ $checkbox_item ] = 'false'; + } } - // Custom variables - for( $i = 1; $i <= 5; $i++ ) { - foreach( array( 'name', 'value', 'scope' ) as $key ) { - if ( isset( $in['custom_vars'][$i][$key] ) ) - $out['custom_vars'][$i][$key] = sanitize_text_field( $in['custom_vars'][$i][$key] ); - else - $out['custom_vars'][$i][$key] = ''; + // Custom variables. + for ( $i = 1; $i <= 5; $i++ ) { + foreach ( array( 'name', 'value', 'scope' ) as $key ) { + if ( isset( $in['custom_vars'][ $i ][ $key ] ) ) { + $out['custom_vars'][ $i ][ $key ] = sanitize_text_field( $in['custom_vars'][ $i ][ $key ] ); + } else { + $out['custom_vars'][ $i ][ $key ] = ''; + } } } return $out; } - /** - * This is used to display the options page for this plugin - */ - public function settings_view() { -?> -
-

-
- -
-
-$value) { - if (strpos(strtolower($value), strtolower($site_url)) === 0) { - $track[$k] = substr($track[$k], strlen($site_url)); + private function _get_url( $track ) { + $site_url = ( is_ssl() ? 'https://':'http://' ) . sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ); // Input var okay. + foreach ( $track as $k => $value ) { + if ( strpos( strtolower( $value ), strtolower( $site_url ) ) === 0 ) { + $track[ $k ] = substr( $track[ $k ], strlen( $site_url ) ); } - if ($k == 'data') { - $track[$k] = preg_replace("/^https?:\/\/|^\/+/i", "", $track[$k]); + if ( 'data' === $k ) { + $track[ $k ] = preg_replace( '/^https?:\/\/|^\/+/i', '', $track[ $k ] ); } - //This way we don't lose search data. - if ($k == 'data' && $track['code'] == 'search') { - $track[$k] = urlencode($track[$k]); + // This way we don't lose search data. + if ( 'data' === $k && 'search' === $track['code'] ) { + $track[ $k ] = rawurlencode( $track[ $k ] ); } else { - $track[$k] = preg_replace("/[^a-z0-9\.\/\+\?=-]+/i", "_", $track[$k]); + $track[ $k ] = preg_replace( '/[^a-z0-9\.\/\+\?=-]+/i', '_', $track[ $k ] ); } - $track[$k] = trim($track[$k], '_'); + $track[ $k ] = trim( $track[ $k ], '_' ); } - $char = (strpos($track['data'], '?') === false)? '?':'&'; - return str_replace("'", "\'", "/{$track['code']}/{$track['data']}{$char}referer=" . urlencode( isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '' ) ); + $char = ( strpos( $track['data'], '?' ) === false ) ? '?' : '&'; + return str_replace( "'", "\'", "/{$track['code']}/{$track['data']}{$char}referer=" . rawurlencode( isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '' ) ); // Input var okay. } /** * Maybe output or return, depending on the context */ private function _output_or_return( $val, $maybe ) { - if ( $maybe ) + if ( $maybe ) { echo $val . "\r\n"; - else + } else { return $val; + } } /** * This injects the Google Analytics code into the footer of the page. * - * @param bool[optional] $output - defaults to true, false returns but does NOT echo the code + * @param bool[optional] $output - defaults to true, false returns but does NOT echo the code. */ public function insert_code( $output = true ) { - //If $output is not a boolean false, set it to true (default) - $output = ($output !== false); + // If $output is not a boolean false, set it to true (default). + $output = ( false !== $output); $tracking_id = $this->_get_options( 'code' ); - if ( empty( $tracking_id ) ) + if ( empty( $tracking_id ) ) { return $this->_output_or_return( '', $output ); + } - //get our plugin options + // get our plugin options. $wga = $this->_get_options(); - //If the user's role has wga_no_track set to true, return without inserting code + // If the user's role has wga_no_track set to true, return without inserting code. if ( is_user_logged_in() ) { $current_user = wp_get_current_user(); $role = array_shift( $current_user->roles ); - if ( 'true' == $this->_get_options( 'ignore_role_' . $role ) ) - return $this->_output_or_return( "", $output ); + if ( 'true' === $this->_get_options( 'ignore_role_' . $role ) ) { + return $this->_output_or_return( '', $output ); + } } - //If $admin is true (we're in the admin_area), and we've been told to ignore_admin_area, return without inserting code - if (is_admin() && (!isset($wga['ignore_admin_area']) || $wga['ignore_admin_area'] != 'false')) - return $this->_output_or_return( "", $output ); + // If $admin is true (we're in the admin_area), and we've been told to ignore_admin_area, return without inserting code. + if ( is_admin() && ( ! isset( $wga['ignore_admin_area'] ) || 'false' !== $wga['ignore_admin_area'] ) ) { + return $this->_output_or_return( '', $output ); + } $custom_vars = array( "_gaq.push(['_setAccount', '{$tracking_id}']);", ); - // Add custom variables specified by the user - foreach( $this->_get_options( 'custom_vars', array() ) as $i => $custom_var ) { - if ( empty( $custom_var['name'] ) || empty( $custom_var['value'] ) ) + // Add custom variables specified by the user. + foreach ( $this->_get_options( 'custom_vars', array() ) as $i => $custom_var ) { + if ( empty( $custom_var['name'] ) || empty( $custom_var['value'] ) ) { continue; + } - // Check whether a token was used with this custom var, and replace with value if so + // Check whether a token was used with this custom var, and replace with value if so. $all_tokens = wp_list_pluck( $this->tokens, 'token' ); - if ( in_array( $custom_var['value'], $all_tokens ) ) { + if ( in_array( $custom_var['value'], $all_tokens, true ) ) { $token = array_pop( wp_filter_object_list( $this->tokens, array( 'token' => $custom_var['value'] ) ) ); - // Allow tokens to return empty values for specific contexts + // Allow tokens to return empty values for specific contexts. $ignore = false; if ( ! empty( $token['ignore_when'] ) ) { - foreach( (array)$token['ignore_when'] as $conditional ) { + foreach ( (array) $token['ignore_when'] as $conditional ) { if ( is_callable( $conditional ) ) { $ignore = call_user_func( $conditional ); - if ( $ignore ) + if ( $ignore ) { break; + } } } } - // If we aren't set to ignore this context, possibly execute the callback - if ( ! $ignore && ! empty( $token['callback'] ) && is_callable( $token['callback'] ) ) + // If we aren't set to ignore this context, possibly execute the callback. + if ( ! $ignore && ! empty( $token['callback'] ) && is_callable( $token['callback'] ) ) { $replace = call_user_func( $token['callback'] ); - else + } else { $replace = ''; + } - if ( ! empty( $token['callback_returns'] ) && 'bool' == $token['callback_returns'] ) + if ( ! empty( $token['callback_returns'] ) && 'bool' === $token['callback_returns'] ) { $replace = ( $replace ) ? 'true' : 'false'; + } - // Replace our token with the value + // Replace our token with the value. $custom_var['value'] = str_replace( $custom_var['value'], $replace, $custom_var['value'] ); } @@ -460,31 +321,32 @@ public function insert_code( $output = true ) { "'" . esc_js( $custom_var['name'] ) . "'", "'" . esc_js( $custom_var['value'] ) . "'", ); - if ( $custom_var['scope'] ) + if ( $custom_var['scope'] ) { $atts[] = intval( $custom_var['scope'] ); - $custom_vars[] = "_gaq.push([" . implode( ', ', $atts ) . "]);"; + } + $custom_vars[] = '_gaq.push([' . implode( ', ', $atts ) . ']);'; } $track = array(); - if (is_404() && (!isset($wga['log_404s']) || $wga['log_404s'] != 'false')) { - // This is a 404 and we are supposed to track them + if ( is_404() && ( ! isset( $wga['log_404s'] ) || 'false' !== $wga['log_404s'] ) ) { + // This is a 404 and we are supposed to track them. $custom_vars[] = "_gaq.push( [ '_trackEvent', '404', document.location.href, document.referrer ] );"; - } elseif (is_search() && (!isset($wga['log_searches']) || $wga['log_searches'] != 'false')) { - //Set track for searches, if it's a search, and we are supposed to - $track['data'] = $_REQUEST['s']; - $track['code'] = "search"; + } elseif ( is_search() && ( ! isset( $wga['log_searches'] ) || 'false' !== $wga['log_searches'] ) ) { + // Set track for searches, if it's a search, and we are supposed to. + $track['data'] = sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ); // Input var okay. + $track['code'] = 'search'; } if ( ! empty( $track ) ) { $track['url'] = $this->_get_url( $track ); - //adjust the code that we output, account for both types of tracking + // adjust the code that we output, account for both types of tracking. $track['url'] = esc_js( str_replace( '&', '&', $track['url'] ) ); $custom_vars[] = "_gaq.push(['_trackPageview','{$track['url']}']);"; } else { $custom_vars[] = "_gaq.push(['_trackPageview']);"; } - if ($wga['enable_display_advertising'] == 'true' ) { + if ( 'true' === $wga['enable_display_advertising'] ) { $async_code = ""; - } - + $custom_vars_string = implode( "\r\n", $custom_vars ); $async_code = str_replace( '%custom_vars%', $custom_vars_string, $async_code ); @@ -519,31 +380,34 @@ public function insert_code( $output = true ) { /** * Used to get one or all of our plugin options * - * @param string[optional] $option - Name of options you want. Do not use if you want ALL options - * @return array of options, or option value + * @param string[optional] $option - Name of options you want. Do not use if you want ALL options. + * @param boolean[optiona] $default - Default value. + * @return array of options, or option value. */ private function _get_options( $option = null, $default = false ) { - $o = get_option('wga'); + $o = get_option( 'jetpack_wga' ); - if (isset($option)) { - - if (isset($o[$option])) { - if ( 'code' == $option ) { - if ( preg_match( '#UA-[\d-]+#', $o[$option], $matches ) ) + if ( isset( $option ) ) { + if ( isset( $o[ $option ] ) ) { + if ( 'code' === $option ) { + if ( preg_match( '#UA-[\d-]+#', $o[ $option ], $matches ) ) { return $matches[0]; - else + } else { return ''; - } else - return $o[$option]; + } + } else { + return $o[ $option ]; + } } else { - if ( 'ignore_role_' == substr( $option, 0, 12 ) ) { + if ( 'ignore_role_' === substr( $option, 0, 12 ) ) { global $wp_roles; - // Backwards compat for when the tracking information was stored as a cap + // Backwards compat for when the tracking information was stored as a cap. $maybe_role = str_replace( 'ignore_role_', '', $option ); - if ( isset( $wp_roles->roles[$maybe_role] ) ) { - if ( isset( $wp_roles->roles[$maybe_role]['capabilities']['wga_no_track'] ) && $wp_roles->roles[$maybe_role]['capabilities']['wga_no_track'] ) + if ( isset( $wp_roles->roles[ $maybe_role ] ) ) { + if ( isset( $wp_roles->roles[ $maybe_role ]['capabilities']['wga_no_track'] ) && $wp_roles->roles[ $maybe_role ]['capabilities']['wga_no_track'] ) { return 'true'; + } } return false; } @@ -558,15 +422,16 @@ private function _get_options( $option = null, $default = false ) { * If we track outgoing links, this will enqueue our javascript file */ public function track_outgoing() { - if ( 'true' == $this->_get_options( 'log_outgoing' ) && (!defined('XMLRPC_REQUEST') || !XMLRPC_REQUEST) && ( ! is_admin() || 'false' == $this->_get_options( 'ignore_admin_area' ) ) ) + if ( 'true' === $this->_get_options( 'log_outgoing' ) && ( ! defined( 'XMLRPC_REQUEST' ) || ! XMLRPC_REQUEST ) && ( ! is_admin() || 'false' === $this->_get_options( 'ignore_admin_area' ) ) ) { wp_enqueue_script( 'wp-google-analytics', plugin_dir_url( __FILE__ ) . 'wp-google-analytics.js', array( 'jquery' ), '0.0.3' ); + } } /** * Callback for %the_category% token */ public function token_the_category() { - return implode( ', ', wp_list_pluck( (array)get_the_category(), 'name' ) ); + return implode( ', ', wp_list_pluck( (array) get_the_category(), 'name' ) ); } /** @@ -575,21 +440,21 @@ public function token_the_category() { public function token_context() { if ( is_admin() ) { return 'admin'; - } else if ( is_home() || is_front_page() ) { + } elseif ( is_home() || is_front_page() ) { return 'home'; - } else if ( is_tax() || is_tag() || is_category() ) { + } elseif ( is_tax() || is_tag() || is_category() ) { return get_queried_object()->taxonomy; - } else if ( is_author() ) { + } elseif ( is_author() ) { return 'author'; - } else if ( is_singular() || is_single() || is_page() ) { + } elseif ( is_singular() || is_single() || is_page() ) { return get_post_type(); - } else if ( is_search() ) { + } elseif ( is_search() ) { return 'search'; - } else if ( is_date() ) { + } elseif ( is_date() ) { return 'date'; - } else if ( is_archive() ) { + } elseif ( is_archive() ) { return 'archive'; - } else if ( is_404() ) { + } elseif ( is_404() ) { return '404'; } } @@ -598,18 +463,9 @@ public function token_context() { * Callback for %the_tags% token */ public function token_the_tags() { - return implode( ', ', wp_list_pluck( (array)get_the_tags(), 'name' ) ); + return implode( ', ', wp_list_pluck( (array) get_the_tags(), 'name' ) ); } - - public function add_plugin_page_links( $links, $file ){ - if ( plugin_basename( __FILE__ ) == $file ) { - $link = '' . __( 'Settings', 'wp-google-analytics' ) . ''; - array_unshift( $links, $link ); - } - return $links; - } - } global $wp_google_analytics; -$wp_google_analytics = wpGoogleAnalytics::get_instance(); +$wp_google_analytics = JetpackGoogleAnalytics::get_instance(); diff --git a/sync/class.jetpack-sync-defaults.php b/sync/class.jetpack-sync-defaults.php index 748bcc65bb009..e56d0e294ea13 100644 --- a/sync/class.jetpack-sync-defaults.php +++ b/sync/class.jetpack-sync-defaults.php @@ -67,7 +67,7 @@ class Jetpack_Sync_Defaults { 'comment_whitelist', 'comment_max_links', 'moderation_keys', - 'wga', + 'jetpack_wga', 'disabled_likes', 'disabled_reblogs', 'jetpack_comment_likes_enabled', @@ -175,22 +175,22 @@ class Jetpack_Sync_Defaults { static $default_post_checksum_columns = array( 'ID', 'post_modified', - ); + ); static $default_post_meta_checksum_columns = array( 'meta_id', 'meta_value' - ); + ); static $default_comment_checksum_columns = array( 'comment_ID', 'comment_content', - ); + ); static $default_comment_meta_checksum_columns = array( 'meta_id', 'meta_value' - ); + ); static $default_option_checksum_columns = array( 'option_name', From 9c3161258a3fd46355adf8c7291a56dd21c7e165 Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Fri, 9 Dec 2016 02:56:03 -0300 Subject: [PATCH 08/43] Adds Jetpack support link --- modules/module-info.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/module-info.php b/modules/module-info.php index 70d24e053e8ea..7bc250d483271 100644 --- a/modules/module-info.php +++ b/modules/module-info.php @@ -646,7 +646,7 @@ function jetpack_wordads_more_info() { * Google Analytics */ function jetpack_google_analytics_more_link() { - echo 'https://support.wordpress.com/google-analytics/'; + echo 'https://jetpack.com/support/'; } add_action( 'jetpack_learn_more_button_google-analytics', 'jetpack_google_analytics_more_link' ); From 61dc4d42bc20fadd3986dda732da9ea6a10f1e9d Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Fri, 9 Dec 2016 03:21:12 -0300 Subject: [PATCH 09/43] Fixes broken tests --- modules/google-analytics/wp-google-analytics.php | 2 -- tests/php/sync/test_class.jetpack-sync-options.php | 6 +++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/modules/google-analytics/wp-google-analytics.php b/modules/google-analytics/wp-google-analytics.php index beeb3cb3e118d..b2eda15056ab5 100644 --- a/modules/google-analytics/wp-google-analytics.php +++ b/modules/google-analytics/wp-google-analytics.php @@ -149,8 +149,6 @@ public function admin_init() { } - - /** * Sanitize all of the options associated with the plugin */ diff --git a/tests/php/sync/test_class.jetpack-sync-options.php b/tests/php/sync/test_class.jetpack-sync-options.php index f3165fcc83b05..13eda3c514106 100644 --- a/tests/php/sync/test_class.jetpack-sync-options.php +++ b/tests/php/sync/test_class.jetpack-sync-options.php @@ -127,7 +127,7 @@ public function test_sync_default_options() { 'comment_whitelist' => 'pineapple', 'comment_max_links' => 99, 'moderation_keys' => 'pineapple', - 'wga' => 'pineapple', + 'jetpack_wga' => 'pineapple', 'disabled_likes' => 'pineapple', 'disabled_reblogs' => 'pineapple', 'jetpack_comment_likes_enabled' => 'pineapple', @@ -201,7 +201,7 @@ public function test_add_whitelisted_option_on_init_89() { do_action( 'init' ); $whitelist = $this->options_module->get_options_whitelist(); - + $this->assertTrue( in_array( 'foo_option_bar', $whitelist ) ); } @@ -215,7 +215,7 @@ public function add_jetpack_options_whitelist_filter( $options ) { } - + function add_option_on_89() { add_filter( 'jetpack_options_whitelist', array( $this, 'add_jetpack_options_whitelist_filter' ) ); } From 339d05759168574df324a323e3bcdde67e2cbb21 Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Fri, 9 Dec 2016 12:56:40 -0300 Subject: [PATCH 10/43] Fixes the Business plan checks and removes unnecessary code --- _inc/client/engagement/index.jsx | 5 ++ modules/google-analytics.php | 2 +- .../google-analytics/wp-google-analytics.php | 61 ------------------- 3 files changed, 6 insertions(+), 62 deletions(-) diff --git a/_inc/client/engagement/index.jsx b/_inc/client/engagement/index.jsx index 5059d6a33e24c..c6fda4908a2b8 100644 --- a/_inc/client/engagement/index.jsx +++ b/_inc/client/engagement/index.jsx @@ -128,6 +128,7 @@ export const Engagement = ( props ) => { toggle = __( 'Unavailable in Dev Mode' ); } else if ( isAdmin ) { if ( ( 'seo-tools' === element[0] && ! hasBusiness ) || + ( 'google-analytics' === element[0] && ! hasBusiness ) || ( 'wordads' === element[0] && ! hasPremiumOrBusiness ) ) { toggle = ; } else { @@ -144,6 +145,10 @@ export const Engagement = ( props ) => { } } + if ( element[0] === 'google-analytics' && ! hasBusiness ) { + isModuleActive = false; + } + if ( isPro ) { // Add a "pro" button next to the header title element[1] = diff --git a/modules/google-analytics.php b/modules/google-analytics.php index 5704193d6fe7d..6a4264ed32ebc 100644 --- a/modules/google-analytics.php +++ b/modules/google-analytics.php @@ -3,7 +3,7 @@ /** * Module Name: Google Analytics * Module Description: Lets you use Google Analytics to track your WordPress site statistics. - * First Introduced: 4.4 + * First Introduced: 4.5 * Sort Order: 37 * Requires Connection: Yes * Auto Activate: No diff --git a/modules/google-analytics/wp-google-analytics.php b/modules/google-analytics/wp-google-analytics.php index b2eda15056ab5..7b0206a4f3a3d 100644 --- a/modules/google-analytics/wp-google-analytics.php +++ b/modules/google-analytics/wp-google-analytics.php @@ -40,7 +40,6 @@ class JetpackGoogleAnalytics { */ private function __construct() { add_filter( 'init', array( $this, 'init' ) ); - add_action( 'admin_init', array( $this, 'admin_init' ) ); add_action( 'get_footer', array( $this, 'insert_code' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'track_outgoing' ) ); } @@ -140,66 +139,6 @@ public function init() { $this->tokens = apply_filters( 'wga_tokens', $this->tokens ); } - /** - * Register our settings - */ - public function admin_init() { - - register_setting( 'jetpack_wga', 'jetpack_wga', array( $this, 'sanitize_general_options' ) ); - - } - - /** - * Sanitize all of the options associated with the plugin - */ - public function sanitize_general_options( $in ) { - - $out = array(); - - // The actual tracking ID. - if ( preg_match( '#UA-[\d-]+#', $in['code'], $matches ) ) { - $out['code'] = $matches[0]; - } else { - $out['code'] = ''; - } - - $checkbox_items = array( - // Additional items you can track. - 'log_404s', - 'log_searches', - 'log_outgoing', - 'enable_display_advertising', - // Things to ignore. - 'ignore_admin_area', - ); - global $wp_roles; - - foreach ( $wp_roles->roles as $role => $role_info ) { - $checkbox_items[] = 'ignore_role_' . $role; - } - - foreach ( $checkbox_items as $checkbox_item ) { - if ( isset( $in[ $checkbox_item ] ) && 'true' === $in[ $checkbox_item ] ) { - $out[ $checkbox_item ] = 'true'; - } else { - $out[ $checkbox_item ] = 'false'; - } - } - - // Custom variables. - for ( $i = 1; $i <= 5; $i++ ) { - foreach ( array( 'name', 'value', 'scope' ) as $key ) { - if ( isset( $in['custom_vars'][ $i ][ $key ] ) ) { - $out['custom_vars'][ $i ][ $key ] = sanitize_text_field( $in['custom_vars'][ $i ][ $key ] ); - } else { - $out['custom_vars'][ $i ][ $key ] = ''; - } - } - } - - return $out; - } - /** * Used to generate a tracking URL * From d3c7c05cae0595ed3ede1fd032d0dde070a9ddcb Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Fri, 9 Dec 2016 13:04:02 -0300 Subject: [PATCH 11/43] Adds Google Analytics JS to the js:hint task exception list --- gulpfile.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gulpfile.js b/gulpfile.js index 884722d467bdc..c7b50097d14b7 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -448,7 +448,8 @@ gulp.task( 'js:hint', function() { 'modules/**/*.js', '!_inc/*.min.js', '!modules/*.min.', - '!modules/**/*.min.js' + '!modules/**/*.min.js', + '!modules/google-analytics/*.js' ] ) .pipe( jshint( '.jshintrc' ) ) .pipe( jshint.reporter( 'jshint-stylish' ) ) From ba504eeec3359f5c75f56f1a41f90d5537e187d2 Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Fri, 9 Dec 2016 13:06:34 -0300 Subject: [PATCH 12/43] Pointing help back to Google Analytics article on WordPress.com --- modules/module-info.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/module-info.php b/modules/module-info.php index 7bc250d483271..70d24e053e8ea 100644 --- a/modules/module-info.php +++ b/modules/module-info.php @@ -646,7 +646,7 @@ function jetpack_wordads_more_info() { * Google Analytics */ function jetpack_google_analytics_more_link() { - echo 'https://jetpack.com/support/'; + echo 'https://support.wordpress.com/google-analytics/'; } add_action( 'jetpack_learn_more_button_google-analytics', 'jetpack_google_analytics_more_link' ); From 7a59438a321e4790a28156f41128df43098946a5 Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Fri, 9 Dec 2016 13:37:52 -0300 Subject: [PATCH 13/43] Maintains compatibility with WordPress.com option name. --- .../class.wpcom-json-api-site-settings-endpoint.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/json-endpoints/class.wpcom-json-api-site-settings-endpoint.php b/json-endpoints/class.wpcom-json-api-site-settings-endpoint.php index 552893ead62a1..2cb56728606aa 100644 --- a/json-endpoints/class.wpcom-json-api-site-settings-endpoint.php +++ b/json-endpoints/class.wpcom-json-api-site-settings-endpoint.php @@ -356,9 +356,14 @@ public function update_settings() { if ( ! isset( $value['code'] ) || ! preg_match( '/^$|^UA-[\d-]+$/i', $value['code'] ) ) { return new WP_Error( 'invalid_code', 'Invalid UA ID' ); } - $wga = get_option( 'jetpack_wga', array() ); + + $is_wpcom = ! $is_jetpack && defined( 'IS_WPCOM' ) && IS_WPCOM; + $option_name = $is_wpcom ? 'wga' : 'jetpack_wga'; + + $wga = get_option( $option_name, array() ); $wga['code'] = $value['code']; // maintain compatibility with wp-google-analytics - if ( update_option( 'jetpack_wga', $wga ) ) { + + if ( update_option( $option_name, $wga ) ) { $updated[ $key ] = $value; } @@ -367,12 +372,11 @@ public function update_settings() { /** This action is documented in modules/widgets/social-media-icons.php */ do_action( 'jetpack_bump_stats_extras', 'google-analytics', $enabled_or_disabled ); - if ( ! $is_jetpack && defined( 'IS_WPCOM' ) && IS_WPCOM ) { + if ( $is_wpcom ) { $business_plugins = WPCOM_Business_Plugins::instance(); $business_plugins->activate_plugin( 'wp-google-analytics' ); } break; - case 'jetpack_testimonial': case 'jetpack_portfolio': case 'jetpack_comment_likes_enabled': From 00a5646c7147c8d79ef10a3b253f0fb8623a260b Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Wed, 14 Dec 2016 11:00:08 -0300 Subject: [PATCH 14/43] Translates the jetpack option name to the WordPress.com name when getting site settings. --- .../class.wpcom-json-api-site-settings-endpoint.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/json-endpoints/class.wpcom-json-api-site-settings-endpoint.php b/json-endpoints/class.wpcom-json-api-site-settings-endpoint.php index 2cb56728606aa..50547b796e4c6 100644 --- a/json-endpoints/class.wpcom-json-api-site-settings-endpoint.php +++ b/json-endpoints/class.wpcom-json-api-site-settings-endpoint.php @@ -199,7 +199,7 @@ public function get_settings_response() { 'moderation_keys' => get_option( 'moderation_keys' ), 'blacklist_keys' => get_option( 'blacklist_keys' ), 'lang_id' => get_option( 'lang_id' ), - 'wga' => get_option( 'wga' ), + 'wga' => $this->get_google_analytics(), 'disabled_likes' => (bool) get_option( 'disabled_likes' ), 'disabled_reblogs' => (bool) get_option( 'disabled_reblogs' ), 'jetpack_comment_likes_enabled' => (bool) get_option( 'jetpack_comment_likes_enabled', false ), @@ -266,6 +266,10 @@ protected function get_locale( $key ) { return false; } + protected function get_google_analytics () { + $option_name = defined( 'IS_WPCOM' ) && IS_WPCOM ? 'wga' : 'jetpack_wga'; + return get_option( $option_name ); + } /** * Updates site settings for authorized users From 29faa7b0d9253d530495e649f2c2a17d722ff857 Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Wed, 14 Dec 2016 11:37:55 -0300 Subject: [PATCH 15/43] Makes form placeholder translatable --- _inc/client/components/module-settings/index.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_inc/client/components/module-settings/index.jsx b/_inc/client/components/module-settings/index.jsx index b41234550e33e..01a72eadd329c 100644 --- a/_inc/client/components/module-settings/index.jsx +++ b/_inc/client/components/module-settings/index.jsx @@ -877,7 +877,7 @@ export let GoogleAnalyticsSettings = React.createClass( { From 67c5f6e34a8ba8801520e106086407895005545a Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Wed, 14 Dec 2016 11:50:02 -0300 Subject: [PATCH 16/43] Reverts snafu with translatable string on the Verification Tools component. --- _inc/client/components/module-settings/index.jsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/_inc/client/components/module-settings/index.jsx b/_inc/client/components/module-settings/index.jsx index 01a72eadd329c..8393ff992dcba 100644 --- a/_inc/client/components/module-settings/index.jsx +++ b/_inc/client/components/module-settings/index.jsx @@ -473,7 +473,7 @@ export let VerificationToolsSettings = React.createClass( {
- { __( 'Google' ) } + Google - { __( 'Bing' ) } + Bing - { __( 'Pinterest' ) } + Pinterest - { __( 'Yandex' ) } + Yandex Date: Wed, 14 Dec 2016 11:54:36 -0300 Subject: [PATCH 17/43] Updates upgrade button with new Jetpack redirect address --- _inc/client/pro-status/index.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_inc/client/pro-status/index.jsx b/_inc/client/pro-status/index.jsx index 7c193b2c86607..8f924ba1f2475 100644 --- a/_inc/client/pro-status/index.jsx +++ b/_inc/client/pro-status/index.jsx @@ -126,7 +126,7 @@ const ProStatus = React.createClass( { From 857ade36652daf3e237c80b7be99a3d49277228b Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Wed, 14 Dec 2016 11:55:48 -0300 Subject: [PATCH 18/43] Removes unused constant. --- modules/google-analytics/wp-google-analytics.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/google-analytics/wp-google-analytics.php b/modules/google-analytics/wp-google-analytics.php index 7b0206a4f3a3d..43ce6e3876fe7 100644 --- a/modules/google-analytics/wp-google-analytics.php +++ b/modules/google-analytics/wp-google-analytics.php @@ -1,6 +1,4 @@ Date: Thu, 15 Dec 2016 14:52:12 -0300 Subject: [PATCH 19/43] Adds Google Analytics to the PRO list on the search page. --- _inc/client/search/index.jsx | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/_inc/client/search/index.jsx b/_inc/client/search/index.jsx index ba6d14486b6f0..2a237a5a1d4ec 100644 --- a/_inc/client/search/index.jsx +++ b/_inc/client/search/index.jsx @@ -5,7 +5,7 @@ import React from 'react'; import { connect } from 'react-redux'; import FoldableCard from 'components/foldable-card'; import { ModuleToggle } from 'components/module-toggle'; -import forEach from 'lodash/forEach'; +import { includes, forEach } from 'lodash'; import Button from 'components/button'; import Gridicon from 'components/gridicon'; import Collection from 'components/search/search-collection.jsx'; @@ -105,11 +105,8 @@ export const SearchResults = ( { } cards = moduleList.map( ( element ) => { - let isPro = 'scan' === element[0] - || 'akismet' === element[0] - || 'backups' === element[0] - || 'seo-tools' === element[0], - proProps = {}, + const isPro = includes( [ 'scan', 'akismet', 'backups', 'seo-tools', 'google-analytics' ], element[0] ); + let proProps = {}, unavailableDevMode = unavailableInDevMode( element[0] ), toggle = unavailableDevMode ? __( 'Unavailable in Dev Mode' ) : ( Date: Thu, 15 Dec 2016 14:52:53 -0300 Subject: [PATCH 20/43] Removes the paywall from the module, adding it to the deactivation list. --- _inc/lib/admin-pages/class.jetpack-admin-page.php | 6 +++--- modules/google-analytics.php | 4 +--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/_inc/lib/admin-pages/class.jetpack-admin-page.php b/_inc/lib/admin-pages/class.jetpack-admin-page.php index f37c5bd8efba6..870b53ac9c552 100644 --- a/_inc/lib/admin-pages/class.jetpack-admin-page.php +++ b/_inc/lib/admin-pages/class.jetpack-admin-page.php @@ -216,15 +216,15 @@ function check_plan_deactivate_modules( $page ) { $active = Jetpack::get_active_modules(); switch ( $current->plan->product_slug ) { case 'jetpack_free': - $to_deactivate = array( 'seo-tools', 'videopress' ); + $to_deactivate = array( 'seo-tools', 'videopress', 'google-analytics' ); break; case 'jetpack_personal': case 'jetpack_personal_monthly': - $to_deactivate = array( 'seo-tools', 'videopress' ); + $to_deactivate = array( 'seo-tools', 'videopress', 'google-analytics' ); break; case 'jetpack_premium': case 'jetpack_premium_monthly': - $to_deactivate = array( 'seo-tools' ); + $to_deactivate = array( 'seo-tools', 'google-analytics' ); break; } $to_deactivate = array_intersect( $active, $to_deactivate ); diff --git a/modules/google-analytics.php b/modules/google-analytics.php index 6a4264ed32ebc..fe22e50268900 100644 --- a/modules/google-analytics.php +++ b/modules/google-analytics.php @@ -11,6 +11,4 @@ * Additional Search Queries: webmaster, google, analytics, console */ -if ( Jetpack::active_plan_supports( 'google-analytics' ) ) { - include dirname( __FILE__ ) . "/google-analytics/wp-google-analytics.php"; -} +include dirname( __FILE__ ) . "/google-analytics/wp-google-analytics.php"; From a83338ace2def19dc068404b8ce4d27641e960e4 Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Mon, 9 Jan 2017 02:13:32 -0300 Subject: [PATCH 21/43] Adds Google Analytics to the Plans page --- _inc/client/plans/plan-body.jsx | 34 +++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/_inc/client/plans/plan-body.jsx b/_inc/client/plans/plan-body.jsx index ba658d2fc95fd..70a35563e34ce 100644 --- a/_inc/client/plans/plan-body.jsx +++ b/_inc/client/plans/plan-body.jsx @@ -28,10 +28,10 @@ const PlanBody = React.createClass( { render() { let planCard = ''; switch ( this.props.plan ) { - case 'jetpack_personal': - case 'jetpack_personal_monthly': - case 'jetpack_premium': - case 'jetpack_premium_monthly': + case 'jetpack_personal': + case 'jetpack_personal_monthly': + case 'jetpack_premium': + case 'jetpack_premium_monthly': case 'jetpack_business': case 'jetpack_business_monthly': planCard = ( @@ -226,6 +226,32 @@ const PlanBody = React.createClass( { : '' } + { + includes( [ 'jetpack_business', 'jetpack_business_monthly' ], this.props.plan ) ? +
+

{ __( 'Google Analytics' ) }

+

{ __( 'Track website statistics with Google Analytics for a deeper understanding of your website visitors and customers.' ) }

+ { + this.props.isFetchingPluginsData ? '' : + this.props.isModuleActivated( 'google-analytics' ) ? ( + + ) + : ( + + ) + } +
+ : '' + } + { includes( [ 'jetpack_personal', 'jetpack_personal_monthly' ], this.props.plan ) ?
From 00118462e9666f1d37738d3e5f2f0b9dde5fc9c4 Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Mon, 9 Jan 2017 02:18:23 -0300 Subject: [PATCH 22/43] Updates the Google Analytics module heading --- modules/module-headings.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/module-headings.php b/modules/module-headings.php index 0d4a0edde677c..88b5575215ca4 100644 --- a/modules/module-headings.php +++ b/modules/module-headings.php @@ -51,7 +51,7 @@ function jetpack_get_module_i18n( $key ) { 'google-analytics' => array( 'name' => _x( 'Google Analytics', 'Module Name', 'jetpack' ), - 'description' => _x( 'Collect traffic stats and insights with Google Analytics.', 'Module Description', 'jetpack' ), + 'description' => _x( 'Lets you use Google Analytics to track your WordPress site statistics.', 'Module Description', 'jetpack' ), ), 'gravatar-hovercards' => array( @@ -319,4 +319,4 @@ function jetpack_get_module_i18n_tag( $key ) { ); } return $module_tags[ $key ]; -} \ No newline at end of file +} From fc3597a1eab3a7154b772e4eb8590500d88f2bb8 Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Mon, 9 Jan 2017 02:20:04 -0300 Subject: [PATCH 23/43] Fixes the Google Analytics module appearance when searching --- _inc/client/search/index.jsx | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/_inc/client/search/index.jsx b/_inc/client/search/index.jsx index 2a237a5a1d4ec..eecd355f27b88 100644 --- a/_inc/client/search/index.jsx +++ b/_inc/client/search/index.jsx @@ -107,11 +107,12 @@ export const SearchResults = ( { cards = moduleList.map( ( element ) => { const isPro = includes( [ 'scan', 'akismet', 'backups', 'seo-tools', 'google-analytics' ], element[0] ); let proProps = {}, + isModuleActive = isModuleActivated( element[0] ), unavailableDevMode = unavailableInDevMode( element[0] ), toggle = unavailableDevMode ? __( 'Unavailable in Dev Mode' ) : ( @@ -129,13 +130,17 @@ export const SearchResults = ( { configure_url: '' }; - if ( + if ( ( 'videopress' !== element[0] || 'seo-tools' !== element[0] || ( 'seo-tools' === element[0] && ! hasBusiness + ) ) + && ( + 'google-analytics' !== element[0] + || ( 'google-analytics' === element[0] && ! hasBusiness ) ) ) { toggle = ; @@ -196,7 +201,7 @@ export const SearchResults = ( { ) } > { - isModuleActivated( element[0] ) || isPro ? + isModuleActive || isPro ? : // Render the long_description if module is deactivated
From 4c9ad806fac3f874ef2d7002a598dd9fc6673871 Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Mon, 9 Jan 2017 11:44:44 -0300 Subject: [PATCH 24/43] Removes tokens for custom variables since they are not supported on WordPress.com --- .../google-analytics/wp-google-analytics.php | 136 ------------------ 1 file changed, 136 deletions(-) diff --git a/modules/google-analytics/wp-google-analytics.php b/modules/google-analytics/wp-google-analytics.php index 43ce6e3876fe7..fcc6a3b0ab1ff 100644 --- a/modules/google-analytics/wp-google-analytics.php +++ b/modules/google-analytics/wp-google-analytics.php @@ -37,7 +37,6 @@ class JetpackGoogleAnalytics { * @return void */ private function __construct() { - add_filter( 'init', array( $this, 'init' ) ); add_action( 'get_footer', array( $this, 'insert_code' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'track_outgoing' ) ); } @@ -53,90 +52,6 @@ public static function get_instance() { return self::$instance; } - public function init() { - $this->tokens = array( - array( - 'token' => '%the_author%', - 'callback' => 'get_the_author', - 'callback_returns' => 'string', - 'description' => __( 'Post author for current view', 'jetpack' ), - 'retval' => __( "Post author's display name", 'jetpack' ), - 'ignore_when' => array( - 'is_home', - 'is_front_page', - 'is_post_type_archive', - 'is_page', - 'is_date', - 'is_category', - 'is_tag', - ), - ), - array( - 'token' => '%the_category%', - 'callback' => array( $this, 'token_the_category' ), - 'callback_returns' => 'string', - 'description' => __( 'Categories assigned to a post', 'jetpack' ), - 'retval' => __( 'Category names in a commma-separated list', 'jetpack' ), - 'ignore_when' => array( - 'is_home', - 'is_front_page', - 'is_page', - 'is_post_type_archive', - 'is_author', - 'is_tag', - ), - ), - array( - 'token' => '%context%', - 'callback' => array( $this, 'token_context' ), - 'callback_returns' => 'string', - 'description' => __( 'Which view the visitor is on', 'jetpack' ), - 'retval' => __( "Samples: 'home', 'category', 'post', 'author'" ), - ), - array( - 'token' => '%the_date%', - 'callback' => 'get_the_date', - 'callback_returns' => 'string', - 'description' => __( 'Publication date for the current view', 'jetpack' ), - 'retval' => __( "Format specified by 'Date Format' in Settings -> General", 'jetpack' ), - 'ignore_when' => array( - 'is_home', - 'is_front_page', - 'is_post_type_archive', - 'is_page', - 'is_author', - 'is_category', - 'is_tag', - ), - ), - array( - 'token' => '%the_tags%', - 'callback' => array( $this, 'token_the_tags' ), - 'callback_returns' => 'string', - 'description' => __( 'Tags assigned to a post', 'jetpack' ), - 'retval' => __( 'Tag names in a commma-separated list', 'jetpack' ), - 'ignore_when' => array( - 'is_home', - 'is_front_page', - 'is_page', - 'is_post_type_archive', - 'is_date', - 'is_category', - 'is_author', - ), - ), - array( - 'token' => '%is_user_logged_in%', - 'callback' => 'is_user_logged_in', - 'callback_returns' => 'bool', - 'description' => __( 'Whether or not the viewer is logged in', 'jetpack' ), - 'retval' => __( "'true' or 'false'", 'jetpack' ), - ), - ); - - $this->tokens = apply_filters( 'wga_tokens', $this->tokens ); - } - /** * Used to generate a tracking URL * @@ -211,57 +126,6 @@ public function insert_code( $output = true ) { "_gaq.push(['_setAccount', '{$tracking_id}']);", ); - // Add custom variables specified by the user. - foreach ( $this->_get_options( 'custom_vars', array() ) as $i => $custom_var ) { - if ( empty( $custom_var['name'] ) || empty( $custom_var['value'] ) ) { - continue; - } - - // Check whether a token was used with this custom var, and replace with value if so. - $all_tokens = wp_list_pluck( $this->tokens, 'token' ); - if ( in_array( $custom_var['value'], $all_tokens, true ) ) { - $token = array_pop( wp_filter_object_list( $this->tokens, array( 'token' => $custom_var['value'] ) ) ); - - // Allow tokens to return empty values for specific contexts. - $ignore = false; - if ( ! empty( $token['ignore_when'] ) ) { - foreach ( (array) $token['ignore_when'] as $conditional ) { - if ( is_callable( $conditional ) ) { - $ignore = call_user_func( $conditional ); - if ( $ignore ) { - break; - } - } - } - } - - // If we aren't set to ignore this context, possibly execute the callback. - if ( ! $ignore && ! empty( $token['callback'] ) && is_callable( $token['callback'] ) ) { - $replace = call_user_func( $token['callback'] ); - } else { - $replace = ''; - } - - if ( ! empty( $token['callback_returns'] ) && 'bool' === $token['callback_returns'] ) { - $replace = ( $replace ) ? 'true' : 'false'; - } - - // Replace our token with the value. - $custom_var['value'] = str_replace( $custom_var['value'], $replace, $custom_var['value'] ); - } - - $atts = array( - "'_setCustomVar'", - intval( $i ), - "'" . esc_js( $custom_var['name'] ) . "'", - "'" . esc_js( $custom_var['value'] ) . "'", - ); - if ( $custom_var['scope'] ) { - $atts[] = intval( $custom_var['scope'] ); - } - $custom_vars[] = '_gaq.push([' . implode( ', ', $atts ) . ']);'; - } - $track = array(); if ( is_404() && ( ! isset( $wga['log_404s'] ) || 'false' !== $wga['log_404s'] ) ) { // This is a 404 and we are supposed to track them. From 88d5bed3bf7a2ccdf8fa9a40017445bef2d36d61 Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Mon, 9 Jan 2017 11:47:15 -0300 Subject: [PATCH 25/43] Removes backward compatibility code since it's not needed with this new plugin --- modules/google-analytics/wp-google-analytics.php | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/modules/google-analytics/wp-google-analytics.php b/modules/google-analytics/wp-google-analytics.php index fcc6a3b0ab1ff..a8be564cb99a6 100644 --- a/modules/google-analytics/wp-google-analytics.php +++ b/modules/google-analytics/wp-google-analytics.php @@ -198,19 +198,6 @@ private function _get_options( $option = null, $default = false ) { } else { return $o[ $option ]; } - } else { - if ( 'ignore_role_' === substr( $option, 0, 12 ) ) { - global $wp_roles; - // Backwards compat for when the tracking information was stored as a cap. - $maybe_role = str_replace( 'ignore_role_', '', $option ); - if ( isset( $wp_roles->roles[ $maybe_role ] ) ) { - if ( isset( $wp_roles->roles[ $maybe_role ]['capabilities']['wga_no_track'] ) && $wp_roles->roles[ $maybe_role ]['capabilities']['wga_no_track'] ) { - return 'true'; - } - } - return false; - } - return $default; } } else { return $o; From afb6967e7693b5df3dfbb6f6ae3112ba737f0812 Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Mon, 9 Jan 2017 11:51:05 -0300 Subject: [PATCH 26/43] Removed code for logging outgoing links since it's not supported on WordPress.com --- .jshintignore | 1 - gulpfile.js | 3 +- .../google-analytics/wp-google-analytics.js | 29 ------------------- .../google-analytics/wp-google-analytics.php | 9 ------ 4 files changed, 1 insertion(+), 41 deletions(-) delete mode 100644 modules/google-analytics/wp-google-analytics.js diff --git a/.jshintignore b/.jshintignore index 95a5db500d13c..b989b90adf04f 100644 --- a/.jshintignore +++ b/.jshintignore @@ -9,7 +9,6 @@ modules/shortcodes/js/jmpress.js modules/shortcodes/js/jmpress.min.js modules/shortcodes/js/jquery.cycle.min.js modules/theme-tools/responsive-videos/responsive-videos.min.js -modules/google-analytics/wp-google-analytics.js todo: modules/infinite-scroll/infinity.js diff --git a/gulpfile.js b/gulpfile.js index c7b50097d14b7..884722d467bdc 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -448,8 +448,7 @@ gulp.task( 'js:hint', function() { 'modules/**/*.js', '!_inc/*.min.js', '!modules/*.min.', - '!modules/**/*.min.js', - '!modules/google-analytics/*.js' + '!modules/**/*.min.js' ] ) .pipe( jshint( '.jshintrc' ) ) .pipe( jshint.reporter( 'jshint-stylish' ) ) diff --git a/modules/google-analytics/wp-google-analytics.js b/modules/google-analytics/wp-google-analytics.js deleted file mode 100644 index 6e1d5917e92ae..0000000000000 --- a/modules/google-analytics/wp-google-analytics.js +++ /dev/null @@ -1,29 +0,0 @@ -(function($){ // Open closure and map jQuery to $. - - // Adds :external for grabbing external links - $.expr[':'].external = function(obj) { - return obj.href && !obj.href.match(/^mailto\:/) && !obj.href.match(/^javascript\:/) && (obj.hostname != location.hostname); - }; - - // Document ready. - $( function() { - // Add 'external' class and _blank target to all external links - $('a:external').on( 'click.wp-google-analytics', function(e){ - try { - _gaq.push( [ '_trackEvent', 'Outbound Links', e.currentTarget.host, $(this).attr('href') ] ); - /** - * If this link is not opened in a new tab or window, we need to add - * a small delay so the event can fully fire. See: - * http://support.google.com/analytics/bin/answer.py?hl=en&answer=1136920 - * - * We're actually checking for modifier keys, middle-click, or pre-existing target=_blank attribute - */ - if ( ! ( e.metaKey || e.ctrlKey || 1 == e.button || '_blank' == $(this).attr('target') ) ) { - e.preventDefault(); - setTimeout('document.location = "' + $(this).attr('href') + '"', 100) - } - } catch(err) {} - }); - }); - -})( jQuery ); // Close closure. diff --git a/modules/google-analytics/wp-google-analytics.php b/modules/google-analytics/wp-google-analytics.php index a8be564cb99a6..1e7c871156fe0 100644 --- a/modules/google-analytics/wp-google-analytics.php +++ b/modules/google-analytics/wp-google-analytics.php @@ -204,15 +204,6 @@ private function _get_options( $option = null, $default = false ) { } } - /** - * If we track outgoing links, this will enqueue our javascript file - */ - public function track_outgoing() { - if ( 'true' === $this->_get_options( 'log_outgoing' ) && ( ! defined( 'XMLRPC_REQUEST' ) || ! XMLRPC_REQUEST ) && ( ! is_admin() || 'false' === $this->_get_options( 'ignore_admin_area' ) ) ) { - wp_enqueue_script( 'wp-google-analytics', plugin_dir_url( __FILE__ ) . 'wp-google-analytics.js', array( 'jquery' ), '0.0.3' ); - } - } - /** * Callback for %the_category% token */ From 0bc5782bd40285f5c97315f8212a1e1934542248 Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Mon, 9 Jan 2017 15:31:36 -0300 Subject: [PATCH 27/43] Adds Jetpack monthly checks to the pro status component --- _inc/client/pro-status/index.jsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/_inc/client/pro-status/index.jsx b/_inc/client/pro-status/index.jsx index 8f924ba1f2475..d42ba4d062b68 100644 --- a/_inc/client/pro-status/index.jsx +++ b/_inc/client/pro-status/index.jsx @@ -4,6 +4,7 @@ import React from 'react'; import { connect } from 'react-redux'; import { translate as __ } from 'i18n-calypso'; +import { includes } from 'lodash'; import Button from 'components/button'; import SimpleNotice from 'components/notice'; @@ -121,7 +122,7 @@ const ProStatus = React.createClass( { ); } - if ( 'google-analytics' === feature && 'jetpack_business' !== sitePlan.product_slug ) { + if ( 'google-analytics' === feature && ! includes( [ 'jetpack_business', 'jetpack_business_monthly' ], sitePlan.product_slug ) ) { return ( From b8546c450a531e31f4bfaaf9e8f504ff5f50c511 Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Tue, 31 Jan 2017 15:58:49 -0300 Subject: [PATCH 36/43] Fixes Google Analytics upgrade url --- _inc/client/pro-status/index.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_inc/client/pro-status/index.jsx b/_inc/client/pro-status/index.jsx index d42ba4d062b68..5428d2abb33c9 100644 --- a/_inc/client/pro-status/index.jsx +++ b/_inc/client/pro-status/index.jsx @@ -127,7 +127,7 @@ const ProStatus = React.createClass( { From 88b2af4033277cc109de12639263d77a74c209f9 Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Tue, 31 Jan 2017 16:02:46 -0300 Subject: [PATCH 37/43] Removes extra call to isModuleActivated --- _inc/client/search/index.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_inc/client/search/index.jsx b/_inc/client/search/index.jsx index eecd355f27b88..93cf6bbc60d72 100644 --- a/_inc/client/search/index.jsx +++ b/_inc/client/search/index.jsx @@ -120,7 +120,7 @@ export const SearchResults = ( { customClasses = unavailableDevMode ? 'devmode-disabled' : '', wordAdsSubHeader = element[2]; - if ( 'wordads' === element[0] && ! isModuleActivated( element[0] ) ) { + if ( 'wordads' === element[0] && ! isModuleActive ) { wordAdsSubHeader = } From 71bdff664eaf2622a68a0dfb294f6cba14b918e7 Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Tue, 31 Jan 2017 16:15:58 -0300 Subject: [PATCH 38/43] Removes Google Analytics link from module description --- modules/google-analytics.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/google-analytics.php b/modules/google-analytics.php index fe22e50268900..9705fe8e459d5 100644 --- a/modules/google-analytics.php +++ b/modules/google-analytics.php @@ -2,7 +2,7 @@ /** * Module Name: Google Analytics - * Module Description: Lets you use Google Analytics to track your WordPress site statistics. + * Module Description: Lets you use Google Analytics to track your WordPress site statistics. * First Introduced: 4.5 * Sort Order: 37 * Requires Connection: Yes From 92e236add4191eae3d47f3b88f60ef910aec5aa3 Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Tue, 31 Jan 2017 16:49:20 -0300 Subject: [PATCH 39/43] Removes the settings form in favor of a link pointing to WordPress.com --- .../components/module-settings/index.jsx | 40 ------------------- .../module-settings/modules-per-tab-page.jsx | 17 ++++++-- _inc/client/engagement/index.jsx | 6 +++ modules/module-headings.php | 2 +- 4 files changed, 21 insertions(+), 44 deletions(-) diff --git a/_inc/client/components/module-settings/index.jsx b/_inc/client/components/module-settings/index.jsx index 8393ff992dcba..df0a74da8de62 100644 --- a/_inc/client/components/module-settings/index.jsx +++ b/_inc/client/components/module-settings/index.jsx @@ -855,43 +855,3 @@ export let WordAdsSettings = React.createClass( { } ); WordAdsSettings = moduleSettingsForm( WordAdsSettings ); - -export let GoogleAnalyticsSettings = React.createClass( { - render() { - return ( -
- -

- { - __( 'Enter your tracking Id value to track your blog with {{a}}Google Analytics{{/a}}.', { - components: { - a: - } - } ) - } -

- -
- - { __( 'Google Analytics Tracking Id' ) } - - -
- - -
- - ); - } -} ); - -GoogleAnalyticsSettings = moduleSettingsForm( GoogleAnalyticsSettings ); diff --git a/_inc/client/components/module-settings/modules-per-tab-page.jsx b/_inc/client/components/module-settings/modules-per-tab-page.jsx index 93334a73fb673..126c221d28c91 100644 --- a/_inc/client/components/module-settings/modules-per-tab-page.jsx +++ b/_inc/client/components/module-settings/modules-per-tab-page.jsx @@ -28,8 +28,7 @@ import { VerificationToolsSettings, SitemapsSettings, VideoPressSettings, - WordAdsSettings, - GoogleAnalyticsSettings + WordAdsSettings } from 'components/module-settings/'; import ExternalLink from 'components/external-link'; @@ -130,7 +129,19 @@ const AllModuleSettingsComponent = React.createClass( { case 'wordads': return ( ); case 'google-analytics': - return ( ); + if ( 'inactive' === module.configure_url ) { + return ( +
+ { __( 'Activate this module to use Google Analytics.' ) } +
+ ); + } else { + return ( +
+ { __( 'Configure Google Analytics settings.' ) } +
+ ); + } case 'gravatar-hovercards': case 'contact-form': case 'latex': diff --git a/_inc/client/engagement/index.jsx b/_inc/client/engagement/index.jsx index c6fda4908a2b8..a0be7eb75ddf0 100644 --- a/_inc/client/engagement/index.jsx +++ b/_inc/client/engagement/index.jsx @@ -178,6 +178,12 @@ export const Engagement = ( props ) => { : 'inactive'; } + moduleDescription = ; + } else if ( element[0] === 'google-analytics' ) { + proProps.configure_url = isModuleActive + ? 'https://wordpress.com/settings/analytics/' + props.siteRawUrl + : 'inactive'; + moduleDescription = ; } diff --git a/modules/module-headings.php b/modules/module-headings.php index f2d9edd28ad1d..f9bedd07fff8f 100644 --- a/modules/module-headings.php +++ b/modules/module-headings.php @@ -319,4 +319,4 @@ function jetpack_get_module_i18n_tag( $key ) { ); } return $module_tags[ $key ]; -} +} \ No newline at end of file From 41af2a8785fc62ba832c68e1f701d62f68ff4495 Mon Sep 17 00:00:00 2001 From: Rodrigo Iloro Date: Tue, 31 Jan 2017 17:56:18 -0300 Subject: [PATCH 40/43] Removes upgrade button flash for Google Analytics --- _inc/client/pro-status/index.jsx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/_inc/client/pro-status/index.jsx b/_inc/client/pro-status/index.jsx index 5428d2abb33c9..1667455de9675 100644 --- a/_inc/client/pro-status/index.jsx +++ b/_inc/client/pro-status/index.jsx @@ -123,6 +123,10 @@ const ProStatus = React.createClass( { } if ( 'google-analytics' === feature && ! includes( [ 'jetpack_business', 'jetpack_business_monthly' ], sitePlan.product_slug ) ) { + if ( this.props.fetchingSiteData ) { + return ''; + } + return (