Google Analyticator - Version 4.0

Version Description

Download this release

Release Info

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

Code changes from version 3.0.3 to 4.0

class.analytics.stats.php ADDED
@@ -0,0 +1,141 @@
1
+ <?php
2
+
3
+ # Include SimplePie if it doesn't exist
4
+ if ( !class_exists('SimplePie') )
5
+ require_once('simplepie.inc');
6
+
7
+ /**
8
+ * Handles interactions with Google Analytics' Stat API
9
+ *
10
+ * @author Spiral Web Consulting
11
+ **/
12
+ class GoogleAnalyticsStats
13
+ {
14
+
15
+ # Class variables
16
+ var $baseFeed = 'https://www.google.com/analytics/feeds';
17
+ var $accountId;
18
+ var $token = false;
19
+
20
+ /**
21
+ * Constructor
22
+ *
23
+ * @param user - the google account's username
24
+ * @param pass - the google account's password
25
+ **/
26
+ function GoogleAnalyticsStats($user, $pass)
27
+ {
28
+ # Encode the login details for sending over HTTP
29
+ $user = urlencode($user);
30
+ $pass = urlencode($pass);
31
+
32
+ # Request authentication with Google
33
+ $response = $this->curl('https://www.google.com/accounts/ClientLogin', 'accountType=GOOGLE&Email=' . $user . '&Passwd=' . $pass);
34
+
35
+ # Get the authentication token
36
+ $this->token = substr(strstr($response, "Auth="), 5);
37
+ }
38
+
39
+ /**
40
+ * Connects over cURL to get data
41
+ *
42
+ * @param url - url to request
43
+ * @param post - post data to pass through curl
44
+ * @return the raw curl response
45
+ **/
46
+ function curl($url, $post = false, $header = 1)
47
+ {
48
+ $curl = curl_init();
49
+ curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
50
+ curl_setopt($curl, CURLOPT_HEADER, $header);
51
+ curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
52
+ curl_setopt($curl, CURLOPT_URL, $url);
53
+
54
+ # Include the authentication token if known
55
+ if ( $this->token ) {
56
+ curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: GoogleLogin auth=' . $this->token));
57
+ }
58
+
59
+ # Include optional post fields
60
+ if ( $post ) {
61
+ $post .= '&service=analytics&source=wp-google-stats';
62
+ curl_setopt($curl, CURLOPT_POST, 1);
63
+ curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
64
+ }
65
+
66
+ return curl_exec($curl);
67
+ }
68
+
69
+ /**
70
+ * Sets the account id to use for queries
71
+ *
72
+ * @param id - the account id
73
+ **/
74
+ function setAccount($id)
75
+ {
76
+ $this->accountId = $id;
77
+ }
78
+
79
+ /**
80
+ * Get a list of Analytics accounts
81
+ *
82
+ * @return a list of analytics accounts
83
+ **/
84
+ function getAnalyticsAccounts()
85
+ {
86
+ # Request the list of accounts
87
+ $response = $this->curl($this->baseFeed . '/accounts/default', false, '0');
88
+
89
+ # Parse the XML using SimplePie
90
+ $simplePie = new SimplePie();
91
+ $simplePie->set_raw_data($response);
92
+ $simplePie->init();
93
+ $simplePie->handle_content_type();
94
+ $accounts = $simplePie->get_items();
95
+
96
+ # Make an array of the accounts
97
+ $ids = array();
98
+ foreach ( $accounts AS $account ) {
99
+ $id = array();
100
+
101
+ $item_info = $account->get_item_tags('http://schemas.google.com/analytics/2009', 'tableId');
102
+
103
+ $id['title'] = $account->get_title();
104
+ $id['id'] = $item_info[0]['data'];
105
+
106
+ $ids[] = $id;
107
+ }
108
+
109
+ return $ids;
110
+ }
111
+
112
+ /**
113
+ * Get a specific data metric
114
+ *
115
+ * @param metric - the metric to get
116
+ * @param startDate - the start date to get
117
+ * @param endDate - the end date to get
118
+ * @return the specific metric
119
+ **/
120
+ function getMetric($metric, $startDate, $endDate)
121
+ {
122
+ # Request the list of accounts
123
+ $response = $this->curl($this->baseFeed . "/data?ids=$this->accountId&start-date=$startDate&end-date=$endDate&metrics=$metric", false, '0');
124
+
125
+ # Parse the XML using SimplePie
126
+ $simplePie = new SimplePie();
127
+ $simplePie->set_raw_data($response);
128
+ $simplePie->init();
129
+ $simplePie->handle_content_type();
130
+ $datas = $simplePie->get_items();
131
+
132
+ # Read out the data until the metric is found
133
+ foreach ( $datas AS $data ) {
134
+ $data_tag = $data->get_item_tags('http://schemas.google.com/analytics/2009', 'metric');
135
+ return $data_tag[0]['attribs']['']['value'];
136
+ }
137
+ }
138
+
139
+ } // END class
140
+
141
+ ?>
ga_logo.png ADDED
Binary file
google-analyticator.php CHANGED
@@ -1,13 +1,17 @@
1
<?php
2
/*
3
* Plugin Name: Google Analyticator
4
- * Version: 3.0.3
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
8
* Author URI: http://spiralwebconsulting.com/
9
*/
10
11
// Constants for enabled/disabled state
12
define("ga_enabled", "enabled", true);
13
define("ga_disabled", "disabled", true);
@@ -165,6 +169,10 @@ function ga_options_page() {
165
if ( $ga_specify_http == '' )
166
$ga_specify_http = 'auto';
167
update_option(key_ga_specify_http, $ga_specify_http);
168
169
// Give an updated message
170
echo "<div class='updated fade'><p><strong>Google Analyticator settings saved.</strong></p></div>";
@@ -233,6 +241,24 @@ function ga_options_page() {
233
</table>
234
<h3>Advanced Settings</h3>
235
<table class="form-table" cellspacing="2" cellpadding="5" width="100%">
236
<tr>
237
<th width="30%" valign="top" style="padding-top: 10px;">
238
<label for="<?php echo key_ga_admin ?>">WordPress admin logging:</label>
1
<?php
2
/*
3
* Plugin Name: Google Analyticator
4
+ * Version: 4.0
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
8
* Author URI: http://spiralwebconsulting.com/
9
*/
10
11
+ # Include Google Analytics Stats widget
12
+ require_once('google-analytics-stats.php');
13
+ $google_analytics_stats = new GoogleStatsWidget();
14
+
15
// Constants for enabled/disabled state
16
define("ga_enabled", "enabled", true);
17
define("ga_disabled", "disabled", true);
169
if ( $ga_specify_http == '' )
170
$ga_specify_http = 'auto';
171
update_option(key_ga_specify_http, $ga_specify_http);
172
+
173
+ # Update the stat options
174
+ update_option('google_stats_user', $_POST['google_stats_user']);
175
+ update_option('google_stats_password', $_POST['google_stats_password']);
176
177
// Give an updated message
178
echo "<div class='updated fade'><p><strong>Google Analyticator settings saved.</strong></p></div>";
241
</table>
242
<h3>Advanced Settings</h3>
243
<table class="form-table" cellspacing="2" cellpadding="5" width="100%">
244
+ <tr valign="top">
245
+ <th scope="row">
246
+ <label for="google_stats_user">Google Username:</label>
247
+ </th>
248
+ <td>
249
+ <input type="text" size="40" name="google_stats_user" id="google_stats_user" value="<?php echo stripslashes(get_option('google_stats_user')); ?>" />
250
+ <br /><span class="setting-description">Your Google Analytics account's username. This is needed to authenticate with Google for use with the stats widget.</span>
251
+ </td>
252
+ </tr>
253
+ <tr valign="top">
254
+ <th scope="row">
255
+ <label for="google_stats_password">Google Password:</label>
256
+ </th>
257
+ <td>
258
+ <input type="password" size="40" name="google_stats_password" id="google_stats_password" value="<?php echo stripslashes(get_option('google_stats_password')); ?>" />
259
+ <br /><span class="setting-description">Your Google Analytics account's password. This is needed to authenticate with Google for use with the stats widget.</span>
260
+ </td>
261
+ </tr>
262
<tr>
263
<th width="30%" valign="top" style="padding-top: 10px;">
264
<label for="<?php echo key_ga_admin ?>">WordPress admin logging:</label>
google-analytics-stats.php ADDED
@@ -0,0 +1,232 @@
1
+ <?php
2
+
3
+ /**
4
+ * The Google Analytics Stats Widget
5
+ *
6
+ * @author Spiral Web Consulting
7
+ **/
8
+ class GoogleStatsWidget
9
+ {
10
+
11
+ /**
12
+ * Start the Google Stats Widget
13
+ **/
14
+ function GoogleStatsWidget()
15
+ {
16
+ add_action('init', array($this, 'init'));
17
+ }
18
+
19
+ /**
20
+ * Register the widget with WordPress
21
+ **/
22
+ function init()
23
+ {
24
+ register_sidebar_widget('Google Analytics Stats', array($this, 'statsWidget'));
25
+ register_widget_control('Google Analytics Stats', array($this, 'widgetControl'), 400, 400);
26
+ }
27
+
28
+ /**
29
+ * The widget output code
30
+ **/
31
+ function statsWidget($args)
32
+ {
33
+ extract($args);
34
+
35
+ # Get the options
36
+ $options = get_option('widget_google_stats');
37
+
38
+ # Before the widget
39
+ echo $before_widget;
40
+
41
+ # The title
42
+ echo $before_title . $options['title'] . $after_title;
43
+
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
+ **/
57
+ function widgetControl()
58
+ {
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']));
72
+ $options['font'] = strip_tags(stripslashes($_POST['google_stats_font']));
73
+ $options['line1'] = strip_tags(stripslashes($_POST['google_stats_line1']));
74
+ $options['line2'] = strip_tags(stripslashes($_POST['google_stats_line2']));
75
+ update_option('widget_google_stats', $options);
76
+ }
77
+
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']);
84
+ $font = htmlspecialchars($options['font']);
85
+ $line1 = htmlspecialchars($options['line1']);
86
+ $line2 = htmlspecialchars($options['line2']);
87
+
88
+ # Get the class for interacting with the Google Analytics
89
+ require_once('class.analytics.stats.php');
90
+
91
+ # Create a new Gdata call
92
+ $stats = new GoogleAnalyticsStats(stripslashes(get_option('google_stats_user')), stripslashes(get_option('google_stats_password')), true);
93
+
94
+ # Get a list of accounts
95
+ $accounts = $stats->getAnalyticsAccounts();
96
+
97
+ # Output the options
98
+ echo '<p style="text-align:right;"><label for="google_stats_title">' . __('Title:') . ' <input style="width: 250px;" id="google_stats_title" name="google_stats_title" type="text" value="' . $title . '" /></label></p>';
99
+ # The list of accounts
100
+ echo '<p style="text-align:right;"><label for="google_stats_account">' . __('Analytics account: ');
101
+ echo '<select name="google_stats_account" style="margin-top: -3px; margin-bottom: 10px;">';
102
+ if ( count($accounts) > 0 )
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
110
+ echo '<p style="text-align:right;"><label for="google_stats_widgetBg">' . __('Widget background:') . ' <input style="width: 150px;" id="google_stats_widgetBg" name="google_stats_widgetBg" type="text" value="' . $widgetBg . '" /></label></p>';
111
+ # Inner background
112
+ echo '<p style="text-align:right;"><label for="google_stats_innerBg">' . __('Inner background:') . ' <input style="width: 150px;" id="google_stats_innerBg" name="google_stats_innerBg" type="text" value="' . $innerBg . '" /></label></p>';
113
+ # Font color
114
+ echo '<p style="text-align:right;"><label for="google_stats_font">' . __('Font color:') . ' <input style="width: 150px;" id="google_stats_font" name="google_stats_font" type="text" value="' . $font . '" /></label></p>';
115
+ # Text line 1
116
+ echo '<p style="text-align:right;"><label for="google_stats_line1">' . __('Line 1 text:') . ' <input style="width: 200px;" id="google_stats_line1" name="google_stats_line1" type="text" value="' . $line1 . '" /></label></p>';
117
+ # Text line 2
118
+ echo '<p style="text-align:right;"><label for="google_stats_line2">' . __('Line 2 text:') . ' <input style="width: 200px;" id="google_stats_line2" name="google_stats_line2" type="text" value="' . $line2 . '" /></label></p>';
119
+ # Mark the form as updated
120
+ echo '<input type="hidden" id="google-stats-submit" name="google-stats-submit" value="1" />';
121
+ }
122
+
123
+ /**
124
+ * This function is used to display the background color behind the widget. This is necessary
125
+ * for the Google Analytics text to have the same background color as the page.
126
+ *
127
+ * @param $font_color - Hexadecimal value for the font color used within the Widget (does not effect "Powered By Google Analytics Text"). This effects border color as well.
128
+ * @param $page_background_color - Hexadecimal value for the page background color
129
+ * @return void
130
+ **/
131
+ function initiateBackground($page_background_color = 'FFF', $font_color = '000')
132
+ {
133
+ echo '<br />';
134
+ echo '<div style="background:#' . $page_background_color . ';font-size:12px;color:#' . $font_color . ';font-family:"Lucida Grande",Helvetica,Verdana,Sans-Serif;">';
135
+ }
136
+
137
+ /**
138
+ * This function starts the widget. The font color and widget background color are customizable.
139
+ *
140
+ * @param $font_color - Hexadecimal value for the font color used within the Widget (does not effect "Powered By Google Analytics Text"). This effects border color as well.
141
+ * @param $widget_background_color - Hexadecimal value for the widget background color.
142
+ * @return void
143
+ **/
144
+ function beginWidget($font_color = '000', $widget_background_color = 'FFF')
145
+ {
146
+ echo '<table style="width:auto!important;border-width:2px;border-color:#' . $font_color . ';border-style:solid;background:#' . $widget_background_color . ';"><tr">';
147
+ }
148
+
149
+ /**
150
+ * This function encases the text that appears on the right hand side of the widget.
151
+ * Both lines of text are customizable by each individual user.
152
+ *
153
+ * It also displays the visitor count that was pulled from the user's Google Analytics account.
154
+ *
155
+ * @param $visitor_count - Number of unique visits to the site pulled from the user's Google Analytics account.
156
+ * @param $line_one - First line of text displayed on the right hand side of the widget.
157
+ * @param $line_two - Second line of text displayed on the right hand side of the widget.
158
+ * @param $inner_background_color - Hexadecimal value for the background color that surrounds the Visitor Count.
159
+ * @param $font_color - Hexadecimal value for the font color used within the Widget (does not effect "Powered By Google Analytics Text"). This effects border color as well
160
+ * @return void
161
+ **/
162
+ function widgetInfo($visitor_count, $line_one = 'Unique', $line_two = 'Visitors', $inner_background_color = 'FFF', $font_color = '000')
163
+ {
164
+
165
+ echo '<td style="width:auto!important;border-width:1px;border-color:#' . $font_color . ';border-style:solid;padding:0px 5px 0px 5px;text-align:right;background:#' . $inner_background_color . ';min-width:80px;*width:80px!important;"><div style="min-width:80px;">'. $visitor_count . '</div></td>';
166
+
167
+ echo '<td style="width:auto!important;padding:0px 5px 0px 5px;text-align:center;font-size:11px;">' . $line_one . '<br />' . $line_two . '</td>';
168
+
169
+ }
170
+
171
+ /**
172
+ * The function is used strictly for visual appearance. It also displays the Google Analytics text.
173
+ *
174
+ * @return void
175
+ **/
176
+ function endWidget()
177
+ {
178
+ // This closes off the widget.
179
+ echo '</tr></table>';
180
+
181
+ // The following is used to displayed the "Powered By Google Anayltics" text.
182
+ echo '<div style="font-size:9px;color:#666666;margin-top:0px;font-family:Verdana;">Powered By <a href="http://google.com/analytics/" alt="Google Analytics" style="text-decoration:none;"><img src="' . get_option('home') . '/wp-content/plugins/google-analyticator/ga_logo.png" alt="Google Analytics" style="border:0px;position:relative;top:4px;" /></a></div></div>';
183
+ }
184
+
185
+ /**
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
+
199
+ # Check if the last time called was within two hours, if so, return that
200
+ if ( $visits['lastcalled'] > ( time() - 7200 ) )
201
+ return $visits['unique'];
202
+
203
+ }
204
+
205
+ # If here, the call has not been made or it is expired
206
+
207
+ # Get the class for interacting with the Google Analytics
208
+ require_once('class.analytics.stats.php');
209
+
210
+ # Create a new Gdata call
211
+ $stats = new GoogleAnalyticsStats(stripslashes(get_option('google_stats_user')), stripslashes(get_option('google_stats_password')), true);
212
+
213
+ # Set the account to the one requested
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()));
222
+
223
+ # Store the array
224
+ update_option('google_stats_visits_' . $account, $visit);
225
+
226
+ # Return the visits
227
+ return $uniques;
228
+ }
229
+
230
+ } // END class
231
+
232
+ ?>
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: 3.0.3
7
8
Adds the necessary JavaScript code to enable Google Analytics.
9
@@ -17,6 +17,7 @@ Google Analyticator adds the necessary JavaScript code to enable Google Analytic
17
18
Google Analyticator Has the Following Features:
19
20
- Standard Google Analytics tracking support
21
- External link tracking of all links on the page, including links not managed by WordPress
22
- Download link tracking
3
Tags: stats, google, analytics, tracking
4
Requires at least: 2.7
5
Tested up to: 2.7
6
+ Stable tag: 4.0
7
8
Adds the necessary JavaScript code to enable Google Analytics.
9
17
18
Google Analyticator Has the Following Features:
19
20
+ - **NEW:** Google Analytics API support. Includes a stats widget showing yesterday's visitors. More stats to come!
21
- Standard Google Analytics tracking support
22
- External link tracking of all links on the page, including links not managed by WordPress
23
- Download link tracking
simplepie.inc ADDED
@@ -0,0 +1,13672 @@