Class: Seo::AhrefsSyncService

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

Overview

Syncs SEO metrics from Ahrefs MCP server to SiteMap records.
Fetches top pages data and organic keywords, updating:

  • seo_traffic, seo_keywords_count, seo_top_keyword, seo_top_position, seo_traffic_value
  • SeoPageKeyword records for cannibalization detection

Examples:

Basic usage

Seo::AhrefsSyncService.new.process

With options

Seo::AhrefsSyncService.new(
  target: 'warmlyyours.com',
  limit: 500
).process

Constant Summary collapse

DOMAIN =
'warmlyyours.com'
DEFAULT_LIMIT =
1000
MAX_RETRIES =
2

Instance Method Summary collapse

Methods inherited from BaseService

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

Constructor Details

#initialize(options = {}) ⇒ AhrefsSyncService

Returns a new instance of AhrefsSyncService.



23
24
25
26
27
28
29
# File 'app/services/seo/ahrefs_sync_service.rb', line 23

def initialize(options = {})
  super
  @target = options[:target] || DOMAIN
  @limit = options[:limit] || DEFAULT_LIMIT
  @sync_keywords = options.fetch(:sync_keywords, false) # Disabled by default (uses more API calls)
  @stats = { pages_updated: 0, keywords_synced: 0, errors: [] }
end

Instance Method Details

#processObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/services/seo/ahrefs_sync_service.rb', line 31

def process
  @logger.info "[AhrefsSyncService] Starting sync for #{@target}"

  # Fetch top pages data from Ahrefs
  pages_data = fetch_top_pages
  return @stats if pages_data.blank?

  @logger.info "[AhrefsSyncService] Fetched #{pages_data.size} pages from Ahrefs"

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

  @logger.info "[AhrefsSyncService] Completed: #{@stats[:pages_updated]} pages updated, " \
               "#{@stats[:keywords_synced]} keywords synced"
  @stats
end