translator = $translator;
parent::__construct();
}
public function getSparklines()
{
$metrics = $this->getReferrersVisitorsByType();
$distinctMetrics = $this->getDistinctReferrersMetrics();
$numberFormatter = NumberFormatter::getInstance();
$totalVisits = array_sum($metrics);
foreach ($metrics as $name => $value) {
// calculate percent of total, if there were any visits
if ($value != 0 && $totalVisits != 0) {
$percentName = $name . 'Percent';
$metrics[$percentName] = round(($value / $totalVisits) * 100, 0);
}
}
// calculate evolution for visit metrics & distinct metrics
list($lastPeriodDate, $ignore) = Range::getLastDate();
if ($lastPeriodDate !== false) {
$date = Common::getRequestVar('date');
$period = Common::getRequestVar('period');
$prettyDate = self::getPrettyDate($date, $period);
$prettyLastPeriodDate = self::getPrettyDate($lastPeriodDate, $period);
// visit metrics
$previousValues = $this->getReferrersVisitorsByType($lastPeriodDate);
$metrics = $this->addEvolutionPropertiesToView($prettyDate, $metrics, $prettyLastPeriodDate, $previousValues);
// distinct metrics
$previousValues = $this->getDistinctReferrersMetrics($lastPeriodDate);
$distinctMetrics = $this->addEvolutionPropertiesToView($prettyDate, $distinctMetrics, $prettyLastPeriodDate, $previousValues);
}
/** @var Sparklines $view */
$view = ViewDataTable\Factory::build(Sparklines::ID, $api = '', $controller = '', $force = true, $loadUserParams = false);
// DIRECT ENTRY
$metrics['visitorsFromDirectEntry'] = $numberFormatter->formatNumber($metrics['visitorsFromDirectEntry']);
$values = array($metrics['visitorsFromDirectEntry']);
$descriptions = array(Piwik::translate('Referrers_TypeDirectEntries'));
if (!empty($metrics['visitorsFromDirectEntryPercent'])) {
$metrics['visitorsFromDirectEntryPercent'] = $numberFormatter->formatPercent($metrics['visitorsFromDirectEntryPercent'], $precision = 1);
$values[] = $metrics['visitorsFromDirectEntryPercent'];
$descriptions[] = Piwik::translate('Referrers_XPercentOfVisits');
}
$directEntryParams = $this->getReferrerSparklineParams(Common::REFERRER_TYPE_DIRECT_ENTRY);
$view->config->addSparkline($directEntryParams, $values, $descriptions, @$metrics['visitorsFromDirectEntryEvolution']);
// WEBSITES
$metrics['visitorsFromWebsites'] = $numberFormatter->formatNumber($metrics['visitorsFromWebsites']);
$values = array($metrics['visitorsFromWebsites']);
$descriptions = array(Piwik::translate('Referrers_TypeWebsites'));
if (!empty($metrics['visitorsFromWebsitesPercent'])) {
$metrics['visitorsFromWebsitesPercent'] = $numberFormatter->formatPercent($metrics['visitorsFromWebsitesPercent'], $precision = 1);
$values[] = $metrics['visitorsFromWebsitesPercent'];
$descriptions[] = Piwik::translate('Referrers_XPercentOfVisits');
}
$searchEngineParams = $this->getReferrerSparklineParams(Common::REFERRER_TYPE_WEBSITE);
$view->config->addSparkline($searchEngineParams, $values, $descriptions, @$metrics['visitorsFromWebsitesEvolution']);
// SEARCH ENGINES
$metrics['visitorsFromSearchEngines'] = $numberFormatter->formatNumber($metrics['visitorsFromSearchEngines']);
$values = array($metrics['visitorsFromSearchEngines']);
$descriptions = array(Piwik::translate('Referrers_TypeSearchEngines'));
if (!empty($metrics['visitorsFromSearchEnginesPercent'])) {
$metrics['visitorsFromSearchEnginesPercent'] = $numberFormatter->formatPercent($metrics['visitorsFromSearchEnginesPercent'], $precision = 1);
$values[] = $metrics['visitorsFromSearchEnginesPercent'];
$descriptions[] = Piwik::translate('Referrers_XPercentOfVisits');
}
$searchEngineParams = $this->getReferrerSparklineParams(Common::REFERRER_TYPE_SEARCH_ENGINE);
$view->config->addSparkline($searchEngineParams, $values, $descriptions, @$metrics['visitorsFromSearchEnginesEvolution']);
// CAMPAIGNS
$metrics['visitorsFromCampaigns'] = $numberFormatter->formatNumber($metrics['visitorsFromCampaigns']);
$values = array($metrics['visitorsFromCampaigns']);
$descriptions = array(Piwik::translate('Referrers_TypeCampaigns'));
if (!empty($metrics['visitorsFromCampaignsPercent'])) {
$metrics['visitorsFromCampaignsPercent'] = $numberFormatter->formatPercent($metrics['visitorsFromCampaignsPercent'], $precision = 1);
$values[] = $metrics['visitorsFromCampaignsPercent'];
$descriptions[] = Piwik::translate('Referrers_XPercentOfVisits');
}
$searchEngineParams = $this->getReferrerSparklineParams(Common::REFERRER_TYPE_CAMPAIGN);
$view->config->addSparkline($searchEngineParams, $values, $descriptions, @$metrics['visitorsFromCampaignsEvolution']);
// DISTINCT SEARCH ENGINES
$sparklineParams = $this->getDistinctSparklineUrlParams('getLastDistinctSearchEnginesGraph');
$value = $distinctMetrics['numberDistinctSearchEngines'];
$value = $numberFormatter->formatNumber($value);
$description = Piwik::translate('Referrers_DistinctSearchEngines');
$view->config->addSparkline($sparklineParams, $value, $description, @$distinctMetrics['numberDistinctSearchEnginesEvolution']);
// DISTINCT WEBSITES
$sparklineParams = $this->getDistinctSparklineUrlParams('getLastDistinctWebsitesGraph');
$distinctMetrics['numberDistinctWebsites'] = $numberFormatter->formatNumber($distinctMetrics['numberDistinctWebsites']);
$distinctMetrics['numberDistinctWebsitesUrls'] = $numberFormatter->formatNumber($distinctMetrics['numberDistinctWebsitesUrls']);
$values = array($distinctMetrics['numberDistinctWebsites'], $distinctMetrics['numberDistinctWebsitesUrls']);
$descriptions = array(Piwik::translate('Referrers_DistinctWebsites'), Piwik::translate('Referrers_UsingNDistinctUrls'));
$view->config->addSparkline($sparklineParams, $values, $descriptions, @$distinctMetrics['numberDistinctWebsitesEvolution']);
// DISTINCT KEYWORDS
$sparklineParams = $this->getDistinctSparklineUrlParams('getLastDistinctKeywordsGraph');
$value = $distinctMetrics['numberDistinctKeywords'];
$value = $numberFormatter->formatNumber($value);
$description = Piwik::translate('Referrers_DistinctKeywords');
$view->config->addSparkline($sparklineParams, $value, $description, @$distinctMetrics['numberDistinctKeywordsEvolution']);
// DISTINCT CAMPAIGNS
$sparklineParams = $this->getDistinctSparklineUrlParams('getLastDistinctCampaignsGraph');
$value = $distinctMetrics['numberDistinctCampaigns'];
$value = $numberFormatter->formatNumber($value);
$description = Piwik::translate('Referrers_DistinctCampaigns');
$view->config->addSparkline($sparklineParams, $value, $description, @$distinctMetrics['numberDistinctCampaignsEvolution']);
return $view->render();
}
private function getDistinctSparklineUrlParams($action)
{
return array('module' => $this->pluginName, 'action' => $action);
}
protected function getReferrersVisitorsByType($date = false)
{
if ($date === false) {
$date = Common::getRequestVar('date', false);
}
// we disable the queued filters because here we want to get the visits coming from search engines
// if the filters were applied we would have to look up for a label looking like "Search Engines"
// which is not good when we have translations
$dataTableReferrersType = Request::processRequest(
"Referrers.getReferrerType", array('disable_queued_filters' => '1', 'date' => $date));
$nameToColumnId = array(
'visitorsFromSearchEngines' => Common::REFERRER_TYPE_SEARCH_ENGINE,
'visitorsFromDirectEntry' => Common::REFERRER_TYPE_DIRECT_ENTRY,
'visitorsFromWebsites' => Common::REFERRER_TYPE_WEBSITE,
'visitorsFromCampaigns' => Common::REFERRER_TYPE_CAMPAIGN,
);
$return = array();
foreach ($nameToColumnId as $nameVar => $columnId) {
$value = 0;
$row = $dataTableReferrersType->getRowFromLabel($columnId);
if ($row !== false) {
$value = $row->getColumn(Metrics::INDEX_NB_VISITS);
}
$return[$nameVar] = $value;
}
return $return;
}
protected $referrerTypeToLabel = array(
Common::REFERRER_TYPE_DIRECT_ENTRY => 'Referrers_DirectEntry',
Common::REFERRER_TYPE_SEARCH_ENGINE => 'Referrers_SearchEngines',
Common::REFERRER_TYPE_WEBSITE => 'Referrers_Websites',
Common::REFERRER_TYPE_CAMPAIGN => 'Referrers_Campaigns',
);
public function getEvolutionGraph($typeReferrer = false, array $columns = array(), array $defaultColumns = array())
{
$view = $this->getLastUnitGraph($this->pluginName, __FUNCTION__, 'Referrers.getReferrerType');
$view->config->add_total_row = true;
// configure displayed columns
if (empty($columns)) {
$columns = Common::getRequestVar('columns', false);
if (false !== $columns) {
$columns = Piwik::getArrayFromApiParameter($columns);
}
}
if (false !== $columns) {
$columns = !is_array($columns) ? array($columns) : $columns;
}
if (!empty($columns)) {
$view->config->columns_to_display = $columns;
} elseif (empty($view->config->columns_to_display) && !empty($defaultColumns)) {
$view->config->columns_to_display = $defaultColumns;
}
// configure selectable columns
// todo: should use SettingsPiwik::isUniqueVisitorsEnabled
if (Common::getRequestVar('period', false) == 'day') {
$selectable = array('nb_visits', 'nb_uniq_visitors', 'nb_users', 'nb_actions');
} else {
$selectable = array('nb_visits', 'nb_actions');
}
$view->config->selectable_columns = $selectable;
// configure displayed rows
$visibleRows = Common::getRequestVar('rows', false);
if ($visibleRows !== false) {
// this happens when the row picker has been used
$visibleRows = Piwik::getArrayFromApiParameter($visibleRows);
// typeReferrer is redundant if rows are defined, so make sure it's not used
$view->config->custom_parameters['typeReferrer'] = false;
} else {
// use $typeReferrer as default
if ($typeReferrer === false) {
$typeReferrer = Common::getRequestVar('typeReferrer', Common::REFERRER_TYPE_DIRECT_ENTRY);
}
$label = self::getTranslatedReferrerTypeLabel($typeReferrer);
$total = $this->translator->translate('General_Total');
if (!empty($view->config->rows_to_display)) {
$visibleRows = $view->config->rows_to_display;
} else {
$visibleRows = array($label, $total);
}
$view->requestConfig->request_parameters_to_modify['rows'] = $label . ',' . $total;
}
$view->config->row_picker_match_rows_by = 'label';
$view->config->rows_to_display = $visibleRows;
$view->config->documentation = $this->translator->translate('Referrers_EvolutionDocumentation') . '
'
. $this->translator->translate('General_BrokenDownReportDocumentation') . '
'
. $this->translator->translate('Referrers_EvolutionDocumentationMoreInfo', '"'
. $this->translator->translate('Referrers_ReferrerTypes') . '"');
return $this->renderView($view);
}
public function getLastDistinctSearchEnginesGraph()
{
$view = $this->getLastUnitGraph($this->pluginName, __FUNCTION__, "Referrers.getNumberOfDistinctSearchEngines");
$view->config->translations['Referrers_distinctSearchEngines'] = ucfirst($this->translator->translate('Referrers_DistinctSearchEngines'));
$view->config->columns_to_display = array('Referrers_distinctSearchEngines');
return $this->renderView($view);
}
public function getLastDistinctKeywordsGraph()
{
$view = $this->getLastUnitGraph($this->pluginName, __FUNCTION__, "Referrers.getNumberOfDistinctKeywords");
$view->config->translations['Referrers_distinctKeywords'] = ucfirst($this->translator->translate('Referrers_DistinctKeywords'));
$view->config->columns_to_display = array('Referrers_distinctKeywords');
return $this->renderView($view);
}
public function getLastDistinctWebsitesGraph()
{
$view = $this->getLastUnitGraph($this->pluginName, __FUNCTION__, "Referrers.getNumberOfDistinctWebsites");
$view->config->translations['Referrers_distinctWebsites'] = ucfirst($this->translator->translate('Referrers_DistinctWebsites'));
$view->config->columns_to_display = array('Referrers_distinctWebsites');
return $this->renderView($view);
}
public function getLastDistinctCampaignsGraph()
{
$view = $this->getLastUnitGraph($this->pluginName, __FUNCTION__, "Referrers.getNumberOfDistinctCampaigns");
$view->config->translations['Referrers_distinctCampaigns'] = ucfirst($this->translator->translate('Referrers_DistinctCampaigns'));
$view->config->columns_to_display = array('Referrers_distinctCampaigns');
return $this->renderView($view);
}
function getKeywordsForPage()
{
Piwik::checkUserHasViewAccess($this->idSite);
$requestUrl = '&date=previous1'
. '&period=week'
. '&idSite=' . $this->idSite;
$topPageUrlRequest = $requestUrl
. '&method=Actions.getPageUrls'
. '&filter_limit=50'
. '&format=original';
$request = new Request($topPageUrlRequest);
$request = $request->process();
/** @var $request Map */
$tables = $request->getDataTables();;
$topPageUrl = false;
$first = key($tables);
if (!empty($first)) {
$topPageUrls = $tables[$first];
$topPageUrls = $topPageUrls->getRowsMetadata('url');
$tmpTopPageUrls = array_values($topPageUrls);
$topPageUrl = current($tmpTopPageUrls);
}
if (empty($topPageUrl)) {
$topPageUrl = $this->site->getMainUrl();
}
$url = $topPageUrl;
// HTML
$api = SettingsPiwik::getPiwikUrl()
. '?module=API&method=Referrers.getKeywordsForPageUrl'
. '&format=json'
. '&filter_limit=10'
. '&token_auth=' . Piwik::getCurrentUserTokenAuth();
$api .= $requestUrl;
$code = '
// This function will call the API to get best keyword for current URL.
// Then it writes the list of best keywords in a HTML list
function DisplayTopKeywords($url = "")
{
// Do not spend more than 1 second fetching the data
@ini_set("default_socket_timeout", $timeout = 1);
// Get the Keywords data
$url = empty($url) ? "http://". $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] : $url;
$api = "' . $api . '&url=" . urlencode($url);
$keywords = @json_decode(file_get_contents($api), $assoc = true);
Common::sendHeader(\'Content-Type: text/html; charset=utf-8\', true);
if ($keywords === false || isset($keywords["result"])) {
// DEBUG ONLY: uncomment for troubleshooting an empty output (the URL output reveals the token_auth)
// echo "Error while fetching the Top Keywords from Piwik";
return;
}
// Display the list in HTML
$url = htmlspecialchars($url, ENT_QUOTES);
$output = "
This widget is designed to work in your website directly. This widget makes it easy to use Piwik to automatically display the list of Top Keywords, for each of your website Page URLs.
Example API URL - For example if you would like to get the top 10 keywords, used last week, to land on the page $topPageUrl, in format JSON: you would dynamically fetch the data using this API request URL. Make sure you encode the 'url' parameter in the URL.
PHP Function ready to use! - If you use PHP on your website, we have prepared a small code snippet that you can copy paste in your Website PHP files. You can then simply call the function DisplayTopKeywords();
anywhere in your template, at the bottom of the content or in your blog sidebar.
If you run this code in your page $topPageUrl, it would output the following:";
echo "
Here is the PHP function that you can paste in your pages:
"; echo "Notes: You can for example edit the code to to make the Top search keywords link to your Website search result pages.
On medium to large traffic websites, we recommend to cache this data, as to minimize the performance impact of calling the Piwik API on each page view.