Coverage index

visitors.install

Lines: 96.21%

LineHitsSource
1
<?php
2
3
/**
4
 * @file
5
 * Install/uninstall visitors module.
6
 */
7
8
use Drupal\Component\Utility\DeprecationHelper;
9
use Drupal\Core\Extension\Requirement\RequirementSeverity;
10
use Drupal\Component\Serialization\Yaml;
11
use Drupal\Core\Entity\EntityTypeInterface;
12
use Drupal\Core\StringTranslation\TranslatableMarkup;
13
use Drupal\Core\Url;
14
use Drupal\visitors\MediaPlaySegments;
15
16
/**
17
 * Implements hook_install().
18
 */
19
function visitors_install($is_syncing) {
203
  if ($is_syncing) {
211
    return;
22
  }
232
  if (!\Drupal::moduleHandler()->moduleExists('statistics')) {
241
    return;
25
  }
261
  $settings = \Drupal::service('config.factory')->getEditable('visitors.settings');
271
  $settings->set('counter.enabled', FALSE)
281
    ->save();
29
301
  \Drupal::messenger()->addWarning(t(
311
    'The Statistics module is installed. <a href="@url">Convert the Statistics node views</a> to Visitors page views.',
321
    ['@url' => Url::fromRoute('visitors.statistics_migrate')->toString()]
331
  ));
34
35
}
36
37
/**
38
 * Implements hook_schema().
39
 */
