Coverage index

visitors.install

Lines: 69.29%

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
15
/**
16
 * Implements hook_install().
17
 */
18
function visitors_install($is_syncing) {
193
  if ($is_syncing) {
201
    return;
21
  }
222
  if (!\Drupal::moduleHandler()->moduleExists('statistics')) {
231
    return;
24
  }
251
  $settings = \Drupal::service('config.factory')->getEditable('visitors.settings');
261
  $settings->set('counter.enabled', FALSE)
271
    ->save();
28
291
  \Drupal::messenger()->addWarning(t(
301
    'The Statistics module is installed. <a href="@url">Convert the Statistics node views</a> to Visitors page views.',
311
    ['@url' => Url::fromRoute('visitors.statistics_migrate')->toString()]
321
  ));
33
34
}
35
36
/**
37
 * Implements hook_schema().
38
 */
39
function visitors_schema() {
40
41
  $schema['visitors_visit'] = [
42
    'fields' => [
43
      'id' => [
44
        'description' => 'Primary key',
45
        'type' => 'serial',
46
        'not null' => TRUE,
47
      ],
48
      'visitor_id' => [
49
        'description' => 'Unique id, provided by the visitor',
50
        'type' => 'varchar',
51
        'length' => 32,
52
        'not null' => FALSE,
53
        'default' => NULL,
54
      ],
55
      'uid' => [
56
        'description' => 'The user of the session',
57
        'type' => 'varchar',
58
        'length' => 32,
59
        'not null' => FALSE,
60
      ],
61
      'localtime' => [
62
        'description' => 'Seconds since midnight, in the local of the visitor',
63
        'type' => 'int',
64
        'size' => 'medium',
65
        'not null' => FALSE,
66
        'default' => NULL,
67
      ],
68
      'returning' => [
69
        'description' => 'Is the visitor returning, or new',
70
        'type' => 'int',
71
        'size' => 'tiny',
72
      ],
73
      'total_visits' => [
74
        'description' => 'Total visits',
75
        'type' => 'int',
76
        'not null' => TRUE,
77
        'default' => 0,
78
      ],
79
      'total_page_views' => [
80
        'description' => 'Total page views this visit',
81
        'type' => 'int',
82
        'not null' => TRUE,
83
        'default' => 0,
84
      ],
85
      'total_events' => [
86
        'description' => 'Total events this visit',
87
        'type' => 'int',
88
        'not null' => TRUE,
89
        'default' => 0,
90
      ],
91
      'entry' => [
92
        'description' => 'First page view',
93
        'type' => 'int',
94
        'not null' => FALSE,
95
        'default' => NULL,
96
      ],
97
      'entry_time' => [
98
        'description' => 'The time the visit was started',
99
        'type' => 'int',
100
        'not null' => FALSE,
101
        'default' => NULL,
102
      ],
103
      'exit' => [
104
        'description' => 'Last page view',
105
        'type' => 'int',
106
        'not null' => FALSE,
107
        'default' => NULL,
108
      ],
109
      'exit_time' => [
110
        'description' => 'The time the visit was ended',
111
        'type' => 'int',
112
        'not null' => FALSE,
113
        'default' => NULL,
114
      ],
115
      'total_time' => [
116
        'description' => 'The total time of the visit',
117
        'type' => 'int',
118
        'not null' => FALSE,
119
        'default' => NULL,
120
      ],
121
      'time_since_first' => [
122
        'description' => 'Seconds since the visitor\'s first visit',
123
        'type' => 'int',
124
        'not null' => FALSE,
125
        'default' => NULL,
126
      ],
127
      'time_since_last' => [
128
        'description' => 'Seconds since the visitor\'s last visit',
129
        'type' => 'int',
130
        'not null' => FALSE,
131
        'default' => NULL,
132
      ],
133
      'bot' => [
134
        'description' => 'Is the visitor a bot',
135
        'type' => 'int',
136
        'size' => 'tiny',
137
        'not null' => FALSE,
138
        'default' => NULL,
139
      ],
140
      'bot_name' => [
141
        'description' => 'Device Detector bot name',
142
        'type' => 'varchar',
143
        'length' => 255,
144
        'not null' => FALSE,
145
        'default' => NULL,
146
      ],
147
      'ai_agent' => [
148
        'description' => 'The visit was opened by an AI agent browser.',
149
        'type' => 'int',
150
        'size' => 'tiny',
151
        'unsigned' => TRUE,
152
        'not null' => TRUE,
153
        'default' => 0,
154
      ],
155
      'config_id' => [
156
        'description' => 'The fingerprint id',
157
        'type' => 'varchar',
158
        'length' => 16,
159
        'not null' => TRUE,
160
      ],
161
      'config_resolution' => [
162
        'description' => 'The screen resolution',
163
        'type' => 'varchar',
164
        'length' => 18,
165
        'not null' => FALSE,
166
        'default' => NULL,
167
      ],
168
      'config_pdf' => [
169
        'description' => 'Has PDF support',
170
        'type' => 'int',
171
        'size' => 'tiny',
172
        'not null' => FALSE,
173
        'default' => NULL,
174
      ],
175
      'config_flash' => [
176
        'description' => 'Has Flash support',
177
        'type' => 'int',
178
        'size' => 'tiny',
179
        'not null' => FALSE,
180
        'default' => NULL,
181
      ],
182
      'config_java' => [
183
        'description' => 'Has Java support',
184
        'type' => 'int',
185
        'size' => 'tiny',
186
        'not null' => FALSE,
187
        'default' => NULL,
188
      ],
189
      'config_quicktime' => [
190
        'description' => 'Has Quicktime support',
191
        'type' => 'int',
192
        'size' => 'tiny',
193
        'not null' => FALSE,
194
        'default' => NULL,
195
      ],
196
      'config_realplayer' => [
197
        'description' => 'Has Realplayer support',
198
        'type' => 'int',
199
        'size' => 'tiny',
200
        'not null' => FALSE,
201
        'default' => NULL,
202
      ],
203
      'config_windowsmedia' => [
204
        'description' => 'Has Windows Media support',
205
        'type' => 'int',
206
        'size' => 'tiny',
207
        'not null' => FALSE,
208
        'default' => NULL,
209
      ],
210
      'config_silverlight' => [
211
        'description' => 'Has Silverlight support',
212
        'type' => 'int',
213
        'size' => 'tiny',
214
        'not null' => FALSE,
215
        'default' => NULL,
216
      ],
217
      'config_cookie' => [
218
        'description' => 'Has cookie support',
219
        'type' => 'int',
220
        'size' => 'tiny',
221
        'not null' => FALSE,
222
        'default' => NULL,
223
      ],
224
      'config_browser_engine' => [
225
        'description' => 'The browser engine',
226
        'type' => 'varchar',
227
        'length' => '10',
228
        'not null' => FALSE,
229
        'default' => NULL,
230
      ],
231
      'config_browser_name' => [
232
        'description' => 'The browser name',
233
        'type' => 'varchar',
234
        'length' => '2',
235
        'not null' => FALSE,
236
        'default' => NULL,
237
      ],
238
      'config_browser_version' => [
239
        'description' => 'The browser version',
240
        'type' => 'varchar',
241
        'length' => '20',
242
        'not null' => FALSE,
243
        'default' => NULL,
244
      ],
245
      'config_client_type' => [
246
        'description' => 'The client type',
247
        'type' => 'varchar',
248
        'length' => '100',
249
        'not null' => FALSE,
250
        'default' => NULL,
251
      ],
252
      'config_device_brand' => [
253
        'description' => 'The device brand',
254
        'type' => 'varchar',
255
        'length' => '100',
256
        'not null' => FALSE,
257
        'default' => NULL,
258
      ],
259
      'config_device_model' => [
260
        'description' => 'The device model',
261
        'type' => 'varchar',
262
        'length' => '100',
263
        'not null' => FALSE,
264
        'default' => NULL,
265
      ],
266
      'config_device_type' => [
267
        'description' => 'The device type',
268
        'type' => 'varchar',
269
        'length' => '100',
270
        'not null' => FALSE,
271
        'default' => NULL,
272
      ],
273
      'config_os' => [
274
        'description' => 'The operating system',
275
        'type' => 'varchar',
276
        'length' => '3',
277
        'not null' => FALSE,
278
        'default' => NULL,
279
      ],
280
      'config_os_version' => [
281
        'description' => 'The operating system version',
282
        'type' => 'varchar',
283
        'length' => '100',
284
        'not null' => FALSE,
285
        'default' => NULL,
286
      ],
287
      'location_browser_lang' => [
288
        'description' => 'The browser language',
289
        'type' => 'varchar',
290
        'length' => 20,
291
        'not null' => FALSE,
292
        'default' => NULL,
293
      ],
294
      'location_ip' => [
295
        'description' => 'The IP address',
296
        'type' => 'varchar',
297
        'length' => 45,
298
        'not null' => TRUE,
299
        'default' => '',
300
      ],
301
      'location_continent' => [
302
        'description' => 'The continent of the visitor',
303
        'type' => 'varchar',
304
        'length' => 2,
305
        'not null' => FALSE,
306
        'default' => '',
307
      ],
308
      'location_country' => [
309
        'description' => 'The country of the visitor',
310
        'type' => 'varchar',
311
        'length' => 2,
312
        'not null' => FALSE,
313
        'default' => '',
314
      ],
315
      'location_region' => [
316
        'description' => 'The region of the visitor',
317
        'type' => 'varchar',
318
        'length' => 128,
319
        'not null' => FALSE,
320
        'default' => '',
321
      ],
322
      'location_city' => [
323
        'description' => 'The city of the visitor',
324
        'type' => 'varchar',
325
        'length' => 128,
326
        'not null' => FALSE,
327
        'default' => '',
328
      ],
329
      'location_latitude' => [
330
        'description' => 'The latitude from the IP address',
331
        'type' => 'numeric',
332
        'precision' => 13,
333
        'scale' => 10,
334
        'default' => NULL,
335
      ],
336
      'location_longitude' => [
337
        'description' => 'The longitude from the IP address',
338
        'type' => 'numeric',
339
        'precision' => 13,
340
        'scale' => 10,
341
        'default' => NULL,
342
      ],
343
      'referer_keyword' => [
344
        'description' => 'The referer keyword',
345
        'type' => 'varchar',
346
        'length' => 255,
347
        'not null' => FALSE,
348
        'default' => NULL,
349
      ],
350
      'referer_name' => [
351
        'description' => 'The referer name',
352
        'type' => 'varchar',
353
        'length' => 255,
354
        'not null' => FALSE,
355
        'default' => NULL,
356
      ],
357
      'referer_type' => [
358
        'description' => 'The referer type',
359
        'type' => 'varchar',
360
        'length' => 32,
361
        'not null' => FALSE,
362
        'default' => NULL,
363
      ],
364
      'referer_url' => [
365
        'description' => 'The referer URL',
366
        'type' => 'varchar',
367
        'length' => 1500,
368
        'not null' => FALSE,
369
        'default' => NULL,
370
      ],
371
      'campaign_source' => [
372
        'description' => 'The campaign source',
373
        'type' => 'varchar',
374
        'length' => 255,
375
        'not null' => FALSE,
376
        'default' => NULL,
377
      ],
378
      'campaign_medium' => [
379
        'description' => 'The campaign medium',
380
        'type' => 'varchar',
381
        'length' => 255,
382
        'not null' => FALSE,
383
        'default' => NULL,
384
      ],
385
      'campaign_content' => [
386
        'description' => 'The campaign content',
387
        'type' => 'varchar',
388
        'length' => 255,
389
        'not null' => FALSE,
390
        'default' => NULL,
391
      ],
392
      'campaign_id' => [
393
        'description' => 'The campaign id',
394
        'type' => 'varchar',
395
        'length' => 255,
396
        'not null' => FALSE,
397
        'default' => NULL,
398
      ],
399
      'campaign_group' => [
400
        'description' => 'The campaign group',
401
        'type' => 'varchar',
402
        'length' => 255,
403
        'not null' => FALSE,
404
        'default' => NULL,
405
      ],
406
      'campaign_placement' => [
407
        'description' => 'The campaign placement',
408
        'type' => 'varchar',
409
        'length' => 255,
410
        'not null' => FALSE,
411
        'default' => NULL,
412
      ],
413
    ],
414
    'primary key' => ['id'],
415
    'indexes' => [
416
      'visitor_id' => ['visitor_id'],
417
      'bot' => ['bot'],
418
      'exit_time' => ['exit_time'],
419
      'uid' => ['uid'],
420
    ],
421
  ];
422
42327
  $schema['visitors_event'] = [
42427
    'fields' => [
42527
      'id' => [
42627
        'description' => 'Primary key',
42727
        'type' => 'serial',
42827
        'not null' => TRUE,
42927
      ],
43027
      'visit_id' => [
43127
        'description' => 'The visitors_visit id',
43227
        'type' => 'int',
43327
      ],
43427
      'page_view' => [
43527
        'description' => 'The unique page view id',
43627
        'type' => 'varchar',
43727
        'length' => 32,
43827
        'not null' => FALSE,
43927
        'default' => NULL,
44027
      ],
44127
      'plugin' => [
44227
        'description' => 'The plugin that generated the event',
44327
        'type' => 'varchar',
44427
        'length' => 255,
44527
        'not null' => FALSE,
44627
        'default' => NULL,
44727
      ],
44827
      'event' => [
44927
        'description' => 'The event',
45027
        'type' => 'varchar',
45127
        'length' => 255,
45227
        'not null' => FALSE,
45327
        'default' => NULL,
45427
      ],
45527
      'plugin_int_1' => [
45627
        'description' => 'The first event int variable',
45727
        'type' => 'int',
45827
        'not null' => FALSE,
45927
        'default' => NULL,
46027
      ],
46127
      'plugin_int_2' => [
46227
        'description' => 'The second event int variable',
46327
        'type' => 'int',
46427
        'not null' => FALSE,
46527
        'default' => NULL,
46627
      ],
46727
      'plugin_var_1' => [
46827
        'description' => 'The first event varchar variable',
46927
        'type' => 'varchar',
47027
        'length' => 255,
47127
        'not null' => FALSE,
47227
        'default' => NULL,
47327
      ],
47427
      'plugin_var_2' => [
47527
        'description' => 'The second event varchar variable',
47627
        'type' => 'varchar',
47727
        'length' => 255,
47827
        'not null' => FALSE,
47927
        'default' => NULL,
48027
      ],
48127
      'plugin_var_3' => [
48227
        'description' => 'The third event varchar variable',
48327
        'type' => 'varchar',
48427
        'length' => 255,
48527
        'not null' => FALSE,
48627
        'default' => NULL,
48727
      ],
48827
      'plugin_var_4' => [
48927
        'description' => 'The fourth event varchar variable',
49027
        'type' => 'varchar',
49127
        'length' => 255,
49227
        'not null' => FALSE,
49327
        'default' => NULL,
49427
      ],
49527
      'event_category' => [
49627
        'description' => 'The Matomo event category',
49727
        'type' => 'varchar',
49827
        'length' => 255,
49927
        'not null' => FALSE,
50027
        'default' => NULL,
50127
      ],
50227
      'event_action' => [
50327
        'description' => 'The Matomo event action',
50427
        'type' => 'varchar',
50527
        'length' => 255,
50627
        'not null' => FALSE,
50727
        'default' => NULL,
50827
      ],
50927
      'event_name' => [
51027
        'description' => 'The Matomo event name',
51127
        'type' => 'varchar',
51227
        'length' => 255,
51327
        'not null' => FALSE,
51427
        'default' => NULL,
51527
      ],
51627
      'event_value' => [
51727
        'description' => 'The Matomo event value',
51827
        'type' => 'float',
51927
        'size' => 'big',
52027
        'not null' => FALSE,
52127
        'default' => NULL,
52227
      ],
52327
      'title' => [
52427
        'description' => 'The page title',
52527
        'type' => 'text',
52627
        'not null' => TRUE,
52727
      ],
52827
      'uid' => [
52927
        'description' => 'The user who viewed the page',
53027
        'type' => 'varchar',
53127
        'length' => 32,
53227
        'not null' => FALSE,
53327
      ],
53427
      'url_prefix' => [
53527
        'description' => 'The protocol and if started with www',
53627
        'type' => 'int',
53727
        'size' => 'small',
53827
      ],
53927
      'url' => [
54027
        'description' => 'The page URL',
54127
        'type' => 'text',
54227
        'not null' => TRUE,
54327
      ],
54427
      'path' => [
54527
        'description' => 'The Drupal path',
54627
        'type' => 'varchar',
54727
        'length' => 255,
54827
        'not null' => TRUE,
54927
        'default' => '',
55027
      ],
55127
      'route' => [
55227
        'description' => 'The Drupal route name',
55327
        'type' => 'varchar',
55427
        'length' => 255,
55527
        'not null' => TRUE,
55627
        'default' => '',
55727
      ],
55827
      'referrer_url' => [
55927
        'description' => 'The referrer URL, without the protocol or www.',
56027
        'type' => 'text',
56127
        'not null' => TRUE,
56227
      ],
56327
      'referrer_url_prefix' => [
56427
        'description' => 'The protocol and whether the referrer started with www.',
56527
        'type' => 'int',
56627
        'size' => 'small',
56727
        'not null' => FALSE,
56827
        'default' => NULL,
56927
      ],
57027
      'referrer_internal' => [
57127
        'description' => 'Whether the referrer host matches the page host.',
57227
        'type' => 'int',
57327
        'size' => 'tiny',
57427
        'not null' => TRUE,
57527
        'default' => 0,
57627
      ],
57727
      'referrer_path' => [
57827
        'description' => 'The Drupal path of an internal referrer.',
57927
        'type' => 'varchar',
58027
        'length' => 255,
58127
        'not null' => TRUE,
58227
        'default' => '',
58327
      ],
58427
      'referrer_route' => [
58527
        'description' => 'The Drupal route of an internal referrer.',
58627
        'type' => 'varchar',
58727
        'length' => 255,
58827
        'not null' => TRUE,
58927
        'default' => '',
59027
      ],
59127
      'server' => [
59227
        'description' => 'The server that generated the response',
59327
        'type' => 'varchar',
59427
        'length' => 255,
59527
        'not null' => FALSE,
59627
        'default' => NULL,
59727
      ],
59827
      'pf_network' => [
59927
        'description' => 'Network performance',
60027
        'type' => 'int',
60127
        'not null' => FALSE,
60227
        'default' => NULL,
60327
      ],
60427
      'pf_server' => [
60527
        'description' => 'Server performance',
60627
        'type' => 'int',
60727
        'not null' => FALSE,
60827
        'default' => NULL,
60927
      ],
61027
      'pf_transfer' => [
61127
        'description' => 'Transfer performance',
61227
        'type' => 'int',
61327
        'not null' => FALSE,
61427
        'default' => NULL,
61527
      ],
61627
      'pf_dom_processing' => [
61727
        'description' => 'DOM processing performance',
61827
        'type' => 'int',
61927
        'not null' => FALSE,
62027
        'default' => NULL,
62127
      ],
62227
      'pf_dom_complete' => [
62327
        'description' => 'DOM complete performance',
62427
        'type' => 'int',
62527
        'not null' => FALSE,
62627
        'default' => NULL,
62727
      ],
62827
      'pf_on_load' => [
62927
        'description' => 'On load performance',
63027
        'type' => 'int',
63127
        'not null' => FALSE,
63227
        'default' => NULL,
63327
      ],
63427
      'pf_total' => [
63527
        'description' => 'Total performance',
63627
        'type' => 'int',
63727
        'not null' => FALSE,
63827
        'default' => NULL,
63927
      ],
64027
      'created' => [
64127
        'description' => 'The time the request was made',
64227
        'type' => 'int',
64327
      ],
64427
      'time_spent' => [
64527
        'description' => 'Seconds on this page, or until the next event.',
64627
        'type' => 'int',
64727
        'unsigned' => TRUE,
64827
        'not null' => TRUE,
64927
        'default' => 0,
65027
      ],
65127
    ],
65227
    'primary key' => ['id'],
65327
    'indexes' => [
65427
      'visit_id' => ['visit_id'],
65527
      'created' => ['created'],
65627
    ],
65727
  ];
658
65927
  $schema['visitors_media'] = visitors_media_schema();
660
66127
  $schema['visitors_counter'] = [
66227
    'fields' => [
66327
      'entity_id' => [
66427
        'description' => 'The entity id for these visits.',
66527
        'type' => 'int',
66627
        'unsigned' => TRUE,
66727
        'not null' => TRUE,
66827
        'default' => 0,
66927
      ],
67027
      'entity_type' => [
67127
        'type' => 'varchar_ascii',
67227
        'not null' => TRUE,
67327
        'default' => 'node',
67427
        'length' => EntityTypeInterface::ID_MAX_LENGTH,
67527
        'description' => 'The entity_type of the entity for these visits.',
67627
      ],
67727
      'total' => [
67827
        'description' => 'The total number of times the entity has been viewed.',
67927
        'type' => 'int',
68027
        'unsigned' => TRUE,
68127
        'not null' => TRUE,
68227
        'default' => 0,
68327
        'size' => 'big',
68427
      ],
68527
      'today' => [
68627
        'description' => 'The total number of times the entity has been viewed today.',
68727
        'type' => 'int',
68827
        'unsigned' => TRUE,
68927
        'not null' => TRUE,
69027
        'default' => 0,
69127
        'size' => 'medium',
69227
      ],
69327
      'timestamp' => [
69427
        'description' => 'The most recent time the entity has been viewed.',
69527
        'type' => 'int',
69627
        'unsigned' => TRUE,
69727
        'not null' => TRUE,
69827
        'default' => 0,
69927
      ],
70027
    ],
70127
    'primary key' => ['entity_type', 'entity_id'],
70227
  ];
703
70427
  return $schema;
705
}
706
707
/**
708
 * Implements hook_requirements().
709
 */
