Google Analytics for WordPress by MonsterInsights - Version 5.0.7

Version Description

Release Date: October 14th, 2014

  • Bugfixes:

    • Fixes a bug where 404 tracking would not work when using ga.js tracking.
  • Enhancements:

    • Refactored several bits of code.
Download this release

Release Info

Developer joostdevalk
Plugin Icon 128x128 Google Analytics for WordPress by MonsterInsights
Version 5.0.7
Comparing to
See all releases

Code changes from version 5.0.6 to 5.0.7

admin/class-admin.php CHANGED
@@ -3,8 +3,6 @@
3
  * This class is for the backend, extendable for all child classes
4
  */
5
 
6
- require_once 'wp-gdata/wp-gdata.php';
7
-
8
  if ( ! class_exists( 'Yoast_GA_Admin' ) ) {
9
 
10
  class Yoast_GA_Admin extends Yoast_GA_Options {
@@ -37,6 +35,11 @@ if ( ! class_exists( 'Yoast_GA_Admin' ) ) {
37
  add_action( 'admin_notices', array( $this, 'config_warning' ) );
38
  }
39
 
 
 
 
 
 
40
  if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
41
 
42
  if ( ! function_exists( 'wp_verify_nonce' ) ) {
@@ -77,14 +80,14 @@ if ( ! class_exists( 'Yoast_GA_Admin' ) ) {
77
  */
78
  public function save_settings( $data ) {
79
  foreach ( $data as $key => $value ) {
80
- $this->options[ $key ] = $value;
81
  }
82
 
83
  // Check checkboxes, on a uncheck they won't be posted to this function
84
  $defaults = $this->default_ga_values();
85
- foreach ( $defaults[ $this->option_prefix ] as $key => $value ) {
86
- if ( ! isset( $data[ $key ] ) ) {
87
- $this->options[ $key ] = $value;
88
  }
89
  }
90
 
@@ -135,49 +138,81 @@ if ( ! class_exists( 'Yoast_GA_Admin' ) ) {
135
  add_menu_page( __( 'Yoast Google Analytics:', 'google-analytics-for-wordpress' ) . ' ' . __( 'General Settings', 'google-analytics-for-wordpress' ), __( 'Analytics', 'google-analytics-for-wordpress' ), 'manage_options', 'yst_ga_dashboard', array(
136
  $this,
137
  'load_page',
138
- ), $icon_svg, $on_top ? '2.00013467543': '100.00013467543' );
139
-
140
- // Sub menu pages
141
- $submenu_pages = array(
142
- array(
143
- 'yst_ga_dashboard',
144
- __( 'Yoast Google Analytics:', 'google-analytics-for-wordpress' ) . ' ' . __( 'Dashboard', 'google-analytics-for-wordpress' ),
145
- __( 'Dashboard', 'google-analytics-for-wordpress' ),
146
- 'manage_options',
147
- 'yst_ga_dashboard',
148
- array( $this, 'load_page' ),
149
- array( array( $this, 'yst_ga_dashboard' ) ),
150
- ),
151
- array(
152
- 'yst_ga_dashboard',
153
- __( 'Yoast Google Analytics:', 'google-analytics-for-wordpress' ) . ' ' . __( 'Settings', 'google-analytics-for-wordpress' ),
154
- __( 'Settings', 'google-analytics-for-wordpress' ),
155
- 'manage_options',
156
- 'yst_ga_settings',
157
- array( $this, 'load_page' ),
158
- array( array( $this, 'yst_ga_settings' ) ),
159
- ),
160
- array(
161
- 'yst_ga_dashboard',
162
- __( 'Yoast Google Analytics:', 'google-analytics-for-wordpress' ) . ' ' . __( 'Extensions', 'google-analytics-for-wordpress' ),
163
- __( '<span style="color:#f18500">' . __( 'Extensions', 'google-analytics-for-wordpress' ) . '</span>', 'google-analytics-for-wordpress' ),
164
- 'manage_options',
165
- 'yst_ga_licenses',
166
- array( $this, 'load_page' ),
167
- array( array( $this, 'yst_ga_licenses' ) ),
168
- ),
169
  );
170
 
171
- if ( count( $submenu_pages ) ) {
172
- foreach ( $submenu_pages as $submenu_page ) {
173
- // Add submenu page
174
- $page = add_submenu_page( $submenu_page[0], $submenu_page[1], $submenu_page[2], $submenu_page[3], $submenu_page[4], $submenu_page[5] );
175
- add_action( 'admin_print_styles-' . $page, array( $this, 'enqueue_styles' ) );
176
- if ( 'yst_ga_settings' === $submenu_page[4] || 'yst_ga_licenses' === $submenu_page[4] ) {
177
- add_action( 'admin_print_styles-' . $page, array( $this, 'enqueue_settings_styles' ) );
178
- add_action( 'admin_print_scripts-' . $page, array( $this, 'enqueue_scripts' ) );
179
- }
180
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
  }
182
  }
183
 
@@ -192,6 +227,7 @@ if ( ! class_exists( 'Yoast_GA_Admin' ) ) {
192
  if ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) {
193
  $ext = '.min' . $ext;
194
  }
 
195
  return $ext;
196
  }
197
 
@@ -232,7 +268,7 @@ if ( ! class_exists( 'Yoast_GA_Admin' ) ) {
232
  require_once( $this->plugin_path . 'admin/pages/settings.php' );
233
 
234
  break;
235
- case 'yst_ga_licenses':
236
  require_once( $this->plugin_path . 'admin/pages/extensions.php' );
237
  break;
238
  case 'yst_ga_dashboard':
@@ -294,17 +330,22 @@ if ( ! class_exists( 'Yoast_GA_Admin' ) ) {
294
  $id = str_replace( '[', '-', $name );
295
  $id = str_replace( ']', '', $id );
296
 
 
 
 
 
 
297
  $input .= '<div class="ga-form ga-form-input">';
298
  if ( ! is_null( $title ) ) {
299
  $input .= '<label class="ga-form ga-form-' . $type . '-label ga-form-label-left" id="yoast-ga-form-label-' . $type . '-' . $this->form_namespace . '-' . $id . '" />' . $title . ':</label>';
300
  }
301
 
302
- if ( $type == 'checkbox' && $this->options[ $name ] == 1 ) {
303
  $input .= '<input type="' . $type . '" class="ga-form ga-form-checkbox" id="yoast-ga-form-' . $type . '-' . $this->form_namespace . '-' . $id . '" name="' . $name . '" value="1" checked="checked" />';
304
  } elseif ( $type == 'checkbox' ) {
305
  $input .= '<input type="' . $type . '" class="ga-form ga-form-checkbox" id="yoast-ga-form-' . $type . '-' . $this->form_namespace . '-' . $id . '" name="' . $name . '" value="1" />';
306
  } else {
307
- $input .= '<input type="' . $type . '" class="ga-form ga-form-' . $type . '" id="yoast-ga-form-' . $type . '-' . $this->form_namespace . '-' . $id . '" name="' . $name . '" value="' . stripslashes( $this->options[ $name ] ) . '" />';
308
  }
309
 
310
  if ( ! is_null( $text_label ) ) {
@@ -317,7 +358,7 @@ if ( ! class_exists( 'Yoast_GA_Admin' ) ) {
317
  if ( ! is_null( $description ) ) {
318
  $input .= '<div class="ga-form ga-form-input">';
319
  $input .= '<label class="ga-form ga-form-select-label ga-form-label-left" id="yoast-ga-form-description-select-' . $this->form_namespace . '-' . $id . '" />&nbsp;</label>';
320
- $input .= '<span class="ga-form ga-form-description">' . $description . '</span>';
321
  $input .= '</div>';
322
  }
323
 
@@ -339,6 +380,12 @@ if ( ! class_exists( 'Yoast_GA_Admin' ) ) {
339
  $select = null;
340
  $id = str_replace( '[', '-', $name );
341
  $id = str_replace( ']', '', $id );
 
 
 
 
 
 
342
  $select .= '<div class="ga-form ga-form-input">';
343
  if ( ! is_null( $title ) ) {
344
  $select .= '<label class="ga-form ga-form-select-label ga-form-label-left" id="yoast-ga-form-label-select-' . $this->form_namespace . '-' . $id . '" />' . $title . ':</label>';
@@ -351,14 +398,14 @@ if ( ! class_exists( 'Yoast_GA_Admin' ) ) {
351
  }
352
  if ( count( $values ) >= 1 ) {
353
  foreach ( $values as $value ) {
354
- if ( is_array( $this->options[ $name ] ) ) {
355
- if ( in_array( $value['id'], $this->options[ $name ] ) ) {
356
  $select .= '<option value="' . $value['id'] . '" selected="selected">' . stripslashes( $value['name'] ) . '</option>';
357
  } else {
358
  $select .= '<option value="' . $value['id'] . '">' . stripslashes( $value['name'] ) . '</option>';
359
  }
360
  } else {
361
- $select .= '<option value="' . $value['id'] . '" ' . selected( $this->options[ $name ], $value['id'], false ) . '>' . stripslashes( $value['name'] ) . '</option>';
362
  }
363
  }
364
  }
@@ -388,11 +435,17 @@ if ( ! class_exists( 'Yoast_GA_Admin' ) ) {
388
  public function textarea( $title, $name, $description = null ) {
389
  $text = null;
390
  $id = $this->option_prefix . '_' . $name;
 
 
 
 
 
 
391
  $text .= '<div class="ga-form ga-form-input">';
392
  if ( ! is_null( $title ) ) {
393
  $text .= '<label class="ga-form ga-form-select-label ga-form-label-left" id="yoast-ga-form-label-select-' . $this->form_namespace . '-' . $id . '" />' . __( $title, 'google-analytics-for-wordpress' ) . ':</label>';
394
  }
395
- $text .= '<textarea rows="5" cols="60" name="' . $name . '" id="yoast-ga-form-textarea-' . $this->form_namespace . '-' . $id . '">' . stripslashes( $this->options[ $name ] ) . '</textarea>';
396
  $text .= '</div>';
397
 
398
  // If we get a description, append it to this select field in a new row
@@ -412,172 +465,33 @@ if ( ! class_exists( 'Yoast_GA_Admin' ) ) {
412
  * @return array
413
  */
414
  public function get_profiles() {
415
-
416
- $option_name = 'yst_ga_api';
417
- $options = get_option( $option_name );
418
- $return = array();
419
-
420
- if ( ! empty ( $options['ga_token'] ) ) {
421
- $args = array(
422
- 'scope' => 'https://www.googleapis.com/auth/analytics.readonly',
423
- 'xoauth_displayname' => 'Google Analytics for WordPress by Yoast',
424
- );
425
- $access_token = $options['ga_oauth']['access_token'];
426
- $gdata = new WP_Gdata( $args, $access_token['oauth_token'], $access_token['oauth_token_secret'] );
427
-
428
- $response = $gdata->get( 'https://www.googleapis.com/analytics/v2.4/management/accounts/~all/webproperties/~all/profiles' );
429
- $http_code = wp_remote_retrieve_response_code( $response );
430
- $response = wp_remote_retrieve_body( $response );
431
-
432
- if ( $http_code == 200 ) {
433
- $options['ga_api_response'] = array(
434
- 'response' => array( 'code' => $http_code ),
435
- 'body' => $response,
436
- );
437
- update_option( $option_name, $options );
438
- } else {
439
- return $return;
440
- }
441
-
442
- try {
443
- $xml_reader = new SimpleXMLElement( $options['ga_api_response']['body'] );
444
-
445
- if ( ! empty( $xml_reader->entry ) ) {
446
-
447
- $ga_accounts = array();
448
-
449
- // Check whether the feed output is the new one, first set, or the old one, second set.
450
- if ( $xml_reader->link['href'] == 'https://www.googleapis.com/analytics/v2.4/management/accounts/~all/webproperties/~all/profiles' ) {
451
- foreach ( $xml_reader->entry AS $entry ) {
452
- $ns = $entry->getNamespaces( true );
453
- $properties = $entry->children( $ns['dxp'] )->property;
454
-
455
- if ( isset ( $properties[1]->attributes()->value ) ) {
456
- $ua = (string) trim( $properties[1]->attributes()->value );
457
- }
458
-
459
- if ( isset ( $properties[2]->attributes()->value ) ) {
460
- $title = (string) trim( $properties[2]->attributes()->value );
461
- }
462
-
463
- if ( ! empty( $ua ) && ! empty( $title ) ) {
464
- $ga_accounts[] = array(
465
- 'ua' => $ua,
466
- 'title' => $title,
467
- );
468
- }
469
- }
470
- } else {
471
- if ( $xml_reader->link['href'] == 'https://www.google.com/analytics/feeds/accounts/default' ) {
472
- foreach ( $xml_reader->entry AS $entry ) {
473
- $ns = $entry->getNamespaces( true );
474
- $properties = $entry->children( $ns['dxp'] )->property;
475
-
476
- if ( isset ( $properties[3]->attributes()->value ) ) {
477
- $ua = (string) trim( $properties[3]->attributes()->value );
478
- }
479
-
480
- if ( isset ( $properties[2]->attributes()->value ) ) {
481
- $title = (string) trim( $properties[2]->attributes()->value );
482
- }
483
-
484
- if ( ! empty( $ua ) && ! empty( $title ) ) {
485
- $ga_accounts[] = array(
486
- 'ua' => $ua,
487
- 'title' => $title,
488
- );
489
- }
490
- }
491
- }
492
- }
493
-
494
- if ( is_array( $ga_accounts ) ) {
495
- usort( $ga_accounts, array( $this, 'sort_profiles' ) );
496
- }
497
-
498
- foreach ( $ga_accounts as $key => $ga_account ) {
499
- $return[] = array(
500
- 'id' => $ga_account['ua'],
501
- 'name' => $ga_account['title'] . ' (' . $ga_account['ua'] . ')',
502
- );
503
- }
504
- }
505
- } catch ( Exception $e ) {
506
-
507
- }
508
  }
509
 
510
  return $return;
511
  }
512
 
513
- /**
514
- * Sorting the array in alphabetic order
515
- *
516
- * @param string $a
517
- * @param string $b
518
- *
519
- * @return int
520
- */
521
- public function sort_profiles( $a, $b ) {
522
- return strcmp( $a['title'], $b['title'] );
523
- }
524
 
525
  /**
526
  * Checks if there is a callback or reauth to get token from Google Analytics api
527
  */
528
  private function connect_with_google_analytics() {
529
 
530
- $option_name = 'yst_ga_api';
531
-
532
  if ( isset( $_REQUEST['ga_oauth_callback'] ) ) {
533
- $o = get_option( $option_name );
534
- if ( isset( $o['ga_oauth']['oauth_token'] ) && $o['ga_oauth']['oauth_token'] == $_REQUEST['oauth_token'] ) {
535
- $gdata = new WP_GData(
536
- array(
537
- 'scope' => 'https://www.google.com/analytics/feeds/',
538
- 'xoauth_displayname' => 'Google Analytics by Yoast',
539
- ),
540
- $o['ga_oauth']['oauth_token'],
541
- $o['ga_oauth']['oauth_token_secret']
542
- );
543
-
544
- $o['ga_oauth']['access_token'] = $gdata->get_access_token( $_REQUEST['oauth_verifier'] );
545
- unset( $o['ga_oauth']['oauth_token'] );
546
- unset( $o['ga_oauth']['oauth_token_secret'] );
547
- $o['ga_token'] = $o['ga_oauth']['access_token']['oauth_token'];
548
- }
549
 
550
- update_option( $option_name, $o );
551
 
552
  wp_redirect( menu_page_url( 'yst_ga_settings', false ) );
553
  exit;
554
  }
555
 
556
  if ( ! empty ( $_GET['reauth'] ) ) {
557
- $gdata = new WP_GData(
558
- array(
559
- 'scope' => 'https://www.google.com/analytics/feeds/',
560
- 'xoauth_displayname' => 'Google Analytics by Yoast',
561
- )
562
- );
563
-
564
- $oauth_callback = add_query_arg( array( 'ga_oauth_callback' => 1 ), menu_page_url( 'yst_ga_settings', false ) );
565
- $request_token = $gdata->get_request_token( $oauth_callback );
566
-
567
- $options = get_option( $option_name );
568
-
569
- if ( is_array( $options ) ) {
570
- unset( $options['ga_token'] );
571
- if ( is_array( $options['ga_oauth'] ) ) {
572
- unset( $options['ga_oauth']['access_token'] );
573
- }
574
- }
575
-
576
- $options['ga_oauth']['oauth_token'] = $request_token['oauth_token'];
577
- $options['ga_oauth']['oauth_token_secret'] = $request_token['oauth_token_secret'];
578
- update_option( $option_name, $options );
579
 
580
- wp_redirect( $gdata->get_authorize_url( $request_token ) );
581
  exit;
582
  }
583
  }
@@ -590,8 +504,8 @@ if ( ! class_exists( 'Yoast_GA_Admin' ) ) {
590
  public function get_userroles() {
591
  global $wp_roles;
592
 
593
- $all_roles = $wp_roles->roles;
594
- $roles = array();
595
 
596
  /**
597
  * Filter: 'editable_roles' - Allows filtering of the roles shown within the plugin (and elsewhere in WP as it's a WP filter)
3
  * This class is for the backend, extendable for all child classes
4
  */
5
 
 
 
6
  if ( ! class_exists( 'Yoast_GA_Admin' ) ) {
7
 
8
  class Yoast_GA_Admin extends Yoast_GA_Options {
35
  add_action( 'admin_notices', array( $this, 'config_warning' ) );
36
  }
37
 
38
+ // Require analytics class
39
+ if ( ! class_exists( 'Yoast_Google_Analytics' ) ) {
40
+ require_once 'class-google-analytics.php';
41
+ }
42
+
43
  if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
44
 
45
  if ( ! function_exists( 'wp_verify_nonce' ) ) {
80
  */
81
  public function save_settings( $data ) {
82
  foreach ( $data as $key => $value ) {
83
+ $this->options[$key] = $value;
84
  }
85
 
86
  // Check checkboxes, on a uncheck they won't be posted to this function
87
  $defaults = $this->default_ga_values();
88
+ foreach ( $defaults[$this->option_prefix] as $key => $value ) {
89
+ if ( ! isset( $data[$key] ) ) {
90
+ $this->options[$key] = $value;
91
  }
92
  }
93
 
138
  add_menu_page( __( 'Yoast Google Analytics:', 'google-analytics-for-wordpress' ) . ' ' . __( 'General Settings', 'google-analytics-for-wordpress' ), __( 'Analytics', 'google-analytics-for-wordpress' ), 'manage_options', 'yst_ga_dashboard', array(
139
  $this,
140
  'load_page',
141
+ ), $icon_svg, $on_top ? '2.00013467543' : '100.00013467543' );
142
+
143
+ $this->add_submenu_pages();
144
+ }
145
+
146
+ /**
147
+ * Prepares an array that can be used to add a submenu page to the Google Analytics for Wordpress menu
148
+ *
149
+ * @param $submenu_name
150
+ * @param $font_color
151
+ *
152
+ * @return array
153
+ */
154
+ private function prepare_submenu_page( $submenu_name, $font_color ) {
155
+ $menu_title = __( ucfirst( $submenu_name ), 'google-analytics-for-wordpress' );
156
+ if ( ! empty( $font_color ) ) {
157
+ $menu_title = __( '<span style="color:' . $font_color . '">' . $menu_title . '</span>', 'google-analytics-for-wordpress' );
158
+ }
159
+
160
+ $submenu_page = array(
161
+ 'parent_slug' => 'yst_ga_dashboard',
162
+ 'page_title' => __( 'Yoast Google Analytics:', 'google-analytics-for-wordpress' ) . ' ' . __( ucfirst( $submenu_name ), 'google-analytics-for-wordpress' ),
163
+ 'menu_title' => $menu_title,
164
+ 'capability' => 'manage_options',
165
+ 'menu_slug' => 'yst_ga_' . $submenu_name,
166
+ 'submenu_function' => array( $this, 'load_page' ),
 
 
 
 
 
167
  );
168
 
169
+ return $submenu_page;
170
+ }
171
+
172
+ /**
173
+ * Adds a submenu page to the Google Analytics for WordPress menu
174
+ *
175
+ * @param $submenu_page
176
+ */
177
+ private function add_submenu_page( $submenu_page ) {
178
+ $page = add_submenu_page( $submenu_page['parent_slug'], $submenu_page['page_title'], $submenu_page['menu_title'], $submenu_page['capability'], $submenu_page['menu_slug'], $submenu_page['submenu_function'] );
179
+ add_action( 'admin_print_styles-' . $page, array( $this, 'enqueue_styles' ) );
180
+ if ( 'yst_ga_settings' === $submenu_page['menu_slug'] || 'yst_ga_extensions' === $submenu_page['menu_slug'] ) {
181
+ add_action( 'admin_print_styles-' . $page, array( $this, 'enqueue_settings_styles' ) );
182
+ add_action( 'admin_print_scripts-' . $page, array( $this, 'enqueue_scripts' ) );
183
+ }
184
+ }
185
+
186
+ /**
187
+ * Prepares and adds submenu pages to the Google Analytics for Wordpress menu:
188
+ * - Dashboard
189
+ * - Settings
190
+ * - Extensions
191
+ *
192
+ * @return array
193
+ */
194
+ private function add_submenu_pages() {
195
+ /**
196
+ * Array structure:
197
+ *
198
+ * array(
199
+ * $submenu_name => $font_color,
200
+ * ..,
201
+ * ..,
202
+ * )
203
+ *
204
+ * $font_color can be left empty.
205
+ *
206
+ */
207
+ $submenu_types = array(
208
+ 'dashboard' => '',
209
+ 'settings' => '',
210
+ 'extensions' => '#f18500',
211
+ );
212
+
213
+ foreach ( $submenu_types as $submenu_name => $font_color ) {
214
+ $submenu_page = $this->prepare_submenu_page( $submenu_name, $font_color );
215
+ $this->add_submenu_page( $submenu_page );
216
  }
217
  }
218
 
227
  if ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) {
228
  $ext = '.min' . $ext;
229
  }
230
+
231
  return $ext;
232
  }
233
 
268
  require_once( $this->plugin_path . 'admin/pages/settings.php' );
269
 
270
  break;
271
+ case 'yst_ga_extensions':
272
  require_once( $this->plugin_path . 'admin/pages/extensions.php' );
273
  break;
274
  case 'yst_ga_dashboard':
330
  $id = str_replace( '[', '-', $name );
331
  $id = str_replace( ']', '', $id );
332
 
333
+ // Catch a notice if the option doesn't exist, yet
334
+ if ( ! isset( $this->options[$name] ) ) {
335
+ $this->options[$name] = '';
336
+ }
337
+
338
  $input .= '<div class="ga-form ga-form-input">';
339
  if ( ! is_null( $title ) ) {
340
  $input .= '<label class="ga-form ga-form-' . $type . '-label ga-form-label-left" id="yoast-ga-form-label-' . $type . '-' . $this->form_namespace . '-' . $id . '" />' . $title . ':</label>';
341
  }
342
 
343
+ if ( $type == 'checkbox' && $this->options[$name] == 1 ) {
344
  $input .= '<input type="' . $type . '" class="ga-form ga-form-checkbox" id="yoast-ga-form-' . $type . '-' . $this->form_namespace . '-' . $id . '" name="' . $name . '" value="1" checked="checked" />';
345
  } elseif ( $type == 'checkbox' ) {
346
  $input .= '<input type="' . $type . '" class="ga-form ga-form-checkbox" id="yoast-ga-form-' . $type . '-' . $this->form_namespace . '-' . $id . '" name="' . $name . '" value="1" />';
347
  } else {
348
+ $input .= '<input type="' . $type . '" class="ga-form ga-form-' . $type . '" id="yoast-ga-form-' . $type . '-' . $this->form_namespace . '-' . $id . '" name="' . $name . '" value="' . stripslashes( $this->options[$name] ) . '" />';
349
  }
350
 
351
  if ( ! is_null( $text_label ) ) {
358
  if ( ! is_null( $description ) ) {
359
  $input .= '<div class="ga-form ga-form-input">';
360
  $input .= '<label class="ga-form ga-form-select-label ga-form-label-left" id="yoast-ga-form-description-select-' . $this->form_namespace . '-' . $id . '" />&nbsp;</label>';
361
+ $input .= '<span class="ga-form ga-form-description">' . $description . '</span>';
362
  $input .= '</div>';
363
  }
364
 
380
  $select = null;
381
  $id = str_replace( '[', '-', $name );
382
  $id = str_replace( ']', '', $id );
383
+
384
+ // Catch a notice if the option doesn't exist, yet
385
+ if ( ! isset( $this->options[$name] ) ) {
386
+ $this->options[$name] = '';
387
+ }
388
+
389
  $select .= '<div class="ga-form ga-form-input">';
390
  if ( ! is_null( $title ) ) {
391
  $select .= '<label class="ga-form ga-form-select-label ga-form-label-left" id="yoast-ga-form-label-select-' . $this->form_namespace . '-' . $id . '" />' . $title . ':</label>';
398
  }
399
  if ( count( $values ) >= 1 ) {
400
  foreach ( $values as $value ) {
401
+ if ( is_array( $this->options[$name] ) ) {
402
+ if ( in_array( $value['id'], $this->options[$name] ) ) {
403
  $select .= '<option value="' . $value['id'] . '" selected="selected">' . stripslashes( $value['name'] ) . '</option>';
404
  } else {
405
  $select .= '<option value="' . $value['id'] . '">' . stripslashes( $value['name'] ) . '</option>';
406
  }
407
  } else {
408
+ $select .= '<option value="' . $value['id'] . '" ' . selected( $this->options[$name], $value['id'], false ) . '>' . stripslashes( $value['name'] ) . '</option>';
409
  }
410
  }
411
  }
435
  public function textarea( $title, $name, $description = null ) {
436
  $text = null;
437
  $id = $this->option_prefix . '_' . $name;
438
+
439
+ // Catch a notice if the option doesn't exist, yet
440
+ if ( ! isset( $this->options[$name] ) ) {
441
+ $this->options[$name] = '';
442
+ }
443
+
444
  $text .= '<div class="ga-form ga-form-input">';
445
  if ( ! is_null( $title ) ) {
446
  $text .= '<label class="ga-form ga-form-select-label ga-form-label-left" id="yoast-ga-form-label-select-' . $this->form_namespace . '-' . $id . '" />' . __( $title, 'google-analytics-for-wordpress' ) . ':</label>';
447
  }
448
+ $text .= '<textarea rows="5" cols="60" name="' . $name . '" id="yoast-ga-form-textarea-' . $this->form_namespace . '-' . $id . '">' . stripslashes( $this->options[$name] ) . '</textarea>';
449
  $text .= '</div>';
450
 
451
  // If we get a description, append it to this select field in a new row
465
  * @return array
466
  */
467
  public function get_profiles() {
468
+ $return = array();
469
+ $google_analytics = Yoast_Google_Analytics::instance();
470
+ if ( $google_analytics->has_token() ) {
471
+ $return = $google_analytics->get_profiles();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
472
  }
473
 
474
  return $return;
475
  }
476
 
 
 
 
 
 
 
 
 
 
 
 
477
 
478
  /**
479
  * Checks if there is a callback or reauth to get token from Google Analytics api
480
  */
481
  private function connect_with_google_analytics() {
482
 
 
 
483
  if ( isset( $_REQUEST['ga_oauth_callback'] ) ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
484
 
485
+ Yoast_Google_Analytics::instance()->authenticate( $_REQUEST['oauth_token'], $_REQUEST['oauth_verifier'] );
486
 
487
  wp_redirect( menu_page_url( 'yst_ga_settings', false ) );
488
  exit;
489
  }
490
 
491
  if ( ! empty ( $_GET['reauth'] ) ) {
492
+ $authorize_url = Yoast_Google_Analytics::instance()->authenticate();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
493
 
494
+ wp_redirect( $authorize_url );
495
  exit;
496
  }
497
  }
504
  public function get_userroles() {
505
  global $wp_roles;
506
 
507
+ $all_roles = $wp_roles->roles;
508
+ $roles = array();
509
 
510
  /**
511
  * Filter: 'editable_roles' - Allows filtering of the roles shown within the plugin (and elsewhere in WP as it's a WP filter)
admin/class-google-analytics.php ADDED
@@ -0,0 +1,350 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+
4
+ if ( ! class_exists( 'Yoast_Google_Analytics', false ) ) {
5
+
6
+ class Yoast_Google_Analytics {
7
+
8
+ private $access_token;
9
+ private $secret;
10
+
11
+ private $option_name = 'yst_ga_api';
12
+ private $options = array();
13
+
14
+ private static $instance = null;
15
+
16
+ public function __construct() {
17
+
18
+ if ( is_null( self::$instance ) ) {
19
+ self::$instance = $this;
20
+ }
21
+
22
+ $this->options = $this->get_options();
23
+
24
+ if ( $this->has_token() ) {
25
+ $this->set_access_token();
26
+ $this->set_secret();
27
+ }
28
+ }
29
+
30
+ /**
31
+ * Getting the instance object
32
+ *
33
+ * This method will return the instance of itself, if instance not exists, becauses of it's called for the first
34
+ * time, the instance will be created.
35
+ *
36
+ * @return null|Yoast_Google_Analytics
37
+ */
38
+ public static function instance() {
39
+ if ( is_null( self::$instance ) ) {
40
+ self::$instance = new self();
41
+ }
42
+
43
+ return self::$instance;
44
+ }
45
+
46
+ /**
47
+ * Is there a token set
48
+ *
49
+ * Checks in options whether there is a token set or not. Will return true if token is set
50
+ *
51
+ * @return bool
52
+ */
53
+ public function has_token() {
54
+ return ! empty( $this->options['ga_token'] );
55
+ }
56
+
57
+ /**
58
+ * Connect with google analytics
59
+ *
60
+ * @param bool $token
61
+ * @param bool $verifier
62
+ *
63
+ * @return string
64
+ */
65
+ public function authenticate( $token = false, $verifier = false ) {
66
+
67
+ if ( ! empty( $token ) && ! empty ( $verifier ) ) {
68
+ if ( isset( $this->options['ga_oauth']['oauth_token'] ) && $this->options['ga_oauth']['oauth_token'] == $token ) {
69
+ $this->get_access_token( $verifier );
70
+ }
71
+ } else {
72
+ $authorize_url = $this->get_authorize_url();
73
+
74
+ return $authorize_url;
75
+ }
76
+
77
+ }
78
+
79
+ /**
80
+ * Getting the analytics profiles
81
+ *
82
+ * Doing the request to the Google analytics API and if there is a response, parses this response and return its
83
+ * array
84
+ *
85
+ * @return array
86
+ */
87
+ public function get_profiles() {
88
+
89
+ $return = array();
90
+ $response = $this->do_request( 'https://www.googleapis.com/analytics/v2.4/management/accounts/~all/webproperties/~all/profiles', 'https://www.googleapis.com/auth/analytics.readonly' );
91
+
92
+ if ( $response ) {
93
+ $this->save_profile_response( $response );
94
+
95
+ $return = $this->parse_profile_response( $response );
96
+ }
97
+
98
+ return $return;
99
+ }
100
+
101
+ /**
102
+ * Getting a access token
103
+ *
104
+ * With this token a reconnection to Google Analytics is possible
105
+ *
106
+ * @param string $verifier
107
+ */
108
+ protected function get_access_token( $verifier ) {
109
+ $gdata = $this->get_gdata(
110
+ 'https://www.google.com/analytics/feeds/',
111
+ $this->options['ga_oauth']['oauth_token'],
112
+ $this->options['ga_oauth']['oauth_token_secret']
113
+ );
114
+
115
+ $access_token = $gdata->get_access_token( $verifier );
116
+
117
+ $this->options['ga_oauth']['access_token'] = $access_token;
118
+ $this->options['ga_token'] = $access_token['oauth_token'];
119
+
120
+ unset( $this->options['ga_oauth']['oauth_token'] );
121
+ unset( $this->options['ga_oauth']['oauth_token_secret'] );
122
+
123
+ $this->update_options();
124
+ }
125
+
126
+ /**
127
+ * Getting the URL to authenticate the use
128
+ *
129
+ * @return string
130
+ */
131
+ protected function get_authorize_url() {
132
+ $gdata = $this->get_gdata( 'https://www.google.com/analytics/feeds/' );
133
+ $request_token = $this->get_request_token( $gdata );
134
+
135
+ if ( is_array( $this->options ) ) {
136
+ unset( $this->options['ga_token'] );
137
+ if ( is_array( $this->options['ga_oauth'] ) ) {
138
+ unset( $this->options['ga_oauth']['access_token'] );
139
+ }
140
+ }
141
+
142
+ $this->options['ga_oauth']['oauth_token'] = $request_token['oauth_token'];
143
+ $this->options['ga_oauth']['oauth_token_secret'] = $request_token['oauth_token_secret'];
144
+
145
+ $this->update_options();
146
+
147
+ return $gdata->get_authorize_url( $request_token );
148
+ }
149
+
150
+ /**
151
+ * Get the request token from Google Analytics
152
+ *
153
+ * @param WP_Gdata $gdata
154
+ *
155
+ * @return array
156
+ */
157
+ protected function get_request_token( $gdata ) {
158
+ $oauth_callback = add_query_arg( array( 'ga_oauth_callback' => 1 ), menu_page_url( 'yst_ga_settings', false ) );
159
+ $request_token = $gdata->get_request_token( $oauth_callback );
160
+
161
+ return $request_token;
162
+ }
163
+
164
+ /**
165
+ * Doing request to Google Analytics
166
+ *
167
+ * This method will do a request to google and get the response code and body from content
168
+ *
169
+ * @param string $target_url
170
+ * @param string $scope
171
+ *
172
+ * @return array|null
173
+ */
174
+ protected function do_request( $target_url, $scope ) {
175
+ $gdata = $this->get_gdata( $scope, $this->access_token, $this->secret );
176
+ $response = $gdata->get( $target_url );
177
+ $http_code = wp_remote_retrieve_response_code( $response );
178
+ $response = wp_remote_retrieve_body( $response );
179
+
180
+ if ( $http_code == 200 ) {
181
+ return array(
182
+ 'response' => array( 'code' => $http_code ),
183
+ 'body' => $response,
184
+ );
185
+ }
186
+ }
187
+
188
+ /**
189
+ * Getting WP_GData object
190
+ *
191
+ * If not available include class file and create an instance of WP_GDAta
192
+ *
193
+ * @param string $scope
194
+ * @param null $token
195
+ * @param null $secret
196
+ *
197
+ * @return WP_GData
198
+ */
199
+ protected function get_gdata( $scope, $token = null, $secret = null ) {
200
+ if ( ! class_exists( 'WP_Gdata', false ) ) {
201
+ require_once 'wp-gdata/wp-gdata.php';
202
+ }
203
+
204
+ $args = array(
205
+ 'scope' => $scope,
206
+ 'xoauth_displayname' => 'Google Analytics by Yoast',
207
+ );
208
+
209
+ $gdata = new WP_GData( $args, $token, $secret );
210
+
211
+ return $gdata;
212
+ }
213
+
214
+ /**
215
+ * Saving profile response in options
216
+ *
217
+ * @param $response
218
+ */
219
+ protected function save_profile_response( $response ) {
220
+ $this->options['ga_api_response'] = $response;
221
+ $this->update_options();
222
+ }
223
+
224
+ /**
225
+ * Parsing the profile response
226
+ *
227
+ * Create XML_Reader for the response. Check if there are entries available. Check which link is used and parsing the entries.
228
+ * If there are entries parse, then sort them and rebuild array
229
+ *
230
+ * @return array
231
+ */
232
+ protected function parse_profile_response() {
233
+ $return = array();
234
+
235
+ try {
236
+ $xml_reader = new SimpleXMLElement( $this->options['ga_api_response']['body'] );
237
+
238
+ if ( ! empty( $xml_reader->entry ) ) {
239
+
240
+ $ga_accounts = array();
241
+
242
+ // Check whether the feed output is the new one, first set, or the old one, second set.
243
+ if ( $xml_reader->link['href'] == 'https://www.googleapis.com/analytics/v2.4/management/accounts/~all/webproperties/~all/profiles' ) {
244
+ $ga_accounts = $this->parse_entries( $xml_reader->entry, 1, 2 );
245
+ } elseif ( $xml_reader->link['href'] == 'https://www.google.com/analytics/feeds/accounts/default' ) {
246
+ $ga_accounts = $this->parse_entries( $xml_reader->entry, 3, 2 );
247
+ }
248
+
249
+ if ( is_array( $ga_accounts ) ) {
250
+ usort( $ga_accounts, array( $this, 'sort_profiles' ) );
251
+ }
252
+
253
+ foreach ( $ga_accounts as $key => $ga_account ) {
254
+ $return[] = array(
255
+ 'id' => $ga_account['ua'],
256
+ 'name' => $ga_account['title'] . ' (' . $ga_account['ua'] . ')',
257
+ );
258
+ }
259
+ }
260
+ } catch ( Exception $e ) {
261
+
262
+ }
263
+
264
+ return $return;
265
+
266
+ }
267
+
268
+ /**
269
+ * Sorting the array in alphabetic order
270
+ *
271
+ * @param string $a
272
+ * @param string $b
273
+ *
274
+ * @return int
275
+ */
276
+ protected function sort_profiles( $a, $b ) {
277
+ return strcmp( $a['title'], $b['title'] );
278
+ }
279
+
280
+
281
+ /**
282
+ * Parses the entries
283
+ *
284
+ * The keys can be different for some types of responses, so there are two params which defines the target keys
285
+ *
286
+ * @param SimpleXMLElement $entries
287
+ * @param integer $ua_key
288
+ * @param integer $title_key
289
+ *
290
+ * @return array
291
+ */
292
+ protected function parse_entries( $entries, $ua_key, $title_key ) {
293
+ $return = array();
294
+
295
+ foreach ( $entries AS $entry ) {
296
+ $ns = $entry->getNamespaces( true );
297
+ $properties = $entry->children( $ns['dxp'] )->property;
298
+
299
+ if ( isset ( $properties[$ua_key]->attributes()->value ) ) {
300
+ $ua = (string) trim( $properties[$ua_key]->attributes()->value );
301
+ }
302
+
303
+ if ( isset ( $properties[$title_key]->attributes()->value ) ) {
304
+ $title = (string) trim( $properties[$title_key]->attributes()->value );
305
+ }
306
+
307
+ if ( ! empty( $ua ) && ! empty( $title ) ) {
308
+ $return[] = array(
309
+ 'ua' => $ua,
310
+ 'title' => $title,
311
+ );
312
+ }
313
+ }
314
+
315
+ return $return;
316
+ }
317
+
318
+ /**
319
+ * Setting the token for Google Analytics api
320
+ */
321
+ protected function set_access_token() {
322
+ $this->access_token = $this->options['ga_oauth']['access_token']['oauth_token'];
323
+ }
324
+
325
+ /**
326
+ * Setting the token secret for Google Analytics API
327
+ */
328
+ protected function set_secret() {
329
+ $this->secret = $this->options['ga_oauth']['access_token']['oauth_token_secret'];
330
+ }
331
+
332
+ /**
333
+ * Getting the options bases on $this->option_name from the database
334
+ *
335
+ * @return mixed
336
+ */
337
+ protected function get_options() {
338
+ return get_option( $this->option_name );
339
+ }
340
+
341
+ /**
342
+ * Updating the options based on $this->option_name and the internal property $this->options
343
+ */
344
+ protected function update_options() {
345
+ update_option( $this->option_name, $this->options );
346
+ }
347
+
348
+ }
349
+
350
+ }
admin/pages/settings.php CHANGED
@@ -90,6 +90,8 @@ echo $yoast_ga_admin->create_form( 'settings' );
90
  echo $yoast_ga_admin->input( 'checkbox', __( 'Allow anchor', 'google-analytics-for-wordpress' ), 'allow_anchor', null, __( 'This adds a <code><a href="http://code.google.com/apis/analytics/docs/gaJSApiCampaignTracking.html#_gat.GA_Tracker_._setAllowAnchor" target="_blank">_setAllowAnchor</a></code> call to your tracking code, and makes RSS link tagging use a # as well.', 'google-analytics-for-wordpress' ) );
91
  echo $yoast_ga_admin->input( 'checkbox', __( 'Add <code>_setAllowLinker</code>', 'google-analytics-for-wordpress' ), 'add_allow_linker', null, __( 'This adds a <code><a href="http://code.google.com/apis/analytics/docs/gaJS/gaJSApiDomainDirectory.html#_gat.GA_Tracker_._setAllowLinker" target="_blank">_setAllowLinker</a></code> call to your tracking code, allowing you to use <code>_link</code> and related functions.', 'google-analytics-for-wordpress' ) );
92
  echo $yoast_ga_admin->textarea( 'Custom code', 'custom_code', __( 'Not for the average user: this allows you to add a line of code, to be added before the <code>trackPageview</code> call.', 'google-analytics-for-wordpress' ) );
 
 
93
  ?>
94
  </div>
95
  <div id="debugmode" class="gatab">
90
  echo $yoast_ga_admin->input( 'checkbox', __( 'Allow anchor', 'google-analytics-for-wordpress' ), 'allow_anchor', null, __( 'This adds a <code><a href="http://code.google.com/apis/analytics/docs/gaJSApiCampaignTracking.html#_gat.GA_Tracker_._setAllowAnchor" target="_blank">_setAllowAnchor</a></code> call to your tracking code, and makes RSS link tagging use a # as well.', 'google-analytics-for-wordpress' ) );
91
  echo $yoast_ga_admin->input( 'checkbox', __( 'Add <code>_setAllowLinker</code>', 'google-analytics-for-wordpress' ), 'add_allow_linker', null, __( 'This adds a <code><a href="http://code.google.com/apis/analytics/docs/gaJS/gaJSApiDomainDirectory.html#_gat.GA_Tracker_._setAllowLinker" target="_blank">_setAllowLinker</a></code> call to your tracking code, allowing you to use <code>_link</code> and related functions.', 'google-analytics-for-wordpress' ) );
92
  echo $yoast_ga_admin->textarea( 'Custom code', 'custom_code', __( 'Not for the average user: this allows you to add a line of code, to be added before the <code>trackPageview</code> call.', 'google-analytics-for-wordpress' ) );
93
+
94
+ do_action( 'yst_ga_advanced-tab' );
95
  ?>
96
  </div>
97
  <div id="debugmode" class="gatab">
frontend/class-frontend.php CHANGED
@@ -58,7 +58,7 @@ if ( ! class_exists( 'Yoast_GA_Frontend' ) ) {
58
  * @return array|bool
59
  */
60
  public function yoast_ga_get_domain( $uri ) {
61
- $hostPattern = '/^(http:\/\/)?([^\/]+)/i';
62
  $domainPatternUS = '/[^\.\/]+\.[^\.\/]+$/';
63
  $domainPatternUK = '/[^\.\/]+\.[^\.\/]+\.[^\.\/]+$/';
64
 
58
  * @return array|bool
59
  */
60
  public function yoast_ga_get_domain( $uri ) {
61
+ $hostPattern = '/^(https?:\/\/)?([^\/]+)/i';
62
  $domainPatternUS = '/[^\.\/]+\.[^\.\/]+$/';
63
  $domainPatternUK = '/[^\.\/]+\.[^\.\/]+\.[^\.\/]+$/';
64
 
frontend/class-ga-js.php CHANGED
@@ -27,14 +27,17 @@ if ( ! class_exists( 'Yoast_GA_JS' ) ) {
27
  /**
28
  * Function to output the GA Tracking code in the wp_head()
29
  *
30
- * @todo, add the tracking code and remove this test output
31
  */
32
- public function tracking() {
33
  global $wp_query;
34
 
35
  if ( parent::do_tracking() && ! is_preview() ) {
36
  $gaq_push = array();
37
 
 
 
 
38
  if ( isset( $this->options['subdomain_tracking'] ) && $this->options['subdomain_tracking'] != '' ) {
39
  $domain = $this->options['subdomain_tracking'];
40
  } else {
@@ -46,7 +49,7 @@ if ( ! class_exists( 'Yoast_GA_JS' ) ) {
46
  }
47
 
48
  $ua_code = $this->get_tracking_code();
49
- if ( is_null( $ua_code ) ) {
50
  return;
51
  }
52
 
@@ -56,7 +59,7 @@ if ( ! class_exists( 'Yoast_GA_JS' ) ) {
56
  $gaq_push[] = "'_setDomainName', '" . $domain . "'";
57
  }
58
 
59
- if ( $this->options['allowanchor'] ) {
60
  $gaq_push[] = "'_setAllowAnchor', true";
61
  }
62
 
@@ -73,7 +76,7 @@ if ( ! class_exists( 'Yoast_GA_JS' ) ) {
73
  // Add custom code to the view
74
  $gaq_push[] = array(
75
  'type' => 'custom_code',
76
- 'value' => $this->options['custom_code'],
77
  );
78
  }
79
 
@@ -87,7 +90,7 @@ if ( ! class_exists( 'Yoast_GA_JS' ) ) {
87
  }
88
 
89
  if ( is_404() ) {
90
- $gaq_push[] = "'_trackPageview,'/404.html?page=' + document.location.pathname + document.location.search + '&from=' + document.referrer";
91
  } else {
92
  if ( $wp_query->is_search ) {
93
  $pushstr = "'_trackPageview','/?s=";
@@ -114,10 +117,15 @@ if ( ! class_exists( 'Yoast_GA_JS' ) ) {
114
  *
115
  * @api array $gaq_push
116
  */
 
 
 
 
117
  $gaq_push = apply_filters( 'yoast-ga-push-array-ga-js', $gaq_push );
118
 
119
  $ga_settings = $this->options; // Assign the settings to the javascript include view
120
 
 
121
  // Include the tracking view
122
  if ( $this->options['debug_mode'] == 1 ) {
123
  require( 'views/tracking-debug.php' );
@@ -312,4 +320,4 @@ if ( ! class_exists( 'Yoast_GA_JS' ) ) {
312
 
313
  global $yoast_ga_js;
314
  $yoast_ga_js = new Yoast_GA_JS;
315
- }
27
  /**
28
  * Function to output the GA Tracking code in the wp_head()
29
  *
30
+ * @param bool $return_array
31
  */
32
+ public function tracking( $return_array = false ) {
33
  global $wp_query;
34
 
35
  if ( parent::do_tracking() && ! is_preview() ) {
36
  $gaq_push = array();
37
 
38
+ // Running action for adding possible code
39
+ do_action( 'yst_tracking' );
40
+
41
  if ( isset( $this->options['subdomain_tracking'] ) && $this->options['subdomain_tracking'] != '' ) {
42
  $domain = $this->options['subdomain_tracking'];
43
  } else {
49
  }
50
 
51
  $ua_code = $this->get_tracking_code();
52
+ if ( is_null( $ua_code ) && $return_array == false ) {
53
  return;
54
  }
55
 
59
  $gaq_push[] = "'_setDomainName', '" . $domain . "'";
60
  }
61
 
62
+ if ( isset( $this->options['allowanchor'] ) && $this->options['allowanchor'] ) {
63
  $gaq_push[] = "'_setAllowAnchor', true";
64
  }
65
 
76
  // Add custom code to the view
77
  $gaq_push[] = array(
78
  'type' => 'custom_code',
79
+ 'value' => stripslashes( $this->options['custom_code'] ),
80
  );
81
  }
82
 
90
  }
91
 
92
  if ( is_404() ) {
93
+ $gaq_push[] = "'_trackPageview','/404.html?page=' + document.location.pathname + document.location.search + '&from=' + document.referrer";
94
  } else {
95
  if ( $wp_query->is_search ) {
96
  $pushstr = "'_trackPageview','/?s=";
117
  *
118
  * @api array $gaq_push
119
  */
120
+ if ( true == $return_array ) {
121
+ return $gaq_push;
122
+ }
123
+
124
  $gaq_push = apply_filters( 'yoast-ga-push-array-ga-js', $gaq_push );
125
 
126
  $ga_settings = $this->options; // Assign the settings to the javascript include view
127
 
128
+
129
  // Include the tracking view
130
  if ( $this->options['debug_mode'] == 1 ) {
131
  require( 'views/tracking-debug.php' );
320
 
321
  global $yoast_ga_js;
322
  $yoast_ga_js = new Yoast_GA_JS;
323
+ }
frontend/class-universal.php CHANGED
@@ -29,12 +29,15 @@ if ( ! class_exists( 'Yoast_GA_Universal' ) ) {
29
  *
30
  * @todo, add the tracking code and remove this test output
31
  */
32
- public function tracking() {
33
  global $wp_query;
34
 
35
  if ( $this->do_tracking() && ! is_preview() ) {
36
  $gaq_push = array();
37
 
 
 
 
38
  if ( isset( $this->options['subdomain_tracking'] ) && $this->options['subdomain_tracking'] != '' ) {
39
  $domain = $this->options['subdomain_tracking'];
40
  } else {
@@ -46,19 +49,19 @@ if ( ! class_exists( 'Yoast_GA_Universal' ) ) {
46
  }
47
 
48
  $ua_code = $this->get_tracking_code();
49
- if ( is_null( $ua_code ) ) {
50
  return;
51
  }
52
 
53
  // Set tracking code here
54
  if ( ! empty( $ua_code ) ) {
55
- if ( $this->options['add_allow_linker'] && ! $this->options['allowanchor'] ) {
56
  $gaq_push[] = "'create', '" . $ua_code . "', '" . $domain . "', {'allowLinker': true}";
57
  } else {
58
- if ( $this->options['allowanchor'] && ! $this->options['add_allow_linker'] ) {
59
  $gaq_push[] = "'create', '" . $ua_code . "', '" . $domain . "', {'allowAnchor': true}";
60
  } else {
61
- if ( $this->options['allowanchor'] && $this->options['add_allow_linker'] ) {
62
  $gaq_push[] = "'create', '" . $ua_code . "', '" . $domain . "', {'allowAnchor': true, 'allowLinker': true}";
63
  } else {
64
  $gaq_push[] = "'create', '" . $ua_code . "', '" . $domain . "'";
@@ -73,7 +76,7 @@ if ( ! class_exists( 'Yoast_GA_Universal' ) ) {
73
  // Add custom code to the view
74
  $gaq_push[] = array(
75
  'type' => 'custom_code',
76
- 'value' => $this->options['custom_code'],
77
  );
78
  }
79
 
@@ -119,6 +122,10 @@ if ( ! class_exists( 'Yoast_GA_Universal' ) ) {
119
  *
120
  * @api array $gaq_push
121
  */
 
 
 
 
122
  $gaq_push = apply_filters( 'yoast-ga-push-array-universal', $gaq_push );
123
 
124
  $ga_settings = $this->options; // Assign the settings to the javascript include view
29
  *
30
  * @todo, add the tracking code and remove this test output
31
  */
32
+ public function tracking( $return_array = false ) {
33
  global $wp_query;
34
 
35
  if ( $this->do_tracking() && ! is_preview() ) {
36
  $gaq_push = array();
37
 
38
+ // Running action for adding possible code
39
+ do_action( 'yst_tracking' );
40
+
41
  if ( isset( $this->options['subdomain_tracking'] ) && $this->options['subdomain_tracking'] != '' ) {
42
  $domain = $this->options['subdomain_tracking'];
43
  } else {
49
  }
50
 
51
  $ua_code = $this->get_tracking_code();
52
+ if ( is_null( $ua_code ) && $return_array == false ) {
53
  return;
54
  }
55
 
56
  // Set tracking code here
57
  if ( ! empty( $ua_code ) ) {
58
+ if ( $this->options['add_allow_linker'] && ! $this->options['allow_anchor'] ) {
59
  $gaq_push[] = "'create', '" . $ua_code . "', '" . $domain . "', {'allowLinker': true}";
60
  } else {
61
+ if ( $this->options['allow_anchor'] && ! $this->options['add_allow_linker'] ) {
62
  $gaq_push[] = "'create', '" . $ua_code . "', '" . $domain . "', {'allowAnchor': true}";
63
  } else {
64
+ if ( $this->options['allow_anchor'] && $this->options['add_allow_linker'] ) {
65
  $gaq_push[] = "'create', '" . $ua_code . "', '" . $domain . "', {'allowAnchor': true, 'allowLinker': true}";
66
  } else {
67
  $gaq_push[] = "'create', '" . $ua_code . "', '" . $domain . "'";
76
  // Add custom code to the view
77
  $gaq_push[] = array(
78
  'type' => 'custom_code',
79
+ 'value' => stripslashes( $this->options['custom_code'] ),
80
  );
81
  }
82
 
122
  *
123
  * @api array $gaq_push
124
  */
125
+ if ( true == $return_array ) {
126
+ return $gaq_push;
127
+ }
128
+
129
  $gaq_push = apply_filters( 'yoast-ga-push-array-universal', $gaq_push );
130
 
131
  $ga_settings = $this->options; // Assign the settings to the javascript include view
googleanalytics.php CHANGED
@@ -4,7 +4,7 @@ Plugin Name: Google Analytics by Yoast
4
  Plugin URI: https://yoast.com/wordpress/plugins/google-analytics/#utm_source=wordpress&utm_medium=plugin&utm_campaign=wpgaplugin&utm_content=v504
5
  Description: This plugin makes it simple to add Google Analytics to your WordPress blog, adding lots of features, eg. error page, search result and automatic clickout and download tracking.
6
  Author: Team Yoast
7
- Version: 5.0.6
8
  Requires at least: 3.8
9
  Author URI: https://yoast.com/
10
  License: GPL v3
@@ -30,7 +30,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
30
 
31
  // This plugin was originally based on Rich Boakes' Analytics plugin: http://boakes.org/analytics, but has since been rewritten and refactored multiple times.
32
 
33
- define( 'GAWP_VERSION', '5.0.6' );
34
 
35
  define( 'GAWP_FILE', __FILE__ );
36
 
4
  Plugin URI: https://yoast.com/wordpress/plugins/google-analytics/#utm_source=wordpress&utm_medium=plugin&utm_campaign=wpgaplugin&utm_content=v504
5
  Description: This plugin makes it simple to add Google Analytics to your WordPress blog, adding lots of features, eg. error page, search result and automatic clickout and download tracking.
6
  Author: Team Yoast
7
+ Version: 5.0.7
8
  Requires at least: 3.8
9
  Author URI: https://yoast.com/
10
  License: GPL v3
30
 
31
  // This plugin was originally based on Rich Boakes' Analytics plugin: http://boakes.org/analytics, but has since been rewritten and refactored multiple times.
32
 
33
+ define( 'GAWP_VERSION', '5.0.7' );
34
 
35
  define( 'GAWP_FILE', __FILE__ );
36
 
includes/class-options.php CHANGED
@@ -34,11 +34,34 @@ if ( ! class_exists( 'Yoast_GA_Options' ) ) {
34
  */
35
  public $plugin_url;
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  /**
38
  * Constructor for the options
39
  */
40
  public function __construct() {
41
  $this->options = $this->get_options();
 
42
 
43
  $this->plugin_path = plugin_dir_path( GAWP_FILE );
44
  $this->plugin_url = trailingslashit( plugin_dir_url( GAWP_FILE ) );
@@ -51,6 +74,11 @@ if ( ! class_exists( 'Yoast_GA_Options' ) ) {
51
  if ( ! isset( $this->options['version'] ) || $this->options['version'] < GAWP_VERSION ) {
52
  $this->upgrade();
53
  }
 
 
 
 
 
54
  }
55
 
56
  /**
@@ -61,8 +89,8 @@ if ( ! class_exists( 'Yoast_GA_Options' ) ) {
61
  * @return bool
62
  */
63
  public function update_option( $val ) {
64
- $options = get_option( $this->option_name );
65
- $options[ $this->option_prefix ] = $val;
66
 
67
  return update_option( $this->option_name, $options );
68
  }
@@ -75,7 +103,32 @@ if ( ! class_exists( 'Yoast_GA_Options' ) ) {
75
  public function get_options() {
76
  $options = get_option( $this->option_name );
77
 
78
- return $options[ $this->option_prefix ];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  }
80
 
81
  /**
@@ -85,6 +138,7 @@ if ( ! class_exists( 'Yoast_GA_Options' ) ) {
85
  */
86
  public function get_tracking_code() {
87
  $tracking_code = null;
 
88
 
89
  if ( ! empty( $this->options['analytics_profile'] ) ) {
90
  $tracking_code = $this->options['analytics_profile'];
@@ -129,7 +183,7 @@ if ( ! class_exists( 'Yoast_GA_Options' ) ) {
129
 
130
  // 5.0.0 to 5.0.1 fix of ignore users array
131
  if ( ! isset( $this->options['version'] ) || version_compare( $this->options['version'], '5.0.1', '<' ) ) {
132
- if ( ! is_array( $this->options['ignore_users'] ) ) {
133
  $this->options['ignore_users'] = (array) $this->options['ignore_users'];
134
  }
135
  }
@@ -142,9 +196,9 @@ if ( ! class_exists( 'Yoast_GA_Options' ) ) {
142
 
143
  // Fallback to make sure every default option has a value
144
  $defaults = $this->default_ga_values();
145
- foreach ( $defaults[ $this->option_prefix ] as $key => $value ) {
146
- if ( ! isset( $this->options[ $key ] ) ) {
147
- $this->options[ $key ] = $value;
148
  }
149
  }
150
 
@@ -159,7 +213,8 @@ if ( ! class_exists( 'Yoast_GA_Options' ) ) {
159
  * @return array
160
  */
161
  public function default_ga_values() {
162
- return array(
 
163
  $this->option_prefix => array(
164
  'analytics_profile' => null,
165
  'manual_ua_code' => 0,
@@ -184,6 +239,10 @@ if ( ! class_exists( 'Yoast_GA_Options' ) ) {
184
  'firebug_lite' => 0,
185
  )
186
  );
 
 
 
 
187
  }
188
  }
189
  }
34
  */
35
  public $plugin_url;
36
 
37
+ /**
38
+ * Saving instance of it's own in this static var
39
+ *
40
+ * @var object
41
+ */
42
+ private static $instance;
43
+
44
+ /**
45
+ * Getting instance of this object. If instance doesn't exists it will be created.
46
+ *
47
+ * @return object|Yoast_GA_Options
48
+ */
49
+ public static function instance() {
50
+
51
+ if ( is_null( self::$instance ) ) {
52
+ self::$instance = new Yoast_GA_Options();
53
+ }
54
+
55
+ return self::$instance;
56
+
57
+ }
58
+
59
  /**
60
  * Constructor for the options
61
  */
62
  public function __construct() {
63
  $this->options = $this->get_options();
64
+ $this->options = $this->check_options( $this->options );
65
 
66
  $this->plugin_path = plugin_dir_path( GAWP_FILE );
67
  $this->plugin_url = trailingslashit( plugin_dir_url( GAWP_FILE ) );
74
  if ( ! isset( $this->options['version'] ) || $this->options['version'] < GAWP_VERSION ) {
75
  $this->upgrade();
76
  }
77
+
78
+ // If instance is null, create it. Prevent creating multiple instances of this class
79
+ if ( is_null( self::$instance ) ) {
80
+ self::$instance = $this;
81
+ }
82
  }
83
 
84
  /**
89
  * @return bool
90
  */
91
  public function update_option( $val ) {
92
+ $options = get_option( $this->option_name );
93
+ $options[$this->option_prefix] = $val;
94
 
95
  return update_option( $this->option_name, $options );
96
  }
103
  public function get_options() {
104
  $options = get_option( $this->option_name );
105
 
106
+ return $options[$this->option_prefix];
107
+ }
108
+
109
+ /**
110
+ * Check if all the options are set, to prevent a notice if debugging is enabled
111
+ * When we have new changes, the settings are saved to the options class
112
+ *
113
+ * @param $options
114
+ *
115
+ * @return mixed
116
+ */
117
+ public function check_options( $options ) {
118
+
119
+ $changes = 0;
120
+ foreach ( $this->default_ga_values() as $key => $value ) {
121
+ if ( ! isset( $options[$key] ) ) {
122
+ $options[$key] = $value;
123
+ $changes ++;
124
+ }
125
+ }
126
+
127
+ if ( $changes >= 1 ) {
128
+ $this->update_option( $options );
129
+ }
130
+
131
+ return $options;
132
  }
133
 
134
  /**
138
  */
139
  public function get_tracking_code() {
140
  $tracking_code = null;
141
+ $this->options = $this->get_options();
142
 
143
  if ( ! empty( $this->options['analytics_profile'] ) ) {
144
  $tracking_code = $this->options['analytics_profile'];
183
 
184
  // 5.0.0 to 5.0.1 fix of ignore users array
185
  if ( ! isset( $this->options['version'] ) || version_compare( $this->options['version'], '5.0.1', '<' ) ) {
186
+ if ( isset( $this->options['ignore_users'] ) && ! is_array( $this->options['ignore_users'] ) ) {
187
  $this->options['ignore_users'] = (array) $this->options['ignore_users'];
188
  }
189
  }
196
 
197
  // Fallback to make sure every default option has a value
198
  $defaults = $this->default_ga_values();
199
+ foreach ( $defaults[$this->option_prefix] as $key => $value ) {
200
+ if ( ! isset( $this->options[$key] ) ) {
201
+ $this->options[$key] = $value;
202
  }
203
  }
204
 
213
  * @return array
214
  */
215
  public function default_ga_values() {
216
+
217
+ $options = array(
218
  $this->option_prefix => array(
219
  'analytics_profile' => null,
220
  'manual_ua_code' => 0,
239
  'firebug_lite' => 0,
240
  )
241
  );
242
+
243
+ $options = apply_filters( 'yst_ga_default-ga-values', $options, $this->option_prefix );
244
+
245
+ return $options;
246
  }
247
  }
248
  }
languages/google-analytics-for-wordpress-de_DE.po CHANGED
@@ -342,14 +342,6 @@ msgstr ""
342
  msgid "Not for the average user: this allows you to add a line of code, to be added before the <code>trackPageview</code> call."
343
  msgstr ""
344
 
345
- #: admin/class-admin.php:548
346
- msgid "Track AdSense"
347
- msgstr ""
348
-
349
- #: admin/class-admin.php:549
350
- msgid "This requires integration of your Analytics and AdSense account, for help, <a href=\"http://google.com/support/analytics/bin/answer.py?answer=92625\">look here</a>."
351
- msgstr ""
352
-
353
  #: admin/class-admin.php:554
354
  msgid "Host ga.js locally"
355
  msgstr ""
342
  msgid "Not for the average user: this allows you to add a line of code, to be added before the <code>trackPageview</code> call."
343
  msgstr ""
344
 
 
 
 
 
 
 
 
 
345
  #: admin/class-admin.php:554
346
  msgid "Host ga.js locally"
347
  msgstr ""
languages/google-analytics-for-wordpress-el_GR.po CHANGED
@@ -342,14 +342,6 @@ msgstr ""
342
  msgid "Not for the average user: this allows you to add a line of code, to be added before the <code>trackPageview</code> call."
343
  msgstr ""
344
 
345
- #: admin/class-admin.php:548
346
- msgid "Track AdSense"
347
- msgstr ""
348
-
349
- #: admin/class-admin.php:549
350
- msgid "This requires integration of your Analytics and AdSense account, for help, <a href=\"http://google.com/support/analytics/bin/answer.py?answer=92625\">look here</a>."
351
- msgstr ""
352
-
353
  #: admin/class-admin.php:554
354
  msgid "Host ga.js locally"
355
  msgstr ""
342
  msgid "Not for the average user: this allows you to add a line of code, to be added before the <code>trackPageview</code> call."
343
  msgstr ""
344
 
 
 
 
 
 
 
 
 
345
  #: admin/class-admin.php:554
346
  msgid "Host ga.js locally"
347
  msgstr ""
languages/google-analytics-for-wordpress-es_ES.po CHANGED
@@ -342,14 +342,6 @@ msgstr ""
342
  msgid "Not for the average user: this allows you to add a line of code, to be added before the <code>trackPageview</code> call."
343
  msgstr ""
344
 
345
- #: admin/class-admin.php:548
346
- msgid "Track AdSense"
347
- msgstr ""
348
-
349
- #: admin/class-admin.php:549
350
- msgid "This requires integration of your Analytics and AdSense account, for help, <a href=\"http://google.com/support/analytics/bin/answer.py?answer=92625\">look here</a>."
351
- msgstr ""
352
-
353
  #: admin/class-admin.php:554
354
  msgid "Host ga.js locally"
355
  msgstr ""
342
  msgid "Not for the average user: this allows you to add a line of code, to be added before the <code>trackPageview</code> call."
343
  msgstr ""
344
 
 
 
 
 
 
 
 
 
345
  #: admin/class-admin.php:554
346
  msgid "Host ga.js locally"
347
  msgstr ""
languages/google-analytics-for-wordpress-fi.po CHANGED
@@ -342,14 +342,6 @@ msgstr "Räätälöity koodi"
342
  msgid "Not for the average user: this allows you to add a line of code, to be added before the <code>trackPageview</code> call."
343
  msgstr "Ei normaaleille käyttäjille: mahdollistaa koodirivin lisäämisen ennen <code>trackPageview</code> kutsua."
344
 
345
- #: admin/class-admin.php:548
346
- msgid "Track AdSense"
347
- msgstr "Seuraa AdSenseä"
348
-
349
- #: admin/class-admin.php:549
350
- msgid "This requires integration of your Analytics and AdSense account, for help, <a href=\"http://google.com/support/analytics/bin/answer.py?answer=92625\">look here</a>."
351
- msgstr "Vaatii integraation Analytics ja AdSense tilien välillä. <a href=\"http://google.com/support/analytics/bin/answer.py?answer=92625\">Lue lisää</a>."
352
-
353
  #: admin/class-admin.php:554
354
  msgid "Host ga.js locally"
355
  msgstr "Lataa ga.js paikallisesti"
342
  msgid "Not for the average user: this allows you to add a line of code, to be added before the <code>trackPageview</code> call."
343
  msgstr "Ei normaaleille käyttäjille: mahdollistaa koodirivin lisäämisen ennen <code>trackPageview</code> kutsua."
344
 
 
 
 
 
 
 
 
 
345
  #: admin/class-admin.php:554
346
  msgid "Host ga.js locally"
347
  msgstr "Lataa ga.js paikallisesti"
languages/google-analytics-for-wordpress-fr_FR.po CHANGED
@@ -342,14 +342,6 @@ msgstr ""
342
  msgid "Not for the average user: this allows you to add a line of code, to be added before the <code>trackPageview</code> call."
343
  msgstr ""
344
 
345
- #: admin/class-admin.php:548
346
- msgid "Track AdSense"
347
- msgstr ""
348
-
349
- #: admin/class-admin.php:549
350
- msgid "This requires integration of your Analytics and AdSense account, for help, <a href=\"http://google.com/support/analytics/bin/answer.py?answer=92625\">look here</a>."
351
- msgstr ""
352
-
353
  #: admin/class-admin.php:554
354
  msgid "Host ga.js locally"
355
  msgstr ""
342
  msgid "Not for the average user: this allows you to add a line of code, to be added before the <code>trackPageview</code> call."
343
  msgstr ""
344
 
 
 
 
 
 
 
 
 
345
  #: admin/class-admin.php:554
346
  msgid "Host ga.js locally"
347
  msgstr ""
languages/google-analytics-for-wordpress-hu_HU.po CHANGED
@@ -342,14 +342,6 @@ msgstr ""
342
  msgid "Not for the average user: this allows you to add a line of code, to be added before the <code>trackPageview</code> call."
343
  msgstr ""
344
 
345
- #: admin/class-admin.php:548
346
- msgid "Track AdSense"
347
- msgstr ""
348
-
349
- #: admin/class-admin.php:549
350
- msgid "This requires integration of your Analytics and AdSense account, for help, <a href=\"http://google.com/support/analytics/bin/answer.py?answer=92625\">look here</a>."
351
- msgstr ""
352
-
353
  #: admin/class-admin.php:554
354
  msgid "Host ga.js locally"
355
  msgstr ""
342
  msgid "Not for the average user: this allows you to add a line of code, to be added before the <code>trackPageview</code> call."
343
  msgstr ""
344
 
 
 
 
 
 
 
 
 
345
  #: admin/class-admin.php:554
346
  msgid "Host ga.js locally"
347
  msgstr ""
languages/google-analytics-for-wordpress-nl_NL.po CHANGED
@@ -342,14 +342,6 @@ msgstr "Eigen code"
342
  msgid "Not for the average user: this allows you to add a line of code, to be added before the <code>trackPageview</code> call."
343
  msgstr "Niet voor de gemiddelde gebruiker: dit zorgt dat je een regel codes kan toevoegen voor de <code>trackPageview</code> call."
344
 
345
- #: admin/class-admin.php:548
346
- msgid "Track AdSense"
347
- msgstr "Track AdSense"
348
-
349
- #: admin/class-admin.php:549
350
- msgid "This requires integration of your Analytics and AdSense account, for help, <a href=\"http://google.com/support/analytics/bin/answer.py?answer=92625\">look here</a>."
351
- msgstr "Dit heeft een integratie van je Analytics en AdSense account nodig. Hulp nodig? <a href=\"http://google.com/support/analytics/bin/answer.py?answer=92625\">Kijk hier</a>."
352
-
353
  #: admin/class-admin.php:554
354
  msgid "Host ga.js locally"
355
  msgstr "Host ga.js lokaal"
342
  msgid "Not for the average user: this allows you to add a line of code, to be added before the <code>trackPageview</code> call."
343
  msgstr "Niet voor de gemiddelde gebruiker: dit zorgt dat je een regel codes kan toevoegen voor de <code>trackPageview</code> call."
344
 
 
 
 
 
 
 
 
 
345
  #: admin/class-admin.php:554
346
  msgid "Host ga.js locally"
347
  msgstr "Host ga.js lokaal"
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: https://yoast.com/donate/
4
  Tags: analytics, google analytics, statistics, tracking, stats, google, yoast
5
  Requires at least: 3.8
6
  Tested up to: 4.0
7
- Stable tag: 5.0.6
8
 
9
  Track your WordPress site easily with the latest tracking codes and lots added data for search result pages and error pages.
10
 
@@ -49,6 +49,15 @@ This section describes how to install the plugin and get it working.
49
 
50
  == Changelog ==
51
 
 
 
 
 
 
 
 
 
 
52
  = 5.0.6 =
53
  Release Date: September 17th, 2014
54
 
4
  Tags: analytics, google analytics, statistics, tracking, stats, google, yoast
5
  Requires at least: 3.8
6
  Tested up to: 4.0
7
+ Stable tag: 5.0.7
8
 
9
  Track your WordPress site easily with the latest tracking codes and lots added data for search result pages and error pages.
10
 
49
 
50
  == Changelog ==
51
 
52
+ = 5.0.7 =
53
+ Release Date: October 14th, 2014
54
+
55
+ * Bugfixes:
56
+ * Fixes a bug where 404 tracking would not work when using ga.js tracking.
57
+
58
+ * Enhancements:
59
+ * Refactored several bits of code.
60
+
61
  = 5.0.6 =
62
  Release Date: September 17th, 2014
63