Google Analytics for WordPress by MonsterInsights - Version 3.1

Version Description

  • Added 404 tracking as described here.
  • Optimized the tracking script, if extra search engine tracking is disabled it'll be a lot smaller now.
  • Various code optimizations to prevent PHP notices and removal of redundant code.
Download this release

Release Info

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

Code changes from version 3.0.1 to 3.1

Files changed (2) hide show
  1. googleanalytics.php +102 -112
  2. readme.txt +6 -1
googleanalytics.php CHANGED
@@ -4,15 +4,17 @@ Plugin Name: Google Analytics for WordPress
4
  Plugin URI: http://yoast.com/wordpress/analytics/
5
  Description: This plugin makes it simple to add Google Analytics with extra search engines and automatic clickout and download tracking to your WordPress blog.
6
  Author: Joost de Valk
7
- Version: 3.0.1
8
  Requires at least: 2.7
9
  Author URI: http://yoast.com/
10
  License: GPL
11
 
12
  */
13
-
14
  // Determine the location
15
- $gapppluginpath = plugins_url('', __FILE__).'/';
 
 
16
 
17
  /*
18
  * Admin User Interface
@@ -83,55 +85,40 @@ if ( ! class_exists( 'GA_Admin' ) ) {
83
  }
84
 
85
  function config_page() {
86
- global $dlextensions, $gapppluginpath;
87
- if ( isset($_POST['reset']) && $_POST['reset'] == "true") {
88
- $options['dlextensions'] = 'doc,exe,.js,pdf,ppt,tgz,zip,xls';
89
- $options['dlprefix'] = '/downloads';
90
- $options['artprefix'] = '/outbound/article';
91
- $options['comprefix'] = '/outbound/comment';
92
- $options['comautprefix'] = '/outbound/commentauthor';
93
- $options['blogrollprefix'] = '/outbound/blogroll';
94
- $options['domainorurl'] = 'domain';
95
- $options['userv2'] = false;
96
- $options['extrase'] = false;
97
- $options['imagese'] = false;
98
- $options['admintracking'] = true;
99
- $options['trackoutbound'] = true;
100
- $options['advancedsettings'] = false;
101
- $options['allowanchor'] = false;
102
- update_option('GoogleAnalyticsPP',$options);
103
  echo "<div class=\"updated\"><p>Google Analytics settings reset to default.</p></div>\n";
104
  }
 
105
  if ( isset($_POST['submit']) ) {
106
  if (!current_user_can('manage_options')) die(__('You cannot edit the Google Analytics for WordPress options.'));
107
  check_admin_referer('analyticspp-config');
108
- $options['uastring'] = $_POST['uastring'];
109
 
110
- foreach (array('dlextensions', 'dlprefix', 'artprefix', 'comprefix', 'comautprefix', 'blogrollprefix', 'domainorurl','position','domain') as $option_name) {
111
- if (isset($_POST[$option_name])) {
112
- $options[$option_name] = strtolower($_POST[$option_name]);
113
- }
 
114
  }
115
 
116
  foreach (array('extrase', 'imagese', 'trackoutbound', 'trackloggedin', 'admintracking', 'trackadsense', 'userv2', 'allowanchor', 'rsslinktagging', 'advancedsettings') as $option_name) {
117
- if (isset($_POST[$option_name])) {
118
  $options[$option_name] = true;
119
- } else {
120
  $options[$option_name] = false;
121
- }
122
  }
123
 
124
- if ($options['imagese']) {
125
  $options['extrase'] = true;
126
- }
127
 
128
  update_option('GoogleAnalyticsPP', $options);
129
  echo "<div id=\"updatemessage\" class=\"updated fade\"><p>Google Analytics settings updated.</p></div>\n";
130
- echo "<script type=\"text/javascript\">setTimeout(function(){jQuery('#updatemessage').hide('slow');}, 3000);</script>";
131
-
132
  }
133
 
134
- $options = get_option('GoogleAnalyticsPP');
135
  ?>
136
  <div class="wrap">
137
  <a href="http://yoast.com/"><div id="yoast-icon" style="background: url(http://cdn.yoast.com/theme/yoast-32x32.png) no-repeat;" class="icon32"><br /></div></a>
@@ -151,7 +138,7 @@ if ( ! class_exists( 'GA_Admin' ) ) {
151
  <strong>Explanation</strong><br/>
152
  Find the Account ID, starting with UA- in your account overview, as marked below:<br/>
153
  <br/>
154
- <img src="'.$gapppluginpath.'/account-id.png" alt="Account ID"/><br/>
155
  <br/>
156
  Once you have entered your Account ID in the box above your pages will be trackable by Google Analytics.<br/>
157
  Still can\'t find it? Watch <a href="http://yoast.com/wordpress/google-analytics/#accountid">this video</a>!
@@ -269,7 +256,7 @@ if ( ! class_exists( 'GA_Admin' ) ) {
269
  </form>
270
  <form action="" method="post">
271
  <input type="hidden" name="reset" value="true"/>
272
- <div class="submit"><input type="submit" value="Reset Settings &raquo;" /></div>
273
  </form>
274
  </div>
275
  </div>
@@ -278,8 +265,8 @@ if ( ! class_exists( 'GA_Admin' ) ) {
278
  <div class="metabox-holder">
279
  <div class="meta-box-sortables">
280
  <?php
281
- $this->plugin_like('blog-icons');
282
- $this->plugin_support('blog-icons');
283
  $this->news();
284
  ?>
285
  </div>
@@ -301,9 +288,10 @@ if ( ! class_exists( 'GA_Admin' ) ) {
301
  } else {
302
  add_action('admin_footer', array(&$this,'warning'));
303
  }
304
- } // end config_page()
305
 
306
- function restore_defaults() {
 
307
  $options['dlextensions'] = 'doc,exe,.js,pdf,ppt,tgz,zip,xls';
308
  $options['dlprefix'] = '/downloads';
309
  $options['artprefix'] = '/outbound/article';
@@ -314,8 +302,10 @@ if ( ! class_exists( 'GA_Admin' ) ) {
314
  $options['userv2'] = false;
315
  $options['extrase'] = false;
316
  $options['imagese'] = false;
317
- $options['trackoutbound'] = true;
318
  $options['admintracking'] = true;
 
 
 
319
  update_option('GoogleAnalyticsPP',$options);
320
  }
321
 
@@ -338,55 +328,71 @@ if ( ! class_exists( 'GA_Filter' ) ) {
338
  /*
339
  * Insert the tracking code into the page
340
  */