710
function visitors_requirements($phase) {
7113
  $requirements = [];
7123
  if ($phase != 'runtime') {
7131
    return $requirements;
714
  }
7152
  $device = \Drupal::service('visitors.device');
7162
  if (!$device->hasLibrary()) {
7171
    $requirements['visitors_device'] = [
7181
      'title' => t('Device Detector'),
7191
      'value' => t('Not found'),
7201
      'severity' => DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '11.2.0', fn() => RequirementSeverity::Warning, fn() => REQUIREMENT_WARNING),
7211
      'description' => t('The <a href=":url">Device Detector</a> library is strongly encouraged by the Visitors module. Composer will download the library automatically.', [
7221
        ':url' => 'https://github.com/matomo-org/device-detector',
7231
      ]),
7241
    ];
725
  }
726
7272
  return $requirements;
728
}
729
730
/**
731
 * Upgrade to Visitors 3.
732
 */
733
function visitors_update_30000(&$sandbox) {
734
7358
  if (!isset($sandbox['current'])) {
7366
    $sandbox['current'] = 0;
7376
    $sandbox['max'] = 5;
738
  }
739
740
  // Change visitors.settings to visitors.settings.
7418
  if ($sandbox['current'] == 0) {
7426
    $config_factory = \Drupal::configFactory();
743
7446
    $config = $config_factory->getEditable('visitors.config');
7456
    $config_data = $config->getRawData();
746
7476
    $settings = $config_factory->getEditable('visitors.settings');
7486
    $settings->setData($config_data)->save();
749
7506
    $config->delete();
751
  }
752
7538
  if ($sandbox['current'] == 1) {
7545
    $schema = \Drupal::database()->schema();
755
    // @todo replace visitors_schema() with static before an initial release.
7565
    $visitors_schema = visitors_schema();
7575
    $schema->createTable('visitors_visit', $visitors_schema['visitors_visit']);
7585
    $schema->createTable('visitors_event', $visitors_schema['visitors_event']);
759
  }
760
7618
  if ($sandbox['current'] == 2) {
7626
    $config_factory = \Drupal::configFactory();
763
764
    // @todo This needs to be replace with static before an initial release.
7656
    $view = file_get_contents(__DIR__ . '/config/optional/views.view.visitors.yml');
7666
    $config_data = Yaml::decode($view);
7676
    $config_factory->getEditable('views.view.visitors')->setData($config_data)->save();
768
769
    // @todo This needs to be replace with static before an initial release.
7706
    $spam = file_get_contents(__DIR__ . '/config/install/visitors.spam.yml');
7716
    $config_data = Yaml::decode($spam);
7726
    $config_factory->getEditable('visitors.spam')->setData($config_data)->save();
773
774
    // @todo This needs to be replace with static before an initial release.
7756
    $ai_assistants = file_get_contents(__DIR__ . '/config/install/visitors.ai_assistants.yml');
7766
    $config_data = Yaml::decode($ai_assistants);
7776
    $config_factory->getEditable('visitors.ai_assistants')->setData($config_data)->save();
778
779
    // @todo This needs to be replace with static before an initial release.
7806
    $search_engine = file_get_contents(__DIR__ . '/config/install/visitors.search_engines.yml');
7816
    $config_data = Yaml::decode($search_engine);
7826
    $config_factory->getEditable('visitors.search_engine')->setData($config_data)->save();
783
784
    // @todo This needs to be replace with static before an initial release.
7856
    $social_networks = file_get_contents(__DIR__ . '/config/install/visitors.social_networks.yml');
7866
    $config_data = Yaml::decode($social_networks);
7876
    $config_factory->getEditable('visitors.social_networks')->setData($config_data)->save();
788
789
    // Check if views 'views.view.visitors_geoip' exists, and delete it.
7906
    $geoip_view = $config_factory->getEditable('views.view.visitors_geoip');
7916
    if ($geoip_view) {
7926
      $geoip_view->delete();
793
    }
794
795
  }
796
7978
  if ($sandbox['current'] == 3) {
7985
    $visitors_geoip_is_installed = \Drupal::moduleHandler()->moduleExists('visitors_geoip');
7995
    if ($visitors_geoip_is_installed) {
8001
      \Drupal::service('module_installer')->uninstall(['visitors_geoip']);
801
    }
802
  }
803
8048
  if ($sandbox['current'] == 4) {
805
    // Migrate existing visibility settings to condition-based system.
8066
    $config_factory = \Drupal::configFactory();
8076
    $settings = $config_factory->getEditable('visitors.settings');
808
8096
    $tracking_conditions = [];
810
811
    // Migrate request path visibility settings.
8126
    $request_path_pages = $settings->get('visibility.request_path_pages') ?: '';
8136
    $request_path_mode = $settings->get('visibility.request_path_mode') ?: 0;
814
8156
    if (!empty($request_path_pages)) {
8160
      $tracking_conditions['request_path'] = [
8170
        'id' => 'request_path',
8180
        'pages' => $request_path_pages,
819
      // Legacy mode 0 = exclude, mode 1 = include.
8200
        'negate' => $request_path_mode === 0,
8210
      ];
822
    }
823
824
    // Migrate user role visibility settings.
8256
    $user_role_roles = $settings->get('visibility.user_role_roles') ?: [];
8266
    $user_role_mode = $settings->get('visibility.user_role_mode') ?: 0;
827
8286
    if (!empty($user_role_roles)) {
8290
      $tracking_conditions['user_role'] = [
8300
        'id' => 'user_role',
8310
        'roles' => $user_role_roles,
832
      // Legacy mode 0 = exclude, mode 1 = include.
8330
        'negate' => $user_role_mode === 0,
8340
      ];
835
    }
836
837
    // Add exclude user 1 condition if configured.
8386
    if ($settings->get('visibility.exclude_user1')) {
8390
      $tracking_conditions['exclude_admin'] = [
8400
        'id' => 'user_role',
8410
        'roles' => ['administrator'],
8420
        'negate' => TRUE,
8430
      ];
844
    }
845
846
    // Save new condition-based configuration.
8476
    if (!empty($tracking_conditions)) {
8480
      $settings->set('tracking_conditions', $tracking_conditions);
849
    }
850
851
    // Remove old visibility configuration.
8526
    $settings->clear('visibility.request_path_mode')
8536
      ->clear('visibility.request_path_pages')
8546
      ->clear('visibility.user_role_mode')
8556
      ->clear('visibility.user_role_roles')
8566
      ->clear('visibility.exclude_user1')
8576
      ->save();
858
  }
859
8608
  $sandbox['current'] += 1;
861
8628
  $sandbox['#finished'] = ($sandbox['max'] == $sandbox['current']) ? 1 : $sandbox['current'] / $sandbox['max'];
863
}
864
865
/**
866
 * Adds the client debug setting.
867
 */
