How To Add Google Analytics Code On Wordpress Without Any Plugin | SEO Updates

Saturday 29 April 2017

How To Add Google Analytics Code On Wordpress Without Any Plugin

Leave a Comment

Adding Google Analytics Code On Wordpress Without Any Plugin



Google Analytics is the defacto standard with regards to adding examination to your site. That being said it can be very hard to include WordPress and keeping in mind that there are numerous modules to include it, the greater part of them include unneeded special notification, consume up menu room, and do significantly more than would normally be appropriate backing your site off. 

Google Analytics can be included in various courses however there is an appropriate approach to add it to your WordPress site that won't make your site break, or back off. The code I will give will load the script in the footer (which is better for execution) yet can be changed to be loaded in the header and I will give both renditions of the script. 

Another imperative subtle elements is this code must be set in your youngster topic functions.php OR a site-particular module (that you use with your other arbitrary capacities). You ought to likewise just utilize one rendition of these modules and please make sure to include your own particular following ID. Also this code will load with async to additionally enhance your site load time.


add_action('wp_head', 'add_google_analytics');
function add_google_analytics() { ?>
<!-- Google Analytics -->
<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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics --> 
<?php } ?> 

This will infuse the Google Analytics code into the header of your site in the event that you are hoping to load the code into the footer you have to make a slight change excessively resemble the underneath code. 


add_action('wp_footer', 'add_google_analytics');
function add_google_analytics() { ?>
<!-- Google Analytics -->
<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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
<?php } ?>

Presently the code is set to load in the footer, and the best part is this code can be added to your functions.php document and spare yourself from utilizing another module!
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment

Feel Free To Comment