Search code examples
matomo

generate report on keyword rank


On the detailed visitor log piwik is able to some the position the keyword was listed on a search engine.

eg:

Where is this stored in the database? I have been looking around for it but not able to find anything in the logs table nor the visitors table.


Solution

  • 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;
    }