40
function visitors_schema() {
41
42
  $schema['visitors_visit'] = [
43
    'fields' => [
44
      'id' => [
45
        'description' => 'Primary key',
46
        'type' => 'serial',
47
        'not null' => TRUE,
48
      ],
49
      'visitor_id' => [
50
        'description' => 'Unique id, provided by the visitor',
51
        'type' => 'varchar',
52
        'length' => 32,
53
        'not null' => FALSE,
54
        'default' => NULL,
55
      ],
56
      'uid' => [
57
        'description' => 'The user of the session',
58
        'type' => 'varchar',
59
        'length' => 32,
60
        'not null' => FALSE,
61
      ],
62
      'localtime' => [
63
        'description' => 'Seconds since midnight, in the local of the visitor',
64
        'type' => 'int',
65
        'size' => 'medium',
66
        'not null' => FALSE,
67
        'default' => NULL,
68
      ],
69
      'returning' => [
70
        'description' => 'Is the visitor returning, or new',
71
        'type' => 'int',
72
        'size' => 'tiny',
73
      ],
74
      'total_visits' => [
75
        'description' => 'Total visits',
76
        'type' => 'int',
77
        'not null' => TRUE,
78
        'default' => 0,
79
      ],
80
      'total_page_views' => [
81
        'description' => 'Total page views this visit',
82
        'type' => 'int',
83
        'not null' => TRUE,
84
        'default' => 0,
85
      ],
86
      'total_events' => [
87
        'description' => 'Total events this visit',
88
        'type' => 'int',
89
        'not null' => TRUE,
90
        'default' => 0,
91
      ],
92
      'entry' => [
93
        'description' => 'First page view',
94
        'type' => 'int',
95
        'not null' => FALSE,
96
        'default' => NULL,
97
      ],
98
      'entry_time' => [
99
        'description' => 'The time the visit was started',
100
        'type' => 'int',
101
        'not null' => FALSE,
102
        'default' => NULL,
103
      ],
104
      'exit' => [
105
        'description' => 'Last page view',
106
        'type' => 'int',
107
        'not null' => FALSE,
108
        'default' => NULL,
109
      ],
110
      'exit_time' => [
111
        'description' => 'The time the visit was ended',
112
        'type' => 'int',
113
        'not null' => FALSE,
114
        'default' => NULL,
115
      ],
116
      'total_time' => [
117
        'description' => 'The total time of the visit',
118
        'type' => 'int',
119
        'not null' => FALSE,
120
        'default' => NULL,
121
      ],
122
      'time_since_first' => [
123
        'description' => 'Seconds since the visitor\'s first visit',
124
        'type' => 'int',
125
        'not null' => FALSE,
126
        'default' => NULL,
127
      ],
128
      'time_since_last' => [
129
        'description' => 'Seconds since the visitor\'s last visit',
130
        'type' => 'int',
131
        'not null' => FALSE,
132
        'default' => NULL,
133
      ],
134
      'bot' => [
135
        'description' => 'Is the visitor a bot',
136
        'type' => 'int',
137
        'size' => 'tiny',
138
        'not null' => FALSE,
139
        'default' => NULL,
140
      ],
141
      'bot_name' => [
142
        'description' => 'Device Detector bot name',
143
        'type' => 'varchar',
144
        'length' => 255,
145
        'not null' => FALSE,
146
        'default' => NULL,
147
      ],
148
      'ai_agent' => [
149
        'description' => 'The visit was opened by an AI agent browser.',
150
        'type' => 'int',
151
        'size' => 'tiny',
152
        'unsigned' => TRUE,
153
        'not null' => TRUE,
154
        'default' => 0,
155
      ],
156
      'config_id' => [
157
        'description' => 'The fingerprint id',
158
        'type' => 'varchar',
159
        'length' => 16,
160
        'not null' => TRUE,
161
      ],
162
      'config_resolution' => [
163
        'description' => 'The screen resolution',
164
        'type' => 'varchar',
165
        'length' => 18,
166
        'not null' => FALSE,
167
        'default' => NULL,
168
      ],
169
      'config_pdf' => [
170
        'description' => 'Has PDF support',
171
        'type' => 'int',
172
        'size' => 'tiny',
173
        'not null' => FALSE,
174
        'default' => NULL,
175
      ],
176
      'config_flash' => [
177
        'description' => 'Has Flash support',
178
        'type' => 'int',
179
        'size' => 'tiny',
180
        'not null' => FALSE,
181
        'default' => NULL,
182
      ],
183
      'config_java' => [
184
        'description' => 'Has Java support',
185
        'type' => 'int',
186
        'size' => 'tiny',
187
        'not null' => FALSE,
188
        'default' => NULL,
189
      ],
190
      'config_quicktime' => [
191
        'description' => 'Has Quicktime support',
192
        'type' => 'int',
193
        'size' => 'tiny',
194
        'not null' => FALSE,
195
        'default' => NULL,
196
      ],
197
      'config_realplayer' => [
198
        'description' => 'Has Realplayer support',
199
        'type' => 'int',
200
        'size' => 'tiny',
201
        'not null' => FALSE,
202
        'default' => NULL,
203
      ],
204
      'config_windowsmedia' => [
205
        'description' => 'Has Windows Media support',
206
        'type' => 'int',
207
        'size' => 'tiny',
208
        'not null' => FALSE,
209
        'default' => NULL,
210
      ],
211
      'config_silverlight' => [
212
        'description' => 'Has Silverlight support',
213
        'type' => 'int',
214
        'size' => 'tiny',
215
        'not null' => FALSE,
216
        'default' => NULL,
217
      ],
218
      'config_cookie' => [
219
        'description' => 'Has cookie support',
220
        'type' => 'int',
221
        'size' => 'tiny',
222
        'not null' => FALSE,
223
        'default' => NULL,
224
      ],
225
      'config_browser_engine' => [
226
        'description' => 'The browser engine',
227
        'type' => 'varchar',
228
        'length' => '10',
229
        'not null' => FALSE,
230
        'default' => NULL,
231
      ],
232
      'config_browser_name' => [
233
        'description' => 'The browser name',
234
        'type' => 'varchar',
235
        'length' => '2',
236
        'not null' => FALSE,
237
        'default' => NULL,
238
      ],
239
      'config_browser_version' => [
240
        'description' => 'The browser version',
241
        'type' => 'varchar',
242
        'length' => '20',
243
        'not null' => FALSE,
244
        'default' => NULL,
245
      ],
246
      'config_client_type' => [
247
        'description' => 'The client type',
248
        'type' => 'varchar',
249
        'length' => '100',
250
        'not null' => FALSE,
251
        'default' => NULL,
252
      ],
253
      'config_device_brand' => [
254
        'description' => 'The device brand',
255
        'type' => 'varchar',
256
        'length' => '100',
257
        'not null' => FALSE,
258
        'default' => NULL,
259
      ],
260
      'config_device_model' => [
261
        'description' => 'The device model',
262
        'type' => 'varchar',
263
        'length' => '100',
264
        'not null' => FALSE,
265
        'default' => NULL,
266
      ],
267
      'config_device_type' => [
268
        'description' => 'The device type',
269
        'type' => 'varchar',
270
        'length' => '100',
271
        'not null' => FALSE,
272
        'default' => NULL,
273
      ],
274
      'config_os' => [
275
        'description' => 'The operating system',
276
        'type' => 'varchar',
277
        'length' => '3',
278
        'not null' => FALSE,
279
        'default' => NULL,
280
      ],
281
      'config_os_version' => [
282
        'description' => 'The operating system version',
283
        'type' => 'varchar',
284
        'length' => '100',
285
        'not null' => FALSE,
286
        'default' => NULL,
287
      ],
288
      'location_browser_lang' => [
289
        'description' => 'The browser language',
290
        'type' => 'varchar',
291
        'length' => 20,
292
        'not null' => FALSE,
293
        'default' => NULL,
294
      ],
295
      'location_ip' => [
296
        'description' => 'The IP address',
297
        'type' => 'varchar',
298
        'length' => 45,
299
        'not null' => TRUE,
300
        'default' => '',
301
      ],
302
      'location_continent' => [
303
        'description' => 'The continent of the visitor',
304
        'type' => 'varchar',
305
        'length' => 2,
306
        'not null' => FALSE,
307
        'default' => '',
308
      ],
309
      'location_country' => [
310
        'description' => 'The country of the visitor',
311
        'type' => 'varchar',
312
        'length' => 2,
313
        'not null' => FALSE,
314
        'default' => '',
315
      ],
316
      'location_region' => [
317
        'description' => 'The region of the visitor',
318
        'type' => 'varchar',
319
        'length' => 128,
320
        'not null' => FALSE,
321
        'default' => '',
322
      ],
323
      'location_city' => [
324
        'description' => 'The city of the visitor',
325
        'type' => 'varchar',
326
        'length' => 128,
327
        'not null' => FALSE,
328
        'default' => '',
329
      ],
330
      'location_latitude' => [
331
        'description' => 'The latitude from the IP address',
332
        'type' => 'numeric',
333
        'precision' => 13,
334
        'scale' => 10,
335
        'default' => NULL,
336
      ],
337
      'location_longitude' => [
338
        'description' => 'The longitude from the IP address',
339
        'type' => 'numeric',
340
        'precision' => 13,
341
        'scale' => 10,
342
        'default' => NULL,
343
      ],
344
      'referer_keyword' => [
345
        'description' => 'The referer keyword',
346
        'type' => 'varchar',
347
        'length' => 255,
348
        'not null' => FALSE,
349
        'default' => NULL,
350
      ],
351
      'referer_name' => [
352
        'description' => 'The referer name',
353
        'type' => 'varchar',
354
        'length' => 255,
355
        'not null' => FALSE,
356
        'default' => NULL,
357
      ],
358
      'referer_type' => [
359
        'description' => 'The referer type',
360
        'type' => 'varchar',
361
        'length' => 32,
362
        'not null' => FALSE,
363
        'default' => NULL,
364
      ],
365
      'referer_url' => [
366
        'description' => 'The referer URL',
367
        'type' => 'varchar',
368
        'length' => 1500,
369
        'not null' => FALSE,
370
        'default' => NULL,
371
      ],
372
      'campaign_source' => [
373
        'description' => 'The campaign source',
374
        'type' => 'varchar',
375
        'length' => 255,
376
        'not null' => FALSE,
377
        'default' => NULL,
378
      ],
379
      'campaign_medium' => [
380
        'description' => 'The campaign medium',
381
        'type' => 'varchar',
382
        'length' => 255,
383
        'not null' => FALSE,
384
        'default' => NULL,
385
      ],
386
      'campaign_content' => [
387
        'description' => 'The campaign content',
388
        'type' => 'varchar',
389
        'length' => 255,
390
        'not null' => FALSE,
391
        'default' => NULL,
392
      ],
393
      'campaign_id' => [
394
        'description' => 'The campaign id',
395
        'type' => 'varchar',
396
        'length' => 255,
397
        'not null' => FALSE,
398
        'default' => NULL,
399
      ],
400
      'campaign_group' => [
401
        'description' => 'The campaign group',
402
        'type' => 'varchar',
403
        'length' => 255,
404
        'not null' => FALSE,
405
        'default' => NULL,
406
      ],
407
      'campaign_placement' => [
408
        'description' => 'The campaign placement',
409
        'type' => 'varchar',
410
        'length' => 255,
411
        'not null' => FALSE,
412
        'default' => NULL,
413
      ],
414
    ],
415
    'primary key' => ['id'],
416
    'indexes' => [
417
      'visitor_id' => ['visitor_id'],
418
      'bot' => ['bot'],
419
      'exit_time' => ['exit_time'],
420
      'uid' => ['uid'],
421
    ],
422
  ];
423
42431
  $schema['visitors_event'] = [
42531
    'fields' => [
42631
      'id' => [
42731
        'description' => 'Primary key',
42831
        'type' => 'serial',
42931
        'not null' => TRUE,
43031
      ],
43131
      'visit_id' => [
43231
        'description' => 'The visitors_visit id',
43331
        'type' => 'int',
43431
      ],
43531
      'page_view' => [
43631
        'description' => 'The unique page view id',
43731
        'type' => 'varchar',
43831
        'length' => 32,
43931
        'not null' => FALSE,
44031
        'default' => NULL,
44131
      ],
44231
      'plugin' => [
44331
        'description' => 'The plugin that generated the event',
44431
        'type' => 'varchar',
44531
        'length' => 255,
44631
        'not null' => FALSE,
44731
        'default' => NULL,
44831
      ],
44931
      'event' => [
45031
        'description' => 'The event',
45131
        'type' => 'varchar',
45231
        'length' => 255,
45331
        'not null' => FALSE,
45431
        'default' => NULL,
45531
      ],
45631
      'plugin_int_1' => [
45731
        'description' => 'The first event int variable',
45831
        'type' => 'int',
45931
        'not null' => FALSE,
46031
        'default' => NULL,
46131
      ],
46231
      'plugin_int_2' => [
46331
        'description' => 'The second event int variable',
46431
        'type' => 'int',
46531
        'not null' => FALSE,
46631
        'default' => NULL,
46731
      ],
46831
      'plugin_var_1' => [
46931
        'description' => 'The first event varchar variable',
47031
        'type' => 'varchar',
47131
        'length' => 255,
47231
        'not null' => FALSE,
47331
        'default' => NULL,
47431
      ],
47531
      'plugin_var_2' => [
47631
        'description' => 'The second event varchar variable',
47731
        'type' => 'varchar',
47831
        'length' => 255,
47931
        'not null' => FALSE,
48031
        'default' => NULL,
48131
      ],
48231
      'plugin_var_3' => [
48331
        'description' => 'The third event varchar variable',
48431
        'type' => 'varchar',
48531
        'length' => 255,
48631
        'not null' => FALSE,
48731
        'default' => NULL,
48831
      ],
48931
      'plugin_var_4' => [
49031
        'description' => 'The fourth event varchar variable',
49131
        'type' => 'varchar',
49231
        'length' => 255,
49331
        'not null' => FALSE,
49431
        'default' => NULL,
49531
      ],
49631
      'event_category' => [
49731
        'description' => 'The Matomo event category',
49831
        'type' => 'varchar',
49931
        'length' => 255,
50031
        'not null' => FALSE,
50131
        'default' => NULL,
50231
      ],
50331
      'event_action' => [
50431
        'description' => 'The Matomo event action',
50531
        'type' => 'varchar',
50631
        'length' => 255,
50731
        'not null' => FALSE,
50831
        'default' => NULL,
50931
      ],
51031
      'event_name' => [
51131
        'description' => 'The Matomo event name',
51231
        'type' => 'varchar',
51331
        'length' => 255,
51431
        'not null' => FALSE,
51531
        'default' => NULL,
51631
      ],
51731
      'event_value' => [
51831
        'description' => 'The Matomo event value',
51931
        'type' => 'float',
52031
        'size' => 'big',
52131
        'not null' => FALSE,
52231
        'default' => NULL,
52331
      ],
52431
      'title' => [
52531
        'description' => 'The page title',
52631
        'type' => 'text',
52731
        'not null' => TRUE,
52831
      ],
52931
      'uid' => [
53031
        'description' => 'The user who viewed the page',
53131
        'type' => 'varchar',
53231
        'length' => 32,
53331
        'not null' => FALSE,
53431
      ],
53531
      'url_prefix' => [
53631
        'description' => 'The protocol and if started with www',
53731
        'type' => 'int',
53831
        'size' => 'small',
53931
      ],
54031
      'url' => [
54131
        'description' => 'The page URL',
54231
        'type' => 'text',
54331
        'not null' => TRUE,
54431
      ],
54531
      'path' => [
54631
        'description' => 'The Drupal path',
54731
        'type' => 'varchar',
54831
        'length' => 255,
54931
        'not null' => TRUE,
55031
        'default' => '',
55131
      ],
55231
      'route' => [
55331
        'description' => 'The Drupal route name',
55431
        'type' => 'varchar',
55531
        'length' => 255,
55631
        'not null' => TRUE,
55731
        'default' => '',
55831
      ],
55931
      'referrer_url' => [
56031
        'description' => 'The referrer URL, without the protocol or www.',
56131
        'type' => 'text',
56231
        'not null' => TRUE,
56331
      ],
56431
      'referrer_url_prefix' => [
56531
        'description' => 'The protocol and whether the referrer started with www.',
56631
        'type' => 'int',
56731
        'size' => 'small',
56831
        'not null' => FALSE,
56931
        'default' => NULL,
57031
      ],
57131
      'referrer_internal' => [
57231
        'description' => 'Whether the referrer host matches the page host.',
57331
        'type' => 'int',
57431
        'size' => 'tiny',
57531
        'not null' => TRUE,
57631
        'default' => 0,
57731
      ],
57831
      'referrer_path' => [
57931
        'description' => 'The Drupal path of an internal referrer.',
58031
        'type' => 'varchar',
58131
        'length' => 255,
58231
        'not null' => TRUE,
58331
        'default' => '',
58431
      ],
58531
      'referrer_route' => [
58631
        'description' => 'The Drupal route of an internal referrer.',
58731
        'type' => 'varchar',
58831
        'length' => 255,
58931
        'not null' => TRUE,
59031
        'default' => '',
59131
      ],
59231
      'server' => [
59331
        'description' => 'The server that generated the response',
59431
        'type' => 'varchar',
59531
        'length' => 255,
59631
        'not null' => FALSE,
59731
        'default' => NULL,
59831
      ],
59931
      'pf_network' => [
60031
        'description' => 'Network performance',
60131
        'type' => 'int',
60231
        'not null' => FALSE,
60331
        'default' => NULL,
60431
      ],
60531
      'pf_server' => [
60631
        'description' => 'Server performance',
60731
        'type' => 'int',
60831
        'not null' => FALSE,
60931
        'default' => NULL,
61031
      ],
61131
      'pf_transfer' => [
61231
        'description' => 'Transfer performance',
61331
        'type' => 'int',
61431
        'not null' => FALSE,
61531
        'default' => NULL,
61631
      ],
61731
      'pf_dom_processing' => [
61831
        'description' => 'DOM processing performance',
61931
        'type' => 'int',
62031
        'not null' => FALSE,
62131
        'default' => NULL,
62231
      ],
62331
      'pf_dom_complete' => [
62431
        'description' => 'DOM complete performance',
62531
        'type' => 'int',
62631
        'not null' => FALSE,
62731
        'default' => NULL,
62831
      ],
62931
      'pf_on_load' => [
63031
        'description' => 'On load performance',
63131
        'type' => 'int',
63231
        'not null' => FALSE,
63331
        'default' => NULL,
63431
      ],
63531
      'pf_total' => [
63631
        'description' => 'Total performance',
63731
        'type' => 'int',
63831
        'not null' => FALSE,
63931
        'default' => NULL,
64031
      ],
64131
      'created' => [
64231
        'description' => 'The time the request was made',
64331
        'type' => 'int',
64431
      ],
64531
      'time_spent' => [
64631
        'description' => 'Seconds on this page, or until the next event.',
64731
        'type' => 'int',
64831
        'unsigned' => TRUE,
64931
        'not null' => TRUE,
65031
        'default' => 0,
65131
      ],
65231
    ],
65331
    'primary key' => ['id'],
65431
    'indexes' => [
65531
      'visit_id' => ['visit_id'],
65631
      'created' => ['created'],
65731
    ],
65831
  ];
659
66031
  $schema['visitors_media'] = visitors_media_schema();
66131
  $schema['visitors_media_play'] = visitors_media_play_schema();
662
66331
  $schema['visitors_counter'] = [
66431
    'fields' => [
66531
      'entity_id' => [
66631
        'description' => 'The entity id for these visits.',
66731
        'type' => 'int',
66831
        'unsigned' => TRUE,
66931
        'not null' => TRUE,
67031
        'default' => 0,
67131
      ],
67231
      'entity_type' => [
67331
        'type' => 'varchar_ascii',
67431
        'not null' => TRUE,
67531
        'default' => 'node',
67631
        'length' => EntityTypeInterface::ID_MAX_LENGTH,
67731
        'description' => 'The entity_type of the entity for these visits.',
67831
      ],
67931
      'total' => [
68031
        'description' => 'The total number of times the entity has been viewed.',
68131
        'type' => 'int',
68231
        'unsigned' => TRUE,
68331
        'not null' => TRUE,
68431
        'default' => 0,
68531
        'size' => 'big',
68631
      ],
68731
      'today' => [
68831
        'description' => 'The total number of times the entity has been viewed today.',
68931
        'type' => 'int',
69031
        'unsigned' => TRUE,
69131
        'not null' => TRUE,
69231
        'default' => 0,
69331
        'size' => 'medium',
69431
      ],
69531
      'timestamp' => [
69631
        'description' => 'The most recent time the entity has been viewed.',
69731
        'type' => 'int',
69831
        'unsigned' => TRUE,
69931
        'not null' => TRUE,
70031
        'default' => 0,
70131
      ],
70231
    ],
70331
    'primary key' => ['entity_type', 'entity_id'],
70431
  ];
705
70631
  return $schema;
707
}
708
709
/**
710
 * Implements hook_requirements().
711
 */
