How to Add Google Analytics to WordPress Admin Panel

Recently I was in a situation where I wanted to track the Pageviews for the admin panel.  Since I use Google Analytics on all my websites, I decided to add it  to all the admin pages.

Since there are no plugins to specifically add google analytics code to admin pages, I wrote a short snippet.

In case you also want to add google analytics to your admin panel , here are the instructions. 

Install the Code Snippets plugin.

First install and activate the code snippet plugin. This is a really handy plugin and allows you to add snippets your wordpress websites

Add the Snippet

Now add the following snippet to the plugin and  activate it.

function google_analytics_script()
	
{
echo "<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', 'insert_your_tracking_id_here', 'auto');
  ga('send', 'pageview');

</script> ";
}

add_action( 'admin_enqueue_scripts', 'google_analytics_script' );

 

Thats it , you can now track pageviews for your admin panel. Just Remember to use your tracking id in the snippet.

Leave a Comment