868
function visitors_update_30001() {
8691
  $config = \Drupal::configFactory()->getEditable('visitors.settings');
8701
  if ($config->get('client_debug') === NULL) {
8711
    $config->set('client_debug', FALSE)->save();
872
  }
873
}
874
875
/**
876
 * Upgrades legacy 3.0.0-alpha database schemas.
877
 *
878
 * Alpha releases changed the schema without update hooks. The installed
879
 * schema version stayed at 30000, so later releases could not alter the
880
 * tables. Inspect the tables to see which alpha created them.
881
 *
882
 * Field specs are copied here so later edits to hook_schema() do not
883
 * change this upgrade.
884
 *
885
 * @return \Drupal\Core\StringTranslation\TranslatableMarkup
886
 *   A message naming the detected release.
887
 */
888
function visitors_update_30002(): TranslatableMarkup {
8894
  $schema = \Drupal::database()->schema();
8904
  if ($schema->tableExists('visitors_action')) {
8911
    $version = '3.0.0-alpha1';
892
  }
8934
  elseif (!$schema->fieldExists('visitors_visit', 'referer_url')) {
8941
    $version = '3.0.0-alpha2';
895
  }
8963
  elseif ($schema->fieldExists('visitors_visit', 'visit_count')) {
8971
    $version = '3.0.0-alpha3';
898
  }
899
  else {
9002
    $version = 'current';
901
  }
902
903
  // Alpha1 stored events in visitors_action.
9044
  if ($schema->tableExists('visitors_action')
9051
    && !$schema->tableExists('visitors_event')) {
9061
    $schema->renameTable('visitors_action', 'visitors_event');
907
  }
9084
  if ($schema->tableExists('visitors_event')
9094
    && $schema->fieldExists('visitors_event', 'action')
9101
    && !$schema->fieldExists('visitors_event', 'event')) {
9111
    $schema->changeField('visitors_event', 'action', 'event', [
9121
      'description' => 'The event',
9131
      'type' => 'varchar',
9141
      'length' => 255,
9151
      'not null' => FALSE,
9161
      'default' => NULL,
9171
    ]);
918
  }
919
920
  // Alpha2 had no referer columns.
921
  $referer_fields = [
922
    'referer_keyword' => [
923
      'description' => 'The referer keyword',
924
      'type' => 'varchar',
925
      'length' => 255,
926
      'not null' => FALSE,
927
      'default' => NULL,
928
    ],
929
    'referer_name' => [
930
      'description' => 'The referer name',
931
      'type' => 'varchar',
932
      'length' => 255,
933
      'not null' => FALSE,
934
      'default' => NULL,
935
    ],
936
    'referer_type' => [
937
      'description' => 'The referer type',
938
      'type' => 'varchar',
939
      'length' => 32,
940
      'not null' => FALSE,
941
      'default' => NULL,
942
    ],
943
    'referer_url' => [
944
      'description' => 'The referer URL',
945
      'type' => 'varchar',
946
      'length' => 1500,
947
      'not null' => FALSE,
948
      'default' => NULL,
949
    ],
950
  ];
9514
  foreach ($referer_fields as $field => $spec) {
9524
    if (!$schema->fieldExists('visitors_visit', $field)) {
9532
      $schema->addField('visitors_visit', $field, $spec);
954
    }
955
  }
956
957
  // Alpha3 used visit_count and page_count. NULLs cannot move into the
958
  // NOT NULL replacements, so store 0 first and keep the old values.
959
  $count_renames = [
960
    'visit_count' => [
961
      'field' => 'total_visits',
962
      'spec' => [
963
        'description' => 'Total visits',
964
        'type' => 'int',
965
        'not null' => TRUE,
966
        'default' => 0,
967
      ],
968
    ],
969
    'page_count' => [
970
      'field' => 'total_page_views',
971
      'spec' => [
972
        'description' => 'Total page views this visit',
973
        'type' => 'int',
974
        'not null' => TRUE,
975
        'default' => 0,
976
      ],
977
    ],
978
  ];
9794
  foreach ($count_renames as $old_field => $replacement) {
9804
    $new_field = $replacement['field'];
9814
    if ($schema->fieldExists('visitors_visit', $old_field)) {
9823
      \Drupal::database()->update('visitors_visit')
9833
        ->fields([$old_field => 0])
9843
        ->isNull($old_field)
9853
        ->execute();
9863
      $schema->changeField(
9873
        'visitors_visit',
9883
        $old_field,
9893
        $new_field,
9903
        $replacement['spec'],
9913
      );
992
    }
9932
    elseif (!$schema->fieldExists('visitors_visit', $new_field)) {
9940
      $schema->addField('visitors_visit', $new_field, $replacement['spec']);
995
    }
996
  }
997
998
  $added_fields = [
999
    'total_events' => [
1000
      'description' => 'Total events this visit',
1001
      'type' => 'int',
1002
      'not null' => TRUE,
1003
      'default' => 0,
1004
    ],
1005
    'time_since_first' => [
1006
      'description' => 'Seconds since the visitor\'s first visit',
1007
      'type' => 'int',
1008
      'not null' => FALSE,
1009
      'default' => NULL,
1010
    ],
1011
    'time_since_last' => [
1012
      'description' => 'Seconds since the visitor\'s last visit',
1013
      'type' => 'int',
1014
      'not null' => FALSE,
1015
      'default' => NULL,
1016
    ],
1017
  ];
10184
  foreach ($added_fields as $field => $spec) {
10194
    if (!$schema->fieldExists('visitors_visit', $field)) {
10203
      $schema->addField('visitors_visit', $field, $spec);
1021
    }
1022
  }
1023
10244
  if ($version === 'current') {
10252
    return t('Visitors schema is already current.');
1026
  }
10273
  return t('Upgraded Visitors schema from @version.', [
10283
    '@version' => $version,
10293
  ]);
1030
}
1031
1032
/**
1033
 * Adds Matomo trackEvent columns to {visitors_event}.
1034
 *
1035
 * Field specs are copied here so later edits to hook_schema() do not
1036
 * change this upgrade.
1037
 */
