
I like to use Google Analytics, but the boring stuff is that when you switch themes, you’ll have to re-insert the code in the new theme. To avoid it, let’s use the power of WordPress hook to insert our analytics code without editing theme files.

I like to use Google Analytics, but the boring stuff is that when you switch themes, you’ll have to re-insert the code in the new theme. To avoid it, let’s use the power of WordPress hook to insert our analytics code without editing theme files.
Just insert the following code into your functions.php file, and you're done. Don't forget to paste your Google Analytics code on line 5.
<?php
add_action('wp_footer', 'ga');
function ga() { ?>
// Paste your Google Analytics code here
<?php } ?>
Have you checked out the new Headway theme for WordPress? It is pretty cool. You can read a review here.
18 Responses
Isnt’t functions.php a part of your theme? Ergo, you’re still editing your theme!
The new async analytics actually recommends adding the code to the header, not the footer. http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&answer=161379
@Ronny : Yes it is, but a functions.php file can easily be imported from a theme to another.
@Artem : You’re right, to add the code in the header instead of the footer, simply replace wp_footer by wp_head
Sorry for bothering about this, I get a white output. How would it look with the analytics code pasted and the opening and closing functions php tags not included?
There are plugins for Wordpress that deal with Google Analytics. I’m using Ultimate Google Analytics. It has a number of option to control which data goes to Google and which data doesn’t. For example, you could block the visits of the admin from showing up at analytics because you’re interested in the regular visitors and not the people that maintain it.
Would it not be better to create a simple plugin?
The use of GA is freaking me out more and more. Nobody seems to even read the TOS: you have to tell your users that you’re using it in your privacy policy. Why would you sell all your users movements to a company, just because they draw nice graphics from your data? there’s much better free and paid solutions for tracking your users.
As a consequence of site owners ignoring their visitor’s rights (most of which aren’t even able to understand what to do and how to manage to not be followed around the web by google 24/7), i today completely block all scripts from google.com, using them only when i have to. Yes that means ads, too, nobody’s making a cent on me, as long as he ignores privacy issues.
It may sound paranoid to some, but a CEO stating that:
[link href="http://www.boingboing.net/2009/12/09/google-ceo-says-priv.html"] If you have something that you don’t want anyone to know,[/link] [link href="http://gawker.com/5419271/google-ceo-secrets-are-for-filthy-people"]maybe you shouldn’t be doing it[/link] [link href="http://www.huffingtonpost.com/2009/12/07/google-ceo-on-privacy-if_n_383105.html"]in the first place.[/link] while being concerned about his own privacy is not evoking the impression, they still have an interest in the civil rights of their userbase.
@stefan If you already have a bunch of plugins running on your website this way is better since the loading time is decreased, But I suppose if you only had one or two plugins loading time isn’t an issue so you could use a plugin. plugins basicly do this.
Nice tip, I didn’t know that it is impossible to do that. But I’m not very experienced when it comes to things connected with code. Thanks for the tip.
By this method, you’d be simply creating an extra function and making WordPress process an extra function.
It would be more efficient to just add the code directly to the header / footer
I see Artem beat me to it but the new asynchronous code goes into the header…
Quick question with that… how do you make sure it’s the last thing in the head tag? Does WP inject plugin wp_head stuff or the functions.php stuff in there first? I know analyticator plugin somehow makes sure it’s last in the head include. Just wondering…
@Ajay – Imo you are correct it probably adds a bit of overhead (un-noticable) but technically yes. This is a good solution in between manually adding it to the template file, and using a plugin!
@Chuck – You can add a “priority” argument to the “add_action” function, the default priority is 10, and the higher the priority the later it runs in wp_head. So you could do something like:
add_action(‘wp_footer’, ‘ga’, 20);
Where 20 is the priority, you might need to increase it even more if some plugins have decided to set it to a high value.
Reference: http://codex.wordpress.org/Function_Reference/add_action
@Chuck: Is it really _that_ important to put the GA code at the very end of teh header?
I was under the assumption that it suffices to keep it rather at the top, but the exact position won’t matter that much.
@Natalie I’m just going off of what G says: “More specifically, you should position it as the last script in the section.”
source: http://code.google.com/apis/analytics/docs/tracking/asyncTracking.html
@Thomas nice dude thanks… I wasn’t aware of the priority flag – wasn’t ever worried about where it fell in header/footer inc – just that it got there.. this will help TY
The functions.php file can’t be ported from any theme to any other theme. Many use this file to define, well, functions that are specific to a theme. They would break another theme, and overwriting another theme’s custom functions with yours would break that too.
That’s the whole point of putting it in a plugin; you can switch themes without making *any* changes. This is something that’s already in the right place.
@Thomas. You can also just put is_user_logged_in conditional right before the google code is displayed. One less plug-in is one less worry.
I just want to add that there is a great Google Analytics WP Plugin also.
Only thing you have to do is install it!
great tips. but i t more easily if you just use plugin, you don;t need change anything when change themes
@tom we have same name and same thought LOL
Trackbacks: