AnalyticsCollectorTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <?php
  2. /**
  3. * Hits through the collector into sessions and events, and back out through reports.
  4. * Test hits are dated in September 2001, far from real traffic, and deleted afterwards.
  5. */
  6. class AnalyticsCollectorTest extends BxDolIntegrationTestCase
  7. {
  8. // 2001-09-09 01:46:40 UTC
  9. const NOW = 1000000000;
  10. const UA_SAFARI = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Safari/605.1.15';
  11. protected $_oModule;
  12. protected $_aOptions = [];
  13. protected $_aGoals = [];
  14. protected $_aFunnels = [];
  15. protected function setUp(): void
  16. {
  17. parent::setUp();
  18. if (!BxDolModuleQuery::getInstance()->isEnabledByName('bx_analytics'))
  19. $this->markTestSkipped('bx_analytics is not installed');
  20. $this->_oModule = BxDolModule::getInstance('bx_analytics');
  21. $this->_oModule->getCollector(); // loads the collector classes
  22. $this->_oModule->getFilter([]);
  23. $this->_cleanup();
  24. foreach (['bx_analytics_track_actions' => 'on', 'bx_analytics_track_bots' => 'on', 'bx_analytics_tracking' => 'on', 'bx_analytics_platforms' => 'core-web,next-web,rn-ios,rn-android,api', 'bx_analytics_respect_dnt' => 'on', 'bx_analytics_link_members' => 'on', 'bx_analytics_identity' => 'cookieless', 'bx_analytics_exclude_ips' => '', 'bx_analytics_exclude_levels' => '', 'bx_analytics_exclude_paths' => '', 'bx_analytics_geo_source' => 'headers'] as $sName => $sValue)
  25. $this->_setOption($sName, $sValue);
  26. }
  27. protected function tearDown(): void
  28. {
  29. if ($this->_oModule) {
  30. $this->_cleanup();
  31. foreach ($this->_aOptions as $sName => $sValue)
  32. setParam($sName, $sValue);
  33. }
  34. parent::tearDown();
  35. }
  36. public function testPageviewsMakeOneSession()
  37. {
  38. $aServer = $this->_server('81.2.69.142', ['HTTP_CF_IPCOUNTRY' => 'GB']);
  39. $this->assertSame(1, $this->_collect($aServer, [
  40. 'platform' => 'core-web',
  41. 'referrer' => 'https://www.google.com/',
  42. 'language' => 'en-GB',
  43. 'screen' => '1440x900',
  44. 'hits' => [['type' => 'pageview', 'url' => 'http://una.test/page/explore?utm_source=nl&utm_medium=email&gclid=1', 'title' => 'Explore']],
  45. ], self::NOW));
  46. $this->assertSame(2, $this->_collect($aServer, [
  47. 'platform' => 'core-web',
  48. 'hits' => [
  49. ['type' => 'leave', 'url' => 'http://una.test/page/explore', 'engaged' => 15000],
  50. ['type' => 'pageview', 'url' => 'http://una.test/page/view-post?id=5', 'title' => 'Post'],
  51. ],
  52. ], self::NOW + 60));
  53. $aSessions = $this->_sessions();
  54. $this->assertCount(1, $aSessions);
  55. $a = $aSessions[0];
  56. $this->assertSame('core-web', $a['platform']);
  57. $this->assertSame(2, (int)$a['pageviews']);
  58. $this->assertSame(0, (int)$a['bounce']);
  59. $this->assertSame(60, (int)$a['duration']);
  60. $this->assertSame('/page/explore', $a['entry_path'], 'campaign tags and click ids are not part of the path');
  61. $this->assertSame('/page/view-post?id=5', $a['exit_path']);
  62. $this->assertSame(['google.com', 'email', 'nl', 'GB', 'Safari', 'macOS', 'desktop', '1440x900'], [$a['referrer_host'], $a['channel'], $a['utm_source'], $a['country'], $a['browser'], $a['os'], $a['device'], $a['screen']]);
  63. $aEvents = BxDolDb::getInstance()->getAll("SELECT `path`, `engaged` FROM `bx_analytics_events` WHERE `session_id` = :id ORDER BY `id`", ['id' => $a['id']]);
  64. $this->assertEquals([['path' => '/page/explore', 'engaged' => 15000], ['path' => '/page/view-post?id=5', 'engaged' => 0]], $aEvents, 'engaged time goes to the page that was left');
  65. }
  66. public function testDroppedHits()
  67. {
  68. $aHit = ['platform' => 'core-web', 'hits' => [['type' => 'pageview', 'url' => 'http://una.test/']]];
  69. $this->assertSame(0, $this->_collect($this->_server('81.2.69.142', [], 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'), $aHit, self::NOW), 'bot');
  70. $this->assertSame(0, $this->_collect($this->_server('81.2.69.142', ['HTTP_DNT' => '1']), $aHit, self::NOW), 'Do Not Track');
  71. $this->assertSame(0, $this->_collect($this->_server('81.2.69.142'), ['platform' => 'core'] + $aHit, self::NOW), 'the server platform is not for clients');
  72. $this->assertSame(0, $this->_collect($this->_server('81.2.69.142'), ['platform' => 'windows-phone'] + $aHit, self::NOW), 'unknown platform');
  73. $this->assertSame(0, $this->_collect($this->_server('81.2.69.142'), ['platform' => 'core-web', 'hits' => [['type' => 'pageview', 'url' => 'https://elsewhere.example/']]], self::NOW, ['same_origin' => true]), 'another site');
  74. $this->assertSame(0, $this->_collect($this->_server('81.2.69.142', ['HTTP_SEC_FETCH_SITE' => 'cross-site']), $aHit, self::NOW, ['same_origin' => true]), 'cross-site request');
  75. $this->_setOption('bx_analytics_exclude_ips', '81.2.69.0/24');
  76. $this->assertSame(0, $this->_collect($this->_server('81.2.69.142'), $aHit, self::NOW), 'excluded address');
  77. $this->_setOption('bx_analytics_platforms', 'next-web');
  78. $this->assertSame(0, $this->_collect($this->_server('93.184.216.34'), $aHit, self::NOW), 'platform switched off');
  79. $this->assertCount(0, $this->_sessions());
  80. }
  81. public function testNativeAppHit()
  82. {
  83. $this->assertSame(1, $this->_collect($this->_server('93.184.216.34', [], 'okhttp/4.12.0'), [
  84. 'platform' => 'rn-android',
  85. 'app' => 'com.neo.so',
  86. 'app_version' => '1.5.0',
  87. 'os_version' => '15',
  88. 'device' => 'tablet',
  89. 'hits' => [['type' => 'pageview', 'url' => '/page/explore', 'title' => 'Explore']],
  90. ], self::NOW), 'an HTTP library user agent is fine for an app');
  91. $a = $this->_sessions()[0];
  92. $this->assertSame(['rn-android', 'com.neo.so', '1.5.0', 'Android', '15', 'tablet', 'com.neo.so'], [$a['platform'], $a['app'], $a['app_version'], $a['os'], $a['os_version'], $a['device'], $a['hostname']]);
  93. }
  94. public function testTrackerConfig()
  95. {
  96. $this->assertTrue($this->_oModule->serviceIsSafeService('get_tracker_config'), 'NEO reads it over the API');
  97. $a = $this->_oModule->serviceGetTrackerConfig('rn-ios');
  98. $this->assertSame(['enabled', 'persistent', 'respect_dnt', 'session_timeout'], array_keys($a), 'settings only, nothing about the site or the viewer');
  99. $this->assertTrue($a['enabled']);
  100. $this->assertFalse($a['persistent']);
  101. $this->assertTrue($a['respect_dnt']);
  102. $this->assertSame($this->_oModule->_oConfig->getSessionTimeout(), $a['session_timeout']);
  103. $this->assertFalse($this->_oModule->serviceGetTrackerConfig('core')['enabled'], 'the server platform is not for clients');
  104. $this->assertFalse($this->_oModule->serviceGetTrackerConfig('windows-phone')['enabled'], 'unknown platform');
  105. $this->_setOption('bx_analytics_identity', 'persistent');
  106. $this->assertTrue($this->_oModule->serviceGetTrackerConfig('next-web')['persistent']);
  107. $this->_setOption('bx_analytics_platforms', 'next-web');
  108. $this->assertFalse($this->_oModule->serviceGetTrackerConfig('rn-ios')['enabled'], 'platform switched off');
  109. $this->_setOption('bx_analytics_tracking', '');
  110. $a = $this->_oModule->serviceGetTrackerConfig('next-web');
  111. $this->assertFalse($a['enabled'], 'tracking off');
  112. $this->assertFalse($a['persistent'], 'no visitor id to keep when nothing is sent');
  113. }
  114. public function testGuestSignsInAndIsForgotten()
  115. {
  116. $iProfileId = 987654321;
  117. $aServer = $this->_server('93.184.216.34');
  118. $aHit = ['platform' => 'next-web', 'hits' => [['type' => 'pageview', 'url' => 'https://neo.test/']]];
  119. $this->_collect($aServer, $aHit, self::NOW);
  120. $this->_collect($aServer, $aHit, self::NOW + 30, ['profile_id' => $iProfileId]);
  121. $aSessions = $this->_sessions();
  122. $this->assertCount(1, $aSessions, 'signing in continues the visit');
  123. $this->assertSame($iProfileId, (int)$aSessions[0]['profile_id']);
  124. $sMemberVisitor = $aSessions[0]['visitor'];
  125. $this->_oModule->onProfileDelete($iProfileId);
  126. $aSessions = $this->_sessions();
  127. $this->assertSame(0, (int)$aSessions[0]['profile_id']);
  128. $this->assertNotSame($sMemberVisitor, $aSessions[0]['visitor']);
  129. $this->assertSame(0, (int)BxDolDb::getInstance()->getOne("SELECT COUNT(*) FROM `bx_analytics_events` WHERE `profile_id` = :p", ['p' => $iProfileId]));
  130. }
  131. public function testCookielessVisitorsChangeDaily()
  132. {
  133. $aServer = $this->_server('93.184.216.34');
  134. $aHit = ['platform' => 'core-web', 'hits' => [['type' => 'pageview', 'url' => 'http://una.test/']]];
  135. $this->_collect($aServer, $aHit, self::NOW);
  136. $this->_collect($aServer, $aHit, self::NOW + 2 * 86400);
  137. $aSessions = $this->_sessions();
  138. $this->assertCount(2, $aSessions);
  139. $this->assertNotSame($aSessions[0]['visitor'], $aSessions[1]['visitor']);
  140. }
  141. public function testReports()
  142. {
  143. // two visitors: one bounces from a search, one views two pages on iOS
  144. $this->_collect($this->_server('81.2.69.142'), ['platform' => 'core-web', 'referrer' => 'https://duckduckgo.com/', 'hits' => [['type' => 'pageview', 'url' => 'http://una.test/a']]], self::NOW);
  145. $this->_collect($this->_server('93.184.216.34', [], 'NEO/1 CFNetwork/1.0 Darwin/24.0.0'), ['platform' => 'rn-ios', 'app' => 'com.neo.so', 'hits' => [['type' => 'pageview', 'url' => '/a'], ['type' => 'pageview', 'url' => '/b']]], self::NOW + 10);
  146. $aParams = ['range' => 'custom', 'from' => '2001-09-09', 'to' => '2001-09-09'];
  147. $aOverview = $this->_oModule->getReports()->getOverview($this->_filter($aParams));
  148. $this->assertSame(2, $aOverview['kpis']['visitors']['value']);
  149. $this->assertSame(3, $aOverview['kpis']['pageviews']['value']);
  150. $this->assertEquals(50, $aOverview['kpis']['bounce_rate']['value']);
  151. $this->assertSame('hour', $aOverview['bucket']);
  152. $this->assertSame(24, count($aOverview['series']));
  153. $aIos = $this->_oModule->getReports()->getOverview($this->_filter($aParams + ['platform' => 'rn-ios']));
  154. $this->assertSame(1, $aIos['kpis']['visitors']['value']);
  155. $aPages = $this->_oModule->getReports()->getTop($this->_filter($aParams), ['group' => 'pages', 'tab' => 'pages']);
  156. $this->assertSame(['/a', '/b'], array_column($aPages['items'], 'value'));
  157. $this->assertSame([2, 1], array_column($aPages['items'], 'visitors'));
  158. $aChannels = $this->_oModule->getReports()->getTop($this->_filter($aParams + ['f' => ['path' => '/b']]), ['group' => 'sources', 'tab' => 'channels']);
  159. $this->assertSame(['direct'], array_column($aChannels['items'], 'value'), 'a page filter keeps the sessions that saw it');
  160. $aPlatforms = $this->_oModule->getReports()->getTop($this->_filter($aParams), ['group' => 'platforms', 'tab' => 'platforms']);
  161. $this->assertEqualsCanonicalizing(['core-web', 'rn-ios'], array_column($aPlatforms['items'], 'value'));
  162. $aTable = $this->_oModule->getReports()->getTable($this->_filter($aParams), ['table' => 'pages']);
  163. $this->assertSame(['/a', '/b'], array_column($aTable['rows'], 'label'));
  164. $this->assertSame(2, $aTable['rows'][0]['values']['entries'], 'both sessions entered on /a');
  165. $this->assertSame(1, $aTable['rows'][0]['values']['exits'], 'the one-page session left from /a');
  166. $this->assertEquals(50, $aTable['rows'][0]['values']['bounce_rate']);
  167. $this->assertSame(2, $aTable['total']);
  168. $this->assertFalse($this->_oModule->getReports()->getTable($this->_filter($aParams), ['table' => 'nope']));
  169. $this->assertFalse($this->_oModule->getReportData('nope', $aParams), 'unknown reports are refused');
  170. }
  171. public function testBotsAreCountedApart()
  172. {
  173. $oCollector = new BxAnalyticsCollector($this->_oModule, new BxAnalyticsRequest($this->_server('66.249.66.1')), self::NOW);
  174. $this->assertTrue($oCollector->collectBot('Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)', '/page/explore?utm_source=x'));
  175. $this->assertTrue($oCollector->collectBot('Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)', '/page/explore'));
  176. $this->assertTrue($oCollector->collectBot('Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ChatGPT-User/1.0; +https://openai.com/bot)', '/'));
  177. $this->assertFalse($oCollector->collectBot(self::UA_SAFARI, '/'), 'browsers are not bots');
  178. // a headless browser that runs the tracker is a bot, not a visit
  179. $this->assertSame(0, $this->_collect($this->_server('66.249.66.1', [], 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/120.0.0.0 Safari/537.36'), ['platform' => 'core-web', 'hits' => [['type' => 'pageview', 'url' => 'http://una.test/']]], self::NOW));
  180. $this->assertCount(0, $this->_sessions());
  181. $aFilter = $this->_filter(['range' => 'custom', 'from' => '2001-09-09', 'to' => '2001-09-09'])->get();
  182. $this->assertEquals(['hits' => 4, 'bots' => 3], $this->_oModule->_oDb->getBotTotals($aFilter));
  183. $this->assertEquals(['hits' => 1, 'bots' => 1], $this->_oModule->_oDb->getBotTotals($aFilter, ['ai_agent']));
  184. $aPages = $this->_oModule->_oDb->getBotPages($aFilter, ['search'], 10);
  185. $this->assertEquals([['path' => '/page/explore', 'hits' => 2]], $aPages, 'campaign tags are not part of the path');
  186. $aReport = $this->_oModule->getReports()->getBots($this->_filter(['range' => 'custom', 'from' => '2001-09-09', 'to' => '2001-09-09']), ['tab_bots' => 'ai']);
  187. $this->assertSame('ai', $aReport['tab']);
  188. $this->assertSame(['ChatGPT'], array_column($aReport['bots']['rows'], 'label'));
  189. }
  190. public function testSiteActionsJoinTheVisit()
  191. {
  192. $iProfileId = 987654321;
  193. $aServer = $this->_server('93.184.216.34', ['REQUEST_METHOD' => 'POST']);
  194. $this->_collect($aServer, ['platform' => 'rn-ios', 'app' => 'com.neo.so', 'hits' => [['type' => 'pageview', 'url' => '/page/create-post']]], self::NOW, ['profile_id' => $iProfileId]);
  195. $oCollector = new BxAnalyticsCollector($this->_oModule, new BxAnalyticsRequest($aServer), self::NOW + 20);
  196. $this->assertTrue($oCollector->collectAction('content_add', 'bx_posts', 77, $iProfileId, ['platform' => 'rn-ios']));
  197. // a renewal by cron has no visit and runs on the server's own platform
  198. $oCron = new BxAnalyticsCollector($this->_oModule, new BxAnalyticsRequest([]), self::NOW + 30);
  199. $this->assertTrue($oCron->collectAction('login', 'account', 1, $iProfileId));
  200. $aSessions = $this->_sessions();
  201. $this->assertCount(1, $aSessions);
  202. $this->assertSame(1, (int)$aSessions[0]['events']);
  203. $this->assertSame(0, (int)$aSessions[0]['bounce'], 'doing something is not a bounce');
  204. $aEvents = BxDolDb::getInstance()->getAll("SELECT `session_id`, `type`, `name`, `module`, `object_id`, `platform` FROM `bx_analytics_events` WHERE `type` = 'una' AND `ts` < :to ORDER BY `id`", ['to' => self::NOW + 86400]);
  205. $this->assertEquals([
  206. ['session_id' => $aSessions[0]['id'], 'type' => 'una', 'name' => 'content_add', 'module' => 'bx_posts', 'object_id' => 77, 'platform' => 'rn-ios'],
  207. ['session_id' => 0, 'type' => 'una', 'name' => 'login', 'module' => 'account', 'object_id' => 1, 'platform' => 'core'],
  208. ], $aEvents);
  209. $aParams = ['range' => 'custom', 'from' => '2001-09-09', 'to' => '2001-09-09'];
  210. $oReports = $this->_oModule->getReports();
  211. $aList = $oReports->getEvents($this->_filter($aParams), ['tab_events' => 'site']);
  212. $this->assertSame('list', $aList['view']);
  213. $this->assertCount(2, $aList['rows']);
  214. $aDetail = $oReports->getEvents($this->_filter($aParams), ['event' => 'una|content_add|bx_posts']);
  215. $this->assertSame('detail', $aDetail['view']);
  216. $this->assertSame(1, $aDetail['tiles'][0]['value']);
  217. $this->assertSame([['label' => $oReports->getLabel('platform', 'rn-ios'), 'count' => 1, 'share' => 100.0]], $aDetail['platforms']);
  218. $this->assertNull($oReports->getEvents($this->_filter($aParams), ['event' => 'pageview|x|y'])['event'] ?? null, 'pageviews are not events');
  219. $aSessionsList = $oReports->getSessions($this->_filter($aParams));
  220. $this->assertCount(1, $aSessionsList['rows']);
  221. $aSessionDetail = $oReports->getSessions($this->_filter($aParams), ['session' => $aSessions[0]['id']]);
  222. $this->assertSame(['pageview', 'una'], array_column($aSessionDetail['timeline'], 'type'));
  223. $this->assertSame(20, $aSessionDetail['timeline'][1]['offset']);
  224. $aUsers = $oReports->getUsers($this->_filter($aParams));
  225. $this->assertSame(1, count($aUsers['rows']));
  226. $this->assertSame(['param' => 'user', 'value' => $iProfileId], $aUsers['rows'][0]['open']);
  227. }
  228. public function testJourneysAndRetention()
  229. {
  230. // three members' visits: / -> /a -> /b, / -> /a -> /c, / -> /d, the first coming back a week later
  231. $aVisits = [
  232. [987654321, self::NOW, ['/', '/a', '/b']],
  233. [987654322, self::NOW + 100, ['/', '/a', '/a', '/c']],
  234. [987654323, self::NOW + 200, ['/', '/d']],
  235. [987654321, self::NOW + 7 * 86400, ['/']],
  236. ];
  237. foreach ($aVisits as list($iProfileId, $iTime, $aPages))
  238. foreach ($aPages as $i => $sPage)
  239. $this->_collect($this->_server('93.184.216.' . ($iProfileId % 100)), ['platform' => 'core-web', 'hits' => [['type' => 'pageview', 'url' => 'http://una.test' . $sPage]]], $iTime + $i * 10, ['profile_id' => $iProfileId]);
  240. $oReports = $this->_oModule->getReports();
  241. $oFilter = $this->_filter(['range' => 'custom', 'from' => '2001-09-09', 'to' => '2001-09-09']);
  242. $aJourneys = $oReports->getJourneys($oFilter);
  243. $this->assertSame('/', $aJourneys['from'], 'the top entry page by default');
  244. $this->assertSame(3, $aJourneys['from_sessions']);
  245. $this->assertSame([['page' => '/a', 'sessions' => 2, 'share' => 66.7], ['page' => '/d', 'sessions' => 1, 'share' => 33.3]], $aJourneys['next']);
  246. $aVia = $oReports->getJourneys($oFilter, ['via' => '/a']);
  247. $this->assertEqualsCanonicalizing(['/b', '/c'], array_column($aVia['after'], 'page'), 'a page reloaded in place counts once');
  248. $aPaths = array_map(function ($a) { return implode(' ', $a['pages']); }, $aJourneys['paths']);
  249. $this->assertContains('/ /a /b', $aPaths);
  250. $aRetention = $oReports->getRetention(new BxAnalyticsFilter($this->_oModule, ['range' => '7d'], self::NOW + 8 * 86400));
  251. $aCohorts = array_values(array_filter($aRetention['rows'], function ($a) { return $a['size'] > 0; }));
  252. $this->assertCount(1, $aCohorts);
  253. $this->assertSame(3, $aCohorts[0]['size']);
  254. $this->assertEquals(100, $aCohorts[0]['weeks'][0]);
  255. $this->assertEquals(33.3, $aCohorts[0]['weeks'][1], 'one of three came back the next week');
  256. }
  257. public function testGoalsAndFunnels()
  258. {
  259. $oDb = $this->_oModule->_oDb;
  260. $iPricing = $oDb->saveGoal(0, ['name' => 'Test 2001 pricing', 'type' => 'page', 'pattern' => '/pricing*']);
  261. $iBuy = $oDb->saveGoal(0, ['name' => 'Test 2001 buy', 'type' => 'event', 'pattern' => 'buy']);
  262. $iFunnel = $oDb->saveFunnel(0, 'Test 2001 funnel', [
  263. ['type' => 'page', 'pattern' => '/'],
  264. ['type' => 'page', 'pattern' => '/pricing*'],
  265. ['type' => 'event', 'pattern' => 'buy'],
  266. ]);
  267. $this->_aGoals = [$iPricing, $iBuy];
  268. $this->_aFunnels = [$iFunnel];
  269. $this->assertGreaterThan(0, $iPricing);
  270. $this->assertGreaterThan(0, $iFunnel);
  271. // A: / -> /pricing -> buy, B: / -> buy -> /pricing?plan=pro, C: /pricing only
  272. $aVisits = [
  273. ['93.184.216.1', [['pageview', '/'], ['pageview', '/pricing'], ['event', '/pricing', 'buy']]],
  274. ['93.184.216.2', [['pageview', '/'], ['event', '/', 'buy'], ['pageview', '/pricing?plan=pro']]],
  275. ['93.184.216.3', [['pageview', '/pricing']]],
  276. ];
  277. foreach ($aVisits as $iVisit => list($sIp, $aHits))
  278. foreach ($aHits as $i => $aHit)
  279. $this->_collect($this->_server($sIp), ['platform' => 'core-web', 'hits' => [['type' => $aHit[0], 'url' => 'http://una.test' . $aHit[1], 'name' => isset($aHit[2]) ? $aHit[2] : '']]], self::NOW + $iVisit * 100 + $i * 10);
  280. $oReports = $this->_oModule->getReports();
  281. $oFilter = $this->_filter(['range' => 'custom', 'from' => '2001-09-09', 'to' => '2001-09-09']);
  282. $aGoals = array_column($oReports->getGoals($oFilter)['goals'], null, 'id');
  283. $this->assertSame(3, $aGoals[$iPricing]['conversions'], 'a page goal with * matches the query string too');
  284. $this->assertEquals(100, $aGoals[$iPricing]['rate']);
  285. $this->assertSame(2, $aGoals[$iBuy]['conversions']);
  286. $this->assertEquals(66.67, $aGoals[$iBuy]['rate']);
  287. $aFunnel = $oReports->getFunnels($oFilter, ['funnel' => $iFunnel]);
  288. $this->assertSame($iFunnel, $aFunnel['funnel']['id']);
  289. $this->assertSame([2, 2, 1], array_column($aFunnel['steps'], 'sessions'), 'a step counts only after the previous one');
  290. $this->assertEquals([100, 100, 50], array_column($aFunnel['steps'], 'rate'));
  291. $this->assertEquals([null, 0, 50], array_column($aFunnel['steps'], 'drop'));
  292. $this->assertTrue($oDb->deleteGoal($iBuy));
  293. $this->assertFalse((bool)$oDb->getGoal($iBuy));
  294. $this->assertTrue($oDb->deleteFunnel($iFunnel));
  295. $this->assertNotContains($iFunnel, array_column($oReports->getFunnels($oFilter)['funnels'], 'id'), 'a deleted funnel is not listed');
  296. }
  297. protected function _collect($aServer, $aPayload, $iNow, $aContext = [])
  298. {
  299. $oCollector = new BxAnalyticsCollector($this->_oModule, new BxAnalyticsRequest($aServer), $iNow);
  300. return $oCollector->collect($aPayload, $aContext + ['platform' => 'api']);
  301. }
  302. protected function _filter($aParams)
  303. {
  304. return new BxAnalyticsFilter($this->_oModule, $aParams, self::NOW + 86400);
  305. }
  306. protected function _server($sIp, $aExtra = [], $sUserAgent = self::UA_SAFARI)
  307. {
  308. return array_merge(['REMOTE_ADDR' => $sIp, 'HTTP_USER_AGENT' => $sUserAgent], $aExtra);
  309. }
  310. protected function _sessions()
  311. {
  312. return BxDolDb::getInstance()->getAll("SELECT * FROM `bx_analytics_sessions` WHERE `started` < :to ORDER BY `id`", ['to' => self::NOW + 30 * 86400]);
  313. }
  314. protected function _setOption($sName, $sValue)
  315. {
  316. if (!array_key_exists($sName, $this->_aOptions))
  317. $this->_aOptions[$sName] = getParam($sName);
  318. setParam($sName, $sValue);
  319. }
  320. protected function _cleanup()
  321. {
  322. $oDb = BxDolDb::getInstance();
  323. $iTo = self::NOW + 30 * 86400;
  324. $oDb->query("DELETE FROM `bx_analytics_events` WHERE `ts` < :to", ['to' => $iTo]);
  325. $oDb->query("DELETE FROM `bx_analytics_sessions` WHERE `started` < :to", ['to' => $iTo]);
  326. $oDb->query("DELETE FROM `bx_analytics_salts` WHERE `day` < '2002-01-01'");
  327. $oDb->query("DELETE FROM `bx_analytics_bots` WHERE `day` < '2002-01-01'");
  328. $oDb->query("DELETE FROM `bx_analytics_bot_pages` WHERE `day` < '2002-01-01'");
  329. foreach ($this->_aGoals as $iId)
  330. $this->_oModule->_oDb->deleteGoal($iId);
  331. foreach ($this->_aFunnels as $iId)
  332. $this->_oModule->_oDb->deleteFunnel($iId);
  333. $this->_aGoals = $this->_aFunnels = [];
  334. }
  335. }