1038
function visitors_update_30003(): void {
10391
  $schema = \Drupal::database()->schema();
1040
  $fields = [
1041
    'event_category' => [
1042
      'description' => 'The Matomo event category',
1043
      'type' => 'varchar',
1044
      'length' => 255,
1045
      'not null' => FALSE,
1046
      'default' => NULL,
1047
    ],
1048
    'event_action' => [
1049
      'description' => 'The Matomo event action',
1050
      'type' => 'varchar',
1051
      'length' => 255,
1052
      'not null' => FALSE,
1053
      'default' => NULL,
1054
    ],
1055
    'event_name' => [
1056
      'description' => 'The Matomo event name',
1057
      'type' => 'varchar',
1058
      'length' => 255,
1059
      'not null' => FALSE,
1060
      'default' => NULL,
1061
    ],
1062
    'event_value' => [
1063
      'description' => 'The Matomo event value',
1064
      'type' => 'float',
1065
      'size' => 'big',
1066
      'not null' => FALSE,
1067
      'default' => NULL,
1068
    ],
1069
  ];
10701
  foreach ($fields as $field => $spec) {
10711
    if (!$schema->fieldExists('visitors_event', $field)) {
10721
      $schema->addField('visitors_event', $field, $spec);
1073
    }
1074
  }
1075
}
1076
1077
/**
1078
 * Stores the user id as a 32 character string.
1079
 *
1080
 * Field specs are copied here so later edits to hook_schema() do not
1081
 * change this upgrade. Existing integers are kept as decimal strings.
1082
 */