712
function visitors_requirements($phase) {
7133
  $requirements = [];
7143
  if ($phase != 'runtime') {
7151
    return $requirements;
716
  }
7172
  $device = \Drupal::service('visitors.device');
7182
  if (!$device->hasLibrary()) {
7191
    $requirements['visitors_device'] = [
7201
      'title' => t('Device Detector'),
7211
      'value' => t('Not found'),
7221
      'severity' => DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '11.2.0', fn() => RequirementSeverity::Warning, fn() => REQUIREMENT_WARNING),
7231
      'description' => t('The <a href=":url">Device Detector</a> library is strongly encouraged by the Visitors module. Composer will download the library automatically.', [
7241
        ':url' => 'https://github.com/matomo-org/device-detector',
7251
      ]),
7261
    ];
727
  }
728
7292
  return $requirements;
730
}
731
732
/**
733
 * Upgrade to Visitors 3.
734
 */
735
function visitors_update_30000(&$sandbox) {
736
7378
  if (!isset($sandbox['current'])) {
7386
    $sandbox['current'] = 0;
7396
    $sandbox['max'] = 5;
740
  }
741
742
  // Change visitors.settings to visitors.settings.
7438
  if ($sandbox['current'] == 0) {
7446
    $config_factory = \Drupal::configFactory();
745
7466
    $config = $config_factory->getEditable('visitors.config');
7476
    $config_data = $config->getRawData();
748
7496
    $settings = $config_factory->getEditable('visitors.settings');
7506
    $settings->setData($config_data)->save();
751
7526
    $config->delete();
753
  }
754
7558
  if ($sandbox['current'] == 1) {
7565
    $schema = \Drupal::database()->schema();
757
    // @todo replace visitors_schema() with static before an initial release.
7585
    $visitors_schema = visitors_schema();
7595
    $schema->createTable('visitors_visit', $visitors_schema['visitors_visit']);
7605
    $schema->createTable('visitors_event', $visitors_schema['visitors_event']);
761
  }
762
7638
  if ($sandbox['current'] == 2) {
7646
    $config_factory = \Drupal::configFactory();
765
766
    // @todo This needs to be replace with static before an initial release.
7676
    $view = file_get_contents(__DIR__ . '/config/optional/views.view.visitors.yml');
7686
    $config_data = Yaml::decode($view);
7696
    $config_factory->getEditable('views.view.visitors')->setData($config_data)->save();
770
771
    // @todo This needs to be replace with static before an initial release.
7726
    $spam = file_get_contents(__DIR__ . '/config/install/visitors.spam.yml');
7736
    $config_data = Yaml::decode($spam);
7746
    $config_factory->getEditable('visitors.spam')->setData($config_data)->save();
775
776
    // @todo This needs to be replace with static before an initial release.
7776
    $ai_assistants = file_get_contents(__DIR__ . '/config/install/visitors.ai_assistants.yml');
7786
    $config_data = Yaml::decode($ai_assistants);
7796
    $config_factory->getEditable('visitors.ai_assistants')->setData($config_data)->save();
780
781
    // @todo This needs to be replace with static before an initial release.
7826
    $search_engine = file_get_contents(__DIR__ . '/config/install/visitors.search_engines.yml');
7836
    $config_data = Yaml::decode($search_engine);
7846
    $config_factory->getEditable('visitors.search_engine')->setData($config_data)->save();
785
786
    // @todo This needs to be replace with static before an initial release.
7876
    $social_networks = file_get_contents(__DIR__ . '/config/install/visitors.social_networks.yml');
7886
    $config_data = Yaml::decode($social_networks);
7896
    $config_factory->getEditable('visitors.social_networks')->setData($config_data)->save();
790
791
    // Check if views 'views.view.visitors_geoip' exists, and delete it.
7926
    $geoip_view = $config_factory->getEditable('views.view.visitors_geoip');
7936
    if ($geoip_view) {
7946
      $geoip_view->delete();
795
    }
796
797
  }
798
7998
  if ($sandbox['current'] == 3) {
8005
    $visitors_geoip_is_installed = \Drupal::moduleHandler()->moduleExists('visitors_geoip');
8015
    if ($visitors_geoip_is_installed) {
8021
      \Drupal::service('module_installer')->uninstall(['visitors_geoip']);
803
    }
804
  }
805
8068
  if ($sandbox['current'] == 4) {
807
    // Migrate existing visibility settings to condition-based system.
8086
    $config_factory = \Drupal::configFactory();
8096
    $settings = $config_factory->getEditable('visitors.settings');
810
8116
    $tracking_conditions = [];
812
813
    // Migrate request path visibility settings.
8146
    $request_path_pages = $settings->get('visibility.request_path_pages') ?: '';
8156
    $request_path_mode = $settings->get('visibility.request_path_mode') ?: 0;
816
8176
    if (!empty($request_path_pages)) {
8180
      $tracking_conditions['request_path'] = [
8190
        'id' => 'request_path',
8200
        'pages' => $request_path_pages,
821
      // Legacy mode 0 = exclude, mode 1 = include.
8220
        'negate' => $request_path_mode === 0,
8230
      ];
824
    }
825
826
    // Migrate user role visibility settings.
8276
    $user_role_roles = $settings->get('visibility.user_role_roles') ?: [];
8286
    $user_role_mode = $settings->get('visibility.user_role_mode') ?: 0;
829
8306
    if (!empty($user_role_roles)) {
8310
      $tracking_conditions['user_role'] = [
8320
        'id' => 'user_role',
8330
        'roles' => $user_role_roles,
834
      // Legacy mode 0 = exclude, mode 1 = include.
8350
        'negate' => $user_role_mode === 0,
8360
      ];
837
    }
838
839
    // Add exclude user 1 condition if configured.
8406
    if ($settings->get('visibility.exclude_user1')) {
8410
      $tracking_conditions['exclude_admin'] = [
8420
        'id' => 'user_role',
8430
        'roles' => ['administrator'],
8440
        'negate' => TRUE,
8450
      ];
846
    }
847
848
    // Save new condition-based configuration.
8496
    if (!empty($tracking_conditions)) {
8500
      $settings->set('tracking_conditions', $tracking_conditions);
851
    }
852
853
    // Remove old visibility configuration.
8546
    $settings->clear('visibility.request_path_mode')
8556
      ->clear('visibility.request_path_pages')
8566
      ->clear('visibility.user_role_mode')
8576
      ->clear('visibility.user_role_roles')
8586
      ->clear('visibility.exclude_user1')
8596
      ->save();
860
  }
861
8628
  $sandbox['current'] += 1;
863
8648
  $sandbox['#finished'] = ($sandbox['max'] == $sandbox['current']) ? 1 : $sandbox['current'] / $sandbox['max'];
865
}
866
867
/**
868
 * Adds the client debug setting.
869
 */
