How to add Google Analytics code to WordPress theme, without any plugins ?. In wordpress for every problem there is a plugin to solve the issue. But, most the issue can be solved withpout th help of the plugins.
So, i am going to tell a simple trick that you can implement on your wordpress website.We all know about Google analytics and we use analytics code to track/count the number of users/vistors to our website.Google analytics code can be added to your website via a plugin or using a widget.
But, here i am going to use the header.php file of the wordpress site. When ever a hit comes to your website, website shuold have to load the header.php file.Which means,every user should have to go through the header.php file.
We can use this header.php file for Google Analytics. AddingAdding Google Analytics code to your header.php file is easy. By doing this, you can accurately track the users.
To do this:
Goto Appearance -> Editor
Look on the right side for header.php.
Take your Google Analytics code. A typical code would look like this :
< script > (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-Your-Analytic-ID', 'auto'); ga('send', 'pageview'); < / script >
Now, place the Google Analytics code to header.php file, inside thetags
< head > < ! -- Under the meta tags --> < script > (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-Your-Analytic-ID', 'auto'); ga('send', 'pageview'); < / script > < / head >
Also, you can add the Google Analytics code to your WordPress Theme.Some of the Themes have this as an inbuilt functionality. Just.Just explore your theme functionalities. But.But, when you change the theme from one to another, Google Analytics will lost.So, in my opinion it is more better to add the Google Analytics code,in your header.php file.