PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/plugins/Live/Visitor.php

https://github.com/quarkness/piwik
PHP | 439 lines | 359 code | 57 blank | 23 comment | 24 complexity | 8e76c392386a831c7f7c07279198f210 MD5 | raw file
  1. <?php
  2. /**
  3. * Piwik - Open source web analytics
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. * @version $Id$
  8. *
  9. * @category Piwik_Plugins
  10. * @package Piwik_Live
  11. */
  12. /**
  13. * @see plugins/Referers/functions.php
  14. * @see plugins/UserCountry/functions.php
  15. * @see plugins/UserSettings/functions.php
  16. * @see plugins/Provider/functions.php
  17. */
  18. require_once PIWIK_INCLUDE_PATH . '/plugins/Referers/functions.php';
  19. require_once PIWIK_INCLUDE_PATH . '/plugins/UserCountry/functions.php';
  20. require_once PIWIK_INCLUDE_PATH . '/plugins/UserSettings/functions.php';
  21. require_once PIWIK_INCLUDE_PATH . '/plugins/Provider/functions.php';
  22. /**
  23. *
  24. * @package Piwik_Live
  25. */
  26. class Piwik_Live_Visitor
  27. {
  28. const DELIMITER_PLUGIN_NAME = ", ";
  29. function __construct($visitorRawData)
  30. {
  31. $this->details = $visitorRawData;
  32. }
  33. function getAllVisitorDetails()
  34. {
  35. return array(
  36. 'idSite' => $this->getIdSite(),
  37. 'idVisit' => $this->getIdVisit(),
  38. 'visitIp' => $this->getIp(),
  39. 'visitorId' => $this->getVisitorId(),
  40. 'visitorType' => $this->getVisitorReturning(),
  41. 'visitorTypeIcon' => $this->getVisitorReturningIcon(),
  42. 'visitConverted' => $this->isVisitorGoalConverted(),
  43. 'visitConvertedIcon' => $this->getVisitorGoalConvertedIcon(),
  44. 'visitEcommerceStatus' => $this->getVisitEcommerceStatus(),
  45. 'visitEcommerceStatusIcon' => $this->getVisitEcommerceStatusIcon(),
  46. 'actions' => $this->getNumberOfActions(),
  47. // => false are placeholders to be filled in API later
  48. 'actionDetails' => false,
  49. 'customVariables' => $this->getCustomVariables(),
  50. 'goalConversions' => false,
  51. 'siteCurrency' => false,
  52. 'siteCurrencySymbol' => false,
  53. // all time entries
  54. 'serverDate' => $this->getServerDate(),
  55. 'visitLocalTime' => $this->getVisitLocalTime(),
  56. 'firstActionTimestamp' => $this->getTimestampFirstAction(),
  57. 'lastActionTimestamp' => $this->getTimestampLastAction(),
  58. 'lastActionDateTime' => $this->getDateTimeLastAction(),
  59. // standard attributes
  60. 'visitDuration' => $this->getVisitLength(),
  61. 'visitDurationPretty' => $this->getVisitLengthPretty(),
  62. 'visitCount' => $this->getVisitCount(),
  63. 'daysSinceLastVisit' => $this->getDaysSinceLastVisit(),
  64. 'daysSinceFirstVisit' => $this->getDaysSinceFirstVisit(),
  65. 'daysSinceLastEcommerceOrder' => $this->getDaysSinceLastEcommerceOrder(),
  66. 'country' => $this->getCountryName(),
  67. 'countryFlag' => $this->getCountryFlag(),
  68. 'continent' => $this->getContinent(),
  69. 'provider' => $this->getProvider(),
  70. 'providerUrl' => $this->getProviderUrl(),
  71. 'referrerType' => $this->getRefererType(),
  72. 'referrerTypeName' => $this->getRefererTypeName(),
  73. 'referrerName' => $this->getRefererName(),
  74. 'referrerKeyword' => $this->getKeyword(),
  75. 'referrerKeywordPosition' => $this->getKeywordPosition(),
  76. 'referrerUrl' => $this->getRefererUrl(),
  77. 'referrerSearchEngineUrl' => $this->getSearchEngineUrl(),
  78. 'referrerSearchEngineIcon' => $this->getSearchEngineIcon(),
  79. 'operatingSystem' => $this->getOperatingSystem(),
  80. 'operatingSystemShortName' => $this->getOperatingSystemShortName(),
  81. 'operatingSystemIcon' => $this->getOperatingSystemIcon(),
  82. 'browserFamily' => $this->getBrowserFamily(),
  83. 'browserFamilyDescription' => $this->getBrowserFamilyDescription(),
  84. 'browserName' => $this->getBrowser(),
  85. 'browserIcon' => $this->getBrowserIcon(),
  86. 'screenType' => $this->getScreenType(),
  87. 'resolution' => $this->getResolution(),
  88. 'screenTypeIcon' => $this->getScreenTypeIcon(),
  89. 'plugins' => $this->getPlugins(),
  90. 'pluginsIcons' => $this->getPluginIcons(),
  91. );
  92. }
  93. function getVisitorId()
  94. {
  95. if(isset($this->details['idvisitor']))
  96. {
  97. return bin2hex($this->details['idvisitor']);
  98. }
  99. return false;
  100. }
  101. function getVisitLocalTime()
  102. {
  103. return $this->details['visitor_localtime'];
  104. }
  105. function getVisitCount()
  106. {
  107. return $this->details['visitor_count_visits'];
  108. }
  109. function getDaysSinceLastVisit()
  110. {
  111. return $this->details['visitor_days_since_last'];
  112. }
  113. function getDaysSinceLastEcommerceOrder()
  114. {
  115. return $this->details['visitor_days_since_order'];
  116. }
  117. function getDaysSinceFirstVisit()
  118. {
  119. return $this->details['visitor_days_since_first'];
  120. }
  121. function getServerDate()
  122. {
  123. return date('Y-m-d', strtotime($this->details['visit_last_action_time']));
  124. }
  125. function getIp()
  126. {
  127. if(isset($this->details['location_ip']))
  128. {
  129. return Piwik_IP::N2P($this->details['location_ip']);
  130. }
  131. return false;
  132. }
  133. function getIdVisit()
  134. {
  135. return $this->details['idvisit'];
  136. }
  137. function getIdSite()
  138. {
  139. return $this->details['idsite'];
  140. }
  141. function getNumberOfActions()
  142. {
  143. return $this->details['visit_total_actions'];
  144. }
  145. function getVisitLength()
  146. {
  147. return $this->details['visit_total_time'];
  148. }
  149. function getVisitLengthPretty()
  150. {
  151. return Piwik::getPrettyTimeFromSeconds($this->details['visit_total_time']);
  152. }
  153. function getVisitorReturning()
  154. {
  155. $type = $this->details['visitor_returning'];
  156. return $type == 2
  157. ? 'returningCustomer'
  158. : ($type == 1
  159. ? 'returning'
  160. : 'new');
  161. }
  162. function getVisitorReturningIcon()
  163. {
  164. $type = $this->getVisitorReturning();
  165. if($type == 'returning'
  166. || $type =='returningCustomer')
  167. {
  168. return "plugins/Live/templates/images/returningVisitor.gif";
  169. }
  170. return null;
  171. }
  172. function getTimestampFirstAction()
  173. {
  174. return strtotime($this->details['visit_first_action_time']);
  175. }
  176. function getTimestampLastAction()
  177. {
  178. return strtotime($this->details['visit_last_action_time']);
  179. }
  180. function getCountryName()
  181. {
  182. return Piwik_CountryTranslate($this->details['location_country']);
  183. }
  184. function getCountryFlag()
  185. {
  186. return Piwik_getFlagFromCode($this->details['location_country']);
  187. }
  188. function getContinent()
  189. {
  190. return Piwik_ContinentTranslate($this->details['location_continent']);
  191. }
  192. function getCustomVariables()
  193. {
  194. $customVariables = array();
  195. for($i = 1; $i <= Piwik_Tracker::MAX_CUSTOM_VARIABLES; $i++)
  196. {
  197. if(!empty($this->details['custom_var_k'.$i])
  198. && !empty($this->details['custom_var_v'.$i]))
  199. {
  200. $customVariables[$i] = array(
  201. 'customVariableName'.$i => $this->details['custom_var_k'.$i],
  202. 'customVariableValue'.$i => $this->details['custom_var_v'.$i],
  203. );
  204. }
  205. }
  206. return $customVariables;
  207. }
  208. function getRefererType()
  209. {
  210. return Piwik_getRefererTypeFromShortName($this->details['referer_type']);
  211. }
  212. function getRefererTypeName()
  213. {
  214. return Piwik_getRefererTypeLabel($this->details['referer_type']);
  215. }
  216. function getKeyword()
  217. {
  218. return urldecode($this->details['referer_keyword']);
  219. }
  220. function getRefererUrl()
  221. {
  222. return $this->details['referer_url'];
  223. }
  224. function getKeywordPosition()
  225. {
  226. if($this->getRefererType() == 'search'
  227. && strpos($this->getRefererName(), 'Google') !== false)
  228. {
  229. $url = $this->getRefererUrl();
  230. $url = @parse_url($url);
  231. if(empty($url['query']))
  232. {
  233. return null;
  234. }
  235. $position = Piwik_Common::getParameterFromQueryString($url['query'], 'cd');
  236. if(!empty($position))
  237. {
  238. return $position;
  239. }
  240. }
  241. return null;
  242. }
  243. function getRefererName()
  244. {
  245. return urldecode($this->details['referer_name']);
  246. }
  247. function getSearchEngineUrl()
  248. {
  249. if($this->getRefererType() == 'search'
  250. && !empty($this->details['referer_name']))
  251. {
  252. return Piwik_getSearchEngineUrlFromName($this->details['referer_name']);
  253. }
  254. return null;
  255. }
  256. function getSearchEngineIcon()
  257. {
  258. $searchEngineUrl = $this->getSearchEngineUrl();
  259. if( !is_null($searchEngineUrl) )
  260. {
  261. return Piwik_getSearchEngineLogoFromUrl($searchEngineUrl);
  262. }
  263. return null;
  264. }
  265. function getPlugins()
  266. {
  267. $plugins = array(
  268. 'config_pdf',
  269. 'config_flash',
  270. 'config_java',
  271. 'config_director',
  272. 'config_quicktime',
  273. 'config_realplayer',
  274. 'config_windowsmedia',
  275. 'config_gears',
  276. 'config_silverlight',
  277. );
  278. $pluginShortNames = array();
  279. foreach($plugins as $plugin)
  280. {
  281. if($this->details[$plugin] == 1)
  282. {
  283. $pluginShortName = substr($plugin, 7);
  284. $pluginShortNames[] = $pluginShortName;
  285. }
  286. }
  287. return implode(self::DELIMITER_PLUGIN_NAME, $pluginShortNames);
  288. }
  289. function getPluginIcons()
  290. {
  291. $pluginNames = $this->getPlugins();
  292. if( !empty($pluginNames) )
  293. {
  294. $pluginNames = explode(self::DELIMITER_PLUGIN_NAME, $pluginNames);
  295. $pluginIcons = array();
  296. foreach($pluginNames as $plugin) {
  297. $pluginIcons[] = array("pluginIcon" =>Piwik_getPluginsLogo($plugin), "pluginName" =>$plugin);
  298. }
  299. return $pluginIcons;
  300. }
  301. return null;
  302. }
  303. function getOperatingSystem()
  304. {
  305. return Piwik_getOSLabel($this->details['config_os']);
  306. }
  307. function getOperatingSystemShortName()
  308. {
  309. return Piwik_getOSShortLabel($this->details['config_os']);
  310. }
  311. function getOperatingSystemIcon()
  312. {
  313. return Piwik_getOSLogo($this->details['config_os']);
  314. }
  315. function getBrowserFamilyDescription()
  316. {
  317. return Piwik_getBrowserTypeLabel($this->getBrowserFamily());
  318. }
  319. function getBrowserFamily()
  320. {
  321. return Piwik_getBrowserFamily($this->details['config_browser_name']);
  322. }
  323. function getBrowser()
  324. {
  325. return Piwik_getBrowserLabel($this->details['config_browser_name'] . ";" . $this->details['config_browser_version']);
  326. }
  327. function getBrowserIcon()
  328. {
  329. return Piwik_getBrowsersLogo($this->details['config_browser_name'] . ";" . $this->details['config_browser_version']);
  330. }
  331. function getScreenType()
  332. {
  333. return Piwik_getScreenTypeFromResolution($this->details['config_resolution']);
  334. }
  335. function getResolution()
  336. {
  337. return $this->details['config_resolution'];
  338. }
  339. function getScreenTypeIcon()
  340. {
  341. return Piwik_getScreensLogo($this->getScreenType());
  342. }
  343. function getProvider()
  344. {
  345. return Piwik_getHostnameName( @$this->details['location_provider']);
  346. }
  347. function getProviderUrl()
  348. {
  349. return Piwik_getHostnameUrl( @$this->details['location_provider']);
  350. }
  351. function getDateTimeLastAction()
  352. {
  353. return date('Y-m-d H:i:s', strtotime($this->details['visit_last_action_time']));
  354. }
  355. function getVisitEcommerceStatusIcon()
  356. {
  357. $status = $this->getVisitEcommerceStatus();
  358. if(in_array($status, array('ordered', 'orderedThenAbandonedCart')))
  359. {
  360. return "themes/default/images/ecommerceOrder.gif";
  361. }
  362. elseif($status == 'abandonedCart')
  363. {
  364. return "themes/default/images/ecommerceAbandonedCart.gif";
  365. }
  366. return null;
  367. }
  368. function getVisitEcommerceStatus()
  369. {
  370. return Piwik_API_API::getVisitEcommerceStatusFromId($this->details['visit_goal_buyer']);
  371. }
  372. function getVisitorGoalConvertedIcon()
  373. {
  374. return $this->isVisitorGoalConverted()
  375. ? "themes/default/images/goal.png"
  376. : null;
  377. }
  378. function isVisitorGoalConverted()
  379. {
  380. return $this->details['visit_goal_converted'];
  381. }
  382. }