Tracking internal users on wordpress

不打扰是莪最后的温柔 提交于 2019-12-19 04:08:29

问题


We have an internal WordPress site, and about 25 users. Our current Google analytics set-up will show us how many times a page has been visited, but because everyone comes from the same IP address it thinks that it is basically one very industrious person clicking a lot.

Does anyone have a strategy for tracking individual users?

(They are all logged into WordPress as a function of our single sign on.)


回答1:


You can use the _setCustomVar method from the JavaScript API to provide the user name of the current user. To my knowledge no GA plugins for Wordpress support this, so you will need to put your tracking code directly into the theme or write a custom plugin for it. The custom variable will then show up as a segment in Google Analytics. To get the current user you can use the wp_get_current_user API call.

Your tracking code would then look something like this:

<?php
    if (is_user_logged_in()) {
        $user = wp_get_current_user();
        $userName = $user->user_login;
    }
?>
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-Y']);
<?php if (isset($userName)) : ?>
  _gaq.push(['_setCustomVar', 1, 'Username', <?php echo(json_encode($userName)); ?>, 1]);
<?php endif; ?>
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
</script>

For the Universal Analytics version:

<?php
    if (is_user_logged_in()) {
        $user = wp_get_current_user();
        $userName = $user->user_login;
    }
?>
<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','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-XXXX-1', 'auto');
    <?php if (isset($userName)) : ?>
        ga('set', 'userId', <?php echo(json_encode($userName)); ?>); // Set the user ID using signed-in user_id.
    <?php endif; ?>
    ga('send', 'pageview');
</script>



回答2:


I co-authored called Stream that tracks logged-in user activity. Basically, it's designed to be a detailed audit trail of everything that happens in the WP Admin area.

It also organizes the activity by user, context, action and IP address so it can be easily filtered/searched later.

More information:

  • http://wptavern.com/stream-a-wordpress-plugin-to-track-and-monitor-changes-in-the-admin
  • http://x-team.com/2013/12/stream-track-every-change-made-on-your-wordpress-site/
  • https://github.com/x-team/wp-stream


来源:https://stackoverflow.com/questions/16578410/tracking-internal-users-on-wordpress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!