Post Pic

Insert Google Analytics without editing your theme

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.

Related Posts

No related posts.

18 Responses

Dec 14 2009 09:44

Isnt’t functions.php a part of your theme? Ergo, you’re still editing your theme!

Dec 14 2009 10:01

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

Dec 14 2009 10:08

@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 :)

Dec 14 2009 11:22

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?

Dec 14 2009 12:07

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.

Dec 14 2009 14:57

Would it not be better to create a simple plugin?

Dec 14 2009 15:17

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.

Dec 14 2009 15:19

@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.

Dec 14 2009 16:26

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.

Dec 14 2009 20:06

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

Dec 14 2009 22:41

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…

Dec 15 2009 17:31

@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

Dec 15 2009 19:38

@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.

Dec 15 2009 20:45

@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

Dec 17 2009 03:20

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.

Dec 17 2009 07:56

@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.

Dec 17 2009 20:59

I just want to add that there is a great Google Analytics WP Plugin also.
Only thing you have to do is install it!

Dec 17 2009 22:28

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:

Leave a Comment

* Name, Email, Comment are Required