1083
function visitors_update_30004(): void {
10841
  $schema = \Drupal::database()->schema();
10851
  $visit_uid = [
10861
    'description' => 'The user of the session',
10871
    'type' => 'varchar',
10881
    'length' => 32,
10891
    'not null' => FALSE,
10901
  ];
10911
  $event_uid = [
10921
    'description' => 'The user who viewed the page',
10931
    'type' => 'varchar',
10941
    'length' => 32,
10951
    'not null' => FALSE,
10961
  ];
1097
10981
  if ($schema->fieldExists('visitors_visit', 'uid')) {
10991
    if ($schema->indexExists('visitors_visit', 'uid')) {
11001
      $schema->dropIndex('visitors_visit', 'uid');
1101
    }
11021
    $schema->changeField('visitors_visit', 'uid', 'uid', $visit_uid);
11031
    if (!$schema->indexExists('visitors_visit', 'uid')) {
11041
      $schema->addIndex('visitors_visit', 'uid', ['uid'], [
11051
        'fields' => [
11061
          'uid' => $visit_uid,
11071
        ],
11081
      ]);
1109
    }
1110
  }
1111
11121
  if ($schema->fieldExists('visitors_event', 'uid')) {
11131
    $schema->changeField('visitors_event', 'uid', 'uid', $event_uid);
1114
  }
1115
}
1116
1117
/**
1118
 * There is no separate scroll depth report.
1119
 *
1120
 * Databases that already ran the first version of this update still have
1121
 * the display. The following update removes it.
1122
 */
