Support » Plugin: Tracking code for Matomo, by Sérgio Coelho Santos » Feature: Log Username if User is logged in

Viewing 14 replies - 1 through 14 (of 14 total)
  • Hi,

    I’m sorry if i took so long to reply, but WordPress doesn’t send me any notifications and i haven’t been able to find any way to enable them.

    Thanks for the suggestion.
    This would be an interesting feature, but i was trying to keep the plugin as simple as possible. I’ll think about it.
    If i do add this, i will also add a configuration to the backend, to make it optional.

    andrew55

    (@andrew55)

    I think this feature would be a great addition!

    Sergio S.

    (@mentebinaria)

    Hmmm… i guess that makes two against one – and adding the option to the backend would be easy too.
    I will add it as soon as i have some time (which can take a while).

    Sergio S.

    (@mentebinaria)

    Done!

    The new username logging feature is optional and disabled by default. To enable it, just visit the options page and tick the suitable checkbox. Also, if you’re using a caching plugin, don’t forget to clear the cache.

    Let me know if you find any bugs or have any more suggestions.

    Is it possible to add this code without using this plugin?

    Hi, owcw,

    I’m going to assume you’re referring to the tracking code on each page. If that is so, then yes, you can copy the code you get from Piwik and add it directly to the theme you use. I advise you to use a child theme instead of hacking the main theme code.

    If you were referring to the feature itself, you can have a look at the code provided by Christian Werner and adjust it to your needs, to achieve the same effect. The code is pretty generic.

    In any case, the plugin is free, even for commercial use (as long as you give credit where it is due), so you can save yourself some trouble by just installing and configuring it.

    Good luck.

    According to Piwik Documentation on Custom Variables, what I did is this:

    <!-- Function to get the username from WordPress for Piwik User Tracking -->
    <?php
    global $current_user;
    get_currentuserinfo();
    $user_info = get_userdata(1);
    $Username =  $current_user->user_login;
    ?>
    
    <!-- Piwik Code -->
    <script type="text/javascript">
      var _paq = _paq || [];
    <!-- Piwik Custom Variable for User Tracking -->
    <?php if (!empty($Username)) {echo "piwikTracker.setCustomVariable(1, 'Visitor', '".$Username."', 'visit');";} else {}?>
    <!-- End Piwik Custom Variable for User Tracking -->
      _paq.push(['trackPageView']);
      _paq.push(['enableLinkTracking']);
      (function() {
        var u=(("https:" == document.location.protocol) ? "https" : "http") + "://mydomain.com/";
        _paq.push(['setTrackerUrl', u+'piwik.php']);
        _paq.push(['setSiteId', 1]);
        var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript';
        g.defer=true; g.async=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
      })();
    </script>
    <noscript>
    <!-- Piwik Image Tracker Fallback -->
    <img src="http://mydomain.com/piwik.php?idsite=1&rec=1" style="border:0" alt="" />
    <!-- End Piwik Image Tracker Fallback -->
    </noscript>
    <!-- End Piwik Code -->

    But I can’t see it in my Piwik… 🙁

    I can’t help noticing how you haven’t replaced “mydomain.com” with your own domain. Do you actually own that domain?

    If you don’t, replacing both instances with the right domain should help solve your issue. Also, please be patient: it may take some time for results to start showing up on Piwik.

    Of course I replaced it and since then there were several logins on my site as I can see in the Piwik visitor log. But there are no usernames tracked and it doesn’t show up in Custom Variables. Any idea why?

    Maybe because I use Theme My Login for user login?

    I deactivated Theme My Login plugin to test, but that doesn’t work neither… 🙁

    I replaced this:

    <?php if (!empty($Username)) {echo "piwikTracker.setCustomVariable(1, 'Visitor', '".$Username."', 'visit');";} else {}?>

    with this:

    _paq.push(['setCustomVariable',1,"Username", "<?php if (empty($Username)) {echo 'Guest';} else {echo $Username;}?>","visit"]);

    Seems to work now.

    But if you have improvements or corrections, let me know.

    Hello again, owcv.

    I’m sorry for not replying sooner, but i’m glad to see you’ve sorted things out on your own.

    Your code seems to make sense. I’m actually thinking about adjusting my plugin, because your version looks a bit better than mine. I still have to look into it, but would you mind if i used that code of yours? (And is it really yours or did you get it from elsewhere?)

    Actually, nevermind: that bit of code you used is already what my plugin writes… (Yeah, i haven’t looked at it in a while. 🙂 )

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Feature: Log Username if User is logged in’ is closed to new replies.