BxAnalyticsReports.php 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006
  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, every day of the period
  745. $aDays = [];
  746. for($i = (int)$aFilter['from']; $i < (int)$aFilter['to'] && count($aDays) < 800; $i += 86400)
  747. $aDays[gmdate('Y-m-d', $i)] = true;
  748. $aDays = array_keys($aDays);
  749. $aSeries = [];
  750. foreach($this->_oDb->getBotSeries($aFilter) as $aRow)
  751. if(empty($aIn) || in_array($aRow['category'], $aIn, true))
  752. $aSeries[$aRow['category']][$aRow['day']] = (int)$aRow['hits'];
  753. $aDatasets = [];
  754. foreach($aSeries as $sCategory => $aValues)
  755. $aDatasets[] = [
  756. 'key' => $sCategory,
  757. 'label' => _t('_bx_analytics_bot_' . $sCategory),
  758. 'values' => array_map(function($sDay) use ($aValues) { return isset($aValues[$sDay]) ? $aValues[$sDay] : 0; }, $aDays),
  759. ];
  760. $iTotal = $this->_oDb->getBotListCount($aFilter, $aIn);
  761. $iPages = max(1, (int)ceil($iTotal / $this->_iPerPage));
  762. $iPage = min($iPages, max(1, (int)$this->_param($aParams, 'page_bot_list', 1)));
  763. $aBots = $this->_oDb->getBotList($aFilter, $aIn, $this->_iPerPage, ($iPage - 1) * $this->_iPerPage);
  764. $iMax = 1;
  765. foreach($aBots as $aBot)
  766. $iMax = max($iMax, (int)$aBot['hits']);
  767. $aRows = [];
  768. foreach($aBots as $aBot)
  769. $aRows[] = [
  770. 'label' => $aBot['bot'],
  771. 'sub' => _t('_bx_analytics_bot_' . $aBot['category']),
  772. 'bar' => round(100 * $aBot['hits'] / $iMax, 1),
  773. 'values' => [
  774. 'hits' => (int)$aBot['hits'],
  775. 'last_seen' => (int)$aBot['last_seen'],
  776. 'platforms' => implode(', ', array_map(function($s) { return $this->getLabel('platform', $s); }, explode(',', (string)$aBot['platforms']))),
  777. ],
  778. ];
  779. $aPagesRows = [];
  780. $aPages = $this->_oDb->getBotPages($aFilter, $aIn, 10);
  781. $iMaxPage = 1;
  782. foreach($aPages as $aPage)
  783. $iMaxPage = max($iMaxPage, (int)$aPage['hits']);
  784. foreach($aPages as $aPage)
  785. $aPagesRows[] = [
  786. 'label' => $aPage['path'],
  787. 'bar' => round(100 * $aPage['hits'] / $iMaxPage, 1),
  788. 'values' => ['hits' => (int)$aPage['hits']],
  789. ];
  790. return [
  791. 'tab' => $sTab,
  792. 'tabs' => $this->_getTabs(array_flip(array_keys($aCategories)), $sTab, '_bx_analytics_tab_bots_'),
  793. 'tiles' => $aTiles,
  794. 'chart' => [
  795. 'kind' => 'stacked',
  796. 'labels' => array_map(function($sDay) { return strtotime($sDay . ' 00:00:00 UTC'); }, $aDays),
  797. 'bucket' => 'day',
  798. 'tz' => 'UTC',
  799. 'datasets' => $aDatasets,
  800. ],
  801. 'bots' => [
  802. 'name' => 'bot_list',
  803. 'caption' => _t('_bx_analytics_lmi_cpt_bots'),
  804. 'columns' => [
  805. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_txt_bot')],
  806. ['key' => 'hits', 'type' => 'number', 'title' => _t('_bx_analytics_metric_bot_hits')],
  807. ['key' => 'platforms', 'type' => 'text', 'title' => _t('_bx_analytics_dim_platform')],
  808. ['key' => 'last_seen', 'type' => 'datetime', 'title' => _t('_bx_analytics_txt_last_seen')],
  809. ],
  810. 'rows' => $aRows,
  811. 'page' => $iPage,
  812. 'pages' => $iPages,
  813. 'empty' => _t('_bx_analytics_txt_no_bots'),
  814. ],
  815. 'pages' => [
  816. 'name' => 'bot_pages',
  817. 'caption' => _t('_bx_analytics_txt_bot_pages'),
  818. 'columns' => [
  819. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_tab_pages')],
  820. ['key' => 'hits', 'type' => 'number', 'title' => _t('_bx_analytics_metric_bot_hits')],
  821. ],
  822. 'rows' => $aPagesRows,
  823. 'empty' => _t('_bx_analytics_txt_no_bots'),
  824. ],
  825. ];
  826. }
  827. /**
  828. * Custom events and site actions with their counts and visitors. With `event`
  829. * (type|name|module) one event over time, by platform and by its properties.
  830. * Tabs: all, custom (from trackers), site (UNA actions).
  831. */
  832. public function getEvents(BxAnalyticsFilter $oFilter, $aParams = [])
  833. {
  834. $aEvent = $this->_parseEvent($this->_param($aParams, 'event'));
  835. if($aEvent)
  836. return $this->_getEventDetail($oFilter, $aEvent);
  837. $aTabs = $this->_aReportTabs['events'];
  838. $sTab = $this->_getTab($aParams, 'events', $aTabs);
  839. $aFilter = $oFilter->get();
  840. $iTotal = $this->_oDb->getEventListCount($aFilter, $aTabs[$sTab]);
  841. $iPages = max(1, (int)ceil($iTotal / $this->_iPerPage));
  842. $iPage = min($iPages, max(1, (int)$this->_param($aParams, 'page_events', 1)));
  843. $aEvents = $this->_oDb->getEventList($aFilter, $aTabs[$sTab], $this->_iPerPage, ($iPage - 1) * $this->_iPerPage);
  844. $iMax = 1;
  845. foreach($aEvents as $aRow)
  846. $iMax = max($iMax, (int)$aRow['count']);
  847. $aRows = [];
  848. foreach($aEvents as $aRow)
  849. $aRows[] = [
  850. 'label' => $this->getEventLabel($aRow['type'], $aRow['name'], $aRow['module']),
  851. 'sub' => _t('_bx_analytics_event_type_' . $aRow['type']),
  852. 'open' => ['param' => 'event', 'value' => $aRow['type'] . '|' . $aRow['name'] . '|' . $aRow['module']],
  853. 'bar' => round(100 * $aRow['count'] / $iMax, 1),
  854. 'values' => [
  855. 'count' => (int)$aRow['count'],
  856. 'visitors' => (int)$aRow['visitors'],
  857. 'per_visitor' => $aRow['visitors'] ? round($aRow['count'] / $aRow['visitors'], 1) : 0,
  858. 'last' => (int)$aRow['last'],
  859. ],
  860. ];
  861. return [
  862. 'view' => 'list',
  863. 'name' => 'events',
  864. 'caption' => _t('_bx_analytics_lmi_cpt_events'),
  865. 'tab' => $sTab,
  866. 'tabs' => $this->_getTabs($aTabs, $sTab, '_bx_analytics_tab_events_'),
  867. 'columns' => [
  868. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_txt_event')],
  869. ['key' => 'count', 'type' => 'number', 'title' => _t('_bx_analytics_metric_count')],
  870. ['key' => 'visitors', 'type' => 'number', 'title' => _t('_bx_analytics_metric_visitors')],
  871. ['key' => 'per_visitor', 'type' => 'decimal', 'title' => _t('_bx_analytics_metric_per_visitor')],
  872. ['key' => 'last', 'type' => 'datetime', 'title' => _t('_bx_analytics_txt_last_seen')],
  873. ],
  874. 'rows' => $aRows,
  875. 'page' => $iPage,
  876. 'pages' => $iPages,
  877. 'empty' => _t('_bx_analytics_txt_no_events'),
  878. ];
  879. }
  880. protected function _getEventDetail(BxAnalyticsFilter $oFilter, $aEvent)
  881. {
  882. $aFilter = $oFilter->get();
  883. $aRows = $this->_oDb->getEventSeries($aFilter, $aEvent, $aFilter['bucket'], $aFilter['offset'], $aFilter['starts']);
  884. $aCounts = [];
  885. $aVisitors = [];
  886. foreach($aFilter['buckets'] as $iBucket) {
  887. $aCounts[] = isset($aRows[$iBucket]) ? (int)$aRows[$iBucket]['count'] : 0;
  888. $aVisitors[] = isset($aRows[$iBucket]) ? (int)$aRows[$iBucket]['visitors'] : 0;
  889. }
  890. $aPlatforms = [];
  891. $iTotal = 0;
  892. $iTotalVisitors = 0;
  893. foreach($this->_oDb->getEventPlatforms($aFilter, $aEvent) as $aRow) {
  894. $iTotal += (int)$aRow['count'];
  895. $iTotalVisitors += (int)$aRow['visitors'];
  896. $aPlatforms[] = ['label' => $this->getLabel('platform', $aRow['platform']), 'count' => (int)$aRow['count']];
  897. }
  898. foreach($aPlatforms as $i => $a)
  899. $aPlatforms[$i]['share'] = $iTotal ? round(100 * $a['count'] / $iTotal, 1) : 0;
  900. // the most common values of each property among the latest occurrences; a sale's
  901. // properties only for those who may see revenue (events recorded earlier carry amounts)
  902. $aProps = [];
  903. $bMoney = $aEvent['type'] == BX_ANALYTICS_HIT_UNA && in_array($aEvent['name'], ['purchase', 'renewal', 'trial_start'], true);
  904. foreach($bMoney && $this->_oModule->checkAllowedRevenue() !== CHECK_ACTION_RESULT_ALLOWED ? [] : $this->_oDb->getEventProps($aFilter, $aEvent) as $sJson) {
  905. $a = json_decode($sJson, true);
  906. if(!is_array($a))
  907. continue;
  908. foreach($a as $sKey => $mixedValue)
  909. if(is_scalar($mixedValue)) {
  910. $sValue = is_bool($mixedValue) ? ($mixedValue ? 'true' : 'false') : (string)$mixedValue;
  911. $aProps[$sKey][$sValue] = (isset($aProps[$sKey][$sValue]) ? $aProps[$sKey][$sValue] : 0) + 1;
  912. }
  913. }
  914. $aPropTables = [];
  915. foreach(array_slice($aProps, 0, 6, true) as $sKey => $aValues) {
  916. arsort($aValues);
  917. $iMax = max($aValues);
  918. $aPropRows = [];
  919. foreach(array_slice($aValues, 0, 10, true) as $sValue => $iCount)
  920. $aPropRows[] = ['label' => (string)$sValue, 'bar' => round(100 * $iCount / $iMax, 1), 'values' => ['count' => $iCount]];
  921. $aPropTables[] = [
  922. 'name' => 'prop_' . preg_replace('/[^a-z0-9_]/i', '_', $sKey),
  923. 'caption' => (string)$sKey,
  924. 'columns' => [
  925. ['key' => 'label', 'type' => 'label', 'title' => (string)$sKey],
  926. ['key' => 'count', 'type' => 'number', 'title' => _t('_bx_analytics_metric_count')],
  927. ],
  928. 'rows' => $aPropRows,
  929. ];
  930. }
  931. return [
  932. 'view' => 'detail',
  933. 'event' => $aEvent,
  934. 'title' => $this->getEventLabel($aEvent['type'], $aEvent['name'], $aEvent['module']),
  935. 'tiles' => [
  936. ['title' => _t('_bx_analytics_metric_count'), 'value' => $iTotal, 'type' => 'number'],
  937. ['title' => _t('_bx_analytics_metric_visitors'), 'value' => $iTotalVisitors, 'type' => 'number'],
  938. ['title' => _t('_bx_analytics_metric_per_visitor'), 'value' => $iTotalVisitors ? round($iTotal / $iTotalVisitors, 1) : 0, 'type' => 'decimal'],
  939. ],
  940. 'chart' => [
  941. 'kind' => 'lines',
  942. 'labels' => $aFilter['buckets'],
  943. 'bucket' => $aFilter['bucket'],
  944. 'tz' => $aFilter['tz'],
  945. 'datasets' => [
  946. ['key' => 'count', 'label' => _t('_bx_analytics_metric_count'), 'values' => $aCounts],
  947. ['key' => 'visitors', 'label' => _t('_bx_analytics_metric_visitors'), 'values' => $aVisitors],
  948. ],
  949. ],
  950. 'platforms' => $aPlatforms,
  951. 'props' => $aPropTables,
  952. ];
  953. }
  954. /**
  955. * Visits, latest first, with who, where from and on what. With `session` one visit's
  956. * timeline. Shows members by name, so it needs the people permission.
  957. */
  958. public function getSessions(BxAnalyticsFilter $oFilter, $aParams = [], $iProfileId = 0)
  959. {
  960. $iSession = (int)$this->_param($aParams, 'session', 0);
  961. if($iSession)
  962. return $this->_getSessionDetail($iSession);
  963. $aFilter = $oFilter->get();
  964. $iTotal = $this->_oDb->getSessionListCount($aFilter, $iProfileId);
  965. $iPages = max(1, (int)ceil($iTotal / $this->_iPerPage));
  966. $iPage = min($iPages, max(1, (int)$this->_param($aParams, 'page_sessions', 1)));
  967. $aRows = [];
  968. foreach($this->_oDb->getSessionList($aFilter, $this->_iPerPage, ($iPage - 1) * $this->_iPerPage, $iProfileId) as $aSession) {
  969. $aMember = $this->getMember((int)$aSession['profile_id']);
  970. $aRows[] = [
  971. 'label' => $aMember ? $aMember['name'] : _t('_bx_analytics_txt_guest'),
  972. 'open' => ['param' => 'session', 'value' => (int)$aSession['id']],
  973. 'values' => [
  974. 'started' => (int)$aSession['started'],
  975. 'platform' => $this->getLabel('platform', $aSession['platform']),
  976. 'location' => $this->_getLocation($aSession),
  977. 'device' => $this->_getDevice($aSession),
  978. 'source' => $aSession['referrer_host'] !== '' ? $aSession['referrer_host'] : $this->getLabel('channel', $aSession['channel'] !== '' ? $aSession['channel'] : 'direct'),
  979. 'pageviews' => (int)$aSession['pageviews'],
  980. 'duration' => (int)$aSession['duration'],
  981. ],
  982. ];
  983. }
  984. return [
  985. 'view' => 'list',
  986. 'name' => 'sessions',
  987. 'caption' => _t('_bx_analytics_lmi_cpt_sessions'),
  988. 'columns' => [
  989. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_txt_visitor')],
  990. ['key' => 'started', 'type' => 'datetime', 'title' => _t('_bx_analytics_txt_started')],
  991. ['key' => 'platform', 'type' => 'text', 'title' => _t('_bx_analytics_dim_platform')],
  992. ['key' => 'location', 'type' => 'text', 'title' => _t('_bx_analytics_txt_location')],
  993. ['key' => 'device', 'type' => 'text', 'title' => _t('_bx_analytics_dim_device')],
  994. ['key' => 'source', 'type' => 'text', 'title' => _t('_bx_analytics_txt_source')],
  995. ['key' => 'pageviews', 'type' => 'number', 'title' => _t('_bx_analytics_metric_pageviews')],
  996. ['key' => 'duration', 'type' => 'duration', 'title' => _t('_bx_analytics_txt_duration')],
  997. ],
  998. 'rows' => $aRows,
  999. 'page' => $iPage,
  1000. 'pages' => $iPages,
  1001. 'empty' => _t('_bx_analytics_txt_no_data'),
  1002. ];
  1003. }
  1004. protected function _getSessionDetail($iSession)
  1005. {
  1006. $aSession = $this->_oDb->getSession($iSession);
  1007. if(empty($aSession))
  1008. return false;
  1009. $iStart = (int)$aSession['started'];
  1010. $aTimeline = [];
  1011. foreach($this->_oDb->getSessionEvents($iSession) as $aEvent)
  1012. $aTimeline[] = [
  1013. 'offset' => max(0, (int)$aEvent['ts'] - $iStart),
  1014. 'type' => $aEvent['type'],
  1015. 'label' => $aEvent['type'] == BX_ANALYTICS_HIT_PAGEVIEW ? ($aEvent['title'] !== '' ? $aEvent['title'] : $aEvent['path']) : $this->getEventLabel($aEvent['type'], $aEvent['name'], $aEvent['module']),
  1016. 'path' => $aEvent['type'] == BX_ANALYTICS_HIT_PAGEVIEW ? $aEvent['path'] : '',
  1017. 'engaged' => (int)round($aEvent['engaged'] / 1000),
  1018. ];
  1019. $aMember = $this->getMember((int)$aSession['profile_id']);
  1020. return [
  1021. 'view' => 'detail',
  1022. 'id' => (int)$aSession['id'],
  1023. 'member' => $aMember,
  1024. 'facts' => [
  1025. ['title' => _t('_bx_analytics_txt_started'), 'value' => (int)$aSession['started'], 'type' => 'datetime'],
  1026. ['title' => _t('_bx_analytics_txt_duration'), 'value' => (int)$aSession['duration'], 'type' => 'duration'],
  1027. ['title' => _t('_bx_analytics_metric_pageviews'), 'value' => (int)$aSession['pageviews'], 'type' => 'number'],
  1028. ['title' => _t('_bx_analytics_dim_platform'), 'value' => $this->getLabel('platform', $aSession['platform']) . ($aSession['app_version'] !== '' ? ' ' . $aSession['app_version'] : ''), 'type' => 'text'],
  1029. ['title' => _t('_bx_analytics_txt_location'), 'value' => $this->_getLocation($aSession), 'type' => 'text'],
  1030. ['title' => _t('_bx_analytics_dim_device'), 'value' => $this->_getDevice($aSession), 'type' => 'text'],
  1031. ['title' => _t('_bx_analytics_txt_source'), 'value' => $aSession['referrer'] !== '' ? $aSession['referrer'] : $this->getLabel('channel', $aSession['channel'] !== '' ? $aSession['channel'] : 'direct'), 'type' => 'text'],
  1032. ['title' => _t('_bx_analytics_dim_language'), 'value' => $aSession['language'], 'type' => 'text'],
  1033. ],
  1034. 'timeline' => $aTimeline,
  1035. ];
  1036. }
  1037. /**
  1038. * Members who visited in the period. With `user` one member's visits and actions; with
  1039. * `session` too, one of their visits. Needs the people permission.
  1040. */
  1041. public function getUsers(BxAnalyticsFilter $oFilter, $aParams = [])
  1042. {
  1043. $iUser = (int)$this->_param($aParams, 'user', 0);
  1044. if($iUser) {
  1045. if((int)$this->_param($aParams, 'session', 0))
  1046. return $this->_getSessionDetail((int)$aParams['session']);
  1047. return $this->_getUserDetail($oFilter, $iUser, $aParams);
  1048. }
  1049. $aFilter = $oFilter->get();
  1050. $iTotal = $this->_oDb->getMemberListCount($aFilter);
  1051. $iPages = max(1, (int)ceil($iTotal / $this->_iPerPage));
  1052. $iPage = min($iPages, max(1, (int)$this->_param($aParams, 'page_users', 1)));
  1053. $aMembers = $this->_oDb->getMemberList($aFilter, $this->_iPerPage, ($iPage - 1) * $this->_iPerPage);
  1054. // lifetime value: what each member paid, for viewers who may see revenue
  1055. $bRevenue = $this->_oModule->checkAllowedRevenue() === CHECK_ACTION_RESULT_ALLOWED;
  1056. $aLtv = $bRevenue ? $this->getLifetimeValues(array_column($aMembers, 'profile_id')) : [];
  1057. $aRows = [];
  1058. foreach($aMembers as $aRow) {
  1059. $aMember = $this->getMember((int)$aRow['profile_id']);
  1060. $aValues = [
  1061. 'sessions' => (int)$aRow['sessions'],
  1062. 'pageviews' => (int)$aRow['pageviews'],
  1063. 'duration' => (int)$aRow['duration'],
  1064. 'platforms' => $this->_getPlatformsText($aRow['platforms']),
  1065. 'last' => (int)$aRow['last'],
  1066. ];
  1067. if($bRevenue)
  1068. $aValues['ltv'] = isset($aLtv[$aRow['profile_id']]) ? round((float)$aLtv[$aRow['profile_id']], 2) : 0;
  1069. $aRows[] = [
  1070. 'label' => $aMember ? $aMember['name'] : _t('_bx_analytics_txt_deleted_member'),
  1071. 'sub' => $aRow['country'] !== '' ? $this->getLabel('country', $aRow['country']) : '',
  1072. 'open' => ['param' => 'user', 'value' => (int)$aRow['profile_id']],
  1073. 'values' => $aValues,
  1074. ];
  1075. }
  1076. $aColumns = [
  1077. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_txt_member_name')],
  1078. ['key' => 'sessions', 'type' => 'number', 'title' => _t('_bx_analytics_metric_sessions')],
  1079. ['key' => 'pageviews', 'type' => 'number', 'title' => _t('_bx_analytics_metric_pageviews')],
  1080. ['key' => 'duration', 'type' => 'duration', 'title' => _t('_bx_analytics_txt_time_spent')],
  1081. ['key' => 'platforms', 'type' => 'text', 'title' => _t('_bx_analytics_tab_platforms')],
  1082. ];
  1083. if($bRevenue)
  1084. $aColumns[] = ['key' => 'ltv', 'type' => 'money', 'title' => _t('_bx_analytics_txt_lifetime_value')];
  1085. $aColumns[] = ['key' => 'last', 'type' => 'datetime', 'title' => _t('_bx_analytics_txt_last_seen')];
  1086. return [
  1087. 'view' => 'list',
  1088. 'name' => 'users',
  1089. 'caption' => _t('_bx_analytics_lmi_cpt_users'),
  1090. 'columns' => $aColumns,
  1091. 'rows' => $aRows,
  1092. 'page' => $iPage,
  1093. 'pages' => $iPages,
  1094. 'empty' => _t('_bx_analytics_txt_no_members'),
  1095. ];
  1096. }
  1097. protected function _getUserDetail(BxAnalyticsFilter $oFilter, $iProfileId, $aParams)
  1098. {
  1099. $aTotals = $this->_oDb->getMemberTotals($iProfileId);
  1100. if(empty($aTotals['totals']['sessions']))
  1101. return false;
  1102. $aPlatforms = [];
  1103. $iSessions = (int)$aTotals['totals']['sessions'];
  1104. foreach($aTotals['platforms'] as $sPlatform => $iCount)
  1105. $aPlatforms[] = [
  1106. 'label' => $this->getLabel('platform', $sPlatform),
  1107. 'count' => (int)$iCount,
  1108. 'share' => $iSessions ? round(100 * $iCount / $iSessions, 1) : 0,
  1109. ];
  1110. $aActions = [];
  1111. $aRows = $this->_oDb->getMemberActions($iProfileId, $oFilter->get());
  1112. $iMax = 1;
  1113. foreach($aRows as $aRow)
  1114. $iMax = max($iMax, (int)$aRow['count']);
  1115. foreach($aRows as $aRow)
  1116. $aActions[] = [
  1117. 'label' => $this->getEventLabel($aRow['type'], $aRow['name'], $aRow['module']),
  1118. 'bar' => round(100 * $aRow['count'] / $iMax, 1),
  1119. 'values' => ['count' => (int)$aRow['count'], 'last' => (int)$aRow['last']],
  1120. ];
  1121. $aSessions = $this->getSessions($oFilter, $aParams, $iProfileId);
  1122. $aTiles = [
  1123. ['title' => _t('_bx_analytics_metric_sessions'), 'value' => $iSessions, 'type' => 'number'],
  1124. ['title' => _t('_bx_analytics_metric_pageviews'), 'value' => (int)$aTotals['totals']['pageviews'], 'type' => 'number'],
  1125. ['title' => _t('_bx_analytics_txt_time_spent'), 'value' => (int)$aTotals['totals']['duration'], 'type' => 'duration'],
  1126. ['title' => _t('_bx_analytics_txt_first_seen'), 'value' => (int)$aTotals['totals']['first'], 'type' => 'date'],
  1127. ['title' => _t('_bx_analytics_txt_last_seen'), 'value' => (int)$aTotals['totals']['last'], 'type' => 'datetime'],
  1128. ];
  1129. if($this->_oModule->checkAllowedRevenue() === CHECK_ACTION_RESULT_ALLOWED) {
  1130. $aLtv = $this->getLifetimeValues([$iProfileId]);
  1131. $aTiles[] = ['title' => _t('_bx_analytics_txt_lifetime_value'), 'value' => isset($aLtv[$iProfileId]) ? round((float)$aLtv[$iProfileId], 2) : 0, 'type' => 'money'];
  1132. }
  1133. return [
  1134. 'view' => 'user',
  1135. 'member' => $this->getMember($iProfileId),
  1136. 'tiles' => $aTiles,
  1137. 'platforms' => $aPlatforms,
  1138. 'actions' => [
  1139. 'name' => 'user_actions',
  1140. 'caption' => _t('_bx_analytics_txt_actions'),
  1141. 'columns' => [
  1142. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_txt_event')],
  1143. ['key' => 'count', 'type' => 'number', 'title' => _t('_bx_analytics_metric_count')],
  1144. ['key' => 'last', 'type' => 'datetime', 'title' => _t('_bx_analytics_txt_last_seen')],
  1145. ],
  1146. 'rows' => $aActions,
  1147. 'empty' => _t('_bx_analytics_txt_no_events'),
  1148. ],
  1149. 'sessions' => $aSessions,
  1150. ];
  1151. }
  1152. /**
  1153. * Where visitors go from a page: the pages they open next and, for one of those, the
  1154. * pages after it; and the most common three-page paths from the entry page. Params:
  1155. * `from` (default: the top entry page), `via` (a next page to follow further).
  1156. */
  1157. public function getJourneys(BxAnalyticsFilter $oFilter, $aParams = [])
  1158. {
  1159. $iLimit = 200000;
  1160. // each visit's pages in order, a page reloaded in place counted once
  1161. $aRead = $this->_oDb->getPageSequences($oFilter->get(), $iLimit);
  1162. $aSequences = $aRead['sequences'];
  1163. $aEntries = [];
  1164. $aPaths = [];
  1165. foreach($aSequences as $aSequence) {
  1166. $aEntries[$aSequence[0]] = (isset($aEntries[$aSequence[0]]) ? $aEntries[$aSequence[0]] : 0) + 1;
  1167. $sPath = implode("\n", array_slice($aSequence, 0, 3));
  1168. $aPaths[$sPath] = (isset($aPaths[$sPath]) ? $aPaths[$sPath] : 0) + 1;
  1169. }
  1170. arsort($aEntries);
  1171. arsort($aPaths);
  1172. $sFrom = (string)$this->_param($aParams, 'jfrom', '');
  1173. if($sFrom === '' || !isset($aEntries[$sFrom]) && !$this->_inSequences($aSequences, $sFrom))
  1174. $sFrom = !empty($aEntries) ? (string)key($aEntries) : '';
  1175. $sVia = (string)$this->_param($aParams, 'via', '');
  1176. // the page after the first visit of `jfrom`, and after `via` when it came right after
  1177. $iFromSessions = 0;
  1178. $aNext = [];
  1179. $iNextExits = 0;
  1180. $iViaSessions = 0;
  1181. $aAfter = [];
  1182. $iAfterExits = 0;
  1183. foreach($aSequences as $aSequence) {
  1184. $i = array_search($sFrom, $aSequence, true);
  1185. if($i === false)
  1186. continue;
  1187. $iFromSessions++;
  1188. if(!isset($aSequence[$i + 1])) {
  1189. $iNextExits++;
  1190. continue;
  1191. }
  1192. $sNext = $aSequence[$i + 1];
  1193. $aNext[$sNext] = (isset($aNext[$sNext]) ? $aNext[$sNext] : 0) + 1;
  1194. if($sVia !== '' && $sNext === $sVia) {
  1195. $iViaSessions++;
  1196. if(isset($aSequence[$i + 2]))
  1197. $aAfter[$aSequence[$i + 2]] = (isset($aAfter[$aSequence[$i + 2]]) ? $aAfter[$aSequence[$i + 2]] : 0) + 1;
  1198. else
  1199. $iAfterExits++;
  1200. }
  1201. }
  1202. arsort($aNext);
  1203. arsort($aAfter);
  1204. $fStep = function($aCounts, $iExits, $iTotal, $iLimit) {
  1205. $aResult = [];
  1206. foreach(array_slice($aCounts, 0, $iLimit, true) as $sPage => $iCount)
  1207. $aResult[] = ['page' => (string)$sPage, 'sessions' => $iCount, 'share' => $iTotal ? round(100 * $iCount / $iTotal, 1) : 0];
  1208. if($iExits)
  1209. $aResult[] = ['page' => '', 'exit' => true, 'sessions' => $iExits, 'share' => $iTotal ? round(100 * $iExits / $iTotal, 1) : 0];
  1210. return $aResult;
  1211. };
  1212. $iTotal = count($aSequences);
  1213. $aTopPaths = [];
  1214. foreach(array_slice($aPaths, 0, 10, true) as $sPath => $iCount)
  1215. $aTopPaths[] = ['pages' => explode("\n", (string)$sPath), 'sessions' => $iCount, 'share' => $iTotal ? round(100 * $iCount / $iTotal, 1) : 0];
  1216. $aEntryList = [];
  1217. foreach(array_slice($aEntries, 0, 10, true) as $sPage => $iCount)
  1218. $aEntryList[] = ['page' => (string)$sPage, 'sessions' => $iCount, 'active' => (string)$sPage === $sFrom];
  1219. return [
  1220. 'jfrom' => $sFrom,
  1221. 'via' => $sVia !== '' && $iViaSessions ? $sVia : '',
  1222. 'sessions' => $iTotal,
  1223. 'truncated' => $aRead['rows'] >= $iLimit,
  1224. 'entries' => $aEntryList,
  1225. 'from_sessions' => $iFromSessions,
  1226. 'next' => $fStep($aNext, $iNextExits, $iFromSessions, 8),
  1227. 'after' => $iViaSessions ? $fStep($aAfter, $iAfterExits, $iViaSessions, 8) : [],
  1228. 'via_sessions' => $iViaSessions,
  1229. 'paths' => $aTopPaths,
  1230. ];
  1231. }
  1232. /**
  1233. * Weekly cohorts by first visit and the share of each that came back in the weeks after.
  1234. * Tabs: members (stable ids) or all visitor ids (with cookieless ids a guest is new every
  1235. * day, so only persistent ids make guest retention meaningful).
  1236. */
  1237. public function getRetention(BxAnalyticsFilter $oFilter, $aParams = [])
  1238. {
  1239. $aTabs = $this->_aReportTabs['retention'];
  1240. $sTab = $this->_getTab($aParams, 'retention', $aTabs);
  1241. $aFilter = $oFilter->get();
  1242. $iWeeks = 12;
  1243. $iOffset = (int)$aFilter['offset'];
  1244. $fWeek = function($iTime) use ($iOffset) {
  1245. return (int)(floor(($iTime + $iOffset - 345600) / 604800) * 604800 + 345600 - $iOffset);
  1246. };
  1247. $iNow = (int)$aFilter['now'];
  1248. $iThisWeek = $fWeek($iNow);
  1249. $iFrom = $iThisWeek - ($iWeeks - 1) * 604800;
  1250. $aCohorts = [];
  1251. for($i = 0; $i < $iWeeks; $i++)
  1252. $aCohorts[$iFrom + $i * 604800] = ['size' => 0, 'back' => array_fill(0, $iWeeks - $i, 0)];
  1253. foreach($this->_oDb->getRetentionCohorts($iFrom, $iNow + 1, $aFilter['platform'], $aTabs[$sTab]) as $aRow) {
  1254. $iCohort = $iFrom + (int)$aRow['cohort'] * 604800;
  1255. if(!isset($aCohorts[$iCohort]))
  1256. continue;
  1257. $iVisitors = (int)$aRow['visitors'];
  1258. $iMask = (int)$aRow['weeks'];
  1259. $aCohorts[$iCohort]['size'] += $iVisitors;
  1260. foreach(array_keys($aCohorts[$iCohort]['back']) as $iIndex)
  1261. if($iMask & (1 << ((int)$aRow['cohort'] + $iIndex)))
  1262. $aCohorts[$iCohort]['back'][$iIndex] += $iVisitors;
  1263. }
  1264. $aRows = [];
  1265. foreach($aCohorts as $iStart => $aCohort)
  1266. $aRows[] = [
  1267. 'start' => $iStart,
  1268. 'size' => $aCohort['size'],
  1269. 'weeks' => array_map(function($iCount) use ($aCohort) {
  1270. return $aCohort['size'] ? round(100 * $iCount / $aCohort['size'], 1) : null;
  1271. }, $aCohort['back']),
  1272. ];
  1273. return [
  1274. 'tab' => $sTab,
  1275. 'tabs' => $this->_getTabs($aTabs, $sTab, '_bx_analytics_tab_retention_'),
  1276. 'weeks' => $iWeeks,
  1277. 'tz' => $aFilter['tz'],
  1278. 'rows' => $aRows,
  1279. 'cookieless' => $sTab == 'visitors' && $this->_oConfig->getIdentityMode() == BX_ANALYTICS_IDENTITY_COOKIELESS,
  1280. ];
  1281. }
  1282. /**
  1283. * Goals with their conversions: visits and visitors that reached them, the conversion
  1284. * rate of all visits, and the change against the previous period.
  1285. */
  1286. public function getGoals(BxAnalyticsFilter $oFilter, $aParams = [])
  1287. {
  1288. $aFilter = $oFilter->get();
  1289. $aPrevious = $oFilter->getPrevious();
  1290. $aTotals = $this->_oDb->getTotals($aFilter);
  1291. $aTotalsPrevious = $this->_oDb->getTotals($aPrevious);
  1292. $iSessions = (int)$aTotals['sessions'];
  1293. $iSessionsPrevious = (int)$aTotalsPrevious['sessions'];
  1294. $aGoals = [];
  1295. foreach($this->_oDb->getGoals($this->_oConfig->CNF['MAX_GOALS']) as $aGoal) {
  1296. $aCurrent = $this->_oDb->getGoalTotals($aFilter, $aGoal);
  1297. $aBefore = $this->_oDb->getGoalTotals($aPrevious, $aGoal);
  1298. $fRate = $iSessions ? round(100 * $aCurrent['sessions'] / $iSessions, 2) : 0;
  1299. $fRateBefore = $iSessionsPrevious ? round(100 * $aBefore['sessions'] / $iSessionsPrevious, 2) : 0;
  1300. $aGoals[] = [
  1301. 'id' => (int)$aGoal['id'],
  1302. 'name' => $aGoal['name'],
  1303. 'type' => $aGoal['type'],
  1304. 'pattern' => $aGoal['pattern'],
  1305. 'conversions' => (int)$aCurrent['sessions'],
  1306. 'visitors' => (int)$aCurrent['visitors'],
  1307. 'rate' => $fRate,
  1308. 'change' => $this->_change($fRate, $fRateBefore),
  1309. ];
  1310. }
  1311. return [
  1312. 'goals' => $aGoals,
  1313. 'sessions' => $iSessions,
  1314. 'manage' => $this->_oModule->checkAllowedManage() === CHECK_ACTION_RESULT_ALLOWED,
  1315. 'edit' => (string)$this->_param($aParams, 'edit_goal', ''),
  1316. ];
  1317. }
  1318. /**
  1319. * A funnel's steps with the visits that reached each one in order within the visit, and
  1320. * the drop between steps. Param `funnel` (default: the first).
  1321. */
  1322. public function getFunnels(BxAnalyticsFilter $oFilter, $aParams = [])
  1323. {
  1324. $aFunnels = $this->_oDb->getFunnels();
  1325. $iFunnel = (int)$this->_param($aParams, 'funnel', 0);
  1326. $aFunnel = !empty($aFunnels) ? $aFunnels[0] : null;
  1327. foreach($aFunnels as $a)
  1328. if((int)$a['id'] === $iFunnel)
  1329. $aFunnel = $a;
  1330. $aSteps = [];
  1331. if($aFunnel) {
  1332. $aDefinition = json_decode($aFunnel['steps'], true);
  1333. $aDefinition = is_array($aDefinition) ? $aDefinition : [];
  1334. // each visit walks the steps in order: a step counts when it happens after the previous one
  1335. $aDefinition = array_values(array_filter($aDefinition, function($aStep) {
  1336. return is_array($aStep) && isset($aStep['type'], $aStep['pattern']);
  1337. }));
  1338. foreach($this->_oDb->getFunnelCounts($oFilter->get(), $aDefinition) as $i => $iSessions)
  1339. $aSteps[] = [
  1340. 'type' => $aDefinition[$i]['type'],
  1341. 'pattern' => $aDefinition[$i]['pattern'],
  1342. 'sessions' => $iSessions,
  1343. ];
  1344. $iFirst = !empty($aSteps) ? $aSteps[0]['sessions'] : 0;
  1345. foreach($aSteps as $i => $aStep) {
  1346. $aSteps[$i]['rate'] = $iFirst ? round(100 * $aStep['sessions'] / $iFirst, 1) : 0;
  1347. $aSteps[$i]['drop'] = $i > 0 && $aSteps[$i - 1]['sessions'] ? round(100 * ($aSteps[$i - 1]['sessions'] - $aStep['sessions']) / $aSteps[$i - 1]['sessions'], 1) : null;
  1348. }
  1349. }
  1350. $aList = [];
  1351. foreach($aFunnels as $a)
  1352. $aList[] = ['id' => (int)$a['id'], 'name' => $a['name'], 'active' => $aFunnel && (int)$a['id'] === (int)$aFunnel['id']];
  1353. return [
  1354. 'funnels' => $aList,
  1355. 'funnel' => $aFunnel ? ['id' => (int)$aFunnel['id'], 'name' => $aFunnel['name']] : null,
  1356. 'steps' => $aSteps,
  1357. 'manage' => $this->_oModule->checkAllowedManage() === CHECK_ACTION_RESULT_ALLOWED,
  1358. 'edit' => (string)$this->_param($aParams, 'edit_funnel', ''),
  1359. 'definition' => $aFunnel ? json_decode($aFunnel['steps'], true) : [],
  1360. 'max_steps' => $this->_oConfig->CNF['MAX_FUNNEL_STEPS'],
  1361. ];
  1362. }
  1363. protected function _inSequences($aSequences, $sPage)
  1364. {
  1365. foreach($aSequences as $aSequence)
  1366. if(in_array($sPage, $aSequence, true))
  1367. return true;
  1368. return false;
  1369. }
  1370. /*
  1371. * Health: client errors and performance
  1372. */
  1373. /**
  1374. * Client errors grouped by fingerprint, with their hits, visitors and platforms in the
  1375. * period and a trend per group. With `error` (a group id) one group in detail. Tabs: open,
  1376. * new (first seen in the period), resolved, ignored, all.
  1377. */
  1378. public function getErrors(BxAnalyticsFilter $oFilter, $aParams = [])
  1379. {
  1380. $iError = (int)$this->_param($aParams, 'error', 0);
  1381. if($iError)
  1382. return $this->_getErrorDetail($oFilter, $iError);
  1383. // ignored errors keep counting but stay out of everything but their own tab
  1384. $aCounted = ['open', 'resolved'];
  1385. $aTabs = $this->_aReportTabs['errors'];
  1386. $sTab = $this->_getTab($aParams, 'errors', $aTabs);
  1387. $aFilter = $oFilter->get();
  1388. $iNewFrom = $sTab == 'new' ? (int)$aFilter['from'] : 0;
  1389. // crashes are fatal errors in the apps, against the apps' visits
  1390. $aApps = array_values(array_filter(array_keys($this->_oConfig->getPlatforms()), [$this->_oConfig, 'isAppPlatform']));
  1391. $aTotals = $this->_oDb->getErrorTotals($aFilter, $aCounted, $aApps);
  1392. $aBefore = $this->_oDb->getErrorTotals($oFilter->getPrevious(), $aCounted);
  1393. $aSessions = $this->_oDb->getSessionsPerPlatform($aFilter);
  1394. $iSessions = (int)array_sum($aSessions);
  1395. $iAppSessions = 0;
  1396. foreach($aApps as $sApp)
  1397. $iAppSessions += isset($aSessions[$sApp]) ? (int)$aSessions[$sApp] : 0;
  1398. $aTiles = [
  1399. ['title' => _t('_bx_analytics_metric_errors'), 'value' => (int)$aTotals['hits'], 'type' => 'number', 'metric' => 'errors', 'change' => $this->_change((int)$aTotals['hits'], (int)$aBefore['hits'])],
  1400. ['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'])],
  1401. ];
  1402. if($aFilter['platform'] === '' || $this->_oConfig->isAppPlatform($aFilter['platform']))
  1403. $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')];
  1404. $aTiles[] = ['title' => _t('_bx_analytics_metric_new_errors'), 'value' => $this->_oDb->getErrorListCount($aFilter, $aCounted, (int)$aFilter['from']), 'type' => 'number'];
  1405. $iTotal = $this->_oDb->getErrorListCount($aFilter, $aTabs[$sTab], $iNewFrom);
  1406. $iPages = max(1, (int)ceil($iTotal / $this->_iPerPage));
  1407. $iPage = min($iPages, max(1, (int)$this->_param($aParams, 'page_errors', 1)));
  1408. $aList = $this->_oDb->getErrorList($aFilter, $aTabs[$sTab], $iNewFrom, $this->_iPerPage, ($iPage - 1) * $this->_iPerPage);
  1409. $aIds = array_map('intval', array_column($aList, 'error_id'));
  1410. $aGroups = $this->_oDb->getErrorGroups($aIds);
  1411. $aTrends = $this->_oDb->getErrorTrends($aFilter, $aIds, $aFilter['bucket'], $aFilter['offset'], $aFilter['starts']);
  1412. $iMax = 1;
  1413. foreach($aList as $aRow)
  1414. $iMax = max($iMax, (int)$aRow['count']);
  1415. $aRows = [];
  1416. foreach($aList as $aRow) {
  1417. $iId = (int)$aRow['error_id'];
  1418. if(!isset($aGroups[$iId]))
  1419. continue;
  1420. $aGroup = $aGroups[$iId];
  1421. $aTrend = [];
  1422. foreach($aFilter['buckets'] as $iBucket)
  1423. $aTrend[] = isset($aTrends[$iId][$iBucket]) ? $aTrends[$iId][$iBucket] : 0;
  1424. $aRows[] = [
  1425. 'label' => $this->getErrorLabel($aGroup),
  1426. 'sub' => implode(' · ', array_filter([
  1427. _t('_bx_analytics_error_kind_' . $aGroup['kind']),
  1428. $this->_getErrorLocation($aGroup),
  1429. (int)$aRow['fatal'] ? _t('_bx_analytics_txt_fatal') : '',
  1430. $sTab == 'all' ? _t('_bx_analytics_error_status_' . $aGroup['status']) : '',
  1431. ])),
  1432. 'open' => ['param' => 'error', 'value' => $iId],
  1433. 'bar' => round(100 * $aRow['count'] / $iMax, 1),
  1434. 'values' => [
  1435. 'trend' => $aTrend,
  1436. 'count' => (int)$aRow['count'],
  1437. 'users' => (int)$aRow['users'],
  1438. 'platforms' => $this->_getPlatformsText($aRow['platforms']),
  1439. 'first_seen' => (int)$aGroup['first_seen'],
  1440. 'last_seen' => (int)$aRow['last_in'],
  1441. ],
  1442. ];
  1443. }
  1444. return [
  1445. 'view' => 'list',
  1446. 'tiles' => $aTiles,
  1447. 'name' => 'errors',
  1448. 'caption' => _t('_bx_analytics_lmi_cpt_errors'),
  1449. 'tab' => $sTab,
  1450. 'tabs' => $this->_getTabs($aTabs, $sTab, '_bx_analytics_tab_errors_'),
  1451. 'columns' => [
  1452. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_txt_error')],
  1453. ['key' => 'trend', 'type' => 'spark', 'title' => _t('_bx_analytics_txt_trend')],
  1454. ['key' => 'count', 'type' => 'number', 'title' => _t('_bx_analytics_metric_occurrences')],
  1455. ['key' => 'users', 'type' => 'number', 'title' => _t('_bx_analytics_metric_visitors')],
  1456. ['key' => 'platforms', 'type' => 'text', 'title' => _t('_bx_analytics_dim_platform')],
  1457. ['key' => 'last_seen', 'type' => 'datetime', 'title' => _t('_bx_analytics_txt_last_seen')],
  1458. ],
  1459. 'rows' => $aRows,
  1460. 'page' => $iPage,
  1461. 'pages' => $iPages,
  1462. 'empty' => _t($sTab == 'open' || $sTab == 'new' ? '_bx_analytics_txt_no_errors' : '_bx_analytics_txt_no_data'),
  1463. ];
  1464. }
  1465. /**
  1466. * One error group: what and where, its hits over time and by client, and the latest
  1467. * occurrences. The stack and the visits behind the occurrences can carry what a visitor
  1468. * typed or opened, so they need the people permission.
  1469. */
  1470. protected function _getErrorDetail(BxAnalyticsFilter $oFilter, $iId)
  1471. {
  1472. $aGroup = $this->_oDb->getErrorGroup($iId);
  1473. if(empty($aGroup))
  1474. return ['view' => 'detail', 'missing' => true, 'title' => _t('_bx_analytics_txt_error_missing')];
  1475. $aFilter = $oFilter->get();
  1476. $bPeople = $this->_oModule->checkAllowedPeople() === CHECK_ACTION_RESULT_ALLOWED;
  1477. $bManage = $this->_oModule->checkAllowedManage() === CHECK_ACTION_RESULT_ALLOWED;
  1478. $aStats = $this->_oDb->getErrorStats($aFilter, $iId);
  1479. $aRows = $this->_oDb->getErrorSeries($aFilter, $iId, $aFilter['bucket'], $aFilter['offset'], $aFilter['starts']);
  1480. $aCounts = [];
  1481. $aUsers = [];
  1482. foreach($aFilter['buckets'] as $iBucket) {
  1483. $aCounts[] = isset($aRows[$iBucket]) ? (int)$aRows[$iBucket]['count'] : 0;
  1484. $aUsers[] = isset($aRows[$iBucket]) ? (int)$aRows[$iBucket]['users'] : 0;
  1485. }
  1486. // where it happens: a table per client column that has values
  1487. $aBreakdowns = [];
  1488. foreach(['platform', 'route', 'browser', 'os', 'device', 'app_version', 'country'] as $sColumn) {
  1489. $aValues = $this->_oDb->getErrorBreakdown($aFilter, $iId, $sColumn);
  1490. if(empty($aValues) || (count($aValues) == 1 && $aValues[0]['value'] === ''))
  1491. continue;
  1492. $iTotal = 0;
  1493. foreach($aValues as $aValue)
  1494. $iTotal += (int)$aValue['count'];
  1495. $aBreakdownRows = [];
  1496. foreach($aValues as $aValue)
  1497. $aBreakdownRows[] = [
  1498. 'label' => $this->getLabel($sColumn, (string)$aValue['value']),
  1499. 'bar' => $iTotal ? round(100 * $aValue['count'] / $iTotal, 1) : 0,
  1500. 'values' => [
  1501. 'count' => (int)$aValue['count'],
  1502. 'share' => $iTotal ? round(100 * $aValue['count'] / $iTotal, 1) : 0,
  1503. ],
  1504. ];
  1505. $sTitle = _t($sColumn == 'route' ? '_bx_analytics_txt_route' : '_bx_analytics_dim_' . $sColumn);
  1506. $aBreakdowns[] = [
  1507. 'name' => 'error_' . $sColumn,
  1508. 'caption' => $sTitle,
  1509. 'columns' => [
  1510. ['key' => 'label', 'type' => 'label', 'title' => $sTitle],
  1511. ['key' => 'count', 'type' => 'number', 'title' => _t('_bx_analytics_metric_occurrences')],
  1512. ['key' => 'share', 'type' => 'percent', 'title' => _t('_bx_analytics_txt_share')],
  1513. ],
  1514. 'rows' => $aBreakdownRows,
  1515. ];
  1516. }
  1517. $aOccurrences = [];
  1518. foreach($this->_oDb->getErrorOccurrences($aFilter, $iId) as $aHit) {
  1519. $aRow = [
  1520. 'label' => $aHit['path'] !== '' ? $aHit['path'] : '/',
  1521. 'sub' => implode(' · ', array_filter([
  1522. $this->getLabel('platform', $aHit['platform']),
  1523. $aHit['app_version'],
  1524. $aHit['device'] !== '' ? $this->getLabel('device', $aHit['device']) : '',
  1525. $aHit['browser'],
  1526. $aHit['os'],
  1527. $aHit['country'] !== '' ? $this->getLabel('country', $aHit['country']) : '',
  1528. ])),
  1529. 'values' => [
  1530. 'ts' => (int)$aHit['ts'],
  1531. 'repeats' => (int)$aHit['repeats'],
  1532. ],
  1533. ];
  1534. // the visit behind it, for those who may see visits (Studio is for administrators)
  1535. if($bPeople && (int)$aHit['session_id'] && isAdmin())
  1536. $aRow['link'] = BX_DOL_URL_STUDIO . 'module.php?name=' . $this->_oModule->getName() . '&page=sessions&session=' . (int)$aHit['session_id'];
  1537. $aOccurrences[] = $aRow;
  1538. }
  1539. $aActions = [];
  1540. if($bManage)
  1541. foreach(['resolved', 'ignored', 'open'] as $sStatus)
  1542. if($sStatus != $aGroup['status'])
  1543. $aActions[] = ['op' => $sStatus, 'title' => _t('_bx_analytics_txt_error_mark_' . $sStatus)];
  1544. $sLocation = $this->_getErrorLocation($aGroup, true);
  1545. return [
  1546. 'view' => 'detail',
  1547. 'id' => (int)$aGroup['id'],
  1548. 'title' => $this->getErrorLabel($aGroup),
  1549. 'status' => $aGroup['status'],
  1550. 'facts' => array_values(array_filter([
  1551. ['title' => _t('_bx_analytics_txt_error_status'), 'value' => _t('_bx_analytics_error_status_' . $aGroup['status']), 'type' => 'text'],
  1552. ['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'],
  1553. $sLocation !== '' ? ['title' => _t('_bx_analytics_txt_error_source'), 'value' => $sLocation, 'type' => 'text'] : null,
  1554. (int)$aGroup['status_code'] ? ['title' => _t('_bx_analytics_txt_error_http_status'), 'value' => (string)(int)$aGroup['status_code'], 'type' => 'text'] : null,
  1555. ['title' => _t('_bx_analytics_txt_first_seen'), 'value' => (int)$aGroup['first_seen'], 'type' => 'datetime'],
  1556. ['title' => _t('_bx_analytics_txt_last_seen'), 'value' => (int)$aGroup['last_seen'], 'type' => 'datetime'],
  1557. ['title' => _t('_bx_analytics_txt_error_all_time'), 'value' => (int)$aGroup['occurrences'], 'type' => 'number'],
  1558. ])),
  1559. 'stack' => $bPeople ? (string)$aGroup['stack'] : '',
  1560. 'stack_hidden' => !$bPeople && (string)$aGroup['stack'] !== '',
  1561. 'tiles' => [
  1562. ['title' => _t('_bx_analytics_metric_occurrences'), 'value' => (int)$aStats['count'], 'type' => 'number'],
  1563. ['title' => _t('_bx_analytics_metric_visitors'), 'value' => (int)$aStats['users'], 'type' => 'number'],
  1564. ['title' => _t('_bx_analytics_metric_sessions'), 'value' => (int)$aStats['sessions'], 'type' => 'number'],
  1565. ],
  1566. 'chart' => [
  1567. 'kind' => 'lines',
  1568. 'labels' => $aFilter['buckets'],
  1569. 'bucket' => $aFilter['bucket'],
  1570. 'tz' => $aFilter['tz'],
  1571. 'datasets' => [
  1572. ['key' => 'count', 'label' => _t('_bx_analytics_metric_occurrences'), 'values' => $aCounts],
  1573. ['key' => 'users', 'label' => _t('_bx_analytics_metric_visitors'), 'values' => $aUsers],
  1574. ],
  1575. ],
  1576. 'breakdowns' => $aBreakdowns,
  1577. 'occurrences' => [
  1578. 'name' => 'error_hits',
  1579. 'caption' => _t('_bx_analytics_txt_error_latest'),
  1580. 'columns' => [
  1581. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_tab_pages')],
  1582. ['key' => 'repeats', 'type' => 'number', 'title' => _t('_bx_analytics_metric_occurrences')],
  1583. ['key' => 'ts', 'type' => 'datetime', 'title' => _t('_bx_analytics_txt_time')],
  1584. ],
  1585. 'rows' => $aOccurrences,
  1586. ],
  1587. 'manage' => $bManage,
  1588. 'actions' => $aActions,
  1589. ];
  1590. }
  1591. /**
  1592. * What an error group is called in lists: its message, or a name for the catch-all group.
  1593. */
  1594. public function getErrorLabel($aGroup)
  1595. {
  1596. if($aGroup['kind'] == BxAnalyticsErrors::KIND_OVERFLOW)
  1597. return _t('_bx_analytics_txt_error_overflow');
  1598. return $aGroup['message'] !== '' ? $aGroup['message'] : _t('_bx_analytics_txt_unknown');
  1599. }
  1600. /**
  1601. * Script and position of an error, file:line or file:line:column.
  1602. */
  1603. protected function _getErrorLocation($aGroup, $bColumn = false)
  1604. {
  1605. if($aGroup['source'] === '')
  1606. return '';
  1607. $sLocation = $aGroup['source'];
  1608. if((int)$aGroup['line'])
  1609. $sLocation .= ':' . (int)$aGroup['line'] . ($bColumn && (int)$aGroup['col'] ? ':' . (int)$aGroup['col'] : '');
  1610. return $sLocation;
  1611. }
  1612. /**
  1613. * Web vitals and app timings: the 75th percentile of each metric with the share of good,
  1614. * needing improvement and poor values; the chosen metric over time, by platform and by
  1615. * page. Params: `vital` (metric), `vital_device` (all, mobile, desktop), tab
  1616. * (`tab_performance`): route or path.
  1617. */
  1618. public function getPerformance(BxAnalyticsFilter $oFilter, $aParams = [])
  1619. {
  1620. $aFilter = $oFilter->get();
  1621. $aThresholds = $this->_oConfig->CNF['VITALS'];
  1622. $iMinSamples = 20;
  1623. // the metrics the platform has: web vitals in browsers, screen loads and starts in apps
  1624. $sPlatform = $aFilter['platform'];
  1625. $aMetrics = BxAnalyticsVitals::METRICS;
  1626. if($sPlatform !== '' && $this->_oConfig->isBrowserPlatform($sPlatform))
  1627. $aMetrics = BxAnalyticsVitals::WEB_METRICS;
  1628. elseif($sPlatform !== '' && $this->_oConfig->isAppPlatform($sPlatform))
  1629. $aMetrics = BxAnalyticsVitals::APP_METRICS;
  1630. $sMetric = (string)$this->_param($aParams, 'vital', '');
  1631. if(!in_array($sMetric, $aMetrics, true))
  1632. $sMetric = $aMetrics[0];
  1633. $sDevice = (string)$this->_param($aParams, 'vital_device', 'all');
  1634. if(!in_array($sDevice, ['all', 'mobile', 'desktop'], true))
  1635. $sDevice = 'all';
  1636. $aGroups = $this->_aReportTabs['performance'];
  1637. $sGroup = $this->_getTab($aParams, 'performance', $aGroups);
  1638. // the cards: exact counts on each side of the thresholds, and the histograms of the
  1639. // metrics with enough samples, each in one pass
  1640. $aDistribution = $this->_oDb->getVitalsDistribution($aFilter, $sDevice, $aThresholds);
  1641. $aCardMetrics = [];
  1642. foreach($aMetrics as $sCardMetric)
  1643. if($aDistribution[$sCardMetric]['samples'] >= $iMinSamples)
  1644. $aCardMetrics[] = $sCardMetric;
  1645. list($aCardHistograms) = $this->_getHistograms($this->_oDb->getVitalsHistograms($aFilter, $aCardMetrics, $sDevice));
  1646. $aCards = [];
  1647. foreach($aMetrics as $sCardMetric) {
  1648. $aCount = $aDistribution[$sCardMetric];
  1649. $iSamples = $aCount['samples'];
  1650. $mixedP75 = null;
  1651. if(isset($aCardHistograms[$sCardMetric]))
  1652. $mixedP75 = BxAnalyticsVitals::getPercentile($aCardHistograms[$sCardMetric], 0.75, $iMinSamples, [$aCount['good'], $iSamples - $aCount['good'] - $aCount['poor'], $aCount['poor']], $aThresholds[$sCardMetric]);
  1653. $aCards[] = [
  1654. 'metric' => $sCardMetric,
  1655. 'title' => _t('_bx_analytics_vital_' . $sCardMetric),
  1656. 'unit' => BxAnalyticsVitals::getUnit($sCardMetric),
  1657. 'value' => $mixedP75 !== null ? BxAnalyticsVitals::toDisplay($sCardMetric, round($mixedP75)) : null,
  1658. 'rating' => $mixedP75 !== null ? BxAnalyticsVitals::getRating(round($mixedP75), $aThresholds[$sCardMetric]) : '',
  1659. 'samples' => $iSamples,
  1660. 'good' => $iSamples ? round(100 * $aCount['good'] / $iSamples, 1) : 0,
  1661. 'needs_improvement' => $iSamples ? round(100 * ($iSamples - $aCount['good'] - $aCount['poor']) / $iSamples, 1) : 0,
  1662. 'poor' => $iSamples ? round(100 * $aCount['poor'] / $iSamples, 1) : 0,
  1663. 'good_max' => BxAnalyticsVitals::toDisplay($sCardMetric, $aThresholds[$sCardMetric][0]),
  1664. 'poor_min' => BxAnalyticsVitals::toDisplay($sCardMetric, $aThresholds[$sCardMetric][1]),
  1665. 'active' => $sCardMetric == $sMetric,
  1666. ];
  1667. }
  1668. $sUnit = BxAnalyticsVitals::getUnit($sMetric);
  1669. $fP75 = function($aHistogram, $aSides, $iMin) use ($sMetric, $aThresholds) {
  1670. $mixedValue = BxAnalyticsVitals::getPercentile($aHistogram, 0.75, $iMin, $aSides, $aThresholds[$sMetric]);
  1671. return $mixedValue !== null ? BxAnalyticsVitals::toDisplay($sMetric, round($mixedValue)) : null;
  1672. };
  1673. $fRating = function($mixedValue) use ($sMetric, $aThresholds) {
  1674. return $mixedValue !== null ? BxAnalyticsVitals::getRating($sMetric == 'cls' ? round($mixedValue * 1000) : $mixedValue, $aThresholds[$sMetric]) : '';
  1675. };
  1676. // the 75th percentile per time bucket, with the thresholds as lines; a bucket with few
  1677. // samples is left as a gap
  1678. list($aByBucket, $aBucketSides) = $this->_getHistograms($this->_oDb->getVitalsHistogram($aFilter, $sMetric, $sDevice, 'bucket', [], [$aFilter['bucket'], $aFilter['offset'], $aFilter['starts']]));
  1679. $aValues = [];
  1680. foreach($aFilter['buckets'] as $iBucket)
  1681. $aValues[] = isset($aByBucket[$iBucket]) ? $fP75($aByBucket[$iBucket], $aBucketSides[$iBucket], 5) : null;
  1682. $iCount = count($aFilter['buckets']);
  1683. $aPlatformRows = [];
  1684. list($aByPlatform, $aPlatformSides) = $this->_getHistograms($this->_oDb->getVitalsHistogram($aFilter, $sMetric, $sDevice, 'platform'));
  1685. foreach($aByPlatform as $sValue => $aHistogram) {
  1686. $mixedValue = $fP75($aHistogram, $aPlatformSides[$sValue], $iMinSamples);
  1687. $aPlatformRows[] = [
  1688. 'label' => $this->getLabel('platform', (string)$sValue),
  1689. 'values' => ['p75' => $mixedValue, 'rating' => $fRating($mixedValue), 'samples' => (int)array_sum($aHistogram)],
  1690. ];
  1691. }
  1692. usort($aPlatformRows, function($a, $b) { return $b['values']['samples'] - $a['values']['samples']; });
  1693. $iTotal = $this->_oDb->getVitalsPagesCount($aFilter, $sMetric, $sDevice, $sGroup);
  1694. $iPages = max(1, (int)ceil($iTotal / $this->_iPerPage));
  1695. $iPage = min($iPages, max(1, (int)$this->_param($aParams, 'page_performance', 1)));
  1696. $aPages = $this->_oDb->getVitalsPages($aFilter, $sMetric, $sDevice, $sGroup, $this->_iPerPage, ($iPage - 1) * $this->_iPerPage);
  1697. list($aPageHistograms, $aPageSides) = $this->_getHistograms($this->_oDb->getVitalsHistogram($aFilter, $sMetric, $sDevice, $sGroup, array_map('strval', array_keys($aPages))));
  1698. $iMax = 1;
  1699. foreach($aPages as $iSamples)
  1700. $iMax = max($iMax, (int)$iSamples);
  1701. $aPageRows = [];
  1702. foreach($aPages as $sValue => $iSamples) {
  1703. $mixedValue = isset($aPageHistograms[$sValue]) ? $fP75($aPageHistograms[$sValue], $aPageSides[$sValue], $iMinSamples) : null;
  1704. $aPageRows[] = [
  1705. 'label' => (string)$sValue,
  1706. 'bar' => round(100 * $iSamples / $iMax, 1),
  1707. 'values' => ['p75' => $mixedValue, 'rating' => $fRating($mixedValue), 'samples' => (int)$iSamples],
  1708. ];
  1709. }
  1710. $aColumns = function($sTitle) use ($sUnit) {
  1711. return [
  1712. ['key' => 'label', 'type' => 'label', 'title' => $sTitle],
  1713. ['key' => 'p75', 'type' => $sUnit, 'title' => _t('_bx_analytics_txt_p75')],
  1714. ['key' => 'rating', 'type' => 'rating', 'title' => _t('_bx_analytics_txt_rating')],
  1715. ['key' => 'samples', 'type' => 'number', 'title' => _t('_bx_analytics_txt_samples')],
  1716. ];
  1717. };
  1718. $aDevices = [];
  1719. foreach(['all', 'mobile', 'desktop'] as $sValue)
  1720. $aDevices[] = ['value' => $sValue, 'title' => _t('_bx_analytics_txt_device_' . $sValue), 'selected' => $sValue == $sDevice];
  1721. return [
  1722. 'metric' => $sMetric,
  1723. 'metric_title' => _t('_bx_analytics_vital_' . $sMetric),
  1724. 'unit' => $sUnit,
  1725. 'device' => $sDevice,
  1726. 'devices' => $aDevices,
  1727. 'min_samples' => $iMinSamples,
  1728. 'cards' => $aCards,
  1729. 'chart' => [
  1730. 'kind' => 'lines',
  1731. 'metric' => $sUnit,
  1732. 'labels' => $aFilter['buckets'],
  1733. 'bucket' => $aFilter['bucket'],
  1734. 'tz' => $aFilter['tz'],
  1735. 'datasets' => [
  1736. ['key' => 'p75', 'label' => _t('_bx_analytics_txt_p75'), 'values' => $aValues],
  1737. ['key' => 'good', 'label' => _t('_bx_analytics_txt_threshold_good'), 'values' => array_fill(0, $iCount, BxAnalyticsVitals::toDisplay($sMetric, $aThresholds[$sMetric][0])), 'dashed' => true, 'tone' => 'good'],
  1738. ['key' => 'poor', 'label' => _t('_bx_analytics_txt_threshold_poor'), 'values' => array_fill(0, $iCount, BxAnalyticsVitals::toDisplay($sMetric, $aThresholds[$sMetric][1])), 'dashed' => true, 'tone' => 'bad'],
  1739. ],
  1740. ],
  1741. 'platforms' => [
  1742. 'name' => 'vital_platforms',
  1743. 'caption' => _t('_bx_analytics_tab_platforms'),
  1744. 'columns' => $aColumns(_t('_bx_analytics_dim_platform')),
  1745. 'rows' => $aPlatformRows,
  1746. 'empty' => _t('_bx_analytics_txt_no_vitals'),
  1747. ],
  1748. // named after the block, so its pager and the tabs (which reset the page) share page_performance
  1749. 'pages' => [
  1750. 'name' => 'performance',
  1751. 'caption' => _t('_bx_analytics_txt_vitals_pages'),
  1752. 'tab' => $sGroup,
  1753. 'tabs' => $this->_getTabs($aGroups, $sGroup, '_bx_analytics_tab_vitals_'),
  1754. 'columns' => $aColumns(_t($sGroup == 'route' ? '_bx_analytics_txt_route' : '_bx_analytics_tab_pages')),
  1755. 'rows' => $aPageRows,
  1756. 'page' => $iPage,
  1757. 'pages' => $iPages,
  1758. 'empty' => _t('_bx_analytics_txt_no_vitals'),
  1759. ],
  1760. ];
  1761. }
  1762. /**
  1763. * Histogram rows (grp, hb, n, and side when the query has it) as [[group => [bucket =>
  1764. * count]], [group => [counts up to the good threshold, between, above the poor one]]].
  1765. */
  1766. protected function _getHistograms($aRows)
  1767. {
  1768. $aHistograms = [];
  1769. $aSides = [];
  1770. foreach($aRows as $aRow) {
  1771. $sGroup = (string)$aRow['grp'];
  1772. $iBucket = (int)$aRow['hb'];
  1773. $aHistograms[$sGroup][$iBucket] = (isset($aHistograms[$sGroup][$iBucket]) ? $aHistograms[$sGroup][$iBucket] : 0) + (int)$aRow['n'];
  1774. if(!isset($aSides[$sGroup]))
  1775. $aSides[$sGroup] = isset($aRow['side']) ? [0, 0, 0] : [];
  1776. if(isset($aRow['side']))
  1777. $aSides[$sGroup][min(2, max(0, (int)$aRow['side']))] += (int)$aRow['n'];
  1778. }
  1779. return [$aHistograms, $aSides];
  1780. }
  1781. /*
  1782. * Revenue. Amounts come from the ledger in the site's default currency, as the payment
  1783. * module recorded them; orders paid with credits are reported apart from money.
  1784. */
  1785. /**
  1786. * Gross and net revenue, refunds, orders, paying customers and revenue per customer, over
  1787. * time and split by source, provider, platform and the buyers' traffic sources. Param
  1788. * `tab` (or tab_revenue): sources, providers, platforms, first_touch, last_touch, campaigns.
  1789. */
  1790. public function getRevenue(BxAnalyticsFilter $oFilter, $aParams = [])
  1791. {
  1792. $aMoney = $this->getMoney();
  1793. $aFilter = $this->_getRevenueFilter($oFilter->get(), $aMoney);
  1794. $aPrevious = $this->_getRevenueFilter($oFilter->getPrevious(), $aMoney);
  1795. $aCurrent = $this->_oDb->getRevenueTotals($aFilter);
  1796. $aBefore = $this->_oDb->getRevenueTotals($aPrevious);
  1797. $aTiles = [];
  1798. foreach(['gross' => 'money', 'refunds' => 'money', 'net' => 'money', 'orders' => 'number', 'customers' => 'number', 'arppu' => 'money'] as $sKey => $sType) {
  1799. $fValue = $sKey == 'arppu' ? ((int)$aCurrent['customers'] ? (float)$aCurrent['net'] / (int)$aCurrent['customers'] : 0) : (float)$aCurrent[$sKey];
  1800. $fPrevious = $sKey == 'arppu' ? ((int)$aBefore['customers'] ? (float)$aBefore['net'] / (int)$aBefore['customers'] : 0) : (float)$aBefore[$sKey];
  1801. $aTiles[] = [
  1802. 'key' => $sKey,
  1803. 'title' => _t('_bx_analytics_metric_revenue_' . $sKey),
  1804. 'value' => $sType == 'money' ? round($fValue, 2) : (int)$fValue,
  1805. 'previous' => $sType == 'money' ? round($fPrevious, 2) : (int)$fPrevious,
  1806. 'change' => $this->_change($fValue, $fPrevious),
  1807. 'type' => $sType,
  1808. 'metric' => $sKey,
  1809. ];
  1810. }
  1811. $aRows = $this->_oDb->getRevenueSeries($aFilter);
  1812. $aSeries = ['gross' => [], 'net' => [], 'refunds' => []];
  1813. foreach($aFilter['buckets'] as $iBucket)
  1814. foreach(array_keys($aSeries) as $sKey)
  1815. $aSeries[$sKey][] = $iBucket > $aFilter['now'] ? null : (isset($aRows[$iBucket]) ? round((float)$aRows[$iBucket][$sKey], 2) : 0);
  1816. $aDatasets = [];
  1817. foreach($aSeries as $sKey => $aValues)
  1818. $aDatasets[] = ['key' => $sKey, 'label' => _t('_bx_analytics_metric_revenue_' . $sKey), 'values' => $aValues];
  1819. $aTabs = $this->_aReportTabs['revenue'];
  1820. $sTab = $this->_getTab($aParams, 'revenue', $aTabs);
  1821. $fNet = (float)$aCurrent['net'];
  1822. $aSplit = [];
  1823. foreach($this->_oDb->getRevenueSplit($aFilter, $aTabs[$sTab], 25) as $aRow) {
  1824. $fRowNet = (float)$aRow['net'];
  1825. $aSplit[] = [
  1826. 'label' => $this->getRevenueLabel($aTabs[$sTab], (string)$aRow['value'], $aMoney['payment']),
  1827. 'bar' => $fNet > 0 ? max(0, round(100 * $fRowNet / $fNet, 1)) : 0,
  1828. 'values' => [
  1829. 'net' => round($fRowNet, 2),
  1830. 'share' => $fNet > 0 ? round(100 * $fRowNet / $fNet, 1) : 0,
  1831. 'customers' => (int)$aRow['customers'],
  1832. 'orders' => (int)$aRow['orders'],
  1833. ],
  1834. ];
  1835. }
  1836. return [
  1837. 'money' => $aMoney,
  1838. 'tiles' => $aTiles,
  1839. 'chart' => [
  1840. 'kind' => 'lines',
  1841. 'metric' => 'money',
  1842. 'currency' => $aMoney['code'],
  1843. 'labels' => $aFilter['buckets'],
  1844. 'bucket' => $aFilter['bucket'],
  1845. 'tz' => $aFilter['tz'],
  1846. 'datasets' => $aDatasets,
  1847. ],
  1848. 'split' => [
  1849. 'name' => 'revenue',
  1850. 'caption' => _t('_bx_analytics_txt_revenue_split'),
  1851. 'tab' => $sTab,
  1852. 'tabs' => $this->_getTabs($aTabs, $sTab, '_bx_analytics_tab_revenue_'),
  1853. 'columns' => [
  1854. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_tab_revenue_' . $sTab)],
  1855. ['key' => 'net', 'type' => 'money', 'title' => _t('_bx_analytics_metric_revenue_net')],
  1856. ['key' => 'share', 'type' => 'percent', 'title' => _t('_bx_analytics_txt_share')],
  1857. ['key' => 'customers', 'type' => 'number', 'title' => _t('_bx_analytics_metric_revenue_customers')],
  1858. ['key' => 'orders', 'type' => 'number', 'title' => _t('_bx_analytics_metric_revenue_orders')],
  1859. ],
  1860. 'rows' => $aSplit,
  1861. 'empty' => _t('_bx_analytics_txt_no_revenue'),
  1862. ],
  1863. 'notes' => $this->_getRevenueNotes($aMoney, (int)$aCurrent['unconverted']),
  1864. 'import' => $this->_getImportStatus(),
  1865. ];
  1866. }
  1867. /**
  1868. * Monthly recurring revenue, new, renewed and cancelled subscriptions and churn per
  1869. * bucket, the plans that bring the MRR, and why members cancel.
  1870. */
  1871. public function getSubscriptions(BxAnalyticsFilter $oFilter, $aParams = [])
  1872. {
  1873. $aMoney = $this->getMoney();
  1874. $aFilter = $this->_getRevenueFilter($oFilter->get(), $aMoney);
  1875. $aPrevious = $this->_getRevenueFilter($oFilter->getPrevious(), $aMoney);
  1876. $iNow = (int)$aFilter['now'];
  1877. bx_import('Revenue', $this->_oModule->_aModule);
  1878. // subscriptions alive in either period, read once
  1879. $aRows = $this->_oDb->getSubscriptionRows(array_merge($aFilter, ['from' => $aPrevious['from']]), [
  1880. 'unit_seconds' => BxAnalyticsRevenue::getUnitSeconds(),
  1881. 'unpaid' => BxAnalyticsRevenue::STATUS_UNPAID,
  1882. 'grace' => BxAnalyticsRevenue::UNPAID_GRACE,
  1883. ]);
  1884. list($aCurrent) = $this->_sweepSubscriptions($aRows, [[$aFilter['from'], min($aFilter['to'], $iNow)]]);
  1885. list($aBefore) = $this->_sweepSubscriptions($aRows, [[$aPrevious['from'], min($aPrevious['to'], $iNow)]]);
  1886. $aTiles = [];
  1887. foreach(['mrr' => 'money', 'active' => 'number', 'trials' => 'number', 'new' => 'number', 'cancelled' => 'number', 'churn' => 'percent'] as $sKey => $sType) {
  1888. $sSub = '';
  1889. if($sKey == 'trials' && $aCurrent['trial_mrr'] > 0)
  1890. $sSub = _t('_bx_analytics_txt_sbs_trial_mrr', _t_format_currency($aCurrent['trial_mrr']));
  1891. else if($sKey == 'churn' && $aCurrent['churn'] === null)
  1892. $sSub = _t('_bx_analytics_txt_sbs_churn_no_base');
  1893. $aTiles[] = [
  1894. 'key' => $sKey,
  1895. 'title' => _t('_bx_analytics_metric_sbs_' . $sKey),
  1896. 'value' => $aCurrent[$sKey],
  1897. 'previous' => $aBefore[$sKey],
  1898. 'change' => $aCurrent[$sKey] !== null && $aBefore[$sKey] !== null ? $this->_change($aCurrent[$sKey], $aBefore[$sKey]) : null,
  1899. 'type' => $sType,
  1900. 'metric' => $sKey,
  1901. 'sub' => $sSub,
  1902. ];
  1903. }
  1904. // per bucket, within the period (the first bucket may start before it); renewals paid
  1905. // with credits too, like the subscriptions
  1906. $aRenewals = $this->_oDb->getRevenueSeries($aFilter, null);
  1907. $aBuckets = $aFilter['buckets'];
  1908. $aSpans = [];
  1909. foreach($aBuckets as $i => $iBucket)
  1910. if($iBucket <= $iNow)
  1911. $aSpans[$i] = [max($iBucket, $aFilter['from']), min(isset($aBuckets[$i + 1]) ? $aBuckets[$i + 1] : $aFilter['to'], $iNow)];
  1912. $aSwept = array_combine(array_keys($aSpans), $this->_sweepSubscriptions($aRows, $aSpans));
  1913. $aMrr = $aNew = $aRenewed = $aCancelled = [];
  1914. foreach($aBuckets as $i => $iBucket) {
  1915. if(!isset($aSwept[$i])) {
  1916. $aMrr[] = $aNew[] = $aRenewed[] = $aCancelled[] = null;
  1917. continue;
  1918. }
  1919. $aMrr[] = $aSwept[$i]['mrr'];
  1920. $aNew[] = $aSwept[$i]['new'];
  1921. $aCancelled[] = $aSwept[$i]['cancelled'];
  1922. $aRenewed[] = isset($aRenewals[$iBucket]) ? (int)$aRenewals[$iBucket]['renewals'] : 0;
  1923. }
  1924. // plans paying at the end of the period, by MRR
  1925. $iEnd = min($aFilter['to'], $iNow);
  1926. $aPlans = [];
  1927. foreach($aRows as $aRow) {
  1928. if(!$this->_isPaying($aRow, $iEnd))
  1929. continue;
  1930. $sKey = $aRow['module'] . '|' . (int)$aRow['item_id'];
  1931. if(!isset($aPlans[$sKey]))
  1932. $aPlans[$sKey] = ['module' => $aRow['module'], 'item_id' => (int)$aRow['item_id'], 'active' => 0, 'mrr' => 0];
  1933. $aPlans[$sKey]['active']++;
  1934. $aPlans[$sKey]['mrr'] += (float)$aRow['mrr_default'];
  1935. }
  1936. uasort($aPlans, function($a, $b) { return $b['mrr'] <=> $a['mrr']; });
  1937. $aPlanRows = [];
  1938. foreach(array_slice($aPlans, 0, 10) as $aPlan) {
  1939. $aItem = $this->_getCartItem($aPlan['module'], $aPlan['item_id']);
  1940. $aPlanRows[] = [
  1941. 'label' => $aItem['title'],
  1942. 'sub' => $aItem['module_title'],
  1943. 'link' => $aItem['url'],
  1944. 'bar' => $aCurrent['mrr'] > 0 ? round(100 * $aPlan['mrr'] / $aCurrent['mrr'], 1) : 0,
  1945. 'values' => ['active' => $aPlan['active'], 'mrr' => round($aPlan['mrr'], 2)],
  1946. ];
  1947. }
  1948. $aReasons = $this->_oDb->getSubscriptionReasons($aFilter, 10);
  1949. $iReasons = array_sum($aReasons);
  1950. $aReasonRows = [];
  1951. foreach($aReasons as $sReason => $iCount)
  1952. $aReasonRows[] = [
  1953. 'label' => $sReason !== '' && $sReason !== 'cancel' ? $sReason : _t('_bx_analytics_txt_sbs_no_reason'),
  1954. 'bar' => $iReasons ? round(100 * $iCount / $iReasons, 1) : 0,
  1955. 'values' => ['count' => (int)$iCount, 'share' => $iReasons ? round(100 * $iCount / $iReasons, 1) : 0],
  1956. ];
  1957. $aLabels = $aFilter['buckets'];
  1958. return [
  1959. 'money' => $aMoney,
  1960. 'tiles' => $aTiles,
  1961. 'chart' => [
  1962. 'kind' => 'lines',
  1963. 'metric' => 'money',
  1964. 'currency' => $aMoney['code'],
  1965. 'labels' => $aLabels,
  1966. 'bucket' => $aFilter['bucket'],
  1967. 'tz' => $aFilter['tz'],
  1968. 'datasets' => [['key' => 'mrr', 'label' => _t('_bx_analytics_metric_sbs_mrr'), 'values' => $aMrr]],
  1969. ],
  1970. 'movement' => [
  1971. 'kind' => 'stacked',
  1972. 'metric' => 'number',
  1973. 'labels' => $aLabels,
  1974. 'bucket' => $aFilter['bucket'],
  1975. 'tz' => $aFilter['tz'],
  1976. 'datasets' => [
  1977. ['key' => 'new', 'label' => _t('_bx_analytics_metric_sbs_new'), 'values' => $aNew],
  1978. ['key' => 'renewed', 'label' => _t('_bx_analytics_metric_sbs_renewed'), 'values' => $aRenewed],
  1979. ['key' => 'cancelled', 'label' => _t('_bx_analytics_metric_sbs_cancelled'), 'values' => $aCancelled],
  1980. ],
  1981. ],
  1982. 'plans' => [
  1983. 'name' => 'sbs_plans',
  1984. 'caption' => _t('_bx_analytics_txt_sbs_plans'),
  1985. 'columns' => [
  1986. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_txt_sbs_plan')],
  1987. ['key' => 'active', 'type' => 'number', 'title' => _t('_bx_analytics_metric_sbs_active')],
  1988. ['key' => 'mrr', 'type' => 'money', 'title' => _t('_bx_analytics_metric_sbs_mrr')],
  1989. ],
  1990. 'rows' => $aPlanRows,
  1991. 'empty' => _t('_bx_analytics_txt_no_subscriptions'),
  1992. ],
  1993. 'reasons' => [
  1994. 'name' => 'sbs_reasons',
  1995. 'caption' => _t('_bx_analytics_txt_sbs_reasons'),
  1996. 'columns' => [
  1997. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_txt_sbs_reason')],
  1998. ['key' => 'count', 'type' => 'number', 'title' => _t('_bx_analytics_metric_sbs_cancelled')],
  1999. ['key' => 'share', 'type' => 'percent', 'title' => _t('_bx_analytics_txt_share')],
  2000. ],
  2001. 'rows' => $aReasonRows,
  2002. 'empty' => _t('_bx_analytics_txt_no_cancellations'),
  2003. ],
  2004. 'notes' => $this->_getRevenueNotes($aMoney, 0),
  2005. ];
  2006. }
  2007. /**
  2008. * The items that sell best, the sellers of a multi-vendor site, the site's own sales
  2009. * against its vendors' and the commissions they paid, and commission invoices.
  2010. */
  2011. public function getSales(BxAnalyticsFilter $oFilter, $aParams = [])
  2012. {
  2013. $aMoney = $this->getMoney();
  2014. $aFilter = $this->_getRevenueFilter($oFilter->get(), $aMoney);
  2015. $aTotals = $this->_oDb->getRevenueTotals($aFilter);
  2016. $fNet = (float)$aTotals['net'];
  2017. $aItems = [];
  2018. foreach($this->_oDb->getRevenueTopItems($aFilter, 10) as $aRow) {
  2019. $aItem = $this->_getCartItem($aRow['module'], (int)$aRow['item_id']);
  2020. $aItems[] = [
  2021. 'label' => $aItem['title'],
  2022. 'sub' => $aItem['module_title'],
  2023. 'link' => $aItem['url'],
  2024. 'bar' => $fNet > 0 ? max(0, round(100 * $aRow['net'] / $fNet, 1)) : 0,
  2025. 'values' => [
  2026. 'net' => round((float)$aRow['net'], 2),
  2027. 'units' => (int)$aRow['units'],
  2028. 'orders' => (int)$aRow['orders'],
  2029. 'renewals' => (int)$aRow['renewals'],
  2030. ],
  2031. ];
  2032. }
  2033. $aSellers = [];
  2034. foreach($this->_oDb->getRevenueSplit($aFilter, 'seller_id', 10) as $aRow) {
  2035. $aMember = $this->getMember((int)$aRow['value']);
  2036. $aSellers[] = [
  2037. 'label' => $aMember ? $aMember['name'] : _t('_bx_analytics_txt_deleted_member'),
  2038. 'link' => $aMember ? $aMember['url'] : '',
  2039. 'bar' => $fNet > 0 ? max(0, round(100 * $aRow['net'] / $fNet, 1)) : 0,
  2040. 'values' => [
  2041. 'net' => round((float)$aRow['net'], 2),
  2042. 'orders' => (int)$aRow['orders'],
  2043. 'customers' => (int)$aRow['customers'],
  2044. ],
  2045. ];
  2046. }
  2047. $iSiteAdmin = (int)BxDolPayments::getInstance()->getOption('site_admin');
  2048. $aSplit = $this->_oDb->getRevenueSellers($aFilter, $iSiteAdmin);
  2049. $aTiles = [
  2050. ['title' => _t('_bx_analytics_metric_revenue_net'), 'value' => round($fNet, 2), 'type' => 'money'],
  2051. ];
  2052. if($iSiteAdmin)
  2053. $aTiles[] = ['title' => _t('_bx_analytics_metric_sales_site'), 'value' => round((float)$aSplit['site'], 2), 'type' => 'money'];
  2054. $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'])];
  2055. $aTiles[] = ['title' => _t('_bx_analytics_metric_sales_commissions'), 'value' => round((float)$aSplit['commissions'], 2), 'type' => 'money'];
  2056. $aInvoices = [];
  2057. foreach($this->getRevenueSources(['from' => $aFilter['from'], 'to' => $aFilter['to']], 'money') as $aSource)
  2058. foreach($aSource['metrics'] as $aMetric)
  2059. $aInvoices[] = [
  2060. 'title' => _t($aMetric['title']),
  2061. 'value' => isset($aMetric['value']) ? $aMetric['value'] : 0,
  2062. 'type' => 'number',
  2063. 'sub' => isset($aMetric['money']) && $aMetric['money'] !== null ? _t_format_currency($aMetric['money']) : '',
  2064. ];
  2065. return [
  2066. 'money' => $aMoney,
  2067. 'tiles' => $aTiles,
  2068. 'items' => [
  2069. 'name' => 'sales_items',
  2070. 'caption' => _t('_bx_analytics_txt_sales_items'),
  2071. 'columns' => [
  2072. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_txt_item')],
  2073. ['key' => 'net', 'type' => 'money', 'title' => _t('_bx_analytics_metric_revenue_net')],
  2074. ['key' => 'units', 'type' => 'number', 'title' => _t('_bx_analytics_metric_sales_units')],
  2075. ['key' => 'renewals', 'type' => 'number', 'title' => _t('_bx_analytics_metric_sbs_renewed')],
  2076. ],
  2077. 'rows' => $aItems,
  2078. 'empty' => _t('_bx_analytics_txt_no_revenue'),
  2079. ],
  2080. 'sellers' => [
  2081. 'name' => 'sales_sellers',
  2082. 'caption' => _t('_bx_analytics_txt_sales_sellers'),
  2083. 'columns' => [
  2084. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_txt_sales_seller')],
  2085. ['key' => 'net', 'type' => 'money', 'title' => _t('_bx_analytics_metric_revenue_net')],
  2086. ['key' => 'orders', 'type' => 'number', 'title' => _t('_bx_analytics_metric_revenue_orders')],
  2087. ['key' => 'customers', 'type' => 'number', 'title' => _t('_bx_analytics_metric_revenue_customers')],
  2088. ],
  2089. 'rows' => $aSellers,
  2090. 'empty' => _t('_bx_analytics_txt_no_revenue'),
  2091. ],
  2092. 'invoices' => $aInvoices,
  2093. 'notes' => $this->_getRevenueNotes($aMoney, (int)$aTotals['unconverted']),
  2094. ];
  2095. }
  2096. /**
  2097. * The credits economy as the modules that keep credits report it: bought, spent, granted,
  2098. * sent and withdrawn, the top spenders and the credits members hold; and from the ledger,
  2099. * what credits paid for, by module, valued in money. Those modules don't know the
  2100. * platform, so the whole block is for every platform, the ledger's figures too.
  2101. */
  2102. public function getCredits(BxAnalyticsFilter $oFilter, $aParams = [])
  2103. {
  2104. $aMoney = $this->getMoney();
  2105. $aFilter = $this->_getRevenueFilter($oFilter->get(), $aMoney);
  2106. $bPlatform = $aFilter['platform'] !== '';
  2107. $aFilter['platform'] = '';
  2108. $aSources = $this->getRevenueSources([
  2109. 'from' => $aFilter['from'],
  2110. 'to' => $aFilter['to'],
  2111. 'starts' => $aFilter['buckets'],
  2112. 'limit' => 10,
  2113. ], 'credits');
  2114. $aSource = reset($aSources);
  2115. $sSource = (string)key($aSources);
  2116. $aTiles = $aDatasets = $aSpenders = [];
  2117. if($aSource) {
  2118. foreach($aSource['metrics'] as $sKey => $aMetric) {
  2119. $aTiles[] = [
  2120. 'key' => $sKey,
  2121. 'title' => _t($aMetric['title']),
  2122. 'value' => (float)$aMetric['value'],
  2123. 'type' => 'credits',
  2124. 'sub' => isset($aMetric['money']) && $aMetric['money'] !== null && (float)$aMetric['value'] > 0 ? _t_format_currency($aMetric['money']) : '',
  2125. ];
  2126. if(in_array($sKey, ['bought', 'spent', 'granted', 'withdrawn'], true))
  2127. $aDatasets[] = [
  2128. 'key' => $sKey,
  2129. 'label' => _t($aMetric['title']),
  2130. 'values' => array_map(function($iBucket) use ($aMetric, $aFilter) {
  2131. return $iBucket > $aFilter['now'] ? null : (isset($aMetric['series'][$iBucket]) ? (float)$aMetric['series'][$iBucket] : 0);
  2132. }, $aFilter['buckets']),
  2133. ];
  2134. }
  2135. if(!empty($aSource['snapshot']['balance'])) {
  2136. $aBalance = $aSource['snapshot']['balance'];
  2137. $aTiles[] = [
  2138. 'key' => 'balance',
  2139. 'title' => _t($aBalance['title']),
  2140. 'value' => (float)$aBalance['value'],
  2141. 'type' => 'credits',
  2142. 'sub' => _t('_bx_analytics_txt_credits_liability', _t_format_currency(isset($aBalance['money_default']) ? $aBalance['money_default'] : 0)),
  2143. ];
  2144. }
  2145. if(!empty($aSource['lists']['top_spenders']['rows'])) {
  2146. $aList = $aSource['lists']['top_spenders']['rows'];
  2147. $fMax = 0;
  2148. foreach($aList as $aRow)
  2149. $fMax = max($fMax, (float)$aRow['value']);
  2150. foreach($aList as $aRow) {
  2151. $aMember = $this->getMember(isset($aRow['profile_id']) ? (int)$aRow['profile_id'] : 0);
  2152. $aSpenders[] = [
  2153. 'label' => $aMember ? $aMember['name'] : (isset($aRow['label']) ? (string)$aRow['label'] : _t('_bx_analytics_txt_deleted_member')),
  2154. 'link' => $aMember ? $aMember['url'] : '',
  2155. 'bar' => $fMax > 0 ? round(100 * $aRow['value'] / $fMax, 1) : 0,
  2156. 'values' => ['value' => (float)$aRow['value']],
  2157. ];
  2158. }
  2159. }
  2160. }
  2161. // what orders paid with credits bought, valued in money
  2162. $aSpentRows = [];
  2163. $aSpent = $this->_oDb->getRevenueSplit($aFilter, 'module', 10, 0);
  2164. $fSpent = 0;
  2165. foreach($aSpent as $aRow)
  2166. $fSpent += max(0, (float)$aRow['net']);
  2167. foreach($aSpent as $aRow)
  2168. $aSpentRows[] = [
  2169. 'label' => $this->getRevenueLabel('module', (string)$aRow['value'], $aMoney['payment']),
  2170. 'bar' => $fSpent > 0 ? max(0, round(100 * $aRow['net'] / $fSpent, 1)) : 0,
  2171. 'values' => ['net' => round((float)$aRow['net'], 2), 'orders' => (int)$aRow['orders']],
  2172. ];
  2173. // credit bundles sold for money
  2174. $fBundles = 0;
  2175. if($sSource !== '')
  2176. foreach($this->_oDb->getRevenueSplit($aFilter, 'module', 1000) as $aRow)
  2177. if($aRow['value'] === $sSource)
  2178. $fBundles = (float)$aRow['net'];
  2179. if($aSource)
  2180. $aTiles[] = ['key' => 'bundles', 'title' => _t('_bx_analytics_metric_credits_bundles'), 'value' => round($fBundles, 2), 'type' => 'money'];
  2181. $aNotes = [];
  2182. if($aMoney['credits_only'])
  2183. $aNotes[] = _t('_bx_analytics_txt_revenue_credits_only');
  2184. if($aSource && $bPlatform)
  2185. $aNotes[] = _t('_bx_analytics_txt_credits_all_platforms');
  2186. // checkouts the ledger doesn't have: paid before it started, or not imported yet
  2187. if(empty($aSpentRows) && !empty($aSource['metrics']['spent']['value']) && (float)$aSource['metrics']['spent']['value'] > 0)
  2188. $aNotes[] = _t('_bx_analytics_txt_credits_not_in_ledger');
  2189. return [
  2190. 'money' => $aMoney,
  2191. 'available' => !empty($aSource),
  2192. 'title' => $aSource ? _t($aSource['title']) : '',
  2193. 'tiles' => $aTiles,
  2194. 'chart' => [
  2195. 'kind' => 'lines',
  2196. 'metric' => 'number',
  2197. 'labels' => $aFilter['buckets'],
  2198. 'bucket' => $aFilter['bucket'],
  2199. 'tz' => $aFilter['tz'],
  2200. 'datasets' => $aDatasets,
  2201. ],
  2202. 'spenders' => [
  2203. 'name' => 'credits_spenders',
  2204. 'caption' => _t('_bx_analytics_txt_credits_spenders'),
  2205. 'columns' => [
  2206. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_txt_member_name')],
  2207. ['key' => 'value', 'type' => 'credits', 'title' => _t('_bx_analytics_txt_credits_spent')],
  2208. ],
  2209. 'rows' => $aSpenders,
  2210. 'empty' => _t('_bx_analytics_txt_no_credits'),
  2211. ],
  2212. 'spent' => [
  2213. 'name' => 'credits_spent',
  2214. 'caption' => _t('_bx_analytics_txt_credits_spent_on'),
  2215. 'columns' => [
  2216. ['key' => 'label', 'type' => 'label', 'title' => _t('_bx_analytics_tab_revenue_sources')],
  2217. ['key' => 'net', 'type' => 'money', 'title' => _t('_bx_analytics_txt_credits_value')],
  2218. ['key' => 'orders', 'type' => 'number', 'title' => _t('_bx_analytics_metric_revenue_orders')],
  2219. ],
  2220. 'rows' => $aSpentRows,
  2221. 'empty' => _t('_bx_analytics_txt_no_credit_orders'),
  2222. ],
  2223. 'notes' => $aNotes,
  2224. ];
  2225. }
  2226. /**
  2227. * The site's default currency and the payment module the ledger is read for.
  2228. */
  2229. public function getMoney()
  2230. {
  2231. $oPayments = BxDolPayments::getInstance();
  2232. $bActive = $oPayments->isActive();
  2233. return [
  2234. 'active' => $bActive,
  2235. 'payment' => $bActive ? (string)$oPayments->getActive() : '',
  2236. 'code' => $bActive ? (string)$oPayments->getCurrencyCode() : '',
  2237. 'credits_only' => $bActive && (bool)$oPayments->isCreditsOnly(),
  2238. ];
  2239. }
  2240. /**
  2241. * What modules report about their own economy through the get_analytics_revenue service,
  2242. * found in every installed module: [module name => report], the ones of a unit only. The
  2243. * unit is read first (meta), so a module of another unit doesn't compute its report.
  2244. *
  2245. * @param array $aParams from, to, starts, limit; or meta to learn titles and units only
  2246. * @param string $sUnit money or credits, '' for any
  2247. */
  2248. public function getRevenueSources($aParams, $sUnit = '')
  2249. {
  2250. $aResult = [];
  2251. foreach(BxDolModuleQuery::getInstance()->getModulesBy(['type' => 'modules', 'active' => 1]) as $aModule) {
  2252. if(empty($aModule['name']) || !bx_is_srv($aModule['name'], 'get_analytics_revenue'))
  2253. continue;
  2254. if($sUnit !== '' && empty($aParams['meta'])) {
  2255. $aMeta = bx_srv($aModule['name'], 'get_analytics_revenue', [['meta' => true]]);
  2256. if(empty($aMeta['unit']) || $aMeta['unit'] != $sUnit)
  2257. continue;
  2258. }
  2259. $aReport = bx_srv($aModule['name'], 'get_analytics_revenue', [$aParams]);
  2260. if(empty($aReport) || !is_array($aReport) || empty($aReport['unit']) || ($sUnit !== '' && $aReport['unit'] != $sUnit))
  2261. continue;
  2262. if(empty($aParams['meta']) && (empty($aReport['metrics']) || !is_array($aReport['metrics'])))
  2263. continue;
  2264. $aResult[$aModule['name']] = $aReport;
  2265. }
  2266. return $aResult;
  2267. }
  2268. /**
  2269. * A human name for a ledger value: a module's title, a provider, a platform, a channel.
  2270. */
  2271. public function getRevenueLabel($sDimension, $sValue, $sPayment = '')
  2272. {
  2273. switch($sDimension) {
  2274. case 'module':
  2275. if($sValue === '')
  2276. return _t('_bx_analytics_txt_unknown');
  2277. if($sPayment !== '' && $sValue === $sPayment)
  2278. return _t('_bx_analytics_txt_revenue_commissions');
  2279. return $this->_getModuleTitle($sValue);
  2280. case 'provider':
  2281. if($sValue === '')
  2282. return _t('_bx_analytics_txt_unknown');
  2283. $sValue = str_replace('_', ' ', $sValue);
  2284. return mb_strtoupper(get_mb_substr($sValue, 0, 1)) . get_mb_substr($sValue, 1, get_mb_len($sValue) - 1);
  2285. case 'platform':
  2286. return $sValue !== '' ? $this->getLabel('platform', $sValue) : _t('_bx_analytics_txt_revenue_imported');
  2287. case 'first_channel':
  2288. case 'last_channel':
  2289. return $sValue !== '' ? $this->getLabel('channel', $sValue) : _t('_bx_analytics_txt_revenue_untracked');
  2290. case 'last_utm_campaign':
  2291. case 'first_utm_campaign':
  2292. return $sValue !== '' ? $sValue : _t('_bx_analytics_txt_revenue_no_campaign');
  2293. }
  2294. return $sValue;
  2295. }
  2296. /**
  2297. * Net revenue of members over all the ledger, for the Users report: [profile id => amount].
  2298. */
  2299. public function getLifetimeValues($aProfileIds)
  2300. {
  2301. $aMoney = $this->getMoney();
  2302. if($aMoney['payment'] === '')
  2303. return [];
  2304. return $this->_oDb->getLifetimeValues($aProfileIds, $aMoney['payment']);
  2305. }
  2306. /**
  2307. * Subscription figures over spans of time, sorted and not overlapping. For each span: MRR
  2308. * and paying subscriptions at its end, trials at its end, new and ended (cancelled, or
  2309. * unpaid past the grace time) paying subscriptions in it, and churn (of those paying at
  2310. * its start, the share that ended in it). One pass over the rows, each finding its spans
  2311. * by binary search.
  2312. *
  2313. * @param array $aSpans [[from, to], ...]
  2314. * @return array a result per span, in the order of $aSpans
  2315. */
  2316. protected function _sweepSubscriptions($aRows, $aSpans)
  2317. {
  2318. $aSpans = array_values($aSpans);
  2319. $iSpans = count($aSpans);
  2320. $aFroms = array_map('intval', array_column($aSpans, 0));
  2321. $aTos = array_map('intval', array_column($aSpans, 1));
  2322. // counts at the spans' ends (or starts) are ranges of spans: added as differences
  2323. $aDiff = array_fill_keys(['mrr', 'active', 'trials', 'trial_mrr', 'at_start'], array_fill(0, $iSpans + 1, 0));
  2324. $aNew = $aEnded = $aChurned = array_fill(0, $iSpans, 0);
  2325. foreach($aRows as $aRow) {
  2326. $iFirst = (int)$aRow['first_paid'];
  2327. $iEnd = $this->_getSubscriptionEnd($aRow);
  2328. $fMrr = (float)$aRow['mrr_default'];
  2329. if($iFirst) {
  2330. $this->_addSpans($aDiff['mrr'], $aTos, $iFirst, $iEnd, $fMrr);
  2331. $this->_addSpans($aDiff['active'], $aTos, $iFirst, $iEnd, 1);
  2332. $this->_addSpans($aDiff['at_start'], $aFroms, $iFirst, $iEnd, 1);
  2333. if(($i = $this->_findSpan($aFroms, $aTos, $iFirst)) !== null)
  2334. $aNew[$i]++;
  2335. if($iEnd && ($i = $this->_findSpan($aFroms, $aTos, $iEnd)) !== null) {
  2336. $aEnded[$i]++;
  2337. if($iFirst < $aFroms[$i])
  2338. $aChurned[$i]++;
  2339. }
  2340. }
  2341. // in trial from its start until the trial, its first charge or its end is over
  2342. $iTrialEnd = $this->_getTrialEnd($aRow);
  2343. if($iTrialEnd !== null) {
  2344. $iUntil = 0;
  2345. foreach([$iTrialEnd, $iFirst, $iEnd] as $iTime)
  2346. if($iTime)
  2347. $iUntil = $iUntil ? min($iUntil, $iTime) : $iTime;
  2348. $this->_addSpans($aDiff['trials'], $aTos, (int)$aRow['started'], $iUntil, 1);
  2349. $this->_addSpans($aDiff['trial_mrr'], $aTos, (int)$aRow['started'], $iUntil, $fMrr);
  2350. }
  2351. }
  2352. $aResults = [];
  2353. $aSum = array_fill_keys(array_keys($aDiff), 0);
  2354. for($i = 0; $i < $iSpans; $i++) {
  2355. foreach($aDiff as $sKey => $aValues)
  2356. $aSum[$sKey] += $aValues[$i];
  2357. if($aTos[$i] <= $aFroms[$i]) {
  2358. $aResults[] = ['mrr' => null, 'active' => null, 'trials' => null, 'trial_mrr' => 0, 'new' => null, 'cancelled' => null, 'churn' => null];
  2359. continue;
  2360. }
  2361. $aResults[] = [
  2362. 'mrr' => round($aSum['mrr'], 2),
  2363. 'active' => (int)$aSum['active'],
  2364. 'trials' => (int)$aSum['trials'],
  2365. 'trial_mrr' => round($aSum['trial_mrr'], 2),
  2366. 'new' => $aNew[$i],
  2367. 'cancelled' => $aEnded[$i],
  2368. 'churn' => $aSum['at_start'] ? round(100 * $aChurned[$i] / $aSum['at_start'], 1) : null,
  2369. ];
  2370. }
  2371. return $aResults;
  2372. }
  2373. /**
  2374. * Add a value to the spans whose edge (from `$aEdges`, ascending) is in (`$iAfter`,
  2375. * `$iUntil`], `$iUntil` 0 for no end.
  2376. */
  2377. protected function _addSpans(&$aDiff, $aEdges, $iAfter, $iUntil, $mixedValue)
  2378. {
  2379. $iLo = self::_firstAbove($aEdges, $iAfter);
  2380. $iHi = $iUntil ? self::_firstAbove($aEdges, $iUntil) : count($aEdges);
  2381. if($iLo >= $iHi)
  2382. return;
  2383. $aDiff[$iLo] += $mixedValue;
  2384. $aDiff[$iHi] -= $mixedValue;
  2385. }
  2386. /**
  2387. * The span a time falls in, or null.
  2388. */
  2389. protected function _findSpan($aFroms, $aTos, $iTime)
  2390. {
  2391. $i = self::_firstAbove($aFroms, $iTime) - 1;
  2392. return $i >= 0 && $iTime < $aTos[$i] ? $i : null;
  2393. }
  2394. /**
  2395. * Index of the first value greater than `$iTime` in an ascending list, or its length.
  2396. */
  2397. protected static function _firstAbove($aList, $iTime)
  2398. {
  2399. $iLo = 0;
  2400. $iHi = count($aList);
  2401. while($iLo < $iHi) {
  2402. $iMid = ($iLo + $iHi) >> 1;
  2403. if($aList[$iMid] > $iTime)
  2404. $iHi = $iMid;
  2405. else
  2406. $iLo = $iMid + 1;
  2407. }
  2408. return $iLo;
  2409. }
  2410. /**
  2411. * A subscription was paying at a time: first paid before it and not ended by then.
  2412. */
  2413. protected function _isPaying($aRow, $iTime)
  2414. {
  2415. $iFirst = (int)$aRow['first_paid'];
  2416. $iEnd = $this->_getSubscriptionEnd($aRow);
  2417. return $iFirst && $iFirst < $iTime && (!$iEnd || $iEnd >= $iTime);
  2418. }
  2419. /**
  2420. * When a subscription stopped paying, or 0: its cancellation, or for an unpaid one the end
  2421. * of its last paid period and the grace time (a failed charge the payment module stopped
  2422. * retrying never cancels it).
  2423. */
  2424. protected function _getSubscriptionEnd($aRow)
  2425. {
  2426. if((int)$aRow['canceled'])
  2427. return (int)$aRow['canceled'];
  2428. if($aRow['status'] != BxAnalyticsRevenue::STATUS_UNPAID || !(int)$aRow['last_paid'])
  2429. return 0;
  2430. return (int)$aRow['last_paid'] + (int)BxAnalyticsRevenue::getPeriodSeconds($aRow['period'], $aRow['period_unit']) + BxAnalyticsRevenue::UNPAID_GRACE;
  2431. }
  2432. /**
  2433. * When a subscription's trial ends (0: a trial of unknown length), or null without a trial.
  2434. */
  2435. protected function _getTrialEnd($aRow)
  2436. {
  2437. if((int)$aRow['trial'] > 0)
  2438. return (int)$aRow['started'] + 86400 * (int)$aRow['trial'];
  2439. return $aRow['status'] == BxAnalyticsRevenue::STATUS_TRIAL ? 0 : null;
  2440. }
  2441. /**
  2442. * Title, link and module of something sold, from its module's get_cart_item.
  2443. */
  2444. protected function _getCartItem($sModule, $iItemId)
  2445. {
  2446. $aResult = [
  2447. 'title' => _t('_bx_analytics_txt_item_n', (int)$iItemId),
  2448. 'url' => '',
  2449. 'module_title' => $sModule !== '' ? $this->_getModuleTitle($sModule) : '',
  2450. ];
  2451. if($sModule === '' || !BxDolModuleQuery::getInstance()->isEnabledByName($sModule) || !bx_is_srv($sModule, 'get_cart_item'))
  2452. return $aResult;
  2453. $aItem = bx_srv($sModule, 'get_cart_item', [(int)$iItemId]);
  2454. if(!empty($aItem) && is_array($aItem)) {
  2455. if(!empty($aItem['title']) && is_string($aItem['title']))
  2456. $aResult['title'] = strip_tags($aItem['title']);
  2457. if(!empty($aItem['url']) && is_string($aItem['url']))
  2458. $aResult['url'] = $aItem['url'];
  2459. }
  2460. return $aResult;
  2461. }
  2462. protected function _getRevenueFilter($aFilter, $aMoney)
  2463. {
  2464. $aFilter['payment'] = $aMoney['payment'];
  2465. return $aFilter;
  2466. }
  2467. protected function _getRevenueNotes($aMoney, $iUnconverted)
  2468. {
  2469. $aNotes = [];
  2470. if(!$aMoney['active'])
  2471. $aNotes[] = _t('_bx_analytics_txt_revenue_no_payment');
  2472. if($aMoney['credits_only'])
  2473. $aNotes[] = _t('_bx_analytics_txt_revenue_credits_only');
  2474. if($iUnconverted > 0)
  2475. $aNotes[] = _t('_bx_analytics_txt_revenue_no_rate', $iUnconverted);
  2476. // no currency without a payment module: the tab still shows, for other revenue sources to come
  2477. if($aMoney['code'] !== '')
  2478. $aNotes[] = _t('_bx_analytics_txt_revenue_as_recorded', $aMoney['code']);
  2479. return $aNotes;
  2480. }
  2481. /**
  2482. * Where the import of past payments stands, for administrators.
  2483. */
  2484. protected function _getImportStatus()
  2485. {
  2486. if(!isAdmin())
  2487. return [];
  2488. $aStatus = $this->_oModule->getRevenue()->getBackfillStatus(true);
  2489. return $aStatus['state'] == 'unavailable' ? [] : $aStatus;
  2490. }
  2491. /**
  2492. * Name, link and picture of a profile, or null when it no longer exists.
  2493. */
  2494. public function getMember($iProfileId)
  2495. {
  2496. if(!$iProfileId)
  2497. return null;
  2498. $oProfile = BxDolProfile::getInstance($iProfileId);
  2499. if(!$oProfile)
  2500. return null;
  2501. return [
  2502. 'id' => (int)$iProfileId,
  2503. 'name' => $oProfile->getDisplayName(),
  2504. 'url' => $oProfile->getUrl(),
  2505. 'thumb' => $oProfile->getThumb(),
  2506. ];
  2507. }
  2508. /**
  2509. * A readable name for an event: a site action with the module it happened in, or a
  2510. * custom event's own name.
  2511. */
  2512. public function getEventLabel($sType, $sName, $sModule)
  2513. {
  2514. $sModuleTitle = $sModule !== '' ? $this->_getModuleTitle($sModule) : '';
  2515. if($sType == BX_ANALYTICS_HIT_UNA) {
  2516. // an action without a title of its own (a module's, or a newer one) by its name
  2517. $sKey = '_bx_analytics_action_' . $sName;
  2518. $sLabel = _t($sKey);
  2519. if($sLabel === $sKey)
  2520. $sLabel = $sName;
  2521. return $sModuleTitle !== '' && $sModule != 'account' ? $sLabel . ' · ' . $sModuleTitle : $sLabel;
  2522. }
  2523. return $sModuleTitle !== '' ? $sName . ' · ' . $sModuleTitle : $sName;
  2524. }
  2525. /**
  2526. * Title of the module an object system belongs to: bx_posts and bx_posts_reactions are both Posts.
  2527. */
  2528. protected function _getModuleTitle($sSystem)
  2529. {
  2530. if($this->_aModuleTitles === null) {
  2531. $this->_aModuleTitles = [];
  2532. foreach(BxDolModuleQuery::getInstance()->getModulesBy(['type' => 'modules', 'active' => 1]) as $aModule)
  2533. $this->_aModuleTitles[$aModule['name']] = $aModule['title'];
  2534. uksort($this->_aModuleTitles, function($a, $b) { return get_mb_len($b) - get_mb_len($a); });
  2535. }
  2536. foreach($this->_aModuleTitles as $sName => $sTitle)
  2537. if($sSystem === $sName || bx_mb_strpos($sSystem, $sName . '_') === 0)
  2538. return $sTitle;
  2539. // core objects, like the friends and follows connections, have their own names
  2540. $sKey = '_bx_analytics_object_' . $sSystem;
  2541. $sTitle = _t($sKey);
  2542. return $sTitle !== $sKey ? $sTitle : $sSystem;
  2543. }
  2544. protected function _parseEvent($sValue)
  2545. {
  2546. $aParts = explode('|', (string)$sValue);
  2547. if(count($aParts) != 3 || !in_array($aParts[0], [BX_ANALYTICS_HIT_EVENT, BX_ANALYTICS_HIT_UNA], true) || $aParts[1] === '')
  2548. return null;
  2549. return ['type' => $aParts[0], 'name' => $aParts[1], 'module' => $aParts[2]];
  2550. }
  2551. protected function _getLocation($aSession)
  2552. {
  2553. $aParts = array_filter([$aSession['city'], $aSession['country'] !== '' ? $this->getLabel('country', $aSession['country']) : '']);
  2554. return !empty($aParts) ? implode(', ', $aParts) : _t('_bx_analytics_txt_unknown');
  2555. }
  2556. protected function _getDevice($aSession)
  2557. {
  2558. $aParts = array_filter([
  2559. $aSession['device'] !== '' ? $this->getLabel('device', $aSession['device']) : '',
  2560. $aSession['browser'],
  2561. trim($aSession['os'] . ' ' . $aSession['os_version']),
  2562. ]);
  2563. return implode(' · ', $aParts);
  2564. }
  2565. protected function _getPlatformsText($sPlatforms)
  2566. {
  2567. $aLabels = [];
  2568. foreach(explode(',', (string)$sPlatforms) as $sPlatform)
  2569. if($sPlatform !== '')
  2570. $aLabels[] = $this->getLabel('platform', $sPlatform);
  2571. return implode(', ', $aLabels);
  2572. }
  2573. /**
  2574. * A human name for a stored dimension value.
  2575. */
  2576. public function getLabel($sDimension, $sValue)
  2577. {
  2578. if($sValue === '')
  2579. return _t($sDimension == 'referrer_host' || $sDimension == 'referrer' ? '_bx_analytics_txt_direct' : '_bx_analytics_txt_unknown');
  2580. switch($sDimension) {
  2581. case 'platform':
  2582. $aPlatforms = $this->_oConfig->getPlatforms();
  2583. return isset($aPlatforms[$sValue]) ? _t($aPlatforms[$sValue]) : $sValue;
  2584. case 'channel':
  2585. return _t('_bx_analytics_channel_' . $sValue);
  2586. case 'device':
  2587. return _t('_bx_analytics_device_' . $sValue);
  2588. case 'country':
  2589. if($this->_aCountries === null)
  2590. $this->_aCountries = BxDolFormQuery::getDataItems('Country');
  2591. return isset($this->_aCountries[$sValue]) ? $this->_aCountries[$sValue] : $sValue;
  2592. }
  2593. return $sValue;
  2594. }
  2595. /**
  2596. * Report metrics from summed session columns.
  2597. */
  2598. protected function _getKpis($aRow)
  2599. {
  2600. $iVisitors = isset($aRow['visitors']) ? (int)$aRow['visitors'] : 0;
  2601. $iSessions = isset($aRow['sessions']) ? (int)$aRow['sessions'] : 0;
  2602. $iPageviews = isset($aRow['pageviews']) ? (int)$aRow['pageviews'] : 0;
  2603. $iBounces = isset($aRow['bounces']) ? (int)$aRow['bounces'] : 0;
  2604. $iDuration = isset($aRow['duration']) ? (int)$aRow['duration'] : 0;
  2605. return [
  2606. 'visitors' => $iVisitors,
  2607. 'sessions' => $iSessions,
  2608. 'pageviews' => $iPageviews,
  2609. 'pages_per_session' => $iSessions ? round($iPageviews / $iSessions, 2) : 0,
  2610. 'bounce_rate' => $iSessions ? round(100 * $iBounces / $iSessions, 1) : 0,
  2611. 'duration' => $iSessions ? (int)round($iDuration / $iSessions) : 0,
  2612. ];
  2613. }
  2614. /**
  2615. * The tab a report shows: `tab`, or `tab_<name>` from the page URL, or the first one.
  2616. */
  2617. protected function _getTab($aParams, $sName, $aTabs)
  2618. {
  2619. $sTab = $this->_param($aParams, 'tab', $this->_param($aParams, 'tab_' . $sName, ''));
  2620. return isset($aTabs[$sTab]) ? $sTab : key($aTabs);
  2621. }
  2622. protected function _getTabs($aTabs, $sActive, $sKeyPrefix = '_bx_analytics_tab_')
  2623. {
  2624. $aResult = [];
  2625. foreach(array_keys($aTabs) as $sName)
  2626. $aResult[] = ['name' => $sName, 'title' => _t($sKeyPrefix . $sName), 'active' => $sName == $sActive];
  2627. return $aResult;
  2628. }
  2629. /**
  2630. * A scalar request parameter, or the default.
  2631. */
  2632. protected function _param($aParams, $sKey, $mixedDefault = '')
  2633. {
  2634. return isset($aParams[$sKey]) && is_scalar($aParams[$sKey]) ? $aParams[$sKey] : $mixedDefault;
  2635. }
  2636. /**
  2637. * Change in percent, or null when there is nothing to compare with.
  2638. */
  2639. protected function _change($fCurrent, $fPrevious)
  2640. {
  2641. if(!$fPrevious)
  2642. return null;
  2643. return round(100 * ($fCurrent - $fPrevious) / $fPrevious, 1);
  2644. }
  2645. }
  2646. /** @} */