870
function visitors_update_30001() {
8711
  $config = \Drupal::configFactory()->getEditable('visitors.settings');
8721
  if ($config->get('client_debug') === NULL) {
8731
    $config->set('client_debug', FALSE)->save();
874
  }
875
}
876
877
/**
878
 * Upgrades legacy 3.0.0-alpha database schemas.
879
 *
880
 * Alpha releases changed the schema without update hooks. The installed
881
 * schema version stayed at 30000, so later releases could not alter the
882
 * tables. Inspect the tables to see which alpha created them.
883
 *
884
 * Field specs are copied here so later edits to hook_schema() do not
885
 * change this upgrade.
886
 *
887
 * @return \Drupal\Core\StringTranslation\TranslatableMarkup
888
 *   A message naming the detected release.
889
 */
890
function visitors_update_30002(): TranslatableMarkup {
8914
  $schema = \Drupal::database()->schema();
8924
  if ($schema->tableExists('visitors_action')) {
8931
    $version = '3.0.0-alpha1';
894
  }
8954
  elseif (!$schema->fieldExists('visitors_visit', 'referer_url')) {
8961
    $version = '3.0.0-alpha2';
897
  }
8983
  elseif ($schema->fieldExists('visitors_visit', 'visit_count')) {
8991
    $version = '3.0.0-alpha3';
900
  }
901
  else {
9022
    $version = 'current';
903
  }
904
905
  // Alpha1 stored events in visitors_action.
9064
  if ($schema->tableExists('visitors_action')
9071
    && !$schema->tableExists('visitors_event')) {
9081
    $schema->renameTable('visitors_action', 'visitors_event');
909
  }
9104
  if ($schema->tableExists('visitors_event')
9114
    && $schema->fieldExists('visitors_event', 'action')
9121
    && !$schema->fieldExists('visitors_event', 'event')) {
9131
    $schema->changeField('visitors_event', 'action', 'event', [
9141
      'description' => 'The event',
9151
      'type' => 'varchar',
9161
      'length' => 255,
9171
      'not null' => FALSE,
9181
      'default' => NULL,
9191
    ]);
920
  }
921
922
  // Alpha2 had no referer columns.
923
  $referer_fields = [
924
    'referer_keyword' => [
925
      'description' => 'The referer keyword',
926
      'type' => 'varchar',
927
      'length' => 255,
928
      'not null' => FALSE,
929
      'default' => NULL,
930
    ],
931
    'referer_name' => [
932
      'description' => 'The referer name',
933
      'type' => 'varchar',
934
      'length' => 255,
935
      'not null' => FALSE,
936
      'default' => NULL,
937
    ],
938
    'referer_type' => [
939
      'description' => 'The referer type',
940
      'type' => 'varchar',
941
      'length' => 32,
942
      'not null' => FALSE,
943
      'default' => NULL,
944
    ],
945
    'referer_url' => [
946
      'description' => 'The referer URL',
947
      'type' => 'varchar',
948
      'length' => 1500,
949
      'not null' => FALSE,
950
      'default' => NULL,
951
    ],
952
  ];
9534
  foreach ($referer_fields as $field => $spec) {
9544
    if (!$schema->fieldExists('visitors_visit', $field)) {
9552
      $schema->addField('visitors_visit', $field, $spec);
956
    }
957
  }
958
959
  // Alpha3 used visit_count and page_count. NULLs cannot move into the
960
  // NOT NULL replacements, so store 0 first and keep the old values.
961
  $count_renames = [
962
    'visit_count' => [
963
      'field' => 'total_visits',
964
      'spec' => [
965
        'description' => 'Total visits',
966
        'type' => 'int',
967
        'not null' => TRUE,
968
        'default' => 0,
969
      ],
970
    ],
971
    'page_count' => [
972
      'field' => 'total_page_views',
973
      'spec' => [
974
        'description' => 'Total page views this visit',
975
        'type' => 'int',
976
        'not null' => TRUE,
977
        'default' => 0,
978
      ],
979
    ],
980
  ];
9814
  foreach ($count_renames as $old_field => $replacement) {
9824
    $new_field = $replacement['field'];
9834
    if ($schema->fieldExists('visitors_visit', $old_field)) {
9843
      \Drupal::database()->update('visitors_visit')
9853
        ->fields([$old_field => 0])
9863
        ->isNull($old_field)
9873
        ->execute();
9883
      $schema->changeField(
9893
        'visitors_visit',
9903
        $old_field,
9913
        $new_field,
9923
        $replacement['spec'],
9933
      );
994
    }
9952
    elseif (!$schema->fieldExists('visitors_visit', $new_field)) {
9960
      $schema->addField('visitors_visit', $new_field, $replacement['spec']);
997
    }
998
  }
999
1000
  $added_fields = [
1001
    'total_events' => [
1002
      'description' => 'Total events this visit',
1003
      'type' => 'int',
1004
      'not null' => TRUE,
1005
      'default' => 0,
1006
    ],
1007
    'time_since_first' => [
1008
      'description' => 'Seconds since the visitor\'s first visit',
1009
      'type' => 'int',
1010
      'not null' => FALSE,
1011
      'default' => NULL,
1012
    ],
1013
    'time_since_last' => [
1014
      'description' => 'Seconds since the visitor\'s last visit',
1015
      'type' => 'int',
1016
      'not null' => FALSE,
1017
      'default' => NULL,
1018
    ],
1019
  ];
10204
  foreach ($added_fields as $field => $spec) {
10214
    if (!$schema->fieldExists('visitors_visit', $field)) {
10223
      $schema->addField('visitors_visit', $field, $spec);
1023
    }
1024
  }
1025
10264
  if ($version === 'current') {
10272
    return t('Visitors schema is already current.');
1028
  }
10293
  return t('Upgraded Visitors schema from @version.', [
10303
    '@version' => $version,
10313
  ]);
1032
}
1033
1034
/**
1035
 * Adds Matomo trackEvent columns to {visitors_event}.
1036
 *
1037
 * Field specs are copied here so later edits to hook_schema() do not
1038
 * change this upgrade.
1039
 */
