Google Analyticator - Version 4.1

Version Description

Download this release

Release Info

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

Code changes from version 4.0.1 to 4.1

google-analyticator.php CHANGED
@@ -1,7 +1,7 @@
1
- <?php
2
  /*
3
  * Plugin Name: Google Analyticator
4
- * Version: 4.0.1
5
  * Plugin URI: http://plugins.spiralwebconsulting.com/analyticator.html
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 settings page</a> and enter your Google Analytics' UID and enable logging.
7
  * Author: Spiral Web Consulting
@@ -24,6 +24,7 @@ define("key_ga_status", "ga_status", true);
24
  define("key_ga_admin", "ga_admin_status", true);
25
  define("key_ga_admin_disable", "ga_admin_disable", true);
26
  define("key_ga_admin_level", "ga_admin_level", true);
 
27
  define("key_ga_extra", "ga_extra", true);
28
  define("key_ga_extra_after", "ga_extra_after", true);
29
  define("key_ga_outbound", "ga_outbound", true);
@@ -38,6 +39,7 @@ define("ga_status_default", ga_disabled, true);
38
  define("ga_admin_default", ga_enabled, true);
39
  define("ga_admin_disable_default", 'remove', true);
40
  define("ga_admin_level_default", 8, true);
 
41
  define("ga_extra_default", "", true);
42
  define("ga_extra_after_default", "", true);
43
  define("ga_outbound_default", ga_enabled, true);
@@ -53,6 +55,7 @@ add_option(key_ga_uid, ga_uid_default, 'Your Google Analytics UID.');
53
  add_option(key_ga_admin, ga_admin_default, 'If WordPress admins are counted in Google Analytics.');
54
  add_option(key_ga_admin_disable, ga_admin_disable_default, '');
55
  add_option(key_ga_admin_level, ga_admin_level_default, 'The level to consider a user a WordPress admin.');
 
56
  add_option(key_ga_extra, ga_extra_default, 'Addition Google Analytics tracking options');
57
  add_option(key_ga_extra_after, ga_extra_after_default, 'Addition Google Analytics tracking options');
58
  add_option(key_ga_outbound, ga_outbound_default, 'Add tracking of outbound links');
@@ -75,6 +78,7 @@ function ga_admin_init() {
75
  register_setting('google-analyticator', key_ga_admin, '');
76
  register_setting('google-analyticator', key_ga_admin_disable, '');
77
  register_setting('google-analyticator', key_ga_admin_level, '');
 
78
  register_setting('google-analyticator', key_ga_extra, '');
79
  register_setting('google-analyticator', key_ga_extra_after, '');
80
  register_setting('google-analyticator', key_ga_outbound, '');
@@ -137,6 +141,10 @@ function ga_options_page() {
137
  // Update the extra after tracking code
138
  $ga_extra_after = $_POST[key_ga_extra_after];
139
  update_option(key_ga_extra_after, $ga_extra_after);
 
 
 
 
140
 
141
  // Update the outbound tracking
142
  $ga_outbound = $_POST[key_ga_outbound];
@@ -425,6 +433,20 @@ function ga_options_page() {
425
  <p style="margin: 5px 10px;" class="setting-description">Enter a name for the section tracked download links will appear under.</em></p>
426
  </td>
427
  </tr>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428
  <tr>
429
  <th valign="top" style="padding-top: 10px;">
430
  <label for="<?php echo key_ga_extra; ?>">Additional tracking code<br />(before tracker initialization):</label>
@@ -513,6 +535,10 @@ function add_google_analytics() {
513
  if ( ( get_option(key_ga_admin) == ga_enabled || !current_user_can('level_' . get_option(key_ga_admin_level)) ) && get_option(key_ga_admin_disable) == 'remove' || get_option(key_ga_admin_disable) != 'remove' ) {
514
 
515
  echo "<!-- Google Analytics Tracking by Google Analyticator: http://plugins.spiralwebconsulting.com/analyticator.html -->\n";
 
 
 
 
516
  // Pick the HTTP connection
517
  if ( get_option(key_ga_specify_http) == 'http' ) {
518
  echo " <script type=\"text/javascript\" src=\"http://www.google-analytics.com/ga.js\"></script>\n\n";
1
+ <?php
2
  /*
3
  * Plugin Name: Google Analyticator
4
+ * Version: 4.1
5
  * Plugin URI: http://plugins.spiralwebconsulting.com/analyticator.html
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 settings page</a> and enter your Google Analytics' UID and enable logging.
7
  * Author: Spiral Web Consulting
24
  define("key_ga_admin", "ga_admin_status", true);
25
  define("key_ga_admin_disable", "ga_admin_disable", true);
26
  define("key_ga_admin_level", "ga_admin_level", true);
27
+ define("key_ga_adsense", "ga_adsense", true);
28
  define("key_ga_extra", "ga_extra", true);
29
  define("key_ga_extra_after", "ga_extra_after", true);
30
  define("key_ga_outbound", "ga_outbound", true);
39
  define("ga_admin_default", ga_enabled, true);
40
  define("ga_admin_disable_default", 'remove', true);
41
  define("ga_admin_level_default", 8, true);
42
+ define("ga_adsense_default", "", true);
43
  define("ga_extra_default", "", true);
44
  define("ga_extra_after_default", "", true);
45
  define("ga_outbound_default", ga_enabled, true);
55
  add_option(key_ga_admin, ga_admin_default, 'If WordPress admins are counted in Google Analytics.');
56
  add_option(key_ga_admin_disable, ga_admin_disable_default, '');
57
  add_option(key_ga_admin_level, ga_admin_level_default, 'The level to consider a user a WordPress admin.');
58
+ add_option(key_ga_adsense, ga_adsense_default, '');
59
  add_option(key_ga_extra, ga_extra_default, 'Addition Google Analytics tracking options');
60
  add_option(key_ga_extra_after, ga_extra_after_default, 'Addition Google Analytics tracking options');
61
  add_option(key_ga_outbound, ga_outbound_default, 'Add tracking of outbound links');
78
  register_setting('google-analyticator', key_ga_admin, '');
79
  register_setting('google-analyticator', key_ga_admin_disable, '');
80
  register_setting('google-analyticator', key_ga_admin_level, '');
81
+ register_setting('google-analyticator', key_ga_adsense, '');
82
  register_setting('google-analyticator', key_ga_extra, '');
83
  register_setting('google-analyticator', key_ga_extra_after, '');
84
  register_setting('google-analyticator', key_ga_outbound, '');
141
  // Update the extra after tracking code
142
  $ga_extra_after = $_POST[key_ga_extra_after];
143
  update_option(key_ga_extra_after, $ga_extra_after);
144
+
145
+ // Update the adsense key
146
+ $ga_adsense = $_POST[key_ga_adsense];
147
+ update_option(key_ga_adsense, $ga_adsense);
148
 
149
  // Update the outbound tracking
150
  $ga_outbound = $_POST[key_ga_outbound];
433
  <p style="margin: 5px 10px;" class="setting-description">Enter a name for the section tracked download links will appear under.</em></p>
434
  </td>
435
  </tr>
436
+ <tr>
437
+ <th valign="top" style="padding-top: 10px;">
438
+ <label for="<?php echo key_ga_adsense; ?>">Google Adsense ID:</label>
439
+ </th>
440
+ <td>
441
+ <?php
442
+ echo "<input type='text' size='50' ";
443
+ echo "name='".key_ga_adsense."' ";
444
+ echo "id='".key_ga_adsense."' ";
445
+ echo "value='".get_option(key_ga_adsense)."' />\n";
446
+ ?>
447
+ <p style="margin: 5px 10px;" class="setting-description">Enter your Google Adsense ID assigned by Google Analytics in this box. This enables Analytics tracking of Adsense information if your Adsense and Analytics accounts are linked. Note: It is highly recommended to not have the Google Analytics tracking code in the footer with this option enabled. Google may be unable to track your Adsense data.</p>
448
+ </td>
449
+ </tr>
450
  <tr>
451
  <th valign="top" style="padding-top: 10px;">
452
  <label for="<?php echo key_ga_extra; ?>">Additional tracking code<br />(before tracker initialization):</label>
535
  if ( ( get_option(key_ga_admin) == ga_enabled || !current_user_can('level_' . get_option(key_ga_admin_level)) ) && get_option(key_ga_admin_disable) == 'remove' || get_option(key_ga_admin_disable) != 'remove' ) {
536
 
537
  echo "<!-- Google Analytics Tracking by Google Analyticator: http://plugins.spiralwebconsulting.com/analyticator.html -->\n";
538
+ # Google Adsense data if enabled
539
+ if ( get_option(key_ga_adsense) != '' )
540
+ echo ' <script type="text/javascript">window.google_analytics_uacct = "' . get_option(key_ga_adsense) . "\";</script>\n\n";
541
+
542
  // Pick the HTTP connection
543
  if ( get_option(key_ga_specify_http) == 'http' ) {
544
  echo " <script type=\"text/javascript\" src=\"http://www.google-analytics.com/ga.js\"></script>\n\n";
google-analytics-stats.php CHANGED
@@ -13,7 +13,7 @@ class GoogleStatsWidget
13
  **/
