There are a few different ways to add your Google Analytics tracking ID to your WordPress site. This method results in inserting the tracking code before the closing body tag.
Where does this go?
Replace the function’s prefix with the appropriate theme prefix or naming conventions you’re following and insert in your theme’s functions.php
(or equivalent) file. Copy and paste your account-specific snippet within the script
tags so your actual tracking ID is applied.
add_action( 'wp_footer', 'prefix_google_analytics' );
/**
* Add Google Analytics Tracking.
*/
function prefix_google_analytics() {
// Hook into the footer.
<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', 'YOUR-GOOGLE-TRACKING-ID-HERE', 'auto');
ga('send', 'pageview');
</script>
}
Once this code is added to your live site, it will likely take a day or so to display the data within your account.
Additional Reading:
- WordPress codex: add_action
- WordPress codex: wp_Footer
- Google Analytics