1040
function visitors_update_30003(): void {
10411
  $schema = \Drupal::database()->schema();
1042
  $fields = [
1043
    'event_category' => [
1044
      'description' => 'The Matomo event category',
1045
      'type' => 'varchar',
1046
      'length' => 255,
1047
      'not null' => FALSE,
1048
      'default' => NULL,
1049
    ],
1050
    'event_action' => [
1051
      'description' => 'The Matomo event action',
1052
      'type' => 'varchar',
1053
      'length' => 255,
1054
      'not null' => FALSE,
1055
      'default' => NULL,
1056
    ],
1057
    'event_name' => [
1058
      'description' => 'The Matomo event name',
1059
      'type' => 'varchar',
1060
      'length' => 255,
1061
      'not null' => FALSE,
1062
      'default' => NULL,
1063
    ],
1064
    'event_value' => [
1065
      'description' => 'The Matomo event value',
1066
      'type' => 'float',
1067
      'size' => 'big',
1068
      'not null' => FALSE,
1069
      'default' => NULL,
1070
    ],
1071
  ];
10721
  foreach ($fields as $field => $spec) {
10731
    if (!$schema->fieldExists('visitors_event', $field)) {
10741
      $schema->addField('visitors_event', $field, $spec);
1075
    }
1076
  }
1077
}
1078
1079
/**
1080
 * Stores the user id as a 32 character string.
1081
 *
1082
 * Field specs are copied here so later edits to hook_schema() do not
1083
 * change this upgrade. Existing integers are kept as decimal strings.
1084
 */