14
  function GoogleStatsWidget()
15
  {
16
- add_action('init', array($this, 'init'));
17
  }
18
 
19
  /**
@@ -44,13 +44,34 @@ class GoogleStatsWidget
44
  # Make the stats chicklet
45
  $this->initiateBackground($options['pageBg'], $options['font']);
46
  $this->beginWidget($options['font'], $options['widgetBg']);
47
- $this->widgetInfo($this->getUniqueVisitors($options['account']), $options['line1'], $options['line2'], $options['innerBg'], $options['font']);
48
  $this->endWidget();
49
 
50
  # After the widget
51
  echo $after_widget;
52
  }
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  /**
55
  * The settings for the stats widget
56
  **/
@@ -59,13 +80,14 @@ class GoogleStatsWidget
59
  # Get the widget options
60
  $options = get_option('widget_google_stats');
61
  if ( !is_array($options) ) {
62
- $options = array('title'=>'', 'account'=>'', 'pageBg'=>'fff', 'widgetBg'=>'999', 'innerBg'=>'fff', 'font'=>'333', 'line1'=>'Unique', 'line2'=>'Visitors');
63
  }
64
 
65
  # Save the options
66
  if ( $_POST['google-stats-submit'] ) {
67
  $options['title'] = strip_tags(stripslashes($_POST['google_stats_title']));
68
  $options['account'] = strip_tags(stripslashes($_POST['google_stats_account']));
 
69
  $options['pageBg'] = strip_tags(stripslashes($_POST['google_stats_pageBg']));
70
  $options['widgetBg'] = strip_tags(stripslashes($_POST['google_stats_widgetBg']));
71
  $options['innerBg'] = strip_tags(stripslashes($_POST['google_stats_innerBg']));
@@ -78,6 +100,7 @@ class GoogleStatsWidget
78
  # Sanitize widget options
79
  $title = htmlspecialchars($options['title']);
80
  $acnt = htmlspecialchars($options['account']);
 
81
  $pageBg = htmlspecialchars($options['pageBg']);
82
  $widgetBg = htmlspecialchars($options['widgetBg']);
83
  $innerBg = htmlspecialchars($options['innerBg']);
@@ -103,7 +126,9 @@ class GoogleStatsWidget
103
  foreach ( $accounts AS $account ) { $select = ( $acnt == $account['id'] ) ? ' selected="selected"' : ''; echo '<option value="' . $account['id'] . '"' . $select . '>' . $account['title'] . '</option>'; }
104
  else
105
  echo '<option value="">No accounts. Set user/pass in settings.</option>';
106
- echo '</select>';
 
 
107
  # Page background
108
  echo '<p style="text-align:right;"><label for="google_stats_pageBg">' . __('Page background:') . ' <input style="width: 150px;" id="google_stats_pageBg" name="google_stats_pageBg" type="text" value="' . $pageBg . '" /></label></p>';
109
  # Widget background
@@ -186,13 +211,19 @@ class GoogleStatsWidget
186
  * Grabs the cached value of the unique visits for the previous day
187
  *
188
  * @param account - the account to get the unique visitors from
 
189
  * @return void
190
  **/
191
- function getUniqueVisitors($account)
192
  {
193
  # Get the value from the database
194
  $visits = unserialize(get_option('google_stats_visits_' . $account));
195
 
 
 
 
 
 
196
  # Check if the call has been made before
197
  if ( is_array($visits) ) {
198
 
@@ -214,8 +245,9 @@ class GoogleStatsWidget
214
  $stats->setAccount($account);
215
 
216
  # Get the latest stats
217
- $yesterday = date('Y-m-j', strtotime('-1 day'));
218
- $uniques = number_format($stats->getMetric('ga:visitors', $yesterday, $yesterday));
 
219
 
220
  # Make the array for database storage
221
  $visit = serialize(array('unique'=>$uniques, 'lastcalled'=>time()));
13
  **/
14
  function GoogleStatsWidget()
15
  {
16
+ add_action('widgets_init', array($this, 'init'));
17
  }
18
 
19
  /**
44
  # Make the stats chicklet
45
  $this->initiateBackground($options['pageBg'], $options['font']);
46
  $this->beginWidget($options['font'], $options['widgetBg']);
47
+ $this->widgetInfo($this->getUniqueVisitors($options['account'], $options['timeFrame']), $options['line1'], $options['line2'], $options['innerBg'], $options['font']);
48
  $this->endWidget();
49
 
50
  # After the widget
51
  echo $after_widget;
52
  }
53
 
54
+ /**
55
+ * A custom function for outputting the widget
56
+ *
57
+ * @param account - Google Analytics Profile ID found on your profile settings page.
58
+ * (For more info check out http://plugins.spiralwebconsulting.com/forums/viewtopic.php?f=5&t=128)
59
+ * @param timeFrame - The number of previous day's traffic stats used. (Example: 1 = Yesterday, 2 = Last Two Days, etc...)
60
+ * @param line1 - The upper line of text displayed by the widget
61
+ * @param line2 - The lower line of text displayed by the widget
62
+ * @param pageBg - The background color of the entire page in hexadecimal form without the #
63
+ * @param widgetBg - The background color of the widget in hexadecimal form without the #
64
+ * @param innerBg - The background color of the inner part of the widget in hexadecimal form without the #
65
+ * @param font - The font color in hexadecimal form without the #
66
+ **/
67
+ function outputWidget($account, $timeFrame, $line1, $line2, $pageBg, $widgetBg, $innerBg, $font)
68
+ {
69
+ $this->initiateBackground($pageBg, $font);
70
+ $this->beginWidget($font, $widgetBg);
71
+ $this->widgetInfo($this->getUniqueVisitors('ga:' . $account, $timeFrame), $line1, $line2, $innerBg, $font);
72
+ $this->endWidget();
73
+ }
74
+
75
  /**
76
  * The settings for the stats widget
77
  **/
80
  # Get the widget options
81
  $options = get_option('widget_google_stats');
82
  if ( !is_array($options) ) {
83
+ $options = array('title'=>'', 'account'=>'', 'timeFrame'=>'1', 'pageBg'=>'fff', 'widgetBg'=>'999', 'innerBg'=>'fff', 'font'=>'333', 'line1'=>'Unique', 'line2'=>'Visitors');
84
  }
85
 
86
  # Save the options
87
  if ( $_POST['google-stats-submit'] ) {
88
  $options['title'] = strip_tags(stripslashes($_POST['google_stats_title']));
89
  $options['account'] = strip_tags(stripslashes($_POST['google_stats_account']));
90
+ $options['timeFrame'] = strip_tags(stripslashes($_POST['google_stats_timeFrame']));
91
  $options['pageBg'] = strip_tags(stripslashes($_POST['google_stats_pageBg']));
92
  $options['widgetBg'] = strip_tags(stripslashes($_POST['google_stats_widgetBg']));
93
  $options['innerBg'] = strip_tags(stripslashes($_POST['google_stats_innerBg']));
100
  # Sanitize widget options
101
  $title = htmlspecialchars($options['title']);
102
  $acnt = htmlspecialchars($options['account']);
103
+ $timeFrame = htmlspecialchars($options['timeFrame']);
104
  $pageBg = htmlspecialchars($options['pageBg']);
105
  $widgetBg = htmlspecialchars($options['widgetBg']);
106
  $innerBg = htmlspecialchars($options['innerBg']);
126
  foreach ( $accounts AS $account ) { $select = ( $acnt == $account['id'] ) ? ' selected="selected"' : ''; echo '<option value="' . $account['id'] . '"' . $select . '>' . $account['title'] . '</option>'; }
127
  else
128
  echo '<option value="">No accounts. Set user/pass in settings.</option>';
129
+ echo '</select>';
130
+ # Time frame
131
+ echo '<p style="text-align:right;"><label for="google_stats_timeFrame">' . __('Days of data to get:') . ' <input style="width: 150px;" id="google_stats_timeFrame" name="google_stats_timeFrame" type="text" value="' . $timeFrame . '" /></label></p>';
132
  # Page background
133
  echo '<p style="text-align:right;"><label for="google_stats_pageBg">' . __('Page background:') . ' <input style="width: 150px;" id="google_stats_pageBg" name="google_stats_pageBg" type="text" value="' . $pageBg . '" /></label></p>';
134
  # Widget background
211
  * Grabs the cached value of the unique visits for the previous day
212
  *
213
  * @param account - the account to get the unique visitors from
214
+ * @param time - the amount of days to get
215
  * @return void
216
  **/
217
+ function getUniqueVisitors($account, $time = 1)
218
  {
219
  # Get the value from the database
220
  $visits = unserialize(get_option('google_stats_visits_' . $account));
221
 
222
+ # Check to make sure the timeframe is an int and greater than one
223
+ $time = (int) $time;
224
+ if ( $time < 1 )
225
+ $time = 1;
226
+
227
  # Check if the call has been made before
228
  if ( is_array($visits) ) {
229
 
245
  $stats->setAccount($account);
246
 
247
  # Get the latest stats
248
+ $before = date('Y-m-d', strtotime('-' . $time . ' days'));
249
+ $yesterday = date('Y-m-d', strtotime('-1 day'));
250
+ $uniques = number_format($stats->getMetric('ga:visitors', $before, $yesterday));
251
 
252
  # Make the array for database storage
253
  $visit = serialize(array('unique'=>$uniques, 'lastcalled'=>time()));
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: cavemonkey50, spiralwebconsulting
3
  Tags: stats, google, analytics, tracking
4
  Requires at least: 2.7
5
  Tested up to: 2.7
6
- Stable tag: 4.0.1
7
 
8
  Adds the necessary JavaScript code to enable Google Analytics.
9
 
3
  Tags: stats, google, analytics, tracking
4
  Requires at least: 2.7
5
  Tested up to: 2.7
6
+ Stable tag: 4.1
7
 
8
  Adds the necessary JavaScript code to enable Google Analytics.
9