341
- function spool_analytics() {
342
- global $gapppluginpath;
343
-
344
  $options = get_option('GoogleAnalyticsPP');
345
 
346
- if ($options["uastring"] != "" && (!current_user_can('edit_users') || $options["admintracking"]) && !is_preview() ) { ?>
347
- <!-- Google Analytics for WordPress | http://yoast.com/wordpress/google-analytics/ -->
348
- <script type="text/javascript">
349
- var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
350
- document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
351
- </script>
352
- <script type="text/javascript">
353
- try {
354
- var pageTracker = _gat._getTracker("<?php echo $options["uastring"]; ?>");
355
- } catch(err) {}
356
- </script>
357
- <?php if ( $options["extrase"] == true ) {
358
- echo("\t<script src=\"".$gapppluginpath."custom_se.js\" type=\"text/javascript\"></script>\n");
359
- } ?>
360
- <script type="text/javascript">
361
- try {
362
- <?php if ( $options['userv2'] ) {
363
- echo("\t\t\tpageTracker._setLocalRemoteServerMode();\n");
364
- }
365
- if ( $options['allowanchor'] ) {
366
- echo("\t\t\tpageTracker._setAllowAnchor(true);\n");
367
- }
368
- if ($options['trackloggedin'] && !isset($_COOKIE['__utmv']) && is_user_logged_in() ) {
369
- echo("\t\t\tpageTracker._setVar('logged-in');\n");
370
- } else {
371
- echo("\t\t\t// Cookied already: ".$_COOKIE['__utmv']."\n");
372
- }
373
- if ( isset($options['domain']) && $options['domain'] != "" ) {
374
- if (substr($options['domain'],0,1) != ".") {
375
- $options['domain'] = ".".$options['domain'];
376
- }
377
- echo("\t\t\tpageTracker._setDomainName(\"".$options['domain']."\");\n");
378
- }
379
- if (strpos($_SERVER['HTTP_REFERER'],"images.google") && strpos($_SERVER['HTTP_REFERER'],"&prev") && $options["imagese"]) { ?>
380
- regex = new RegExp("images.google.([^\/]+).*&prev=([^&]+)");
381
- var match = regex.exec(pageTracker.qa);
382
- pageTracker.qa = "http://images.google." + match[1] + unescape(match[2]); <?php }
383
- ?> pageTracker._trackPageview();
384
- } catch(err) {}
385
- </script>
386
- <!-- End of Google Analytics code -->
387
- <?php
388
- } else if ((current_user_can('edit_users') && !$options["admintracking"])) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
389
  echo "<!-- Google Analytics tracking code not shown because admin tracking is disabled -->";
 
 
390
  }
