Leon4gr45 commited on
Commit
e979fd8
·
verified ·
1 Parent(s): 012223f

Delete test/plausible_web/controllers/api/stats_controller/suggestions_test.exs with huggingface_hub

Browse files
test/plausible_web/controllers/api/stats_controller/suggestions_test.exs DELETED
@@ -1,1490 +0,0 @@
1
- defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do
2
- use PlausibleWeb.ConnCase
3
-
4
- describe "GET /api/stats/:domain/suggestions/:filter_name" do
5
- setup [:create_user, :log_in, :create_site]
6
-
7
- test "returns suggestions for pages with empty query", %{conn: conn, site: site} do
8
- populate_stats(site, [
9
- build(:pageview, timestamp: ~N[2019-01-01 23:00:00], pathname: "/"),
10
- build(:pageview, timestamp: ~N[2019-01-01 23:00:00], pathname: "/register"),
11
- build(:pageview, timestamp: ~N[2019-01-01 23:00:01], pathname: "/contact"),
12
- build(:pageview, timestamp: ~N[2019-01-01 23:00:01], pathname: "/irrelevant")
13
- ])
14
-
15
- conn =
16
- get(conn, "/api/stats/#{site.domain}/suggestions/page?period=month&date=2019-01-01&q=")
17
-
18
- assert json_response(conn, 200) == [
19
- %{"label" => "/", "value" => "/"},
20
- %{"label" => "/contact", "value" => "/contact"},
21
- %{"label" => "/irrelevant", "value" => "/irrelevant"},
22
- %{"label" => "/register", "value" => "/register"}
23
- ]
24
- end
25
-
26
- test "returns suggestions for pages with a query", %{conn: conn, site: site} do
27
- populate_stats(site, [
28
- build(:pageview, timestamp: ~N[2019-01-01 23:00:00], pathname: "/"),
29
- build(:pageview, timestamp: ~N[2019-01-01 23:00:00], pathname: "/register"),
30
- build(:pageview, timestamp: ~N[2019-01-01 23:00:01], pathname: "/contact"),
31
- build(:pageview, timestamp: ~N[2019-01-01 23:00:01], pathname: "/irrelevant")
32
- ])
33
-
34
- conn =
35
- get(conn, "/api/stats/#{site.domain}/suggestions/page?period=month&date=2019-01-01&q=re")
36
-
37
- assert json_response(conn, 200) == [
38
- %{"label" => "/irrelevant", "value" => "/irrelevant"},
39
- %{"label" => "/register", "value" => "/register"}
40
- ]
41
- end
42
-
43
- test "returns suggestions for pages without any suggestions", %{conn: conn, site: site} do
44
- conn =
45
- get(
46
- conn,
47
- "/api/stats/#{site.domain}/suggestions/page?period=month&date=2019-01-01&q=/123"
48
- )
49
-
50
- assert json_response(conn, 200) == []
51
- end
52
-
53
- test "returns suggestions for goals", %{conn: conn, site: site} do
54
- conn =
55
- get(conn, "/api/stats/#{site.domain}/suggestions/goal?period=month&date=2019-01-01&q=")
56
-
57
- assert json_response(conn, 200) == []
58
- end
59
-
60
- test "returns suggestions for configured site goals but not all event names", %{
61
- conn: conn,
62
- site: site
63
- } do
64
- insert(:goal, site: site, event_name: "Signup")
65
-
66
- populate_stats(site, [
67
- build(:event, name: "Signup", timestamp: ~N[2019-01-01 00:00:00]),
68
- build(:event, name: "another", timestamp: ~N[2019-01-01 00:00:00])
69
- ])
70
-
71
- conn = get(conn, "/api/stats/#{site.domain}/suggestions/goal?period=day&date=2019-01-01&q=")
72
-
73
- assert json_response(conn, 200) == [%{"label" => "Signup", "value" => "Signup"}]
74
- end
75
-
76
- @tag :ee_only
77
- test "excludes goals with custom props when Props feature is unavailable", %{
78
- conn: conn,
79
- site: site,
80
- user: user
81
- } do
82
- user
83
- |> team_of()
84
- |> Plausible.Teams.Team.end_trial()
85
- |> Plausible.Repo.update!()
86
-
87
- {:ok, _goal_with_props} =
88
- Plausible.Goals.create(site, %{
89
- "event_name" => "Purchase",
90
- "custom_props" => %{"product" => "Shirt"}
91
- })
92
-
93
- {:ok, _goal_without_props} =
94
- Plausible.Goals.create(site, %{"event_name" => "Signup"})
95
-
96
- conn = get(conn, "/api/stats/#{site.domain}/suggestions/goal?period=day&q=")
97
-
98
- assert json_response(conn, 200) == [%{"label" => "Signup", "value" => "Signup"}]
99
- end
100
-
101
- @tag :ee_only
102
- test "includes goals with custom props when Props feature is available", %{
103
- conn: conn,
104
- site: site
105
- } do
106
- {:ok, _goal_with_props} =
107
- Plausible.Goals.create(site, %{
108
- "event_name" => "Purchase",
109
- "custom_props" => %{"product" => "Shirt"}
110
- })
111
-
112
- {:ok, _goal_without_props} =
113
- Plausible.Goals.create(site, %{"event_name" => "Signup"})
114
-
115
- conn = get(conn, "/api/stats/#{site.domain}/suggestions/goal?period=day&q=")
116
-
117
- suggestions = json_response(conn, 200)
118
- assert length(suggestions) == 2
119
- assert %{"label" => "Purchase", "value" => "Purchase"} in suggestions
120
- assert %{"label" => "Signup", "value" => "Signup"} in suggestions
121
- end
122
-
123
- test "returns suggestions for sources", %{conn: conn, site: site} do
124
- populate_stats(site, [
125
- build(:pageview, timestamp: ~N[2019-01-01 23:00:00], referrer_source: "Bing"),
126
- build(:pageview, timestamp: ~N[2019-01-01 23:00:00], referrer_source: "Bing"),
127
- build(:pageview,
128
- timestamp: ~N[2019-01-01 23:00:00],
129
- referrer_source: "10words"
130
- )
131
- ])
132
-
133
- conn =
134
- get(conn, "/api/stats/#{site.domain}/suggestions/source?period=month&date=2019-01-01&q=")
135
-
136
- assert json_response(conn, 200) == [
137
- %{"label" => "Direct / None", "value" => "Direct / None"},
138
- %{"label" => "Bing", "value" => "Bing"},
139
- %{"label" => "10words", "value" => "10words"}
140
- ]
141
- end
142
-
143
- test "returns suggestions for channels", %{conn: conn, site: site} do
144
- populate_stats(site, [
145
- build(:pageview, timestamp: ~N[2019-01-01 23:00:00], referrer_source: "Bing"),
146
- build(:pageview, timestamp: ~N[2019-01-01 23:00:00], referrer_source: "Bing"),
147
- build(:pageview,
148
- timestamp: ~N[2019-01-01 23:00:00],
149
- referrer_source: "Youtube"
150
- )
151
- ])
152
-
153
- conn =
154
- get(conn, "/api/stats/#{site.domain}/suggestions/channel?period=month&date=2019-01-01&q=")
155
-
156
- assert json_response(conn, 200) == [
157
- %{"label" => "Organic Search", "value" => "Organic Search"},
158
- %{"label" => "Organic Video", "value" => "Organic Video"}
159
- ]
160
- end
161
-
162
- test "returns suggestions for countries", %{conn: conn, site: site} do
163
- populate_stats(site, [
164
- build(:pageview,
165
- timestamp: ~N[2019-01-01 23:00:01],
166
- pathname: "/",
167
- country_code: "US"
168
- )
169
- ])
170
-
171
- conn =
172
- get(
173
- conn,
174
- "/api/stats/#{site.domain}/suggestions/country?period=month&date=2019-01-01&q=Unit"
175
- )
176
-
177
- assert json_response(conn, 200) == [%{"value" => "US", "label" => "United States"}]
178
- end
179
-
180
- test "returns suggestions for regions", %{conn: conn, user: user} do
181
- {:ok, [site: site]} = create_site(%{user: user})
182
-
183
- populate_stats(site, [
184
- build(:pageview, country_code: "EE", subdivision1_code: "EE-37"),
185
- build(:pageview, country_code: "EE", subdivision1_code: "EE-39")
186
- ])
187
-
188
- conn =
189
- get(
190
- conn,
191
- "/api/stats/#{site.domain}/suggestions/region?period=day&q=Har"
192
- )
193
-
194
- assert json_response(conn, 200) == [%{"value" => "EE-37", "label" => "Harjumaa"}]
195
- end
196
-
197
- test "returns suggestions for cities", %{conn: conn, user: user} do
198
- {:ok, [site: site]} = create_site(%{user: user})
199
-
200
- populate_stats(site, [
201
- build(:pageview,
202
- country_code: "EE",
203
- subdivision1_code: "EE-37",
204
- city_geoname_id: 588_409
205
- ),
206
- build(:pageview,
207
- country_code: "EE",
208
- subdivision1_code: "EE-39",
209
- city_geoname_id: 591_632
210
- )
211
- ])
212
-
213
- conn =
214
- get(
215
- conn,
216
- "/api/stats/#{site.domain}/suggestions/city?period=day&q=Kär"
217
- )
218
-
219
- assert json_response(conn, 200) == [%{"value" => 591_632, "label" => "Kärdla"}]
220
- end
221
-
222
- test "returns suggestions for countries without country in search", %{conn: conn, site: site} do
223
- conn =
224
- get(
225
- conn,
226
- "/api/stats/#{site.domain}/suggestions/country?period=month&date=2019-01-01&q=GBR,UKR,URY"
227
- )
228
-
229
- assert json_response(conn, 200) == []
230
- end
231
-
232
- test "returns 400 for nil query parameter", %{conn: conn, site: site} do
233
- populate_stats(site, [
234
- build(:pageview,
235
- timestamp: ~N[2019-01-01 23:00:01],
236
- pathname: "/",
237
- country_code: "US"
238
- )
239
- ])
240
-
241
- conn =
242
- get(
243
- conn,
244
- "/api/stats/#{site.domain}/suggestions/country?period=month&date=2019-01-01"
245
- )
246
-
247
- assert json_response(conn, 400) == %{
248
- "error" => "Search parameter 'q' is required"
249
- }
250
- end
251
-
252
- test "returns suggestions for screen sizes", %{conn: conn, site: site} do
253
- populate_stats(site, [
254
- build(:pageview,
255
- timestamp: ~N[2019-01-01 23:00:00],
256
- pathname: "/",
257
- screen_size: "Desktop"
258
- )
259
- ])
260
-
261
- conn =
262
- get(conn, "/api/stats/#{site.domain}/suggestions/screen?period=month&date=2019-01-01&q=")
263
-
264
- assert json_response(conn, 200) == [%{"value" => "Desktop", "label" => "Desktop"}]
265
- end
266
-
267
- test "returns suggestions for browsers", %{conn: conn, site: site} do
268
- populate_stats(site, [
269
- build(:pageview,
270
- timestamp: ~N[2019-01-01 23:00:00],
271
- pathname: "/",
272
- browser: "Chrome"
273
- )
274
- ])
275
-
276
- conn =
277
- get(conn, "/api/stats/#{site.domain}/suggestions/browser?period=month&date=2019-01-01&q=")
278
-
279
- assert json_response(conn, 200) == [%{"label" => "Chrome", "value" => "Chrome"}]
280
- end
281
-
282
- test "returns suggestions for browser versions", %{conn: conn, site: site} do
283
- filters = Jason.encode!([[:is, "visit:browser", ["Chrome"]]])
284
-
285
- populate_stats(site, [
286
- build(:pageview,
287
- timestamp: ~N[2019-01-01 00:00:00],
288
- browser: "Chrome",
289
- browser_version: "78.0"
290
- )
291
- ])
292
-
293
- conn =
294
- get(
295
- conn,
296
- "/api/stats/#{site.domain}/suggestions/browser_version?period=month&date=2019-01-01&filters=#{filters}&q="
297
- )
298
-
299
- assert json_response(conn, 200) == [%{"value" => "78.0", "label" => "78.0"}]
300
- end
301
-
302
- test "returns suggestions for OS", %{conn: conn, site: site} do
303
- populate_stats(site, [
304
- build(:pageview, timestamp: ~N[2019-01-01 00:00:00], operating_system: "Mac")
305
- ])
306
-
307
- conn = get(conn, "/api/stats/#{site.domain}/suggestions/os?period=month&date=2019-01-01&q=")
308
-
309
- assert json_response(conn, 200) == [%{"value" => "Mac", "label" => "Mac"}]
310
- end
311
-
312
- test "returns suggestions for OS versions", %{conn: conn, site: site} do
313
- filters = Jason.encode!([[:is, "visit:os", ["Mac"]]])
314
-
315
- populate_stats(site, [
316
- build(:pageview,
317
- timestamp: ~N[2019-01-01 00:00:00],
318
- operating_system: "Mac",
319
- operating_system_version: "10.15"
320
- )
321
- ])
322
-
323
- conn =
324
- get(
325
- conn,
326
- "/api/stats/#{site.domain}/suggestions/os_version?period=month&date=2019-01-01&filters=#{filters}&q="
327
- )
328
-
329
- assert json_response(conn, 200) == [%{"label" => "10.15", "value" => "10.15"}]
330
- end
331
-
332
- test "returns suggestions for OS versions with search", %{conn: conn, site: site} do
333
- filters = Jason.encode!([[:is, "visit:os", ["Mac"]]])
334
-
335
- conn =
336
- get(
337
- conn,
338
- "/api/stats/#{site.domain}/suggestions/os_version?period=month&date=2019-01-01&filters=#{filters}&q=11"
339
- )
340
-
341
- assert json_response(conn, 200) == []
342
- end
343
-
344
- test "returns suggestions for hostnames", %{conn: conn1, user: user} do
345
- {:ok, [site: site]} = create_site(%{user: user})
346
-
347
- populate_stats(site, [
348
- build(:pageview,
349
- pathname: "/",
350
- hostname: "host-alice.example.com"
351
- ),
352
- build(:pageview,
353
- pathname: "/some-other-page",
354
- hostname: "host-bob.example.com",
355
- user_id: 123
356
- ),
357
- build(:pageview, pathname: "/exit", hostname: "host-carol.example.com", user_id: 123)
358
- ])
359
-
360
- conn =
361
- get(
362
- conn1,
363
- "/api/stats/#{site.domain}/suggestions/hostname?period=day&q=alice"
364
- )
365
-
366
- assert json_response(conn, 200) == [
367
- %{"value" => "host-alice.example.com", "label" => "host-alice.example.com"}
368
- ]
369
-
370
- conn =
371
- get(
372
- conn1,
373
- "/api/stats/#{site.domain}/suggestions/hostname?period=day&q=host"
374
- )
375
-
376
- suggestions = json_response(conn, 200)
377
-
378
- assert length(suggestions) == 3
379
-
380
- assert %{"label" => "host-alice.example.com", "value" => "host-alice.example.com"} in suggestions
381
-
382
- assert %{"label" => "host-carol.example.com", "value" => "host-carol.example.com"} in suggestions
383
-
384
- assert %{"label" => "host-bob.example.com", "value" => "host-bob.example.com"} in suggestions
385
- end
386
-
387
- test "returns suggestions for hostnames limited by shields", %{conn: conn1, user: user} do
388
- {:ok, [site: site]} = create_site(%{user: user})
389
- Plausible.Shields.add_hostname_rule(site, %{"hostname" => "*.example.com"})
390
- Plausible.Shields.add_hostname_rule(site, %{"hostname" => "erin.rogue.com"})
391
-
392
- populate_stats(site, [
393
- build(:pageview,
394
- pathname: "/",
395
- hostname: "host-alice.example.com"
396
- ),
397
- build(:pageview,
398
- pathname: "/some-other-page",
399
- hostname: "host-bob.example.com",
400
- user_id: 123
401
- ),
402
- build(:pageview, pathname: "/exit", hostname: "host-carol.example.com", user_id: 123),
403
- build(:pageview,
404
- pathname: "/",
405
- hostname: "host-dave.rogue.com"
406
- ),
407
- build(:pageview,
408
- pathname: "/",
409
- hostname: "erin.rogue.com"
410
- )
411
- ])
412
-
413
- conn =
414
- get(
415
- conn1,
416
- "/api/stats/#{site.domain}/suggestions/hostname?period=day&q=host"
417
- )
418
-
419
- results = json_response(conn, 200)
420
-
421
- assert length(results) == 3
422
-
423
- assert %{"label" => "host-alice.example.com", "value" => "host-alice.example.com"} in results
424
-
425
- assert %{"label" => "host-carol.example.com", "value" => "host-carol.example.com"} in results
426
-
427
- assert %{"label" => "host-bob.example.com", "value" => "host-bob.example.com"} in results
428
-
429
- conn =
430
- get(
431
- conn1,
432
- "/api/stats/#{site.domain}/suggestions/hostname?period=day&q=dave"
433
- )
434
-
435
- assert json_response(conn, 200) == []
436
-
437
- conn =
438
- get(
439
- conn1,
440
- "/api/stats/#{site.domain}/suggestions/hostname?period=day&q=rogue"
441
- )
442
-
443
- assert json_response(conn, 200) == [
444
- %{"label" => "erin.rogue.com", "value" => "erin.rogue.com"}
445
- ]
446
- end
447
-
448
- test "returns suggestions for referrers", %{conn: conn, site: site} do
449
- populate_stats(site, [
450
- build(:pageview,
451
- timestamp: ~N[2019-01-01 23:00:00],
452
- pathname: "/",
453
- referrer: "10words.com/page1"
454
- )
455
- ])
456
-
457
- conn =
458
- get(
459
- conn,
460
- "/api/stats/#{site.domain}/suggestions/referrer?period=month&date=2019-01-01&q="
461
- )
462
-
463
- assert json_response(conn, 200) == [
464
- %{"value" => "10words.com/page1", "label" => "10words.com/page1"}
465
- ]
466
- end
467
- end
468
-
469
- describe "suggestions for props" do
470
- setup [:create_user, :log_in, :create_site]
471
-
472
- test "returns suggestions for prop key ordered by count", %{conn: conn, site: site} do
473
- populate_stats(site, [
474
- build(:pageview,
475
- "meta.key": ["author"],
476
- "meta.value": ["Uku Taht"],
477
- timestamp: ~N[2022-01-01 00:00:00]
478
- ),
479
- build(:pageview,
480
- "meta.key": ["author"],
481
- "meta.value": ["Uku Taht"],
482
- timestamp: ~N[2022-01-01 00:00:00]
483
- ),
484
- build(:pageview,
485
- "meta.key": ["author"],
486
- "meta.value": ["Uku Taht"],
487
- timestamp: ~N[2022-01-01 00:00:00]
488
- ),
489
- build(:pageview,
490
- "meta.key": ["logged_in"],
491
- "meta.value": ["false"],
492
- timestamp: ~N[2022-01-01 00:00:00]
493
- ),
494
- build(:pageview,
495
- "meta.key": ["logged_in"],
496
- "meta.value": ["false"],
497
- timestamp: ~N[2022-01-01 00:00:00]
498
- ),
499
- build(:pageview,
500
- "meta.key": ["dark_mode"],
501
- "meta.value": ["true"],
502
- timestamp: ~N[2022-01-01 00:00:00]
503
- )
504
- ])
505
-
506
- conn =
507
- get(conn, "/api/stats/#{site.domain}/suggestions/prop_key?period=day&date=2022-01-01&q=")
508
-
509
- assert json_response(conn, 200) == [
510
- %{"label" => "author", "value" => "author"},
511
- %{"label" => "logged_in", "value" => "logged_in"},
512
- %{"label" => "dark_mode", "value" => "dark_mode"}
513
- ]
514
- end
515
-
516
- test "returns prop key suggestions found in time frame", %{
517
- conn: conn,
518
- site: site
519
- } do
520
- populate_stats(site, [
521
- build(:pageview,
522
- "meta.key": ["author", "logged_in"],
523
- "meta.value": ["Uku Taht", "false"],
524
- timestamp: ~N[2022-01-01 00:00:00]
525
- ),
526
- build(:pageview,
527
- "meta.key": ["dark_mode"],
528
- "meta.value": ["true"],
529
- timestamp: ~N[2022-01-02 00:00:00]
530
- )
531
- ])
532
-
533
- conn =
534
- get(conn, "/api/stats/#{site.domain}/suggestions/prop_key?period=day&date=2022-01-01&q=")
535
-
536
- assert json_response(conn, 200) == [
537
- %{"label" => "author", "value" => "author"},
538
- %{"label" => "logged_in", "value" => "logged_in"}
539
- ]
540
- end
541
-
542
- test "returns prop key suggestions by a search string", %{
543
- conn: conn,
544
- site: site
545
- } do
546
- populate_stats(site, [
547
- build(:pageview,
548
- "meta.key": ["author", "logged_in"],
549
- "meta.value": ["Uku Taht", "false"],
550
- timestamp: ~N[2022-01-01 00:00:00]
551
- ),
552
- build(:pageview,
553
- "meta.key": ["dark_mode"],
554
- "meta.value": ["true"],
555
- timestamp: ~N[2022-01-02 00:00:00]
556
- )
557
- ])
558
-
559
- conn =
560
- get(
561
- conn,
562
- "/api/stats/#{site.domain}/suggestions/prop_key?period=day&date=2022-01-01&q=aut"
563
- )
564
-
565
- assert json_response(conn, 200) == [
566
- %{"label" => "author", "value" => "author"}
567
- ]
568
- end
569
-
570
- test "returns only prop keys which exist under filter", %{
571
- conn: conn,
572
- site: site
573
- } do
574
- populate_stats(site, [
575
- build(:pageview,
576
- "meta.key": ["author", "bank"],
577
- "meta.value": ["Uku", "a"],
578
- timestamp: ~N[2022-01-01 00:00:00]
579
- ),
580
- build(:pageview,
581
- "meta.key": ["author", "serial_number"],
582
- "meta.value": ["Marko", "b"],
583
- timestamp: ~N[2022-01-01 00:00:00]
584
- )
585
- ])
586
-
587
- filters = Jason.encode!([[:is, "event:props:author", ["Uku"]]])
588
-
589
- conn =
590
- get(
591
- conn,
592
- "/api/stats/#{site.domain}/suggestions/prop_key?period=day&date=2022-01-01&filters=#{filters}&q="
593
- )
594
-
595
- assert json_response(conn, 200) == [
596
- %{"label" => "author", "value" => "author"},
597
- %{"label" => "bank", "value" => "bank"}
598
- ]
599
- end
600
-
601
- test "returns suggestions for prop key based on site.allowed_event_props list", %{
602
- conn: conn,
603
- site: site
604
- } do
605
- {:ok, site} = Plausible.Props.allow(site, ["author"])
606
-
607
- populate_stats(site, [
608
- build(:pageview,
609
- "meta.key": ["author"],
610
- "meta.value": ["Uku Taht"],
611
- timestamp: ~N[2022-01-01 00:00:00]
612
- ),
613
- build(:pageview,
614
- "meta.key": ["garbage1"],
615
- "meta.value": ["somegarbage1"],
616
- timestamp: ~N[2022-01-01 00:00:00]
617
- ),
618
- build(:pageview,
619
- "meta.key": ["garbage2"],
620
- "meta.value": ["somegarbage2"],
621
- timestamp: ~N[2022-01-01 00:00:00]
622
- )
623
- ])
624
-
625
- conn =
626
- get(conn, "/api/stats/#{site.domain}/suggestions/prop_key?period=day&date=2022-01-01&q=")
627
-
628
- assert json_response(conn, 200) == [
629
- %{"label" => "author", "value" => "author"}
630
- ]
631
- end
632
-
633
- test "does not filter out prop key suggestions by default (when site.allowed_event_props is nil)",
634
- %{
635
- conn: conn,
636
- site: site
637
- } do
638
- populate_stats(site, [
639
- build(:pageview,
640
- "meta.key": ["author"],
641
- "meta.value": ["Uku Taht"],
642
- timestamp: ~N[2022-01-01 00:00:00]
643
- ),
644
- build(:pageview,
645
- "meta.key": ["garbage1"],
646
- "meta.value": ["somegarbage1"],
647
- timestamp: ~N[2022-01-01 00:00:00]
648
- ),
649
- build(:pageview,
650
- "meta.key": ["garbage2"],
651
- "meta.value": ["somegarbage2"],
652
- timestamp: ~N[2022-01-01 00:00:00]
653
- )
654
- ])
655
-
656
- conn =
657
- get(conn, "/api/stats/#{site.domain}/suggestions/prop_key?period=day&date=2022-01-01&q=")
658
-
659
- suggestions = json_response(conn, 200)
660
- assert %{"label" => "author", "value" => "author"} in suggestions
661
- assert %{"label" => "garbage1", "value" => "garbage1"} in suggestions
662
- assert %{"label" => "garbage2", "value" => "garbage2"} in suggestions
663
- end
664
-
665
- test "returns suggestions for prop value ordered by count, but (none) value is always first",
666
- %{conn: conn, site: site} do
667
- populate_stats(site, [
668
- build(:pageview,
669
- "meta.key": ["author"],
670
- "meta.value": ["Uku Taht"],
671
- timestamp: ~N[2022-01-01 00:00:00]
672
- ),
673
- build(:pageview,
674
- "meta.key": ["author"],
675
- "meta.value": ["Uku Taht"],
676
- timestamp: ~N[2022-01-01 00:00:00]
677
- ),
678
- build(:pageview,
679
- "meta.key": ["author"],
680
- "meta.value": ["Marko Saric"],
681
- timestamp: ~N[2022-01-01 00:00:00]
682
- ),
683
- build(:pageview,
684
- timestamp: ~N[2022-01-01 00:00:00]
685
- )
686
- ])
687
-
688
- filters = Jason.encode!([[:is_not, "event:props:author", ["(none)"]]])
689
-
690
- conn =
691
- get(
692
- conn,
693
- "/api/stats/#{site.domain}/suggestions/custom-prop-values/author?period=day&date=2022-01-01&filters=#{filters}&q="
694
- )
695
-
696
- assert json_response(conn, 200) == [
697
- %{"label" => "(none)", "value" => "(none)"},
698
- %{"label" => "Uku Taht", "value" => "Uku Taht"},
699
- %{"label" => "Marko Saric", "value" => "Marko Saric"}
700
- ]
701
- end
702
-
703
- test "returns (none) value as the first suggestion even when a search string is provided",
704
- %{conn: conn, site: site} do
705
- populate_stats(site, [
706
- build(:pageview,
707
- "meta.key": ["author"],
708
- "meta.value": ["Uku Taht"],
709
- timestamp: ~N[2022-01-01 00:00:00]
710
- ),
711
- build(:pageview,
712
- "meta.key": ["author"],
713
- "meta.value": ["Marko Saric"],
714
- timestamp: ~N[2022-01-01 00:00:00]
715
- ),
716
- build(:pageview,
717
- "meta.key": ["author"],
718
- "meta.value": ["Marko Saric"],
719
- timestamp: ~N[2022-01-01 00:00:00]
720
- ),
721
- build(:pageview,
722
- timestamp: ~N[2022-01-01 00:00:00]
723
- )
724
- ])
725
-
726
- filters = Jason.encode!([[:is_not, "event:props:author", ["(none)"]]])
727
-
728
- conn =
729
- get(
730
- conn,
731
- "/api/stats/#{site.domain}/suggestions/custom-prop-values/author?period=day&date=2022-01-01&filters=#{filters}&q=Mar"
732
- )
733
-
734
- assert json_response(conn, 200) == [
735
- %{"label" => "(none)", "value" => "(none)"},
736
- %{"label" => "Marko Saric", "value" => "Marko Saric"}
737
- ]
738
- end
739
-
740
- test "does not show (none) value suggestion when all events have that prop_key", %{
741
- conn: conn,
742
- site: site
743
- } do
744
- populate_stats(site, [
745
- build(:pageview,
746
- "meta.key": ["author"],
747
- "meta.value": ["Uku Taht"],
748
- timestamp: ~N[2022-01-01 00:00:00]
749
- ),
750
- build(:pageview,
751
- "meta.key": ["author"],
752
- "meta.value": ["Uku Taht"],
753
- timestamp: ~N[2022-01-01 00:00:00]
754
- ),
755
- build(:pageview,
756
- "meta.key": ["author"],
757
- "meta.value": ["Marko Saric"],
758
- timestamp: ~N[2022-01-01 00:00:00]
759
- )
760
- ])
761
-
762
- filters = Jason.encode!([[:is_not, "event:props:author", ["(none)"]]])
763
-
764
- conn =
765
- get(
766
- conn,
767
- "/api/stats/#{site.domain}/suggestions/custom-prop-values/author?period=day&date=2022-01-01&filters=#{filters}"
768
- )
769
-
770
- assert json_response(conn, 200) == [
771
- %{"label" => "Uku Taht", "value" => "Uku Taht"},
772
- %{"label" => "Marko Saric", "value" => "Marko Saric"}
773
- ]
774
- end
775
-
776
- test "returns prop value suggestions with multiple custom property filters in query", %{
777
- conn: conn,
778
- site: site
779
- } do
780
- populate_stats(site, [
781
- build(:pageview,
782
- "meta.key": ["author", "browser_language"],
783
- "meta.value": ["Uku Taht", "en-US"],
784
- timestamp: ~N[2022-01-01 00:00:00]
785
- ),
786
- build(:pageview,
787
- "meta.key": ["author", "browser_language"],
788
- "meta.value": ["Uku Taht", "en-US"],
789
- timestamp: ~N[2022-01-01 00:00:00]
790
- ),
791
- build(:pageview,
792
- "meta.key": ["author", "browser_language"],
793
- "meta.value": ["Marko Saric", "de-DE"],
794
- timestamp: ~N[2022-01-01 00:00:00]
795
- ),
796
- build(:pageview, timestamp: ~N[2022-01-01 00:00:00])
797
- ])
798
-
799
- filters =
800
- Jason.encode!([
801
- [:is_not, "event:props:browser_language", ["(none)"]],
802
- [:is, "event:props:author", ["Uku Taht"]]
803
- ])
804
-
805
- conn =
806
- get(
807
- conn,
808
- "/api/stats/#{site.domain}/suggestions/custom-prop-values/browser_language?period=day&date=2022-01-01&filters=#{filters}"
809
- )
810
-
811
- assert json_response(conn, 200) == [
812
- %{"label" => "en-US", "value" => "en-US"}
813
- ]
814
- end
815
-
816
- test "when date is borked, bad request is returned", %{
817
- conn: conn,
818
- site: site
819
- } do
820
- today = (Date.utc_today() |> Date.to_iso8601()) <> " 00:00:00"
821
- naive_today = NaiveDateTime.from_iso8601!(today)
822
-
823
- populate_stats(site, [
824
- build(:pageview,
825
- "meta.key": ["author"],
826
- "meta.value": ["Alice Bob"],
827
- timestamp: naive_today
828
- ),
829
- build(:pageview,
830
- "meta.key": ["author"],
831
- "meta.value": ["Cecil"],
832
- timestamp: ~N[2022-01-01 00:00:00]
833
- )
834
- ])
835
-
836
- filters = Jason.encode!([[:is_not, "event:props:author", ["(none)"]]])
837
-
838
- conn =
839
- get(
840
- conn,
841
- "/api/stats/#{site.domain}/suggestions/custom-prop-values/author?period=all&date=CLEVER_SECURITY_RESEARCH&filters=#{filters}"
842
- )
843
-
844
- assert json_response(conn, 400) == %{
845
- "error" =>
846
- "Failed to parse 'date' argument. Only ISO 8601 dates are allowed, e.g. `2019-09-07`, `2020-01-01`"
847
- }
848
- end
849
- end
850
-
851
- describe "imported data" do
852
- setup [:create_user, :log_in, :create_site, :create_site_import]
853
-
854
- test "merges country suggestions from native and imported data", %{
855
- conn: conn,
856
- site: site,
857
- site_import: site_import
858
- } do
859
- populate_stats(site, site_import.id, [
860
- build(:pageview, timestamp: ~N[2019-01-01 23:00:01], country_code: "US"),
861
- build(:pageview, timestamp: ~N[2019-01-01 23:30:01], country_code: "US"),
862
- build(:pageview, timestamp: ~N[2019-01-01 23:40:01], country_code: "US"),
863
- build(:pageview, timestamp: ~N[2019-01-01 23:00:01], country_code: "GB"),
864
- build(:imported_locations, date: ~D[2019-01-01], country: "GB", pageviews: 3)
865
- ])
866
-
867
- conn =
868
- get(
869
- conn,
870
- "/api/stats/#{site.domain}/suggestions/country?period=month&date=2019-01-01&q=Unit&with_imported=true"
871
- )
872
-
873
- assert json_response(conn, 200) == [
874
- %{"value" => "GB", "label" => "United Kingdom"},
875
- %{"value" => "US", "label" => "United States"}
876
- ]
877
- end
878
-
879
- test "ignores imported data in country suggestions when a different property is filtered by",
880
- %{
881
- conn: conn,
882
- site: site,
883
- site_import: site_import
884
- } do
885
- populate_stats(site, site_import.id, [
886
- build(:pageview, country_code: "EE", referrer_source: "Bing"),
887
- build(:imported_locations, country: "GB")
888
- ])
889
-
890
- filters = Jason.encode!([[:is, "visit:source", ["Bing"]]])
891
-
892
- conn =
893
- get(
894
- conn,
895
- "/api/stats/#{site.domain}/suggestions/country?period=day&filters=#{filters}&q=&with_imported=true"
896
- )
897
-
898
- assert json_response(conn, 200) == [%{"value" => "EE", "label" => "Estonia"}]
899
- end
900
-
901
- test "queries imported countries when filtering by country", %{
902
- conn: conn,
903
- site: site,
904
- site_import: site_import
905
- } do
906
- populate_stats(site, site_import.id, [
907
- build(:imported_locations, date: ~D[2019-01-01], country: "EE")
908
- ])
909
-
910
- filters = Jason.encode!([[:is, "visit:country", ["EE"]]])
911
-
912
- conn =
913
- get(
914
- conn,
915
- "/api/stats/#{site.domain}/suggestions/country?period=month&date=2019-01-01&filters=#{filters}&q=&with_imported=true"
916
- )
917
-
918
- assert json_response(conn, 200) == [%{"value" => "EE", "label" => "Estonia"}]
919
- end
920
-
921
- test "ignores imported country data when not requested", %{
922
- conn: conn,
923
- site: site,
924
- site_import: site_import
925
- } do
926
- populate_stats(site, site_import.id, [
927
- build(:imported_locations, date: ~D[2019-01-01], country: "GB", pageviews: 3)
928
- ])
929
-
930
- conn =
931
- get(
932
- conn,
933
- "/api/stats/#{site.domain}/suggestions/country?period=month&date=2019-01-01&q="
934
- )
935
-
936
- assert json_response(conn, 200) == []
937
- end
938
-
939
- for {q, label} <- [{"", "without filter"}, {"H", "with filter"}] do
940
- test "merges region suggestions from native and imported data #{label}", %{
941
- conn: conn,
942
- site: site,
943
- site_import: site_import
944
- } do
945
- populate_stats(site, site_import.id, [
946
- build(:pageview, country_code: "EE", subdivision1_code: "EE-37"),
947
- build(:pageview, country_code: "EE", subdivision1_code: "EE-39"),
948
- build(:pageview, country_code: "EE", subdivision1_code: "EE-39"),
949
- build(:imported_locations, country: "EE", region: "EE-37", pageviews: 2)
950
- ])
951
-
952
- conn =
953
- get(
954
- conn,
955
- "/api/stats/#{site.domain}/suggestions/region?period=day&q=#{unquote(q)}&with_imported=true"
956
- )
957
-
958
- assert json_response(conn, 200) == [
959
- %{"value" => "EE-37", "label" => "Harjumaa"},
960
- %{"value" => "EE-39", "label" => "Hiiumaa"}
961
- ]
962
- end
963
- end
964
-
965
- test "handles invalid region codes in imported data gracefully (GA4)", %{
966
- conn: conn,
967
- site: site,
968
- site_import: site_import
969
- } do
970
- # NOTE: Currently, the regions imported from GA4 do not conform to region code standard
971
- # we are using. Instead, literal region names are persisted. Those names often do not
972
- # match the names from our region databases either. Regardless of that, we still consider
973
- # them when filtering suggestions.
974
-
975
- populate_stats(site, site_import.id, [
976
- build(:imported_locations, country: "EE", region: "EE-37", pageviews: 2),
977
- build(:imported_locations, country: "EE", region: "Hiiumaa", pageviews: 1)
978
- ])
979
-
980
- conn1 =
981
- get(
982
- conn,
983
- "/api/stats/#{site.domain}/suggestions/region?period=day&q=&with_imported=true"
984
- )
985
-
986
- assert json_response(conn1, 200) == [
987
- %{"value" => "EE-37", "label" => "Harjumaa"},
988
- %{"value" => "Hiiumaa", "label" => "Hiiumaa"}
989
- ]
990
-
991
- conn2 =
992
- get(
993
- conn,
994
- "/api/stats/#{site.domain}/suggestions/region?period=day&q=H&with_imported=true"
995
- )
996
-
997
- assert json_response(conn2, 200) == [
998
- %{"value" => "EE-37", "label" => "Harjumaa"},
999
- %{"value" => "Hiiumaa", "label" => "Hiiumaa"}
1000
- ]
1001
- end
1002
-
1003
- test "ignores imported data in region suggestions when a different property is filtered by",
1004
- %{
1005
- conn: conn,
1006
- site: site,
1007
- site_import: site_import
1008
- } do
1009
- populate_stats(site, site_import.id, [
1010
- build(:pageview,
1011
- country_code: "EE",
1012
- subdivision1_code: "EE-39",
1013
- referrer_source: "Bing"
1014
- ),
1015
- build(:imported_locations, country: "EE", region: "EE-37")
1016
- ])
1017
-
1018
- filters = Jason.encode!([[:is, "visit:source", ["Bing"]]])
1019
-
1020
- conn =
1021
- get(
1022
- conn,
1023
- "/api/stats/#{site.domain}/suggestions/region?period=day&filters=#{filters}&q=&with_imported=true"
1024
- )
1025
-
1026
- assert json_response(conn, 200) == [%{"value" => "EE-39", "label" => "Hiiumaa"}]
1027
- end
1028
-
1029
- test "queries imported regions when filtering by region", %{
1030
- conn: conn,
1031
- site: site,
1032
- site_import: site_import
1033
- } do
1034
- populate_stats(site, site_import.id, [
1035
- build(:imported_locations, date: ~D[2019-01-01], region: "EE-39")
1036
- ])
1037
-
1038
- filters = Jason.encode!([[:is, "visit:region", ["EE-39"]]])
1039
-
1040
- conn =
1041
- get(
1042
- conn,
1043
- "/api/stats/#{site.domain}/suggestions/region?period=month&date=2019-01-01&filters=#{filters}&q=&with_imported=true"
1044
- )
1045
-
1046
- assert json_response(conn, 200) == [%{"value" => "EE-39", "label" => "Hiiumaa"}]
1047
- end
1048
-
1049
- test "ignores imported region data when not requested", %{
1050
- conn: conn,
1051
- site: site,
1052
- site_import: site_import
1053
- } do
1054
- populate_stats(site, site_import.id, [
1055
- build(:imported_locations, country: "EE", region: "EE-37", pageviews: 2)
1056
- ])
1057
-
1058
- conn =
1059
- get(
1060
- conn,
1061
- "/api/stats/#{site.domain}/suggestions/region?q="
1062
- )
1063
-
1064
- assert json_response(conn, 200) == []
1065
- end
1066
-
1067
- for {q, label} <- [{"", "without filter"}, {"l", "with filter"}] do
1068
- test "merges city suggestions from native and imported data #{label}", %{
1069
- conn: conn,
1070
- site: site,
1071
- site_import: site_import
1072
- } do
1073
- populate_stats(site, site_import.id, [
1074
- build(:pageview,
1075
- country_code: "EE",
1076
- subdivision1_code: "EE-37",
1077
- city_geoname_id: 588_409
1078
- ),
1079
- build(:pageview,
1080
- country_code: "EE",
1081
- subdivision1_code: "EE-39",
1082
- city_geoname_id: 591_632
1083
- ),
1084
- build(:pageview,
1085
- country_code: "EE",
1086
- subdivision1_code: "EE-39",
1087
- city_geoname_id: 591_632
1088
- ),
1089
- build(:imported_locations, country: "EE", region: "EE-37", city: 588_409, pageviews: 2)
1090
- ])
1091
-
1092
- conn =
1093
- get(
1094
- conn,
1095
- "/api/stats/#{site.domain}/suggestions/city?period=day&q=#{unquote(q)}&with_imported=true"
1096
- )
1097
-
1098
- assert json_response(conn, 200) == [
1099
- %{"value" => 588_409, "label" => "Tallinn"},
1100
- %{"value" => 591_632, "label" => "Kärdla"}
1101
- ]
1102
- end
1103
- end
1104
-
1105
- test "ignores imported data in city suggestions when a different property is filtered by", %{
1106
- conn: conn,
1107
- site: site,
1108
- site_import: site_import
1109
- } do
1110
- populate_stats(site, site_import.id, [
1111
- build(:pageview,
1112
- country_code: "EE",
1113
- subdivision1_code: "EE-39",
1114
- city_geoname_id: 591_632,
1115
- referrer_source: "Bing"
1116
- ),
1117
- build(:imported_locations, country: "EE", region: "EE-37", city: 588_409)
1118
- ])
1119
-
1120
- filters = Jason.encode!([[:is, "visit:source", ["Bing"]]])
1121
-
1122
- conn =
1123
- get(
1124
- conn,
1125
- "/api/stats/#{site.domain}/suggestions/city?period=day&filters=#{filters}&q=&with_imported=true"
1126
- )
1127
-
1128
- assert json_response(conn, 200) == [%{"value" => 591_632, "label" => "Kärdla"}]
1129
- end
1130
-
1131
- test "queries imported cities when filtering by city", %{
1132
- conn: conn,
1133
- site: site,
1134
- site_import: site_import
1135
- } do
1136
- populate_stats(site, site_import.id, [
1137
- build(:imported_locations, date: ~D[2019-01-01], city: 591_632)
1138
- ])
1139
-
1140
- filters = Jason.encode!([[:is, "visit:city", ["591632"]]])
1141
-
1142
- conn =
1143
- get(
1144
- conn,
1145
- "/api/stats/#{site.domain}/suggestions/city?period=month&date=2019-01-01&filters=#{filters}&q=&with_imported=true"
1146
- )
1147
-
1148
- assert json_response(conn, 200) == [%{"value" => 591_632, "label" => "Kärdla"}]
1149
- end
1150
-
1151
- test "ignores imported city data when not requested", %{
1152
- conn: conn,
1153
- site: site,
1154
- site_import: site_import
1155
- } do
1156
- populate_stats(site, site_import.id, [
1157
- build(:imported_locations, country: "EE", region: "EE-37", city: 588_409, pageviews: 2)
1158
- ])
1159
-
1160
- conn =
1161
- get(
1162
- conn,
1163
- "/api/stats/#{site.domain}/suggestions/city?q="
1164
- )
1165
-
1166
- assert json_response(conn, 200) == []
1167
- end
1168
-
1169
- test "ignores imported data when asking for prop key and value suggestions", %{
1170
- conn: conn,
1171
- site: site,
1172
- site_import: site_import
1173
- } do
1174
- populate_stats(site, site_import.id, [
1175
- build(:pageview,
1176
- "meta.key": ["url"],
1177
- "meta.value": ["http://example1.com"],
1178
- timestamp: ~N[2022-01-01 00:00:00]
1179
- ),
1180
- build(:imported_custom_events,
1181
- date: ~D[2022-01-01],
1182
- name: "Outbound Link: Click",
1183
- link_url: "http://example2.com"
1184
- ),
1185
- build(:imported_custom_events,
1186
- date: ~D[2022-01-01],
1187
- name: "404",
1188
- path: "/dev/null"
1189
- )
1190
- ])
1191
-
1192
- key_conn =
1193
- get(
1194
- conn,
1195
- "/api/stats/#{site.domain}/suggestions/prop_key?period=day&date=2022-01-01&with_imported=true&q="
1196
- )
1197
-
1198
- assert json_response(key_conn, 200) == [%{"label" => "url", "value" => "url"}]
1199
-
1200
- filters = Jason.encode!([[:is_not, "event:props:url", ["(none)"]]])
1201
-
1202
- value_conn =
1203
- get(
1204
- conn,
1205
- "/api/stats/#{site.domain}/suggestions/custom-prop-values/url?period=day&date=2022-01-01&with_imported=true&filters=#{filters}"
1206
- )
1207
-
1208
- assert json_response(value_conn, 200) == [
1209
- %{"label" => "http://example1.com", "value" => "http://example1.com"}
1210
- ]
1211
- end
1212
-
1213
- for {q, label} <- [{"", "without filter"}, {"g", "with filter"}] do
1214
- test "merges source suggestions from native and imported data #{label}", %{
1215
- conn: conn,
1216
- site: site,
1217
- site_import: site_import
1218
- } do
1219
- populate_stats(site, site_import.id, [
1220
- build(:pageview, timestamp: ~N[2019-01-01 23:00:01], referrer_source: "Bing"),
1221
- build(:pageview, timestamp: ~N[2019-01-01 23:30:01], referrer_source: "Bing"),
1222
- build(:pageview, timestamp: ~N[2019-01-01 23:40:01], referrer_source: "Bing"),
1223
- build(:pageview, timestamp: ~N[2019-01-01 23:00:01], referrer_source: "Google"),
1224
- build(:imported_sources, date: ~D[2019-01-01], source: "Google", pageviews: 3)
1225
- ])
1226
-
1227
- conn =
1228
- get(
1229
- conn,
1230
- "/api/stats/#{site.domain}/suggestions/source?period=month&date=2019-01-01&q=#{unquote(q)}&with_imported=true"
1231
- )
1232
-
1233
- if unquote(label) == "with filter" do
1234
- assert json_response(conn, 200) == [
1235
- %{"value" => "Google", "label" => "Google"},
1236
- %{"value" => "Bing", "label" => "Bing"}
1237
- ]
1238
- else
1239
- assert json_response(conn, 200) == [
1240
- %{"value" => "Direct / None", "label" => "Direct / None"},
1241
- %{"value" => "Google", "label" => "Google"},
1242
- %{"value" => "Bing", "label" => "Bing"}
1243
- ]
1244
- end
1245
- end
1246
-
1247
- test "merges channel suggestions from native and imported data #{label}", %{
1248
- conn: conn,
1249
- site: site,
1250
- site_import: site_import
1251
- } do
1252
- populate_stats(site, site_import.id, [
1253
- build(:pageview, timestamp: ~N[2019-01-01 23:00:01], referrer_source: "Bing"),
1254
- build(:pageview, timestamp: ~N[2019-01-01 23:30:01], referrer_source: "Bing"),
1255
- build(:pageview, timestamp: ~N[2019-01-01 23:40:01], referrer_source: "Bing"),
1256
- build(:pageview, timestamp: ~N[2019-01-01 23:00:01], referrer_source: "Google"),
1257
- build(:imported_sources, date: ~D[2019-01-01], channel: "Organic Social", pageviews: 3)
1258
- ])
1259
-
1260
- conn =
1261
- get(
1262
- conn,
1263
- "/api/stats/#{site.domain}/suggestions/channel?period=month&date=2019-01-01&q=#{unquote(q)}&with_imported=true"
1264
- )
1265
-
1266
- assert json_response(conn, 200) == [
1267
- %{"value" => "Organic Search", "label" => "Organic Search"},
1268
- %{"value" => "Organic Social", "label" => "Organic Social"}
1269
- ]
1270
- end
1271
- end
1272
-
1273
- for {q, label} <- [{"", "without filter"}, {"o", "with filter"}] do
1274
- test "merges screen suggestions from native and imported data #{label}", %{
1275
- conn: conn,
1276
- site: site,
1277
- site_import: site_import
1278
- } do
1279
- populate_stats(site, site_import.id, [
1280
- build(:pageview, timestamp: ~N[2019-01-01 23:00:01], screen_size: "Mobile"),
1281
- build(:pageview, timestamp: ~N[2019-01-01 23:30:01], screen_size: "Mobile"),
1282
- build(:pageview, timestamp: ~N[2019-01-01 23:40:01], screen_size: "Mobile"),
1283
- build(:pageview, timestamp: ~N[2019-01-01 23:00:01], screen_size: "Desktop"),
1284
- build(:imported_devices, date: ~D[2019-01-01], device: "Desktop", pageviews: 3)
1285
- ])
1286
-
1287
- conn =
1288
- get(
1289
- conn,
1290
- "/api/stats/#{site.domain}/suggestions/screen?period=month&date=2019-01-01&q=#{unquote(q)}&with_imported=true"
1291
- )
1292
-
1293
- assert json_response(conn, 200) == [
1294
- %{"value" => "Desktop", "label" => "Desktop"},
1295
- %{"value" => "Mobile", "label" => "Mobile"}
1296
- ]
1297
- end
1298
- end
1299
-
1300
- for {q, label} <- [{"", "without filter"}, {"o", "with filter"}] do
1301
- test "merges page suggestions from native and imported data #{label}", %{
1302
- conn: conn,
1303
- site: site,
1304
- site_import: site_import
1305
- } do
1306
- populate_stats(site, site_import.id, [
1307
- build(:pageview, timestamp: ~N[2019-01-01 23:00:01], pathname: "/welcome"),
1308
- build(:pageview, timestamp: ~N[2019-01-01 23:30:01], pathname: "/welcome"),
1309
- build(:pageview, timestamp: ~N[2019-01-01 23:40:01], pathname: "/welcome"),
1310
- build(:pageview, timestamp: ~N[2019-01-01 23:00:01], pathname: "/blog"),
1311
- build(:imported_pages, date: ~D[2019-01-01], page: "/blog", pageviews: 3)
1312
- ])
1313
-
1314
- conn =
1315
- get(
1316
- conn,
1317
- "/api/stats/#{site.domain}/suggestions/page?period=month&date=2019-01-01&q=#{unquote(q)}&with_imported=true"
1318
- )
1319
-
1320
- assert json_response(conn, 200) == [
1321
- %{"value" => "/blog", "label" => "/blog"},
1322
- %{"value" => "/welcome", "label" => "/welcome"}
1323
- ]
1324
- end
1325
- end
1326
-
1327
- for {q, label} <- [{"", "without filter"}, {"o", "with filter"}] do
1328
- test "merges entry page suggestions from native and imported data #{label}", %{
1329
- conn: conn,
1330
- site: site,
1331
- site_import: site_import
1332
- } do
1333
- populate_stats(site, site_import.id, [
1334
- build(:pageview, timestamp: ~N[2019-01-01 23:00:01], pathname: "/welcome"),
1335
- build(:pageview, timestamp: ~N[2019-01-01 23:30:01], pathname: "/welcome"),
1336
- build(:pageview, timestamp: ~N[2019-01-01 23:40:01], pathname: "/welcome"),
1337
- build(:pageview, timestamp: ~N[2019-01-01 23:00:01], pathname: "/blog"),
1338
- build(:imported_entry_pages, date: ~D[2019-01-01], entry_page: "/blog", pageviews: 3)
1339
- ])
1340
-
1341
- conn =
1342
- get(
1343
- conn,
1344
- "/api/stats/#{site.domain}/suggestions/entry_page?period=month&date=2019-01-01&q=#{unquote(q)}&with_imported=true"
1345
- )
1346
-
1347
- assert json_response(conn, 200) == [
1348
- %{"value" => "/blog", "label" => "/blog"},
1349
- %{"value" => "/welcome", "label" => "/welcome"}
1350
- ]
1351
- end
1352
- end
1353
-
1354
- for {q, label} <- [{"", "without filter"}, {"o", "with filter"}] do
1355
- test "merges exit page suggestions from native and imported data #{label}", %{
1356
- conn: conn,
1357
- site: site,
1358
- site_import: site_import
1359
- } do
1360
- populate_stats(site, site_import.id, [
1361
- build(:pageview, timestamp: ~N[2019-01-01 23:00:01], pathname: "/welcome"),
1362
- build(:pageview, timestamp: ~N[2019-01-01 23:30:01], pathname: "/welcome"),
1363
- build(:pageview, timestamp: ~N[2019-01-01 23:40:01], pathname: "/welcome"),
1364
- build(:pageview, timestamp: ~N[2019-01-01 23:00:01], pathname: "/blog"),
1365
- build(:imported_exit_pages, date: ~D[2019-01-01], exit_page: "/blog", pageviews: 3)
1366
- ])
1367
-
1368
- conn =
1369
- get(
1370
- conn,
1371
- "/api/stats/#{site.domain}/suggestions/exit_page?period=month&date=2019-01-01&q=#{unquote(q)}&with_imported=true"
1372
- )
1373
-
1374
- assert json_response(conn, 200) == [
1375
- %{"value" => "/blog", "label" => "/blog"},
1376
- %{"value" => "/welcome", "label" => "/welcome"}
1377
- ]
1378
- end
1379
- end
1380
-
1381
- for {q, label} <- [{"", "without filter"}, {"o", "with filter"}] do
1382
- test "merges browser suggestions from native and imported data #{label}", %{
1383
- conn: conn,
1384
- site: site,
1385
- site_import: site_import
1386
- } do
1387
- populate_stats(site, site_import.id, [
1388
- build(:pageview, timestamp: ~N[2019-01-01 23:00:01], browser: "Chrome"),
1389
- build(:pageview, timestamp: ~N[2019-01-01 23:30:01], browser: "Chrome"),
1390
- build(:pageview, timestamp: ~N[2019-01-01 23:40:01], browser: "Chrome"),
1391
- build(:pageview, timestamp: ~N[2019-01-01 23:00:01], browser: "Firefox"),
1392
- build(:imported_browsers, date: ~D[2019-01-01], browser: "Firefox", pageviews: 3)
1393
- ])
1394
-
1395
- conn =
1396
- get(
1397
- conn,
1398
- "/api/stats/#{site.domain}/suggestions/browser?period=month&date=2019-01-01&q=#{unquote(q)}&with_imported=true"
1399
- )
1400
-
1401
- assert json_response(conn, 200) == [
1402
- %{"value" => "Firefox", "label" => "Firefox"},
1403
- %{"value" => "Chrome", "label" => "Chrome"}
1404
- ]
1405
- end
1406
- end
1407
-
1408
- for {q, label} <- [{"", "without filter"}, {"i", "with filter"}] do
1409
- test "merges operating system suggestions from native and imported data #{label}", %{
1410
- conn: conn,
1411
- site: site,
1412
- site_import: site_import
1413
- } do
1414
- populate_stats(site, site_import.id, [
1415
- build(:pageview, timestamp: ~N[2019-01-01 23:00:01], operating_system: "Linux"),
1416
- build(:pageview, timestamp: ~N[2019-01-01 23:30:01], operating_system: "Linux"),
1417
- build(:pageview, timestamp: ~N[2019-01-01 23:40:01], operating_system: "Linux"),
1418
- build(:pageview, timestamp: ~N[2019-01-01 23:00:01], operating_system: "Windows"),
1419
- build(:imported_operating_systems,
1420
- date: ~D[2019-01-01],
1421
- operating_system: "Windows",
1422
- pageviews: 3
1423
- )
1424
- ])
1425
-
1426
- conn =
1427
- get(
1428
- conn,
1429
- "/api/stats/#{site.domain}/suggestions/operating_system?period=month&date=2019-01-01&q=#{unquote(q)}&with_imported=true"
1430
- )
1431
-
1432
- assert json_response(conn, 200) == [
1433
- %{"value" => "Windows", "label" => "Windows"},
1434
- %{"value" => "Linux", "label" => "Linux"}
1435
- ]
1436
- end
1437
- end
1438
-
1439
- test "does not query imported data when a different property is filtered by", %{
1440
- conn: conn,
1441
- site: site,
1442
- site_import: site_import
1443
- } do
1444
- populate_stats(site, site_import.id, [
1445
- build(:pageview,
1446
- timestamp: ~N[2019-01-01 23:00:01],
1447
- pathname: "/blog",
1448
- operating_system: "Linux"
1449
- ),
1450
- build(:imported_operating_systems, date: ~D[2019-01-01], operating_system: "Windows")
1451
- ])
1452
-
1453
- filters = Jason.encode!([[:is, "event:page", ["/blog"]]])
1454
-
1455
- conn =
1456
- get(
1457
- conn,
1458
- "/api/stats/#{site.domain}/suggestions/operating_system?period=month&date=2019-01-01&filters=#{filters}&q=&with_imported=true"
1459
- )
1460
-
1461
- assert json_response(conn, 200) == [%{"value" => "Linux", "label" => "Linux"}]
1462
- end
1463
-
1464
- test "queries imported data when filtering by the same property", %{
1465
- conn: conn,
1466
- site: site,
1467
- site_import: site_import
1468
- } do
1469
- populate_stats(site, site_import.id, [
1470
- build(:pageview,
1471
- timestamp: ~N[2019-01-01 23:00:01],
1472
- pathname: "/blog",
1473
- operating_system: "Linux"
1474
- ),
1475
- build(:imported_operating_systems, date: ~D[2019-01-01], operating_system: "Windows"),
1476
- build(:imported_operating_systems, date: ~D[2019-01-01], operating_system: "Linux")
1477
- ])
1478
-
1479
- filters = Jason.encode!([[:is_not, "visit:os", ["Linux"]]])
1480
-
1481
- conn =
1482
- get(
1483
- conn,
1484
- "/api/stats/#{site.domain}/suggestions/operating_system?period=month&date=2019-01-01&filters=#{filters}&q=&with_imported=true"
1485
- )
1486
-
1487
- assert json_response(conn, 200) == [%{"value" => "Windows", "label" => "Windows"}]
1488
- end
1489
- end
1490
- end