BxAnalyticsConfig.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. bx_import('BxDolModuleConfig');
  12. define('BX_ANALYTICS_PLATFORM_CORE_WEB', 'core-web');
  13. define('BX_ANALYTICS_PLATFORM_NEXT_WEB', 'next-web');
  14. define('BX_ANALYTICS_PLATFORM_RN_IOS', 'rn-ios');
  15. define('BX_ANALYTICS_PLATFORM_RN_ANDROID', 'rn-android');
  16. define('BX_ANALYTICS_PLATFORM_API', 'api');
  17. define('BX_ANALYTICS_PLATFORM_CORE', 'core');
  18. define('BX_ANALYTICS_IDENTITY_COOKIELESS', 'cookieless');
  19. define('BX_ANALYTICS_IDENTITY_PERSISTENT', 'persistent');
  20. define('BX_ANALYTICS_GEO_NONE', 'none');
  21. define('BX_ANALYTICS_GEO_HEADERS', 'headers');
  22. define('BX_ANALYTICS_GEO_CLOUDFLARE', 'cloudflare');
  23. define('BX_ANALYTICS_GEO_VERCEL', 'vercel');
  24. define('BX_ANALYTICS_GEO_CLOUDFRONT', 'cloudfront');
  25. // hit and event types: pageviews, custom events, page leaves (engaged time), site actions
  26. define('BX_ANALYTICS_HIT_PAGEVIEW', 'pageview');
  27. define('BX_ANALYTICS_HIT_EVENT', 'event');
  28. define('BX_ANALYTICS_HIT_LEAVE', 'leave');
  29. define('BX_ANALYTICS_HIT_UNA', 'una');
  30. // health hits: a client error, and a page's web vitals (or an app screen's load times); they
  31. // are stored apart from events and never start a visit
  32. define('BX_ANALYTICS_HIT_ERROR', 'error');
  33. define('BX_ANALYTICS_HIT_VITALS', 'vitals');
  34. class BxAnalyticsConfig extends BxBaseModGeneralConfig
  35. {
  36. /**
  37. * Where hits come from, as code => language key. Codes are stored with every session and
  38. * event, so they never change; a new client is a new code.
  39. * - core-web: the classic UNA web UI (PHP and HTML)
  40. * - next-web, rn-ios, rn-android: NEO on the web (Next.js) and its React Native apps
  41. * - api: any other client calling api.php directly
  42. * - core: the server itself, with no client request behind it (cron, background jobs)
  43. */
  44. protected $_aPlatforms = [
  45. BX_ANALYTICS_PLATFORM_CORE_WEB => '_bx_analytics_platform_core_web',
  46. BX_ANALYTICS_PLATFORM_NEXT_WEB => '_bx_analytics_platform_next_web',
  47. BX_ANALYTICS_PLATFORM_RN_IOS => '_bx_analytics_platform_rn_ios',
  48. BX_ANALYTICS_PLATFORM_RN_ANDROID => '_bx_analytics_platform_rn_android',
  49. BX_ANALYTICS_PLATFORM_API => '_bx_analytics_platform_api',
  50. BX_ANALYTICS_PLATFORM_CORE => '_bx_analytics_platform_core',
  51. ];
  52. /**
  53. * Platforms that run in a browser, where the user agent tells the browser and the device.
  54. */
  55. protected $_aBrowserPlatforms = [
  56. BX_ANALYTICS_PLATFORM_CORE_WEB,
  57. BX_ANALYTICS_PLATFORM_NEXT_WEB,
  58. ];
  59. /**
  60. * Native apps, which report their system and device themselves.
  61. */
  62. protected $_aAppPlatforms = [
  63. BX_ANALYTICS_PLATFORM_RN_IOS,
  64. BX_ANALYTICS_PLATFORM_RN_ANDROID,
  65. ];
  66. function __construct($aModule)
  67. {
  68. parent::__construct($aModule);
  69. $this->CNF = [
  70. 'TABLE_SESSIONS' => $aModule['db_prefix'] . 'sessions',
  71. 'TABLE_EVENTS' => $aModule['db_prefix'] . 'events',
  72. 'TABLE_SALTS' => $aModule['db_prefix'] . 'salts',
  73. 'TABLE_LIMITS' => $aModule['db_prefix'] . 'limits',
  74. 'TABLE_BOTS' => $aModule['db_prefix'] . 'bots',
  75. 'TABLE_BOT_PAGES' => $aModule['db_prefix'] . 'bot_pages',
  76. 'TABLE_GOALS' => $aModule['db_prefix'] . 'goals',
  77. 'TABLE_FUNNELS' => $aModule['db_prefix'] . 'funnels',
  78. 'TABLE_REVENUE' => $aModule['db_prefix'] . 'revenue',
  79. 'TABLE_SUBSCRIPTIONS' => $aModule['db_prefix'] . 'subscriptions',
  80. 'TABLE_ERRORS' => $aModule['db_prefix'] . 'errors',
  81. 'TABLE_ERROR_HITS' => $aModule['db_prefix'] . 'error_hits',
  82. 'TABLE_VITALS' => $aModule['db_prefix'] . 'vitals',
  83. // import of past payments: the cursors, rows read per service call, and the time
  84. // one step may take in the hourly cron and from the Studio button
  85. 'OPTION_BACKFILL' => 'bx_analytics_revenue_backfill',
  86. 'OPTION_BACKFILL_SBS' => 'bx_analytics_revenue_backfill_sbs',
  87. 'BACKFILL_PAGE' => 500,
  88. 'BACKFILL_SECONDS' => 10,
  89. // funnel steps at most
  90. 'MAX_FUNNEL_STEPS' => 6,
  91. 'MAX_GOALS' => 20,
  92. // Query: values drawn on the chart, and rows in a CSV download at most
  93. 'QUERY_SERIES_VALUES' => 5,
  94. 'MAX_EXPORT_ROWS' => 10000,
  95. // report data is cached this many seconds per report, filters, parameters,
  96. // language and kind of viewer (live reports aren't); goal, funnel and error
  97. // status changes drop the copies
  98. 'REPORT_CACHE_TTL' => 180,
  99. 'REPORT_CACHE_PREFIX' => 'bx_analytics_report_',
  100. // hits per request and their properties, to bound the work of one beacon, and the
  101. // largest request body read at all (20 hits, 10 of them errors with long stacks)
  102. 'MAX_HITS_PER_REQUEST' => 20,
  103. 'MAX_PROPS_LENGTH' => 2048,
  104. 'MAX_COLLECT_BYTES' => 256 * 1024,
  105. // health: error hits per request and per visit, new error groups per hour (more go
  106. // to one catch-all group), how old a deferred error may be (an app that crashed
  107. // sends it on the next launch), and vitals hits per request
  108. 'MAX_ERRORS_PER_REQUEST' => 10,
  109. 'MAX_ERRORS_PER_SESSION' => 50,
  110. 'MAX_NEW_ERROR_GROUPS_PER_HOUR' => 200,
  111. 'MAX_ERROR_AGE' => 7 * 86400,
  112. 'MAX_VITALS_PER_REQUEST' => 1,
  113. // vitals thresholds, metric => [good up to, poor above]: milliseconds, and CLS in
  114. // thousandths (the web.dev and Lighthouse values; app screens and starts by RN guides)
  115. 'VITALS' => [
  116. 'lcp' => [2500, 4000],
  117. 'inp' => [200, 500],
  118. 'cls' => [100, 250],
  119. 'fcp' => [1800, 3000],
  120. 'ttfb' => [800, 1800],
  121. 'screen_load' => [1000, 2500],
  122. 'app_start' => [2000, 4000],
  123. ],
  124. // engaged time above this on one page is a tab left open, not reading
  125. 'MAX_ENGAGED_MS' => 30 * 60 * 1000,
  126. // rows deleted per query when pruning, and the time one pruning run may take
  127. 'PRUNING_BATCH' => 5000,
  128. 'PRUNING_SECONDS' => 20,
  129. ];
  130. $this->_aJsClasses = [
  131. 'analytics' => 'BxAnalytics',
  132. 'dashboard' => 'BxAnalyticsDashboard',
  133. 'tools' => 'BxAnalyticsStudioTools',
  134. ];
  135. $this->_aJsObjects = [
  136. 'analytics' => 'oAnalytics',
  137. 'dashboard' => 'oBxAnalyticsDashboard',
  138. 'tools' => 'oBxAnalyticsStudioTools',
  139. ];
  140. }
  141. public function getPlatforms()
  142. {
  143. return $this->_aPlatforms;
  144. }
  145. public function isPlatform($sPlatform)
  146. {
  147. return is_string($sPlatform) && isset($this->_aPlatforms[$sPlatform]);
  148. }
  149. public function isBrowserPlatform($sPlatform)
  150. {
  151. return in_array($sPlatform, $this->_aBrowserPlatforms, true);
  152. }
  153. public function isAppPlatform($sPlatform)
  154. {
  155. return in_array($sPlatform, $this->_aAppPlatforms, true);
  156. }
  157. public function isTrackingEnabled()
  158. {
  159. return getParam('bx_analytics_tracking') == 'on';
  160. }
  161. public function isPlatformEnabled($sPlatform)
  162. {
  163. return $this->isPlatform($sPlatform) && in_array($sPlatform, $this->_getList('bx_analytics_platforms'), true);
  164. }
  165. public function getSessionTimeout()
  166. {
  167. return 60 * max(1, (int)getParam('bx_analytics_session_timeout'));
  168. }
  169. /**
  170. * Hits the public collector takes from one address in a minute, 0 for no limit.
  171. */
  172. public function getLimitHitsPerMinute()
  173. {
  174. return max(0, (int)getParam('bx_analytics_limit_hits'));
  175. }
  176. /**
  177. * Visits that may start from one address in an hour, 0 for no limit.
  178. */
  179. public function getLimitVisitsPerHour()
  180. {
  181. return max(0, (int)getParam('bx_analytics_limit_visits'));
  182. }
  183. public function getOnlineWindow()
  184. {
  185. return 60 * max(1, (int)getParam('bx_analytics_online_window'));
  186. }
  187. public function getIdentityMode()
  188. {
  189. return getParam('bx_analytics_identity') == BX_ANALYTICS_IDENTITY_PERSISTENT ? BX_ANALYTICS_IDENTITY_PERSISTENT : BX_ANALYTICS_IDENTITY_COOKIELESS;
  190. }
  191. public function isLinkMembers()
  192. {
  193. return getParam('bx_analytics_link_members') == 'on';
  194. }
  195. public function isTrackActions()
  196. {
  197. return getParam('bx_analytics_track_actions') == 'on';
  198. }
  199. public function isTrackBots()
  200. {
  201. return getParam('bx_analytics_track_bots') == 'on';
  202. }
  203. public function isRespectDnt()
  204. {
  205. return getParam('bx_analytics_respect_dnt') == 'on';
  206. }
  207. public function getExcludedLevels()
  208. {
  209. return array_map('intval', $this->_getList('bx_analytics_exclude_levels'));
  210. }
  211. public function getExcludedIps()
  212. {
  213. return $this->_getList('bx_analytics_exclude_ips');
  214. }
  215. public function getExcludedPaths()
  216. {
  217. return $this->_getList('bx_analytics_exclude_paths');
  218. }
  219. /**
  220. * Query parameters left out of stored paths, as patterns with * for any characters.
  221. */
  222. public function getIgnoredParams()
  223. {
  224. return $this->_getList('bx_analytics_ignore_params');
  225. }
  226. public function getTrustedProxies()
  227. {
  228. return $this->_getList('bx_analytics_trusted_proxies');
  229. }
  230. /**
  231. * Where the visitor's location comes from: none, the headers of any CDN (the first one
  232. * found), or the headers of one CDN only (the others can be sent by the client, since an
  233. * edge sets its own headers and passes the rest through).
  234. */
  235. public function getGeoSource()
  236. {
  237. $sSource = getParam('bx_analytics_geo_source');
  238. return in_array($sSource, [BX_ANALYTICS_GEO_HEADERS, BX_ANALYTICS_GEO_CLOUDFLARE, BX_ANALYTICS_GEO_VERCEL, BX_ANALYTICS_GEO_CLOUDFRONT], true) ? $sSource : BX_ANALYTICS_GEO_NONE;
  239. }
  240. /**
  241. * The CDN whose location headers are read ('' for any), or false when the location isn't
  242. * read at all.
  243. *
  244. * @return string|false
  245. */
  246. public function getGeoEdge()
  247. {
  248. $sSource = $this->getGeoSource();
  249. if($sSource == BX_ANALYTICS_GEO_NONE)
  250. return false;
  251. return $sSource == BX_ANALYTICS_GEO_HEADERS ? '' : $sSource;
  252. }
  253. public function getRetentionDays()
  254. {
  255. return max(1, (int)getParam('bx_analytics_retention_days'));
  256. }
  257. public function isTrackErrors()
  258. {
  259. return getParam('bx_analytics_track_errors') == 'on';
  260. }
  261. public function isTrackVitals()
  262. {
  263. return getParam('bx_analytics_track_vitals') == 'on';
  264. }
  265. /**
  266. * Percent of page loads that measure vitals, 0 to 100; the client samples.
  267. */
  268. public function getVitalsSample()
  269. {
  270. return min(100, max(0, (int)getParam('bx_analytics_vitals_sample')));
  271. }
  272. /**
  273. * Error messages that are not stored, one pattern per line with * for any characters.
  274. * Lines, not commas: messages have spaces and commas.
  275. */
  276. public function getErrorsIgnore()
  277. {
  278. $sValue = trim((string)getParam('bx_analytics_errors_ignore'));
  279. if($sValue === '')
  280. return [];
  281. $aLines = preg_split('/\R/u', $sValue);
  282. if($aLines === false)
  283. return [];
  284. return array_values(array_filter(array_map('trim', $aLines), 'get_mb_len'));
  285. }
  286. /**
  287. * Vitals are kept for this many days, at most as long as the rest.
  288. */
  289. public function getVitalsRetentionDays()
  290. {
  291. return min($this->getRetentionDays(), max(1, (int)getParam('bx_analytics_vitals_retention_days')));
  292. }
  293. /**
  294. * Hosts that belong to the site: a referrer from one of them isn't a traffic source.
  295. */
  296. public function getInternalHosts()
  297. {
  298. $aHosts = [parse_url(BX_DOL_URL_ROOT, PHP_URL_HOST)];
  299. /**
  300. * @hooks
  301. * @hookdef hook-bx_analytics-get_internal_hosts 'bx_analytics', 'get_internal_hosts' - hook to add the hosts of other clients (NEO web, white-label domains) whose referrers are internal
  302. * - $unit_name - equals `bx_analytics`
  303. * - $action - equals `get_internal_hosts`
  304. * - $object_id - not used
  305. * - $sender_id - not used
  306. * - $extra_params - array of additional params with the following array keys:
  307. * - `hosts` - [array] by ref, list of host names, can be overridden in hook processing
  308. * @hook @ref hook-bx_analytics-get_internal_hosts
  309. */
  310. bx_alert($this->getName(), 'get_internal_hosts', 0, 0, [
  311. 'hosts' => &$aHosts,
  312. ]);
  313. return array_filter($aHosts);
  314. }
  315. /**
  316. * Values of a list setting, which Studio stores comma separated; text settings may use lines too.
  317. */
  318. protected function _getList($sName)
  319. {
  320. $sValue = trim((string)getParam($sName));
  321. if($sValue === '')
  322. return [];
  323. return array_values(array_filter(array_map('trim', preg_split('/[\s,]+/', $sValue)), 'get_mb_len'));
  324. }
  325. }
  326. /** @} */