通过Wordpress上的Google Analytics进行作者跟踪

通过Wordpress上的Google Analytics进行作者跟踪,wordpress,google-analytics,Wordpress,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.getElementsByTagN

我在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', 'UA-XXXXXXX-XX', 'auto');
      ga('send', 'pageview', {
      'dimension1': '<?=$author?>'
    });

    </script>

(函数(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]| |函数(){
(i[r].q=i[r].q | |[]).push(参数)},i[r].l=1*新日期();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(窗口,文档,“脚本”,“www.google-analytics.com/analytics.js”,“ga”);
ga(“创建”、“UA-XXXXXXX-XX”、“自动”);
ga('send'、'pageview'{
“维度1”:”
});
现在,当我查看Google Analytics页面中的数据时,我看到作者名为“”,而不是循环中的实际作者 你确定你在wordpress循环中设置了它吗?这取决于变量的设置位置。如果您在oif HTML页面的开头有此项,那么此时不必提供此变量

速记语法 这似乎是正确的速记
。但是请尝试完整的语法

这应该可以正常工作:

<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-XXXXXXX-1', 'auto');
<?php if (is_single()) :
    $post_author_id = get_post_field( 'post_author', $post_id ) ?>
ga('set', 'dimension1', '<?php echo get_the_author_meta('display_name', $post_author_id); ?>' );
<?php endif ?>
ga('send', 'pageview');
</script>

(函数(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]| |函数(){
(i[r].q=i[r].q | |[]).push(参数)},i[r].l=1*新日期();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(窗口,文档,“脚本”,“www.google-analytics.com/analytics.js”,“ga”);
ga(“创建”、“UA-XXXXXXX-1”、“自动”);
ga('set','dimension1','');
ga(‘发送’、‘页面浏览’);

这可能是因为您正在发送字符串。我真的很惊讶,我希望您在维度中看到它,除非它不是html编码的。有趣。@DaImTo实际上这是有效的php(目前,ASP样式的标记将在不久的将来被删除,但至少在PHP5.5中它们仍然有效)。似乎变量没有设置。@Eikepierstorf nice我总是通过能够看到JavaScript中的实际数据来进行测试。不会想到php可以在JavaScript创建之后运行。问题一定是$author没有设置。我以为字符串应该自动提取“author name”。搞定了,那就是你的问题了。您需要自己设置字符串的值。这里是SE wordpress网站上的一篇文章,解释了如何获得作者姓名:欢迎使用Stack Overflow!请确保在您的答案中添加代码,并解释其原因。