Adding hostnames or PTR records to piwik

If you are using piwik and desire to know exactly where your website visitors come from, this hack will allow you to display the hostname or PTR record beside the IP addresses on the piwik dashboard. The Ip2Hostname plugin logs down the visitor's hostname in an additional column but provides no options to display it. This hack reqires the Ip2Hostname plugin to be activated. Do note that the changes may be overwritten when updating piwik.

Result:

Result

Add the function to /plugins/CoreHome/Visitor.php

1
2
3
4
5
6
7
function getLocationHostname()
{
    if (isset($this->details['location_hostname'])){
        return $this->details['location_hostname'];
    }
    return null;
}

Add to /plugins/CoreHome/CoreHome.php

1
2
$visitor['visitDurationPretty']         = $instance->getVisitLengthPretty();
$visitor['locationHostname']            = $instance->getLocationHostname(); #add this

Create a file /plugin/CoreHome/Columns/IpHostname.php with

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<?php
    namespace Piwik\Plugins\CoreHome\Columns;

    use Piwik\Config;
    use Piwik\Plugin\Dimension\VisitDimension;
    use Piwik\Plugins\CoreHome\Segment;
    use Piwik\Tracker\Action;
    use Piwik\Tracker\GoalManager;
    use Piwik\Tracker\Request;
    use Piwik\Tracker\Visitor;

    class IpHostname extends VisitDimension
    {
        public function getLocationHostname()
        {
            return array('location_hostname');
        }
    }
?>

Add to /plugins/Live/templates/_dataTableViz_visitorLog.twig

1
2
3
4
IP: {{ visitor.getColumn('visitIp') }}
{% if visitor.getColumn('locationHostname') is not empty %}     #add this
    ({{ visitor.getColumn('locationHostname') }})           #add this
{% endif %}                                                     #add this

Add to /plugins/Live/templates/getLastVisitStart.twig

1
2
3
IP: {{ visitor.visitIp }}</span>
{% endif %}
{% if visitor.locationHostname %} ({{ visitor.locationHostname }}){% endif %} #add this