1085
function visitors_update_30004(): void {
10861
  $schema = \Drupal::database()->schema();
10871
  $visit_uid = [
10881
    'description' => 'The user of the session',
10891
    'type' => 'varchar',
10901
    'length' => 32,
10911
    'not null' => FALSE,
10921
  ];
10931
  $event_uid = [
10941
    'description' => 'The user who viewed the page',
10951
    'type' => 'varchar',
10961
    'length' => 32,
10971
    'not null' => FALSE,
10981
  ];
1099
11001
  if ($schema->fieldExists('visitors_visit', 'uid')) {
11011
    if ($schema->indexExists('visitors_visit', 'uid')) {
11021
      $schema->dropIndex('visitors_visit', 'uid');
1103
    }
11041
    $schema->changeField('visitors_visit', 'uid', 'uid', $visit_uid);
11051
    if (!$schema->indexExists('visitors_visit', 'uid')) {
11061
      $schema->addIndex('visitors_visit', 'uid', ['uid'], [
11071
        'fields' => [
11081
          'uid' => $visit_uid,
11091
        ],
11101
      ]);
1111
    }
1112
  }
1113
11141
  if ($schema->fieldExists('visitors_event', 'uid')) {
11151
    $schema->changeField('visitors_event', 'uid', 'uid', $event_uid);
1116
  }
1117
}
1118
1119
/**
1120
 * There is no separate scroll depth report.
1121
 *
1122
 * Databases that already ran the first version of this update still have
1123
 * the display. The following update removes it.
1124
 */
1125
function visitors_update_30005(): void {
1126
  // Scroll depth is stored as events. It does not have its own report.
11271
}
1128
1129
/**
1130
 * Removes the scroll depth display from the Visitors view.
1131
 */
1132
function visitors_update_30006(): void {
11331
  $storage = \Drupal::entityTypeManager()->getStorage('view');
1134
  /** @var \Drupal\views\Entity\View|null $view */
11351
  $view = $storage->load('visitors');
11361
  if (!$view) {
11370
    return;
1138
  }
1139
11401
  $displays = $view->get('display');
11411
  if (!isset($displays['scroll_depth_table'])) {
11420
    return;
1143
  }
1144
11451
  unset($displays['scroll_depth_table']);
11461
  $view->set('display', $displays);
11471
  $view->save();
1148
}
1149
1150
/**
1151
 * Splits referrer URLs into a prefix, host, and internal path.
1152
 */
1153
function visitors_update_30007(&$sandbox): void {
11541
  $database = \Drupal::database();
11551
  $schema = $database->schema();
1156
11571
  if (!isset($sandbox['last_id'])) {
11581
    $fields = visitors_schema()['visitors_event']['fields'];
11591
    foreach ([
11601
      'referrer_url_prefix',
11611
      'referrer_internal',
11621
      'referrer_path',
11631
      'referrer_route',
11641
    ] as $name) {
11651
      if (!$schema->fieldExists('visitors_event', $name)) {
11660
        $schema->addField('visitors_event', $name, $fields[$name]);
1167
      }
1168
    }
11691
    visitors_update_30007_displays();
1170
11711
    $sandbox['last_id'] = 0;
11721
    $sandbox['progress'] = 0;
11731
    $sandbox['max'] = (int) $database->select('visitors_event', 'e')
11741
      ->countQuery()
11751
      ->execute()
11761
      ->fetchField();
1177
  }
1178
11791
  if ($sandbox['max'] === 0) {
11800
    $sandbox['#finished'] = 1;
11810
    return;
1182
  }
1183
1184
  /** @var \Drupal\visitors\VisitorsReferrerInterface $referrer */
11851
  $referrer = \Drupal::service('visitors.referrer');
11861
  $rows = $database->select('visitors_event', 'e')
11871
    ->fields('e', ['id', 'referrer_url', 'url'])
11881
    ->condition('id', $sandbox['last_id'], '>')
11891
    ->orderBy('id')
11901
    ->range(0, 50)
11911
    ->execute();
1192
11931
  $count = 0;
11941
  foreach ($rows as $row) {
11951
    $count++;
11961
    $sandbox['last_id'] = (int) $row->id;
11971
    $page_host = $referrer->hostOf((string) $row->url);
11981
    $database->update('visitors_event')
11991
      ->fields($referrer->storageFields((string) $row->referrer_url, $page_host))
12001
      ->condition('id', $row->id)
12011
      ->execute();
12021
    $sandbox['progress']++;
1203
  }
1204
12051
  if ($count === 0) {
12060
    $sandbox['#finished'] = 1;
12070
    return;
1208
  }
1209
12101
  $sandbox['#finished'] = min(0.99, $sandbox['progress'] / $sandbox['max']);
12111
  if ($sandbox['progress'] >= $sandbox['max']) {
12121
    $sandbox['#finished'] = 1;
1213
  }
1214
}
1215
1216
/**
1217
 * Copies the referrer displays from the shipped Visitors view.
1218
 */
