简体   繁体   English

在Wordpress上使用Google Analytics(分析)进行作者跟踪

[英]Author Tracking With Google Analytics on Wordpress

I'm having a little trouble with getting author tracking set up on my google analytics. 我在Google Analytics(分析)上设置作者跟踪时遇到了一些麻烦。 This is my code 这是我的代码

        <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>

Now, when I look at the data in my Google Analytics page, I see the author name as "" and not the actual authors 现在,当我查看Google Analytics(分析)页面中的数据时,看到的作者名称为“”,而不是实际的作者

The loop 循环

Are you sure you set it up in the wordpress loop? 您确定要在wordpress循环中进行设置吗? It really depends, where the variable is setted. 确实取决于设置变量的位置。 If you have this at the beginning oif HTML page, the variable do not have to be available in this time. 如果在HTML页面的开头有此变量,则该变量不必在此时可用。

Shorthand syntax 速记语法

It seems to be correct shorthand <?=$author?> . 似乎是正确的速记<?=$author?> But try full syntax <?php echo $author; ?> 但是尝试完整的语法<?php echo $author; ?> <?php echo $author; ?> . <?php echo $author; ?>

This should work fine: 这应该工作正常:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM