Class: Seo::GscSyncService

Inherits:
BaseService show all
Defined in:
app/services/seo/gsc_sync_service.rb

Overview

Syncs search performance data from Google Search Console.
Writes to site_map_data_points (source of truth) and updates seo_clicks on site_maps (cached for SQL sorting).

GSC provides first-party, authoritative data on how your pages appear in Google Search.
Data is typically delayed 2-3 days from Google.

Examples:

Basic usage (last 28 days)

Seo::GscSyncService.new.process

Custom date range

Seo::GscSyncService.new(
  start_date: 90.days.ago.to_date,
  end_date: Date.yesterday
).process

Constant Summary collapse

DEFAULT_DATE_RANGE =

days

28

Instance Method Summary collapse

Methods inherited from BaseService

#log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger

Constructor Details

#initialize(options = {}) ⇒ GscSyncService

Returns a new instance of GscSyncService.



22
23
24
25
26
27
# File 'app/services/seo/gsc_sync_service.rb', line 22

def initialize(options = {})
  super
  @start_date = options[:start_date] || DEFAULT_DATE_RANGE.days.ago.to_date
  @end_date = options[:end_date] || Date.yesterday
  @stats = { pages_updated: 0, errors: [] }
end

Instance Method Details

#processObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/services/seo/gsc_sync_service.rb', line 29

def process
  @logger.info "[GscSyncService] Starting sync for #{@start_date} to #{@end_date}"

  # Fetch all pages data from GSC
  pages_data = fetch_all_pages
  return @stats if pages_data.blank?

  @logger.info "[GscSyncService] Fetched #{pages_data.size} pages from GSC"

  # Match GSC URLs to SiteMap records and update metrics
  process_pages(pages_data)

  @logger.info "[GscSyncService] Completed: #{@stats[:pages_updated]} pages updated"
  @stats
end