1219
function visitors_update_30007_displays(): void {
12201
  $view_yaml = file_get_contents(__DIR__ . '/config/optional/views.view.visitors.yml');
12211
  if ($view_yaml === FALSE) {
12220
    return;
1223
  }
12241
  $config_data = Yaml::decode($view_yaml);
12251
  $storage = \Drupal::entityTypeManager()->getStorage('view');
1226
  /** @var \Drupal\views\Entity\View|null $view */
12271
  $view = $storage->load('visitors');
12281
  if (!$view || !isset($config_data['display'])) {
12290
    return;
1230
  }
1231
12321
  $displays = $view->get('display');
12331
  foreach (['referrers_table', 'referrer_table'] as $id) {
12341
    if (isset($config_data['display'][$id])) {
12351
      $displays[$id] = $config_data['display'][$id];
1236
    }
1237
  }
12381
  $view->set('display', $displays);
12391
  $view->save();
1240
}
1241
1242
/**
1243
 * Stores same-site visit referrers as direct entry.
1244
 */
1245
function visitors_update_30008(): void {
12461
  $database = \Drupal::database();
12471
  if ($database->schema()->tableExists('visitors_visit')) {
12481
    $database->update('visitors_visit')
12491
      ->fields(['referer_type' => 'direct'])
12501
      ->condition('referer_type', 'internal')
12511
      ->execute();
1252
  }
1253
12541
  $view_yaml = file_get_contents(__DIR__ . '/config/optional/views.view.visitors.yml');
12551
  if ($view_yaml === FALSE) {
12560
    return;
1257
  }
12581
  $config_data = Yaml::decode($view_yaml);
12591
  $storage = \Drupal::entityTypeManager()->getStorage('view');
1260
  /** @var \Drupal\views\Entity\View|null $view */
12611
  $view = $storage->load('visitors');
12621
  if (!$view || !isset($config_data['display'])) {
12630
    return;
1264
  }
1265
12661
  $displays = $view->get('display');
1267
  $ids = [
1268
    'referrer_overview_pie',
1269
    'referrer_overview_table',
1270
    'referrers_all_table',
1271
    'search_engines_table',
1272
    'search_keywords_table',
1273
    'search_engine_keywords',
1274
    'search_keyword_engines',
1275
    'websites_table',
1276
    'website_urls',
1277
    'social_referrers_table',
1278
    'social_urls',
1279
    'ai_visitors_pie',
1280
    'ai_visitors_table',
1281
    'ai_entry_pages',
1282
    'campaigns_table',
1283
    'campaign_keywords',
1284
  ];
12851
  foreach ($ids as $id) {
12861
    if (isset($config_data['display'][$id])) {
12871
      $displays[$id] = $config_data['display'][$id];
1288
    }
1289
  }
12901
  $view->set('display', $displays);
12911
  $view->save();
1292
}
1293
1294
/**
1295
 * Adds the Downloads report.
1296
 */
1297
function visitors_update_30009(): void {
12981
  $view_yaml = file_get_contents(__DIR__ . '/config/optional/views.view.visitors.yml');
12991
  if ($view_yaml === FALSE) {
13000
    return;
1301
  }
13021
  $config_data = Yaml::decode($view_yaml);
13031
  $storage = \Drupal::entityTypeManager()->getStorage('view');
1304
  /** @var \Drupal\views\Entity\View|null $view */
13051
  $view = $storage->load('visitors');
13061
  if (!$view || !isset($config_data['display']['downloads_table'])) {
13070
    return;
1308
  }
1309
13101
  $displays = $view->get('display');
13111
  $displays['downloads_table'] = $config_data['display']['downloads_table'];
13121
  $view->set('display', $displays);
13131
  $view->save();
1314
}
1315
1316
/**
1317
 * Schema for one media view.
1318
 *
1319
 * A row is inserted when the media is seen and updated while it plays.
1320
 *
1321
 * @return array
1322
 *   The visitors_media table definition.
1323
 */
1324
function visitors_media_schema(): array {
13251
  return [
13261
    'description' => 'Audio and video viewed during a visit.',
13271
    'fields' => [
13281
      'id' => [
13291
        'description' => 'Primary key',
13301
        'type' => 'serial',
13311
        'not null' => TRUE,
13321
      ],
13331
      'visit_id' => [
13341
        'description' => 'The visitors_visit id',
13351
        'type' => 'int',
13361
        'not null' => TRUE,
13371
      ],
13381
      'page_view' => [
13391
        'description' => 'The unique page view id',
13401
        'type' => 'varchar',
13411
        'length' => 32,
13421
        'not null' => FALSE,
13431
        'default' => NULL,
13441
      ],
13451
      'media_id' => [
13461
        'description' => 'The id of this media view',
13471
        'type' => 'varchar',
13481
        'length' => 32,
13491
        'not null' => TRUE,
13501
      ],
13511
      'resource' => [
13521
        'description' => 'The media resource URL',
13531
        'type' => 'text',
13541
        'not null' => TRUE,
13551
      ],
13561
      'media_type' => [
13571
        'description' => 'video or audio',
13581
        'type' => 'varchar',
13591
        'length' => 16,
13601
        'not null' => TRUE,
13611
      ],
13621
      'title' => [
13631
        'description' => 'The media title',
13641
        'type' => 'varchar',
13651
        'length' => 255,
13661
        'not null' => TRUE,
13671
        'default' => '',
13681
      ],
13691
      'player' => [
13701
        'description' => 'The media player name',
13711
        'type' => 'varchar',
13721
        'length' => 32,
13731
        'not null' => TRUE,
13741
        'default' => 'html5',
13751
      ],
13761
      'spent_time' => [
13771
        'description' => 'Seconds the media was actually playing',
13781
        'type' => 'int',
13791
        'not null' => TRUE,
13801
        'default' => 0,
13811
      ],
13821
      'length' => [
13831
        'description' => 'The media duration in seconds',
13841
        'type' => 'int',
13851
        'not null' => FALSE,
13861
        'default' => NULL,
13871
      ],
13881
      'progress' => [
13891
        'description' => 'The furthest position reached, in seconds',
13901
        'type' => 'int',
13911
        'not null' => FALSE,
13921
        'default' => NULL,
13931
      ],
13941
      'progress_percent' => [
13951
        'description' => 'The furthest position as a percent of the length',
13961
        'type' => 'int',
13971
        'not null' => FALSE,
13981
        'default' => NULL,
13991
      ],
14001
      'time_to_play' => [
14011
        'description' => 'Seconds from impression until the first play',
14021
        'type' => 'int',
14031
        'not null' => FALSE,
14041
        'default' => NULL,
14051
      ],
14061
      'width' => [
14071
        'description' => 'The video width in pixels',
14081
        'type' => 'int',
14091
        'not null' => FALSE,
14101
        'default' => NULL,
14111
      ],
14121
      'height' => [
14131
        'description' => 'The video height in pixels',
14141
        'type' => 'int',
14151
        'not null' => FALSE,
14161
        'default' => NULL,
14171
      ],
14181
      'fullscreen' => [
14191
        'description' => 'Whether the video was fullscreen',
14201
        'type' => 'int',
14211
        'size' => 'tiny',
14221
        'not null' => TRUE,
14231
        'default' => 0,
14241
      ],
14251
      'play_count' => [
14261
        'description' => 'How many times play started',
14271
        'type' => 'int',
14281
        'not null' => TRUE,
14291
        'default' => 0,
14301
      ],
14311
      'finish_count' => [
14321
        'description' => 'How many times playback finished',
14331
        'type' => 'int',
14341
        'not null' => TRUE,
14351
        'default' => 0,
14361
      ],
14371
      'created' => [
14381
        'description' => 'When the media was first seen',
14391
        'type' => 'int',
14401
        'not null' => TRUE,
14411
      ],
14421
      'changed' => [
14431
        'description' => 'When the media row was last updated',
14441
        'type' => 'int',
14451
        'not null' => TRUE,
14461
      ],
14471
      'entity_type' => [
14481
        'description' => 'The Drupal entity type, usually media',
14491
        'type' => 'varchar_ascii',
14501
        'length' => 32,
14511
        'not null' => FALSE,
14521
        'default' => NULL,
14531
      ],
14541
      'entity_id' => [
14551
        'description' => 'The Drupal entity id',
14561
        'type' => 'int',
14571
        'unsigned' => TRUE,
14581
        'not null' => FALSE,
14591
        'default' => NULL,
14601
      ],
14611
    ],
14621
    'primary key' => ['id'],
14631
    'unique keys' => [
14641
      'visit_media' => ['visit_id', 'media_id'],
14651
    ],
14661
    'indexes' => [
14671
      'visit_id' => ['visit_id'],
14681
      'entity' => ['entity_type', 'entity_id'],
14691
    ],
14701
  ];
1471
}
1472
1473
/**
1474
 * Schema for watched segments of one media view.
1475
 *
1476
 * Each segment column is 1 when any second in that bucket was played.
1477
 * The first five minutes use 15 second buckets. Later buckets are 30
1478
 * seconds, through two hours. The column name is the end of the bucket.
1479
 *
1480
 * @return array
1481
 *   The visitors_media_play table definition.
1482
 */
