সোমবার, ১২ ডিসেম্বর, ২০১১

How to Add Google Analytics code in WordPress & Website


For website owners, Google Analytics is one of the very best tool to get rich insight of their site traffic & marketing effectiveness. Even though adding Analytics code on site is not that hard but lot’s of people don’t follow the way they should have done it.
In this post I will write about where & how to add this little piece of code which is so powerful. Now, the concept of Website has changed a lot over the years. It’s just not only few simple HTML pages anymore. Technologies like PHP, JSP, CSS, SQL & whole bunch of them made their way to Web. Very normally, things has got way complex yet easier than ever before.
In this post I focused on WordPress & regular HTML based web sites. I will try to explain how exactly you can add Google Analytics code on these two different sites. If you own or maintaine a website, I am pretty sure that you are quite familiar with the Google Analytics code. Here is a sample of typical GA code.

Adding Google Analytics in Your Website

If you are running any static html based website, adding Google Analytics code is much easier. There are several ways that can be used to add this little piece of code on your site. In this post I will be talking about two very simple & easy method of adding Analytics code on your site.
Method 1: Once you have the Analytics code, you must copy & paste it on your every single html file. Now, you might ask, “Where?”. The answer is, different people have different views on this issue. However, Google suggest to paste this code right before tag of your html file. Save the html files & upload it on your server. You are done.
Method 2: In recent days we found it really helpful for the site owners, to be able to put Google Analytics code in an eternal .js file. JS stands for JavaScript which is a popular file extension name for web designers or programmers. You can do it either by your html page editor or by Notepad application from Windows.
Copy & paste everything in between  tag from your original code & save the file with .js extension. Upload it on your web server’s root directory. I am assuming that’s going to be something like http://www.yourdomain.com/gac.js. Now, all you have to do is to call this js file from every single html file that you have on your web server. Simply add
right before your tag. Change the domain name with yours one. Your should follow this procedure for every single html file that you have on your site.

Adding Google Analytics in WordPress

Google Analytics & WordPressLuckily, adding Google Analytics code in WordPress is lot easier than typical html based websites. Since, the WordPress pages & posts are being shown dynamically from your SQL database, it takes less than five minuets to finish your job. Once again, there are several methods of adding Analytics code in WordPress but I will show you two different very simple way to add them.
Method 1: First of all create an external JavaScript (js) file just like before. Now,  log in to your WordPress site & go to “Appearance > Editor” menu. Select header.php(template or theme files) file from the right hand side of the new window. At this point, I want you to log in to your cPanel or FTP server where your WordPress theme is located. Upload the “gac.js” file at the root directory of your theme.
Go back to your WordPress window and copy paste the following code right before your tag.
Remember “” basically defines the directory of your WordPress theme. Save the code & update your header.php file. You are pretty much done with this method.
Method 2: I am assuming that are already logged in to your WordPress admin panel. Go to “Appearance > Editor” menu & select functions.php (it carries numerous functions for your WordPress theme) file from the right hand side of the window. Paste the following code inside the functions.php file.
add_action(‘wp_footer’, ‘add_googleanalytics’);
function add_googleanalytics() { ?>
// Paste your Google Analytics code here
Take a look at the code carefully. At this point get(copy) your entire Google Analytics code and paste it inside the PHP function where it says “// Paste your Google Analytics code here”. Remove the entire line & paste it. Update the file. You are good to go.
Now, I got lots of questions from many people that what’s wrong if they want to simply copy & paste the entire Google Analytics code on their  tag (withing header.php file) directly? There is nothing with that, but WordPress suggests to use Javascript code from an external file just like we did today. Now, it’s entirely upto you to. You can do it eitherway but I don’t recommend something that WordPress itself didn’t suggest. Hope you got my point. Good luck with adding your own Google Analytics code on your site. Fell free to make comments and ask questions (if you have any). We would be more than happy to get back to you.

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন

পৃষ্ঠাসমূহ