391
  }
392
 
@@ -395,12 +401,10 @@ if (strpos($_SERVER['HTTP_REFERER'],"images.google") && strpos($_SERVER['HTTP_RE
395
  */
396
  function spool_adsense() {
397
  $options = get_option('GoogleAnalyticsPP');
398
- if ($options["uastring"] != "" && (!current_user_can('edit_users') || $options["admintracking"]) && !is_preview() ) { ?>
399
-
400
- <script type="text/javascript">
401
- window.google_analytics_uacct = "<?php echo $options["uastring"]; ?>";
402
- </script>
403
- <?php
404
  }
405
  }
406
 
@@ -422,7 +426,7 @@ if (strpos($_SERVER['HTTP_REFERER'],"images.google") && strpos($_SERVER['HTTP_RE
422
  }
423
 
424
  function ga_parse_link($leaf, $matches){
425
- global $origin ;
426
 
427
  $options = get_option('GoogleAnalyticsPP');
428
 
@@ -571,25 +575,11 @@ if ( $options['allowanchor'] ) {
571
  add_action('init','ga_utm_hastag_redirect',1);
572
  }
573
 
574
- $gaf = new GA_Filter();
575
- $origin = $gaf->ga_get_domain($_SERVER["HTTP_HOST"]);
576
-
577
- $options = get_option('GoogleAnalyticsPP',"");
578
-
579
- if ($options == "") {
580
- $options['dlextensions'] = 'doc,exe,js,pdf,ppt,tgz,zip,xls';
581
- $options['dlprefix'] = '/downloads';
582
- $options['artprefix'] = '/outbound/article';
583
- $options['comprefix'] = '/outbound/comment';
584
- $options['comautprefix'] = '/outbound/commentauthor';
585
- $options['blogrollprefix'] = '/outbound/blogroll';
586
- $options['domainorurl'] = 'domain';
587
- $options['position'] = 'footer';
588
- $options['userv2'] = false;
589
- $options['extrase'] = false;
590
- $options['imagese'] = false;
591
- $options['trackoutbound'] = true;
592
- update_option('GoogleAnalyticsPP',$options);
593
  }
594
 
595
  if ($options['trackoutbound']) {
4
  Plugin URI: http://yoast.com/wordpress/analytics/
5
  Description: This plugin makes it simple to add Google Analytics with extra search engines and automatic clickout and download tracking to your WordPress blog.
6
  Author: Joost de Valk
7
+ Version: 3.1
8
  Requires at least: 2.7
9
  Author URI: http://yoast.com/
10
  License: GPL
11
 
12
  */
13
+
14
  // Determine the location
15
+ function gapp_plugin_path() {
16
+ return plugins_url('', __FILE__).'/';
17
+ }
18
 
19
  /*
20
  * Admin User Interface
85
  }
86
 
87
  function config_page() {
88
+ $options = get_option('GoogleAnalyticsPP');
89
+
90
+ if ( (isset($_POST['reset']) && $_POST['reset'] == "true") || !is_array($options) ) {
91
+ $this->set_defaults();
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  echo "<div class=\"updated\"><p>Google Analytics settings reset to default.</p></div>\n";
93
  }
94
+
95
  if ( isset($_POST['submit']) ) {
96
  if (!current_user_can('manage_options')) die(__('You cannot edit the Google Analytics for WordPress options.'));
97
  check_admin_referer('analyticspp-config');
 
98
 
99
+ foreach (array('uastring', 'dlextensions', 'dlprefix', 'artprefix', 'comprefix', 'comautprefix', 'blogrollprefix', 'domainorurl','position','domain') as $option_name) {
100
+ if (isset($_POST[$option_name]))
101
+ $options[$option_name] = $_POST[$option_name];
102
+ else
103
+ $options[$option_name] = '';
104
  }
105
 
106
  foreach (array('extrase', 'imagese', 'trackoutbound', 'trackloggedin', 'admintracking', 'trackadsense', 'userv2', 'allowanchor', 'rsslinktagging', 'advancedsettings') as $option_name) {
107
+ if (isset($_POST[$option_name]))
108
  $options[$option_name] = true;
109
+ else
110
  $options[$option_name] = false;
 
111
  }
112
 
113
+ if ($options['imagese'])
114
  $options['extrase'] = true;
 
115
 
116
  update_option('GoogleAnalyticsPP', $options);
117
  echo "<div id=\"updatemessage\" class=\"updated fade\"><p>Google Analytics settings updated.</p></div>\n";
118
+ echo "<script type=\"text/javascript\">setTimeout(function(){jQuery('#updatemessage').hide('slow');}, 3000);</script>";
 
119
  }
120
 
121
+
122
  ?>
123
  <div class="wrap">
124
  <a href="http://yoast.com/"><div id="yoast-icon" style="background: url(http://cdn.yoast.com/theme/yoast-32x32.png) no-repeat;" class="icon32"><br /></div></a>
138
  <strong>Explanation</strong><br/>
139
  Find the Account ID, starting with UA- in your account overview, as marked below:<br/>
140
  <br/>
141
+ <img src="'.gapp_plugin_path().'/account-id.png" alt="Account ID"/><br/>
142
  <br/>
143
  Once you have entered your Account ID in the box above your pages will be trackable by Google Analytics.<br/>
144
  Still can\'t find it? Watch <a href="http://yoast.com/wordpress/google-analytics/#accountid">this video</a>!
256
  </form>
257
  <form action="" method="post">
258
  <input type="hidden" name="reset" value="true"/>
259
+ <div class="submit"><input type="submit" value="Reset Default Settings &raquo;" /></div>
260
  </form>
261
  </div>
262
  </div>
265
  <div class="metabox-holder">
266
  <div class="meta-box-sortables">
267
  <?php
268
+ $this->plugin_like();
269
+ $this->plugin_support();
270
  $this->news();
271
  ?>
272
  </div>
288
  } else {
289
  add_action('admin_footer', array(&$this,'warning'));
290
  }
291
+ }
292
 
293
+ function set_defaults() {
294
+ $options = get_option('GoogleAnalyticsPP');
295
  $options['dlextensions'] = 'doc,exe,.js,pdf,ppt,tgz,zip,xls';
296
  $options['dlprefix'] = '/downloads';
297
  $options['artprefix'] = '/outbound/article';
302
  $options['userv2'] = false;
303
  $options['extrase'] = false;
304
  $options['imagese'] = false;
 
305
  $options['admintracking'] = true;
306
+ $options['trackoutbound'] = true;
307
+ $options['advancedsettings'] = false;
308
+ $options['allowanchor'] = false;
309
  update_option('GoogleAnalyticsPP',$options);
310
  }
311
 
328
  /*
329
  * Insert the tracking code into the page
330
  */
331
+ function spool_analytics() {
 
 
332
  $options = get_option('GoogleAnalyticsPP');
333
 
334
+ if ( $options["uastring"] != "" && (!current_user_can('edit_users') || $options["admintracking"]) && !is_preview() ) {
335
+ echo "\n".'<!-- Google Analytics for WordPress | http://yoast.com/wordpress/google-analytics/ -->'."\n";
336
+ echo '<script type="text/javascript">'."\n";
337
+ echo "\t".'var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");'."\n";
338
+ echo "\t".'document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));'."\n";
339
+ echo '</script>'."\n";
340
+ echo '<script type="text/javascript">'."\n";
341
+ echo "\t".'try {'."\n";
342
+ echo "\t\t".'var pageTracker = _gat._getTracker("'.$options["uastring"].'");'."\n";
343
+
344
+ /**
345
+ * If this is a 404 page, track the 404 and prevent all other stuff as it's not needed.
346
+ */
347
+ if ( is_404() ) {
348
+ echo "\t".'try {'."\n";
349
+ echo "\t\t".'pageTracker._trackPageview("/404.html?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer);'."\n";
350
+ echo "\t".'} catch(err) {}'."\n";
351
+ echo '</script>'."\n";
352
+ } else {
353
+ if ( $options["extrase"] ) {
354
+ /**
355
+ * We need to load another script, so we need to close the try / catch, load the script, and open it again.
356
+ */
357
+ echo "\t".'} catch(err) {}'."\n";
358
+ echo '</script>'."\n";
359
+ echo '<script src="'.gapp_plugin_path().'"custom_se.js" type="text/javascript"></script>'."\n";
360
+ echo '<script type="text/javascript">'."\n";
361
+ echo "\t".'try {'."\n";
362
+ }
363
+
364
+ if ( $options['userv2'] )
365
+ echo "\t\t".'pageTracker._setLocalRemoteServerMode();'."\n";
366
+
367
+ if ( $options['allowanchor'] )
368
+ echo "\t\t".'pageTracker._setAllowAnchor(true);'."\n";
369
+
370
+ if ( $options['trackloggedin'] && !isset($_COOKIE['__utmv']) && is_user_logged_in() )
371
+ echo "\t\tpageTracker._setVar('logged-in');\n";
372
+ else
373
+ echo "\t\t// Cookied already: ".$_COOKIE['__utmv']."\n";
374
+
375
+ if ( isset($options['domain']) && $options['domain'] != "" ) {
376
+ if (substr($options['domain'],0,1) != ".")
377
+ $options['domain'] = ".".$options['domain'];
378
+ echo "\t\t".'pageTracker._setDomainName("'.$options['domain'].'");'."\n";
379
+ }
380
+
381
+ if ( strpos($_SERVER['HTTP_REFERER'],"images.google") && strpos($_SERVER['HTTP_REFERER'],"&prev") && $options["imagese"] ) {
382
+ echo "\t\t".'regex = new RegExp("images.google.([^\/]+).*&prev=([^&]+)");'."\n";
383
+ echo "\t\t".'var match = regex.exec(pageTracker.qa);'."\n";
384
+ echo "\t\t".'pageTracker.qa = "http://images.google." + match[1] + unescape(match[2]);'."\n";
385
+ }
386
+
387
+ echo "\t\t".'pageTracker._trackPageview();'."\n";
388
+ echo "\t".'} catch(err) {}'."\n";
389
+ echo '</script>'."\n";
390
+ }
391
+ echo '<!-- End of Google Analytics code -->'."\n";
392
+ } else if ( $options["uastring"] != "" && current_user_can('edit_users') && !$options["admintracking"] ) {
393
  echo "<!-- Google Analytics tracking code not shown because admin tracking is disabled -->";
394
+ } else if ( $options["uastring"] == "" && current_user_can('edit_users') ) {
395
+ echo "<!-- Google Analytics tracking code not shown because yo haven't entered your UA string yet. -->";
396
  }
397
  }
398
 
401
  */
402
  function spool_adsense() {
403
  $options = get_option('GoogleAnalyticsPP');
404
+ if ( $options["uastring"] != "" && (!current_user_can('edit_users') || $options["admintracking"]) && !is_preview() ) {
405
+ echo '<script type="text/javascript">'."\n";
406
+ echo "\t".'window.google_analytics_uacct = "'.$options["uastring"].'";'."\n";
407
+ echo '</script>'."\n";
 
 
408
  }
409
  }
410
 
426
  }
427
 
428
  function ga_parse_link($leaf, $matches){
429
+ $origin = GA_Filter::ga_get_domain($_SERVER["HTTP_HOST"]);
430
 
431
  $options = get_option('GoogleAnalyticsPP');
432
 
575
  add_action('init','ga_utm_hastag_redirect',1);
576
  }
577
 
578
+ $gaf = new GA_Filter();
579
+ $options = get_option('GoogleAnalyticsPP',"");
580
+
581
+ if (is_array($options)) {
582
+ $ga_admin->set_defaults();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
583
  }
584
 
585
  if ($options['trackoutbound']) {
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: http://yoast.com/donate/
4
  Tags: analytics, google analytics, statistics
5
  Requires at least: 2.7
6
  Tested up to: 2.8.2
7
- Stable tag: 3.0.1
8
 
9
  The Google Analytics for WordPress plugin automatically tracks and segments all outbound links from within posts, comment author links, links within comments, blogroll links and downloads. It also allows you to track AdSense clicks, add extra search engines, track image search queries and it will even work together with Urchin.
10
 
@@ -29,6 +29,11 @@ This section describes how to install the plugin and get it working.
29
 
30
  == Changelog ==
31
 
 
 
 
 
 
32
  = 3.0.1 =
33
  * Removed no longer needed code to add config page that caused PHP warnings.
34
 
4
  Tags: analytics, google analytics, statistics
5
  Requires at least: 2.7
6
  Tested up to: 2.8.2
7
+ Stable tag: 3.1
8
 
9
  The Google Analytics for WordPress plugin automatically tracks and segments all outbound links from within posts, comment author links, links within comments, blogroll links and downloads. It also allows you to track AdSense clicks, add extra search engines, track image search queries and it will even work together with Urchin.
10
 
29
 
30
  == Changelog ==
31
 
32
+ = 3.1 =
33
+ * Added 404 tracking as described [here](http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&answer=86927).
34
+ * Optimized the tracking script, if extra search engine tracking is disabled it'll be a lot smaller now.
35
+ * Various code optimizations to prevent PHP notices and removal of redundant code.
36
+
37
  = 3.0.1 =
38
  * Removed no longer needed code to add config page that caused PHP warnings.
39