1483
function visitors_media_play_schema(): array {
148431
  $fields = [
148531
    'media_row' => [
148631
      'description' => 'The visitors_media id',
148731
      'type' => 'int',
148831
      'unsigned' => TRUE,
148931
      'not null' => TRUE,
149031
    ],
149131
  ];
149231
  foreach (MediaPlaySegments::columns() as $name) {
149331
    $fields[$name] = [
149431
      'description' => '1 when a second in this bucket was played',
149531
      'type' => 'int',
149631
      'size' => 'tiny',
149731
      'unsigned' => TRUE,
149831
      'not null' => TRUE,
149931
      'default' => 0,
150031
    ];
1501
  }
1502
150331
  return [
150431
    'description' => 'Watched segments for one media view.',
150531
    'fields' => $fields,
150631
    'primary key' => ['media_row'],
150731
  ];
1508
}
1509
1510
/**
1511
 * Adds the media log and watched segments.
1512
 */
1513
function visitors_update_30010(): void {
15141
  $schema = \Drupal::database()->schema();
15151
  if (!$schema->tableExists('visitors_media')) {
15161
    $schema->createTable('visitors_media', visitors_media_schema());
1517
  }
15181
  if (!$schema->tableExists('visitors_media_play')) {
15191
    $schema->createTable('visitors_media_play', visitors_media_play_schema());
1520
  }
1521
}
1522
1523
/**
1524
 * Adds time spent to each event.
1525
 */
1526
function visitors_update_30011(): void {
15271
  $schema = \Drupal::database()->schema();
15281
  if (!$schema->tableExists('visitors_event')) {
15291
    return;
1530
  }
15311
  if ($schema->fieldExists('visitors_event', 'time_spent')) {
15321
    return;
1533
  }
15341
  $schema->addField('visitors_event', 'time_spent', [
15351
    'description' => 'Seconds on this page, or until the next event.',
15361
    'type' => 'int',
15371
    'unsigned' => TRUE,
15381
    'not null' => TRUE,
15391
    'default' => 0,
15401
  ]);
1541
}
1542
1543
/**
1544
 * Adds the Device Detector bot name to each visit.
1545
 */
1546
function visitors_update_30012(): void {
15471
  $schema = \Drupal::database()->schema();
15481
  if (!$schema->tableExists('visitors_visit')) {
15491
    return;
1550
  }
15511
  if ($schema->fieldExists('visitors_visit', 'bot_name')) {
15521
    return;
1553
  }
15541
  $schema->addField('visitors_visit', 'bot_name', [
15551
    'description' => 'Device Detector bot name',
15561
    'type' => 'varchar',
15571
    'length' => 255,
15581
    'not null' => FALSE,
15591
    'default' => NULL,
15601
  ]);
1561
}
1562
1563
/**
1564
 * Adds the AI agent flag to each visit.
1565
 */
1566
function visitors_update_30013(): void {
15671
  $schema = \Drupal::database()->schema();
15681
  if (!$schema->tableExists('visitors_visit')) {
15691
    return;
1570
  }
15711
  if ($schema->fieldExists('visitors_visit', 'ai_agent')) {
15721
    return;
1573
  }
15741
  $schema->addField('visitors_visit', 'ai_agent', [
15751
    'description' => 'The visit was opened by an AI agent browser.',
15761
    'type' => 'int',
15771
    'size' => 'tiny',
15781
    'unsigned' => TRUE,
15791
    'not null' => TRUE,
15801
    'default' => 0,
15811
  ]);
1582
}
1583
1584
/**
1585
 * Adds campaign dimension columns to each visit.
1586
 */
1587
function visitors_update_30014(): void {
15881
  $schema = \Drupal::database()->schema();
15891
  if (!$schema->tableExists('visitors_visit')) {
15901
    return;
1591
  }
1592
  $columns = [
1593
    'campaign_source' => 'The campaign source',
1594
    'campaign_medium' => 'The campaign medium',
1595
    'campaign_content' => 'The campaign content',
1596
    'campaign_id' => 'The campaign id',
1597
    'campaign_group' => 'The campaign group',
1598
    'campaign_placement' => 'The campaign placement',
1599
  ];
16001
  foreach ($columns as $name => $description) {
16011
    if ($schema->fieldExists('visitors_visit', $name)) {
1602
      continue;
1603
    }
16041
    $schema->addField('visitors_visit', $name, [
16051
      'description' => $description,
16061
      'type' => 'varchar',
16071
      'length' => 255,
16081
      'not null' => FALSE,
16091
      'default' => NULL,
16101
    ]);
1611
  }
1612
}
1613
1614
/**
1615
 * Adds the Drupal entity pair to each media view.
1616
 */
1617
function visitors_update_30015(): void {
16181
  $schema = \Drupal::database()->schema();
16191
  if (!$schema->tableExists('visitors_media')) {
16201
    return;
1621
  }
16221
  $spec = visitors_media_schema();
16231
  foreach (['entity_type', 'entity_id'] as $field) {
16241
    if ($schema->fieldExists('visitors_media', $field)) {
1625
      continue;
1626
    }
16271
    $schema->addField('visitors_media', $field, $spec['fields'][$field]);
1628
  }
16291
  if (!$schema->indexExists('visitors_media', 'entity')) {
16301
    $schema->addIndex('visitors_media', 'entity', ['entity_type', 'entity_id'], $spec);
1631
  }
1632
}