Mam coś takiego:
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
- Kod: Zaznacz cały
DisplayTopKeywords();
anywhere in your template, at the bottom of the content or in your blog sidebar. If you run this code in your page
http://www.cermarket.pl, it would output the following:
Here is the PHP function that you can paste in your pages:
- Kod: Zaznacz cały
<?php
// 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 = "http://www.analytics.cermarket.pl/?module=API&method=Referers.getKeywordsForPageUrl&format=php&filter_limit=10&token_auth=d4e600549edf36e742831f2bb8304521&date=previous1&period=week&idSite=1&url=" . urlencode($url);
$keywords = @unserialize(file_get_contents($api));
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 <a href='$api'>Top Keywords from Piwik</a>";
return;
}
// Display the list in HTML
$url = htmlspecialchars($url, ENT_QUOTES);
$output = "<h2>Top Keywords for <a href='$url'>$url</a></h2><ul>";
foreach($keywords as $keyword) {
$output .= "<li>". $keyword[0]. "</li>";
}
if(empty($keywords)) { $output .= "Nothing yet..."; }
$output .= "</ul>";
echo $output;
}
DisplayTopKeywords();
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.
Chciałbym to zrobić jak należy dlatego założyłem wątek.