1123
function visitors_update_30005(): void {
1124
  // Scroll depth is stored as events. It does not have its own report.
11250
}
1126
1127
/**
1128
 * Removes the scroll depth display from the Visitors view.
1129
 */
1130
function visitors_update_30006(): void {
11311
  $storage = \Drupal::entityTypeManager()->getStorage('view');
1132
  /** @var \Drupal\views\Entity\View|null $view */
11331
  $view = $storage->load('visitors');
11341
  if (!$view) {
11350
    return;
1136
  }
1137
11381
  $displays = $view->get('display');
11391
  if (!isset($displays['scroll_depth_table'])) {
11400
    return;
1141
  }
1142
11431
  unset($displays['scroll_depth_table']);
11441
  $view->set('display', $displays);
11451
  $view->save();
1146
}
1147
1148
/**
1149
 * Splits referrer URLs into a prefix, host, and internal path.
1150
 */
1151
function visitors_update_30007(&$sandbox): void {
11521
  $database = \Drupal::database();
11531
  $schema = $database->schema();
1154
11551
  if (!isset($sandbox['last_id'])) {
11561
    $fields = visitors_schema()['visitors_event']['fields'];
11571
    foreach ([
11581
      'referrer_url_prefix',
11591
      'referrer_internal',
11601
      'referrer_path',
11611
      'referrer_route',
11621
    ] as $name) {
11631
      if (!$schema->fieldExists('visitors_event', $name)) {
11640
        $schema->addField('visitors_event', $name, $fields[$name]);
1165
      }
1166
    }
11671
    visitors_update_30007_displays();
1168
11691
    $sandbox['last_id'] = 0;
11701
    $sandbox['progress'] = 0;
11711
    $sandbox['max'] = (int) $database->select('visitors_event', 'e')
11721
      ->countQuery()
11731
      ->execute()
11741
      ->fetchField();
1175
  }
1176
11771
  if ($sandbox['max'] === 0) {
11780
    $sandbox['#finished'] = 1;
11790
    return;
1180
  }
1181
1182
  /** @var \Drupal\visitors\VisitorsReferrerInterface $referrer */
11831
  $referrer = \Drupal::service('visitors.referrer');
11841
  $rows = $database->select('visitors_event', 'e')
11851
    ->fields('e', ['id', 'referrer_url', 'url'])
11861
    ->condition('id', $sandbox['last_id'], '>')
11871
    ->orderBy('id')
11881
    ->range(0, 50)
11891
    ->execute();
1190
11911
  $count = 0;
11921
  foreach ($rows as $row) {
11931
    $count++;
11941
    $sandbox['last_id'] = (int) $row->id;
11951
    $page_host = $referrer->hostOf((string) $row->url);
11961
    $database->update('visitors_event')
11971
      ->fields($referrer->storageFields((string) $row->referrer_url, $page_host))
11981
      ->condition('id', $row->id)
11991
      ->execute();
12001
    $sandbox['progress']++;
1201
  }
1202
12031
  if ($count === 0) {
12040
    $sandbox['#finished'] = 1;
12050
    return;
1206
  }
1207
12081
  $sandbox['#finished'] = min(0.99, $sandbox['progress'] / $sandbox['max']);
12091
  if ($sandbox['progress'] >= $sandbox['max']) {
12101
    $sandbox['#finished'] = 1;
1211
  }
1212
}
1213
1214
/**
1215
 * Copies the referrer displays from the shipped Visitors view.
1216
 */
