Lines: 69.70%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Drupal\visitors\Plugin\views\field; | |
| 6 | ||
| 7 | use Drupal\Core\Form\FormStateInterface; | |
| 8 | use Drupal\views\Attribute\ViewsField; | |
| 9 | use Drupal\views\Plugin\views\field\FieldPluginBase; | |
| 10 | ||
| 11 | /** | |
| 12 | * Counts visits whose last page view is this one. | |
| 13 | * | |
| 14 | * Matomo's exits metric is one per visit that ended on the page. | |
| 15 | */ | |
| 16 | #[ViewsField("visitors_exit")] | |
| 17 | class Exits extends FieldPluginBase { | |
| 18 | ||
| 19 | /** | |
| 20 | * {@inheritdoc} | |
| 21 | */ | |
| 22 | protected function defineOptions() { | |
| 23 | 11 | $options = parent::defineOptions(); |
| 24 | 11 | $options['hide_zero'] = ['default' => FALSE]; |
| 25 | 11 | return $options; |
| 26 | } | |
| 27 | ||
| 28 | /** | |
| 29 | * {@inheritdoc} | |
| 30 | */ | |
| 31 | public function buildOptionsForm(&$form, FormStateInterface $form_state) { | |
| 32 | 0 | parent::buildOptionsForm($form, $form_state); |
| 33 | 0 | $form['hide_zero'] = [ |
| 34 | 0 | '#type' => 'checkbox', |
| 35 | 0 | '#title' => $this->t('Hide groups with no exits'), |
| 36 | 0 | '#description' => $this->t( |
| 37 | 0 | 'Omit rows that were never the last page in a visit.' |
| 38 | 0 | ), |
| 39 | 0 | '#default_value' => !empty($this->options['hide_zero']), |
| 40 | 0 | ]; |
| 41 | } | |
| 42 | ||
| 43 | /** | |
| 44 | * {@inheritdoc} | |
| 45 | */ | |
| 46 | public function query() { | |
| 47 | 3 | $this->ensureMyTable(); |
| 48 | 3 | $formula = $this->exitCase(); |
| 49 | 3 | $params = []; |
| 50 | ||
| 51 | // Sum the 0/1 flag when the display groups rows. | |
| 52 | 3 | if ($this->view->display_handler->useGroupBy()) { |
| 53 | 1 | $params['function'] = !empty($this->options['group_type']) && $this->options['group_type'] != 'group' |
| 54 | 1 | ? $this->options['group_type'] |
| 55 | 1 | : 'sum'; |
| 56 | 1 | $params['orderby'] = FALSE; |
| 57 | 1 | $params['no_group'] = TRUE; |
| 58 | } | |
| 59 | ||
| 60 | 3 | $alias = $this->tableAlias . '__exit'; |
| 61 | 3 | $this->field_alias = $this->query->addField(NULL, $formula, $alias, $params); |
| 62 | ||
| 63 | 3 | if (!empty($this->options['hide_zero'])) { |
| 64 | 1 | $this->query->addHavingExpression(0, 'SUM(' . $formula . ') > 0'); |
| 65 | } | |
| 66 | } | |
| 67 | ||
| 68 | /** | |
| 69 | * {@inheritdoc} | |
| 70 | */ | |
| 71 | public function usesGroupBy() { | |
| 72 | 1 | return FALSE; |
| 73 | } | |
| 74 | ||
| 75 | /** | |
| 76 | * SQL that is 1 when this page view is the visit exit. | |
| 77 | */ | |
| 78 | protected function exitCase(): string { | |
| 79 | 3 | $event = $this->tableAlias; |
| 80 | 3 | $visit = $this->visitAlias(); |
| 81 | 3 | return "CASE WHEN {$event}.id = {$visit}.exit THEN 1 ELSE 0 END"; |
| 82 | } | |
| 83 | ||
| 84 | /** | |
| 85 | * Alias of the visit table, which stores the exit event id. | |
| 86 | */ | |
| 87 | protected function visitAlias(): string { | |
| 88 | 3 | $storage = $this->view->storage ?? NULL; |
| 89 | 3 | if ($storage) { |
| 90 | 3 | return (string) $storage->get('base_table'); |
| 91 | } | |
| 92 | 0 | return 'visitors_visit'; |
| 93 | } | |
| 94 | ||
| 95 | } |