Patch for Live/Realtime visitors widget to improve GeoIP accuracy with Maxmind

Great mod. It frustrated me that the Visitor Log didn’t have city/state so, after including the above changes, I modified plugins/Live/templates/visitorLog.tpl to include the following:

After:


				{if (isset($visitor.columns.provider)&&$visitor.columns.provider!='IP')} 
					<br />
					{'Provider_ColumnProvider'|translate}: 
					<a href="{$visitor.columns.providerUrl}" target="_blank" title="{$visitor.columns.providerUrl}" style="text-decoration:underline;">
						{$visitor.columns.provider}
					</a>
				{/if}

Add:


				<br/>{$visitor.columns.country}, {$visitor.columns.city}

Great patch… thanks

after hours of reading and searching why and how I could fix that problem with different countries in different widgets, I finally… found you! :)-D

The first patch is working perfectly.

For the second patch, about showing the cities, your point 2 “edit /plugins/Live/templates/lastVisits.tpl, and replace” is not good anymore as the html in that file isn’t the same anymore.

I personally just changed:


<div class="datetime">
	...
	&nbsp;<img src="{$visitor.countryFlag}" title="{$visitor.country}, {'Provider_ColumnProvider'|translate} {$visitor.provider}" />
	...
	{if $visitor.visitIp}- <span title="{if !empty($visitor.visitorId)}{'General_VisitorID'|translate}: {$visitor.visitorId}{/if}">IP: {$visitor.visitIp}</span>{/if}
</div>

by this:


<div class="datetime">
	...
	&nbsp;<img src="{$visitor.countryFlag}" title="{$visitor.country}, {$visitor.city}, {'Provider_ColumnProvider'|translate} {$visitor.provider}" />
	...
	{if $visitor.visitIp}- <span title="{if !empty($visitor.visitorId)}{'General_VisitorID'|translate}: {$visitor.visitorId}{/if}">IP: {$visitor.visitIp}</span>{/if}
	<br />{$visitor.country}, {$visitor.city}
</div>

that way, it adds the city in the title of the country logo and a line with the country and city…

I don’t know yet if it won’t be too much to add that line, I’ll see in the future, but in the meantime, I’m happy…

the general idea is just to place the “{$visitor.city}” variable where you want it…

Still that problem with the wrong countries in the world map widget, and it will be the 100% perfect software…

Anyone know how do get GeoIP data into the visitor countries (world map) widget on dashboard?

Thank you for that great patch !!!

I’ve just updated the file /plugins/Live/templates/lastVisits.tpl to be usable with the latest version of Piwik. If it can help someone …


			<div class="datetime">
				<span style='display:none' class='serverTimestamp'>{$visitor.serverTimestamp}</span>
				{$visitor.serverDatePretty} - {$visitor.serverTimePretty} ({$visitor.visitDurationPretty})&nbsp;
				<div style="display: inline; float:right">{if $visitor.visitIp}IP: {$visitor.visitIp}{/if}</div>
				<div>&nbsp;<img src="{$visitor.countryFlag}" title="{$visitor.country}, {'Provider_ColumnProvider'|translate} {$visitor.provider}" /> {$visitor.country}, {$visitor.city}
					<div style="display: inline; float:right">
						&nbsp;<img src="{$visitor.browserIcon}" title="{$visitor.browserName}, {'UserSettings_Plugins'|translate}: {$visitor.plugins}" />
						&nbsp;<img src="{$visitor.operatingSystemIcon}" title="{$visitor.operatingSystem}, {$visitor.resolution}" />
						{if $visitor.visitConverted}
							<span title="{'General_VisitConvertedNGoals'|translate:$visitor.goalConversions}" class='visitorRank'>
							<img src="{$visitor.visitConvertedIcon}" />
							<span class='hash'>#</span>{$visitor.goalConversions}
							{if $visitor.visitEcommerceStatusIcon}
								&nbsp;- <img src="{$visitor.visitEcommerceStatusIcon}" title="{$visitor.visitEcommerceStatus}"/>
							{/if}
							</span>
						{/if}
						{if $visitor.visitorTypeIcon}&nbsp;- <img src="{$visitor.visitorTypeIcon}" title="{'General_ReturningVisitor'|translate}" />{/if}
					</div>
				</div>
			</div>