1217
function visitors_update_30007_displays(): void {
12181
  $view_yaml = file_get_contents(__DIR__ . '/config/optional/views.view.visitors.yml');
12191
  if ($view_yaml === FALSE) {
12200
    return;
1221
  }
12221
  $config_data = Yaml::decode($view_yaml);
12231
  $storage = \Drupal::entityTypeManager()->getStorage('view');
1224
  /** @var \Drupal\views\Entity\View|null $view */
12251
  $view = $storage->load('visitors');
12261
  if (!$view || !isset($config_data['display'])) {
12270
    return;
1228
  }
1229
12301
  $displays = $view->get('display');
12311
  foreach (['referrers_table', 'referrer_table'] as $id) {
12321
    if (isset($config_data['display'][$id])) {
12331
      $displays[$id] = $config_data['display'][$id];
1234
    }
1235
  }
12361
  $view->set('display', $displays);
12371
  $view->save();
1238
}
1239
1240
/**
1241
 * Stores same-site visit referrers as direct entry.
1242
 */
1243
function visitors_update_30008(): void {
12441
  $database = \Drupal::database();
12451
  if ($database->schema()->tableExists('visitors_visit')) {
12461
    $database->update('visitors_visit')
12471
      ->fields(['referer_type' => 'direct'])
12481
      ->condition('referer_type', 'internal')
12491
      ->execute();
1250
  }
1251
12521
  $view_yaml = file_get_contents(__DIR__ . '/config/optional/views.view.visitors.yml');
12531
  if ($view_yaml === FALSE) {
12540
    return;
1255
  }
12561
  $config_data = Yaml::decode($view_yaml);
12571
  $storage = \Drupal::entityTypeManager()->getStorage('view');
1258
  /** @var \Drupal\views\Entity\View|null $view */
12591
  $view = $storage->load('visitors');
12601
  if (!$view || !isset($config_data['display'])) {
12610
    return;
1262
  }
1263
12641
  $displays = $view->get('display');
1265
  $ids = [
1266
    'referrer_overview_pie',
1267
    'referrer_overview_table',
1268
    'referrers_all_table',
1269
    'search_engines_table',
1270
    'search_keywords_table',
1271
    'search_engine_keywords',
1272
    'search_keyword_engines',
1273
    'websites_table',
1274
    'website_urls',
1275
    'social_referrers_table',
1276
    'social_urls',
1277
    'ai_visitors_pie',
1278
    'ai_visitors_table',
1279
    'ai_entry_pages',
1280
    'campaigns_table',
1281
    'campaign_keywords',
1282
  ];
12831
  foreach ($ids as $id) {
12841
    if (isset($config_data['display'][$id])) {
12851
      $displays[$id] = $config_data['display'][$id];
1286
    }
1287
  }
12881
  $view->set('display', $displays);
12891
  $view->save();
1290
}
1291
1292
/**
1293
 * Adds the Downloads report.
1294
 */
1295
function visitors_update_30009(): void {
12960
  $view_yaml = file_get_contents(__DIR__ . '/config/optional/views.view.visitors.yml');
12970
  if ($view_yaml === FALSE) {
12980
    return;
1299
  }
13000
  $config_data = Yaml::decode($view_yaml);
13010
  $storage = \Drupal::entityTypeManager()->getStorage('view');
1302
  /** @var \Drupal\views\Entity\View|null $view */
13030
  $view = $storage->load('visitors');
13040
  if (!$view || !isset($config_data['display']['downloads_table'])) {
13050
    return;
1306
  }
1307
13080
  $displays = $view->get('display');
13090
  $displays['downloads_table'] = $config_data['display']['downloads_table'];
13100
  $view->set('display', $displays);
13110
  $view->save();
1312
}
1313
1314
/**
1315
 * Schema for one media view.
1316
 *
1317
 * A row is inserted when the media is seen and updated while it plays.
1318
 *
1319
 * @return array
1320
 *   The visitors_media table definition.
1321
 */
