Class: Ga4Rails::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ga4-rails/client.rb

Overview

A client for interacting with the Google Analytics API.

Examples:

client = Ga4Rails::Client.new(access_token: access_token)
client.admin.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token:) ⇒ Client

Initializes a new client.

Parameters:

  • access_token (OAuth2::AccessToken)

    - the access token obtained from the Google OAuth2 flow



19
20
21
# File 'lib/ga4-rails/client.rb', line 19

def initialize(access_token:)
  @access_token = access_token
end

Instance Attribute Details

#access_tokenOAuth2::AccessToken (readonly)

- the access token obtained from the Google OAuth2 flow

Returns:

  • (OAuth2::AccessToken)

    the current value of access_token



12
13
14
# File 'lib/ga4-rails/client.rb', line 12

def access_token
  @access_token
end

Instance Method Details

#adminGa4Rails::AnalyticsAdmin

Returns an instance of the AnalyticsAdmin class.

Returns:



27
28
29
# File 'lib/ga4-rails/client.rb', line 27

def admin
  analytics_admin_service
end

#available_dimensions(property:) ⇒ Array

Returns an array of available dimensions for a property.

Example: ‘properties/341194148’

Parameters:

  • property (String)

    - the property ID

Returns:

  • (Array)

    - an array of available dimensions for a property



58
59
60
61
62
63
# File 'lib/ga4-rails/client.rb', line 58

def available_dimensions(property:)
  Ga4Rails::GetPropertyMetadata.call(
    data_service: analytics_data_service,
    property_id: property
  ).dimensions
end

#available_metrics(property:) ⇒ Array

Returns an array of available metrics for a property.

Example: ‘properties/341194148’

Parameters:

  • property (String)

    - the property ID

Returns:

  • (Array)

    - an array of available metrics for a property



43
44
45
46
47
48
# File 'lib/ga4-rails/client.rb', line 43

def available_metrics(property:)
  Ga4Rails::GetPropertyMetadata.call(
    data_service: analytics_data_service,
    property_id: property
  ).metrics
end

#dataObject



31
32
33
# File 'lib/ga4-rails/client.rb', line 31

def data
  analytics_data_service
end

#profilesObject

TODO



93
94
# File 'lib/ga4-rails/client.rb', line 93

def profiles
end

#run_property_report(property_id:, body:) ⇒ Object

Parameters:

  • property_id (String)

    - the property ID. Example: ‘properties/341194148’

  • body (Hash)

    - the report request body. Example:

     body = {
      "date_ranges": [
        {
          "start_date": "30daysAgo",
          "end_date": "today"
        }
      ],
      "metrics": [
        { name: "totalUsers" }
      ]
    }
    


84
85
86
87
88
89
90
# File 'lib/ga4-rails/client.rb', line 84

def run_property_report(property_id:, body:)
  Ga4Rails::RunPropertyReport.call(
    data_service: analytics_data_service,
    property_id: property_id,
    body: body
  )
end