Class: Assistant::Ga4ToolBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/services/assistant/ga4_tool_builder.rb

Constant Summary collapse

TOOL_DEFINITIONS =
[
  { tool_name: 'ga4_top_pages',
    description: '[Google Analytics] Get top pages by page views for a date range. ' \
                 'Returns page path, page views, sessions, users, bounce rate, and engagement metrics.',
    params: {
      type: 'object',
      properties: {
        start_date: { type: 'string', description: 'Start date (YYYY-MM-DD format, or relative like "30daysAgo")' },
        end_date: { type: 'string', description: 'End date (YYYY-MM-DD format, or "yesterday")' },
        limit: { type: 'integer', description: 'Max pages to return (default: 50, max: 500)' }
      },
      required: %w[start_date end_date]
    },
    execute: ->(start_date:, end_date:, limit: 50, **_) {
      client = Seo::Ga4ApiClient.new
      results = client.top_pages(start_date: start_date, end_date: end_date, limit: [limit.to_i, 500].min)
      Assistant::ChatToolBuilder.truncate_result(results.to_json)
    } },
  { tool_name: 'ga4_page_metrics',
    description: '[Google Analytics] Get detailed engagement metrics for a specific page path. ' \
                 'Returns page views, sessions, users, bounce rate, engagement rate, and avg session duration.',
    params: {
      type: 'object',
      properties: {
        page_path: { type: 'string', description: 'Page path (e.g., "/posts/my-article" or "/en-US/floor-heating")' },
        start_date: { type: 'string', description: 'Start date (YYYY-MM-DD format)' },
        end_date: { type: 'string', description: 'End date (YYYY-MM-DD format)' }
      },
      required: %w[page_path start_date end_date]
    },
    execute: ->(page_path:, start_date:, end_date:, **_) {
      client = Seo::Ga4ApiClient.new
      result = client.page_metrics(page_path: page_path, start_date: start_date, end_date: end_date)
      (result || { message: 'No data found for this page path and date range' }).to_json
    } },
  { tool_name: 'ga4_traffic_sources',
    description: '[Google Analytics] Get traffic source breakdown for a specific page. ' \
                 'Shows which channels (organic, paid, social, direct, etc.) drive traffic to the page.',
    params: {
      type: 'object',
      properties: {
        page_path: { type: 'string', description: 'Page path (e.g., "/posts/my-article")' },
        start_date: { type: 'string', description: 'Start date (YYYY-MM-DD format)' },
        end_date: { type: 'string', description: 'End date (YYYY-MM-DD format)' }
      },
      required: %w[page_path start_date end_date]
    },
    execute: ->(page_path:, start_date:, end_date:, **_) {
      client = Seo::Ga4ApiClient.new
      results = client.page_traffic_sources(page_path: page_path, start_date: start_date, end_date: end_date)
      results.to_json
    } }
].freeze

Class Method Summary collapse

Class Method Details

.toolsObject



60
61
62
63
64
65
# File 'app/services/assistant/ga4_tool_builder.rb', line 60

def tools
  @tools ||= build_tools
rescue StandardError => err
  Rails.logger.warn("[Ga4ToolBuilder] Failed to build GA4 tools: #{err.message}")
  []
end