Where to Put Google Analytics Code in WordPress

So you are looking for information about where to put google analytics code in WordPress. There are 3 ways to install google analytics in WordPress. They are:

  • Install a WordPress plugin that could handle the job. (PREFERRED method)
  • Add a code to functions.php file in your theme. (NOT RECOMMENDED)
  • Add google analytics tracking code to header.php file located under your theme folder. (preferred method for high-traffic blogs)

How to set up Google Analytics with a plugin

This is the easiest method and newbies should prefer this one. The downside of installing analytics with a wordpress plugin is that your blog will slow down a little bit due to an additional plugin. However, 99.9% of the time, this is not an issue.  The advantage of this method is that it is easy, user-friendly and requires less time.

There are tons of wordpress plugins that you can use to install Google Analytics. You can search the plugin directory to find a one.  Best Google Analytics WordPress plugin is simplyGoogle Analytics for WordPress developed by Joost de Valk.  You don’t even need to copy and paste the tracking code. The plugin asks you to grant access for google analytics, then you are done. It is very simple and user friendly. Watch the video on plugin homepage prior to plugin installation.

Do not mess with functions.php

Second method, adding code to functions.php, is not recommended. There are some tutorials that describes how to install Google Analytics by placing a php code to functions.php file in your theme. If you are newbie, you should avoid this approach. The reason is simple. If you mess up, you will hard time restoring your wordpress blog. You can always restore the original functions.php; however, it requires a little bit technical knowledge and patience. Here is a screenshot I found online for a WordPress blogger who placed the php code wrong in functions.php.

google analytics wordpress

Even if you can do it properly, you should do your best not to modify functions.php. If you follow online tutorials for a long time, you will make various changes to functions.php. After some time, it will be difficult to manage those modifications. If something stops working, you have to go over those code snippets one by one to resolve the issue. Believe me, this will eliminate headaches and save a lot of time in the long run.

How to add Google Analytics to WordPress blog

It is easier to set up Google Analytics with a WordPress plugin; however, this causes slight performance degradation. For a high-traffic blog, even a 1% increase in page load time could make a difference. A plugin modifies the way your WordPress blog functions. When a visitor requests a webpage, plugin calls slow down the performance of the blog. Therefore, one should install Google Analytics tracking code directly into theme files. So the ultimate question becomes Where to put google analytics code in WordPress

The answer is right before the </head> tag in your header.php file. Do NOT place the Analytics tracking code in your footer.php. There are various wordpress tutorials that tells you to put the tracking code in your footer.php file. The advocates of footer.php were once correct, but they aren’t anymore. It is better to put the tracking code in header.php now. Google used to have old-style tracking code before December, 2009.  This was like a old-style javascript code. When old tracking code was used, the browsers had to hang and wait for Google to send the necessary tracking code. If google delivers the necessary code late to visitor’s browser, the webpage keeps hanging, and your visitors had to wait for your real content to be delivered. In the old days, it was better to put the analytics code to the bottom of the page to make sure that the delays on google side don’t affect your webiste’s performance.  However, Google released Asynchronous Tracking code on December 1st, 2009. New tracking code works differently, it doesn’t affect your webpage’s load time. Your webpage will load up regardless of how long it will take the browser to retrieve data from Google. When a browser gets the Google Analytics tracking code, it sets up tracking variables and starts to store the events. At the same time, it starts to download ga.js file which is used to send the event info back to Google. In other words, you can put the Google Analytics tracking code wherever you want. But it is better to put in header.php.

It is a better practice to use the new Google Analytics tracking code in the head of your webpage, it won’t hold back the rest of your page from loading. Moreover, it won’t be delayed by the rest of the webpage either. It will start functioning sooner, and you will have more accurate visitor data. If you put the tracking code all the way to the bottom of the page and your visitor left the page before tracking code is fired, those visitors are recorded as a direct visit. Therefore, you will have inaccurate visitor data.

Here are the steps to install Google Analytics tracking code on WordPress:

1. Login as an admin to wordpress

2. Go to Appearance > Editor

3. Select the header.php

google analytics

4. Find the </head> html tag

5. Paste the Google Analytics code before the </head> tag.

google analytics wordpress

6. Click Update File to save changes.

Site Footer

2019-02-17 16:56:36 Type: E_NOTICE Msg: Undefined index: asalah_page_excerpt File: /var/www/u6007393/public_html/webspidersolution.com/wp-content/themes/writing/functions.php Line: 468