Thanks for sharing this. And to make it even better i decided to fix the wolrd map widget and share it with you guys

Ok so go into piwik directory and open /plugins/UserCountry/UserCountry.php and find


protected function archiveDayAggregateVisits($archiveProcessing)
	{
		$labelSQL = "log_visit.location_country";
		$this->interestByCountry = $archiveProcessing->getArrayInterestForLabel($labelSQL);
		
		$labelSQL = "log_visit.location_continent";
		$this->interestByContinent = $archiveProcessing->getArrayInterestForLabel($labelSQL);
	}


and replace it with


protected function archiveDayAggregateVisits($archiveProcessing)
	{
		//$labelSQL = "log_visit.location_country";
                  $labelSQL = "log_visit.location_geoip_country"; 
		$this->interestByCountry = $archiveProcessing->getArrayInterestForLabel($labelSQL);
		
		//$labelSQL = "log_visit.location_continent";
                  $labelSQL = "log_visit.location_geoip_continent";
		$this->interestByContinent = $archiveProcessing->getArrayInterestForLabel($labelSQL);
	}

That shold make the world map display the corect country, nice hm?haha enjoy

“Patch” applied !
Thanks

Some weird problem with GeoIP. In some times city name doesn’t display correctly.
In example


Düsseldorf

What’s wrong?

I made following changed from first patch and i got error. following are my changes


 function getCountryName()
        {
                return Piwik_CountryTranslate($this->details['location_geoip_country']);
        }

        function getCountryFlag()
        {
                return Piwik_getFlagFromCode($this->details['location_geoip_country']);
        }

        function getContinent()
        {
                return Piwik_ContinentTranslate($this->details['location_geoip_continent']);
        }


Following error i got on Realtime visitor log


There is an error. Please report the message (Piwik 1.8.1) and full backtrace in the Piwik forums (please do a Search first as it might have been reported already!).

Notice: Undefined index: location_geoip_country in /var/www/html/owa/piwik/plugins/Live/Visitor.php on line 207

Backtrace -->
#0 Piwik_ErrorHandler(...) called at [/var/www/html/owa/piwik/plugins/Live/Visitor.php:207]#1 Piwik_Live_Visitor->getCountryName(...) called at [/var/www/html/owa/piwik/plugins/Live/Visitor.php:74]#2 Piwik_Live_Visitor->getAllVisitorDetails(...) called at [/var/www/html/owa/piwik/plugins/Live/API.php:162]#3 Piwik_Live_API->getCleanedVisitorsFromDetails(...) called at [/var/www/html/owa/piwik/plugins/Live/API.php:132]#4 Piwik_Live_API->getLastVisitsDetails(...) called at [:]#5 call_user_func_array(...) called at [/var/www/html/owa/piwik/core/API/Proxy.php:190]#6 Piwik_API_Proxy->call(...) called at [/var/www/html/owa/piwik/core/API/Request.php:128]#7 Piwik_API_Request->process(...) called at [/var/www/html/owa/piwik/plugins/Live/Controller.php:96]#8 Piwik_Live_Controller->getLastVisitsStart(...) called at [/var/www/html/owa/piwik/plugins/Live/Controller.php:29]#9 Piwik_Live_Controller->widget(...) called at [:]#10 call_user_func_array(...) called at [/var/www/html/owa/piwik/core/FrontController.php:138]#11 Piwik_FrontController->dispatch(...) called at [/var/www/html/owa/piwik/index.php:53]

Works great for Piwik 1.8.2 !
That’s really great !

I’m using 1.8.2 and im getting the following error when I make the change to visitorlog.tpl

There is an error. Please report the message (Piwik 1.8.2) and full backtrace in the Piwik forums (please do a Search first as it might have been reported already!).

Notice: Undefined index: city in /home/kkpixels/public_html/stats/tmp/templates_c/%%46^462^46227F0A%%visitorLog.tpl.php on line 147

