如何使用Google Analytics跟踪wordpress中每个作者的页面浏览量

如何使用Google Analytics跟踪wordpress中每个作者的页面浏览量,wordpress,google-analytics,Wordpress,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.parent

我使用的通用跟踪代码如下所示:

<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-XXXXXXXX-X', 'auto');
  ga('send', 'pageview');

</script>
这是可行的,但当我尝试这样做时:

ga('create', 'UA-XXXXXXXX-Y', 'example.com');

<?php
   if (is_single()){
      echo "ga('set', 'contentGroup1', '".get_the_author()."');\n";
   }
?>

ga('send', 'pageview');
为了获取作者数据,整个代码崩溃,我的跟踪代码停止工作


有人知道怎么解决这个问题吗

在ga语句中,新行字符之前有一个额外的双引号。。。更不用说你在第一次/最后一次通话中使用了卷曲引号

ga('create', 'UA-XXXXXXXX-Y', 'example.com');

<?php
   if (is_single()){
      echo "ga('set', 'contentGroup1', '".get_the_author()."');\n";
   }
?>

ga('send', 'pageview');

谢谢您的回答,但即使使用您的代码,它仍然不起作用。。它似乎不支持php。那么,您添加的代码是php文件吗?你看到了吗。。该死我只是假设用于插入GA代码的主题字段支持php。谢谢你的回答!现在在functions.php文件中添加了代码。根据wordpress文档get_,必须在循环中使用作者。由于GA代码通常不在循环中,这可能是个问题。谢谢您的回答。解决方案是,我没有在支持PHP的文件中添加GA代码。我在functions.php文件中添加了GA代码,而不是使用add_action'wp_head'。。。。这就解决了我的问题。
ga('create', 'UA-XXXXXXXX-Y', 'example.com');

<?php
   if (is_single()){
      echo "ga('set', 'contentGroup1', '".get_the_author()."');\n";
   }
?>

ga('send', 'pageview');