Easily Insert Google Analytics Into WordPress
Google Analytics is a great tool for checking and analyzing website traffic. But at times it can be a pain to go through your theme, and find just the right place to put the tracking code. There are plugins that can do this, but they aren't really necessary to do this. Thanks to the magic of WordPress hooks, you don't need to worry!
First, in your theme's folder, find a file called functions.php. If it's not already there, create it.
Insert the following code into the file. It doesn't really matter where, but around the end is generally a good idea.
<?php add_action('wp_footer', 'analytics'); function analytics() { ?> Your Google Analytics tracking code <?php }
Be sure to insert your own Google Analytics tracking code in there. This will add your tracking code into the footer of your WordPress blog.
This calls the add_action function, which is used to add a function to a hook. It is used by plugins and themes to easily modify certain parts in WordPress. Here we are using the wp_footer hook. The function we are using is called analytics.
Next we actually create the analytics function. It basically just displays what is on the line. Since it is called with the wp_footer hook, the text there (your tracking code) will be displayed in the footer of your site.
thanks man exactlly what i want because i don't want to install plugin...
Thanks again..
Do you have any idea how long it takes Google Analytics to start 'reading' my site after installing the code?
I think probably a few hours to a day or two.
Hello, I just added google analytics to my footer php file...what's the difference if I leave it there or put it into the function php file in word press?
Well if you're distributing the theme, it can help make it easier for users to modify the code and insert their own tracking code.
Also, some new users may find editing the footer.php file a bit confusing, maybe not knowing exactly where to put it. But here, you just add one snippet of code, pretty much anywhere in your functions.php file.
Hi all, I have a problem trying to input the code in my website, the fact is i dont know how to do this, i am actually a beginner at this and need to learn this for a job. Can anyone please tell me how to start and finish this? I have a static website.
If you have a static website, this won't work at all. :P
For a WordPress blog if you just follow the instructions, it should work fine! If not, please describe your problem so I can help you.
this is a good way of inserting google analytics into your blog, i also use it. got it from http://wordpressfunctions.com/performance-and-tracking/statistics/google-analytics-integration/
Thanks for the tip - I wasn't sure where to put the code :-)