Lines: 100.00%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Drupal\visitors\Service; | |
| 6 | ||
| 7 | use Drupal\Component\Datetime\TimeInterface; | |
| 8 | use Drupal\Component\Utility\Unicode; | |
| 9 | use Drupal\Core\Database\Connection; | |
| 10 | use Drupal\visitors\VisitorsCookieInterface; | |
| 11 | use Drupal\visitors\VisitorsEventInterface; | |
| 12 | use Drupal\visitors\VisitorsTrackerInterface; | |
| 13 | ||
| 14 | /** | |
| 15 | * Queues browser events and writes events PHP already knows about. | |
| 16 | */ | |
| 17 | final class EventService implements VisitorsEventInterface { | |
| 18 | ||
| 19 | /** | |
| 20 | * Page columns copied from the latest page view. | |
| 21 | */ | |
| 22 | private const PAGE_COLUMNS = [ | |
| 23 | 'page_view', | |
| 24 | 'title', | |
| 25 | 'uid', | |
| 26 | 'url_prefix', | |
| 27 | 'url', | |
| 28 | 'path', | |
| 29 | 'route', | |
| 30 | 'referrer_url', | |
| 31 | 'referrer_url_prefix', | |
| 32 | 'referrer_internal', | |
| 33 | 'referrer_path', | |
| 34 | 'referrer_route', | |
| 35 | 'server', | |
| 36 | ]; | |
| 37 | ||
| 38 | /** | |
| 39 | * Events queued for the browser tracker. | |
| 40 | * | |
| 41 | * @var array<int, array<string, float|string>> | |
| 42 | */ | |
| 43 | protected array $queue = []; | |
| 44 | ||
| 45 | /** | |
| 46 | * Constructs an EventService. | |
| 47 | * | |
| 48 | * @param \Drupal\Core\Database\Connection $database | |
| 49 | * The database connection. | |
| 50 | * @param \Drupal\visitors\VisitorsTrackerInterface $tracker | |
| 51 | * The visit and event tracker. | |
| 52 | * @param \Drupal\visitors\VisitorsCookieInterface $cookie | |
| 53 | * The visitor cookie. | |
| 54 | * @param \Drupal\Component\Datetime\TimeInterface $time | |
| 55 | * The time service. | |
| 56 | */ | |
| 57 | public function __construct( | |
| 58 | protected Connection $database, | |
| 59 | protected VisitorsTrackerInterface $tracker, | |
| 60 | protected VisitorsCookieInterface $cookie, | |
| 61 | protected TimeInterface $time, | |
| 62 | 3 | ) {} |
| 63 | ||
| 64 | /** | |
| 65 | * {@inheritdoc} | |
| 66 | */ | |
| 67 | public function trackEvent(string $category, string $action, ?string $name = NULL, ?float $value = NULL): void { | |
| 68 | 1 | $category = $this->eventString($category); |
| 69 | 1 | $action = $this->eventString($action); |
| 70 | 1 | if ($category === '' || $action === '') { |
| 71 | 1 | return; |
| 72 | } | |
| 73 | ||
| 74 | 1 | $item = [ |
| 75 | 1 | 'category' => $category, |
| 76 | 1 | 'action' => $action, |
| 77 | 1 | 'name' => $this->eventString($name), |
| 78 | 1 | ]; |
| 79 | 1 | if ($value !== NULL) { |
| 80 | 1 | $item['value'] = $value; |
| 81 | } | |
| 82 | 1 | $this->queue[] = $item; |
| 83 | } | |
| 84 | ||
| 85 | /** | |
| 86 | * {@inheritdoc} | |
| 87 | */ | |
| 88 | public function getQueuedEvents(): array { | |
| 89 | 1 | return $this->queue; |
| 90 | } | |
| 91 | ||
| 92 | /** | |
| 93 | * {@inheritdoc} | |
| 94 | */ | |
| 95 | public function record(string $category, string $action, ?string $name = NULL, ?float $value = NULL): ?int { | |
| 96 | 2 | $category = $this->eventString($category); |
| 97 | 2 | $action = $this->eventString($action); |
| 98 | 2 | if ($category === '' || $action === '') { |
| 99 | 1 | return NULL; |
| 100 | } | |
| 101 | ||
| 102 | 2 | $visitor_id = $this->cookie->getId(); |
| 103 | 2 | if ($visitor_id === NULL || $visitor_id === '') { |
| 104 | 1 | return NULL; |
| 105 | } | |
| 106 | ||
| 107 | 2 | $request_time = $this->time->getRequestTime(); |
| 108 | 2 | $visit = $this->openVisit($visitor_id, $request_time); |
| 109 | 2 | if ($visit === NULL) { |
| 110 | 1 | return NULL; |
| 111 | } | |
| 112 | ||
| 113 | 1 | $page = $this->latestPageView((int) $visit['id']); |
| 114 | 1 | $stored_name = $this->eventString($name); |
| 115 | 1 | $fields = [ |
| 116 | 1 | 'visit_id' => (int) $visit['id'], |
| 117 | 1 | 'created' => $request_time, |
| 118 | 1 | 'event_category' => $category, |
| 119 | 1 | 'event_action' => $action, |
| 120 | 1 | 'event_name' => $stored_name === '' ? NULL : $stored_name, |
| 121 | 1 | 'event_value' => $value, |
| 122 | 1 | 'title' => '', |
| 123 | 1 | 'url' => '', |
| 124 | 1 | 'path' => '', |
| 125 | 1 | 'route' => '', |
| 126 | 1 | 'referrer_url' => '', |
| 127 | 1 | 'referrer_internal' => 0, |
| 128 | 1 | 'referrer_path' => '', |
| 129 | 1 | 'referrer_route' => '', |
| 130 | 1 | ]; |
| 131 | 1 | foreach (self::PAGE_COLUMNS as $column) { |
| 132 | 1 | if ($page !== NULL && array_key_exists($column, $page)) { |
| 133 | 1 | $fields[$column] = $page[$column]; |
| 134 | } | |
| 135 | } | |
| 136 | ||
| 137 | 1 | $event_id = $this->tracker->writeEvent($fields); |
| 138 | 1 | $uid = isset($visit['uid']) && $visit['uid'] !== '' ? (string) $visit['uid'] : NULL; |
| 139 | 1 | $this->tracker->updateVisit( |
| 140 | 1 | (int) $visit['id'], |
| 141 | 1 | $event_id, |
| 142 | 1 | $request_time, |
| 143 | 1 | $uid, |
| 144 | 1 | FALSE, |
| 145 | 1 | ); |
| 146 | ||
| 147 | 1 | return $event_id; |
| 148 | } | |
| 149 | ||
| 150 | /** | |
| 151 | * The open visit for this visitor. | |
| 152 | * | |
| 153 | * @param string $visitor_id | |
| 154 | * The visitor id from the cookie. | |
| 155 | * @param int $request_time | |
| 156 | * The request time. | |
| 157 | * | |
| 158 | * @return array<string, mixed>|null | |
| 159 | * The visit id and user id, or NULL. | |
| 160 | */ | |
| 161 | protected function openVisit(string $visitor_id, int $request_time): ?array { | |
| 162 | 2 | $ago = $request_time - VisitorsTrackerInterface::SESSION_TIMEOUT; |
| 163 | 2 | $visit = $this->database->select('visitors_visit', 'vv') |
| 164 | 2 | ->fields('vv', ['id', 'uid']) |
| 165 | 2 | ->condition('vv.visitor_id', $visitor_id) |
| 166 | 2 | ->condition('vv.exit_time', $ago, '>') |
| 167 | 2 | ->orderBy('vv.exit_time', 'DESC') |
| 168 | 2 | ->range(0, 1) |
| 169 | 2 | ->execute() |
| 170 | 2 | ->fetchAssoc(); |
| 171 | ||
| 172 | 2 | return $visit === FALSE ? NULL : $visit; |
| 173 | } | |
| 174 | ||
| 175 | /** | |
| 176 | * The latest page view on a visit. | |
| 177 | * | |
| 178 | * Downloads, outlinks, and custom events are not page views. | |
| 179 | * | |
| 180 | * @param int $visit_id | |
| 181 | * The visit id. | |
| 182 | * | |
| 183 | * @return array<string, mixed>|null | |
| 184 | * The page-view columns, or NULL. | |
| 185 | */ | |
| 186 | protected function latestPageView(int $visit_id): ?array { | |
| 187 | 1 | $query = $this->database->select('visitors_event', 've'); |
| 188 | 1 | $query->fields('ve', self::PAGE_COLUMNS); |
| 189 | 1 | $query->condition('ve.visit_id', $visit_id); |
| 190 | 1 | $query->isNull('ve.event_category'); |
| 191 | 1 | $query->condition($query->orConditionGroup() |
| 192 | 1 | ->isNull('ve.event') |
| 193 | 1 | ->condition('ve.event', ['download', 'outlink'], 'NOT IN')); |
| 194 | 1 | $query->orderBy('ve.id', 'DESC'); |
| 195 | 1 | $query->range(0, 1); |
| 196 | 1 | $page = $query->execute()->fetchAssoc(); |
| 197 | ||
| 198 | 1 | return $page === FALSE ? NULL : $page; |
| 199 | } | |
| 200 | ||
| 201 | /** | |
| 202 | * Trims and cuts an event string to the column length. | |
| 203 | * | |
| 204 | * @param string|null $value | |
| 205 | * The event string. | |
| 206 | * | |
| 207 | * @return string | |
| 208 | * The stored string, or an empty string when the value is blank. | |
| 209 | */ | |
| 210 | protected function eventString(?string $value): string { | |
| 211 | 3 | $value = trim((string) $value); |
| 212 | 3 | if ($value === '') { |
| 213 | 2 | return ''; |
| 214 | } | |
| 215 | 3 | return Unicode::truncate($value, 255, FALSE, FALSE); |
| 216 | } | |
| 217 | ||
| 218 | } |