File size: 2,713 Bytes
c9c49ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
defmodule Plausible.Google.GA4.APITest do
  use Plausible.DataCase, async: true

  import Mox

  alias Plausible.Google.GA4

  setup :verify_on_exit!

  describe "list_properties/1" do
    test "returns list of properties grouped by accounts" do
      result = Jason.decode!(File.read!("fixture/ga4_list_properties.json"))

      expect(Plausible.HTTPClient.Mock, :get, fn _url, _opts ->
        {:ok, %Finch.Response{status: 200, body: result}}
      end)

      assert {:ok, accounts} = GA4.API.list_properties("some_access_token")

      assert [
               {"account.one (accounts/28425178)",
                [{"account.one - GA4 (properties/428685906)", "properties/428685906"}]},
               {"Demo Account (accounts/54516992)",
                [
                  {"GA4 - Flood-It! (properties/153293282)", "properties/153293282"},
                  {"GA4 - Google Merch Shop (properties/213025502)", "properties/213025502"}
                ]}
             ] = accounts
    end

    test "handles empty response properly" do
      expect(Plausible.HTTPClient.Mock, :get, fn _url, _opts ->
        {:ok, %Finch.Response{status: 200, body: %{}}}
      end)

      assert {:ok, []} = GA4.API.list_properties("some_access_token")
    end
  end

  describe "get_property/2" do
    test "returns tuple consisting of display name and value of a property" do
      result = Jason.decode!(File.read!("fixture/ga4_get_property.json"))

      expect(Plausible.HTTPClient.Mock, :get, fn _url, _opts ->
        {:ok, %Finch.Response{status: 200, body: result}}
      end)

      assert {:ok,
              %{name: "account.one - GA4 (properties/428685444)", id: "properties/428685444"}} =
               GA4.API.get_property("some_access_token", "properties/428685444")
    end
  end

  describe "get_analytics_start_date/2" do
    test "returns stats start date for a given property" do
      result = Jason.decode!(File.read!("fixture/ga4_start_date.json"))

      expect(Plausible.HTTPClient.Mock, :post, fn _url, _headers, _body ->
        {:ok, %Finch.Response{status: 200, body: result}}
      end)

      assert {:ok, ~D[2024-02-22]} =
               GA4.API.get_analytics_start_date("some_access_token", "properties/153293282")
    end
  end

  describe "get_analytics_end_date/2" do
    test "returns stats end date for a given property" do
      result = Jason.decode!(File.read!("fixture/ga4_end_date.json"))

      expect(Plausible.HTTPClient.Mock, :post, fn _url, _headers, _body ->
        {:ok, %Finch.Response{status: 200, body: result}}
      end)

      assert {:ok, ~D[2024-03-02]} =
               GA4.API.get_analytics_end_date("some_access_token", "properties/153293282")
    end
  end
end