Backtrace -->
#0 Piwik_ErrorHandler(…) called at [/home/kkpixels/public_html/stats/tmp/templates_c/%%46^462^46227F0A%%visitorLog.tpl.php:147]#1 include(…) called at [/home/kkpixels/public_html/stats/libs/Smarty/Smarty.class.php:1263]#2 Smarty->fetch(…) called at [/home/kkpixels/public_html/stats/core/View.php:133]#3 Piwik_View->render(…) called at [/home/kkpixels/public_html/stats/core/Controller.php:153]#4 Piwik_Controller->renderView(…) called at [/home/kkpixels/public_html/stats/plugins/Live/Controller.php:84]#5 Piwik_Live_Controller->getVisitorLog(…) called at [:]#6 call_user_func_array(…) called at [/home/kkpixels/public_html/stats/core/FrontController.php:138]#7 Piwik_FrontController->dispatch(…) called at [/home/kkpixels/public_html/stats/index.php:53]

Perhaps I have missed something, can someone just list all the changes that are needed to make this work on 1.8.2

Or better still, just post the changed files so anyone can just copy these into their installation

Many thanks in anticipation

You have to update files like explained in the first two messages.
Based on your error message, it seems that the field ‘city’ does not exist. You have to add the line in Visitor.php and also add the function that will return the city name (check in the second message of the post)

Then i updated LastVist.tpl with my own code

Good workaround, it should be included in core as the Geo IP features should work out of the box if Piwik is to be considered as a viable alternative to GA. And of course, non-developer users might not be able to edit the files in question.

Thanks for taking the time to post this solution.

Thanks for the help chris, seems i had updated my local server copy and not uploaded all changes!

For what it’s worth whilst I dont mind updating files it can get more difficult when the changes are for much older versions, my vote would also be that is should be in core as GeoIP is being integrated so much

One other change I made was to visitorLog.tpl
If you change the line (around line number 46) that reads


&nbsp;<img src="{$visitor.columns.countryFlag}" title="{$visitor.columns.country}, Provider {$visitor.columns.provider}" />

and change it to


&nbsp;<img src="{$visitor.columns.countryFlag}" title="{$visitor.columns.city}, {$visitor.columns.country}, Provider {$visitor.columns.provider}" />

Not only will you have the right country flag from the earlier update, but on hovering over the flag you will also see the city (from GeoIP) not just the country. Might be of interest to some people

Cheers

Thank you for the tip. I will update my files !

Found That - it´s work perfect!
http://www.statstory.com/get-more-accurate-geo-data-in-piwik/

Does anyone already modified the code for the new build-in-GeoIP?

How do I get the variables?

Fount it! It’s in the ToolTip on the Flag:

{$visitor.location}

Data is generated by the following function (visitor.php):


	function getPrettyLocation()
	{
		$parts = array();
		
		// add city if it's known
		if ($this->details['location_city'] != '')
		{
			$parts[] = $this->details['location_city'];
		}
		
		// add region if it's known
		$region = $this->details['location_region'];
		if ($region != '' && $region != Piwik_UserCountry::UNKNOWN_CODE)
		{
			$parts[] = Piwik_UserCountry_LocationProvider_GeoIp::getRegionNameFromCodes(
				$this->details['location_country'], $region);
		}
		
		// add country & return concatenated result
		$parts[] = $this->getCountryName();
		return implode(', ', $parts);
	}

Good news: GeoIP is now integrated in Piwik, enabling Accurate Visitors Geolocation in your Analytics reports. To enable GeoIP go to the Settings > Geolocation admin page, and follow the short instructions.

You can also get an even more accurate Country & City Database from here to enjoy top accuracy in detecting your visitors locations.

See also the documentation about Geolocation - Analytics Reports in Piwik.

We need YOUR help! We are running a crowd funding campaign to raise funds to implement the detailed Visitors Maps of Countries, Regions and Cities (for all countries)!

These maps will be beautiful, usable, and built using open standards SVG+JS. They will show detailed visitor count, conversion rates, by Country but also (New!) by city and region.

Pledge now at: http://crowdfunding.piwik.org/analytics-maps-world-country-city-region/

There will be no more flash in Piwik and we will add amazing new Maps of your visitors.

Piwik needs you!

Here’s a neat and clean website that helped me implement a very good geo ip and zip code distance locator.
http://www.ezcmd.com