This is Google specific. The rank in the search result is actually submitted by Google in the referrer as the cd=
param. There's a breakdown of all the available params available.
The visitor log report in Piwik just extracts this information from the referrer URL that is stored for the visit. Have a look at the source of the Live plugin:
function getKeywordPosition()
{
if($this->getRefererType() == 'search'
&& strpos($this->getRefererName(), 'Google') !== false)
{
$url = @parse_url($this->details['referer_url']);
if(empty($url['query']))
{
return null;
}
$position = Piwik_Common::getParameterFromQueryString($url['query'], 'cd');
if(!empty($position))
{
return $position;
}
}
return null;
}