Google Analytics Plugin for WordPress

This is the first beta release of a WordPress plugin that can add Google Analytics to your website without you needing to code one single set of <>’s.
I have it running on this site and it seems to be working just fine so far.
Installation instructions
To use it:
- If you have a previous version of the plugin, delete it.
Download this file- Rename the file
googleanalytics.phpand copy it to your/wp-content/pluginsdirectory. - Enable it on the plugins page.
- Enter your Google Analytics User Account string (it’ll be something like UA-12345-6. It appears when you “Add a channel”)
- That’s it.
You don’t need to look alter the code in any way to make it run; of course you’re welcome to improve it and send me any updates for inclusion.
Features
- Zero Coding
A zero coding install enables both the default tracking and the use of different channels for specific posts. - Outgoing links
Track which outgoing links your users click on – i.e. the ones that don’t traditionally register in your logs. Separate tracking streams for outbound links that are in the main article, comments, and comment author URL’s
- Multiple Channels
Assign a specific channel to any article by entering the channel ID as metadata. i.e. simply type “analytics” as the metadata field name and the channel ID as the value and you can run a channel for each specific post
Requested Features
- Filtering the Sidebar
WordPress provides hooks for filtering articles and comments, but not for filtering the sidebar and footer. Since the sidebar often includes the blogroll there are page-exit clicks that cannot be tracked. If anyoen has a clean solution to filtering the sidebar then please speak up! - 100% Point’n'Click UI
I’ve asked on the forums to see if there’s away to automate the retrieval of the account id, so hopefully the plugin can become a configuration free install.
Assign a different channel ID to each category.
Versions
A historical list of releases.
Contribute
Please neatly tuck any feedback, comments, suggestions & requests onto the forum!
329 Comments
You have a bug on line 79:
echo(“_uacct = “$uadtring”;\n”);
should be:
echo(“_uacct = “$uastring”;\n”);
Version 0.30 adds rudimentary support for tracking exit pages (described here). For any link that the user selects which is in a different domain to the source blog, a record of /outgoing/hostname is stored.
Thanks Troy. Version 0.31 which fixes that is now the latest version.
Thanks for the quick updates on this!
could you elaborate re: exit page tracking? how rudimentary is it?
Ah… I see. Outbound link tracking is enabled for links appearing within the loop, but not the sidebar, for example. I’ll be looking forward to an update that adds support for that, as I’m sure will others. It’ll be interesting to see how one’s blogroll is directing traffic.
It seems like we both wrote almost identical plugins at the same time, lol. I had finished writing my Google Analyticator around 4pm. What time did you finish yours, because our code is almost identical.
Hi C – a great idea re the blogroll – I’ll look into enabling it for the whole page.
Cavemonkey, yo, yeah, your’s is the 5th plugin I’m now aware of! I think we ought to get at least one of them into SVN and centralise efforts!
Hi
I’ve tried to install and configure your plugin, but when I’m trying to validate the setup on Google’s page it says that it can’t detect the tracking code.
Hi Dado, I’ll need a little more information to begin with. What’s the URL of the blog where you’re installing the plugin? I’m guessing it’s the one in your email address since that site does indeed have the tracking code present, with the UA number of UA-119977-1. So my next question is, can you confirm that 119977 is correct? The codes I’ve seen so far have only had 5 digits in the middle section (but then, Google have been very busy so this may have increased massively. From what I can see so far it looks like it’s working properly, so it may be a case of sit and wait whilst Google catches up with you.
The plugin doesn’t seem to work on my block either.
Cant finde any code inserted in the mail page between the header tags?
Any ideas?
Cheers
-michael
Hi Michael, I just looked at the source of your blog and I notice 2 things.
1. The plugin is working (do a find on the word “urchin” and you’ll see that every externally directed link has been re-written to include a javascript call to teh google code).
2. You’re corrct that the script-bit is missing, my guess is that there is no call to wp_footer in your theme. As a workaround I’m going to change the hook in the next release to use wp_head rather than wp_footer.
Rich
Rich,
Thank you very much for the plugin, and for looking into my problem.
I’ll be waiting for that ‘fix’
)
-michael
I’ve just uploaded verion 0.40 to the site which shoudl fix michael’s issue of not having a blog footer.
There are also two new features, firstly, comment author URL’s are now included in the outbound link tracking and secondly, it’s now possible to assign a channel to a specific post by giving the channel if (i.e. UA-12345-6) as the valeu of an “analytics” filed in the metadata.
It’s spitting the correct code on my site, but Google Analytics claims not to see it. I suspect GA is sensitive to placement and wants the code to immediately follow the last meta tag. I’ll fiddle some more and let you know if I find out.
Nit: on your web site here, your link to v4.0 in the “list of releases” points to http://boakes.org/download/googleanalytics_0.30.zip instead of _0.40.zip.
Thanks for the cool plugin!
Thanks for the plugin – sounds great.
On clicking to enter my UA string, though, I get this message:
Fatal error: Cannot redeclare analytics_config_page() (previously declared in /home/joefoote/public_html/blogs/wp-content/plugins/googleanalytics.php:30) in /home/joefoote/public_html/blogs/wp-content/plugins/googleanalytics.php on line 29
My blogis at http://www.joefoote.com
Thanks in advance for any help!
Joe Foote
Osh, thanks for the heads up, I’ve fixed the nit. I’m fairly certain the problem will just be that Google are oversubscribed right now (which is a blinkin’ pain for testing but i suppose that’s the price of popularity!).
Joe, that’s a curious one. Have you (perhaps) installed the plugin twice? Maybe once in the directory and once not?
Rich,
You said, 4) Enter your Google Analytics User Account string (it’ll be something like UA-12345-6. It appears when you “Add a channelâ€)
Add a channel – What do you mean by that?
I have done steps 12&3, after I enabled it in the plugin screen, it
it tells me Google Analytics is not active. You must enter your
UA String to work. But where do I do that? When I click on the link I get: Fatal error: Cannot redeclare analytics_config_page() (previously declared in C:\xampplite\htdocs\wp-content\plugins\googleanalytics.php:30) in C:\xampplite\htdocs\wp-content\plugins\googleanalytics.php on line 33
Rich,
I am getting the same error as Joe. The only copy of the PHP file I have resides in the plugins directory.
Hello Rich,
I have the same bug with Joe:
Fatal error: Cannot redeclare analytics_config_page() (previously declared in /…/wp-content/plugins/googleanalytics.php:30) in /…/wp-content/plugins/googleanalytics.php on line 33
I checked the plugin dir and there is only one copy of googleanalytics.php. Is this problem related to php version? I’m using php5.
Thanks for your great work!
dhchen, Kevin, Mike & Joe,
Thanks for your feedback! dhchen mentions PHP5, and having looked around the issue it appears that others have had similar problems.
So, I’m going to assume it’s a PHP5 issue and attack it from that angle. Watch this space
Release 0.50
Download it!
I’ve re-written the method defintions and filters in the plugin to be more defensive than they previously were, which means it should now play nicely with PHP5 based installations.
Hopefully this will solve the “Cannot redeclare analytics_config_page()” errors – RSVP!
I’ve also added a few words to the instructions to make the activation provess more obvious, and included a little feedback message to give warm feelings when a User Account number has been entered.
Hey there!
Thanks a lot for this plug-in! It works wonderfully on my site.
Rich, version 0.50 works perfectly. I’ve given your plugin a brief writeup at http://www.joefoote.com/blogs/google-analytics-is-now-lurking
Thanks!
Joe
http://www.joefoote.com
i installed the plugin, but the Google Analytics says that my Tracking is Unknown yet… =/
Hi Lerakpo, I’ve looked at your home page source and everything seems to be in order. Google took 48 hours bewfore I saw my first reports so sit tight, the result really is very impressive for a free service, it just takes a whilto get started.
Does you plugin track traffic from registered admins?
It would be nice if I could work on my site without registering hits.
Hi Darryl, Google Analytics already does this, just set up a filter for your IP range. I could add it to the plugin with just a little tweak, but is there a need beyond the existing filtering?
Rich, I think it would be nice. For example if you access from multiple ip’s (as I do) or dial up for example with who knows what IP.
Hi, thanks for the plugin. BTW to validate XHTML 1.0 Strict I had to change both of the “onClick” commands in the script to “onclick” (without the capital C).
(ref http://www.w3.org/TR/html4/struct/links.html)
Thanks Danloreye! I’ve included that in the code ready for the next release.
Hi Rich,
Does this work in WP 2.0 ?
Diolch yn fawr!
Chris
Hi Chris,
it does indeed work with WP2.0 – it’s working on this website right now.
Rich.
Hi,
What is the difference between this plugin and Cavemonkey50′s
Google Analyticator? Both seem to be doing the same exact thing.
I am trying to select one but not sure if one has an advantage
over the other.
Thank you
Eran
Just saying thanks!
Hello and thanks for making this plug-in.
I don’t know where to enter the google channel code and I get the following error at this url:
http://www.nationofimmigrators.com/wp-content/plugins/googleanalytics.php
=================
Fatal error: Call to undefined function: get_option() in /home/dotfusi0/public_html/nationofimmigrators/wp-content/plugins/googleanalytics.php on line 259
=================
Hi Chris, the output you describe is what you’d see if you tried loading the plugin as a standard PHP page. Plugins don’t work like normal pages – they just provide capabilities to wordpress so you (999 times out of 1000) don’t need to load the file directly as you’re trying to do.
Go into the admin interface (where you normally write articles) and look for the Plugins tab, under that is a Google Analytics tab, like this:

That’s where you have to enter the magic number.
If you have any more questions, let me know, but hopefully that should be enough of a leg-up.
Hello there,
Thank you very much for your response but this is what I see:

Thank you.
Hi Chris, that certainly doesn’t look right!
The good news is that I can immediately see something that may be causing your trouble: the version string reads “1.5-beta-1 2005-01-01″.
Yes, it really is over 12 months old, and beta level code, so it’s not bug free. You are strongly urged to upgrade to the newer codebase, or at the very least to a release version of the code.
I’m guessing that will fix your problem, and if it doesn’t, at least we’ll be on a more level playing field for me to hypothesize about.
It seems to be working fine (the urchin code’s there; will be a day or so before I see results at analytics.google.com). Thanks for the simple way to get analytics code onto my site.
S
could you elaborate re: exit page tracking? how rudimentary is it?
Hi dede, sure. Currently four types of content are filtered so that they have their outgoing links tracked, they are:
What’s not translated at the moment (because there are no filter hooks for the plugin to use – so if anyone knows a neat way to achieve this, let me know) is the header, the footer and the sidebar.
I seem to have a conflict between this plugin and my technorati tags plugin from http://www.broobles.com/ does anyone know how to fix this?
Hi A Samuel, can you be a bit more descriptive? I have no idea what you are seeing sorry. I’ve had a look at your site and the plugin is currently disabled, so I’m a bit limited in what I can diagnose so far!
Strange it is activated? I am seeing this in the source code:
Technorati Tags: manchester, offplan, off plan, affordable, housing, flats, leeds, city pod, UK
It seems to be stopping technorati log my posts any ideas?
Aha, got it. Yes, this is a most curious one and it’s down to the combination of the that Analytics works and the way Technorati do their indexing. The analytics plugin updates every link to include the onClick code which can be seen in each of your tags, but technorati don’t seem to like this.
Two solutions spring to mind (in advance of me being able to code a workaround).
Thanks for the response, I appreciate your help. I have raised a ticket and await to hear from them. I will try the scenario you suggested if no solution can be found. Ill keep you posted.
Cheers once again!
re: a clean solution to filtering the sidebar, I see that the “Ultimate Google Analytics” plugin facilitates this by having the user manually edit their sidebar template to wrap the get_links call with a call to the uga plugin. not entirely “clean” I suppose, but a great option to have for folks who don’t mind editing their templates. chance of the Boakes GA plugin supporting similar?
I tried installing this plugin and got a Fatal error: Cannot access empty property…My ISP is using PHP5. Has anyone else seen this issue? Soultion? I would like to use this plugin.
Just a quick comment to say that 0.51 (and I assume earlier ones) don’t validate as XHTML 1.1. It’s the version=”0.51″ part that does it.