Leon4gr45 commited on
Commit
ff73aca
·
verified ·
1 Parent(s): 9f88e01

Delete test/plausible/imported/google_analytics4_test.exs with huggingface_hub

Browse files
test/plausible/imported/google_analytics4_test.exs DELETED
@@ -1,799 +0,0 @@
1
- defmodule Plausible.Imported.GoogleAnalytics4Test do
2
- use PlausibleWeb.ConnCase, async: true
3
- use Oban.Testing, repo: Plausible.Repo
4
-
5
- import Mox
6
- import Ecto.Query, only: [from: 2]
7
- import ExUnit.CaptureLog
8
-
9
- alias Plausible.Repo
10
- alias Plausible.Imported.GoogleAnalytics4
11
-
12
- @refresh_token_body Jason.decode!(File.read!("fixture/ga_refresh_token.json"))
13
-
14
- @full_report_mock [
15
- "fixture/ga4_report_imported_visitors.json",
16
- "fixture/ga4_report_imported_sources.json",
17
- "fixture/ga4_report_imported_pages.json",
18
- "fixture/ga4_report_imported_entry_pages.json",
19
- "fixture/ga4_report_imported_custom_events.json",
20
- "fixture/ga4_report_imported_locations.json",
21
- "fixture/ga4_report_imported_devices.json",
22
- "fixture/ga4_report_imported_browsers.json",
23
- "fixture/ga4_report_imported_operating_systems.json"
24
- ]
25
- |> Enum.map(&File.read!/1)
26
- |> Enum.map(&Jason.decode!/1)
27
-
28
- on_ce do
29
- @moduletag :capture_log
30
- end
31
-
32
- setup :verify_on_exit!
33
-
34
- describe "parse_args/1 and import_data/2" do
35
- setup [:create_user, :create_site]
36
-
37
- test "imports data returned from GA4 Data API", %{conn: conn, user: user, site: site} do
38
- past = DateTime.add(DateTime.utc_now(), -3600, :second)
39
-
40
- {:ok, job} =
41
- Plausible.Imported.GoogleAnalytics4.new_import(
42
- site,
43
- user,
44
- label: "properties/123456",
45
- property: "properties/123456",
46
- start_date: ~D[2024-01-01],
47
- end_date: ~D[2024-01-31],
48
- access_token: "redacted_access_token",
49
- refresh_token: "redacted_refresh_token",
50
- token_expires_at: DateTime.to_iso8601(past)
51
- )
52
-
53
- site_import = Plausible.Imported.get_import(site, job.args.import_id)
54
-
55
- assert site_import.label == "properties/123456"
56
-
57
- opts = job |> Repo.reload!() |> Map.get(:args) |> GoogleAnalytics4.parse_args()
58
-
59
- opts = Keyword.put(opts, :flush_interval_ms, 10)
60
-
61
- expect(Plausible.HTTPClient.Mock, :post, fn "https://www.googleapis.com/oauth2/v4/token",
62
- headers,
63
- body ->
64
- assert [{"content-type", "application/x-www-form-urlencoded"}] == headers
65
-
66
- assert %{
67
- grant_type: :refresh_token,
68
- redirect_uri: "http://localhost:8000/auth/google/callback",
69
- refresh_token: "redacted_refresh_token"
70
- } = body
71
-
72
- {:ok, %Finch.Response{status: 200, body: @refresh_token_body}}
73
- end)
74
-
75
- for report <- @full_report_mock do
76
- expect(Plausible.HTTPClient.Mock, :post, fn _url, headers, _body, _opts ->
77
- assert [{"Authorization", "Bearer 1/fFAGRNJru1FTz70BzhT3Zg"}] == headers
78
- {:ok, %Finch.Response{status: 200, body: report}}
79
- end)
80
- end
81
-
82
- Enum.each(Plausible.Imported.tables(), fn table ->
83
- query = from(imported in table, where: imported.site_id == ^site.id)
84
- assert await_clickhouse_count(query, 0)
85
- end)
86
-
87
- assert :ok = GoogleAnalytics4.import_data(site_import, opts)
88
-
89
- Enum.each(Plausible.Imported.tables(), fn table ->
90
- count =
91
- case table do
92
- "imported_sources" -> 210
93
- "imported_visitors" -> 31
94
- "imported_pages" -> 3340
95
- "imported_entry_pages" -> 2934
96
- "imported_exit_pages" -> 0
97
- "imported_custom_events" -> 56
98
- "imported_locations" -> 2291
99
- "imported_devices" -> 93
100
- "imported_browsers" -> 233
101
- "imported_operating_systems" -> 1068
102
- end
103
-
104
- query = from(imported in table, where: imported.site_id == ^site.id)
105
- assert await_clickhouse_count(query, count)
106
- end)
107
-
108
- # NOTE: Consider using GoogleAnalytics.run_import instead of import_data
109
- # to avoid having to set SiteImport to completed manually
110
- site_import
111
- |> Plausible.Imported.SiteImport.complete_changeset()
112
- |> Repo.update!()
113
-
114
- # Assert the actual data via Stats API requests
115
- common_params = %{
116
- "site_id" => site.domain,
117
- "period" => "custom",
118
- "date" => "2024-01-01,2024-01-31",
119
- "with_imported" => "true"
120
- }
121
-
122
- breakdown_params =
123
- common_params
124
- |> Map.put("metrics", "visitors,visits,visit_duration,bounce_rate")
125
- |> Map.put("limit", 1000)
126
-
127
- %{key: api_key} = insert(:api_key, user: user)
128
-
129
- conn = put_req_header(conn, "authorization", "Bearer #{api_key}")
130
-
131
- insert(:goal, event_name: "Outbound Link: Click", site: site)
132
- insert(:goal, event_name: "view_search_results", site: site)
133
- insert(:goal, event_name: "scroll", site: site)
134
-
135
- # Timeseries
136
- assert_timeseries(conn, common_params)
137
-
138
- # Breakdown (event:*)
139
- assert_pages(conn, common_params)
140
- assert_custom_events(conn, common_params)
141
- assert_outbound_link_urls(conn, common_params)
142
-
143
- # Breakdown (visit:*)
144
- assert_sources(conn, breakdown_params)
145
- assert_utm_mediums(conn, breakdown_params)
146
- assert_entry_pages(conn, breakdown_params)
147
- assert_cities(conn, breakdown_params)
148
- assert_devices(conn, breakdown_params)
149
- assert_browsers(conn, breakdown_params)
150
- assert_os(conn, breakdown_params)
151
- assert_os_versions(conn, breakdown_params)
152
- end
153
-
154
- test "handles empty response payload gracefully", %{user: user, site: site} do
155
- future = DateTime.add(DateTime.utc_now(), 3600, :second)
156
-
157
- empty_custom_events = %{
158
- "reports" => [
159
- %{
160
- "dimensionHeaders" => [
161
- %{"name" => "date"},
162
- %{"name" => "eventName"},
163
- %{"name" => "linkUrl"}
164
- ],
165
- "kind" => "analyticsData#runReport",
166
- "metadata" => %{"currencyCode" => "USD", "timeZone" => "Etc/GMT"},
167
- "metricHeaders" => [
168
- %{"name" => "totalUsers", "type" => "TYPE_INTEGER"},
169
- %{"name" => "eventCount", "type" => "TYPE_INTEGER"}
170
- ]
171
- }
172
- ]
173
- }
174
-
175
- {:ok, job} =
176
- Plausible.Imported.GoogleAnalytics4.new_import(
177
- site,
178
- user,
179
- label: "properties/123456",
180
- property: "properties/123456",
181
- start_date: ~D[2024-01-01],
182
- end_date: ~D[2024-01-31],
183
- access_token: "redacted_access_token",
184
- refresh_token: "redacted_refresh_token",
185
- token_expires_at: DateTime.to_iso8601(future)
186
- )
187
-
188
- site_import = Plausible.Imported.get_import(site, job.args.import_id)
189
-
190
- opts = job |> Repo.reload!() |> Map.get(:args) |> GoogleAnalytics4.parse_args()
191
-
192
- opts = Keyword.put(opts, :flush_interval_ms, 10)
193
-
194
- for report <- Enum.take(@full_report_mock, 4) do
195
- expect(Plausible.HTTPClient.Mock, :post, fn _url, headers, _body, _opts ->
196
- assert [{"Authorization", "Bearer redacted_access_token"}] == headers
197
- {:ok, %Finch.Response{status: 200, body: report}}
198
- end)
199
- end
200
-
201
- expect(Plausible.HTTPClient.Mock, :post, fn _url, headers, _body, _opts ->
202
- assert [{"Authorization", "Bearer redacted_access_token"}] == headers
203
- {:ok, %Finch.Response{status: 200, body: empty_custom_events}}
204
- end)
205
-
206
- for report <- Enum.drop(@full_report_mock, 5) do
207
- expect(Plausible.HTTPClient.Mock, :post, fn _url, headers, _body, _opts ->
208
- assert [{"Authorization", "Bearer redacted_access_token"}] == headers
209
- {:ok, %Finch.Response{status: 200, body: report}}
210
- end)
211
- end
212
-
213
- assert :ok = GoogleAnalytics4.import_data(site_import, opts)
214
-
215
- Enum.each(Plausible.Imported.tables(), fn table ->
216
- count =
217
- case table do
218
- "imported_sources" -> 210
219
- "imported_visitors" -> 31
220
- "imported_pages" -> 3340
221
- "imported_entry_pages" -> 2934
222
- "imported_exit_pages" -> 0
223
- "imported_locations" -> 2291
224
- "imported_devices" -> 93
225
- "imported_browsers" -> 233
226
- "imported_operating_systems" -> 1068
227
- "imported_custom_events" -> 0
228
- end
229
-
230
- query = from(imported in table, where: imported.site_id == ^site.id)
231
- assert await_clickhouse_count(query, count)
232
- end)
233
- end
234
-
235
- test "handles rate limiting gracefully", %{user: user, site: site} do
236
- future = DateTime.add(DateTime.utc_now(), 3600, :second)
237
-
238
- {:ok, job} =
239
- Plausible.Imported.GoogleAnalytics4.new_import(
240
- site,
241
- user,
242
- label: "properties/123456",
243
- property: "properties/123456",
244
- start_date: ~D[2024-01-01],
245
- end_date: ~D[2024-01-31],
246
- access_token: "redacted_access_token",
247
- refresh_token: "redacted_refresh_token",
248
- token_expires_at: DateTime.to_iso8601(future)
249
- )
250
-
251
- site_import = Plausible.Imported.get_import(site, job.args.import_id)
252
-
253
- opts = job |> Repo.reload!() |> Map.get(:args) |> GoogleAnalytics4.parse_args()
254
-
255
- opts = Keyword.put(opts, :flush_interval_ms, 10)
256
-
257
- expect(Plausible.HTTPClient.Mock, :post, fn _url, headers, _body, _opts ->
258
- assert [{"Authorization", "Bearer redacted_access_token"}] == headers
259
- {:ok, %Finch.Response{status: 200, body: List.first(@full_report_mock)}}
260
- end)
261
-
262
- expect(Plausible.HTTPClient.Mock, :post, fn _url, headers, _body, _opts ->
263
- assert [{"Authorization", "Bearer redacted_access_token"}] == headers
264
-
265
- {:error,
266
- Plausible.HTTPClient.Non200Error.new(%Finch.Response{
267
- status: 429,
268
- body: "Rate limit exceeded"
269
- })}
270
- end)
271
-
272
- assert {:error, :rate_limit_exceeded, skip_purge?: true, skip_mark_failed?: true} =
273
- GoogleAnalytics4.import_data(site_import, opts)
274
-
275
- in_65_minutes = DateTime.add(DateTime.utc_now(), 3900, :second)
276
-
277
- assert_enqueued(
278
- worker: Plausible.Workers.ImportAnalytics,
279
- args: %{resume_from_import_id: site_import.id},
280
- scheduled_at: {in_65_minutes, delta: 10}
281
- )
282
-
283
- [%{args: resume_args}, _] = all_enqueued()
284
-
285
- resume_opts = GoogleAnalytics4.parse_args(resume_args)
286
- resume_opts = Keyword.put(resume_opts, :flush_interval_ms, 10)
287
- site_import = Repo.reload!(site_import)
288
-
289
- Enum.each(Plausible.Imported.tables(), fn table ->
290
- count =
291
- case table do
292
- "imported_visitors" -> 31
293
- "imported_sources" -> 0
294
- "imported_pages" -> 0
295
- "imported_entry_pages" -> 0
296
- "imported_exit_pages" -> 0
297
- "imported_locations" -> 0
298
- "imported_devices" -> 0
299
- "imported_browsers" -> 0
300
- "imported_operating_systems" -> 0
301
- "imported_custom_events" -> 0
302
- end
303
-
304
- query = from(imported in table, where: imported.site_id == ^site.id)
305
- assert await_clickhouse_count(query, count)
306
- end)
307
-
308
- for report <- tl(@full_report_mock) do
309
- expect(Plausible.HTTPClient.Mock, :post, fn _url, headers, _body, _opts ->
310
- assert [{"Authorization", "Bearer redacted_access_token"}] == headers
311
- {:ok, %Finch.Response{status: 200, body: report}}
312
- end)
313
- end
314
-
315
- assert :ok = GoogleAnalytics4.import_data(site_import, resume_opts)
316
-
317
- Enum.each(Plausible.Imported.tables(), fn table ->
318
- count =
319
- case table do
320
- "imported_sources" -> 210
321
- "imported_visitors" -> 31
322
- "imported_pages" -> 3340
323
- "imported_entry_pages" -> 2934
324
- "imported_exit_pages" -> 0
325
- "imported_locations" -> 2291
326
- "imported_devices" -> 93
327
- "imported_browsers" -> 233
328
- "imported_operating_systems" -> 1068
329
- "imported_custom_events" -> 56
330
- end
331
-
332
- query = from(imported in table, where: imported.site_id == ^site.id)
333
- assert await_clickhouse_count(query, count)
334
- end)
335
- end
336
-
337
- @recoverable_errors [
338
- {
339
- Macro.escape(
340
- Plausible.HTTPClient.Non200Error.new(%Finch.Response{
341
- status: 500,
342
- body: "Internal server error"
343
- })
344
- ),
345
- :server_failed,
346
- ~s|Request failed for imported_sources with code 500: "Internal server error"|
347
- },
348
- {
349
- :timeout,
350
- :socket_failed,
351
- ~s|Request failed for imported_sources: :timeout|
352
- }
353
- ]
354
-
355
- for {error_mock, error_returned, log_message} <- @recoverable_errors do
356
- test "handles #{error_returned} gracefully", %{user: user, site: site} do
357
- future = DateTime.add(DateTime.utc_now(), 3600, :second)
358
-
359
- {:ok, job} =
360
- Plausible.Imported.GoogleAnalytics4.new_import(
361
- site,
362
- user,
363
- label: "properties/123456",
364
- property: "properties/123456",
365
- start_date: ~D[2024-01-01],
366
- end_date: ~D[2024-01-31],
367
- access_token: "redacted_access_token",
368
- refresh_token: "redacted_refresh_token",
369
- token_expires_at: DateTime.to_iso8601(future)
370
- )
371
-
372
- site_import = Plausible.Imported.get_import(site, job.args.import_id)
373
-
374
- opts = job |> Repo.reload!() |> Map.get(:args) |> GoogleAnalytics4.parse_args()
375
-
376
- opts =
377
- opts
378
- |> Keyword.put(:flush_interval_ms, 10)
379
- |> Keyword.put(:fetch_opts, max_attempts: 2, sleep_time: 50)
380
-
381
- expect(Plausible.HTTPClient.Mock, :post, fn _url, headers, _body, _opts ->
382
- assert [{"Authorization", "Bearer redacted_access_token"}] == headers
383
- {:ok, %Finch.Response{status: 200, body: List.first(@full_report_mock)}}
384
- end)
385
-
386
- expect(Plausible.HTTPClient.Mock, :post, 2, fn _url, headers, _body, _opts ->
387
- assert [{"Authorization", "Bearer redacted_access_token"}] == headers
388
-
389
- {:error, unquote(error_mock)}
390
- end)
391
-
392
- assert capture_log(fn ->
393
- assert {:error, unquote(error_returned),
394
- skip_purge?: true, skip_mark_failed?: true} =
395
- GoogleAnalytics4.import_data(site_import, opts)
396
- end) =~ unquote(log_message)
397
-
398
- in_65_minutes = DateTime.add(DateTime.utc_now(), 3900, :second)
399
-
400
- assert_enqueued(
401
- worker: Plausible.Workers.ImportAnalytics,
402
- args: %{resume_from_import_id: site_import.id},
403
- scheduled_at: {in_65_minutes, delta: 10}
404
- )
405
-
406
- [%{args: resume_args}, _] = all_enqueued()
407
-
408
- resume_opts = GoogleAnalytics4.parse_args(resume_args)
409
- resume_opts = Keyword.put(resume_opts, :flush_interval_ms, 10)
410
- site_import = Repo.reload!(site_import)
411
-
412
- Enum.each(Plausible.Imported.tables(), fn table ->
413
- count =
414
- case table do
415
- "imported_visitors" -> 31
416
- "imported_sources" -> 0
417
- "imported_pages" -> 0
418
- "imported_entry_pages" -> 0
419
- "imported_exit_pages" -> 0
420
- "imported_locations" -> 0
421
- "imported_devices" -> 0
422
- "imported_browsers" -> 0
423
- "imported_operating_systems" -> 0
424
- "imported_custom_events" -> 0
425
- end
426
-
427
- query = from(imported in table, where: imported.site_id == ^site.id)
428
- assert await_clickhouse_count(query, count)
429
- end)
430
-
431
- for report <- tl(@full_report_mock) do
432
- expect(Plausible.HTTPClient.Mock, :post, fn _url, headers, _body, _opts ->
433
- assert [{"Authorization", "Bearer redacted_access_token"}] == headers
434
- {:ok, %Finch.Response{status: 200, body: report}}
435
- end)
436
- end
437
-
438
- assert :ok = GoogleAnalytics4.import_data(site_import, resume_opts)
439
-
440
- Enum.each(Plausible.Imported.tables(), fn table ->
441
- count =
442
- case table do
443
- "imported_sources" -> 210
444
- "imported_visitors" -> 31
445
- "imported_pages" -> 3340
446
- "imported_entry_pages" -> 2934
447
- "imported_exit_pages" -> 0
448
- "imported_locations" -> 2291
449
- "imported_devices" -> 93
450
- "imported_browsers" -> 233
451
- "imported_operating_systems" -> 1068
452
- "imported_custom_events" -> 56
453
- end
454
-
455
- query = from(imported in table, where: imported.site_id == ^site.id)
456
- assert await_clickhouse_count(query, count)
457
- end)
458
- end
459
- end
460
- end
461
-
462
- defp assert_custom_events(conn, params) do
463
- params =
464
- params
465
- |> Map.put("metrics", "visitors,events,conversion_rate")
466
- |> Map.put("property", "event:goal")
467
-
468
- %{"results" => results} =
469
- get(conn, "/api/v1/stats/breakdown", params) |> json_response(200)
470
-
471
- assert results == [
472
- %{
473
- "goal" => "scroll",
474
- "visitors" => 1513,
475
- "events" => 2130,
476
- "conversion_rate" => 24.69
477
- },
478
- %{
479
- "goal" => "Outbound Link: Click",
480
- "visitors" => 17,
481
- "events" => 17,
482
- "conversion_rate" => 0.28
483
- },
484
- %{
485
- "goal" => "view_search_results",
486
- "visitors" => 11,
487
- "events" => 30,
488
- "conversion_rate" => 0.18
489
- }
490
- ]
491
- end
492
-
493
- defp assert_outbound_link_urls(conn, params) do
494
- params =
495
- Map.merge(params, %{
496
- "metrics" => "visitors,events,conversion_rate",
497
- "property" => "event:props:url",
498
- "filters" => "event:goal==Outbound Link: Click"
499
- })
500
-
501
- %{"results" => results} =
502
- get(conn, "/api/v1/stats/breakdown", params) |> json_response(200)
503
-
504
- assert length(results) == 10
505
-
506
- assert List.first(results) ==
507
- %{
508
- "url" => "https://www.facebook.com/kuhinjskeprice",
509
- "visitors" => 6,
510
- "conversion_rate" => 0.1,
511
- "events" => 6
512
- }
513
-
514
- results
515
- |> Enum.find(
516
- &(&1["url"] ==
517
- "http://www.jamieoliver.com/recipes/pasta-recipes/spinach-ricotta-cannelloni/")
518
- )
519
- |> then(fn page ->
520
- assert %{"visitors" => 1, "conversion_rate" => 0.02, "events" => 1} = page
521
- end)
522
- end
523
-
524
- defp assert_timeseries(conn, params) do
525
- params =
526
- Map.put(
527
- params,
528
- "metrics",
529
- "visitors,visits,pageviews,views_per_visit,visit_duration,bounce_rate"
530
- )
531
-
532
- %{"results" => results} =
533
- get(conn, "/api/v1/stats/timeseries", params) |> json_response(200)
534
-
535
- assert length(results) == 31
536
-
537
- assert List.first(results) == %{
538
- "bounce_rate" => 36.0,
539
- "date" => "2024-01-01",
540
- "pageviews" => 224,
541
- "views_per_visit" => 1.14,
542
- "visit_duration" => 41.0,
543
- "visitors" => 191,
544
- "visits" => 197
545
- }
546
-
547
- assert List.last(results) == %{
548
- "bounce_rate" => 38.0,
549
- "date" => "2024-01-31",
550
- "pageviews" => 195,
551
- "views_per_visit" => 1.34,
552
- "visit_duration" => 33.0,
553
- "visitors" => 141,
554
- "visits" => 146
555
- }
556
- end
557
-
558
- defp assert_sources(conn, params) do
559
- params = Map.put(params, "property", "visit:source")
560
-
561
- %{"results" => results} =
562
- get(conn, "/api/v1/stats/breakdown", params) |> json_response(200)
563
-
564
- assert length(results) == 26
565
-
566
- assert List.first(results) == %{
567
- "bounce_rate" => 35.0,
568
- "visit_duration" => 40.0,
569
- "visitors" => 4671,
570
- "visits" => 4917,
571
- "source" => "Google"
572
- }
573
-
574
- assert List.last(results) == %{
575
- "bounce_rate" => 0.0,
576
- "source" => "yahoo",
577
- "visit_duration" => 41.0,
578
- "visitors" => 1,
579
- "visits" => 1
580
- }
581
- end
582
-
583
- defp assert_utm_mediums(conn, params) do
584
- params = Map.put(params, "property", "visit:utm_medium")
585
-
586
- %{"results" => results} =
587
- get(conn, "/api/v1/stats/breakdown", params) |> json_response(200)
588
-
589
- assert [
590
- %{
591
- "bounce_rate" => 35.0,
592
- "utm_medium" => "organic",
593
- "visit_duration" => 40.0,
594
- "visitors" => 4787,
595
- "visits" => 5042
596
- },
597
- %{
598
- "bounce_rate" => 58.0,
599
- "utm_medium" => "referral",
600
- "visit_duration" => 27.0,
601
- "visitors" => 294,
602
- "visits" => 298
603
- }
604
- ] = results
605
- end
606
-
607
- defp assert_entry_pages(conn, params) do
608
- params = Map.put(params, "property", "visit:entry_page")
609
-
610
- %{"results" => results} =
611
- get(conn, "/api/v1/stats/breakdown", params) |> json_response(200)
612
-
613
- assert length(results) == 629
614
-
615
- assert List.first(results) == %{
616
- "bounce_rate" => 35.0,
617
- "visit_duration" => 43.0,
618
- "visitors" => 675,
619
- "visits" => 712,
620
- "entry_page" => "/brza-kukuruza"
621
- }
622
-
623
- assert List.last(results) == %{
624
- "bounce_rate" => 0.0,
625
- "entry_page" => "/znamenitosti-rima-koje-treba-vidjeti",
626
- "visit_duration" => 40.0,
627
- "visitors" => 1,
628
- "visits" => 1
629
- }
630
- end
631
-
632
- defp assert_cities(conn, params) do
633
- params = Map.put(params, "property", "visit:city")
634
-
635
- %{"results" => results} =
636
- get(conn, "/api/v1/stats/breakdown", params) |> json_response(200)
637
-
638
- assert length(results) == 494
639
-
640
- assert List.first(results) == %{
641
- "bounce_rate" => 35.0,
642
- "city" => 792_680,
643
- "visit_duration" => 39.0,
644
- "visitors" => 1233,
645
- "visits" => 1273
646
- }
647
-
648
- assert List.last(results) == %{
649
- "bounce_rate" => 0.0,
650
- "city" => 11_951_298,
651
- "visit_duration" => 271.0,
652
- "visitors" => 1,
653
- "visits" => 1
654
- }
655
- end
656
-
657
- defp assert_devices(conn, params) do
658
- params = Map.put(params, "property", "visit:device")
659
-
660
- %{"results" => results} =
661
- get(conn, "/api/v1/stats/breakdown", params) |> json_response(200)
662
-
663
- assert length(results) == 3
664
-
665
- assert List.first(results) == %{
666
- "bounce_rate" => 38.0,
667
- "visit_duration" => 37.0,
668
- "visitors" => 5277,
669
- "visits" => 5532,
670
- "device" => "Mobile"
671
- }
672
-
673
- assert List.last(results) == %{
674
- "bounce_rate" => 37.0,
675
- "visit_duration" => 60.0,
676
- "visitors" => 97,
677
- "visits" => 100,
678
- "device" => "Tablet"
679
- }
680
- end
681
-
682
- defp assert_browsers(conn, params) do
683
- params = Map.put(params, "property", "visit:browser")
684
-
685
- %{"results" => results} =
686
- get(conn, "/api/v1/stats/breakdown", params) |> json_response(200)
687
-
688
- assert length(results) == 11
689
-
690
- assert List.first(results) == %{
691
- "bounce_rate" => 33.0,
692
- "visit_duration" => 50.0,
693
- "visitors" => 4625,
694
- "visits" => 4655,
695
- "browser" => "Chrome"
696
- }
697
-
698
- assert List.last(results) == %{
699
- "bounce_rate" => 0.0,
700
- "visit_duration" => 0.0,
701
- "visitors" => 1,
702
- "visits" => 1,
703
- "browser" => "Opera Mini"
704
- }
705
- end
706
-
707
- defp assert_os(conn, params) do
708
- params = Map.put(params, "property", "visit:os")
709
-
710
- %{"results" => results} =
711
- get(conn, "/api/v1/stats/breakdown", params) |> json_response(200)
712
-
713
- assert length(results) == 7
714
-
715
- assert List.first(results) == %{
716
- "bounce_rate" => 34.0,
717
- "visit_duration" => 41.0,
718
- "visitors" => 4319,
719
- "visits" => 4495,
720
- "os" => "Android"
721
- }
722
-
723
- assert List.last(results) == %{
724
- "bounce_rate" => 0.0,
725
- "visit_duration" => 0.0,
726
- "visitors" => 1,
727
- "visits" => 1,
728
- "os" => "(not set)"
729
- }
730
- end
731
-
732
- defp assert_os_versions(conn, params) do
733
- params = Map.put(params, "property", "visit:os_version")
734
-
735
- %{"results" => results} =
736
- get(conn, "/api/v1/stats/breakdown", params) |> json_response(200)
737
-
738
- assert length(results) == 107
739
-
740
- assert List.first(results) == %{
741
- "bounce_rate" => 32.0,
742
- "os" => "Android",
743
- "os_version" => "13.0.0",
744
- "visit_duration" => 42.0,
745
- "visitors" => 1247,
746
- "visits" => 1295
747
- }
748
-
749
- assert List.last(results) == %{
750
- "bounce_rate" => 0.0,
751
- "os" => "Chrome OS",
752
- "os_version" => "x86_64 15662.76.0",
753
- "visit_duration" => 16.0,
754
- "visitors" => 1,
755
- "visits" => 1
756
- }
757
- end
758
-
759
- defp assert_pages(conn, params) do
760
- metrics = "visitors,visits,time_on_page,visit_duration,bounce_rate"
761
-
762
- params =
763
- params
764
- |> Map.put("metrics", metrics)
765
- |> Map.put("limit", 1000)
766
- |> Map.put("property", "event:page")
767
-
768
- %{"results" => results} =
769
- get(conn, "/api/v1/stats/breakdown", params) |> json_response(200)
770
-
771
- assert length(results) == 730
772
-
773
- # The `event:page` breakdown is currently using the `entry_page`
774
- # property to allow querying session metrics.
775
- #
776
- # We assert on the 3rd element of the results, because that page
777
- # was also an entry page somewhere along the queried period. So
778
- # it will allow us to assert on the session metrics as well.
779
- assert Enum.at(results, 2) == %{
780
- "page" => "/",
781
- "time_on_page" => 462,
782
- "visitors" => 371,
783
- "visits" => 212,
784
- "bounce_rate" => 54.0,
785
- "visit_duration" => 45.0
786
- }
787
-
788
- # This page was never an entry_page in the imported data, and
789
- # therefore the session metrics are returned as `nil`.
790
- assert List.last(results) == %{
791
- "bounce_rate" => 0.0,
792
- "page" => "/znamenitosti-rima-koje-treba-vidjeti/",
793
- "time_on_page" => 40,
794
- "visit_duration" => 0.0,
795
- "visitors" => 1,
796
- "visits" => 1
797
- }
798
- end
799
- end