به نام حضرت حق
کلاس :
namespace FreshCore\Analytics;
use Piwik\API\Request;
use Piwik\FrontController;
define('PIWIK_INCLUDE_PATH', realpath('analytics'));
define('PIWIK_USER_PATH', realpath('analytics'));
define('PIWIK_ENABLE_DISPATCH', false);
define('PIWIK_ENABLE_ERROR_HANDLER', false);
define('PIWIK_ENABLE_SESSION_START', false);
require_once PIWIK_INCLUDE_PATH . "/index.php";
require_once PIWIK_INCLUDE_PATH . "/core/API/Request.php";
class Analytics
{
public function Start()
{
$environment = new \Piwik\Application\Environment(null);
$environment->init();
FrontController::getInstance()->init();
}
public function Request(array $attributes)
{
// Merge attributes
$text = '';
foreach ($attributes as $k => $v) {
$text .= '&' . $k . '=' . $v;
}
// This inits the API Request with the specified parameters
$request = new Request('module=API&idSite=1&format=JSON&token_auth={Yout token}' . $text);
// Calls the API and fetch JSON data back
return $request->process();
}
}
نمونه کدی که من ازش استفاده می کنم :
function ajax_analytics_page_watch_summery()
{
$id = (isset($_POST['id'])) ? $_POST['id'] : '';
$video = Video::where('video_id', $id)->first();
if ($video) {
$date = new DateTime($video->date);
with(new FreshCore\Analytics\Analytics)->Start();
// Get video visit hits
$visit = json_decode(with(new FreshCore\Analytics\Analytics)->Request(array(
'method' => 'CustomVariables.getCustomVariables',
'period' => 'range',
'date' => $date->format('Y-m-d') . ',' . with(new DateTime)->format('Y-m-d'),
'label' => 'videos>@' . $id
)), true);
// Get video play hits
$play = json_decode(with(new FreshCore\Analytics\Analytics)->Request(array(
'method' => 'Events.getName',
'period' => 'range',
'date' => $date->format('Y-m-d') . ',' . with(new DateTime)->format('Y-m-d'),
'label' => $id . '>@play'
)), true);
echo json_encode(array(
'visit' => (isset($visit[0]['nb_visits']) ? tr_num($visit[0]['nb_visits'], 'fa') : '0'),
'play' => (isset($play[0]['nb_visits']) ? tr_num($play[0]['nb_visits'], 'fa') : '0')
), JSON_UNESCAPED_UNICODE);
}
}
من از این کد برای دریافت تعداد بازدید کنندگان از یک ویدیو و همچنین تعداد افرادی که بر روی دکمه پلی ویدیو کلیک کردن استفاده می کنم با کمک Events و CustomVariables در واسط برنامه نویسی Piwik به کمک جاوا اسکریپت .