Google Analyticator - Version 2.40

Version Description

Download this release

Release Info

Developer cavemonkey50
Plugin Icon 128x128 Google Analyticator
Version 2.40
Comparing to
See all releases

Code changes from version 2.3 to 2.40

Files changed (3) hide show
  1. external-tracking.js +23 -0
  2. google-analyticator.php +43 -62
  3. readme.txt +5 -2
external-tracking.js ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ jQuery('a').each(function() {
2
+ var a = jQuery(this);
3
+ var href = a.attr('href');
4
+ var hrefArray = href.split('.');
5
+ var extension = hrefArray[href.length - 1];
6
+
7
+ // If the link is external
8
+ if ( ( href.match(/^http/) ) && ( !href.match(document.domain) ) ) {
9
+ // Add the tracking code
10
+ a.click(function() {
11
+ pageTracker._trackPageview('/outgoing/' + href);
12
+ });
13
+
14
+ }
15
+
16
+ // If the link is a download
17
+ if (jQuery.inArray(extension,fileTypes) != -1) {
18
+ // Add the tracking code
19
+ a.click(function() {
20
+ pageTracker._trackPageview('/documents/' + href);
21
+ });
22
+ }
23
+ });
google-analyticator.php CHANGED
@@ -1,7 +1,7 @@
1
  <?php
2
  /*
3
  * Plugin Name: Google Analyticator
4
- * Version: 2.3
5
  * Plugin URI: http://cavemonkey50.com/code/google-analyticator/
6
  * Description: Adds the necessary JavaScript code to enable <a href="http://www.google.com/analytics/">Google's Analytics</a>. After enabling this plugin visit <a href="options-general.php?page=google-analyticator.php">the options page</a> and enter your Google Analytics' UID and enable logging.
7
  * Author: Ronald Heft, Jr.
@@ -426,78 +426,59 @@ function add_google_analytics() {
426
  echo " " . $extra_after . "\n";
427
 
428
  echo " } catch(err) {}</script>\n";
 
 
 
 
 
 
 
 
429
  }
430
  }
431
  }
432
 
433
- // Add the ougoing links script
434
- function ga_outgoing_links() {
 
 
 
 
 
 
435
  if (get_option(key_ga_outbound) == ga_enabled) {
 
436
  if ((get_option(key_ga_admin) == ga_enabled) || ((get_option(key_ga_admin) == ga_disabled) && ( !current_user_can('level_' . get_option(key_ga_admin_level)) ))) {
437
- add_filter('comment_text', 'ga_outgoing', -10);
438
- add_filter('get_comment_author_link', 'ga_outgoing', -10);
439
- add_filter('the_content', 'ga_outgoing', -10);
440
- add_filter('the_excerpt', 'ga_outgoing', -10);
441
  }
442
  }
443
  }
444
 
445
- // Finds all the links contained in a post or comment
446
- function ga_outgoing($input) {
447
- if ( !is_feed() ) {
448
- static $link_pattern = '/<a (.*?)href="(.*?)\/\/(.*?)"(.*?)>(.*?)<\/a>/i';
449
- static $link_pattern_2 = '/<a (.*?)href=\'(.*?)\/\/(.*?)\'(.*?)>(.*?)<\/a>/i';
450
- $input = preg_replace_callback($link_pattern, ga_parse_link, $input);
451
- $input = preg_replace_callback($link_pattern_2, ga_parse_link, $input);
452
- }
453
- return $input;
454
- }
455
-
456
- // Takes a link and adds the Google outgoing tracking code
457
- function ga_parse_link($matches){
458
- $local_host = ga_find_domain($_SERVER["HTTP_HOST"]);
459
- $target = ga_find_domain($matches[3]);
460
- $url = $matches[3];
461
- $file_extension = strtolower(substr(strrchr($url,"."),1));
462
- if ( $target["domain"] != $local_host["domain"] ){
463
- $tracker_code .= " onclick=\"javascript:pageTracker._trackPageview ('/outbound/".$target["host"]."');\"";
464
- }
465
- if ( ($target["domain"] == $local_host["domain"]) && (ga_check_download($file_extension)) ){
466
- $url = strtolower(substr(strrchr($url,"/"),1));
467
- $tracker_code .= " onclick=\"javascript:pageTracker._trackPageview ('/downloads/".$file_extension."/".$url."');\"";
468
- }
469
- // Properly format additional code
470
- if ( $matches[1] != '' ) {
471
- $matches[1] = ' '. trim($matches[1]);
472
- }
473
- if ( $matches[4] != '' ) {
474
- $matches[4] = ' '. trim($matches[4]);
475
- }
476
-
477
- return '<a href="' . $matches[2] . '//' . $matches[3] . '"' . $matches[1] . $matches[4].$tracker_code.'>' . $matches[5] . '</a>';
478
- }
479
-
480
- // Checks to see if the link is on your site
481
- function ga_find_domain($url){
482
- $host_pattern = "/^(http:\/\/)?([^\/]+)/i";
483
- $domain_pattern = "/[^\.\/]+\.[^\.\/]+$/";
484
-
485
- preg_match($host_pattern, $url, $matches);
486
- $host = $matches[2];
487
- preg_match($domain_pattern, $host, $matches);
488
- return array("domain"=>$matches[0],"host"=>$host);
489
  }
490
 
491
- // Checks to see if the requested URL is a download
492
- function ga_check_download($file_extension){
493
- if (get_option(key_ga_downloads)){
494
- $extensions = explode(',', stripslashes(get_option(key_ga_downloads)));
495
-
496
- foreach ($extensions as $extension) {
497
- if ($extension == $file_extension)
498
- return true;
499
- }
500
- }
501
- }
 
 
 
502
 
503
  ?>
1
  <?php
2
  /*
3
  * Plugin Name: Google Analyticator
4
+ * Version: 2.40
5
  * Plugin URI: http://cavemonkey50.com/code/google-analyticator/
6
  * Description: Adds the necessary JavaScript code to enable <a href="http://www.google.com/analytics/">Google's Analytics</a>. After enabling this plugin visit <a href="options-general.php?page=google-analyticator.php">the options page</a> and enter your Google Analytics' UID and enable logging.
7
  * Author: Ronald Heft, Jr.
426
  echo " " . $extra_after . "\n";
427
 
428
  echo " } catch(err) {}</script>\n";
429
+
430
+
431
+ $extensions = explode(',', stripslashes(get_option(key_ga_downloads)));
432
+ foreach ( $extensions AS $extension )
433
+ $ext .= "'$extension',";
434
+ $ext = substr($ext, 0, -1); ?>
435
+ <script type="text/javascript">var fileTypes = [<?php echo $ext; ?>];</script>
436
+ <?php
437
  }
438
  }
439
  }
440
 
441
+ /**
442
+ * Adds outbound link tracking to Google Analyticator
443
+ *
444
+ * @author Ronald Heft
445
+ **/
446
+ function ga_outgoing_links()
447
+ {
448
+ // If outbound tracking is enabled
449
  if (get_option(key_ga_outbound) == ga_enabled) {
450
+ // If admin tracking is enabled or not an admin user
451
  if ((get_option(key_ga_admin) == ga_enabled) || ((get_option(key_ga_admin) == ga_disabled) && ( !current_user_can('level_' . get_option(key_ga_admin_level)) ))) {
452
+ add_action('wp_print_scripts', 'ga_external_tracking_js');
453
+ add_action('wp_head', 'ga_file_extensions');
 
 
454
  }
455
  }
456
  }
