Google Analyticator - Version 1.4

Version Description

Download this release

Release Info

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

Version 1.4

Files changed (2) hide show
  1. google-analyticator.php +322 -0
  2. readme.txt +32 -0
google-analyticator.php ADDED
@@ -0,0 +1,322 @@
1
+ <?php
2
+ /*
3
+ * Plugin Name: Google Analyticator
4
+ * Version: 1.4
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.
8
+ * Author URI: http://cavemonkey50.com/
9
+ */
10
+
11
+ // Constants for enabled/disabled state
12
+ define("ga_enabled", "enabled", true);
13
+ define("ga_disabled", "disabled", true);
14
+
15
+ // Defaults, etc.
16
+ define("key_ga_uid", "ga_uid", true);
17
+ define("key_ga_status", "ga_status", true);
18
+ define("key_ga_admin", "ga_admin_status", true);
19
+ define("key_ga_extra", "ga_extra", true);
20
+ define("key_ga_outbound", "ga_outbound", true);
21
+ define("key_ga_downloads", "ga_downloads", true);
22
+
23
+ define("ga_uid_default", "XX-XXXXX-X", true);
24
+ define("ga_status_default", ga_disabled, true);
25
+ define("ga_admin_default", ga_enabled, true);
26
+ define("ga_extra_default", "", true);
27
+ define("ga_outbound_default", ga_enabled, true);
28
+ define("ga_downloads_default", "", true);
29
+
30
+ // Create the default key and status
31
+ add_option(key_ga_status, ga_status_default, 'If Google Analytics logging in turned on or off.');
32
+ add_option(key_ga_uid, ga_uid_default, 'Your Google Analytics UID.');
33
+ add_option(key_ga_admin, ga_admin_default, 'If WordPress admins are counted in Google Analytics.');
34
+ add_option(key_ga_extra, ga_extra_default, 'Addition Google Analytics tracking options');
35
+ add_option(key_ga_outbound, ga_outbound_default, 'Add tracking of outbound links');
36
+ add_option(key_ga_downloads, ga_downloads_default, 'Download extensions to track with Google Analyticator');
37
+
38
+ // Create a option page for settings
39
+ add_action('admin_menu', 'add_ga_option_page');
40
+
41
+ // Hook in the options page function
42
+ function add_ga_option_page() {
43
+ global $wpdb;
44
+ add_options_page('Google Analyticator Options', 'Google Analytics', 8, basename(__FILE__), 'ga_options_page');
45
+ }
46
+
47
+ function ga_options_page() {
48
+ // If we are a postback, store the options
49
+ if (isset($_POST['info_update'])) {
50
+ check_admin_referer();
51
+
52
+ // Update the status
53
+ $ga_status = $_POST[key_ga_status];
54
+ if (($ga_status != ga_enabled) && ($ga_status != ga_disabled))
55
+ $ga_status = ga_status_default;
56
+ update_option(key_ga_status, $ga_status);
57
+
58
+ // Update the UID
59
+ $ga_uid = $_POST[key_ga_uid];
60
+ if ($ga_uid == '')
61
+ $ga_uid = ga_uid_default;
62
+ update_option(key_ga_uid, $ga_uid);
63
+
64
+ // Update the admin logging
65
+ $ga_admin = $_POST[key_ga_admin];
66
+ if (($ga_admin != ga_enabled) && ($ga_admin != ga_disabled))
67
+ $ga_admin = ga_admin_default;
68
+ update_option(key_ga_admin, $ga_admin);
69
+
70
+ // Update the extra tracking code
71
+ $ga_extra = $_POST[key_ga_extra];
72
+ update_option(key_ga_extra, $ga_extra);
73
+
74
+ // Update the outbound tracking
75
+ $ga_outbound = $_POST[key_ga_outbound];
76
+ if (($ga_outbound != ga_enabled) && ($ga_outbound != ga_disabled))
77
+ $ga_outbound = ga_outbound_default;
78
+ update_option(key_ga_outbound, $ga_outbound);
79
+
80
+ // Update the download tracking code
81
+ $ga_downloads = $_POST[key_ga_downloads];
82
+ update_option(key_ga_downloads, $ga_downloads);
83
+
84
+ // Give an updated message
85
+ echo "<div class='updated'><p><strong>Google Analyticator options updated</strong></p></div>";
86
+ }
87
+
88
+ // Output the options page
89
+ ?>
90
+
91
+ <form method="post" action="options-general.php?page=google-analyticator.php">
92
+ <div class="wrap">
93
+ <h2>Google Analyticator Options</h2>
94
+ <fieldset class='options'>
95
+ <legend>Basic Options</legend>
96
+ <?php if (get_option(key_ga_status) == ga_disabled) { ?>
97
+ <div style="margin:10px auto; border:3px #f00 solid; background-color:#fdd; color:#000; padding:10px; text-align:center;">
98
+ Google Analytics integration is currently <strong>DISABLED</strong>.
99
+ </div>
100
+ <?php } ?>
101
+ <?php if ((get_option(key_ga_uid) == "XX-XXXXX-X") && (get_option(key_ga_status) != ga_disabled)) { ?>
102
+ <div style="margin:10px auto; border:3px #f00 solid; background-color:#fdd; color:#000; padding:10px; text-align:center;">
103
+ Google Analytics integration is currently enabled, but you did not enter a UID. Tracking will not occur.
104
+ </div>
105
+ <?php } ?>
106
+ <table class="editform" cellspacing="2" cellpadding="5" width="100%">
107
+ <tr>
108
+ <th width="30%" valign="top" style="padding-top: 10px;">
109
+ <label for="<?php echo key_ga_status ?>">Google Analytics logging is:</label>
110
+ </th>
111
+ <td>
112
+ <?php
113
+ echo "<select name='".key_ga_status."' id='".key_ga_status."'>\n";
114
+
115
+ echo "<option value='".ga_enabled."'";
116
+ if(get_option(key_ga_status) == ga_enabled)
117
+ echo " selected='selected'";
118
+ echo ">Enabled</option>\n";
119
+
120
+ echo "<option value='".ga_disabled."'";
121
+ if(get_option(key_ga_status) == ga_disabled)
122
+ echo" selected='selected'";
123
+ echo ">Disabled</option>\n";
124
+
125
+ echo "</select>\n";
126
+ ?>
127
+ </td>
128
+ </tr>
129
+ <tr>
130
+ <th valign="top" style="padding-top: 10px;">
131
+ <label for="<?php echo key_ga_uid; ?>">Your Google Analytics' UID:</label>
132
+ </th>
133
+ <td>
134
+ <?php
135
+ echo "<input type='text' size='50' ";
136
+ echo "name='".key_ga_uid."' ";
137
+ echo "id='".key_ga_uid."' ";
138
+ echo "value='".get_option(key_ga_uid)."' />\n";
139
+ ?>
140
+ <p style="margin: 5px 10px;">Enter your Google Analytics' UID in this box. The UID is needed for Google Analytics to log your website stats. Your UID can be found by looking in the JavaScript Google Analytics gives you to put on your page. Look for your UID in between <strong>_uacct = "UA-11111-1";</strong> in the JavaScript. In this example you would put <strong>UA-11111-1</strong> in the UID box.</p>
141
+ </td>
142
+ </tr>
143
+ </table>
144
+ </fieldset>
145
+ <fieldset class='options'>
146
+ <legend>Advanced Options</legend>
147
+ <table class="editform" cellspacing="2" cellpadding="5" width="100%">
148
+ <tr>
149
+ <th width="30%" valign="top" style="padding-top: 10px;">
150
+ <label for="<?php echo key_ga_admin ?>">WordPress admin logging:</label>
151
+ </th>
152
+ <td>
153
+ <?php
154
+ echo "<select name='".key_ga_admin."' id='".key_ga_admin."'>\n";
155
+
156
+ echo "<option value='".ga_enabled."'";
157
+ if(get_option(key_ga_admin) == ga_enabled)
158
+ echo " selected='selected'";
159
+ echo ">Enabled</option>\n";
160
+
161
+ echo "<option value='".ga_disabled."'";
162
+ if(get_option(key_ga_admin) == ga_disabled)
163
+ echo" selected='selected'";
164
+ echo ">Disabled</option>\n";
165
+
166
+ echo "</select>\n";
167
+ ?>
168
+ <p style="margin: 5px 10px;">Disabling this option will prevent all logged in WordPress admins from showing up on your Google Analytics reports. A WordPress admin is defined as a user with a level 8 or higher. Your user level is <?php global $user_level; echo $user_level; ?>.</p>
169
+ </td>
170
+ </tr>
171
+ <tr>
172
+ <th width="30%" valign="top" style="padding-top: 10px;">
173
+ <label for="<?php echo key_ga_outbound ?>">Outbound link tracking:</label>
174
+ </th>
175
+ <td>
176
+ <?php
177
+ echo "<select name='".key_ga_outbound."' id='".key_ga_outbound."'>\n";
178
+
179
+ echo "<option value='".ga_enabled."'";
180
+ if(get_option(key_ga_outbound) == ga_enabled)
181
+ echo " selected='selected'";
182
+ echo ">Enabled</option>\n";
183
+
184
+ echo "<option value='".ga_disabled."'";
185
+ if(get_option(key_ga_outbound) == ga_disabled)
186
+ echo" selected='selected'";
187
+ echo ">Disabled</option>\n";
188
+
189
+ echo "</select>\n";
190
+ ?>
191
+ <p style="margin: 5px 10px;">Disabling this option will turn off the tracking of outbound links. It's recommended not to disable this option unless you're a privacy advocate (now why would you be using Google Analytics in the first place?) or it's causing some kind of weird issue.</p>
192
+ </td>
193
+ </tr>
194
+ <tr>
195
+ <th valign="top" style="padding-top: 10px;">
196
+ <label for="<?php echo key_ga_extra; ?>">Additional tracking code:</label>
197
+ </th>
198
+ <td>
199
+ <?php
200
+ echo "<input type='text' size='50' ";
201
+ echo "name='".key_ga_extra."' ";
202
+ echo "id='".key_ga_extra."' ";
203
+ echo "value='".stripslashes(get_option(key_ga_extra))."' />\n";
204
+ ?>
205
+ <p style="margin: 5px 10px;">Enter any additional bits of tracking code that you would like to include in the script. For example to track a subdomain in your main domain's profile you would add <strong>_udn="example.com";</strong>. Addition tracking code information can be found in <a href="http://www.google.com/support/analytics/">Google Analytic's FAQ</a>.</p>
206
+ </td>
207
+ </tr>
208
+ <tr>
209
+ <th valign="top" style="padding-top: 10px;">
210
+ <label for="<?php echo key_ga_downloads; ?>">Download extensions to track:</label>
211
+ </th>
212
+ <td>
213
+ <?php
214
+ echo "<input type='text' size='50' ";
215
+ echo "name='".key_ga_downloads."' ";
216
+ echo "id='".key_ga_downloads."' ";
217
+ echo "value='".stripslashes(get_option(key_ga_downloads))."' />\n";
218
+ ?>
219
+ <p style="margin: 5px 10px;">Enter any extensions of files you would like to be tracked as a download. For example to track all MP3s and PDFs enter <strong>mp3,pdf</strong>. <em>Outbound link tracking must be enabled for downloads to be tracked.</em></p>
220
+ </td>
221
+ </tr>
222
+ </table>
223
+ </fieldset>
224
+ <p class="submit">
225
+ <input type='submit' name='info_update' value='Update Options' />
226
+ </p>
227
+ </div>
228
+ </form>
229
+
230
+ <?php
231
+ }
232
+
233
+ // Add the script
234
+ add_action('wp_head', 'add_google_analytics');
235
+
236
+ // Add the ougoing links script
237
+ function outgoing_links() {
238
+ if (get_option(key_ga_outbound) == ga_enabled) {
239
+ add_filter('comment_text', 'ga_outgoing');
240
+ add_filter('get_comment_author_link', 'ga_outgoing_comment_author');
241
+ add_filter('the_content', 'ga_outgoing');
242
+ add_filter('the_excerpt', 'ga_outgoing');
243
+ }
244
+ }
245
+
246
+ // The guts of the Google Analytics script
247
+ function add_google_analytics() {
248
+ global $user_level;
249
+ $uid = get_option(key_ga_uid);
250
+ $extra = stripslashes(get_option(key_ga_extra));
251
+ if ((get_option(key_ga_status) != ga_disabled) && ($uid != "XX-XXXXX-X")) {
252
+ if ((get_option(key_ga_admin) == ga_enabled) || ((get_option(key_ga_admin) == ga_disabled) && ($user_level < 8))) {
253
+ echo "<!-- Google Analytics Tracking by Google Analyticator: http://cavemonkey50.com/code/google-analyticator/ -->\n";
254
+ echo " <script src=\"http://www.google-analytics.com/urchin.js\" type=\"text/javascript\"></script><script type=\"text/javascript\"> _uacct=\"$uid\"; $extra urchinTracker(); </script>\n";
255
+ outgoing_links();
256
+ }
257
+ }
258
+ }
259
+
260
+ // Finds all the links contained in a post or comment
261
+ function ga_outgoing($input) {
262
+ static $link_pattern = '/<a (.*?)href="(.*?)\/\/(.*?)"(.*?)>(.*?)<\/a>/i';
263
+ static $link_pattern_2 = '/<a (.*?)href=\'(.*?)\/\/(.*?)\'(.*?)>(.*?)<\/a>/i';
264
+ $input = preg_replace_callback($link_pattern, ga_parse_link, $input);
265
+ $input = preg_replace_callback($link_pattern_2, ga_parse_link, $input);
266
+ return $input;
267
+ }
268
+
269
+ // Takes the comment author link and adds the Google outgoing tracking code
270
+ function ga_outgoing_comment_author($input) {
271
+ static $link_pattern = '(.*href\s*=\s*)[\"\']*(.*)[\"\'] (.*)';
272
+ ereg($link_pattern, $input, $matches);
273
+ if ($matches[2] == "") return $input;
274
+
275
+ $target = ga_find_domain($matches[2]);
276
+ $local_host = ga_find_domain($_SERVER["HTTP_HOST"]);
277
+ if ( $target["domain"] != $local_host["domain"] ){
278
+ $tracker_code .= " onclick=\"javascript:urchinTracker ('/outbound/".$target["host"]."');\" ";
279
+ }
280
+ return $matches[1] . "\"" . $matches[2] . "\"" . $tracker_code . $matches[3];
281
+ }
282
+
283
+ // Takes a link and adds the Google outgoing tracking code
284
+ function ga_parse_link($matches){
285
+ $local_host = ga_find_domain($_SERVER["HTTP_HOST"]);
286
+ $target = ga_find_domain($matches[3]);
287
+ $url = $matches[3];
288
+ $file_extension = strtolower(substr(strrchr($url,"."),1));
289
+ if ( $target["domain"] != $local_host["domain"] ){
290
+ $tracker_code .= " onclick=\"javascript:urchinTracker ('/outbound/".$target["host"]."');\"";
291
+ }
292
+ if ( ($target["domain"] == $local_host["domain"]) && (ga_check_download($file_extension)) ){
293
+ $url = strtolower(substr(strrchr($url,"/"),1));
294
+ $tracker_code .= " onclick=\"javascript:urchinTracker ('/downloads/".$file_extension."/".$url."');\"";
295
+ }
296
+ return '<a href="' . $matches[2] . '//' . $matches[3] . '"' . $matches[1] . $matches[4].$tracker_code.'>' . $matches[5] . '</a>';
297
+ }
298
+
299
+ // Checks to see if the link is on your site
300
+ function ga_find_domain($url){
301
+ $host_pattern = "/^(http:\/\/)?([^\/]+)/i";
302
+ $domain_pattern = "/[^\.\/]+\.[^\.\/]+#x2F;";
303
+
304
+ preg_match($host_pattern, $url, $matches);
305
+ $host = $matches[2];
306
+ preg_match($domain_pattern, $host, $matches);
307
+ return array("domain"=>$matches[0],"host"=>$host);
308
+ }
309
+
310
+ // Checks to see if the requested URL is a download
311
+ function ga_check_download($file_extension){
312
+ if (get_option(key_ga_downloads)){
313
+ $extensions = explode(',', get_option(key_ga_downloads));
314
+
315
+ foreach ($extensions as $extension) {
316
+ if ($extension == $file_extension)
317
+ return true;
318
+ }
319
+ }
320
+ }
321
+
322
+ ?>
readme.txt ADDED
@@ -0,0 +1,32 @@
1
+ == Google Analyticator ==
2
+
3
+ Google Analyticator adds the necessary JavaScript code to enable Google Analytics (http://google.com/analytics) logging. This eliminates the need to edit your template code to begin logging.
4
+
5
+ == Installation ==
6
+ Drop the google-analyticator.php file into /wp-content/plugins/, and activate the plugin.
7
+
8
+ == Usage ==
9
+ In your WordPress administration page go to Options > Google Analytics. From there enter your UID and enable logging. Information on how to obtain your UID can be found on the options page.
10
+
11
+ Once you save your settings, the JavaScript code should now be appearing on all of your WordPress pages.
12
+
13
+ == Version History ==
14
+ 1.4 - Adds download tracking.
15
+
16
+ 1.31 - Fixes a small bug with backslashes in the additional tracking code box.
17
+
18
+ 1.3 - WordPress 2.0 beta is now supported. Also, the missing options page bug should be fixed for good.
19
+
20
+ 1.2 - Added support for outbound links.
21
+
22
+ 1.12 - Try number two at fixing missing option page bug.
23
+
24
+ 1.11 - Hopefully fixed a bug where options page would sometimes not display.
25
+
26
+ 1.1 - Added an option to disable administrator logging, and added an option to add additional tracking code that Google has.
27
+
28
+ 1.0 - Initial Release
29
+
30
+ == Credits ==
31
+ Copyright (c) 2005 Ronald Heft, Jr. (ron@cavemonkey50.com)
32
+ Released under the terms of the GPL