BxAnalyticsReports.php 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Analytics Analytics
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Report data as plain arrays. The classic templates, Studio and the NEO elements all render
  13. * from these, so a number means the same thing everywhere.
  14. *
  15. * Every report is a method get<Report>(BxAnalyticsFilter $oFilter, array $aParams) that returns
  16. * an array, or false when the parameters name nothing it knows.
  17. */
  18. class BxAnalyticsReports
  19. {
  20. /**
  21. * Tabbed top lists: group => [tab => dimension]. A block shows one group.
  22. */
  23. protected $_aGroups = [
  24. 'pages' => ['pages' => 'path', 'titles' => 'title', 'entry' => 'entry_path', 'exit' => 'exit_path', 'hostnames' => 'hostname'],
  25. 'sources' => ['referrers' => 'referrer_host', 'channels' => 'channel', 'utm_source' => 'utm_source', 'utm_medium' => 'utm_medium', 'utm_campaign' => 'utm_campaign'],
  26. 'locations' => ['countries' => 'country', 'regions' => 'region', 'cities' => 'city', 'languages' => 'language'],
  27. 'devices' => ['browsers' => 'browser', 'devices' => 'device', 'os' => 'os', 'screens' => 'screen'],
  28. 'platforms' => ['platforms' => 'platform', 'apps' => 'app', 'app_versions' => 'app_version'],
  29. ];
  30. /**
  31. * Full tables with every metric: table => [tab => dimension].
  32. */
  33. protected $_aTables = [
  34. 'pages' => ['pages' => 'path', 'titles' => 'title', 'entry' => 'entry_path', 'exit' => 'exit_path', 'hostnames' => 'hostname'],
  35. 'sources' => ['referrers' => 'referrer_host', 'referrer_urls' => 'referrer', 'channels' => 'channel', 'utm_source' => 'utm_source', 'utm_medium' => 'utm_medium', 'utm_campaign' => 'utm_campaign', 'utm_term' => 'utm_term', 'utm_content' => 'utm_content'],
  36. ];
  37. protected $_aMetrics = ['visitors', 'sessions', 'pageviews', 'pages_per_session', 'bounce_rate', 'duration'];
  38. /**
  39. * Tabs of the other reports: report => [tab => what the tab shows]. The first is the default.
  40. * - map: the period's visitors or those online now
  41. * - events: event types
  42. * - retention: whether only members count (cohorts of member visitors)
  43. * - errors: statuses (`new` is open or resolved, first seen in the period)
  44. * - performance: pages grouped by route or by path
  45. * - revenue: splits as columns of the ledger
  46. */
  47. protected $_aReportTabs = [
  48. 'map' => ['period' => 'period', 'live' => 'live'],
  49. // set in the constructor: the hit types are constants of the config, which the module loads on first use
  50. 'events' => [],
  51. 'retention' => ['members' => true, 'visitors' => false],
  52. 'errors' => [
  53. 'open' => ['open'],
  54. 'new' => ['open', 'resolved'],
  55. 'resolved' => ['resolved'],
  56. 'ignored' => ['ignored'],
  57. 'all' => ['open', 'resolved', 'ignored'],
  58. ],
  59. 'performance' => ['route' => 'route', 'path' => 'path'],
  60. 'revenue' => ['sources' => 'module', 'providers' => 'provider', 'platforms' => 'platform', 'first_touch' => 'first_channel', 'last_touch' => 'last_channel', 'campaigns' => 'last_utm_campaign'],
  61. ];
  62. protected $_iPerPage = 25;
  63. protected $_oModule;
  64. protected $_oDb;
  65. protected $_oConfig;
  66. protected $_aCountries;
  67. protected $_aModuleTitles;
  68. public function __construct($oModule)
  69. {
  70. $this->_oModule = $oModule;
  71. $this->_oDb = $oModule->_oDb;
  72. $this->_oConfig = $oModule->_oConfig;
  73. $this->_aReportTabs['events'] = ['all' => [BX_ANALYTICS_HIT_EVENT, BX_ANALYTICS_HIT_UNA], 'custom' => [BX_ANALYTICS_HIT_EVENT], 'site' => [BX_ANALYTICS_HIT_UNA]];
  74. $this->_oDb->setReportSession();
  75. }
  76. /**
  77. * Whether a report shows live figures (visitors online, the last minutes), which are
  78. * never cached.
  79. */
  80. public function isLive($sReport, $aParams = [])
  81. {
  82. if(in_array($sReport, ['realtime', 'filters'], true))
  83. return true;
  84. return $sReport == 'map' && $this->_getTab($aParams, 'map', $this->_aReportTabs['map']) == 'live';
  85. }
  86. /**
  87. * The parameters a report reads, past the filter: its cache name is made of them. Blocks
  88. * send the whole page URL, which also holds other blocks' tabs and pages, the chart's
  89. * metric and opened rows; those would make a copy per combination of things the report
  90. * never shows. A report missing here keeps every parameter.
  91. */
  92. public function getCacheParams($sReport, $aParams)
  93. {
  94. $aKeys = [
  95. 'overview' => [],
  96. 'top' => ['group', 'tab'],
  97. 'table' => ['table', 'tab', 'page'],
  98. 'query' => ['q_dim', 'q_metric', 'q_dir', 'page', 'page_query'],
  99. 'map' => ['tab', 'tab_map'],
  100. 'bots' => ['tab', 'tab_bots', 'page_bot_list'],
  101. 'events' => ['event', 'tab', 'tab_events', 'page_events'],
  102. 'sessions' => ['session', 'page_sessions'],
  103. 'users' => ['user', 'session', 'page_users', 'page_sessions'],
  104. 'journeys' => ['jfrom', 'via'],
  105. 'retention' => ['tab', 'tab_retention'],
  106. 'goals' => ['edit_goal'],
  107. 'funnels' => ['funnel', 'edit_funnel'],
  108. 'revenue' => ['tab', 'tab_revenue'],
  109. 'subscriptions' => [],
  110. 'sales' => [],
  111. 'credits' => [],
  112. 'errors' => ['error', 'tab', 'tab_errors', 'page_errors'],
  113. 'performance' => ['vital', 'vital_device', 'tab', 'tab_performance', 'page_performance'],
  114. ];
  115. if(!isset($aKeys[$sReport]))
  116. return $aParams;
  117. // a top list's tab, and a table's tab and page, are named after the group or the table
  118. $aOwn = array_merge($aKeys[$sReport], ['limit', 'meta']);
  119. if($sReport == 'top' && ($sGroup = (string)$this->_param($aParams, 'group')) !== '')
  120. $aOwn[] = 'tab_' . $sGroup;
  121. if($sReport == 'table' && ($sTable = (string)$this->_param($aParams, 'table')) !== '')
  122. array_push($aOwn, 'tab_' . $sTable, 'page_' . $sTable);
  123. return array_intersect_key($aParams, array_flip($aOwn));
  124. }
  125. public function getGroups()
  126. {
  127. return $this->_aGroups;
  128. }
  129. public function isGroup($sGroup)
  130. {
  131. return isset($this->_aGroups[$sGroup]);
  132. }
  133. public function getMetrics()
  134. {
  135. return $this->_aMetrics;
  136. }
  137. /**
  138. * What get_report accepts, for the API playground: the filter parameters every report
  139. * reads, and each report's own. A parameter is [type, values, min, max, default] with
  140. * type enum (one of values), enum_by (one of values[the value of the parameter `by`]),
  141. * int (min to max), date (YYYY-MM-DD), string, or dims (an object of dimension => value,
  142. * the dimensions in values).
  143. *
  144. * @return array common => parameters, reports => [report => parameters]
  145. */
  146. public function getParamsSchema()
  147. {
  148. $fEnum = function($aValues, $sDefault = null) {
  149. return ['type' => 'enum', 'values' => array_values($aValues), 'default' => $sDefault !== null ? $sDefault : reset($aValues)];
  150. };
  151. $fInt = function($iMin, $iMax = null, $iDefault = null) {
  152. return array_filter(['type' => 'int', 'min' => $iMin, 'max' => $iMax, 'default' => $iDefault], function($m) { return $m !== null; });
  153. };
  154. $fString = function() {
  155. return ['type' => 'string'];
  156. };
  157. // a tab is one of its group's or table's: group or table => tabs
  158. $fTabs = function($sBy, $aLists) {
  159. return ['type' => 'enum_by', 'by' => $sBy, 'values' => array_map('array_keys', $aLists)];
  160. };
  161. $aDimensions = array_keys($this->_oDb->getDimensions());
  162. $aQueryMetrics = [];
  163. foreach($aDimensions as $sDimension)
  164. $aQueryMetrics = array_merge($aQueryMetrics, $this->_oDb->getBreakdownMetrics($sDimension));
  165. bx_import('Filter', $this->_oModule->_aModule);
  166. return [
  167. 'common' => [
  168. 'range' => $fEnum(BxAnalyticsFilter::$aRanges, BxAnalyticsFilter::RANGE_DEFAULT),
  169. 'from' => ['type' => 'date'],
  170. 'to' => ['type' => 'date'],
  171. 'tz' => ['type' => 'string', 'default' => 'UTC'],
  172. 'platform' => ['type' => 'enum', 'values' => array_keys($this->_oConfig->getPlatforms())],
  173. 'bucket' => ['type' => 'enum', 'values' => ['hour', 'day', 'week', 'month']],
  174. 'f' => ['type' => 'dims', 'values' => array_values(array_diff($aDimensions, ['platform']))],
  175. ],
  176. 'reports' => [
  177. 'filters' => [],
  178. 'overview' => [],
  179. 'top' => ['group' => $fEnum(array_keys($this->_aGroups)), 'tab' => $fTabs('group', $this->_aGroups), 'limit' => $fInt(1, 100, 10)],
  180. 'table' => ['table' => $fEnum(array_keys($this->_aTables)), 'tab' => $fTabs('table', $this->_aTables), 'page' => $fInt(1)],
  181. 'realtime' => ['limit' => $fInt(0, 50, 20)],
  182. 'map' => ['tab' => $fEnum(array_keys($this->_aReportTabs['map']))],
  183. 'bots' => ['tab' => $fEnum(array_keys($this->_getBotCategories())), 'page_bot_list' => $fInt(1)],
  184. 'events' => ['event' => $fString(), 'tab' => $fEnum(array_keys($this->_aReportTabs['events'])), 'page_events' => $fInt(1)],
  185. 'sessions' => ['session' => $fInt(1), 'page_sessions' => $fInt(1)],
  186. 'users' => ['user' => $fInt(1), 'session' => $fInt(1), 'page_users' => $fInt(1)],
  187. 'journeys' => ['jfrom' => $fString(), 'via' => $fString()],
  188. 'retention' => ['tab' => $fEnum(array_keys($this->_aReportTabs['retention']))],
  189. 'goals' => [],
  190. 'funnels' => ['funnel' => $fInt(1)],
  191. 'revenue' => ['tab' => $fEnum(array_keys($this->_aReportTabs['revenue']))],
  192. 'subscriptions' => [],
  193. 'sales' => [],
  194. 'credits' => [],
  195. 'errors' => ['error' => $fInt(1), 'tab' => $fEnum(array_keys($this->_aReportTabs['errors'])), 'page_errors' => $fInt(1)],
  196. 'performance' => ['vital' => $fEnum(BxAnalyticsVitals::METRICS), 'vital_device' => $fEnum(['all', 'mobile', 'desktop']), 'tab' => $fEnum(array_keys($this->_aReportTabs['performance'])), 'page_performance' => $fInt(1)],
  197. 'query' => ['q_dim' => $fEnum($aDimensions), 'q_metric' => $fEnum(array_unique($aQueryMetrics)), 'q_dir' => $fEnum(['desc', 'asc']), 'page_query' => $fInt(1)],
  198. ],
  199. ];
  200. }
  201. /**
  202. * Bot tabs: tab => bot categories (none for all).
  203. */
  204. protected function _getBotCategories()
  205. {
  206. bx_import('UserAgent', $this->_oModule->_aModule);
  207. return [
  208. 'all' => [],
  209. 'ai' => [BxAnalyticsUserAgent::BOT_AI_CRAWLER, BxAnalyticsUserAgent::BOT_AI_AGENT],
  210. 'search' => [BxAnalyticsUserAgent::BOT_SEARCH],
  211. 'social' => [BxAnalyticsUserAgent::BOT_SOCIAL],
  212. 'seo' => [BxAnalyticsUserAgent::BOT_SEO],
  213. 'monitor' => [BxAnalyticsUserAgent::BOT_MONITOR],
  214. 'script' => [BxAnalyticsUserAgent::BOT_SCRIPT, BxAnalyticsUserAgent::BOT_FEED, BxAnalyticsUserAgent::BOT_OTHER],
  215. ];
  216. }
  217. /**
  218. * The filter bar: active dimension filters with readable values, and visitors online.
  219. */
  220. public function getFilters(BxAnalyticsFilter $oFilter, $aParams = [])
  221. {
  222. $aFilter = $oFilter->get();
  223. $aChips = [];
  224. foreach($aFilter['dims'] as $sName => $sValue)
  225. $aChips[] = [
  226. 'name' => $sName,
  227. 'value' => $sValue,
  228. 'title' => _t('_bx_analytics_dim_' . $sName),
  229. 'label' => $this->getLabel($sName, $sValue),
  230. ];
  231. $aOnline = $this->_oDb->getOnline(time() - $this->_oConfig->getOnlineWindow());
  232. return [
  233. 'chips' => $aChips,
  234. 'online' => $aFilter['platform'] !== '' ? (isset($aOnline[$aFilter['platform']]) ? (int)$aOnline[$aFilter['platform']] : 0) : (int)array_sum($aOnline),
  235. 'window' => (int)($this->_oConfig->getOnlineWindow() / 60),
  236. ];
  237. }
  238. /**
  239. * KPIs with the change against the previous period, and the series for the chart.
  240. */
  241. public function getOverview(BxAnalyticsFilter $oFilter, $aParams = [])
  242. {
  243. $aFilter = $oFilter->get();
  244. $aCurrent = $this->_getKpis($this->_oDb->getTotals($aFilter));
  245. $aPrevious = $this->_getKpis($this->_oDb->getTotals($oFilter->getPrevious()));
  246. $aKpis = [];
  247. foreach($this->_aMetrics as $sMetric)
  248. $aKpis[$sMetric] = [
  249. 'value' => $aCurrent[$sMetric],
  250. 'previous' => $aPrevious[$sMetric],
  251. 'change' => $this->_change($aCurrent[$sMetric], $aPrevious[$sMetric]),
  252. ];
  253. $aRows = $this->_oDb->getSeries($aFilter, $aFilter['bucket'], $aFilter['offset'], $aFilter['starts']);
  254. // the bucket that holds "now" is still filling up, and the chart draws it dashed
  255. $aSeries = [];
  256. $aBuckets = $aFilter['buckets'];
  257. foreach($aBuckets as $i => $iBucket) {
  258. $iNext = isset($aBuckets[$i + 1]) ? $aBuckets[$i + 1] : $aFilter['to'];
  259. $aKpi = $this->_getKpis(isset($aRows[$iBucket]) ? $aRows[$iBucket] : []);
  260. $aSeries[] = array_merge([
  261. 't' => $iBucket,
  262. 'future' => $iBucket > $aFilter['now'],
  263. 'partial' => $iBucket <= $aFilter['now'] && $aFilter['now'] < $iNext,
  264. ], $aKpi);
  265. }
  266. return [
  267. 'kpis' => $aKpis,
  268. 'series' => $aSeries,
  269. 'bucket' => $aFilter['bucket'],
  270. 'from' => $aFilter['from'],
  271. 'to' => $aFilter['to'],
  272. 'tz' => $aFilter['tz'],
  273. ];
  274. }
  275. /**
  276. * One tab of a top list, with labels, shares and the filter each row applies when clicked.
  277. * Params: group, tab (or tab_<group>), limit.
  278. */
  279. public function getTop(BxAnalyticsFilter $oFilter, $aParams = [])
  280. {
  281. $sGroup = $this->_param($aParams, 'group');
  282. if(!isset($this->_aGroups[$sGroup]))
  283. return false;
  284. $sTab = $this->_getTab($aParams, $sGroup, $this->_aGroups[$sGroup]);
  285. $sDimension = $this->_aGroups[$sGroup][$sTab];
  286. $iLimit = isset($aParams['limit']) ? max(1, min(100, (int)$aParams['limit'])) : 10;
  287. $aRows = $this->_oDb->getTop($oFilter->get(), $sDimension, $iLimit);
  288. $iMax = 0;
  289. $iTotal = 0;
  290. foreach($aRows as $aRow) {
  291. $iMax = max($iMax, (int)$aRow['visitors']);
  292. $iTotal += (int)$aRow['visitors'];
  293. }
  294. $aItems = [];
  295. foreach($aRows as $aRow)
  296. $aItems[] = [
  297. 'value' => (string)$aRow['value'],
  298. 'label' => $this->getLabel($sDimension, (string)$aRow['value']),
  299. 'visitors' => (int)$aRow['visitors'],
  300. 'pageviews' => (int)$aRow['pageviews'],
  301. 'share' => $iTotal ? round(100 * $aRow['visitors'] / $iTotal, 1) : 0,
  302. 'bar' => $iMax ? round(100 * $aRow['visitors'] / $iMax, 1) : 0,
  303. ];
  304. return [
  305. 'group' => $sGroup,
  306. 'tab' => $sTab,
  307. 'tabs' => $this->_getTabs($this->_aGroups[$sGroup], $sTab),
  308. 'dimension' => $sDimension,
  309. 'column' => _t('_bx_analytics_tab_' . $sTab),
  310. 'items' => $aItems,
  311. ];
  312. }
  313. /**
  314. * A full table of a dimension with every metric, a page at a time.
  315. * Params: table (pages, sources), tab (or tab_<table>), page (or page_<table>).
  316. */
  317. public function getTable(BxAnalyticsFilter $oFilter, $aParams = [])
  318. {
  319. $sTable = $this->_param($aParams, 'table');
  320. if(!isset($this->_aTables[$sTable]))
  321. return false;
  322. $sTab = $this->_getTab($aParams, $sTable, $this->_aTables[$sTable]);
  323. $sDimension = $this->_aTables[$sTable][$sTab];
  324. $aFilter = $oFilter->get();
  325. $iTotal = $this->_oDb->getTopCount($aFilter, $sDimension);
  326. $iPages = max(1, (int)ceil($iTotal / $this->_iPerPage));
  327. // page_<table> first: in Studio `page` names the section
  328. $iPage = min($iPages, max(1, (int)$this->_param($aParams, 'page_' . $sTable, $this->_param($aParams, 'page', 1))));
  329. $iStart = ($iPage - 1) * $this->_iPerPage;
  330. if($this->_oDb->isEventDimension($sDimension)) {
  331. $aRows = $this->_oDb->getPageStats($aFilter, $sDimension, $this->_iPerPage, $iStart);
  332. $aColumns = [
  333. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_tab_' . $sTab)],
  334. ['key' => 'visitors', 'type' => 'number', 'title' => _t('_bx_analytics_metric_visitors')],
  335. ['key' => 'pageviews', 'type' => 'number', 'title' => _t('_bx_analytics_metric_pageviews')],
  336. ['key' => 'engaged', 'type' => 'engaged', 'title' => _t('_bx_analytics_metric_engaged')],
  337. ];
  338. if($sDimension == 'path')
  339. $aColumns = array_merge($aColumns, [
  340. ['key' => 'entries', 'type' => 'number', 'title' => _t('_bx_analytics_metric_entries')],
  341. ['key' => 'bounce_rate', 'type' => 'percent', 'title' => _t('_bx_analytics_metric_bounce_rate')],
  342. ['key' => 'exits', 'type' => 'number', 'title' => _t('_bx_analytics_metric_exits')],
  343. ]);
  344. }
  345. else {
  346. $aRows = [];
  347. foreach($this->_oDb->getTop($aFilter, $sDimension, $this->_iPerPage, $iStart) as $aRow)
  348. $aRows[] = array_merge($aRow, $this->_getKpis($aRow));
  349. $aColumns = [
  350. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_tab_' . $sTab)],
  351. ['key' => 'visitors', 'type' => 'number', 'title' => _t('_bx_analytics_metric_visitors')],
  352. ['key' => 'sessions', 'type' => 'number', 'title' => _t('_bx_analytics_metric_sessions')],
  353. ['key' => 'bounce_rate', 'type' => 'percent', 'title' => _t('_bx_analytics_metric_bounce_rate')],
  354. ['key' => 'pages_per_session', 'type' => 'decimal', 'title' => _t('_bx_analytics_metric_pages_per_session')],
  355. ['key' => 'duration', 'type' => 'duration', 'title' => _t('_bx_analytics_metric_duration')],
  356. ];
  357. }
  358. $iMax = 0;
  359. foreach($aRows as $aRow)
  360. $iMax = max($iMax, (int)$aRow['visitors']);
  361. $aResult = [];
  362. foreach($aRows as $aRow)
  363. $aResult[] = [
  364. 'label' => $this->getLabel($sDimension, (string)$aRow['value']),
  365. 'filter' => ['dim' => $sDimension, 'value' => (string)$aRow['value']],
  366. 'bar' => $iMax ? round(100 * $aRow['visitors'] / $iMax, 1) : 0,
  367. 'values' => $aRow,
  368. ];
  369. return [
  370. 'name' => $sTable,
  371. 'caption' => _t('_bx_analytics_group_' . $sTable),
  372. 'tab' => $sTab,
  373. 'tabs' => $this->_getTabs($this->_aTables[$sTable], $sTab),
  374. 'dimension' => $sDimension,
  375. 'columns' => $aColumns,
  376. 'rows' => $aResult,
  377. 'page' => $iPage,
  378. 'pages' => $iPages,
  379. 'total' => $iTotal,
  380. ];
  381. }
  382. /**
  383. * Explore any dimension: its values sorted by a metric, a page at a time, and the first
  384. * few of them over time. Same shape as a table, so it renders and pages like one.
  385. * Params: q_dim, q_metric, q_dir (see getQueryParams), page (or page_query).
  386. */
  387. public function getQuery(BxAnalyticsFilter $oFilter, $aParams = [])
  388. {
  389. $aQuery = $this->getQueryParams($aParams);
  390. $sDimension = $aQuery['dimension'];
  391. $sMetric = $aQuery['metric'];
  392. $aFilter = $oFilter->get();
  393. $iTotal = $this->_oDb->getTopCount($aFilter, $sDimension);
  394. $iPages = max(1, (int)ceil($iTotal / $this->_iPerPage));
  395. // page_query first: in Studio `page` names the section
  396. $iPage = min($iPages, max(1, (int)$this->_param($aParams, 'page_query', $this->_param($aParams, 'page', 1))));
  397. $iStart = ($iPage - 1) * $this->_iPerPage;
  398. $aRows = $this->_getQueryRows($aFilter, $aQuery, $this->_iPerPage, $iStart);
  399. // the chart follows the first values in this order, whatever page the table shows
  400. $iSeries = (int)$this->_oConfig->CNF['QUERY_SERIES_VALUES'];
  401. $aFirst = $iStart == 0 ? array_slice($aRows, 0, $iSeries) : $this->_getQueryRows($aFilter, $aQuery, $iSeries, 0, $iSeries, false);
  402. $fMax = 0;
  403. foreach($aRows as $aRow)
  404. $fMax = max($fMax, (float)$aRow[$sMetric]);
  405. $aResult = [];
  406. foreach($aRows as $aRow)
  407. $aResult[] = [
  408. 'label' => $this->getLabel($sDimension, $aRow['value']),
  409. 'filter' => ['dim' => $sDimension, 'value' => $aRow['value']],
  410. 'bar' => $fMax > 0 ? round(100 * $aRow[$sMetric] / $fMax, 1) : 0,
  411. 'values' => $aRow,
  412. ];
  413. $aDimensions = [];
  414. foreach($this->_oDb->getDimensions() as $sName => $sKind)
  415. $aDimensions[] = ['name' => $sName, 'title' => _t('_bx_analytics_dim_' . $sName), 'kind' => $sKind];
  416. $aMetrics = [];
  417. foreach($aQuery['metrics'] as $sName)
  418. $aMetrics[] = ['name' => $sName, 'title' => _t('_bx_analytics_metric_' . $sName)];
  419. return [
  420. 'name' => 'query',
  421. 'caption' => _t('_bx_analytics_dim_' . $sDimension),
  422. 'dimension' => $sDimension,
  423. 'kind' => $aQuery['kind'],
  424. 'metric' => $sMetric,
  425. 'dir' => $aQuery['dir'],
  426. 'dimensions' => $aDimensions,
  427. 'metrics' => $aMetrics,
  428. 'tabs' => [],
  429. 'columns' => $this->_getQueryColumns($aQuery),
  430. 'rows' => $aResult,
  431. 'page' => $iPage,
  432. 'pages' => $iPages,
  433. 'total' => $iTotal,
  434. 'export_max' => (int)$this->_oConfig->CNF['MAX_EXPORT_ROWS'],
  435. 'chart' => $this->_getQuerySeries($aFilter, $aQuery, $aFirst),
  436. ];
  437. }
  438. /**
  439. * What a query asks for, from q_dim (a dimension, see BxAnalyticsDb::getDimensions),
  440. * q_metric (one the dimension can be sorted by) and q_dir (asc or desc). Anything unknown
  441. * falls back to the first valid choice: pages, by visitors, highest first.
  442. *
  443. * @return array dimension, kind (event or session), metric, metrics (the dimension's
  444. * sortable metrics), dir
  445. */
  446. public function getQueryParams($aParams)
  447. {
  448. $aDimensions = $this->_oDb->getDimensions();
  449. $sDimension = (string)$this->_param($aParams, 'q_dim');
  450. if(!isset($aDimensions[$sDimension]))
  451. $sDimension = key($aDimensions);
  452. $aMetrics = $this->_oDb->getBreakdownMetrics($sDimension);
  453. $sMetric = (string)$this->_param($aParams, 'q_metric');
  454. if(!in_array($sMetric, $aMetrics, true))
  455. $sMetric = $aMetrics[0];
  456. return [
  457. 'dimension' => $sDimension,
  458. 'kind' => $aDimensions[$sDimension],
  459. 'metric' => $sMetric,
  460. 'metrics' => $aMetrics,
  461. 'dir' => $this->_param($aParams, 'q_dir') === 'asc' ? 'asc' : 'desc',
  462. ];
  463. }
  464. /**
  465. * Every value of a report's dimension with its metrics, for a CSV download: the query,
  466. * or a table or top list tab (by visitors). Rows are the label, the stored value and the
  467. * metrics as plain numbers.
  468. *
  469. * @param string $sReport query, table or top
  470. * @param int $iMax rows at most
  471. * @return array dimension, header, rows, total (values in the period), truncated; or false
  472. */
  473. public function getExport($sReport, BxAnalyticsFilter $oFilter, $aParams, $iMax)
  474. {
  475. switch($sReport) {
  476. case 'query':
  477. $aQuery = $this->getQueryParams($aParams);
  478. break;
  479. case 'table':
  480. case 'top':
  481. $aLists = $sReport == 'table' ? $this->_aTables : $this->_aGroups;
  482. $sList = (string)$this->_param($aParams, $sReport == 'table' ? 'table' : 'group');
  483. if(!isset($aLists[$sList]))
  484. return false;
  485. $aQuery = $this->getQueryParams(['q_dim' => $aLists[$sList][$this->_getTab($aParams, $sList, $aLists[$sList])]]);
  486. break;
  487. default:
  488. return false;
  489. }
  490. $aFilter = $oFilter->get();
  491. $aColumns = $this->_getQueryColumns($aQuery);
  492. $aHeader = [$aColumns[0]['title'], _t('_bx_analytics_txt_raw_value')];
  493. foreach(array_slice($aColumns, 1) as $aColumn)
  494. $aHeader[] = $aColumn['title'];
  495. // one query: pages of a re-run aggregate would cost a full scan each and could
  496. // repeat or skip values that change places while hits come in
  497. $iMax = max(1, (int)$iMax);
  498. $aRows = [];
  499. foreach($this->_getQueryRows($aFilter, $aQuery, $iMax, 0, $iMax) as $aRow) {
  500. $aLine = [$this->getLabel($aQuery['dimension'], $aRow['value']), $aRow['value']];
  501. foreach(array_slice($aColumns, 1) as $aColumn)
  502. $aLine[] = isset($aRow[$aColumn['key']]) ? $aRow[$aColumn['key']] : 0;
  503. $aRows[] = $aLine;
  504. }
  505. $iTotal = $this->_oDb->getTopCount($aFilter, $aQuery['dimension']);
  506. return [
  507. 'dimension' => $aQuery['dimension'],
  508. 'header' => $aHeader,
  509. 'rows' => $aRows,
  510. 'total' => $iTotal,
  511. 'truncated' => $iTotal > count($aRows),
  512. ];
  513. }
  514. /**
  515. * Query rows: the stored value and the metrics as numbers (KPIs for a session dimension,
  516. * pageview figures for a page or title).
  517. *
  518. * @param bool $bPathSessions pages get entries, bounce rate and exits
  519. */
  520. protected function _getQueryRows($aFilter, $aQuery, $iLimit, $iStart, $iMax = 1000, $bPathSessions = true)
  521. {
  522. $aRows = $this->_oDb->getBreakdown($aFilter, $aQuery['dimension'], $aQuery['metric'], $aQuery['dir'] == 'asc' ? 'ASC' : 'DESC', $iLimit, $iStart, $iMax, $bPathSessions);
  523. $aResult = [];
  524. foreach($aRows as $aRow)
  525. $aResult[] = ['value' => (string)$aRow['value']] + ($aQuery['kind'] == 'session' ? $this->_getKpis($aRow) : $this->_getPageKpis($aRow));
  526. return $aResult;
  527. }
  528. /**
  529. * Metrics of a page or title row from BxAnalyticsDb::getPageStats.
  530. */
  531. protected function _getPageKpis($aRow)
  532. {
  533. $aResult = [
  534. 'visitors' => isset($aRow['visitors']) ? (int)$aRow['visitors'] : 0,
  535. 'pageviews' => isset($aRow['pageviews']) ? (int)$aRow['pageviews'] : 0,
  536. 'engaged' => isset($aRow['engaged']) ? round((float)$aRow['engaged'], 1) : 0,
  537. ];
  538. if(isset($aRow['entries']))
  539. $aResult += [
  540. 'entries' => (int)$aRow['entries'],
  541. 'bounce_rate' => (float)$aRow['bounce_rate'],
  542. 'exits' => (int)$aRow['exits'],
  543. ];
  544. return $aResult;
  545. }
  546. /**
  547. * Table columns of a query, the sorted one marked.
  548. */
  549. protected function _getQueryColumns($aQuery)
  550. {
  551. if($aQuery['kind'] == 'session')
  552. $aTypes = ['visitors' => 'number', 'sessions' => 'number', 'pageviews' => 'number', 'bounce_rate' => 'percent', 'pages_per_session' => 'decimal', 'duration' => 'duration'];
  553. else {
  554. $aTypes = ['visitors' => 'number', 'pageviews' => 'number', 'engaged' => 'engaged'];
  555. if($aQuery['dimension'] == 'path')
  556. $aTypes += ['entries' => 'number', 'bounce_rate' => 'percent', 'exits' => 'number'];
  557. }
  558. $aColumns = [['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_dim_' . $aQuery['dimension'])]];
  559. foreach($aTypes as $sKey => $sType) {
  560. $aColumn = ['key' => $sKey, 'type' => $sType, 'title' => _t('_bx_analytics_metric_' . $sKey)];
  561. if($sKey == $aQuery['metric'])
  562. $aColumn['sort'] = $aQuery['dir'] == 'asc' ? 'ascending' : 'descending';
  563. $aColumns[] = $aColumn;
  564. }
  565. return $aColumns;
  566. }
  567. /**
  568. * The chosen metric of some values per bucket. A bucket without data is 0 for a count
  569. * and a gap for an average or a rate; buckets after now are gaps.
  570. */
  571. protected function _getQuerySeries($aFilter, $aQuery, $aValues)
  572. {
  573. $sMetric = $aQuery['metric'];
  574. $bRatio = in_array($sMetric, ['pages_per_session', 'bounce_rate', 'duration', 'engaged'], true);
  575. $aData = [];
  576. foreach($this->_oDb->getBreakdownSeries($aFilter, $aQuery['dimension'], array_column($aValues, 'value'), $aFilter['bucket'], $aFilter['offset'], $aFilter['starts']) as $aRow) {
  577. $aKpis = $aQuery['kind'] == 'session' ? $this->_getKpis($aRow) : $this->_getPageKpis($aRow);
  578. $aData[(string)$aRow['value']][(int)$aRow['bucket']] = $aKpis[$sMetric];
  579. }
  580. $aBuckets = $aFilter['buckets'];
  581. $aSeries = [];
  582. foreach($aValues as $aValue) {
  583. $aPoints = [];
  584. foreach($aBuckets as $i => $iBucket) {
  585. $iNext = isset($aBuckets[$i + 1]) ? $aBuckets[$i + 1] : $aFilter['to'];
  586. if($iBucket > $aFilter['now'])
  587. $mixedValue = null;
  588. elseif(isset($aData[$aValue['value']][$iBucket]))
  589. $mixedValue = $aData[$aValue['value']][$iBucket];
  590. else
  591. $mixedValue = $bRatio ? null : 0;
  592. $aPoints[] = [
  593. 't' => $iBucket,
  594. 'v' => $mixedValue,
  595. 'partial' => $iBucket <= $aFilter['now'] && $aFilter['now'] < $iNext,
  596. ];
  597. }
  598. $aSeries[] = [
  599. 'value' => $aValue['value'],
  600. 'label' => $this->getLabel($aQuery['dimension'], $aValue['value']),
  601. 'points' => $aPoints,
  602. ];
  603. }
  604. return [
  605. 'kind' => 'series',
  606. 'metric' => $sMetric,
  607. 'title' => _t('_bx_analytics_metric_' . $sMetric),
  608. 'bucket' => $aFilter['bucket'],
  609. 'tz' => $aFilter['tz'],
  610. 'series' => $aSeries,
  611. ];
  612. }
  613. /**
  614. * Visitors online now, per platform, pageviews per minute over the last 30 minutes, and
  615. * the latest pageviews.
  616. */
  617. public function getRealtime(BxAnalyticsFilter $oFilter, $aParams = [])
  618. {
  619. $iNow = time();
  620. $sPlatform = $oFilter->get()['platform'];
  621. $aOnline = $this->_oDb->getOnline($iNow - $this->_oConfig->getOnlineWindow());
  622. $aPlatforms = [];
  623. $iTotal = 0;
  624. foreach($this->_oConfig->getPlatforms() as $sCode => $sTitle) {
  625. $iCount = isset($aOnline[$sCode]) ? (int)$aOnline[$sCode] : 0;
  626. $iTotal += $iCount;
  627. if($iCount)
  628. $aPlatforms[] = ['value' => $sCode, 'label' => _t($sTitle), 'visitors' => $iCount];
  629. }
  630. foreach($aPlatforms as $i => $a)
  631. $aPlatforms[$i]['share'] = $iTotal ? round(100 * $a['visitors'] / $iTotal, 1) : 0;
  632. $iMinutes = 30;
  633. $iStart = (int)(floor($iNow / 60) * 60) - ($iMinutes - 1) * 60;
  634. $aPerMinute = $this->_oDb->getPageviewsPerMinute($iStart, $sPlatform);
  635. $aMinutes = [];
  636. for($i = 0; $i < $iMinutes; $i++) {
  637. $iMinute = $iStart + $i * 60;
  638. $aMinutes[] = ['t' => $iMinute, 'pageviews' => isset($aPerMinute[$iMinute]) ? (int)$aPerMinute[$iMinute] : 0];
  639. }
  640. $iLimit = isset($aParams['limit']) ? max(0, min(50, (int)$aParams['limit'])) : 20;
  641. $aRecent = [];
  642. $aRows = $iLimit > 0 ? $this->_oDb->getRecentPageviews($iNow - $iMinutes * 60, $iLimit, $sPlatform) : [];
  643. foreach($aRows as $aRow)
  644. $aRecent[] = [
  645. 'ts' => (int)$aRow['ts'],
  646. 'platform' => $aRow['platform'],
  647. 'platform_label' => $this->getLabel('platform', $aRow['platform']),
  648. 'path' => $aRow['path'],
  649. 'title' => $aRow['title'],
  650. 'country' => $aRow['country'],
  651. 'country_label' => $this->getLabel('country', $aRow['country']),
  652. 'city' => $aRow['city'],
  653. 'device' => $this->getLabel('device', $aRow['device']),
  654. 'browser' => $aRow['browser'],
  655. 'os' => $aRow['os'],
  656. 'member' => (int)$aRow['profile_id'] > 0,
  657. ];
  658. return [
  659. 'online' => $sPlatform !== '' ? (isset($aOnline[$sPlatform]) ? (int)$aOnline[$sPlatform] : 0) : $iTotal,
  660. 'platforms' => $aPlatforms,
  661. 'minutes' => $aMinutes,
  662. 'recent' => $aRecent,
  663. 'window' => (int)($this->_oConfig->getOnlineWindow() / 60),
  664. ];
  665. }
  666. /**
  667. * Visitors on a map: per country at the country's centre, and per place where the edge
  668. * gave coordinates. Tabs: the period, or live (the last 30 minutes).
  669. */
  670. public function getMap(BxAnalyticsFilter $oFilter, $aParams = [])
  671. {
  672. bx_import('Geo', $this->_oModule->_aModule);
  673. $aTabs = $this->_aReportTabs['map'];
  674. $sTab = $this->_getTab($aParams, 'map', $aTabs);
  675. $aFilter = $oFilter->get();
  676. if($sTab == 'live') {
  677. $aFilter['from'] = time() - 30 * 60;
  678. $aFilter['to'] = time() + 60;
  679. }
  680. $aCountries = [];
  681. $iMax = 0;
  682. $iUnknown = 0;
  683. foreach($this->_oDb->getTop($aFilter, 'country', 250) as $aRow) {
  684. $aCenter = BxAnalyticsGeo::getCenter($aRow['value']);
  685. if(!$aCenter) {
  686. $iUnknown += (int)$aRow['visitors'];
  687. continue;
  688. }
  689. $iMax = max($iMax, (int)$aRow['visitors']);
  690. $aCountries[] = [
  691. 'country' => $aRow['value'],
  692. 'label' => $this->getLabel('country', $aRow['value']),
  693. 'visitors' => (int)$aRow['visitors'],
  694. 'lat' => $aCenter[0],
  695. 'lng' => $aCenter[1],
  696. ];
  697. }
  698. $aPoints = [];
  699. foreach($this->_oDb->getMapPoints($aFilter) as $aRow)
  700. $aPoints[] = [
  701. 'label' => trim($aRow['city'] . ($aRow['country'] !== '' ? ', ' . $this->getLabel('country', $aRow['country']) : ''), ', '),
  702. 'visitors' => (int)$aRow['visitors'],
  703. 'lat' => (float)$aRow['lat'],
  704. 'lng' => (float)$aRow['lng'],
  705. ];
  706. $iTotal = array_sum(array_column($aCountries, 'visitors'));
  707. foreach($aCountries as $i => $a) {
  708. $aCountries[$i]['share'] = $iTotal ? round(100 * $a['visitors'] / $iTotal, 1) : 0;
  709. $aCountries[$i]['bar'] = $iMax ? round(100 * $a['visitors'] / $iMax, 1) : 0;
  710. }
  711. return [
  712. 'tab' => $sTab,
  713. 'tabs' => array_map(function($sName) use ($sTab) {
  714. return ['name' => $sName, 'title' => _t('_bx_analytics_tab_map_' . $sName), 'active' => $sName == $sTab];
  715. }, array_keys($aTabs)),
  716. 'countries' => $aCountries,
  717. 'points' => $aPoints,
  718. 'unknown' => $iUnknown,
  719. 'provider' => getParam('sys_location_leaflet_provider'),
  720. ];
  721. }
  722. /**
  723. * Bots and agents: totals, hits per day by category, the bots and the pages they ask for.
  724. * Tabs narrow it to a category; AI covers crawlers and assistants fetching for a person.
  725. */
  726. public function getBots(BxAnalyticsFilter $oFilter, $aParams = [])
  727. {
  728. bx_import('UserAgent', $this->_oModule->_aModule);
  729. $aCategories = $this->_getBotCategories();
  730. $sTab = $this->_getTab($aParams, 'bots', $aCategories);
  731. $aIn = $aCategories[$sTab];
  732. $aFilter = $oFilter->get();
  733. $aTotals = $this->_oDb->getBotTotals($aFilter, $aIn);
  734. $aAi = $this->_oDb->getBotTotals($aFilter, $aCategories['ai']);
  735. $aHumans = $this->_oDb->getTotals(array_merge($aFilter, ['dims' => []]));
  736. $iHits = (int)$aTotals['hits'];
  737. $iAll = $sTab == 'all' ? $iHits : (int)$this->_oDb->getBotTotals($aFilter)['hits'];
  738. $aTiles = [
  739. ['title' => _t('_bx_analytics_metric_bot_hits'), 'value' => $iHits, 'type' => 'number'],
  740. ['title' => _t('_bx_analytics_metric_bots'), 'value' => (int)$aTotals['bots'], 'type' => 'number'],
  741. ['title' => _t('_bx_analytics_metric_ai_hits'), 'value' => (int)$aAi['hits'], 'type' => 'number'],
  742. ['title' => _t('_bx_analytics_metric_bot_share'), 'value' => $iAll + (int)$aHumans['pageviews'] ? round(100 * $iAll / ($iAll + (int)$aHumans['pageviews']), 1) : 0, 'type' => 'percent'],
  743. ];
  744. // hits per day and category: rows are per UTC day, so the chart has every UTC day the
  745. // query reads (the same days as the table) and labels them in UTC. Stepping from the
  746. // local start instead dropped the last UTC day east of UTC, and with it today's hits.
  747. $aBotDays = $this->_oDb->getBotDays($aFilter);
  748. $iDayTo = strtotime($aBotDays['to'] . ' 00:00:00 UTC');
  749. $aDays = [];
  750. for($i = strtotime($aBotDays['from'] . ' 00:00:00 UTC'); $i <= $iDayTo && count($aDays) < 800; $i += 86400)
  751. $aDays[] = gmdate('Y-m-d', $i);
  752. $aSeries = [];
  753. foreach($this->_oDb->getBotSeries($aFilter) as $aRow)
  754. if(empty($aIn) || in_array($aRow['category'], $aIn, true))
  755. $aSeries[$aRow['category']][$aRow['day']] = (int)$aRow['hits'];
  756. $aDatasets = [];
  757. foreach($aSeries as $sCategory => $aValues)
  758. $aDatasets[] = [
  759. 'key' => $sCategory,
  760. 'label' => _t('_bx_analytics_bot_' . $sCategory),
  761. 'values' => array_map(function($sDay) use ($aValues) { return isset($aValues[$sDay]) ? $aValues[$sDay] : 0; }, $aDays),
  762. ];
  763. $iTotal = $this->_oDb->getBotListCount($aFilter, $aIn);
  764. $iPages = max(1, (int)ceil($iTotal / $this->_iPerPage));
  765. $iPage = min($iPages, max(1, (int)$this->_param($aParams, 'page_bot_list', 1)));
  766. $aBots = $this->_oDb->getBotList($aFilter, $aIn, $this->_iPerPage, ($iPage - 1) * $this->_iPerPage);
  767. $iMax = 1;
  768. foreach($aBots as $aBot)
  769. $iMax = max($iMax, (int)$aBot['hits']);
  770. $aRows = [];
  771. foreach($aBots as $aBot)
  772. $aRows[] = [
  773. 'label' => $aBot['bot'],
  774. 'sub' => _t('_bx_analytics_bot_' . $aBot['category']),
  775. 'bar' => round(100 * $aBot['hits'] / $iMax, 1),
  776. 'values' => [
  777. 'hits' => (int)$aBot['hits'],
  778. 'last_seen' => (int)$aBot['last_seen'],
  779. 'platforms' => implode(', ', array_map(function($s) { return $this->getLabel('platform', $s); }, explode(',', (string)$aBot['platforms']))),
  780. ],
  781. ];
  782. $aPagesRows = [];
  783. $aPages = $this->_oDb->getBotPages($aFilter, $aIn, 10);
  784. $iMaxPage = 1;
  785. foreach($aPages as $aPage)
  786. $iMaxPage = max($iMaxPage, (int)$aPage['hits']);
  787. foreach($aPages as $aPage)
  788. $aPagesRows[] = [
  789. 'label' => $aPage['path'],
  790. 'bar' => round(100 * $aPage['hits'] / $iMaxPage, 1),
  791. 'values' => ['hits' => (int)$aPage['hits']],
  792. ];
  793. return [
  794. 'tab' => $sTab,
  795. 'tabs' => $this->_getTabs(array_flip(array_keys($aCategories)), $sTab, '_bx_analytics_tab_bots_'),
  796. 'tiles' => $aTiles,
  797. 'chart' => [
  798. 'kind' => 'stacked',
  799. 'labels' => array_map(function($sDay) { return strtotime($sDay . ' 00:00:00 UTC'); }, $aDays),
  800. 'bucket' => 'day',
  801. 'tz' => 'UTC',
  802. 'datasets' => $aDatasets,
  803. ],
  804. 'bots' => [
  805. 'name' => 'bot_list',
  806. 'caption' => _t('_bx_analytics_lmi_cpt_bots'),
  807. 'columns' => [
  808. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_txt_bot')],
  809. ['key' => 'hits', 'type' => 'number', 'title' => _t('_bx_analytics_metric_bot_hits')],
  810. ['key' => 'platforms', 'type' => 'text', 'title' => _t('_bx_analytics_dim_platform')],
  811. ['key' => 'last_seen', 'type' => 'datetime', 'title' => _t('_bx_analytics_txt_last_seen')],
  812. ],
  813. 'rows' => $aRows,
  814. 'page' => $iPage,
  815. 'pages' => $iPages,
  816. 'empty' => _t('_bx_analytics_txt_no_bots'),
  817. ],
  818. 'pages' => [
  819. 'name' => 'bot_pages',
  820. 'caption' => _t('_bx_analytics_txt_bot_pages'),
  821. 'columns' => [
  822. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_tab_pages')],
  823. ['key' => 'hits', 'type' => 'number', 'title' => _t('_bx_analytics_metric_bot_hits')],
  824. ],
  825. 'rows' => $aPagesRows,
  826. 'empty' => _t('_bx_analytics_txt_no_bots'),
  827. ],
  828. ];
  829. }
  830. /**
  831. * Custom events and site actions with their counts and visitors. With `event`
  832. * (type|name|module) one event over time, by platform and by its properties.
  833. * Tabs: all, custom (from trackers), site (UNA actions).
  834. */
  835. public function getEvents(BxAnalyticsFilter $oFilter, $aParams = [])
  836. {
  837. $aEvent = $this->_parseEvent($this->_param($aParams, 'event'));
  838. if($aEvent)
  839. return $this->_getEventDetail($oFilter, $aEvent);
  840. $aTabs = $this->_aReportTabs['events'];
  841. $sTab = $this->_getTab($aParams, 'events', $aTabs);
  842. $aFilter = $oFilter->get();
  843. $iTotal = $this->_oDb->getEventListCount($aFilter, $aTabs[$sTab]);
  844. $iPages = max(1, (int)ceil($iTotal / $this->_iPerPage));
  845. $iPage = min($iPages, max(1, (int)$this->_param($aParams, 'page_events', 1)));
  846. $aEvents = $this->_oDb->getEventList($aFilter, $aTabs[$sTab], $this->_iPerPage, ($iPage - 1) * $this->_iPerPage);
  847. $iMax = 1;
  848. foreach($aEvents as $aRow)
  849. $iMax = max($iMax, (int)$aRow['count']);
  850. $aRows = [];
  851. foreach($aEvents as $aRow)
  852. $aRows[] = [
  853. 'label' => $this->getEventLabel($aRow['type'], $aRow['name'], $aRow['module']),
  854. 'sub' => _t('_bx_analytics_event_type_' . $aRow['type']),
  855. 'open' => ['param' => 'event', 'value' => $aRow['type'] . '|' . $aRow['name'] . '|' . $aRow['module']],
  856. 'bar' => round(100 * $aRow['count'] / $iMax, 1),
  857. 'values' => [
  858. 'count' => (int)$aRow['count'],
  859. 'visitors' => (int)$aRow['visitors'],
  860. 'per_visitor' => $aRow['visitors'] ? round($aRow['count'] / $aRow['visitors'], 1) : 0,
  861. 'last' => (int)$aRow['last'],
  862. ],
  863. ];
  864. return [
  865. 'view' => 'list',
  866. 'name' => 'events',
  867. 'caption' => _t('_bx_analytics_lmi_cpt_events'),
  868. 'tab' => $sTab,
  869. 'tabs' => $this->_getTabs($aTabs, $sTab, '_bx_analytics_tab_events_'),
  870. 'columns' => [
  871. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_txt_event')],
  872. ['key' => 'count', 'type' => 'number', 'title' => _t('_bx_analytics_metric_count')],
  873. ['key' => 'visitors', 'type' => 'number', 'title' => _t('_bx_analytics_metric_visitors')],
  874. ['key' => 'per_visitor', 'type' => 'decimal', 'title' => _t('_bx_analytics_metric_per_visitor')],
  875. ['key' => 'last', 'type' => 'datetime', 'title' => _t('_bx_analytics_txt_last_seen')],
  876. ],
  877. 'rows' => $aRows,
  878. 'page' => $iPage,
  879. 'pages' => $iPages,
  880. 'empty' => _t('_bx_analytics_txt_no_events'),
  881. ];
  882. }
  883. protected function _getEventDetail(BxAnalyticsFilter $oFilter, $aEvent)
  884. {
  885. $aFilter = $oFilter->get();
  886. $aRows = $this->_oDb->getEventSeries($aFilter, $aEvent, $aFilter['bucket'], $aFilter['offset'], $aFilter['starts']);
  887. $aCounts = [];
  888. $aVisitors = [];
  889. foreach($aFilter['buckets'] as $iBucket) {
  890. $aCounts[] = isset($aRows[$iBucket]) ? (int)$aRows[$iBucket]['count'] : 0;
  891. $aVisitors[] = isset($aRows[$iBucket]) ? (int)$aRows[$iBucket]['visitors'] : 0;
  892. }
  893. $aPlatforms = [];
  894. $iTotal = 0;
  895. $iTotalVisitors = 0;
  896. foreach($this->_oDb->getEventPlatforms($aFilter, $aEvent) as $aRow) {
  897. $iTotal += (int)$aRow['count'];
  898. $iTotalVisitors += (int)$aRow['visitors'];
  899. $aPlatforms[] = ['label' => $this->getLabel('platform', $aRow['platform']), 'count' => (int)$aRow['count']];
  900. }
  901. foreach($aPlatforms as $i => $a)
  902. $aPlatforms[$i]['share'] = $iTotal ? round(100 * $a['count'] / $iTotal, 1) : 0;
  903. // the most common values of each property among the latest occurrences; a sale's
  904. // properties only for those who may see revenue (events recorded earlier carry amounts)
  905. $aProps = [];
  906. $bMoney = $aEvent['type'] == BX_ANALYTICS_HIT_UNA && in_array($aEvent['name'], ['purchase', 'renewal', 'trial_start'], true);
  907. foreach($bMoney && $this->_oModule->checkAllowedRevenue() !== CHECK_ACTION_RESULT_ALLOWED ? [] : $this->_oDb->getEventProps($aFilter, $aEvent) as $sJson) {
  908. $a = json_decode($sJson, true);
  909. if(!is_array($a))
  910. continue;
  911. foreach($a as $sKey => $mixedValue)
  912. if(is_scalar($mixedValue)) {
  913. $sValue = is_bool($mixedValue) ? ($mixedValue ? 'true' : 'false') : (string)$mixedValue;
  914. $aProps[$sKey][$sValue] = (isset($aProps[$sKey][$sValue]) ? $aProps[$sKey][$sValue] : 0) + 1;
  915. }
  916. }
  917. $aPropTables = [];
  918. foreach(array_slice($aProps, 0, 6, true) as $sKey => $aValues) {
  919. arsort($aValues);
  920. $iMax = max($aValues);
  921. $aPropRows = [];
  922. foreach(array_slice($aValues, 0, 10, true) as $sValue => $iCount)
  923. $aPropRows[] = ['label' => (string)$sValue, 'bar' => round(100 * $iCount / $iMax, 1), 'values' => ['count' => $iCount]];
  924. $aPropTables[] = [
  925. 'name' => 'prop_' . preg_replace('/[^a-z0-9_]/i', '_', $sKey),
  926. 'caption' => (string)$sKey,
  927. 'columns' => [
  928. ['key' => 'label', 'type' => 'label', 'title' => (string)$sKey],
  929. ['key' => 'count', 'type' => 'number', 'title' => _t('_bx_analytics_metric_count')],
  930. ],
  931. 'rows' => $aPropRows,
  932. ];
  933. }
  934. return [
  935. 'view' => 'detail',
  936. 'event' => $aEvent,
  937. 'title' => $this->getEventLabel($aEvent['type'], $aEvent['name'], $aEvent['module']),
  938. 'tiles' => [
  939. ['title' => _t('_bx_analytics_metric_count'), 'value' => $iTotal, 'type' => 'number'],
  940. ['title' => _t('_bx_analytics_metric_visitors'), 'value' => $iTotalVisitors, 'type' => 'number'],
  941. ['title' => _t('_bx_analytics_metric_per_visitor'), 'value' => $iTotalVisitors ? round($iTotal / $iTotalVisitors, 1) : 0, 'type' => 'decimal'],
  942. ],
  943. 'chart' => [
  944. 'kind' => 'lines',
  945. 'labels' => $aFilter['buckets'],
  946. 'bucket' => $aFilter['bucket'],
  947. 'tz' => $aFilter['tz'],
  948. 'datasets' => [
  949. ['key' => 'count', 'label' => _t('_bx_analytics_metric_count'), 'values' => $aCounts],
  950. ['key' => 'visitors', 'label' => _t('_bx_analytics_metric_visitors'), 'values' => $aVisitors],
  951. ],
  952. ],
  953. 'platforms' => $aPlatforms,
  954. 'props' => $aPropTables,
  955. ];
  956. }
  957. /**
  958. * Visits, latest first, with who, where from and on what. With `session` one visit's
  959. * timeline. Shows members by name, so it needs the people permission.
  960. */
  961. public function getSessions(BxAnalyticsFilter $oFilter, $aParams = [], $iProfileId = 0)
  962. {
  963. $iSession = (int)$this->_param($aParams, 'session', 0);
  964. if($iSession)
  965. return $this->_getSessionDetail($iSession);
  966. $aFilter = $oFilter->get();
  967. $iTotal = $this->_oDb->getSessionListCount($aFilter, $iProfileId);
  968. $iPages = max(1, (int)ceil($iTotal / $this->_iPerPage));
  969. $iPage = min($iPages, max(1, (int)$this->_param($aParams, 'page_sessions', 1)));
  970. $aRows = [];
  971. foreach($this->_oDb->getSessionList($aFilter, $this->_iPerPage, ($iPage - 1) * $this->_iPerPage, $iProfileId) as $aSession) {
  972. $aMember = $this->getMember((int)$aSession['profile_id']);
  973. $aRows[] = [
  974. 'label' => $aMember ? $aMember['name'] : _t('_bx_analytics_txt_guest'),
  975. 'open' => ['param' => 'session', 'value' => (int)$aSession['id']],
  976. 'values' => [
  977. 'started' => (int)$aSession['started'],
  978. 'platform' => $this->getLabel('platform', $aSession['platform']),
  979. 'location' => $this->_getLocation($aSession),
  980. 'device' => $this->_getDevice($aSession),
  981. 'source' => $aSession['referrer_host'] !== '' ? $aSession['referrer_host'] : $this->getLabel('channel', $aSession['channel'] !== '' ? $aSession['channel'] : 'direct'),
  982. 'pageviews' => (int)$aSession['pageviews'],
  983. 'duration' => (int)$aSession['duration'],
  984. ],
  985. ];
  986. }
  987. return [
  988. 'view' => 'list',
  989. 'name' => 'sessions',
  990. 'caption' => _t('_bx_analytics_lmi_cpt_sessions'),
  991. 'columns' => [
  992. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_txt_visitor')],
  993. ['key' => 'started', 'type' => 'datetime', 'title' => _t('_bx_analytics_txt_started')],
  994. ['key' => 'platform', 'type' => 'text', 'title' => _t('_bx_analytics_dim_platform')],
  995. ['key' => 'location', 'type' => 'text', 'title' => _t('_bx_analytics_txt_location')],
  996. ['key' => 'device', 'type' => 'text', 'title' => _t('_bx_analytics_dim_device')],
  997. ['key' => 'source', 'type' => 'text', 'title' => _t('_bx_analytics_txt_source')],
  998. ['key' => 'pageviews', 'type' => 'number', 'title' => _t('_bx_analytics_metric_pageviews')],
  999. ['key' => 'duration', 'type' => 'duration', 'title' => _t('_bx_analytics_txt_duration')],
  1000. ],
  1001. 'rows' => $aRows,
  1002. 'page' => $iPage,
  1003. 'pages' => $iPages,
  1004. 'empty' => _t('_bx_analytics_txt_no_data'),
  1005. ];
  1006. }
  1007. protected function _getSessionDetail($iSession)
  1008. {
  1009. $aSession = $this->_oDb->getSession($iSession);
  1010. if(empty($aSession))
  1011. return false;
  1012. $iStart = (int)$aSession['started'];
  1013. $aTimeline = [];
  1014. foreach($this->_oDb->getSessionEvents($iSession) as $aEvent)
  1015. $aTimeline[] = [
  1016. 'offset' => max(0, (int)$aEvent['ts'] - $iStart),
  1017. 'type' => $aEvent['type'],
  1018. 'label' => $aEvent['type'] == BX_ANALYTICS_HIT_PAGEVIEW ? ($aEvent['title'] !== '' ? $aEvent['title'] : $aEvent['path']) : $this->getEventLabel($aEvent['type'], $aEvent['name'], $aEvent['module']),
  1019. 'path' => $aEvent['type'] == BX_ANALYTICS_HIT_PAGEVIEW ? $aEvent['path'] : '',
  1020. 'engaged' => (int)round($aEvent['engaged'] / 1000),
  1021. ];
  1022. $aMember = $this->getMember((int)$aSession['profile_id']);
  1023. return [
  1024. 'view' => 'detail',
  1025. 'id' => (int)$aSession['id'],
  1026. 'member' => $aMember,
  1027. 'facts' => [
  1028. ['title' => _t('_bx_analytics_txt_started'), 'value' => (int)$aSession['started'], 'type' => 'datetime'],
  1029. ['title' => _t('_bx_analytics_txt_duration'), 'value' => (int)$aSession['duration'], 'type' => 'duration'],
  1030. ['title' => _t('_bx_analytics_metric_pageviews'), 'value' => (int)$aSession['pageviews'], 'type' => 'number'],
  1031. ['title' => _t('_bx_analytics_dim_platform'), 'value' => $this->getLabel('platform', $aSession['platform']) . ($aSession['app_version'] !== '' ? ' ' . $aSession['app_version'] : ''), 'type' => 'text'],
  1032. ['title' => _t('_bx_analytics_txt_location'), 'value' => $this->_getLocation($aSession), 'type' => 'text'],
  1033. ['title' => _t('_bx_analytics_dim_device'), 'value' => $this->_getDevice($aSession), 'type' => 'text'],
  1034. ['title' => _t('_bx_analytics_txt_source'), 'value' => $aSession['referrer'] !== '' ? $aSession['referrer'] : $this->getLabel('channel', $aSession['channel'] !== '' ? $aSession['channel'] : 'direct'), 'type' => 'text'],
  1035. ['title' => _t('_bx_analytics_dim_language'), 'value' => $aSession['language'], 'type' => 'text'],
  1036. ],
  1037. 'timeline' => $aTimeline,
  1038. ];
  1039. }
  1040. /**
  1041. * Members who visited in the period. With `user` one member's visits and actions; with
  1042. * `session` too, one of their visits. Needs the people permission.
  1043. */
  1044. public function getUsers(BxAnalyticsFilter $oFilter, $aParams = [])
  1045. {
  1046. $iUser = (int)$this->_param($aParams, 'user', 0);
  1047. if($iUser) {
  1048. if((int)$this->_param($aParams, 'session', 0))
  1049. return $this->_getSessionDetail((int)$aParams['session']);
  1050. return $this->_getUserDetail($oFilter, $iUser, $aParams);
  1051. }
  1052. $aFilter = $oFilter->get();
  1053. $iTotal = $this->_oDb->getMemberListCount($aFilter);
  1054. $iPages = max(1, (int)ceil($iTotal / $this->_iPerPage));
  1055. $iPage = min($iPages, max(1, (int)$this->_param($aParams, 'page_users', 1)));
  1056. $aMembers = $this->_oDb->getMemberList($aFilter, $this->_iPerPage, ($iPage - 1) * $this->_iPerPage);
  1057. // lifetime value: what each member paid, for viewers who may see revenue
  1058. $bRevenue = $this->_oModule->checkAllowedRevenue() === CHECK_ACTION_RESULT_ALLOWED;
  1059. $aLtv = $bRevenue ? $this->getLifetimeValues(array_column($aMembers, 'profile_id')) : [];
  1060. $aRows = [];
  1061. foreach($aMembers as $aRow) {
  1062. $aMember = $this->getMember((int)$aRow['profile_id']);
  1063. $aValues = [
  1064. 'sessions' => (int)$aRow['sessions'],
  1065. 'pageviews' => (int)$aRow['pageviews'],
  1066. 'duration' => (int)$aRow['duration'],
  1067. 'platforms' => $this->_getPlatformsText($aRow['platforms']),
  1068. 'last' => (int)$aRow['last'],
  1069. ];
  1070. if($bRevenue)
  1071. $aValues['ltv'] = isset($aLtv[$aRow['profile_id']]) ? round((float)$aLtv[$aRow['profile_id']], 2) : 0;
  1072. $aRows[] = [
  1073. 'label' => $aMember ? $aMember['name'] : _t('_bx_analytics_txt_deleted_member'),
  1074. 'sub' => $aRow['country'] !== '' ? $this->getLabel('country', $aRow['country']) : '',
  1075. 'open' => ['param' => 'user', 'value' => (int)$aRow['profile_id']],
  1076. 'values' => $aValues,
  1077. ];
  1078. }
  1079. $aColumns = [
  1080. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_txt_member_name')],
  1081. ['key' => 'sessions', 'type' => 'number', 'title' => _t('_bx_analytics_metric_sessions')],
  1082. ['key' => 'pageviews', 'type' => 'number', 'title' => _t('_bx_analytics_metric_pageviews')],
  1083. ['key' => 'duration', 'type' => 'duration', 'title' => _t('_bx_analytics_txt_time_spent')],
  1084. ['key' => 'platforms', 'type' => 'text', 'title' => _t('_bx_analytics_tab_platforms')],
  1085. ];
  1086. if($bRevenue)
  1087. $aColumns[] = ['key' => 'ltv', 'type' => 'money', 'title' => _t('_bx_analytics_txt_lifetime_value')];
  1088. $aColumns[] = ['key' => 'last', 'type' => 'datetime', 'title' => _t('_bx_analytics_txt_last_seen')];
  1089. return [
  1090. 'view' => 'list',
  1091. 'name' => 'users',
  1092. 'caption' => _t('_bx_analytics_lmi_cpt_users'),
  1093. 'columns' => $aColumns,
  1094. 'rows' => $aRows,
  1095. 'page' => $iPage,
  1096. 'pages' => $iPages,
  1097. 'empty' => _t('_bx_analytics_txt_no_members'),
  1098. ];
  1099. }
  1100. protected function _getUserDetail(BxAnalyticsFilter $oFilter, $iProfileId, $aParams)
  1101. {
  1102. $aTotals = $this->_oDb->getMemberTotals($iProfileId);
  1103. if(empty($aTotals['totals']['sessions']))
  1104. return false;
  1105. $aPlatforms = [];
  1106. $iSessions = (int)$aTotals['totals']['sessions'];
  1107. foreach($aTotals['platforms'] as $sPlatform => $iCount)
  1108. $aPlatforms[] = [
  1109. 'label' => $this->getLabel('platform', $sPlatform),
  1110. 'count' => (int)$iCount,
  1111. 'share' => $iSessions ? round(100 * $iCount / $iSessions, 1) : 0,
  1112. ];
  1113. $aActions = [];
  1114. $aRows = $this->_oDb->getMemberActions($iProfileId, $oFilter->get());
  1115. $iMax = 1;
  1116. foreach($aRows as $aRow)
  1117. $iMax = max($iMax, (int)$aRow['count']);
  1118. foreach($aRows as $aRow)
  1119. $aActions[] = [
  1120. 'label' => $this->getEventLabel($aRow['type'], $aRow['name'], $aRow['module']),
  1121. 'bar' => round(100 * $aRow['count'] / $iMax, 1),
  1122. 'values' => ['count' => (int)$aRow['count'], 'last' => (int)$aRow['last']],
  1123. ];
  1124. $aSessions = $this->getSessions($oFilter, $aParams, $iProfileId);
  1125. $aTiles = [
  1126. ['title' => _t('_bx_analytics_metric_sessions'), 'value' => $iSessions, 'type' => 'number'],
  1127. ['title' => _t('_bx_analytics_metric_pageviews'), 'value' => (int)$aTotals['totals']['pageviews'], 'type' => 'number'],
  1128. ['title' => _t('_bx_analytics_txt_time_spent'), 'value' => (int)$aTotals['totals']['duration'], 'type' => 'duration'],
  1129. ['title' => _t('_bx_analytics_txt_first_seen'), 'value' => (int)$aTotals['totals']['first'], 'type' => 'date'],
  1130. ['title' => _t('_bx_analytics_txt_last_seen'), 'value' => (int)$aTotals['totals']['last'], 'type' => 'datetime'],
  1131. ];
  1132. if($this->_oModule->checkAllowedRevenue() === CHECK_ACTION_RESULT_ALLOWED) {
  1133. $aLtv = $this->getLifetimeValues([$iProfileId]);
  1134. $aTiles[] = ['title' => _t('_bx_analytics_txt_lifetime_value'), 'value' => isset($aLtv[$iProfileId]) ? round((float)$aLtv[$iProfileId], 2) : 0, 'type' => 'money'];
  1135. }
  1136. return [
  1137. 'view' => 'user',
  1138. 'member' => $this->getMember($iProfileId),
  1139. 'tiles' => $aTiles,
  1140. 'platforms' => $aPlatforms,
  1141. 'actions' => [
  1142. 'name' => 'user_actions',
  1143. 'caption' => _t('_bx_analytics_txt_actions'),
  1144. 'columns' => [
  1145. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_txt_event')],
  1146. ['key' => 'count', 'type' => 'number', 'title' => _t('_bx_analytics_metric_count')],
  1147. ['key' => 'last', 'type' => 'datetime', 'title' => _t('_bx_analytics_txt_last_seen')],
  1148. ],
  1149. 'rows' => $aActions,
  1150. 'empty' => _t('_bx_analytics_txt_no_events'),
  1151. ],
  1152. 'sessions' => $aSessions,
  1153. ];
  1154. }
  1155. /**
  1156. * Where visitors go from a page: the pages they open next and, for one of those, the
  1157. * pages after it; and the most common three-page paths from the entry page. Params:
  1158. * `from` (default: the top entry page), `via` (a next page to follow further).
  1159. */
  1160. public function getJourneys(BxAnalyticsFilter $oFilter, $aParams = [])
  1161. {
  1162. $iLimit = 200000;
  1163. // each visit's pages in order, a page reloaded in place counted once
  1164. $aRead = $this->_oDb->getPageSequences($oFilter->get(), $iLimit);
  1165. $aSequences = $aRead['sequences'];
  1166. $aEntries = [];
  1167. $aPaths = [];
  1168. foreach($aSequences as $aSequence) {
  1169. $aEntries[$aSequence[0]] = (isset($aEntries[$aSequence[0]]) ? $aEntries[$aSequence[0]] : 0) + 1;
  1170. $sPath = implode("\n", array_slice($aSequence, 0, 3));
  1171. $aPaths[$sPath] = (isset($aPaths[$sPath]) ? $aPaths[$sPath] : 0) + 1;
  1172. }
  1173. arsort($aEntries);
  1174. arsort($aPaths);
  1175. $sFrom = (string)$this->_param($aParams, 'jfrom', '');
  1176. if($sFrom === '' || !isset($aEntries[$sFrom]) && !$this->_inSequences($aSequences, $sFrom))
  1177. $sFrom = !empty($aEntries) ? (string)key($aEntries) : '';
  1178. $sVia = (string)$this->_param($aParams, 'via', '');
  1179. // the page after the first visit of `jfrom`, and after `via` when it came right after
  1180. $iFromSessions = 0;
  1181. $aNext = [];
  1182. $iNextExits = 0;
  1183. $iViaSessions = 0;
  1184. $aAfter = [];
  1185. $iAfterExits = 0;
  1186. foreach($aSequences as $aSequence) {
  1187. $i = array_search($sFrom, $aSequence, true);
  1188. if($i === false)
  1189. continue;
  1190. $iFromSessions++;
  1191. if(!isset($aSequence[$i + 1])) {
  1192. $iNextExits++;
  1193. continue;
  1194. }
  1195. $sNext = $aSequence[$i + 1];
  1196. $aNext[$sNext] = (isset($aNext[$sNext]) ? $aNext[$sNext] : 0) + 1;
  1197. if($sVia !== '' && $sNext === $sVia) {
  1198. $iViaSessions++;
  1199. if(isset($aSequence[$i + 2]))
  1200. $aAfter[$aSequence[$i + 2]] = (isset($aAfter[$aSequence[$i + 2]]) ? $aAfter[$aSequence[$i + 2]] : 0) + 1;
  1201. else
  1202. $iAfterExits++;
  1203. }
  1204. }
  1205. arsort($aNext);
  1206. arsort($aAfter);
  1207. $fStep = function($aCounts, $iExits, $iTotal, $iLimit) {
  1208. $aResult = [];
  1209. foreach(array_slice($aCounts, 0, $iLimit, true) as $sPage => $iCount)
  1210. $aResult[] = ['page' => (string)$sPage, 'sessions' => $iCount, 'share' => $iTotal ? round(100 * $iCount / $iTotal, 1) : 0];
  1211. if($iExits)
  1212. $aResult[] = ['page' => '', 'exit' => true, 'sessions' => $iExits, 'share' => $iTotal ? round(100 * $iExits / $iTotal, 1) : 0];
  1213. return $aResult;
  1214. };
  1215. $iTotal = count($aSequences);
  1216. $aTopPaths = [];
  1217. foreach(array_slice($aPaths, 0, 10, true) as $sPath => $iCount)
  1218. $aTopPaths[] = ['pages' => explode("\n", (string)$sPath), 'sessions' => $iCount, 'share' => $iTotal ? round(100 * $iCount / $iTotal, 1) : 0];
  1219. $aEntryList = [];
  1220. foreach(array_slice($aEntries, 0, 10, true) as $sPage => $iCount)
  1221. $aEntryList[] = ['page' => (string)$sPage, 'sessions' => $iCount, 'active' => (string)$sPage === $sFrom];
  1222. return [
  1223. 'jfrom' => $sFrom,
  1224. 'via' => $sVia !== '' && $iViaSessions ? $sVia : '',
  1225. 'sessions' => $iTotal,
  1226. 'truncated' => $aRead['rows'] >= $iLimit,
  1227. 'entries' => $aEntryList,
  1228. 'from_sessions' => $iFromSessions,
  1229. 'next' => $fStep($aNext, $iNextExits, $iFromSessions, 8),
  1230. 'after' => $iViaSessions ? $fStep($aAfter, $iAfterExits, $iViaSessions, 8) : [],
  1231. 'via_sessions' => $iViaSessions,
  1232. 'paths' => $aTopPaths,
  1233. ];
  1234. }
  1235. /**
  1236. * Weekly cohorts by first visit and the share of each that came back in the weeks after.
  1237. * Tabs: members (stable ids) or all visitor ids (with cookieless ids a guest is new every
  1238. * day, so only persistent ids make guest retention meaningful).
  1239. */
  1240. public function getRetention(BxAnalyticsFilter $oFilter, $aParams = [])
  1241. {
  1242. $aTabs = $this->_aReportTabs['retention'];
  1243. $sTab = $this->_getTab($aParams, 'retention', $aTabs);
  1244. $aFilter = $oFilter->get();
  1245. $iWeeks = 12;
  1246. $iOffset = (int)$aFilter['offset'];
  1247. $fWeek = function($iTime) use ($iOffset) {
  1248. return (int)(floor(($iTime + $iOffset - 345600) / 604800) * 604800 + 345600 - $iOffset);
  1249. };
  1250. $iNow = (int)$aFilter['now'];
  1251. $iThisWeek = $fWeek($iNow);
  1252. $iFrom = $iThisWeek - ($iWeeks - 1) * 604800;
  1253. $aCohorts = [];
  1254. for($i = 0; $i < $iWeeks; $i++)
  1255. $aCohorts[$iFrom + $i * 604800] = ['size' => 0, 'back' => array_fill(0, $iWeeks - $i, 0)];
  1256. foreach($this->_oDb->getRetentionCohorts($iFrom, $iNow + 1, $aFilter['platform'], $aTabs[$sTab]) as $aRow) {
  1257. $iCohort = $iFrom + (int)$aRow['cohort'] * 604800;
  1258. if(!isset($aCohorts[$iCohort]))
  1259. continue;
  1260. $iVisitors = (int)$aRow['visitors'];
  1261. $iMask = (int)$aRow['weeks'];
  1262. $aCohorts[$iCohort]['size'] += $iVisitors;
  1263. foreach(array_keys($aCohorts[$iCohort]['back']) as $iIndex)
  1264. if($iMask & (1 << ((int)$aRow['cohort'] + $iIndex)))
  1265. $aCohorts[$iCohort]['back'][$iIndex] += $iVisitors;
  1266. }
  1267. $aRows = [];
  1268. foreach($aCohorts as $iStart => $aCohort)
  1269. $aRows[] = [
  1270. 'start' => $iStart,
  1271. 'size' => $aCohort['size'],
  1272. 'weeks' => array_map(function($iCount) use ($aCohort) {
  1273. return $aCohort['size'] ? round(100 * $iCount / $aCohort['size'], 1) : null;
  1274. }, $aCohort['back']),
  1275. ];
  1276. return [
  1277. 'tab' => $sTab,
  1278. 'tabs' => $this->_getTabs($aTabs, $sTab, '_bx_analytics_tab_retention_'),
  1279. 'weeks' => $iWeeks,
  1280. 'tz' => $aFilter['tz'],
  1281. 'rows' => $aRows,
  1282. 'cookieless' => $sTab == 'visitors' && $this->_oConfig->getIdentityMode() == BX_ANALYTICS_IDENTITY_COOKIELESS,
  1283. ];
  1284. }
  1285. /**
  1286. * Goals with their conversions: visits and visitors that reached them, the conversion
  1287. * rate of all visits, and the change against the previous period.
  1288. */
  1289. public function getGoals(BxAnalyticsFilter $oFilter, $aParams = [])
  1290. {
  1291. $aFilter = $oFilter->get();
  1292. $aPrevious = $oFilter->getPrevious();
  1293. $aTotals = $this->_oDb->getTotals($aFilter);
  1294. $aTotalsPrevious = $this->_oDb->getTotals($aPrevious);
  1295. $iSessions = (int)$aTotals['sessions'];
  1296. $iSessionsPrevious = (int)$aTotalsPrevious['sessions'];
  1297. $aGoals = [];
  1298. foreach($this->_oDb->getGoals($this->_oConfig->CNF['MAX_GOALS']) as $aGoal) {
  1299. $aCurrent = $this->_oDb->getGoalTotals($aFilter, $aGoal);
  1300. $aBefore = $this->_oDb->getGoalTotals($aPrevious, $aGoal);
  1301. $fRate = $iSessions ? round(100 * $aCurrent['sessions'] / $iSessions, 2) : 0;
  1302. $fRateBefore = $iSessionsPrevious ? round(100 * $aBefore['sessions'] / $iSessionsPrevious, 2) : 0;
  1303. $aGoals[] = [
  1304. 'id' => (int)$aGoal['id'],
  1305. 'name' => $aGoal['name'],
  1306. 'type' => $aGoal['type'],
  1307. 'pattern' => $aGoal['pattern'],
  1308. 'conversions' => (int)$aCurrent['sessions'],
  1309. 'visitors' => (int)$aCurrent['visitors'],
  1310. 'rate' => $fRate,
  1311. 'change' => $this->_change($fRate, $fRateBefore),
  1312. ];
  1313. }
  1314. return [
  1315. 'goals' => $aGoals,
  1316. 'sessions' => $iSessions,
  1317. 'manage' => $this->_oModule->checkAllowedManage() === CHECK_ACTION_RESULT_ALLOWED,
  1318. 'edit' => (string)$this->_param($aParams, 'edit_goal', ''),
  1319. ];
  1320. }
  1321. /**
  1322. * A funnel's steps with the visits that reached each one in order within the visit, and
  1323. * the drop between steps. Param `funnel` (default: the first).
  1324. */
  1325. public function getFunnels(BxAnalyticsFilter $oFilter, $aParams = [])
  1326. {
  1327. $aFunnels = $this->_oDb->getFunnels();
  1328. $iFunnel = (int)$this->_param($aParams, 'funnel', 0);
  1329. $aFunnel = !empty($aFunnels) ? $aFunnels[0] : null;
  1330. foreach($aFunnels as $a)
  1331. if((int)$a['id'] === $iFunnel)
  1332. $aFunnel = $a;
  1333. $aSteps = [];
  1334. if($aFunnel) {
  1335. $aDefinition = json_decode($aFunnel['steps'], true);
  1336. $aDefinition = is_array($aDefinition) ? $aDefinition : [];
  1337. // each visit walks the steps in order: a step counts when it happens after the previous one
  1338. $aDefinition = array_values(array_filter($aDefinition, function($aStep) {
  1339. return is_array($aStep) && isset($aStep['type'], $aStep['pattern']);
  1340. }));
  1341. foreach($this->_oDb->getFunnelCounts($oFilter->get(), $aDefinition) as $i => $iSessions)
  1342. $aSteps[] = [
  1343. 'type' => $aDefinition[$i]['type'],
  1344. 'pattern' => $aDefinition[$i]['pattern'],
  1345. 'sessions' => $iSessions,
  1346. ];
  1347. $iFirst = !empty($aSteps) ? $aSteps[0]['sessions'] : 0;
  1348. foreach($aSteps as $i => $aStep) {
  1349. $aSteps[$i]['rate'] = $iFirst ? round(100 * $aStep['sessions'] / $iFirst, 1) : 0;
  1350. $aSteps[$i]['drop'] = $i > 0 && $aSteps[$i - 1]['sessions'] ? round(100 * ($aSteps[$i - 1]['sessions'] - $aStep['sessions']) / $aSteps[$i - 1]['sessions'], 1) : null;
  1351. }
  1352. }
  1353. $aList = [];
  1354. foreach($aFunnels as $a)
  1355. $aList[] = ['id' => (int)$a['id'], 'name' => $a['name'], 'active' => $aFunnel && (int)$a['id'] === (int)$aFunnel['id']];
  1356. return [
  1357. 'funnels' => $aList,
  1358. 'funnel' => $aFunnel ? ['id' => (int)$aFunnel['id'], 'name' => $aFunnel['name']] : null,
  1359. 'steps' => $aSteps,
  1360. 'manage' => $this->_oModule->checkAllowedManage() === CHECK_ACTION_RESULT_ALLOWED,
  1361. 'edit' => (string)$this->_param($aParams, 'edit_funnel', ''),
  1362. 'definition' => $aFunnel ? json_decode($aFunnel['steps'], true) : [],
  1363. 'max_steps' => $this->_oConfig->CNF['MAX_FUNNEL_STEPS'],
  1364. ];
  1365. }
  1366. protected function _inSequences($aSequences, $sPage)
  1367. {
  1368. foreach($aSequences as $aSequence)
  1369. if(in_array($sPage, $aSequence, true))
  1370. return true;
  1371. return false;
  1372. }
  1373. /*
  1374. * Health: client errors and performance
  1375. */
  1376. /**
  1377. * Client errors grouped by fingerprint, with their hits, visitors and platforms in the
  1378. * period and a trend per group. With `error` (a group id) one group in detail. Tabs: open,
  1379. * new (first seen in the period), resolved, ignored, all.
  1380. */
  1381. public function getErrors(BxAnalyticsFilter $oFilter, $aParams = [])
  1382. {
  1383. $iError = (int)$this->_param($aParams, 'error', 0);
  1384. if($iError)
  1385. return $this->_getErrorDetail($oFilter, $iError);
  1386. // ignored errors keep counting but stay out of everything but their own tab
  1387. $aCounted = ['open', 'resolved'];
  1388. $aTabs = $this->_aReportTabs['errors'];
  1389. $sTab = $this->_getTab($aParams, 'errors', $aTabs);
  1390. $aFilter = $oFilter->get();
  1391. $iNewFrom = $sTab == 'new' ? (int)$aFilter['from'] : 0;
  1392. // crashes are fatal errors in the apps, against the apps' visits
  1393. $aApps = array_values(array_filter(array_keys($this->_oConfig->getPlatforms()), [$this->_oConfig, 'isAppPlatform']));
  1394. $aTotals = $this->_oDb->getErrorTotals($aFilter, $aCounted, $aApps);
  1395. $aBefore = $this->_oDb->getErrorTotals($oFilter->getPrevious(), $aCounted);
  1396. $aSessions = $this->_oDb->getSessionsPerPlatform($aFilter);
  1397. $iSessions = (int)array_sum($aSessions);
  1398. $iAppSessions = 0;
  1399. foreach($aApps as $sApp)
  1400. $iAppSessions += isset($aSessions[$sApp]) ? (int)$aSessions[$sApp] : 0;
  1401. $aTiles = [
  1402. ['title' => _t('_bx_analytics_metric_errors'), 'value' => (int)$aTotals['hits'], 'type' => 'number', 'metric' => 'errors', 'change' => $this->_change((int)$aTotals['hits'], (int)$aBefore['hits'])],
  1403. ['title' => _t('_bx_analytics_metric_error_sessions'), 'value' => $iSessions ? round(100 * $aTotals['sessions'] / $iSessions, 1) : null, 'type' => 'percent', 'sub' => _t('_bx_analytics_txt_n_sessions', (int)$aTotals['sessions'])],
  1404. ];
  1405. if($aFilter['platform'] === '' || $this->_oConfig->isAppPlatform($aFilter['platform']))
  1406. $aTiles[] = ['title' => _t('_bx_analytics_metric_crash_free'), 'value' => $iAppSessions ? round(100 - 100 * min($iAppSessions, (int)$aTotals['fatal_sessions']) / $iAppSessions, 2) : null, 'type' => 'percent_precise', 'sub' => _t('_bx_analytics_txt_crash_free_apps')];
  1407. $aTiles[] = ['title' => _t('_bx_analytics_metric_new_errors'), 'value' => $this->_oDb->getErrorListCount($aFilter, $aCounted, (int)$aFilter['from']), 'type' => 'number'];
  1408. $iTotal = $this->_oDb->getErrorListCount($aFilter, $aTabs[$sTab], $iNewFrom);
  1409. $iPages = max(1, (int)ceil($iTotal / $this->_iPerPage));
  1410. $iPage = min($iPages, max(1, (int)$this->_param($aParams, 'page_errors', 1)));
  1411. $aList = $this->_oDb->getErrorList($aFilter, $aTabs[$sTab], $iNewFrom, $this->_iPerPage, ($iPage - 1) * $this->_iPerPage);
  1412. $aIds = array_map('intval', array_column($aList, 'error_id'));
  1413. $aGroups = $this->_oDb->getErrorGroups($aIds);
  1414. $aTrends = $this->_oDb->getErrorTrends($aFilter, $aIds, $aFilter['bucket'], $aFilter['offset'], $aFilter['starts']);
  1415. $iMax = 1;
  1416. foreach($aList as $aRow)
  1417. $iMax = max($iMax, (int)$aRow['count']);
  1418. $aRows = [];
  1419. foreach($aList as $aRow) {
  1420. $iId = (int)$aRow['error_id'];
  1421. if(!isset($aGroups[$iId]))
  1422. continue;
  1423. $aGroup = $aGroups[$iId];
  1424. $aTrend = [];
  1425. foreach($aFilter['buckets'] as $iBucket)
  1426. $aTrend[] = isset($aTrends[$iId][$iBucket]) ? $aTrends[$iId][$iBucket] : 0;
  1427. $aRows[] = [
  1428. 'label' => $this->getErrorLabel($aGroup),
  1429. 'sub' => implode(' · ', array_filter([
  1430. _t('_bx_analytics_error_kind_' . $aGroup['kind']),
  1431. $this->_getErrorLocation($aGroup),
  1432. (int)$aRow['fatal'] ? _t('_bx_analytics_txt_fatal') : '',
  1433. $sTab == 'all' ? _t('_bx_analytics_error_status_' . $aGroup['status']) : '',
  1434. ])),
  1435. 'open' => ['param' => 'error', 'value' => $iId],
  1436. 'bar' => round(100 * $aRow['count'] / $iMax, 1),
  1437. 'values' => [
  1438. 'trend' => $aTrend,
  1439. 'count' => (int)$aRow['count'],
  1440. 'users' => (int)$aRow['users'],
  1441. 'platforms' => $this->_getPlatformsText($aRow['platforms']),
  1442. 'first_seen' => (int)$aGroup['first_seen'],
  1443. 'last_seen' => (int)$aRow['last_in'],
  1444. ],
  1445. ];
  1446. }
  1447. return [
  1448. 'view' => 'list',
  1449. 'tiles' => $aTiles,
  1450. 'name' => 'errors',
  1451. 'caption' => _t('_bx_analytics_lmi_cpt_errors'),
  1452. 'tab' => $sTab,
  1453. 'tabs' => $this->_getTabs($aTabs, $sTab, '_bx_analytics_tab_errors_'),
  1454. 'columns' => [
  1455. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_txt_error')],
  1456. ['key' => 'trend', 'type' => 'spark', 'title' => _t('_bx_analytics_txt_trend')],
  1457. ['key' => 'count', 'type' => 'number', 'title' => _t('_bx_analytics_metric_occurrences')],
  1458. ['key' => 'users', 'type' => 'number', 'title' => _t('_bx_analytics_metric_visitors')],
  1459. ['key' => 'platforms', 'type' => 'text', 'title' => _t('_bx_analytics_dim_platform')],
  1460. ['key' => 'last_seen', 'type' => 'datetime', 'title' => _t('_bx_analytics_txt_last_seen')],
  1461. ],
  1462. 'rows' => $aRows,
  1463. 'page' => $iPage,
  1464. 'pages' => $iPages,
  1465. 'empty' => _t($sTab == 'open' || $sTab == 'new' ? '_bx_analytics_txt_no_errors' : '_bx_analytics_txt_no_data'),
  1466. ];
  1467. }
  1468. /**
  1469. * One error group: what and where, its hits over time and by client, and the latest
  1470. * occurrences. The stack and the visits behind the occurrences can carry what a visitor
  1471. * typed or opened, so they need the people permission.
  1472. */
  1473. protected function _getErrorDetail(BxAnalyticsFilter $oFilter, $iId)
  1474. {
  1475. $aGroup = $this->_oDb->getErrorGroup($iId);
  1476. if(empty($aGroup))
  1477. return ['view' => 'detail', 'missing' => true, 'title' => _t('_bx_analytics_txt_error_missing')];
  1478. $aFilter = $oFilter->get();
  1479. $bPeople = $this->_oModule->checkAllowedPeople() === CHECK_ACTION_RESULT_ALLOWED;
  1480. $bManage = $this->_oModule->checkAllowedManage() === CHECK_ACTION_RESULT_ALLOWED;
  1481. $aStats = $this->_oDb->getErrorStats($aFilter, $iId);
  1482. $aRows = $this->_oDb->getErrorSeries($aFilter, $iId, $aFilter['bucket'], $aFilter['offset'], $aFilter['starts']);
  1483. $aCounts = [];
  1484. $aUsers = [];
  1485. foreach($aFilter['buckets'] as $iBucket) {
  1486. $aCounts[] = isset($aRows[$iBucket]) ? (int)$aRows[$iBucket]['count'] : 0;
  1487. $aUsers[] = isset($aRows[$iBucket]) ? (int)$aRows[$iBucket]['users'] : 0;
  1488. }
  1489. // where it happens: a table per client column that has values
  1490. $aBreakdowns = [];
  1491. foreach(['platform', 'route', 'browser', 'os', 'device', 'app_version', 'country'] as $sColumn) {
  1492. $aValues = $this->_oDb->getErrorBreakdown($aFilter, $iId, $sColumn);
  1493. if(empty($aValues) || (count($aValues) == 1 && $aValues[0]['value'] === ''))
  1494. continue;
  1495. $iTotal = 0;
  1496. foreach($aValues as $aValue)
  1497. $iTotal += (int)$aValue['count'];
  1498. $aBreakdownRows = [];
  1499. foreach($aValues as $aValue)
  1500. $aBreakdownRows[] = [
  1501. 'label' => $this->getLabel($sColumn, (string)$aValue['value']),
  1502. 'bar' => $iTotal ? round(100 * $aValue['count'] / $iTotal, 1) : 0,
  1503. 'values' => [
  1504. 'count' => (int)$aValue['count'],
  1505. 'share' => $iTotal ? round(100 * $aValue['count'] / $iTotal, 1) : 0,
  1506. ],
  1507. ];
  1508. $sTitle = _t($sColumn == 'route' ? '_bx_analytics_txt_route' : '_bx_analytics_dim_' . $sColumn);
  1509. $aBreakdowns[] = [
  1510. 'name' => 'error_' . $sColumn,
  1511. 'caption' => $sTitle,
  1512. 'columns' => [
  1513. ['key' => 'label', 'type' => 'label', 'title' => $sTitle],
  1514. ['key' => 'count', 'type' => 'number', 'title' => _t('_bx_analytics_metric_occurrences')],
  1515. ['key' => 'share', 'type' => 'percent', 'title' => _t('_bx_analytics_txt_share')],
  1516. ],
  1517. 'rows' => $aBreakdownRows,
  1518. ];
  1519. }
  1520. $aOccurrences = [];
  1521. foreach($this->_oDb->getErrorOccurrences($aFilter, $iId) as $aHit) {
  1522. $aRow = [
  1523. 'label' => $aHit['path'] !== '' ? $aHit['path'] : '/',
  1524. 'sub' => implode(' · ', array_filter([
  1525. $this->getLabel('platform', $aHit['platform']),
  1526. $aHit['app_version'],
  1527. $aHit['device'] !== '' ? $this->getLabel('device', $aHit['device']) : '',
  1528. $aHit['browser'],
  1529. $aHit['os'],
  1530. $aHit['country'] !== '' ? $this->getLabel('country', $aHit['country']) : '',
  1531. ])),
  1532. 'values' => [
  1533. 'ts' => (int)$aHit['ts'],
  1534. 'repeats' => (int)$aHit['repeats'],
  1535. ],
  1536. ];
  1537. // the visit behind it, for those who may see visits (Studio is for administrators)
  1538. if($bPeople && (int)$aHit['session_id'] && isAdmin())
  1539. $aRow['link'] = BX_DOL_URL_STUDIO . 'module.php?name=' . $this->_oModule->getName() . '&page=sessions&session=' . (int)$aHit['session_id'];
  1540. $aOccurrences[] = $aRow;
  1541. }
  1542. $aActions = [];
  1543. if($bManage)
  1544. foreach(['resolved', 'ignored', 'open'] as $sStatus)
  1545. if($sStatus != $aGroup['status'])
  1546. $aActions[] = ['op' => $sStatus, 'title' => _t('_bx_analytics_txt_error_mark_' . $sStatus)];
  1547. $sLocation = $this->_getErrorLocation($aGroup, true);
  1548. return [
  1549. 'view' => 'detail',
  1550. 'id' => (int)$aGroup['id'],
  1551. 'title' => $this->getErrorLabel($aGroup),
  1552. 'status' => $aGroup['status'],
  1553. 'facts' => array_values(array_filter([
  1554. ['title' => _t('_bx_analytics_txt_error_status'), 'value' => _t('_bx_analytics_error_status_' . $aGroup['status']), 'type' => 'text'],
  1555. ['title' => _t('_bx_analytics_txt_error_kind'), 'value' => _t('_bx_analytics_error_kind_' . $aGroup['kind']) . ((int)$aGroup['fatal'] ? ' · ' . _t('_bx_analytics_txt_fatal') : ''), 'type' => 'text'],
  1556. $sLocation !== '' ? ['title' => _t('_bx_analytics_txt_error_source'), 'value' => $sLocation, 'type' => 'text'] : null,
  1557. (int)$aGroup['status_code'] ? ['title' => _t('_bx_analytics_txt_error_http_status'), 'value' => (string)(int)$aGroup['status_code'], 'type' => 'text'] : null,
  1558. ['title' => _t('_bx_analytics_txt_first_seen'), 'value' => (int)$aGroup['first_seen'], 'type' => 'datetime'],
  1559. ['title' => _t('_bx_analytics_txt_last_seen'), 'value' => (int)$aGroup['last_seen'], 'type' => 'datetime'],
  1560. ['title' => _t('_bx_analytics_txt_error_all_time'), 'value' => (int)$aGroup['occurrences'], 'type' => 'number'],
  1561. ])),
  1562. 'stack' => $bPeople ? (string)$aGroup['stack'] : '',
  1563. 'stack_hidden' => !$bPeople && (string)$aGroup['stack'] !== '',
  1564. 'tiles' => [
  1565. ['title' => _t('_bx_analytics_metric_occurrences'), 'value' => (int)$aStats['count'], 'type' => 'number'],
  1566. ['title' => _t('_bx_analytics_metric_visitors'), 'value' => (int)$aStats['users'], 'type' => 'number'],
  1567. ['title' => _t('_bx_analytics_metric_sessions'), 'value' => (int)$aStats['sessions'], 'type' => 'number'],
  1568. ],
  1569. 'chart' => [
  1570. 'kind' => 'lines',
  1571. 'labels' => $aFilter['buckets'],
  1572. 'bucket' => $aFilter['bucket'],
  1573. 'tz' => $aFilter['tz'],
  1574. 'datasets' => [
  1575. ['key' => 'count', 'label' => _t('_bx_analytics_metric_occurrences'), 'values' => $aCounts],
  1576. ['key' => 'users', 'label' => _t('_bx_analytics_metric_visitors'), 'values' => $aUsers],
  1577. ],
  1578. ],
  1579. 'breakdowns' => $aBreakdowns,
  1580. 'occurrences' => [
  1581. 'name' => 'error_hits',
  1582. 'caption' => _t('_bx_analytics_txt_error_latest'),
  1583. 'columns' => [
  1584. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_tab_pages')],
  1585. ['key' => 'repeats', 'type' => 'number', 'title' => _t('_bx_analytics_metric_occurrences')],
  1586. ['key' => 'ts', 'type' => 'datetime', 'title' => _t('_bx_analytics_txt_time')],
  1587. ],
  1588. 'rows' => $aOccurrences,
  1589. ],
  1590. 'manage' => $bManage,
  1591. 'actions' => $aActions,
  1592. ];
  1593. }
  1594. /**
  1595. * What an error group is called in lists: its message, or a name for the catch-all group.
  1596. */
  1597. public function getErrorLabel($aGroup)
  1598. {
  1599. if($aGroup['kind'] == BxAnalyticsErrors::KIND_OVERFLOW)
  1600. return _t('_bx_analytics_txt_error_overflow');
  1601. return $aGroup['message'] !== '' ? $aGroup['message'] : _t('_bx_analytics_txt_unknown');
  1602. }
  1603. /**
  1604. * Script and position of an error, file:line or file:line:column.
  1605. */
  1606. protected function _getErrorLocation($aGroup, $bColumn = false)
  1607. {
  1608. if($aGroup['source'] === '')
  1609. return '';
  1610. $sLocation = $aGroup['source'];
  1611. if((int)$aGroup['line'])
  1612. $sLocation .= ':' . (int)$aGroup['line'] . ($bColumn && (int)$aGroup['col'] ? ':' . (int)$aGroup['col'] : '');
  1613. return $sLocation;
  1614. }
  1615. /**
  1616. * Web vitals and app timings: the 75th percentile of each metric with the share of good,
  1617. * needing improvement and poor values; the chosen metric over time, by platform and by
  1618. * page. Params: `vital` (metric), `vital_device` (all, mobile, desktop), tab
  1619. * (`tab_performance`): route or path.
  1620. */
  1621. public function getPerformance(BxAnalyticsFilter $oFilter, $aParams = [])
  1622. {
  1623. $aFilter = $oFilter->get();
  1624. $aThresholds = $this->_oConfig->CNF['VITALS'];
  1625. $iMinSamples = 20;
  1626. // the metrics the platform has: web vitals in browsers, screen loads and starts in apps
  1627. $sPlatform = $aFilter['platform'];
  1628. $aMetrics = BxAnalyticsVitals::METRICS;
  1629. if($sPlatform !== '' && $this->_oConfig->isBrowserPlatform($sPlatform))
  1630. $aMetrics = BxAnalyticsVitals::WEB_METRICS;
  1631. elseif($sPlatform !== '' && $this->_oConfig->isAppPlatform($sPlatform))
  1632. $aMetrics = BxAnalyticsVitals::APP_METRICS;
  1633. $sMetric = (string)$this->_param($aParams, 'vital', '');
  1634. if(!in_array($sMetric, $aMetrics, true))
  1635. $sMetric = $aMetrics[0];
  1636. $sDevice = (string)$this->_param($aParams, 'vital_device', 'all');
  1637. if(!in_array($sDevice, ['all', 'mobile', 'desktop'], true))
  1638. $sDevice = 'all';
  1639. $aGroups = $this->_aReportTabs['performance'];
  1640. $sGroup = $this->_getTab($aParams, 'performance', $aGroups);
  1641. // the cards: exact counts on each side of the thresholds, and the histograms of the
  1642. // metrics with enough samples, each in one pass
  1643. $aDistribution = $this->_oDb->getVitalsDistribution($aFilter, $sDevice, $aThresholds);
  1644. $aCardMetrics = [];
  1645. foreach($aMetrics as $sCardMetric)
  1646. if($aDistribution[$sCardMetric]['samples'] >= $iMinSamples)
  1647. $aCardMetrics[] = $sCardMetric;
  1648. list($aCardHistograms) = $this->_getHistograms($this->_oDb->getVitalsHistograms($aFilter, $aCardMetrics, $sDevice));
  1649. $aCards = [];
  1650. foreach($aMetrics as $sCardMetric) {
  1651. $aCount = $aDistribution[$sCardMetric];
  1652. $iSamples = $aCount['samples'];
  1653. $mixedP75 = null;
  1654. if(isset($aCardHistograms[$sCardMetric]))
  1655. $mixedP75 = BxAnalyticsVitals::getPercentile($aCardHistograms[$sCardMetric], 0.75, $iMinSamples, [$aCount['good'], $iSamples - $aCount['good'] - $aCount['poor'], $aCount['poor']], $aThresholds[$sCardMetric]);
  1656. $aCards[] = [
  1657. 'metric' => $sCardMetric,
  1658. 'title' => _t('_bx_analytics_vital_' . $sCardMetric),
  1659. 'unit' => BxAnalyticsVitals::getUnit($sCardMetric),
  1660. 'value' => $mixedP75 !== null ? BxAnalyticsVitals::toDisplay($sCardMetric, round($mixedP75)) : null,
  1661. 'rating' => $mixedP75 !== null ? BxAnalyticsVitals::getRating(round($mixedP75), $aThresholds[$sCardMetric]) : '',
  1662. 'samples' => $iSamples,
  1663. 'good' => $iSamples ? round(100 * $aCount['good'] / $iSamples, 1) : 0,
  1664. 'needs_improvement' => $iSamples ? round(100 * ($iSamples - $aCount['good'] - $aCount['poor']) / $iSamples, 1) : 0,
  1665. 'poor' => $iSamples ? round(100 * $aCount['poor'] / $iSamples, 1) : 0,
  1666. 'good_max' => BxAnalyticsVitals::toDisplay($sCardMetric, $aThresholds[$sCardMetric][0]),
  1667. 'poor_min' => BxAnalyticsVitals::toDisplay($sCardMetric, $aThresholds[$sCardMetric][1]),
  1668. 'active' => $sCardMetric == $sMetric,
  1669. ];
  1670. }
  1671. $sUnit = BxAnalyticsVitals::getUnit($sMetric);
  1672. $fP75 = function($aHistogram, $aSides, $iMin) use ($sMetric, $aThresholds) {
  1673. $mixedValue = BxAnalyticsVitals::getPercentile($aHistogram, 0.75, $iMin, $aSides, $aThresholds[$sMetric]);
  1674. return $mixedValue !== null ? BxAnalyticsVitals::toDisplay($sMetric, round($mixedValue)) : null;
  1675. };
  1676. $fRating = function($mixedValue) use ($sMetric, $aThresholds) {
  1677. return $mixedValue !== null ? BxAnalyticsVitals::getRating($sMetric == 'cls' ? round($mixedValue * 1000) : $mixedValue, $aThresholds[$sMetric]) : '';
  1678. };
  1679. // the 75th percentile per time bucket, with the thresholds as lines; a bucket with few
  1680. // samples is left as a gap
  1681. list($aByBucket, $aBucketSides) = $this->_getHistograms($this->_oDb->getVitalsHistogram($aFilter, $sMetric, $sDevice, 'bucket', [], [$aFilter['bucket'], $aFilter['offset'], $aFilter['starts']]));
  1682. $aValues = [];
  1683. foreach($aFilter['buckets'] as $iBucket)
  1684. $aValues[] = isset($aByBucket[$iBucket]) ? $fP75($aByBucket[$iBucket], $aBucketSides[$iBucket], 5) : null;
  1685. $iCount = count($aFilter['buckets']);
  1686. $aPlatformRows = [];
  1687. list($aByPlatform, $aPlatformSides) = $this->_getHistograms($this->_oDb->getVitalsHistogram($aFilter, $sMetric, $sDevice, 'platform'));
  1688. foreach($aByPlatform as $sValue => $aHistogram) {
  1689. $mixedValue = $fP75($aHistogram, $aPlatformSides[$sValue], $iMinSamples);
  1690. $aPlatformRows[] = [
  1691. 'label' => $this->getLabel('platform', (string)$sValue),
  1692. 'values' => ['p75' => $mixedValue, 'rating' => $fRating($mixedValue), 'samples' => (int)array_sum($aHistogram)],
  1693. ];
  1694. }
  1695. usort($aPlatformRows, function($a, $b) { return $b['values']['samples'] - $a['values']['samples']; });
  1696. $iTotal = $this->_oDb->getVitalsPagesCount($aFilter, $sMetric, $sDevice, $sGroup);
  1697. $iPages = max(1, (int)ceil($iTotal / $this->_iPerPage));
  1698. $iPage = min($iPages, max(1, (int)$this->_param($aParams, 'page_performance', 1)));
  1699. $aPages = $this->_oDb->getVitalsPages($aFilter, $sMetric, $sDevice, $sGroup, $this->_iPerPage, ($iPage - 1) * $this->_iPerPage);
  1700. list($aPageHistograms, $aPageSides) = $this->_getHistograms($this->_oDb->getVitalsHistogram($aFilter, $sMetric, $sDevice, $sGroup, array_map('strval', array_keys($aPages))));
  1701. $iMax = 1;
  1702. foreach($aPages as $iSamples)
  1703. $iMax = max($iMax, (int)$iSamples);
  1704. $aPageRows = [];
  1705. foreach($aPages as $sValue => $iSamples) {
  1706. $mixedValue = isset($aPageHistograms[$sValue]) ? $fP75($aPageHistograms[$sValue], $aPageSides[$sValue], $iMinSamples) : null;
  1707. $aPageRows[] = [
  1708. 'label' => (string)$sValue,
  1709. 'bar' => round(100 * $iSamples / $iMax, 1),
  1710. 'values' => ['p75' => $mixedValue, 'rating' => $fRating($mixedValue), 'samples' => (int)$iSamples],
  1711. ];
  1712. }
  1713. $aColumns = function($sTitle) use ($sUnit) {
  1714. return [
  1715. ['key' => 'label', 'type' => 'label', 'title' => $sTitle],
  1716. ['key' => 'p75', 'type' => $sUnit, 'title' => _t('_bx_analytics_txt_p75')],
  1717. ['key' => 'rating', 'type' => 'rating', 'title' => _t('_bx_analytics_txt_rating')],
  1718. ['key' => 'samples', 'type' => 'number', 'title' => _t('_bx_analytics_txt_samples')],
  1719. ];
  1720. };
  1721. $aDevices = [];
  1722. foreach(['all', 'mobile', 'desktop'] as $sValue)
  1723. $aDevices[] = ['value' => $sValue, 'title' => _t('_bx_analytics_txt_device_' . $sValue), 'selected' => $sValue == $sDevice];
  1724. return [
  1725. 'metric' => $sMetric,
  1726. 'metric_title' => _t('_bx_analytics_vital_' . $sMetric),
  1727. 'unit' => $sUnit,
  1728. 'device' => $sDevice,
  1729. 'devices' => $aDevices,
  1730. 'min_samples' => $iMinSamples,
  1731. 'cards' => $aCards,
  1732. 'chart' => [
  1733. 'kind' => 'lines',
  1734. 'metric' => $sUnit,
  1735. 'labels' => $aFilter['buckets'],
  1736. 'bucket' => $aFilter['bucket'],
  1737. 'tz' => $aFilter['tz'],
  1738. 'datasets' => [
  1739. ['key' => 'p75', 'label' => _t('_bx_analytics_txt_p75'), 'values' => $aValues],
  1740. ['key' => 'good', 'label' => _t('_bx_analytics_txt_threshold_good'), 'values' => array_fill(0, $iCount, BxAnalyticsVitals::toDisplay($sMetric, $aThresholds[$sMetric][0])), 'dashed' => true, 'tone' => 'good'],
  1741. ['key' => 'poor', 'label' => _t('_bx_analytics_txt_threshold_poor'), 'values' => array_fill(0, $iCount, BxAnalyticsVitals::toDisplay($sMetric, $aThresholds[$sMetric][1])), 'dashed' => true, 'tone' => 'bad'],
  1742. ],
  1743. ],
  1744. 'platforms' => [
  1745. 'name' => 'vital_platforms',
  1746. 'caption' => _t('_bx_analytics_tab_platforms'),
  1747. 'columns' => $aColumns(_t('_bx_analytics_dim_platform')),
  1748. 'rows' => $aPlatformRows,
  1749. 'empty' => _t('_bx_analytics_txt_no_vitals'),
  1750. ],
  1751. // named after the block, so its pager and the tabs (which reset the page) share page_performance
  1752. 'pages' => [
  1753. 'name' => 'performance',
  1754. 'caption' => _t('_bx_analytics_txt_vitals_pages'),
  1755. 'tab' => $sGroup,
  1756. 'tabs' => $this->_getTabs($aGroups, $sGroup, '_bx_analytics_tab_vitals_'),
  1757. 'columns' => $aColumns(_t($sGroup == 'route' ? '_bx_analytics_txt_route' : '_bx_analytics_tab_pages')),
  1758. 'rows' => $aPageRows,
  1759. 'page' => $iPage,
  1760. 'pages' => $iPages,
  1761. 'empty' => _t('_bx_analytics_txt_no_vitals'),
  1762. ],
  1763. ];
  1764. }
  1765. /**
  1766. * Histogram rows (grp, hb, n, and side when the query has it) as [[group => [bucket =>
  1767. * count]], [group => [counts up to the good threshold, between, above the poor one]]].
  1768. */
  1769. protected function _getHistograms($aRows)
  1770. {
  1771. $aHistograms = [];
  1772. $aSides = [];
  1773. foreach($aRows as $aRow) {
  1774. $sGroup = (string)$aRow['grp'];
  1775. $iBucket = (int)$aRow['hb'];
  1776. $aHistograms[$sGroup][$iBucket] = (isset($aHistograms[$sGroup][$iBucket]) ? $aHistograms[$sGroup][$iBucket] : 0) + (int)$aRow['n'];
  1777. if(!isset($aSides[$sGroup]))
  1778. $aSides[$sGroup] = isset($aRow['side']) ? [0, 0, 0] : [];
  1779. if(isset($aRow['side']))
  1780. $aSides[$sGroup][min(2, max(0, (int)$aRow['side']))] += (int)$aRow['n'];
  1781. }
  1782. return [$aHistograms, $aSides];
  1783. }
  1784. /*
  1785. * Revenue. Amounts come from the ledger in the site's default currency, as the payment
  1786. * module recorded them; orders paid with credits are reported apart from money.
  1787. */
  1788. /**
  1789. * Gross and net revenue, refunds, orders, paying customers and revenue per customer, over
  1790. * time and split by source, provider, platform and the buyers' traffic sources. Param
  1791. * `tab` (or tab_revenue): sources, providers, platforms, first_touch, last_touch, campaigns.
  1792. */
  1793. public function getRevenue(BxAnalyticsFilter $oFilter, $aParams = [])
  1794. {
  1795. $aMoney = $this->getMoney();
  1796. $aFilter = $this->_getRevenueFilter($oFilter->get(), $aMoney);
  1797. $aPrevious = $this->_getRevenueFilter($oFilter->getPrevious(), $aMoney);
  1798. $aCurrent = $this->_oDb->getRevenueTotals($aFilter);
  1799. $aBefore = $this->_oDb->getRevenueTotals($aPrevious);
  1800. $aTiles = [];
  1801. foreach(['gross' => 'money', 'refunds' => 'money', 'net' => 'money', 'orders' => 'number', 'customers' => 'number', 'arppu' => 'money'] as $sKey => $sType) {
  1802. $fValue = $sKey == 'arppu' ? ((int)$aCurrent['customers'] ? (float)$aCurrent['net'] / (int)$aCurrent['customers'] : 0) : (float)$aCurrent[$sKey];
  1803. $fPrevious = $sKey == 'arppu' ? ((int)$aBefore['customers'] ? (float)$aBefore['net'] / (int)$aBefore['customers'] : 0) : (float)$aBefore[$sKey];
  1804. $aTiles[] = [
  1805. 'key' => $sKey,
  1806. 'title' => _t('_bx_analytics_metric_revenue_' . $sKey),
  1807. 'value' => $sType == 'money' ? round($fValue, 2) : (int)$fValue,
  1808. 'previous' => $sType == 'money' ? round($fPrevious, 2) : (int)$fPrevious,
  1809. 'change' => $this->_change($fValue, $fPrevious),
  1810. 'type' => $sType,
  1811. 'metric' => $sKey,
  1812. ];
  1813. }
  1814. $aRows = $this->_oDb->getRevenueSeries($aFilter);
  1815. $aSeries = ['gross' => [], 'net' => [], 'refunds' => []];
  1816. foreach($aFilter['buckets'] as $iBucket)
  1817. foreach(array_keys($aSeries) as $sKey)
  1818. $aSeries[$sKey][] = $iBucket > $aFilter['now'] ? null : (isset($aRows[$iBucket]) ? round((float)$aRows[$iBucket][$sKey], 2) : 0);
  1819. $aDatasets = [];
  1820. foreach($aSeries as $sKey => $aValues)
  1821. $aDatasets[] = ['key' => $sKey, 'label' => _t('_bx_analytics_metric_revenue_' . $sKey), 'values' => $aValues];
  1822. $aTabs = $this->_aReportTabs['revenue'];
  1823. $sTab = $this->_getTab($aParams, 'revenue', $aTabs);
  1824. $fNet = (float)$aCurrent['net'];
  1825. $aSplit = [];
  1826. foreach($this->_oDb->getRevenueSplit($aFilter, $aTabs[$sTab], 25) as $aRow) {
  1827. $fRowNet = (float)$aRow['net'];
  1828. $aSplit[] = [
  1829. 'label' => $this->getRevenueLabel($aTabs[$sTab], (string)$aRow['value'], $aMoney['payment']),
  1830. 'bar' => $fNet > 0 ? max(0, round(100 * $fRowNet / $fNet, 1)) : 0,
  1831. 'values' => [
  1832. 'net' => round($fRowNet, 2),
  1833. 'share' => $fNet > 0 ? round(100 * $fRowNet / $fNet, 1) : 0,
  1834. 'customers' => (int)$aRow['customers'],
  1835. 'orders' => (int)$aRow['orders'],
  1836. ],
  1837. ];
  1838. }
  1839. return [
  1840. 'money' => $aMoney,
  1841. 'tiles' => $aTiles,
  1842. 'chart' => [
  1843. 'kind' => 'lines',
  1844. 'metric' => 'money',
  1845. 'currency' => $aMoney['code'],
  1846. 'labels' => $aFilter['buckets'],
  1847. 'bucket' => $aFilter['bucket'],
  1848. 'tz' => $aFilter['tz'],
  1849. 'datasets' => $aDatasets,
  1850. ],
  1851. 'split' => [
  1852. 'name' => 'revenue',
  1853. 'caption' => _t('_bx_analytics_txt_revenue_split'),
  1854. 'tab' => $sTab,
  1855. 'tabs' => $this->_getTabs($aTabs, $sTab, '_bx_analytics_tab_revenue_'),
  1856. 'columns' => [
  1857. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_tab_revenue_' . $sTab)],
  1858. ['key' => 'net', 'type' => 'money', 'title' => _t('_bx_analytics_metric_revenue_net')],
  1859. ['key' => 'share', 'type' => 'percent', 'title' => _t('_bx_analytics_txt_share')],
  1860. ['key' => 'customers', 'type' => 'number', 'title' => _t('_bx_analytics_metric_revenue_customers')],
  1861. ['key' => 'orders', 'type' => 'number', 'title' => _t('_bx_analytics_metric_revenue_orders')],
  1862. ],
  1863. 'rows' => $aSplit,
  1864. 'empty' => _t('_bx_analytics_txt_no_revenue'),
  1865. ],
  1866. 'notes' => $this->_getRevenueNotes($aMoney, (int)$aCurrent['unconverted']),
  1867. 'import' => $this->_getImportStatus(),
  1868. ];
  1869. }
  1870. /**
  1871. * Monthly recurring revenue, new, renewed and cancelled subscriptions and churn per
  1872. * bucket, the plans that bring the MRR, and why members cancel.
  1873. */
  1874. public function getSubscriptions(BxAnalyticsFilter $oFilter, $aParams = [])
  1875. {
  1876. $aMoney = $this->getMoney();
  1877. $aFilter = $this->_getRevenueFilter($oFilter->get(), $aMoney);
  1878. $aPrevious = $this->_getRevenueFilter($oFilter->getPrevious(), $aMoney);
  1879. $iNow = (int)$aFilter['now'];
  1880. bx_import('Revenue', $this->_oModule->_aModule);
  1881. // subscriptions alive in either period, read once
  1882. $aRows = $this->_oDb->getSubscriptionRows(array_merge($aFilter, ['from' => $aPrevious['from']]), [
  1883. 'unit_seconds' => BxAnalyticsRevenue::getUnitSeconds(),
  1884. 'unpaid' => BxAnalyticsRevenue::STATUS_UNPAID,
  1885. 'grace' => BxAnalyticsRevenue::UNPAID_GRACE,
  1886. ]);
  1887. list($aCurrent) = $this->_sweepSubscriptions($aRows, [[$aFilter['from'], min($aFilter['to'], $iNow)]]);
  1888. list($aBefore) = $this->_sweepSubscriptions($aRows, [[$aPrevious['from'], min($aPrevious['to'], $iNow)]]);
  1889. $aTiles = [];
  1890. foreach(['mrr' => 'money', 'active' => 'number', 'trials' => 'number', 'new' => 'number', 'cancelled' => 'number', 'churn' => 'percent'] as $sKey => $sType) {
  1891. $sSub = '';
  1892. if($sKey == 'trials' && $aCurrent['trial_mrr'] > 0)
  1893. $sSub = _t('_bx_analytics_txt_sbs_trial_mrr', _t_format_currency($aCurrent['trial_mrr']));
  1894. else if($sKey == 'churn' && $aCurrent['churn'] === null)
  1895. $sSub = _t('_bx_analytics_txt_sbs_churn_no_base');
  1896. $aTiles[] = [
  1897. 'key' => $sKey,
  1898. 'title' => _t('_bx_analytics_metric_sbs_' . $sKey),
  1899. 'value' => $aCurrent[$sKey],
  1900. 'previous' => $aBefore[$sKey],
  1901. 'change' => $aCurrent[$sKey] !== null && $aBefore[$sKey] !== null ? $this->_change($aCurrent[$sKey], $aBefore[$sKey]) : null,
  1902. 'type' => $sType,
  1903. 'metric' => $sKey,
  1904. 'sub' => $sSub,
  1905. ];
  1906. }
  1907. // per bucket, within the period (the first bucket may start before it); renewals paid
  1908. // with credits too, like the subscriptions
  1909. $aRenewals = $this->_oDb->getRevenueSeries($aFilter, null);
  1910. $aBuckets = $aFilter['buckets'];
  1911. $aSpans = [];
  1912. foreach($aBuckets as $i => $iBucket)
  1913. if($iBucket <= $iNow)
  1914. $aSpans[$i] = [max($iBucket, $aFilter['from']), min(isset($aBuckets[$i + 1]) ? $aBuckets[$i + 1] : $aFilter['to'], $iNow)];
  1915. $aSwept = array_combine(array_keys($aSpans), $this->_sweepSubscriptions($aRows, $aSpans));
  1916. $aMrr = $aNew = $aRenewed = $aCancelled = [];
  1917. foreach($aBuckets as $i => $iBucket) {
  1918. if(!isset($aSwept[$i])) {
  1919. $aMrr[] = $aNew[] = $aRenewed[] = $aCancelled[] = null;
  1920. continue;
  1921. }
  1922. $aMrr[] = $aSwept[$i]['mrr'];
  1923. $aNew[] = $aSwept[$i]['new'];
  1924. $aCancelled[] = $aSwept[$i]['cancelled'];
  1925. $aRenewed[] = isset($aRenewals[$iBucket]) ? (int)$aRenewals[$iBucket]['renewals'] : 0;
  1926. }
  1927. // plans paying at the end of the period, by MRR
  1928. $iEnd = min($aFilter['to'], $iNow);
  1929. $aPlans = [];
  1930. foreach($aRows as $aRow) {
  1931. if(!$this->_isPaying($aRow, $iEnd))
  1932. continue;
  1933. $sKey = $aRow['module'] . '|' . (int)$aRow['item_id'];
  1934. if(!isset($aPlans[$sKey]))
  1935. $aPlans[$sKey] = ['module' => $aRow['module'], 'item_id' => (int)$aRow['item_id'], 'active' => 0, 'mrr' => 0];
  1936. $aPlans[$sKey]['active']++;
  1937. $aPlans[$sKey]['mrr'] += (float)$aRow['mrr_default'];
  1938. }
  1939. uasort($aPlans, function($a, $b) { return $b['mrr'] <=> $a['mrr']; });
  1940. $aPlanRows = [];
  1941. foreach(array_slice($aPlans, 0, 10) as $aPlan) {
  1942. $aItem = $this->_getCartItem($aPlan['module'], $aPlan['item_id']);
  1943. $aPlanRows[] = [
  1944. 'label' => $aItem['title'],
  1945. 'sub' => $aItem['module_title'],
  1946. 'link' => $aItem['url'],
  1947. 'bar' => $aCurrent['mrr'] > 0 ? round(100 * $aPlan['mrr'] / $aCurrent['mrr'], 1) : 0,
  1948. 'values' => ['active' => $aPlan['active'], 'mrr' => round($aPlan['mrr'], 2)],
  1949. ];
  1950. }
  1951. $aReasons = $this->_oDb->getSubscriptionReasons($aFilter, 10);
  1952. $iReasons = array_sum($aReasons);
  1953. $aReasonRows = [];
  1954. foreach($aReasons as $sReason => $iCount)
  1955. $aReasonRows[] = [
  1956. 'label' => $sReason !== '' && $sReason !== 'cancel' ? $sReason : _t('_bx_analytics_txt_sbs_no_reason'),
  1957. 'bar' => $iReasons ? round(100 * $iCount / $iReasons, 1) : 0,
  1958. 'values' => ['count' => (int)$iCount, 'share' => $iReasons ? round(100 * $iCount / $iReasons, 1) : 0],
  1959. ];
  1960. $aLabels = $aFilter['buckets'];
  1961. return [
  1962. 'money' => $aMoney,
  1963. 'tiles' => $aTiles,
  1964. 'chart' => [
  1965. 'kind' => 'lines',
  1966. 'metric' => 'money',
  1967. 'currency' => $aMoney['code'],
  1968. 'labels' => $aLabels,
  1969. 'bucket' => $aFilter['bucket'],
  1970. 'tz' => $aFilter['tz'],
  1971. 'datasets' => [['key' => 'mrr', 'label' => _t('_bx_analytics_metric_sbs_mrr'), 'values' => $aMrr]],
  1972. ],
  1973. 'movement' => [
  1974. 'kind' => 'stacked',
  1975. 'metric' => 'number',
  1976. 'labels' => $aLabels,
  1977. 'bucket' => $aFilter['bucket'],
  1978. 'tz' => $aFilter['tz'],
  1979. 'datasets' => [
  1980. ['key' => 'new', 'label' => _t('_bx_analytics_metric_sbs_new'), 'values' => $aNew],
  1981. ['key' => 'renewed', 'label' => _t('_bx_analytics_metric_sbs_renewed'), 'values' => $aRenewed],
  1982. ['key' => 'cancelled', 'label' => _t('_bx_analytics_metric_sbs_cancelled'), 'values' => $aCancelled],
  1983. ],
  1984. ],
  1985. 'plans' => [
  1986. 'name' => 'sbs_plans',
  1987. 'caption' => _t('_bx_analytics_txt_sbs_plans'),
  1988. 'columns' => [
  1989. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_txt_sbs_plan')],
  1990. ['key' => 'active', 'type' => 'number', 'title' => _t('_bx_analytics_metric_sbs_active')],
  1991. ['key' => 'mrr', 'type' => 'money', 'title' => _t('_bx_analytics_metric_sbs_mrr')],
  1992. ],
  1993. 'rows' => $aPlanRows,
  1994. 'empty' => _t('_bx_analytics_txt_no_subscriptions'),
  1995. ],
  1996. 'reasons' => [
  1997. 'name' => 'sbs_reasons',
  1998. 'caption' => _t('_bx_analytics_txt_sbs_reasons'),
  1999. 'columns' => [
  2000. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_txt_sbs_reason')],
  2001. ['key' => 'count', 'type' => 'number', 'title' => _t('_bx_analytics_metric_sbs_cancelled')],
  2002. ['key' => 'share', 'type' => 'percent', 'title' => _t('_bx_analytics_txt_share')],
  2003. ],
  2004. 'rows' => $aReasonRows,
  2005. 'empty' => _t('_bx_analytics_txt_no_cancellations'),
  2006. ],
  2007. 'notes' => $this->_getRevenueNotes($aMoney, 0),
  2008. ];
  2009. }
  2010. /**
  2011. * The items that sell best, the sellers of a multi-vendor site, the site's own sales
  2012. * against its vendors' and the commissions they paid, and commission invoices.
  2013. */
  2014. public function getSales(BxAnalyticsFilter $oFilter, $aParams = [])
  2015. {
  2016. $aMoney = $this->getMoney();
  2017. $aFilter = $this->_getRevenueFilter($oFilter->get(), $aMoney);
  2018. $aTotals = $this->_oDb->getRevenueTotals($aFilter);
  2019. $fNet = (float)$aTotals['net'];
  2020. $aItems = [];
  2021. foreach($this->_oDb->getRevenueTopItems($aFilter, 10) as $aRow) {
  2022. $aItem = $this->_getCartItem($aRow['module'], (int)$aRow['item_id']);
  2023. $aItems[] = [
  2024. 'label' => $aItem['title'],
  2025. 'sub' => $aItem['module_title'],
  2026. 'link' => $aItem['url'],
  2027. 'bar' => $fNet > 0 ? max(0, round(100 * $aRow['net'] / $fNet, 1)) : 0,
  2028. 'values' => [
  2029. 'net' => round((float)$aRow['net'], 2),
  2030. 'units' => (int)$aRow['units'],
  2031. 'orders' => (int)$aRow['orders'],
  2032. 'renewals' => (int)$aRow['renewals'],
  2033. ],
  2034. ];
  2035. }
  2036. $aSellers = [];
  2037. foreach($this->_oDb->getRevenueSplit($aFilter, 'seller_id', 10) as $aRow) {
  2038. $aMember = $this->getMember((int)$aRow['value']);
  2039. $aSellers[] = [
  2040. 'label' => $aMember ? $aMember['name'] : _t('_bx_analytics_txt_deleted_member'),
  2041. 'link' => $aMember ? $aMember['url'] : '',
  2042. 'bar' => $fNet > 0 ? max(0, round(100 * $aRow['net'] / $fNet, 1)) : 0,
  2043. 'values' => [
  2044. 'net' => round((float)$aRow['net'], 2),
  2045. 'orders' => (int)$aRow['orders'],
  2046. 'customers' => (int)$aRow['customers'],
  2047. ],
  2048. ];
  2049. }
  2050. $iSiteAdmin = (int)BxDolPayments::getInstance()->getOption('site_admin');
  2051. $aSplit = $this->_oDb->getRevenueSellers($aFilter, $iSiteAdmin);
  2052. $aTiles = [
  2053. ['title' => _t('_bx_analytics_metric_revenue_net'), 'value' => round($fNet, 2), 'type' => 'money'],
  2054. ];
  2055. if($iSiteAdmin)
  2056. $aTiles[] = ['title' => _t('_bx_analytics_metric_sales_site'), 'value' => round((float)$aSplit['site'], 2), 'type' => 'money'];
  2057. $aTiles[] = ['title' => _t('_bx_analytics_metric_sales_vendors'), 'value' => round((float)$aSplit['vendors'], 2), 'type' => 'money', 'sub' => _t('_bx_analytics_txt_sales_vendor_count', (int)$aSplit['vendor_count'])];
  2058. $aTiles[] = ['title' => _t('_bx_analytics_metric_sales_commissions'), 'value' => round((float)$aSplit['commissions'], 2), 'type' => 'money'];
  2059. $aInvoices = [];
  2060. foreach($this->getRevenueSources(['from' => $aFilter['from'], 'to' => $aFilter['to']], 'money') as $aSource)
  2061. foreach($aSource['metrics'] as $aMetric)
  2062. $aInvoices[] = [
  2063. 'title' => _t($aMetric['title']),
  2064. 'value' => isset($aMetric['value']) ? $aMetric['value'] : 0,
  2065. 'type' => 'number',
  2066. 'sub' => isset($aMetric['money']) && $aMetric['money'] !== null ? _t_format_currency($aMetric['money']) : '',
  2067. ];
  2068. return [
  2069. 'money' => $aMoney,
  2070. 'tiles' => $aTiles,
  2071. 'items' => [
  2072. 'name' => 'sales_items',
  2073. 'caption' => _t('_bx_analytics_txt_sales_items'),
  2074. 'columns' => [
  2075. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_txt_item')],
  2076. ['key' => 'net', 'type' => 'money', 'title' => _t('_bx_analytics_metric_revenue_net')],
  2077. ['key' => 'units', 'type' => 'number', 'title' => _t('_bx_analytics_metric_sales_units')],
  2078. ['key' => 'renewals', 'type' => 'number', 'title' => _t('_bx_analytics_metric_sbs_renewed')],
  2079. ],
  2080. 'rows' => $aItems,
  2081. 'empty' => _t('_bx_analytics_txt_no_revenue'),
  2082. ],
  2083. 'sellers' => [
  2084. 'name' => 'sales_sellers',
  2085. 'caption' => _t('_bx_analytics_txt_sales_sellers'),
  2086. 'columns' => [
  2087. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_txt_sales_seller')],
  2088. ['key' => 'net', 'type' => 'money', 'title' => _t('_bx_analytics_metric_revenue_net')],
  2089. ['key' => 'orders', 'type' => 'number', 'title' => _t('_bx_analytics_metric_revenue_orders')],
  2090. ['key' => 'customers', 'type' => 'number', 'title' => _t('_bx_analytics_metric_revenue_customers')],
  2091. ],
  2092. 'rows' => $aSellers,
  2093. 'empty' => _t('_bx_analytics_txt_no_revenue'),
  2094. ],
  2095. 'invoices' => $aInvoices,
  2096. 'notes' => $this->_getRevenueNotes($aMoney, (int)$aTotals['unconverted']),
  2097. ];
  2098. }
  2099. /**
  2100. * The credits economy as the modules that keep credits report it: bought, spent, granted,
  2101. * sent and withdrawn, the top spenders and the credits members hold; and from the ledger,
  2102. * what credits paid for, by module, valued in money. Those modules don't know the
  2103. * platform, so the whole block is for every platform, the ledger's figures too.
  2104. */
  2105. public function getCredits(BxAnalyticsFilter $oFilter, $aParams = [])
  2106. {
  2107. $aMoney = $this->getMoney();
  2108. $aFilter = $this->_getRevenueFilter($oFilter->get(), $aMoney);
  2109. $bPlatform = $aFilter['platform'] !== '';
  2110. $aFilter['platform'] = '';
  2111. $aSources = $this->getRevenueSources([
  2112. 'from' => $aFilter['from'],
  2113. 'to' => $aFilter['to'],
  2114. 'starts' => $aFilter['buckets'],
  2115. 'limit' => 10,
  2116. ], 'credits');
  2117. $aSource = reset($aSources);
  2118. $sSource = (string)key($aSources);
  2119. $aTiles = $aDatasets = $aSpenders = [];
  2120. if($aSource) {
  2121. foreach($aSource['metrics'] as $sKey => $aMetric) {
  2122. $aTiles[] = [
  2123. 'key' => $sKey,
  2124. 'title' => _t($aMetric['title']),
  2125. 'value' => (float)$aMetric['value'],
  2126. 'type' => 'credits',
  2127. 'sub' => isset($aMetric['money']) && $aMetric['money'] !== null && (float)$aMetric['value'] > 0 ? _t_format_currency($aMetric['money']) : '',
  2128. ];
  2129. if(in_array($sKey, ['bought', 'spent', 'granted', 'withdrawn'], true))
  2130. $aDatasets[] = [
  2131. 'key' => $sKey,
  2132. 'label' => _t($aMetric['title']),
  2133. 'values' => array_map(function($iBucket) use ($aMetric, $aFilter) {
  2134. return $iBucket > $aFilter['now'] ? null : (isset($aMetric['series'][$iBucket]) ? (float)$aMetric['series'][$iBucket] : 0);
  2135. }, $aFilter['buckets']),
  2136. ];
  2137. }
  2138. if(!empty($aSource['snapshot']['balance'])) {
  2139. $aBalance = $aSource['snapshot']['balance'];
  2140. $aTiles[] = [
  2141. 'key' => 'balance',
  2142. 'title' => _t($aBalance['title']),
  2143. 'value' => (float)$aBalance['value'],
  2144. 'type' => 'credits',
  2145. 'sub' => _t('_bx_analytics_txt_credits_liability', _t_format_currency(isset($aBalance['money_default']) ? $aBalance['money_default'] : 0)),
  2146. ];
  2147. }
  2148. if(!empty($aSource['lists']['top_spenders']['rows'])) {
  2149. $aList = $aSource['lists']['top_spenders']['rows'];
  2150. $fMax = 0;
  2151. foreach($aList as $aRow)
  2152. $fMax = max($fMax, (float)$aRow['value']);
  2153. foreach($aList as $aRow) {
  2154. $aMember = $this->getMember(isset($aRow['profile_id']) ? (int)$aRow['profile_id'] : 0);
  2155. $aSpenders[] = [
  2156. 'label' => $aMember ? $aMember['name'] : (isset($aRow['label']) ? (string)$aRow['label'] : _t('_bx_analytics_txt_deleted_member')),
  2157. 'link' => $aMember ? $aMember['url'] : '',
  2158. 'bar' => $fMax > 0 ? round(100 * $aRow['value'] / $fMax, 1) : 0,
  2159. 'values' => ['value' => (float)$aRow['value']],
  2160. ];
  2161. }
  2162. }
  2163. }
  2164. // what orders paid with credits bought, valued in money
  2165. $aSpentRows = [];
  2166. $aSpent = $this->_oDb->getRevenueSplit($aFilter, 'module', 10, 0);
  2167. $fSpent = 0;
  2168. foreach($aSpent as $aRow)
  2169. $fSpent += max(0, (float)$aRow['net']);
  2170. foreach($aSpent as $aRow)
  2171. $aSpentRows[] = [
  2172. 'label' => $this->getRevenueLabel('module', (string)$aRow['value'], $aMoney['payment']),
  2173. 'bar' => $fSpent > 0 ? max(0, round(100 * $aRow['net'] / $fSpent, 1)) : 0,
  2174. 'values' => ['net' => round((float)$aRow['net'], 2), 'orders' => (int)$aRow['orders']],
  2175. ];
  2176. // credit bundles sold for money
  2177. $fBundles = 0;
  2178. if($sSource !== '')
  2179. foreach($this->_oDb->getRevenueSplit($aFilter, 'module', 1000) as $aRow)
  2180. if($aRow['value'] === $sSource)
  2181. $fBundles = (float)$aRow['net'];
  2182. if($aSource)
  2183. $aTiles[] = ['key' => 'bundles', 'title' => _t('_bx_analytics_metric_credits_bundles'), 'value' => round($fBundles, 2), 'type' => 'money'];
  2184. $aNotes = [];
  2185. if($aMoney['credits_only'])
  2186. $aNotes[] = _t('_bx_analytics_txt_revenue_credits_only');
  2187. if($aSource && $bPlatform)
  2188. $aNotes[] = _t('_bx_analytics_txt_credits_all_platforms');
  2189. // checkouts the ledger doesn't have: paid before it started, or not imported yet
  2190. if(empty($aSpentRows) && !empty($aSource['metrics']['spent']['value']) && (float)$aSource['metrics']['spent']['value'] > 0)
  2191. $aNotes[] = _t('_bx_analytics_txt_credits_not_in_ledger');
  2192. return [
  2193. 'money' => $aMoney,
  2194. 'available' => !empty($aSource),
  2195. 'title' => $aSource ? _t($aSource['title']) : '',
  2196. 'tiles' => $aTiles,
  2197. 'chart' => [
  2198. 'kind' => 'lines',
  2199. 'metric' => 'number',
  2200. 'labels' => $aFilter['buckets'],
  2201. 'bucket' => $aFilter['bucket'],
  2202. 'tz' => $aFilter['tz'],
  2203. 'datasets' => $aDatasets,
  2204. ],
  2205. 'spenders' => [
  2206. 'name' => 'credits_spenders',
  2207. 'caption' => _t('_bx_analytics_txt_credits_spenders'),
  2208. 'columns' => [
  2209. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_txt_member_name')],
  2210. ['key' => 'value', 'type' => 'credits', 'title' => _t('_bx_analytics_txt_credits_spent')],
  2211. ],
  2212. 'rows' => $aSpenders,
  2213. 'empty' => _t('_bx_analytics_txt_no_credits'),
  2214. ],
  2215. 'spent' => [
  2216. 'name' => 'credits_spent',
  2217. 'caption' => _t('_bx_analytics_txt_credits_spent_on'),
  2218. 'columns' => [
  2219. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_tab_revenue_sources')],
  2220. ['key' => 'net', 'type' => 'money', 'title' => _t('_bx_analytics_txt_credits_value')],
  2221. ['key' => 'orders', 'type' => 'number', 'title' => _t('_bx_analytics_metric_revenue_orders')],
  2222. ],
  2223. 'rows' => $aSpentRows,
  2224. 'empty' => _t('_bx_analytics_txt_no_credit_orders'),
  2225. ],
  2226. 'notes' => $aNotes,
  2227. ];
  2228. }
  2229. /**
  2230. * The site's default currency and the payment module the ledger is read for.
  2231. */
  2232. public function getMoney()
  2233. {
  2234. $oPayments = BxDolPayments::getInstance();
  2235. $bActive = $oPayments->isActive();
  2236. return [
  2237. 'active' => $bActive,
  2238. 'payment' => $bActive ? (string)$oPayments->getActive() : '',
  2239. 'code' => $bActive ? (string)$oPayments->getCurrencyCode() : '',
  2240. 'credits_only' => $bActive && (bool)$oPayments->isCreditsOnly(),
  2241. ];
  2242. }
  2243. /**
  2244. * What modules report about their own economy through the get_analytics_revenue service,
  2245. * found in every installed module: [module name => report], the ones of a unit only. The
  2246. * unit is read first (meta), so a module of another unit doesn't compute its report.
  2247. *
  2248. * @param array $aParams from, to, starts, limit; or meta to learn titles and units only
  2249. * @param string $sUnit money or credits, '' for any
  2250. */
  2251. public function getRevenueSources($aParams, $sUnit = '')
  2252. {
  2253. $aResult = [];
  2254. foreach(BxDolModuleQuery::getInstance()->getModulesBy(['type' => 'modules', 'active' => 1]) as $aModule) {
  2255. if(empty($aModule['name']) || !bx_is_srv($aModule['name'], 'get_analytics_revenue'))
  2256. continue;
  2257. if($sUnit !== '' && empty($aParams['meta'])) {
  2258. $aMeta = bx_srv($aModule['name'], 'get_analytics_revenue', [['meta' => true]]);
  2259. if(empty($aMeta['unit']) || $aMeta['unit'] != $sUnit)
  2260. continue;
  2261. }
  2262. $aReport = bx_srv($aModule['name'], 'get_analytics_revenue', [$aParams]);
  2263. if(empty($aReport) || !is_array($aReport) || empty($aReport['unit']) || ($sUnit !== '' && $aReport['unit'] != $sUnit))
  2264. continue;
  2265. if(empty($aParams['meta']) && (empty($aReport['metrics']) || !is_array($aReport['metrics'])))
  2266. continue;
  2267. $aResult[$aModule['name']] = $aReport;
  2268. }
  2269. return $aResult;
  2270. }
  2271. /**
  2272. * A human name for a ledger value: a module's title, a provider, a platform, a channel.
  2273. */
  2274. public function getRevenueLabel($sDimension, $sValue, $sPayment = '')
  2275. {
  2276. switch($sDimension) {
  2277. case 'module':
  2278. if($sValue === '')
  2279. return _t('_bx_analytics_txt_unknown');
  2280. if($sPayment !== '' && $sValue === $sPayment)
  2281. return _t('_bx_analytics_txt_revenue_commissions');
  2282. return $this->_getModuleTitle($sValue);
  2283. case 'provider':
  2284. if($sValue === '')
  2285. return _t('_bx_analytics_txt_unknown');
  2286. $sValue = str_replace('_', ' ', $sValue);
  2287. return mb_strtoupper(get_mb_substr($sValue, 0, 1)) . get_mb_substr($sValue, 1, get_mb_len($sValue) - 1);
  2288. case 'platform':
  2289. return $sValue !== '' ? $this->getLabel('platform', $sValue) : _t('_bx_analytics_txt_revenue_imported');
  2290. case 'first_channel':
  2291. case 'last_channel':
  2292. return $sValue !== '' ? $this->getLabel('channel', $sValue) : _t('_bx_analytics_txt_revenue_untracked');
  2293. case 'last_utm_campaign':
  2294. case 'first_utm_campaign':
  2295. return $sValue !== '' ? $sValue : _t('_bx_analytics_txt_revenue_no_campaign');
  2296. }
  2297. return $sValue;
  2298. }
  2299. /**
  2300. * Net revenue of members over all the ledger, for the Users report: [profile id => amount].
  2301. */
  2302. public function getLifetimeValues($aProfileIds)
  2303. {
  2304. $aMoney = $this->getMoney();
  2305. if($aMoney['payment'] === '')
  2306. return [];
  2307. return $this->_oDb->getLifetimeValues($aProfileIds, $aMoney['payment']);
  2308. }
  2309. /**
  2310. * Subscription figures over spans of time, sorted and not overlapping. For each span: MRR
  2311. * and paying subscriptions at its end, trials at its end, new and ended (cancelled, or
  2312. * unpaid past the grace time) paying subscriptions in it, and churn (of those paying at
  2313. * its start, the share that ended in it). One pass over the rows, each finding its spans
  2314. * by binary search.
  2315. *
  2316. * @param array $aSpans [[from, to], ...]
  2317. * @return array a result per span, in the order of $aSpans
  2318. */
  2319. protected function _sweepSubscriptions($aRows, $aSpans)
  2320. {
  2321. $aSpans = array_values($aSpans);
  2322. $iSpans = count($aSpans);
  2323. $aFroms = array_map('intval', array_column($aSpans, 0));
  2324. $aTos = array_map('intval', array_column($aSpans, 1));
  2325. // counts at the spans' ends (or starts) are ranges of spans: added as differences
  2326. $aDiff = array_fill_keys(['mrr', 'active', 'trials', 'trial_mrr', 'at_start'], array_fill(0, $iSpans + 1, 0));
  2327. $aNew = $aEnded = $aChurned = array_fill(0, $iSpans, 0);
  2328. foreach($aRows as $aRow) {
  2329. $iFirst = (int)$aRow['first_paid'];
  2330. $iEnd = $this->_getSubscriptionEnd($aRow);
  2331. $fMrr = (float)$aRow['mrr_default'];
  2332. if($iFirst) {
  2333. $this->_addSpans($aDiff['mrr'], $aTos, $iFirst, $iEnd, $fMrr);
  2334. $this->_addSpans($aDiff['active'], $aTos, $iFirst, $iEnd, 1);
  2335. $this->_addSpans($aDiff['at_start'], $aFroms, $iFirst, $iEnd, 1);
  2336. if(($i = $this->_findSpan($aFroms, $aTos, $iFirst)) !== null)
  2337. $aNew[$i]++;
  2338. if($iEnd && ($i = $this->_findSpan($aFroms, $aTos, $iEnd)) !== null) {
  2339. $aEnded[$i]++;
  2340. if($iFirst < $aFroms[$i])
  2341. $aChurned[$i]++;
  2342. }
  2343. }
  2344. // in trial from its start until the trial, its first charge or its end is over
  2345. $iTrialEnd = $this->_getTrialEnd($aRow);
  2346. if($iTrialEnd !== null) {
  2347. $iUntil = 0;
  2348. foreach([$iTrialEnd, $iFirst, $iEnd] as $iTime)
  2349. if($iTime)
  2350. $iUntil = $iUntil ? min($iUntil, $iTime) : $iTime;
  2351. $this->_addSpans($aDiff['trials'], $aTos, (int)$aRow['started'], $iUntil, 1);
  2352. $this->_addSpans($aDiff['trial_mrr'], $aTos, (int)$aRow['started'], $iUntil, $fMrr);
  2353. }
  2354. }
  2355. $aResults = [];
  2356. $aSum = array_fill_keys(array_keys($aDiff), 0);
  2357. for($i = 0; $i < $iSpans; $i++) {
  2358. foreach($aDiff as $sKey => $aValues)
  2359. $aSum[$sKey] += $aValues[$i];
  2360. if($aTos[$i] <= $aFroms[$i]) {
  2361. $aResults[] = ['mrr' => null, 'active' => null, 'trials' => null, 'trial_mrr' => 0, 'new' => null, 'cancelled' => null, 'churn' => null];
  2362. continue;
  2363. }
  2364. $aResults[] = [
  2365. 'mrr' => round($aSum['mrr'], 2),
  2366. 'active' => (int)$aSum['active'],
  2367. 'trials' => (int)$aSum['trials'],
  2368. 'trial_mrr' => round($aSum['trial_mrr'], 2),
  2369. 'new' => $aNew[$i],
  2370. 'cancelled' => $aEnded[$i],
  2371. 'churn' => $aSum['at_start'] ? round(100 * $aChurned[$i] / $aSum['at_start'], 1) : null,
  2372. ];
  2373. }
  2374. return $aResults;
  2375. }
  2376. /**
  2377. * Add a value to the spans whose edge (from `$aEdges`, ascending) is in (`$iAfter`,
  2378. * `$iUntil`], `$iUntil` 0 for no end.
  2379. */
  2380. protected function _addSpans(&$aDiff, $aEdges, $iAfter, $iUntil, $mixedValue)
  2381. {
  2382. $iLo = self::_firstAbove($aEdges, $iAfter);
  2383. $iHi = $iUntil ? self::_firstAbove($aEdges, $iUntil) : count($aEdges);
  2384. if($iLo >= $iHi)
  2385. return;
  2386. $aDiff[$iLo] += $mixedValue;
  2387. $aDiff[$iHi] -= $mixedValue;
  2388. }
  2389. /**
  2390. * The span a time falls in, or null.
  2391. */
  2392. protected function _findSpan($aFroms, $aTos, $iTime)
  2393. {
  2394. $i = self::_firstAbove($aFroms, $iTime) - 1;
  2395. return $i >= 0 && $iTime < $aTos[$i] ? $i : null;
  2396. }
  2397. /**
  2398. * Index of the first value greater than `$iTime` in an ascending list, or its length.
  2399. */
  2400. protected static function _firstAbove($aList, $iTime)
  2401. {
  2402. $iLo = 0;
  2403. $iHi = count($aList);
  2404. while($iLo < $iHi) {
  2405. $iMid = ($iLo + $iHi) >> 1;
  2406. if($aList[$iMid] > $iTime)
  2407. $iHi = $iMid;
  2408. else
  2409. $iLo = $iMid + 1;
  2410. }
  2411. return $iLo;
  2412. }
  2413. /**
  2414. * A subscription was paying at a time: first paid before it and not ended by then.
  2415. */
  2416. protected function _isPaying($aRow, $iTime)
  2417. {
  2418. $iFirst = (int)$aRow['first_paid'];
  2419. $iEnd = $this->_getSubscriptionEnd($aRow);
  2420. return $iFirst && $iFirst < $iTime && (!$iEnd || $iEnd >= $iTime);
  2421. }
  2422. /**
  2423. * When a subscription stopped paying, or 0: its cancellation, or for an unpaid one the end
  2424. * of its last paid period and the grace time (a failed charge the payment module stopped
  2425. * retrying never cancels it).
  2426. */
  2427. protected function _getSubscriptionEnd($aRow)
  2428. {
  2429. if((int)$aRow['canceled'])
  2430. return (int)$aRow['canceled'];
  2431. if($aRow['status'] != BxAnalyticsRevenue::STATUS_UNPAID || !(int)$aRow['last_paid'])
  2432. return 0;
  2433. return (int)$aRow['last_paid'] + (int)BxAnalyticsRevenue::getPeriodSeconds($aRow['period'], $aRow['period_unit']) + BxAnalyticsRevenue::UNPAID_GRACE;
  2434. }
  2435. /**
  2436. * When a subscription's trial ends (0: a trial of unknown length), or null without a trial.
  2437. */
  2438. protected function _getTrialEnd($aRow)
  2439. {
  2440. if((int)$aRow['trial'] > 0)
  2441. return (int)$aRow['started'] + 86400 * (int)$aRow['trial'];
  2442. return $aRow['status'] == BxAnalyticsRevenue::STATUS_TRIAL ? 0 : null;
  2443. }
  2444. /**
  2445. * Title, link and module of something sold, from its module's get_cart_item.
  2446. */
  2447. protected function _getCartItem($sModule, $iItemId)
  2448. {
  2449. $aResult = [
  2450. 'title' => _t('_bx_analytics_txt_item_n', (int)$iItemId),
  2451. 'url' => '',
  2452. 'module_title' => $sModule !== '' ? $this->_getModuleTitle($sModule) : '',
  2453. ];
  2454. if($sModule === '' || !BxDolModuleQuery::getInstance()->isEnabledByName($sModule) || !bx_is_srv($sModule, 'get_cart_item'))
  2455. return $aResult;
  2456. $aItem = bx_srv($sModule, 'get_cart_item', [(int)$iItemId]);
  2457. if(!empty($aItem) && is_array($aItem)) {
  2458. if(!empty($aItem['title']) && is_string($aItem['title']))
  2459. $aResult['title'] = strip_tags($aItem['title']);
  2460. if(!empty($aItem['url']) && is_string($aItem['url']))
  2461. $aResult['url'] = $aItem['url'];
  2462. }
  2463. return $aResult;
  2464. }
  2465. protected function _getRevenueFilter($aFilter, $aMoney)
  2466. {
  2467. $aFilter['payment'] = $aMoney['payment'];
  2468. return $aFilter;
  2469. }
  2470. protected function _getRevenueNotes($aMoney, $iUnconverted)
  2471. {
  2472. $aNotes = [];
  2473. if(!$aMoney['active'])
  2474. $aNotes[] = _t('_bx_analytics_txt_revenue_no_payment');
  2475. if($aMoney['credits_only'])
  2476. $aNotes[] = _t('_bx_analytics_txt_revenue_credits_only');
  2477. if($iUnconverted > 0)
  2478. $aNotes[] = _t('_bx_analytics_txt_revenue_no_rate', $iUnconverted);
  2479. // no currency without a payment module: the tab still shows, for other revenue sources to come
  2480. if($aMoney['code'] !== '')
  2481. $aNotes[] = _t('_bx_analytics_txt_revenue_as_recorded', $aMoney['code']);
  2482. return $aNotes;
  2483. }
  2484. /**
  2485. * Where the import of past payments stands, for administrators.
  2486. */
  2487. protected function _getImportStatus()
  2488. {
  2489. if(!isAdmin())
  2490. return [];
  2491. $aStatus = $this->_oModule->getRevenue()->getBackfillStatus(true);
  2492. return $aStatus['state'] == 'unavailable' ? [] : $aStatus;
  2493. }
  2494. /**
  2495. * Name, link and picture of a profile, or null when it no longer exists.
  2496. */
  2497. public function getMember($iProfileId)
  2498. {
  2499. if(!$iProfileId)
  2500. return null;
  2501. $oProfile = BxDolProfile::getInstance($iProfileId);
  2502. if(!$oProfile)
  2503. return null;
  2504. return [
  2505. 'id' => (int)$iProfileId,
  2506. 'name' => $oProfile->getDisplayName(),
  2507. 'url' => $oProfile->getUrl(),
  2508. 'thumb' => $oProfile->getThumb(),
  2509. ];
  2510. }
  2511. /**
  2512. * A readable name for an event: a site action with the module it happened in, or a
  2513. * custom event's own name.
  2514. */
  2515. public function getEventLabel($sType, $sName, $sModule)
  2516. {
  2517. $sModuleTitle = $sModule !== '' ? $this->_getModuleTitle($sModule) : '';
  2518. if($sType == BX_ANALYTICS_HIT_UNA) {
  2519. // an action without a title of its own (a module's, or a newer one) by its name
  2520. $sKey = '_bx_analytics_action_' . $sName;
  2521. $sLabel = _t($sKey);
  2522. if($sLabel === $sKey)
  2523. $sLabel = $sName;
  2524. return $sModuleTitle !== '' && $sModule != 'account' ? $sLabel . ' · ' . $sModuleTitle : $sLabel;
  2525. }
  2526. return $sModuleTitle !== '' ? $sName . ' · ' . $sModuleTitle : $sName;
  2527. }
  2528. /**
  2529. * Title of the module an object system belongs to: bx_posts and bx_posts_reactions are both Posts.
  2530. */
  2531. protected function _getModuleTitle($sSystem)
  2532. {
  2533. if($this->_aModuleTitles === null) {
  2534. $this->_aModuleTitles = [];
  2535. foreach(BxDolModuleQuery::getInstance()->getModulesBy(['type' => 'modules', 'active' => 1]) as $aModule)
  2536. $this->_aModuleTitles[$aModule['name']] = $aModule['title'];
  2537. uksort($this->_aModuleTitles, function($a, $b) { return get_mb_len($b) - get_mb_len($a); });
  2538. }
  2539. foreach($this->_aModuleTitles as $sName => $sTitle)
  2540. if($sSystem === $sName || bx_mb_strpos($sSystem, $sName . '_') === 0)
  2541. return $sTitle;
  2542. // core objects, like the friends and follows connections, have their own names
  2543. $sKey = '_bx_analytics_object_' . $sSystem;
  2544. $sTitle = _t($sKey);
  2545. return $sTitle !== $sKey ? $sTitle : $sSystem;
  2546. }
  2547. protected function _parseEvent($sValue)
  2548. {
  2549. $aParts = explode('|', (string)$sValue);
  2550. if(count($aParts) != 3 || !in_array($aParts[0], [BX_ANALYTICS_HIT_EVENT, BX_ANALYTICS_HIT_UNA], true) || $aParts[1] === '')
  2551. return null;
  2552. return ['type' => $aParts[0], 'name' => $aParts[1], 'module' => $aParts[2]];
  2553. }
  2554. protected function _getLocation($aSession)
  2555. {
  2556. $aParts = array_filter([$aSession['city'], $aSession['country'] !== '' ? $this->getLabel('country', $aSession['country']) : '']);
  2557. return !empty($aParts) ? implode(', ', $aParts) : _t('_bx_analytics_txt_unknown');
  2558. }
  2559. protected function _getDevice($aSession)
  2560. {
  2561. $aParts = array_filter([
  2562. $aSession['device'] !== '' ? $this->getLabel('device', $aSession['device']) : '',
  2563. $aSession['browser'],
  2564. trim($aSession['os'] . ' ' . $aSession['os_version']),
  2565. ]);
  2566. return implode(' · ', $aParts);
  2567. }
  2568. protected function _getPlatformsText($sPlatforms)
  2569. {
  2570. $aLabels = [];
  2571. foreach(explode(',', (string)$sPlatforms) as $sPlatform)
  2572. if($sPlatform !== '')
  2573. $aLabels[] = $this->getLabel('platform', $sPlatform);
  2574. return implode(', ', $aLabels);
  2575. }
  2576. /**
  2577. * A human name for a stored dimension value.
  2578. */
  2579. public function getLabel($sDimension, $sValue)
  2580. {
  2581. if($sValue === '')
  2582. return _t($sDimension == 'referrer_host' || $sDimension == 'referrer' ? '_bx_analytics_txt_direct' : '_bx_analytics_txt_unknown');
  2583. switch($sDimension) {
  2584. case 'platform':
  2585. $aPlatforms = $this->_oConfig->getPlatforms();
  2586. return isset($aPlatforms[$sValue]) ? _t($aPlatforms[$sValue]) : $sValue;
  2587. case 'channel':
  2588. return _t('_bx_analytics_channel_' . $sValue);
  2589. case 'device':
  2590. return _t('_bx_analytics_device_' . $sValue);
  2591. case 'country':
  2592. if($this->_aCountries === null)
  2593. $this->_aCountries = BxDolFormQuery::getDataItems('Country');
  2594. return isset($this->_aCountries[$sValue]) ? $this->_aCountries[$sValue] : $sValue;
  2595. }
  2596. return $sValue;
  2597. }
  2598. /**
  2599. * Report metrics from summed session columns.
  2600. */
  2601. protected function _getKpis($aRow)
  2602. {
  2603. $iVisitors = isset($aRow['visitors']) ? (int)$aRow['visitors'] : 0;
  2604. $iSessions = isset($aRow['sessions']) ? (int)$aRow['sessions'] : 0;
  2605. $iPageviews = isset($aRow['pageviews']) ? (int)$aRow['pageviews'] : 0;
  2606. $iBounces = isset($aRow['bounces']) ? (int)$aRow['bounces'] : 0;
  2607. $iDuration = isset($aRow['duration']) ? (int)$aRow['duration'] : 0;
  2608. return [
  2609. 'visitors' => $iVisitors,
  2610. 'sessions' => $iSessions,
  2611. 'pageviews' => $iPageviews,
  2612. 'pages_per_session' => $iSessions ? round($iPageviews / $iSessions, 2) : 0,
  2613. 'bounce_rate' => $iSessions ? round(100 * $iBounces / $iSessions, 1) : 0,
  2614. 'duration' => $iSessions ? (int)round($iDuration / $iSessions) : 0,
  2615. ];
  2616. }
  2617. /**
  2618. * The tab a report shows: `tab`, or `tab_<name>` from the page URL, or the first one.
  2619. */
  2620. protected function _getTab($aParams, $sName, $aTabs)
  2621. {
  2622. $sTab = $this->_param($aParams, 'tab', $this->_param($aParams, 'tab_' . $sName, ''));
  2623. return isset($aTabs[$sTab]) ? $sTab : key($aTabs);
  2624. }
  2625. protected function _getTabs($aTabs, $sActive, $sKeyPrefix = '_bx_analytics_tab_')
  2626. {
  2627. $aResult = [];
  2628. foreach(array_keys($aTabs) as $sName)
  2629. $aResult[] = ['name' => $sName, 'title' => _t($sKeyPrefix . $sName), 'active' => $sName == $sActive];
  2630. return $aResult;
  2631. }
  2632. /**
  2633. * A scalar request parameter, or the default.
  2634. */
  2635. protected function _param($aParams, $sKey, $mixedDefault = '')
  2636. {
  2637. return isset($aParams[$sKey]) && is_scalar($aParams[$sKey]) ? $aParams[$sKey] : $mixedDefault;
  2638. }
  2639. /**
  2640. * Change in percent, or null when there is nothing to compare with.
  2641. */
  2642. protected function _change($fCurrent, $fPrevious)
  2643. {
  2644. if(!$fPrevious)
  2645. return null;
  2646. return round(100 * ($fCurrent - $fPrevious) / $fPrevious, 1);
  2647. }
  2648. }
  2649. /** @} */