457
 
458
+ /**
459
+ * Adds the scripts required for outbound link tracking
460
+ *
461
+ * @author Ronald Heft
462
+ **/
463
+ function ga_external_tracking_js()
464
+ {
465
+ wp_enqueue_script('jquery');
466
+ wp_enqueue_script('ga-external-tracking', plugins_url('/google-analyticator/external-tracking.js'));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
467
  }
468
 
469
+ /**
470
+ * Adds download tracking via jQuery to Google Analyticator
471
+ *
472
+ * @author Ronald Heft
473
+ **/
474
+ function ga_file_extensions()
475
+ {
476
+ $extensions = explode(',', stripslashes(get_option(key_ga_downloads)));
477
+ foreach ( $extensions AS $extension )
478
+ $ext .= "'$extensions',";
479
+ $ext = substr($ext, 0, -1);
480
+ ?>
481
+ <script type="text/javascript">var fileTypes = [<?php echo $ext; ?>];</script>
482
+ <?php }
483
 
484
  ?>
readme.txt CHANGED
@@ -2,9 +2,9 @@
2
  Contributors: cavemonkey50
3
  Donate link: http://cavemonkey50.com/code/
4
  Tags: stats, google, analytics, tracking
5
- Requires at least: 2.3
6
  Tested up to: 2.7
7
- Stable tag: 2.3
8
 
9
  Adds the necessary JavaScript code to enable Google Analytics.
10
 
@@ -55,6 +55,9 @@ Google's servers are slow at crawling for the tracking code. While the code may
55
 
56
  == Changelog ==
57
 
 
 
 
58
  **2.3** - Minor Update
59
  - Updates the Analytics script to match a change by Google. This should resolve the undefined _gat errors.
60
 
2
  Contributors: cavemonkey50
3
  Donate link: http://cavemonkey50.com/code/
4
  Tags: stats, google, analytics, tracking
5
+ Requires at least: 2.5
6
  Tested up to: 2.7
7
+ Stable tag: 2.40
8
 
9
  Adds the necessary JavaScript code to enable Google Analytics.
10
 
55
 
56
  == Changelog ==
57
 
58
+ **2.40** - Minor Update
59
+ - Replaces the PHP-based external tracking solution with a jQuery-based one.
60
+
61
  **2.3** - Minor Update
62
  - Updates the Analytics script to match a change by Google. This should resolve the undefined _gat errors.
63