1322
function visitors_media_schema(): array {
13230
  return [
13240
    'description' => 'Audio and video viewed during a visit.',
13250
    'fields' => [
13260
      'id' => [
13270
        'description' => 'Primary key',
13280
        'type' => 'serial',
13290
        'not null' => TRUE,
13300
      ],
13310
      'visit_id' => [
13320
        'description' => 'The visitors_visit id',
13330
        'type' => 'int',
13340
        'not null' => TRUE,
13350
      ],
13360
      'page_view' => [
13370
        'description' => 'The unique page view id',
13380
        'type' => 'varchar',
13390
        'length' => 32,
13400
        'not null' => FALSE,
13410
        'default' => NULL,
13420
      ],
13430
      'media_id' => [
13440
        'description' => 'The id of this media view',
13450
        'type' => 'varchar',
13460
        'length' => 32,
13470
        'not null' => TRUE,
13480
      ],
13490
      'resource' => [
13500
        'description' => 'The media resource URL',
13510
        'type' => 'text',
13520
        'not null' => TRUE,
13530
      ],
13540
      'media_type' => [
13550
        'description' => 'video or audio',
13560
        'type' => 'varchar',
13570
        'length' => 16,
13580
        'not null' => TRUE,
13590
      ],
13600
      'title' => [
13610
        'description' => 'The media title',
13620
        'type' => 'varchar',
13630
        'length' => 255,
13640
        'not null' => TRUE,
13650
        'default' => '',
13660
      ],
13670
      'player' => [
13680
        'description' => 'The media player name',
13690
        'type' => 'varchar',
13700
        'length' => 32,
13710
        'not null' => TRUE,
13720
        'default' => 'html5',
13730
      ],
13740
      'spent_time' => [
13750
        'description' => 'Seconds the media was actually playing',
13760
        'type' => 'int',
13770
        'not null' => TRUE,
13780
        'default' => 0,
13790
      ],
13800
      'length' => [
13810
        'description' => 'The media duration in seconds',
13820
        'type' => 'int',
13830
        'not null' => FALSE,
13840
        'default' => NULL,
13850
      ],
13860
      'progress' => [
13870
        'description' => 'The furthest position reached, in seconds',
13880
        'type' => 'int',
13890
        'not null' => FALSE,
13900
        'default' => NULL,
13910
      ],
13920
      'progress_percent' => [
13930
        'description' => 'The furthest position as a percent of the length',
13940
        'type' => 'int',
13950
        'not null' => FALSE,
13960
        'default' => NULL,
13970
      ],
13980
      'time_to_play' => [
13990
        'description' => 'Seconds from impression until the first play',
14000
        'type' => 'int',
14010
        'not null' => FALSE,
14020
        'default' => NULL,
14030
      ],
14040
      'width' => [
14050
        'description' => 'The video width in pixels',
14060
        'type' => 'int',
14070
        'not null' => FALSE,
14080
        'default' => NULL,
14090
      ],
14100
      'height' => [
14110
        'description' => 'The video height in pixels',
14120
        'type' => 'int',
14130
        'not null' => FALSE,
14140
        'default' => NULL,
14150
      ],
14160
      'fullscreen' => [
14170
        'description' => 'Whether the video was fullscreen',
14180
        'type' => 'int',
14190
        'size' => 'tiny',
14200
        'not null' => TRUE,
14210
        'default' => 0,
14220
      ],
14230
      'play_count' => [
14240
        'description' => 'How many times play started',
14250
        'type' => 'int',
14260
        'not null' => TRUE,
14270
        'default' => 0,
14280
      ],
14290
      'finish_count' => [
14300
        'description' => 'How many times playback finished',
14310
        'type' => 'int',
14320
        'not null' => TRUE,
14330
        'default' => 0,
14340
      ],
14350
      'segments' => [
14360
        'description' => 'Comma-separated positions that were consumed',
14370
        'type' => 'text',
14380
        'not null' => FALSE,
14390
      ],
14400
      'created' => [
14410
        'description' => 'When the media was first seen',
14420
        'type' => 'int',
14430
        'not null' => TRUE,
14440
      ],
14450
      'changed' => [
14460
        'description' => 'When the media row was last updated',
14470
        'type' => 'int',
14480
        'not null' => TRUE,
14490
      ],
14500
    ],
14510
    'primary key' => ['id'],
14520
    'unique keys' => [
14530
      'visit_media' => ['visit_id', 'media_id'],
14540
    ],
14550
    'indexes' => [
14560
      'visit_id' => ['visit_id'],
14570
    ],
14580
  ];
1459
}
1460
1461
/**
1462
 * Adds the media log.
1463
 */
1464
function visitors_update_30010(): void {
14650
  $schema = \Drupal::database()->schema();
14660
  if ($schema->tableExists('visitors_media')) {
14670
    return;
1468
  }
14690
  $schema->createTable('visitors_media', visitors_media_schema());
1470
}
1471
1472
/**
1473
 * Adds time spent to each event.
1474
 */
1475
function visitors_update_30011(): void {
14760
  $schema = \Drupal::database()->schema();
14770
  if (!$schema->tableExists('visitors_event')) {
14780
    return;
1479
  }
14800
  if ($schema->fieldExists('visitors_event', 'time_spent')) {
14810
    return;
1482
  }
14830
  $schema->addField('visitors_event', 'time_spent', [
14840
    'description' => 'Seconds on this page, or until the next event.',
14850
    'type' => 'int',
14860
    'unsigned' => TRUE,
14870
    'not null' => TRUE,
14880
    'default' => 0,
14890
  ]);
1490
}
1491
1492
/**
1493
 * Adds the Device Detector bot name to each visit.
1494
 */
1495
function visitors_update_30012(): void {
14960
  $schema = \Drupal::database()->schema();
14970
  if (!$schema->tableExists('visitors_visit')) {
14980
    return;
1499
  }
15000
  if ($schema->fieldExists('visitors_visit', 'bot_name')) {
15010
    return;
1502
  }
15030
  $schema->addField('visitors_visit', 'bot_name', [
15040
    'description' => 'Device Detector bot name',
15050
    'type' => 'varchar',
15060
    'length' => 255,
15070
    'not null' => FALSE,
15080
    'default' => NULL,
15090
  ]);
1510
}
1511
1512
/**
1513
 * Adds the AI agent flag to each visit.
1514
 */
1515
function visitors_update_30013(): void {
15160
  $schema = \Drupal::database()->schema();
15170
  if (!$schema->tableExists('visitors_visit')) {
15180
    return;
1519
  }
15200
  if ($schema->fieldExists('visitors_visit', 'ai_agent')) {
15210
    return;
1522
  }
15230
  $schema->addField('visitors_visit', 'ai_agent', [
15240
    'description' => 'The visit was opened by an AI agent browser.',
15250
    'type' => 'int',
15260
    'size' => 'tiny',
15270
    'unsigned' => TRUE,
15280
    'not null' => TRUE,
15290
    'default' => 0,
15300
  ]);
1531
}
1532
1533
/**
1534
 * Adds campaign dimension columns to each visit.
1535
 */
1536
function visitors_update_30014(): void {
15370
  $schema = \Drupal::database()->schema();
15380
  if (!$schema->tableExists('visitors_visit')) {
15390
    return;
1540
  }
1541
  $columns = [
1542
    'campaign_source' => 'The campaign source',
1543
    'campaign_medium' => 'The campaign medium',
1544
    'campaign_content' => 'The campaign content',
1545
    'campaign_id' => 'The campaign id',
1546
    'campaign_group' => 'The campaign group',
1547
    'campaign_placement' => 'The campaign placement',
1548
  ];
15490
  foreach ($columns as $name => $description) {
15500
    if ($schema->fieldExists('visitors_visit', $name)) {
1551
      continue;
1552
    }
15530
    $schema->addField('visitors_visit', $name, [
15540
      'description' => $description,
15550
      'type' => 'varchar',
15560
      'length' => 255,
15570
      'not null' => FALSE,
15580
      'default' => NULL,
15590
    ]);
1560
  }
1561
}