Shoora
2/8/2015 - 7:23 PM

add WordPress author tracking using Google Analytics custom dimension

add WordPress author tracking using Google Analytics custom dimension

//Google Universal Analytics
function 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','//www.google-analytics.com/analytics.js','ga');
 
  ga('create', 'YOUR ANALYTICS PROPERTY ID HERE', 'auto');
  <?php if (is_singular('post') :
    $this_post = get_queried_object();
    $author_id = $this_post->post_author;
    //dimension index number must be retrieved for your GA property ?>
    ga('set', 'dimension1', '<?php echo get_the_author_meta('display_name', $author_id); ?>');
  <?php endif; ?>
  ga('send', 'pageview');
</script>    
<?php }
add_action ('wp_head', 'google_analytics');