Thread: [Phphtmllib-devel] SF.net SVN: phphtmllib:[3533] trunk/open2300/lib/external/PiwikTracker.inc
Status: Beta
Brought to you by:
hemna
From: <he...@us...> - 2010-07-14 19:24:51
|
Revision: 3533 http://phphtmllib.svn.sourceforge.net/phphtmllib/?rev=3533&view=rev Author: hemna Date: 2010-07-14 19:24:43 +0000 (Wed, 14 Jul 2010) Log Message: ----------- added Added Paths: ----------- trunk/open2300/lib/external/PiwikTracker.inc Added: trunk/open2300/lib/external/PiwikTracker.inc =================================================================== --- trunk/open2300/lib/external/PiwikTracker.inc (rev 0) +++ trunk/open2300/lib/external/PiwikTracker.inc 2010-07-14 19:24:43 UTC (rev 3533) @@ -0,0 +1,359 @@ +<?php +/** + * Piwik - Open source web analytics + * + * Client to record visits, page views, Goals, in a Piwik server. + * For more information, see http://piwik.org/docs/tracking-api/ + * + * Note: Piwik Cookies are not forwarded in the request and from the response + * + * @license released under BSD License http://www.opensource.org/licenses/bsd-license.php + * @version $Id: PiwikTracker.php 2453 2010-07-08 19:07:59Z matt $ + * @link http://piwik.org/docs/tracking-api/ + */ +class PiwikTracker +{ + /** + * Piwik base URL, for example http://example.org/piwik/ + * Must be set before using the class by calling + * PiwikTracker::$url = 'http://yourwebsite.org/piwik/'; + * + * @var string + */ + static public $URL = ''; + + const VERSION = 1; + + /** + * Builds a PiwikTracker object, used to track visits, pages and Goal conversions + * for a specific website, by using the Piwik Tracking API. + * + * @param $idSite Id site to be tracked + * @param $apiUrl If set, will overwrite PiwikTracker::$url + */ + function __construct( $idSite, $apiUrl = false ) + { + $this->userAgent = false; + $this->localHour = false; + $this->localMinute = false; + $this->localSecond = false; + $this->hasCookies = false; + $this->plugins = false; + $this->customData = false; + $this->forcedDatetime = false; + + $this->idSite = $idSite; + $this->urlReferer = @$_SERVER['HTTP_REFERER']; + $this->pageUrl = self::getCurrentUrl(); + $this->ip = @$_SERVER['REMOTE_ADDR']; + $this->acceptLanguage = @$_SERVER['HTTP_ACCEPT_LANGUAGE']; + $this->userAgent = @$_SERVER['HTTP_USER_AGENT']; + if(!empty($apiUrl)) { + self::$URL = $apiUrl; + } + } + + /** + * Tracks a page view + * + * @param $documentTitle string Page view name as it will appear in Piwik reports + * @return string Response + */ + public function doTrackPageView( $documentTitle ) + { + $url = $this->getUrlTrackPageView($documentTitle); + return $this->sendRequest($url); + } + + /** + * Tracks a Goal + * + * @param $idGoal int Id Goal to record a conversion + * @param $revenue int Revenue for this conversion + * @return string Response + */ + public function doTrackGoal($idGoal, $revenue = false) + { + $url = $this->getUrlTrackGoal($idGoal, $revenue); + return $this->sendRequest($url); + } + + /** + * Tracks a download or outlink + * + * @param $actionUrl URL of the download or outlink + * @param $actionType Type of the action: 'download' or 'link' + * @return string Response + */ + public function doTrackAction($actionUrl, $actionType) + { + // Referer could be udpated to be the current URL temporarily (to mimic JS behavior) + $url = $this->getUrlTrackAction($actionUrl, $actionType); + return $this->sendRequest($url); + } + + public function setUrl( $url ) + { + $this->pageUrl = $url; + } + + public function setUrlReferer( $url ) + { + $this->urlReferer = $url; + } + + public function setCustomData( $data ) + { + $this->customData = json_encode($data); + } + + public function setBrowserLanguage( $acceptLanguage ) + { + $this->acceptLanguage = $acceptLanguage; + } + + public function setUserAgent($userAgent) + { + $this->userAgent = $userAgent; + } + + public function setLocalTime($time) + { + list($hour, $minute, $second) = explode(':', $time); + $this->localHour = (int)$hour; + $this->localMinute = (int)$minute; + $this->localSecond = (int)$second; + } + + public function setResolution($width, $height) + { + $this->width = $width; + $this->height = $height; + } + + public function setBrowserHasCookies( $bool ) + { + $this->hasCookies = $bool ; + } + + public function setPlugins($flash = false, $java = false, $director = false, $quickTime = false, $realPlayer = false, $pdf = false, $windowsMedia = false, $gears = false, $silverlight = false) + { + $this->plugins = + '&fla='.(int)$flash. + '&java='.(int)$java. + '&dir='.(int)$director. + '&qt='.(int)$quickTime. + '&realp='.(int)$realPlayer. + '&pdf='.(int)$pdf. + '&wma='.(int)$windowsMedia. + '&gears='.(int)$gears. + '&ag='.(int)$silverlight + ; + } + + // Note: this will only work when used in tests + public function setForceVisitDateTime($dateTime) + { + $this->forcedDatetime = $dateTime; + } + + // Note: this will only work when used in tests + public function setIp($ip) + { + $this->ip = $ip; + } + + /** + * @see doTrackPageView() + */ + public function getUrlTrackPageView( $documentTitle = false ) + { + $url = $this->getRequest( $this->idSite ); + if(!empty($documentTitle)) { + $url .= '&action_name=' . urlencode($documentTitle); + } + return $url; + } + + /** + * @see doTrackGoal() + */ + public function getUrlTrackGoal($idGoal, $revenue = false) + { + $url = $this->getRequest( $this->idSite ); + $url .= '&idgoal=' . $idGoal; + if(!empty($revenue)) { + $url .= '&revenue=' . $revenue; + } + return $url; + } + + /** + * @see doTrackAction() + */ + public function getUrlTrackAction($actionUrl, $actionType) + { + $url = $this->getRequest( $this->idSite ); + $url .= '&'.$actionType.'=' . $actionUrl . + '&redirect=0'; + + return $url; + } + + protected function sendRequest($url) + { + if(function_exists('stream_context_create')) { + $timeout = 600; // Allow debug while blocking the request + $stream_options = array( + 'http' => array( + 'user_agent' => $this->userAgent, + 'header' => "Accept-Language: " . $this->acceptLanguage . "\r\n" . + "Cookie: \r\n", + 'timeout' => $timeout, // PHP 5.2.1 + ) + ); + $ctx = stream_context_create($stream_options); + } + $response = @file_get_contents($url, 0, $ctx); + return $response; + } + + protected function getRequest( $idSite ) + { + if(empty(self::$URL)) + { + throw new Exception('You must first set the Piwik Tracker URL by calling PiwikTracker::$URL = \'http://your-website.org/piwik/\';'); + } + if(strpos(self::$URL, '/piwik.php') === false) + { + self::$URL .= '/piwik.php'; + } + $url = self::$URL . + '?idsite=' . $idSite . + '&rec=1' . + '&apiv=' . self::VERSION . + '&url=' . urlencode($this->pageUrl) . + '&urlref=' . urlencode($this->urlReferer) . + '&rand=' . mt_rand() . + + // Optional since debugger can be triggered remotely + '&XDEBUG_SESSION_START=' . @$_GET['XDEBUG_SESSION_START'] . + '&KEY=' . @$_GET['KEY'] . + + // only allowed in tests (see tests/integration/piwik.php) + (!empty($this->ip) ? '&cip=' . $this->ip : '') . + (!empty($this->forcedDatetime) ? '&cdt=' . urlencode($this->forcedDatetime) : '') . + + // These parameters are set by the JS, but optional when using API + (!empty($this->plugins) ? $this->plugins : '') . + (($this->localHour !== false && $this->localMinute !== false && $this->localSecond !== false) ? '&h=' . $this->localHour . '&m=' . $this->localMinute . '&s=' . $this->localSecond : '' ). + (!empty($this->width) && !empty($this->height) ? '&res=' . $this->width . 'x' . $this->height : '') . + (!empty($this->hasCookies) ? '&cookie=' . $this->hasCookies : '') . + (!empty($this->customData) ? '&data=' . $this->customData : '') + ; + return $url; + } + + /** + * If current URL is "http://example.org/dir1/dir2/index.php?param1=value1¶m2=value2" + * will return "/dir1/dir2/index.php" + * + * @return string + */ + static protected function getCurrentScriptName() + { + $url = ''; + if( !empty($_SERVER['PATH_INFO']) ) { + $url = $_SERVER['PATH_INFO']; + } + else if( !empty($_SERVER['REQUEST_URI']) ) { + if( ($pos = strpos($_SERVER['REQUEST_URI'], '?')) !== false ) { + $url = substr($_SERVER['REQUEST_URI'], 0, $pos); + } else { + $url = $_SERVER['REQUEST_URI']; + } + } + if(empty($url)) { + $url = $_SERVER['SCRIPT_NAME']; + } + + if($url[0] !== '/') { + $url = '/' . $url; + } + return $url; + } + + + /** + * If the current URL is 'http://example.org/dir1/dir2/index.php?param1=value1¶m2=value2" + * will return 'http' + * + * @return string 'https' or 'http' + */ + static protected function getCurrentScheme() + { + if(isset($_SERVER['HTTPS']) + && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] === true)) + { + return 'https'; + } + return 'http'; + } + + /** + * If current URL is "http://example.org/dir1/dir2/index.php?param1=value1¶m2=value2" + * will return "http://example.org" + * + * @return string + */ + static protected function getCurrentHost() + { + if(isset($_SERVER['HTTP_HOST'])) { + return $_SERVER['HTTP_HOST']; + } + return 'unknown'; + } + + /** + * If current URL is "http://example.org/dir1/dir2/index.php?param1=value1¶m2=value2" + * will return "?param1=value1¶m2=value2" + * + * @return string + */ + static protected function getCurrentQueryString() + { + $url = ''; + if(isset($_SERVER['QUERY_STRING']) + && !empty($_SERVER['QUERY_STRING'])) + { + $url .= '?'.$_SERVER['QUERY_STRING']; + } + return $url; + } + + /** + * Returns the current full URL (scheme, host, path and query string. + * + * @return string + */ + static protected function getCurrentUrl() + { + return self::getCurrentScheme() . '://' + . self::getCurrentHost() + . self::getCurrentScriptName() + . self::getCurrentQueryString(); + } +} + + +function Piwik_getUrlTrackPageView( $idSite, $documentTitle = false ) +{ + $tracker = new PiwikTracker($idSite); + return $tracker->getUrlTrackPageView($documentTitle); +} +function Piwik_getUrlTrackGoal($idSite, $idGoal, $revenue = false) +{ + $tracker = new PiwikTracker($idSite); + return $tracker->getUrlTrackGoal($idGoal, $revenue); +} + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |