Leon4gr45 commited on
Commit
ba928b0
·
verified ·
1 Parent(s): 3b73bf4

Delete test/plausible_web/controllers/api/external_stats_controller/query_imported_test.exs with huggingface_hub

Browse files
test/plausible_web/controllers/api/external_stats_controller/query_imported_test.exs DELETED
@@ -1,1882 +0,0 @@
1
- defmodule PlausibleWeb.Api.ExternalStatsController.QueryImportedTest do
2
- use PlausibleWeb.ConnCase
3
-
4
- @user_id Enum.random(1000..9999)
5
-
6
- @unsupported_interval_warning Plausible.Stats.QueryResult.imports_warnings().unsupported_interval
7
- @unsupported_query_warning Plausible.Stats.QueryResult.imports_warnings().unsupported_query
8
-
9
- def unsupported_query_warning(), do: @unsupported_query_warning
10
-
11
- @no_imported_scroll_depth_metric_warning %{
12
- "code" => "no_imported_scroll_depth",
13
- "warning" => "No imports with scroll depth data were found"
14
- }
15
-
16
- setup [:create_user, :create_site, :create_api_key, :use_api_key]
17
-
18
- describe "aggregation with imported data" do
19
- setup :create_site_import
20
-
21
- test "does not count imported stats unless specified", %{
22
- conn: conn,
23
- site: site,
24
- site_import: site_import
25
- } do
26
- populate_stats(site, site_import.id, [
27
- build(:imported_visitors, date: ~D[2023-01-01]),
28
- build(:pageview, timestamp: ~N[2023-01-01 00:00:00])
29
- ])
30
-
31
- query_params = %{
32
- "site_id" => site.domain,
33
- "date_range" => "all",
34
- "metrics" => ["pageviews"]
35
- }
36
-
37
- conn1 = post(conn, "/api/v2/query", query_params)
38
-
39
- assert json_response(conn1, 200)["results"] == [%{"metrics" => [1], "dimensions" => []}]
40
-
41
- conn2 = post(conn, "/api/v2/query", Map.put(query_params, "include", %{"imports" => true}))
42
-
43
- assert json_response(conn2, 200)["results"] == [%{"metrics" => [2], "dimensions" => []}]
44
- assert json_response(conn2, 200)["meta"]["imports_included"]
45
- refute json_response(conn2, 200)["meta"]["imports_warning"]
46
- end
47
-
48
- test "filters correctly with 'is' operator", %{
49
- conn: conn,
50
- site: site,
51
- site_import: site_import
52
- } do
53
- populate_stats(site, site_import.id, [
54
- build(:pageview, pathname: "/blog", timestamp: ~N[2023-01-01 00:00:00]),
55
- build(:pageview, pathname: "/blog", timestamp: ~N[2023-01-01 00:00:00]),
56
- build(:pageview, pathname: "/blog/post/1", timestamp: ~N[2023-01-01 00:00:00]),
57
- build(:pageview, pathname: "/about", timestamp: ~N[2023-01-01 00:00:00]),
58
- build(:imported_pages,
59
- page: "/blog",
60
- pageviews: 5,
61
- visitors: 3,
62
- date: ~D[2023-01-01]
63
- ),
64
- build(:imported_pages,
65
- page: "/blog/post/1",
66
- pageviews: 2,
67
- visitors: 2,
68
- date: ~D[2023-01-01]
69
- )
70
- ])
71
-
72
- conn =
73
- post(conn, "/api/v2/query", %{
74
- "site_id" => site.domain,
75
- "date_range" => "all",
76
- "metrics" => ["visitors", "pageviews"],
77
- "filters" => [
78
- ["is", "event:page", ["/blog"]]
79
- ],
80
- "include" => %{"imports" => true}
81
- })
82
-
83
- assert json_response(conn, 200)["results"] == [
84
- %{"metrics" => [5, 7], "dimensions" => []}
85
- ]
86
- end
87
-
88
- test "filters correctly with 'is' operator (case insensitive)", %{
89
- conn: conn,
90
- site: site,
91
- site_import: site_import
92
- } do
93
- populate_stats(site, site_import.id, [
94
- build(:pageview, pathname: "/BLOG", timestamp: ~N[2023-01-01 00:00:00]),
95
- build(:pageview, pathname: "/blog", timestamp: ~N[2023-01-01 00:00:00]),
96
- build(:pageview, pathname: "/blog/post/1", timestamp: ~N[2023-01-01 00:00:00]),
97
- build(:pageview, pathname: "/about", timestamp: ~N[2023-01-01 00:00:00]),
98
- build(:imported_pages,
99
- page: "/BLOG",
100
- pageviews: 5,
101
- visitors: 3,
102
- date: ~D[2023-01-01]
103
- ),
104
- build(:imported_pages,
105
- page: "/blog/post/1",
106
- pageviews: 2,
107
- visitors: 2,
108
- date: ~D[2023-01-01]
109
- )
110
- ])
111
-
112
- conn =
113
- post(conn, "/api/v2/query", %{
114
- "site_id" => site.domain,
115
- "date_range" => "all",
116
- "metrics" => ["visitors", "pageviews"],
117
- "filters" => [
118
- ["is", "event:page", ["/blOG"], %{"case_sensitive" => false}]
119
- ],
120
- "include" => %{"imports" => true}
121
- })
122
-
123
- assert json_response(conn, 200)["results"] == [
124
- %{"metrics" => [5, 7], "dimensions" => []}
125
- ]
126
- end
127
-
128
- test "filters correctly with 'contains' operator", %{
129
- conn: conn,
130
- site: site,
131
- site_import: site_import
132
- } do
133
- populate_stats(site, site_import.id, [
134
- build(:pageview, pathname: "/blog", timestamp: ~N[2023-01-01 00:00:00]),
135
- build(:pageview, pathname: "/blog/post/1", timestamp: ~N[2023-01-01 00:00:00]),
136
- build(:pageview, pathname: "/blog/post/2", timestamp: ~N[2023-01-01 00:00:00]),
137
- build(:pageview, pathname: "/about", timestamp: ~N[2023-01-01 00:00:00]),
138
- build(:imported_pages,
139
- page: "/blog",
140
- pageviews: 5,
141
- visitors: 3,
142
- date: ~D[2023-01-01]
143
- ),
144
- build(:imported_pages,
145
- page: "/blog/post/1",
146
- pageviews: 2,
147
- visitors: 2,
148
- date: ~D[2023-01-01]
149
- ),
150
- build(:imported_pages,
151
- page: "/blog/POST/2",
152
- pageviews: 3,
153
- visitors: 1,
154
- date: ~D[2023-01-01]
155
- )
156
- ])
157
-
158
- conn =
159
- post(conn, "/api/v2/query", %{
160
- "site_id" => site.domain,
161
- "date_range" => "all",
162
- "metrics" => ["visitors", "pageviews"],
163
- "filters" => [
164
- ["contains", "event:page", ["blog/post"]]
165
- ],
166
- "include" => %{"imports" => true}
167
- })
168
-
169
- assert json_response(conn, 200)["results"] == [
170
- %{"metrics" => [4, 4], "dimensions" => []}
171
- ]
172
- end
173
-
174
- test "filters correctly with 'contains' operator (case insensitive)", %{
175
- conn: conn,
176
- site: site,
177
- site_import: site_import
178
- } do
179
- populate_stats(site, site_import.id, [
180
- build(:pageview, pathname: "/BLOG/post/1", timestamp: ~N[2023-01-01 00:00:00]),
181
- build(:pageview, pathname: "/blog/POST/2", timestamp: ~N[2023-01-01 00:00:00]),
182
- build(:pageview, pathname: "/about", timestamp: ~N[2023-01-01 00:00:00]),
183
- build(:imported_pages,
184
- page: "/BLOG/POST/1",
185
- pageviews: 5,
186
- visitors: 3,
187
- date: ~D[2023-01-01]
188
- ),
189
- build(:imported_pages,
190
- page: "/blog/post/2",
191
- pageviews: 2,
192
- visitors: 2,
193
- date: ~D[2023-01-01]
194
- )
195
- ])
196
-
197
- conn =
198
- post(conn, "/api/v2/query", %{
199
- "site_id" => site.domain,
200
- "date_range" => "all",
201
- "metrics" => ["visitors", "pageviews"],
202
- "filters" => [
203
- ["contains", "event:page", ["blog/POST"], %{"case_sensitive" => false}]
204
- ],
205
- "include" => %{"imports" => true}
206
- })
207
-
208
- assert json_response(conn, 200)["results"] == [
209
- %{"metrics" => [7, 9], "dimensions" => []}
210
- ]
211
- end
212
-
213
- test "aggregates custom event goals with 'is' and 'contains' operators", %{
214
- conn: conn,
215
- site: site,
216
- site_import: site_import
217
- } do
218
- insert(:goal, event_name: "Purchase", site: site)
219
-
220
- populate_stats(site, site_import.id, [
221
- build(:event,
222
- name: "Purchase",
223
- timestamp: ~N[2023-01-01 00:00:00]
224
- ),
225
- build(:event,
226
- name: "Purchase",
227
- timestamp: ~N[2023-01-01 00:00:00]
228
- ),
229
- build(:event,
230
- name: "Signup",
231
- timestamp: ~N[2023-01-01 00:00:00]
232
- ),
233
- build(:imported_custom_events,
234
- name: "Purchase",
235
- visitors: 3,
236
- events: 5,
237
- date: ~D[2023-01-01]
238
- ),
239
- build(:imported_custom_events,
240
- name: "Signup",
241
- visitors: 2,
242
- events: 2,
243
- date: ~D[2023-01-01]
244
- )
245
- ])
246
-
247
- conn =
248
- post(conn, "/api/v2/query", %{
249
- "site_id" => site.domain,
250
- "date_range" => "all",
251
- "metrics" => ["visitors", "events"],
252
- "filters" => [
253
- ["is", "event:goal", ["Purchase"]]
254
- ],
255
- "include" => %{"imports" => true}
256
- })
257
-
258
- assert json_response(conn, 200)["results"] == [
259
- %{"dimensions" => [], "metrics" => [5, 7]}
260
- ]
261
-
262
- conn =
263
- post(conn, "/api/v2/query", %{
264
- "site_id" => site.domain,
265
- "date_range" => "all",
266
- "metrics" => ["visitors", "events"],
267
- "filters" => [
268
- ["contains", "event:goal", ["Purch", "sign"]]
269
- ],
270
- "include" => %{"imports" => true}
271
- })
272
-
273
- assert json_response(conn, 200)["results"] == [
274
- %{"dimensions" => [], "metrics" => [5, 7]}
275
- ]
276
- end
277
-
278
- test "aggregates custom event goals with 'is' and 'contains' operators (case insensitive)", %{
279
- conn: conn,
280
- site: site,
281
- site_import: site_import
282
- } do
283
- insert(:goal, event_name: "Purchase", site: site)
284
-
285
- populate_stats(site, site_import.id, [
286
- build(:event,
287
- name: "Purchase",
288
- timestamp: ~N[2023-01-01 00:00:00]
289
- ),
290
- build(:event,
291
- name: "Purchase",
292
- timestamp: ~N[2023-01-01 00:00:00]
293
- ),
294
- build(:event,
295
- name: "Signup",
296
- timestamp: ~N[2023-01-01 00:00:00]
297
- ),
298
- build(:imported_custom_events,
299
- name: "Purchase",
300
- visitors: 3,
301
- events: 5,
302
- date: ~D[2023-01-01]
303
- ),
304
- build(:imported_custom_events,
305
- name: "Signup",
306
- visitors: 2,
307
- events: 2,
308
- date: ~D[2023-01-01]
309
- )
310
- ])
311
-
312
- conn =
313
- post(conn, "/api/v2/query", %{
314
- "site_id" => site.domain,
315
- "date_range" => "all",
316
- "metrics" => ["visitors", "events"],
317
- "filters" => [
318
- ["is", "event:goal", ["purchase"], %{"case_sensitive" => false}]
319
- ],
320
- "include" => %{"imports" => true}
321
- })
322
-
323
- assert json_response(conn, 200)["results"] == [
324
- %{"dimensions" => [], "metrics" => [5, 7]}
325
- ]
326
-
327
- conn =
328
- post(conn, "/api/v2/query", %{
329
- "site_id" => site.domain,
330
- "date_range" => "all",
331
- "metrics" => ["visitors", "events"],
332
- "filters" => [
333
- ["contains", "event:goal", ["PURCH"], %{"case_sensitive" => false}]
334
- ],
335
- "include" => %{"imports" => true}
336
- })
337
-
338
- assert json_response(conn, 200)["results"] == [
339
- %{"dimensions" => [], "metrics" => [5, 7]}
340
- ]
341
- end
342
- end
343
-
344
- describe "timeseries with imported data" do
345
- setup :create_site_import
346
-
347
- test "views_per_visit breakdown by time:month", %{
348
- conn: conn,
349
- site: site,
350
- site_import: site_import
351
- } do
352
- populate_stats(site, site_import.id, [
353
- # January 2021 - only imported
354
- build(:imported_visitors, date: ~D[2021-01-01], visits: 6, pageviews: 7),
355
- # March 2021 - imported + native combined
356
- build(:imported_visitors, date: ~D[2021-03-01], visits: 1, pageviews: 4),
357
- build(:pageview, user_id: 1, timestamp: ~N[2021-03-15 00:00:00]),
358
- build(:pageview, user_id: 1, timestamp: ~N[2021-03-15 00:05:00]),
359
- # September 2021 - only native
360
- build(:pageview, user_id: 2, timestamp: ~N[2021-09-01 00:00:00])
361
- ])
362
-
363
- conn =
364
- post(conn, "/api/v2/query", %{
365
- "site_id" => site.domain,
366
- "metrics" => ["views_per_visit"],
367
- "date_range" => ["2021-01-01", "2021-12-31"],
368
- "dimensions" => ["time:month"],
369
- "include" => %{"imports" => true}
370
- })
371
-
372
- assert json_response(conn, 200)["results"] == [
373
- %{"dimensions" => ["2021-01-01"], "metrics" => [1.17]},
374
- %{"dimensions" => ["2021-03-01"], "metrics" => [3.0]},
375
- %{"dimensions" => ["2021-09-01"], "metrics" => [1.0]}
376
- ]
377
- end
378
- end
379
-
380
- test "breaks down all metrics by visit:referrer with imported data", %{conn: conn, site: site} do
381
- site_import =
382
- insert(:site_import,
383
- site: site,
384
- start_date: ~D[2005-01-01],
385
- end_date: Date.utc_today(),
386
- source: :universal_analytics
387
- )
388
-
389
- populate_stats(site, site_import.id, [
390
- build(:pageview, referrer: "site.com", timestamp: ~N[2021-01-01 00:00:00]),
391
- build(:pageview, referrer: "site.com/1", timestamp: ~N[2021-01-01 00:00:00]),
392
- build(:imported_sources,
393
- referrer: "site.com",
394
- date: ~D[2021-01-01],
395
- visitors: 2,
396
- visits: 2,
397
- pageviews: 2,
398
- bounces: 1,
399
- visit_duration: 120
400
- ),
401
- build(:imported_sources,
402
- referrer: "site.com/2",
403
- date: ~D[2021-01-01],
404
- visitors: 2,
405
- visits: 2,
406
- pageviews: 2,
407
- bounces: 2,
408
- visit_duration: 0
409
- ),
410
- build(:imported_sources,
411
- date: ~D[2021-01-01],
412
- visitors: 10,
413
- visits: 11,
414
- pageviews: 50,
415
- bounces: 0,
416
- visit_duration: 1100
417
- )
418
- ])
419
-
420
- conn =
421
- post(conn, "/api/v2/query", %{
422
- "site_id" => site.domain,
423
- "metrics" => ["visitors", "visits", "pageviews", "bounce_rate", "visit_duration"],
424
- "date_range" => "all",
425
- "dimensions" => ["visit:referrer"],
426
- "include" => %{"imports" => true}
427
- })
428
-
429
- assert json_response(conn, 200)["results"] == [
430
- %{"dimensions" => ["Direct / None"], "metrics" => [10, 11, 50, 0.0, 100.0]},
431
- %{"dimensions" => ["site.com"], "metrics" => [3, 3, 3, 67.0, 40.0]},
432
- %{"dimensions" => ["site.com/2"], "metrics" => [2, 2, 2, 100.0, 0.0]},
433
- %{"dimensions" => ["site.com/1"], "metrics" => [1, 1, 1, 100.0, 0.0]}
434
- ]
435
- end
436
-
437
- test "breaks down all metrics by visit:utm_source with imported data", %{conn: conn, site: site} do
438
- site_import =
439
- insert(:site_import,
440
- site: site,
441
- start_date: ~D[2005-01-01],
442
- end_date: Date.utc_today(),
443
- source: :universal_analytics
444
- )
445
-
446
- populate_stats(site, site_import.id, [
447
- build(:pageview, utm_source: "SomeUTMSource", timestamp: ~N[2021-01-01 00:00:00]),
448
- build(:pageview, utm_source: "SomeUTMSource-1", timestamp: ~N[2021-01-01 00:00:00]),
449
- build(:imported_sources,
450
- utm_source: "SomeUTMSource",
451
- date: ~D[2021-01-01],
452
- visitors: 2,
453
- visits: 2,
454
- pageviews: 2,
455
- bounces: 1,
456
- visit_duration: 120
457
- ),
458
- build(:imported_sources,
459
- utm_source: "SomeUTMSource-2",
460
- date: ~D[2021-01-01],
461
- visitors: 2,
462
- visits: 2,
463
- pageviews: 2,
464
- bounces: 2,
465
- visit_duration: 0
466
- ),
467
- build(:imported_sources,
468
- date: ~D[2021-01-01],
469
- visitors: 10,
470
- visits: 11,
471
- pageviews: 50,
472
- bounces: 0,
473
- visit_duration: 1100
474
- )
475
- ])
476
-
477
- conn =
478
- post(conn, "/api/v2/query", %{
479
- "site_id" => site.domain,
480
- "metrics" => ["visitors", "visits", "pageviews", "bounce_rate", "visit_duration"],
481
- "date_range" => "all",
482
- "dimensions" => ["visit:utm_source"],
483
- "include" => %{"imports" => true}
484
- })
485
-
486
- %{"results" => results} = json_response(conn, 200)
487
-
488
- assert results == [
489
- %{"dimensions" => ["SomeUTMSource"], "metrics" => [3, 3, 3, 67.0, 40.0]},
490
- %{"dimensions" => ["SomeUTMSource-2"], "metrics" => [2, 2, 2, 100.0, 0.0]},
491
- %{"dimensions" => ["SomeUTMSource-1"], "metrics" => [1, 1, 1, 100.0, 0.0]}
492
- ]
493
- end
494
-
495
- test "pageviews breakdown by event:page - imported data having pageviews=0 and visitors=n should be bypassed",
496
- %{conn: conn, site: site} do
497
- site_import =
498
- insert(:site_import,
499
- site: site,
500
- start_date: ~D[2005-01-01],
501
- end_date: Date.utc_today(),
502
- source: :universal_analytics
503
- )
504
-
505
- populate_stats(site, site_import.id, [
506
- build(:pageview, pathname: "/", timestamp: ~N[2021-01-01 00:00:00]),
507
- build(:pageview, pathname: "/", timestamp: ~N[2021-01-01 00:25:00]),
508
- build(:pageview,
509
- pathname: "/plausible.io",
510
- timestamp: ~N[2021-01-01 00:00:00]
511
- ),
512
- build(:imported_pages,
513
- page: "/skip-me",
514
- date: ~D[2021-01-01],
515
- visitors: 1,
516
- pageviews: 0
517
- ),
518
- build(:imported_pages,
519
- page: "/include-me",
520
- date: ~D[2021-01-01],
521
- visitors: 1,
522
- pageviews: 1
523
- )
524
- ])
525
-
526
- conn =
527
- post(conn, "/api/v2/query", %{
528
- "site_id" => site.domain,
529
- "metrics" => ["pageviews"],
530
- "date_range" => "all",
531
- "dimensions" => ["event:page"],
532
- "include" => %{"imports" => true}
533
- })
534
-
535
- assert json_response(conn, 200)["results"] == [
536
- %{"dimensions" => ["/"], "metrics" => [2]},
537
- %{"dimensions" => ["/plausible.io"], "metrics" => [1]},
538
- %{"dimensions" => ["/include-me"], "metrics" => [1]}
539
- ]
540
- end
541
-
542
- describe "breakdown by visit:exit_page with" do
543
- setup %{site: site} do
544
- site_import = insert(:site_import, site: site)
545
-
546
- populate_stats(site, site_import.id, [
547
- build(:pageview,
548
- pathname: "/a",
549
- timestamp: ~N[2021-01-01 00:00:00]
550
- ),
551
- build(:pageview,
552
- user_id: @user_id,
553
- pathname: "/a",
554
- timestamp: ~N[2021-01-01 00:25:00]
555
- ),
556
- build(:pageview,
557
- user_id: @user_id,
558
- pathname: "/b",
559
- timestamp: ~N[2021-01-01 00:35:00]
560
- ),
561
- build(:imported_exit_pages,
562
- exit_page: "/b",
563
- exits: 3,
564
- visitors: 2,
565
- pageviews: 5,
566
- date: ~D[2021-01-01]
567
- )
568
- ])
569
- end
570
-
571
- test "can query with visit:exit_page dimension", %{conn: conn, site: site} do
572
- conn =
573
- post(conn, "/api/v2/query", %{
574
- "site_id" => site.domain,
575
- "metrics" => ["visits"],
576
- "date_range" => "all",
577
- "dimensions" => ["visit:exit_page"],
578
- "include" => %{"imports" => true}
579
- })
580
-
581
- %{"results" => results} = json_response(conn, 200)
582
-
583
- assert results == [
584
- %{"dimensions" => ["/b"], "metrics" => [4]},
585
- %{"dimensions" => ["/a"], "metrics" => [1]}
586
- ]
587
- end
588
- end
589
-
590
- describe "imported data" do
591
- test "returns screen sizes breakdown when filtering by screen size", %{conn: conn, site: site} do
592
- site_import = insert(:site_import, site: site)
593
-
594
- populate_stats(site, site_import.id, [
595
- build(:pageview,
596
- timestamp: ~N[2021-01-01 00:00:01],
597
- screen_size: "Mobile"
598
- ),
599
- build(:imported_devices,
600
- device: "Mobile",
601
- visitors: 3,
602
- pageviews: 5,
603
- date: ~D[2021-01-01]
604
- )
605
- ])
606
-
607
- conn =
608
- post(conn, "/api/v2/query", %{
609
- "site_id" => site.domain,
610
- "metrics" => ["visitors", "pageviews"],
611
- "date_range" => "all",
612
- "dimensions" => ["visit:device"],
613
- "filters" => [
614
- ["is", "visit:device", ["Mobile"]]
615
- ],
616
- "include" => %{"imports" => true}
617
- })
618
-
619
- %{"results" => results} = json_response(conn, 200)
620
-
621
- assert results == [%{"dimensions" => ["Mobile"], "metrics" => [4, 6]}]
622
- end
623
-
624
- test "returns custom event goals and pageview goals", %{conn: conn, site: site} do
625
- insert(:goal, site: site, event_name: "Purchase")
626
- insert(:goal, site: site, page_path: "/test")
627
-
628
- site_import = insert(:site_import, site: site)
629
-
630
- populate_stats(site, site_import.id, [
631
- build(:pageview,
632
- timestamp: ~N[2021-01-01 00:00:01],
633
- pathname: "/test"
634
- ),
635
- build(:event,
636
- name: "Purchase",
637
- timestamp: ~N[2021-01-01 00:00:03]
638
- ),
639
- build(:event,
640
- name: "Purchase",
641
- timestamp: ~N[2021-01-01 00:00:03]
642
- ),
643
- build(:imported_custom_events,
644
- name: "Purchase",
645
- visitors: 3,
646
- events: 5,
647
- date: ~D[2021-01-01]
648
- ),
649
- build(:imported_pages,
650
- page: "/test",
651
- visitors: 2,
652
- pageviews: 2,
653
- date: ~D[2021-01-01]
654
- ),
655
- build(:imported_visitors, visitors: 5, date: ~D[2021-01-01])
656
- ])
657
-
658
- conn =
659
- post(conn, "/api/v2/query", %{
660
- "site_id" => site.domain,
661
- "date_range" => "all",
662
- "dimensions" => ["event:goal"],
663
- "metrics" => ["visitors", "events", "pageviews", "conversion_rate"],
664
- "include" => %{"imports" => true}
665
- })
666
-
667
- assert json_response(conn, 200)["results"] == [
668
- %{"dimensions" => ["Purchase"], "metrics" => [5, 7, 0, 62.5]},
669
- %{"dimensions" => ["Visit /test"], "metrics" => [3, 3, 3, 37.5]}
670
- ]
671
- end
672
-
673
- test "pageviews are returned as events for breakdown reports other than custom events", %{
674
- conn: conn,
675
- site: site
676
- } do
677
- site_import = insert(:site_import, site: site)
678
-
679
- populate_stats(site, site_import.id, [
680
- build(:imported_browsers, browser: "Chrome", pageviews: 1, date: ~D[2021-01-01]),
681
- build(:imported_devices, device: "Desktop", pageviews: 1, date: ~D[2021-01-01]),
682
- build(:imported_entry_pages, entry_page: "/test", pageviews: 1, date: ~D[2021-01-01]),
683
- build(:imported_exit_pages, exit_page: "/test", pageviews: 1, date: ~D[2021-01-01]),
684
- build(:imported_locations, country: "EE", pageviews: 1, date: ~D[2021-01-01]),
685
- build(:imported_operating_systems,
686
- operating_system: "Mac",
687
- pageviews: 1,
688
- date: ~D[2021-01-01]
689
- ),
690
- build(:imported_pages, page: "/test", pageviews: 1, date: ~D[2021-01-01]),
691
- build(:imported_sources, source: "Google", pageviews: 1, date: ~D[2021-01-01])
692
- ])
693
-
694
- breakdown_and_first = fn dimension ->
695
- conn
696
- |> post("/api/v2/query", %{
697
- "site_id" => site.domain,
698
- "metrics" => ["events"],
699
- "date_range" => ["2021-01-01", "2021-01-01"],
700
- "dimensions" => [dimension],
701
- "include" => %{"imports" => true}
702
- })
703
- |> json_response(200)
704
- |> Map.get("results")
705
- |> List.first()
706
- end
707
-
708
- assert %{"dimensions" => ["Chrome"], "metrics" => [1]} =
709
- breakdown_and_first.("visit:browser")
710
-
711
- assert %{"dimensions" => ["Desktop"], "metrics" => [1]} =
712
- breakdown_and_first.("visit:device")
713
-
714
- assert %{"dimensions" => ["EE"], "metrics" => [1]} = breakdown_and_first.("visit:country")
715
- assert %{"dimensions" => ["Mac"], "metrics" => [1]} = breakdown_and_first.("visit:os")
716
- assert %{"dimensions" => ["/test"], "metrics" => [1]} = breakdown_and_first.("event:page")
717
-
718
- assert %{"dimensions" => ["Google"], "metrics" => [1]} =
719
- breakdown_and_first.("visit:source")
720
- end
721
-
722
- for event_name <- Plausible.Event.SystemEvents.events_with_url_prop() do
723
- test "returns url breakdown for #{event_name} goal", %{conn: conn, site: site} do
724
- insert(:goal, event_name: unquote(event_name), site: site)
725
- site_import = insert(:site_import, site: site)
726
-
727
- populate_stats(site, site_import.id, [
728
- build(:event,
729
- name: unquote(event_name),
730
- "meta.key": ["url"],
731
- "meta.value": ["https://one.com"]
732
- ),
733
- build(:imported_custom_events,
734
- name: unquote(event_name),
735
- visitors: 2,
736
- events: 5,
737
- link_url: "https://one.com"
738
- ),
739
- build(:imported_custom_events,
740
- name: unquote(event_name),
741
- visitors: 5,
742
- events: 10,
743
- link_url: "https://two.com"
744
- ),
745
- build(:imported_custom_events,
746
- name: "some goal",
747
- visitors: 5,
748
- events: 10
749
- ),
750
- build(:imported_visitors, visitors: 9)
751
- ])
752
-
753
- conn =
754
- post(conn, "/api/v2/query", %{
755
- "site_id" => site.domain,
756
- "metrics" => ["visitors", "events", "conversion_rate"],
757
- "date_range" => "all",
758
- "dimensions" => ["event:props:url"],
759
- "filters" => [
760
- ["is", "event:goal", [unquote(event_name)]]
761
- ],
762
- "include" => %{"imports" => true}
763
- })
764
-
765
- assert json_response(conn, 200)["results"] == [
766
- %{"dimensions" => ["https://two.com"], "metrics" => [5, 10, 50]},
767
- %{"dimensions" => ["https://one.com"], "metrics" => [3, 6, 30]}
768
- ]
769
-
770
- assert json_response(conn, 200)["meta"]["imports_included"]
771
- refute json_response(conn, 200)["meta"]["imports_warning"]
772
- end
773
- end
774
-
775
- for event_name <- Plausible.Event.SystemEvents.events_with_path_prop() do
776
- test "returns path breakdown for #{event_name} goal", %{conn: conn, site: site} do
777
- insert(:goal, event_name: unquote(event_name), site: site)
778
- site_import = insert(:site_import, site: site)
779
-
780
- populate_stats(site, site_import.id, [
781
- build(:event,
782
- name: unquote(event_name),
783
- "meta.key": ["path"],
784
- "meta.value": ["/one"]
785
- ),
786
- build(:imported_custom_events,
787
- name: unquote(event_name),
788
- visitors: 2,
789
- events: 5,
790
- path: "/one"
791
- ),
792
- build(:imported_custom_events,
793
- name: unquote(event_name),
794
- visitors: 5,
795
- events: 10,
796
- path: "/two"
797
- ),
798
- build(:imported_custom_events,
799
- name: "some goal",
800
- visitors: 5,
801
- events: 10
802
- ),
803
- build(:imported_visitors, visitors: 9)
804
- ])
805
-
806
- conn =
807
- post(conn, "/api/v2/query", %{
808
- "site_id" => site.domain,
809
- "metrics" => ["visitors", "events", "conversion_rate"],
810
- "date_range" => "all",
811
- "dimensions" => ["event:props:path"],
812
- "filters" => [
813
- ["is", "event:goal", [unquote(event_name)]]
814
- ],
815
- "include" => %{"imports" => true}
816
- })
817
-
818
- assert json_response(conn, 200)["results"] == [
819
- %{"dimensions" => ["/two"], "metrics" => [5, 10, 50]},
820
- %{"dimensions" => ["/one"], "metrics" => [3, 6, 30]}
821
- ]
822
-
823
- assert json_response(conn, 200)["meta"]["imports_included"]
824
- refute json_response(conn, 200)["meta"]["imports_warning"]
825
- end
826
- end
827
-
828
- test "gracefully ignores unsupported WP Search Queries goal for imported data", %{
829
- conn: conn,
830
- site: site
831
- } do
832
- insert(:goal, event_name: "WP Search Queries", site: site)
833
- site_import = insert(:site_import, site: site)
834
-
835
- populate_stats(site, site_import.id, [
836
- build(:event,
837
- name: "WP Search Queries",
838
- "meta.key": ["search_query", "result_count"],
839
- "meta.value": ["some phrase", "12"]
840
- ),
841
- build(:imported_custom_events,
842
- name: "view_search_results",
843
- visitors: 100,
844
- events: 200
845
- ),
846
- build(:imported_visitors, visitors: 9)
847
- ])
848
-
849
- conn =
850
- post(conn, "/api/v2/query", %{
851
- "site_id" => site.domain,
852
- "metrics" => ["visitors", "events", "conversion_rate"],
853
- "date_range" => "all",
854
- "dimensions" => ["event:props:search_query"],
855
- "filters" => [
856
- ["is", "event:goal", ["WP Search Queries"]]
857
- ],
858
- "include" => %{"imports" => true}
859
- })
860
-
861
- assert json_response(conn, 200)["results"] == [
862
- %{"dimensions" => ["some phrase"], "metrics" => [1, 1, 100.0]}
863
- ]
864
- end
865
-
866
- test "includes imported data for time:day dimension", %{
867
- conn: conn,
868
- site: site
869
- } do
870
- site_import = insert(:site_import, site: site)
871
-
872
- insert(:goal, event_name: "Signup", site: site)
873
-
874
- populate_stats(site, site_import.id, [
875
- build(:pageview, timestamp: ~N[2021-01-01 00:00:00]),
876
- build(:pageview, timestamp: ~N[2021-01-01 00:10:00]),
877
- build(:pageview, timestamp: ~N[2021-01-01 23:59:00]),
878
- build(:imported_visitors, date: ~D[2021-01-01], visitors: 5)
879
- ])
880
-
881
- conn =
882
- post(conn, "/api/v2/query", %{
883
- "site_id" => site.domain,
884
- "metrics" => ["visitors"],
885
- "date_range" => ["2021-01-01", "2021-01-02"],
886
- "dimensions" => ["time:day"],
887
- "include" => %{"imports" => true}
888
- })
889
-
890
- assert json_response(conn, 200)["results"] == [
891
- %{"dimensions" => ["2021-01-01"], "metrics" => [8]}
892
- ]
893
-
894
- assert json_response(conn, 200)["meta"] == %{"imports_included" => true}
895
- end
896
-
897
- test "adds a warning when time:hour dimension", %{
898
- conn: conn,
899
- site: site
900
- } do
901
- site_import = insert(:site_import, site: site)
902
-
903
- insert(:goal, event_name: "Signup", site: site)
904
-
905
- populate_stats(site, site_import.id, [
906
- build(:pageview, timestamp: ~N[2021-01-01 00:00:00]),
907
- build(:pageview, timestamp: ~N[2021-01-01 00:10:00]),
908
- build(:pageview, timestamp: ~N[2021-01-01 23:59:00]),
909
- build(:imported_visitors, date: ~D[2021-01-01], visitors: 5)
910
- ])
911
-
912
- conn =
913
- post(conn, "/api/v2/query", %{
914
- "site_id" => site.domain,
915
- "metrics" => ["visitors"],
916
- "date_range" => ["2021-01-01", "2021-01-02"],
917
- "dimensions" => ["time:hour"],
918
- "include" => %{"imports" => true}
919
- })
920
-
921
- assert json_response(conn, 200)["results"] == [
922
- %{"dimensions" => ["2021-01-01 00:00:00"], "metrics" => [2]},
923
- %{"dimensions" => ["2021-01-01 23:00:00"], "metrics" => [1]}
924
- ]
925
-
926
- refute json_response(conn, 200)["meta"]["imports_included"]
927
- assert json_response(conn, 200)["meta"]["imports_skip_reason"] == "unsupported_interval"
928
-
929
- assert json_response(conn, 200)["meta"]["imports_warning"] == @unsupported_interval_warning
930
- end
931
-
932
- test "adds a warning when query params are not supported for imported data", %{
933
- conn: conn,
934
- site: site
935
- } do
936
- site_import = insert(:site_import, site: site)
937
-
938
- insert(:goal, event_name: "Signup", site: site)
939
-
940
- populate_stats(site, site_import.id, [
941
- build(:event,
942
- name: "Signup",
943
- "meta.key": ["package"],
944
- "meta.value": ["large"]
945
- ),
946
- build(:imported_visitors, visitors: 9)
947
- ])
948
-
949
- conn =
950
- post(conn, "/api/v2/query", %{
951
- "site_id" => site.domain,
952
- "metrics" => ["visitors"],
953
- "date_range" => "all",
954
- "dimensions" => ["event:props:package"],
955
- "filters" => [
956
- ["is", "event:goal", ["Signup"]]
957
- ],
958
- "include" => %{"imports" => true}
959
- })
960
-
961
- assert json_response(conn, 200)["results"] == [
962
- %{"dimensions" => ["large"], "metrics" => [1]}
963
- ]
964
-
965
- refute json_response(conn, 200)["meta"]["imports_included"]
966
- assert json_response(conn, 200)["meta"]["imports_skip_reason"] == "unsupported_query"
967
-
968
- assert json_response(conn, 200)["meta"]["imports_warning"] == @unsupported_query_warning
969
- end
970
-
971
- test "does not add a warning when there are no site imports", %{conn: conn, site: site} do
972
- insert(:goal, event_name: "Signup", site: site)
973
-
974
- populate_stats(site, [
975
- build(:event,
976
- name: "Signup",
977
- "meta.key": ["package"],
978
- "meta.value": ["large"]
979
- )
980
- ])
981
-
982
- conn =
983
- post(conn, "/api/v2/query", %{
984
- "site_id" => site.domain,
985
- "metrics" => ["visitors"],
986
- "date_range" => "all",
987
- "dimensions" => ["event:props:package"],
988
- "filters" => [
989
- ["is", "event:goal", ["Signup"]]
990
- ],
991
- "include" => %{"imports" => true}
992
- })
993
-
994
- assert json_response(conn, 200)["meta"] == %{
995
- "imports_included" => false,
996
- "imports_skip_reason" => "no_imported_data"
997
- }
998
- end
999
-
1000
- test "does not add a warning when import is out of queried date range", %{
1001
- conn: conn,
1002
- site: site
1003
- } do
1004
- site_import = insert(:site_import, site: site, end_date: Date.add(Date.utc_today(), -3))
1005
-
1006
- insert(:goal, event_name: "Signup", site: site)
1007
-
1008
- populate_stats(site, site_import.id, [
1009
- build(:event,
1010
- name: "Signup",
1011
- "meta.key": ["package"],
1012
- "meta.value": ["large"]
1013
- ),
1014
- build(:imported_visitors, visitors: 9)
1015
- ])
1016
-
1017
- conn =
1018
- post(conn, "/api/v2/query", %{
1019
- "site_id" => site.domain,
1020
- "metrics" => ["visitors"],
1021
- "date_range" => "day",
1022
- "dimensions" => ["event:props:package"],
1023
- "filters" => [
1024
- ["is", "event:goal", ["Signup"]]
1025
- ],
1026
- "include" => %{"imports" => true}
1027
- })
1028
-
1029
- assert json_response(conn, 200)["meta"] == %{
1030
- "imports_included" => false,
1031
- "imports_skip_reason" => "out_of_range"
1032
- }
1033
- end
1034
-
1035
- test "applies multiple filters if the properties belong to the same table", %{
1036
- conn: conn,
1037
- site: site
1038
- } do
1039
- site_import = insert(:site_import, site: site)
1040
-
1041
- populate_stats(site, site_import.id, [
1042
- build(:imported_sources, source: "Google", utm_medium: "organic", utm_term: "one"),
1043
- build(:imported_sources, source: "Twitter", utm_medium: "organic", utm_term: "two"),
1044
- build(:imported_sources,
1045
- source: "Facebook",
1046
- utm_medium: "something_else",
1047
- utm_term: "one"
1048
- )
1049
- ])
1050
-
1051
- conn =
1052
- post(conn, "/api/v2/query", %{
1053
- "site_id" => site.domain,
1054
- "metrics" => ["visitors"],
1055
- "date_range" => "day",
1056
- "dimensions" => ["visit:source"],
1057
- "filters" => [
1058
- ["is", "visit:utm_medium", ["organic"]],
1059
- ["is", "visit:utm_term", ["one"]]
1060
- ],
1061
- "include" => %{"imports" => true}
1062
- })
1063
-
1064
- assert json_response(conn, 200)["results"] == [
1065
- %{"dimensions" => ["Google"], "metrics" => [1]}
1066
- ]
1067
- end
1068
-
1069
- test "ignores imported data if filtered property belongs to a different table than the breakdown property",
1070
- %{
1071
- conn: conn,
1072
- site: site
1073
- } do
1074
- site_import = insert(:site_import, site: site)
1075
-
1076
- populate_stats(site, site_import.id, [
1077
- build(:imported_sources, source: "Google"),
1078
- build(:imported_devices, device: "Desktop")
1079
- ])
1080
-
1081
- conn =
1082
- post(conn, "/api/v2/query", %{
1083
- "site_id" => site.domain,
1084
- "metrics" => ["visitors"],
1085
- "date_range" => "day",
1086
- "dimensions" => ["visit:source"],
1087
- "filters" => [
1088
- ["is", "visit:device", ["Desktop"]]
1089
- ],
1090
- "include" => %{"imports" => true}
1091
- })
1092
-
1093
- assert %{
1094
- "results" => [],
1095
- "meta" => meta
1096
- } = json_response(conn, 200)
1097
-
1098
- refute json_response(conn, 200)["meta"]["imports_included"]
1099
- assert meta["imports_warning"] == @unsupported_query_warning
1100
- end
1101
-
1102
- test "imported country, region and city data",
1103
- %{
1104
- conn: conn,
1105
- site: site
1106
- } do
1107
- site_import = insert(:site_import, site: site)
1108
-
1109
- populate_stats(site, site_import.id, [
1110
- build(:pageview,
1111
- timestamp: ~N[2021-01-01 00:15:00],
1112
- country_code: "DE",
1113
- subdivision1_code: "DE-BE",
1114
- city_geoname_id: 2_950_159
1115
- ),
1116
- build(:pageview,
1117
- timestamp: ~N[2021-01-01 00:15:00],
1118
- country_code: "DE",
1119
- subdivision1_code: "DE-BE",
1120
- city_geoname_id: 2_950_159
1121
- ),
1122
- build(:pageview,
1123
- timestamp: ~N[2021-01-01 00:15:00],
1124
- country_code: "EE",
1125
- subdivision1_code: "EE-37",
1126
- city_geoname_id: 588_409
1127
- ),
1128
- build(:imported_locations, country: "EE", region: "EE-37", city: 588_409, visitors: 33)
1129
- ])
1130
-
1131
- for {dimension, stats_value, imports_value} <- [
1132
- {"visit:country", "DE", "EE"},
1133
- {"visit:region", "DE-BE", "EE-37"},
1134
- {"visit:city", 2_950_159, 588_409},
1135
- {"visit:country_name", "Germany", "Estonia"},
1136
- {"visit:region_name", "Berlin", "Harjumaa"},
1137
- {"visit:city_name", "Berlin", "Tallinn"}
1138
- ] do
1139
- conn =
1140
- post(conn, "/api/v2/query", %{
1141
- "site_id" => site.domain,
1142
- "metrics" => ["visitors"],
1143
- "date_range" => "all",
1144
- "dimensions" => [dimension],
1145
- "include" => %{"imports" => true}
1146
- })
1147
-
1148
- assert json_response(conn, 200)["results"] == [
1149
- %{"dimensions" => [imports_value], "metrics" => [34]},
1150
- %{"dimensions" => [stats_value], "metrics" => [2]}
1151
- ]
1152
- end
1153
- end
1154
-
1155
- test "imported non-empty region IDs that don't have a corresponding name in our locations dictionary have region_name set to the ID itself, empty region IDs are excluded",
1156
- %{conn: conn, site: site} do
1157
- site_import = insert(:site_import, site: site)
1158
-
1159
- populate_stats(site, site_import.id, [
1160
- # GA4 import: `region` is the name of the `region`
1161
- build(:imported_locations, country: "US", region: "California", visitors: 10),
1162
- # Plausible import: `region` is the region ID
1163
- build(:imported_locations, country: "EE", region: "EE-37", visitors: 5),
1164
- # Plausible import: `region` is the region ID, but the ID is stale
1165
- build(:imported_locations, country: "NO", region: "NO-99", visitors: 50),
1166
- # Plausible / GA4 import when the region is unknown
1167
- build(:imported_locations, country: "US", region: "", visitors: 99)
1168
- ])
1169
-
1170
- for {dimensions, expected_dimension_values} <- [
1171
- {["visit:region_name", "visit:region"],
1172
- [
1173
- NO: ["NO-99", "NO-99"],
1174
- US: ["California", "California"],
1175
- EE: ["Harjumaa", "EE-37"]
1176
- ]},
1177
- {["visit:region"],
1178
- [
1179
- NO: ["NO-99"],
1180
- US: ["California"],
1181
- EE: ["EE-37"]
1182
- ]},
1183
- {["visit:region_name"],
1184
- [
1185
- NO: ["NO-99"],
1186
- US: ["California"],
1187
- EE: ["Harjumaa"]
1188
- ]}
1189
- ] do
1190
- conn =
1191
- post(conn, "/api/v2/query", %{
1192
- "site_id" => site.domain,
1193
- "metrics" => ["visitors"],
1194
- "date_range" => "all",
1195
- "dimensions" => dimensions,
1196
- "include" => %{"imports" => true}
1197
- })
1198
-
1199
- assert json_response(conn, 200)["results"] == [
1200
- %{"dimensions" => expected_dimension_values[:NO], "metrics" => [50]},
1201
- %{"dimensions" => expected_dimension_values[:US], "metrics" => [10]},
1202
- %{"dimensions" => expected_dimension_values[:EE], "metrics" => [5]}
1203
- ]
1204
- end
1205
- end
1206
-
1207
- test "imported country and city names", %{
1208
- site: site,
1209
- conn: conn
1210
- } do
1211
- site_import = insert(:site_import, site: site)
1212
-
1213
- populate_stats(site, site_import.id, [
1214
- build(:pageview,
1215
- country_code: "GB",
1216
- # London
1217
- city_geoname_id: 2_643_743
1218
- ),
1219
- build(:pageview,
1220
- country_code: "CA",
1221
- # Different London
1222
- city_geoname_id: 6_058_560
1223
- ),
1224
- build(:imported_locations, country: "GB", city: 2_643_743, visitors: 33)
1225
- ])
1226
-
1227
- conn =
1228
- post(conn, "/api/v2/query", %{
1229
- "site_id" => site.domain,
1230
- "metrics" => ["visitors"],
1231
- "date_range" => "all",
1232
- "dimensions" => ["visit:city_name"],
1233
- "include" => %{"imports" => true}
1234
- })
1235
-
1236
- assert json_response(conn, 200)["results"] == [
1237
- %{"dimensions" => ["London"], "metrics" => [35]}
1238
- ]
1239
-
1240
- conn =
1241
- post(conn, "/api/v2/query", %{
1242
- "site_id" => site.domain,
1243
- "metrics" => ["visitors"],
1244
- "date_range" => "all",
1245
- "dimensions" => ["visit:city_name", "visit:country_name"],
1246
- "include" => %{"imports" => true}
1247
- })
1248
-
1249
- assert json_response(conn, 200)["results"] == [
1250
- %{"dimensions" => ["London", "United Kingdom"], "metrics" => [34]},
1251
- %{"dimensions" => ["London", "Canada"], "metrics" => [1]}
1252
- ]
1253
- end
1254
-
1255
- test "imported country and city names with complex conditions", %{
1256
- site: site,
1257
- conn: conn
1258
- } do
1259
- site_import = insert(:site_import, site: site)
1260
-
1261
- populate_stats(site, site_import.id, [
1262
- build(:pageview,
1263
- country_code: "GB",
1264
- # London
1265
- city_geoname_id: 2_643_743
1266
- ),
1267
- build(:pageview,
1268
- country_code: "CA",
1269
- # Different London
1270
- city_geoname_id: 6_058_560
1271
- ),
1272
- build(:imported_locations,
1273
- country: "EE",
1274
- # Tallinn
1275
- city: 588_409,
1276
- visitors: 3
1277
- ),
1278
- build(:imported_locations,
1279
- country: "EE",
1280
- # Kärdla
1281
- city: 591_632,
1282
- visitors: 2
1283
- ),
1284
- build(:imported_locations,
1285
- country: "GB",
1286
- # London
1287
- city: 2_643_743,
1288
- visitors: 33
1289
- )
1290
- ])
1291
-
1292
- conn =
1293
- post(conn, "/api/v2/query", %{
1294
- "site_id" => site.domain,
1295
- "metrics" => ["visitors"],
1296
- "date_range" => "all",
1297
- "dimensions" => ["visit:city_name", "visit:country_name"],
1298
- "filters" => [
1299
- [
1300
- "or",
1301
- [
1302
- [
1303
- "and",
1304
- [
1305
- ["is", "visit:city_name", ["London"]],
1306
- ["not", ["is", "visit:country_name", ["Canada"]]]
1307
- ]
1308
- ],
1309
- ["is", "visit:country_name", ["Estonia"]]
1310
- ]
1311
- ]
1312
- ],
1313
- "include" => %{"imports" => true}
1314
- })
1315
-
1316
- assert json_response(conn, 200)["results"] == [
1317
- %{"dimensions" => ["London", "United Kingdom"], "metrics" => [34]},
1318
- %{"dimensions" => ["Tallinn", "Estonia"], "metrics" => [3]},
1319
- %{"dimensions" => ["Kärdla", "Estonia"], "metrics" => [2]}
1320
- ]
1321
- end
1322
-
1323
- test "page breakdown with paginate_optimization (ideal case)", %{
1324
- conn: conn,
1325
- site: site
1326
- } do
1327
- site_import = insert(:site_import, site: site)
1328
-
1329
- populate_stats(
1330
- site,
1331
- site_import.id,
1332
- [
1333
- build(:pageview, pathname: "/99", timestamp: ~N[2021-01-01 00:00:00])
1334
- ] ++
1335
- Enum.map(1..100, fn i ->
1336
- build(:imported_pages, page: "/#{i}", pageviews: 1, visitors: 1, date: ~D[2021-01-01])
1337
- end)
1338
- )
1339
-
1340
- conn =
1341
- post(conn, "/api/v2/query", %{
1342
- "site_id" => site.domain,
1343
- "metrics" => ["pageviews"],
1344
- "date_range" => "all",
1345
- "dimensions" => ["event:page"],
1346
- "include" => %{"imports" => true},
1347
- "pagination" => %{"limit" => 1}
1348
- })
1349
-
1350
- assert json_response(conn, 200)["results"] == [%{"dimensions" => ["/99"], "metrics" => [2]}]
1351
- end
1352
-
1353
- test "page breakdown with paginate_optimization (lossy case)", %{
1354
- conn: conn,
1355
- site: site
1356
- } do
1357
- site_import = insert(:site_import, site: site)
1358
-
1359
- populate_stats(
1360
- site,
1361
- site_import.id,
1362
- [
1363
- build(:pageview, pathname: "/99", timestamp: ~N[2021-01-01 00:00:00])
1364
- ] ++
1365
- Enum.map(1..200, fn i ->
1366
- build(:imported_pages, page: "/#{i}", pageviews: 1, visitors: 1, date: ~D[2021-01-01])
1367
- end)
1368
- )
1369
-
1370
- conn =
1371
- post(conn, "/api/v2/query", %{
1372
- "site_id" => site.domain,
1373
- "metrics" => ["pageviews"],
1374
- "date_range" => "all",
1375
- "dimensions" => ["event:page"],
1376
- "include" => %{"imports" => true},
1377
- "pagination" => %{"limit" => 1}
1378
- })
1379
-
1380
- [%{"dimensions" => ["/99"], "metrics" => [pageviews]}] = json_response(conn, 200)["results"]
1381
-
1382
- # This is non-deterministic since /99 might not be in the top N items of imported pages subquery.
1383
- assert pageviews in 1..2
1384
- end
1385
- end
1386
-
1387
- describe "metric warnings" do
1388
- test "returns scroll_depth warning when a site_import without scroll depth is in queried range",
1389
- %{
1390
- conn: conn,
1391
- site: site
1392
- } do
1393
- site_import =
1394
- insert(:site_import, site: site, start_date: ~D[2021-02-01], end_date: ~D[2021-02-28])
1395
-
1396
- populate_stats(site, site_import.id, [
1397
- build(:pageview, user_id: 123, pathname: "/", timestamp: ~N[2021-02-01 00:00:00]),
1398
- build(:engagement,
1399
- user_id: 123,
1400
- pathname: "/",
1401
- timestamp: ~N[2021-02-01 00:01:00],
1402
- scroll_depth: 70
1403
- ),
1404
- build(:imported_pages, page: "/", date: ~D[2021-02-01]),
1405
- build(:imported_pages, page: "/", date: ~D[2021-02-28])
1406
- ])
1407
-
1408
- conn =
1409
- post(conn, "/api/v2/query", %{
1410
- "site_id" => site.domain,
1411
- "metrics" => ["visitors", "scroll_depth"],
1412
- "date_range" => "all",
1413
- "filters" => [["is", "event:page", ["/"]]],
1414
- "include" => %{"imports" => true}
1415
- })
1416
-
1417
- assert %{"results" => results, "meta" => meta} = json_response(conn, 200)
1418
-
1419
- assert results == [%{"dimensions" => [], "metrics" => [3, 70]}]
1420
-
1421
- assert meta["imports_included"] == true
1422
-
1423
- assert meta["metric_warnings"] == %{
1424
- "scroll_depth" => @no_imported_scroll_depth_metric_warning
1425
- }
1426
- end
1427
-
1428
- test "does not return scroll depth warning when imports requested but rejected", %{
1429
- conn: conn,
1430
- site: site
1431
- } do
1432
- site_import =
1433
- insert(:site_import, site: site, start_date: ~D[2021-02-01], end_date: ~D[2021-02-28])
1434
-
1435
- populate_stats(site, site_import.id, [
1436
- build(:pageview, user_id: 123, pathname: "/", timestamp: ~N[2021-02-01 00:00:00]),
1437
- build(:engagement,
1438
- user_id: 123,
1439
- pathname: "/",
1440
- timestamp: ~N[2021-02-01 00:01:00],
1441
- scroll_depth: 70
1442
- ),
1443
- build(:imported_pages, page: "/", date: ~D[2021-02-01]),
1444
- build(:imported_pages, page: "/", date: ~D[2021-02-28])
1445
- ])
1446
-
1447
- conn =
1448
- post(conn, "/api/v2/query", %{
1449
- "site_id" => site.domain,
1450
- "metrics" => ["visitors", "scroll_depth"],
1451
- "date_range" => "all",
1452
- "filters" => [["is", "event:page", ["/"]], ["is_not", "visit:browser", ["abc"]]],
1453
- "include" => %{"imports" => true}
1454
- })
1455
-
1456
- assert %{"results" => results, "meta" => meta} = json_response(conn, 200)
1457
-
1458
- assert results == [%{"dimensions" => [], "metrics" => [1, 70]}]
1459
-
1460
- refute meta["imports_included"]
1461
- refute meta["metric_warnings"]["scroll_depth"]
1462
- end
1463
-
1464
- test "does not return scroll_depth warning when imports without scroll depth exist but outside the queried range",
1465
- %{
1466
- conn: conn,
1467
- site: site
1468
- } do
1469
- site_import =
1470
- insert(:site_import,
1471
- site: site,
1472
- start_date: ~D[2021-02-01],
1473
- end_date: ~D[2021-02-28]
1474
- )
1475
-
1476
- populate_stats(site, site_import.id, [
1477
- build(:imported_pages, page: "/", date: ~D[2021-02-01]),
1478
- build(:imported_pages, page: "/", date: ~D[2021-02-28])
1479
- ])
1480
-
1481
- conn =
1482
- post(conn, "/api/v2/query", %{
1483
- "site_id" => site.domain,
1484
- "metrics" => ["visitors", "scroll_depth"],
1485
- "date_range" => ["2022-01-01", "2022-01-31"],
1486
- "dimensions" => ["event:page"],
1487
- "include" => %{"imports" => true}
1488
- })
1489
-
1490
- assert %{"results" => [], "meta" => meta} = json_response(conn, 200)
1491
-
1492
- refute meta["imports_included"]
1493
- refute meta["metric_warnings"]["scroll_depth"]
1494
- end
1495
-
1496
- test "does not return scroll_depth warning when imported scroll depth exists", %{
1497
- conn: conn,
1498
- site: site
1499
- } do
1500
- site_import =
1501
- insert(:site_import,
1502
- site: site,
1503
- start_date: ~D[2021-02-01],
1504
- end_date: ~D[2021-02-28],
1505
- has_scroll_depth: true
1506
- )
1507
-
1508
- populate_stats(site, site_import.id, [
1509
- build(:pageview, user_id: 123, pathname: "/", timestamp: ~N[2021-02-01 00:00:00]),
1510
- build(:engagement,
1511
- user_id: 123,
1512
- pathname: "/",
1513
- timestamp: ~N[2021-02-01 00:01:00],
1514
- scroll_depth: 70
1515
- ),
1516
- build(:imported_pages,
1517
- page: "/",
1518
- date: ~D[2021-02-01],
1519
- total_scroll_depth: 80,
1520
- total_scroll_depth_visits: 1
1521
- ),
1522
- build(:imported_pages, page: "/", date: ~D[2021-02-28])
1523
- ])
1524
-
1525
- conn =
1526
- post(conn, "/api/v2/query", %{
1527
- "site_id" => site.domain,
1528
- "metrics" => ["visitors", "scroll_depth"],
1529
- "date_range" => "all",
1530
- "filters" => [["is", "event:page", ["/"]]],
1531
- "include" => %{"imports" => true}
1532
- })
1533
-
1534
- assert %{"results" => results, "meta" => meta} = json_response(conn, 200)
1535
-
1536
- assert results == [%{"dimensions" => [], "metrics" => [3, 75]}]
1537
-
1538
- assert meta["imports_included"]
1539
- refute meta["metric_warnings"]["scroll_depth"]
1540
- end
1541
-
1542
- test "returns bounce_rate warning when query includes imported and page filter used", %{
1543
- conn: conn,
1544
- site: site
1545
- } do
1546
- site_import =
1547
- insert(:site_import, site: site, start_date: ~D[2021-02-01], end_date: ~D[2021-02-28])
1548
-
1549
- populate_stats(site, site_import.id, [
1550
- build(:pageview, user_id: 123, pathname: "/", timestamp: ~N[2021-02-01 00:00:00]),
1551
- build(:pageview, user_id: 123, pathname: "/next", timestamp: ~N[2021-02-01 00:10:00]),
1552
- build(:pageview, pathname: "/", timestamp: ~N[2021-02-01 00:00:00]),
1553
- build(:imported_pages, page: "/", date: ~D[2021-02-01]),
1554
- build(:imported_pages, pageviews: 3, page: "/", date: ~D[2021-02-02]),
1555
- build(:imported_visitors, date: ~D[2021-02-01]),
1556
- build(:imported_visitors, pageviews: 3, date: ~D[2021-02-02])
1557
- ])
1558
-
1559
- conn =
1560
- post(conn, "/api/v2/query", %{
1561
- "site_id" => site.domain,
1562
- "metrics" => ["visitors", "pageviews", "bounce_rate"],
1563
- "date_range" => "all",
1564
- "filters" => [["is", "event:page", ["/"]]],
1565
- "include" => %{"imports" => true}
1566
- })
1567
-
1568
- assert %{"results" => results, "meta" => meta} = json_response(conn, 200)
1569
-
1570
- assert results == [%{"dimensions" => [], "metrics" => [4, 6, 50.0]}]
1571
-
1572
- assert meta["imports_included"] == true
1573
-
1574
- assert bounce_rate_warning = meta["metric_warnings"]["bounce_rate"]
1575
-
1576
- expected_warning = Plausible.Stats.QueryResult.no_imported_bounce_rate_warning()
1577
- assert bounce_rate_warning["code"] == to_string(expected_warning.code)
1578
- assert bounce_rate_warning["warning"] == expected_warning.warning
1579
- end
1580
-
1581
- test "returns bounce_rate warning when query includes imported and page dimension used", %{
1582
- conn: conn,
1583
- site: site
1584
- } do
1585
- site_import =
1586
- insert(:site_import, site: site, start_date: ~D[2021-02-01], end_date: ~D[2021-02-28])
1587
-
1588
- populate_stats(site, site_import.id, [
1589
- build(:pageview, user_id: 123, pathname: "/", timestamp: ~N[2021-02-01 00:00:00]),
1590
- build(:pageview, user_id: 123, pathname: "/next", timestamp: ~N[2021-02-01 00:10:00]),
1591
- build(:pageview, pathname: "/", timestamp: ~N[2021-02-01 00:00:00]),
1592
- build(:imported_pages, page: "/", date: ~D[2021-02-01]),
1593
- build(:imported_pages, pageviews: 3, page: "/", date: ~D[2021-02-02]),
1594
- build(:imported_visitors, date: ~D[2021-02-01]),
1595
- build(:imported_visitors, pageviews: 3, date: ~D[2021-02-02])
1596
- ])
1597
-
1598
- conn =
1599
- post(conn, "/api/v2/query", %{
1600
- "site_id" => site.domain,
1601
- "metrics" => ["visitors", "pageviews", "bounce_rate"],
1602
- "date_range" => "all",
1603
- "dimensions" => ["event:page"],
1604
- "include" => %{"imports" => true}
1605
- })
1606
-
1607
- assert %{"results" => results, "meta" => meta} = json_response(conn, 200)
1608
-
1609
- assert results == [
1610
- %{"dimensions" => ["/"], "metrics" => [4, 6, 50.0]},
1611
- %{"dimensions" => ["/next"], "metrics" => [1, 1, 0.0]}
1612
- ]
1613
-
1614
- assert meta["imports_included"] == true
1615
-
1616
- assert bounce_rate_warning = meta["metric_warnings"]["bounce_rate"]
1617
-
1618
- expected_warning = Plausible.Stats.QueryResult.no_imported_bounce_rate_warning()
1619
- assert bounce_rate_warning["code"] == to_string(expected_warning.code)
1620
- assert bounce_rate_warning["warning"] == expected_warning.warning
1621
- end
1622
- end
1623
-
1624
- describe "order_by" do
1625
- test "bounce_rate", %{site: site, conn: conn} do
1626
- site_import =
1627
- insert(:site_import, site: site, start_date: ~D[2021-01-01], end_date: ~D[2021-02-28])
1628
-
1629
- populate_stats(site, site_import.id, [
1630
- build(:pageview, user_id: 123, pathname: "/a", timestamp: ~N[2021-01-01 00:00:00]),
1631
- build(:imported_entry_pages,
1632
- entry_page: "/a",
1633
- entrances: 2,
1634
- bounces: 1,
1635
- date: ~D[2021-01-01]
1636
- ),
1637
- build(:imported_entry_pages,
1638
- entry_page: "/b",
1639
- entrances: 4,
1640
- bounces: 1,
1641
- date: ~D[2021-01-01]
1642
- )
1643
- ])
1644
-
1645
- conn =
1646
- post(conn, "/api/v2/query", %{
1647
- "site_id" => site.domain,
1648
- "metrics" => ["bounce_rate"],
1649
- "order_by" => [["bounce_rate", "asc"]],
1650
- "date_range" => "all",
1651
- "dimensions" => ["visit:entry_page"],
1652
- "include" => %{"imports" => true}
1653
- })
1654
-
1655
- response = json_response(conn, 200)
1656
-
1657
- assert [
1658
- %{"dimensions" => ["/b"], "metrics" => [25.0]},
1659
- %{"dimensions" => ["/a"], "metrics" => [67.0]}
1660
- ] = response["results"]
1661
- end
1662
-
1663
- test "visit_duration", %{site: site, conn: conn} do
1664
- site_import =
1665
- insert(:site_import, site: site, start_date: ~D[2021-01-01], end_date: ~D[2021-02-28])
1666
-
1667
- populate_stats(site, site_import.id, [
1668
- build(:pageview, user_id: 123, pathname: "/a", timestamp: ~N[2021-01-01 00:00:00]),
1669
- build(:imported_exit_pages,
1670
- exit_page: "/a",
1671
- exits: 2,
1672
- visit_duration: 90,
1673
- date: ~D[2021-01-01]
1674
- ),
1675
- build(:imported_exit_pages,
1676
- exit_page: "/b",
1677
- exits: 4,
1678
- visit_duration: 400,
1679
- date: ~D[2021-01-01]
1680
- )
1681
- ])
1682
-
1683
- conn =
1684
- post(conn, "/api/v2/query", %{
1685
- "site_id" => site.domain,
1686
- "metrics" => ["visit_duration"],
1687
- "order_by" => [["visit_duration", "desc"]],
1688
- "dimensions" => ["visit:exit_page"],
1689
- "date_range" => "all",
1690
- "include" => %{"imports" => true}
1691
- })
1692
-
1693
- response = json_response(conn, 200)
1694
-
1695
- assert [
1696
- %{"dimensions" => ["/b"], "metrics" => [100.0]},
1697
- %{"dimensions" => ["/a"], "metrics" => [30.0]}
1698
- ] = response["results"]
1699
- end
1700
-
1701
- test "views_per_visit", %{site: site, conn: conn} do
1702
- site_import =
1703
- insert(:site_import, site: site, start_date: ~D[2021-01-01], end_date: ~D[2021-02-28])
1704
-
1705
- populate_stats(site, site_import.id, [
1706
- build(:pageview, user_id: 123, pathname: "/a", timestamp: ~N[2021-01-01 00:00:00]),
1707
- build(:imported_visitors,
1708
- visits: 1,
1709
- pageviews: 3,
1710
- date: ~D[2021-01-01]
1711
- )
1712
- ])
1713
-
1714
- conn =
1715
- post(conn, "/api/v2/query", %{
1716
- "site_id" => site.domain,
1717
- "metrics" => ["views_per_visit"],
1718
- "order_by" => [["views_per_visit", "asc"]],
1719
- "date_range" => "all",
1720
- "include" => %{"imports" => true}
1721
- })
1722
-
1723
- response = json_response(conn, 200)
1724
-
1725
- assert [%{"dimensions" => [], "metrics" => [2.0]}] = response["results"]
1726
- end
1727
- end
1728
-
1729
- describe "page scroll goals" do
1730
- setup :create_site_import
1731
-
1732
- test "rejects imported data with warning when page scroll goal is filtered by", %{
1733
- conn: conn,
1734
- site: site,
1735
- site_import: site_import
1736
- } do
1737
- insert(:goal,
1738
- site: site,
1739
- page_path: "/blog",
1740
- scroll_threshold: 50,
1741
- display_name: "Scroll /blog 50"
1742
- )
1743
-
1744
- insert(:goal, site: site, page_path: "/blog**")
1745
-
1746
- populate_stats(site, site_import.id, [
1747
- build(:pageview,
1748
- user_id: 12,
1749
- pathname: "/blog",
1750
- timestamp: ~N[2021-01-01 00:00:00]
1751
- ),
1752
- build(:engagement,
1753
- user_id: 12,
1754
- pathname: "/blog",
1755
- timestamp: ~N[2021-01-01 00:00:10],
1756
- scroll_depth: 50
1757
- ),
1758
- build(:imported_pages, page: "/blog", date: ~D[2021-01-01])
1759
- ])
1760
-
1761
- conn =
1762
- post(conn, "/api/v2/query", %{
1763
- "site_id" => site.domain,
1764
- "metrics" => ["visitors", "events", "conversion_rate"],
1765
- "date_range" => "all",
1766
- "filters" => [["is", "event:goal", ["Scroll /blog 50"]]],
1767
- "include" => %{"imports" => true}
1768
- })
1769
-
1770
- assert %{
1771
- "results" => results,
1772
- "meta" => %{
1773
- "imports_included" => false,
1774
- "imports_skip_reason" => "unsupported_query",
1775
- "imports_warning" => @unsupported_query_warning
1776
- }
1777
- } = json_response(conn, 200)
1778
-
1779
- assert results == [%{"dimensions" => [], "metrics" => [1, nil, 100.0]}]
1780
- end
1781
-
1782
- test "rejects imported data with warning when page scroll goal is filtered by (IN filter)", %{
1783
- conn: conn,
1784
- site: site,
1785
- site_import: site_import
1786
- } do
1787
- insert(:goal,
1788
- site: site,
1789
- page_path: "/blog",
1790
- scroll_threshold: 50,
1791
- display_name: "Scroll /blog 50"
1792
- )
1793
-
1794
- insert(:goal, site: site, page_path: "/blog**")
1795
-
1796
- populate_stats(site, site_import.id, [
1797
- build(:pageview,
1798
- user_id: 12,
1799
- pathname: "/blog",
1800
- timestamp: ~N[2021-01-01 00:00:00]
1801
- ),
1802
- build(:engagement,
1803
- user_id: 12,
1804
- pathname: "/blog",
1805
- timestamp: ~N[2021-01-01 00:00:10],
1806
- scroll_depth: 50
1807
- ),
1808
- build(:imported_pages, page: "/blog", date: ~D[2021-01-01])
1809
- ])
1810
-
1811
- conn =
1812
- post(conn, "/api/v2/query", %{
1813
- "site_id" => site.domain,
1814
- "metrics" => ["visitors", "events", "conversion_rate"],
1815
- "date_range" => "all",
1816
- "filters" => [["is", "event:goal", ["Scroll /blog 50", "Visit /blog**"]]],
1817
- "include" => %{"imports" => true}
1818
- })
1819
-
1820
- assert %{
1821
- "results" => results,
1822
- "meta" => %{
1823
- "imports_included" => false,
1824
- "imports_skip_reason" => "unsupported_query",
1825
- "imports_warning" => @unsupported_query_warning
1826
- }
1827
- } = json_response(conn, 200)
1828
-
1829
- assert results == [%{"dimensions" => [], "metrics" => [1, nil, 100.0]}]
1830
- end
1831
-
1832
- test "ignores imported data for page scroll goal conversions in goal breakdown", %{
1833
- conn: conn,
1834
- site: site,
1835
- site_import: site_import
1836
- } do
1837
- insert(:goal,
1838
- site: site,
1839
- page_path: "/blog",
1840
- scroll_threshold: 50,
1841
- display_name: "Scroll /blog 50"
1842
- )
1843
-
1844
- insert(:goal, site: site, page_path: "/blog**")
1845
-
1846
- populate_stats(site, site_import.id, [
1847
- build(:pageview,
1848
- user_id: 12,
1849
- pathname: "/blog",
1850
- timestamp: ~N[2021-01-01 00:00:00]
1851
- ),
1852
- build(:engagement,
1853
- user_id: 12,
1854
- pathname: "/blog",
1855
- timestamp: ~N[2021-01-01 00:00:10],
1856
- scroll_depth: 50
1857
- ),
1858
- build(:imported_pages, page: "/blog"),
1859
- build(:imported_visitors)
1860
- ])
1861
-
1862
- conn =
1863
- post(conn, "/api/v2/query", %{
1864
- "site_id" => site.domain,
1865
- "metrics" => ["visitors", "events", "conversion_rate"],
1866
- "date_range" => "all",
1867
- "include" => %{"imports" => true},
1868
- "dimensions" => ["event:goal"]
1869
- })
1870
-
1871
- assert %{
1872
- "results" => results,
1873
- "meta" => %{"imports_included" => true}
1874
- } = json_response(conn, 200)
1875
-
1876
- assert results == [
1877
- %{"dimensions" => ["Visit /blog**"], "metrics" => [2, 2, 100.0]},
1878
- %{"dimensions" => ["Scroll /blog 50"], "metrics" => [1, nil, 50.0]}
1879
- ]
1880
- end
1881
- end
1882
- end