Class: Seo::CannibalizationService
- Inherits:
-
BaseService
- Object
- BaseService
- Seo::CannibalizationService
- Defined in:
- app/services/seo/cannibalization_service.rb
Overview
Detects keyword cannibalization across all pages in the site.
Cannibalization occurs when multiple pages compete for the same keyword,
potentially diluting ranking power and confusing search engines.
Constant Summary collapse
- AT_RISK_POSITIONS =
Position range considered at risk for cannibalization
Pages ranking 5-30 are most vulnerable as they're competing (5..30)
- MIN_COMPETING_PAGES =
Minimum number of pages ranking for same keyword to flag
2
Instance Attribute Summary
Attributes inherited from BaseService
Instance Method Summary collapse
-
#analyze_keyword(keyword) ⇒ Hash?
Analyze a specific keyword for cannibalization.
-
#for_page(site_map) ⇒ Array<Hash>
Get cannibalization issues for a specific page.
-
#initialize(options = {}) ⇒ CannibalizationService
constructor
A new instance of CannibalizationService.
-
#process ⇒ Array<Hash>
Find all cannibalization issues site-wide.
-
#recommendations(issues) ⇒ Array<Hash>
Get recommended actions for cannibalization issues.
Methods inherited from BaseService
#log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger
Constructor Details
#initialize(options = {}) ⇒ CannibalizationService
Returns a new instance of CannibalizationService.
23 24 25 26 |
# File 'app/services/seo/cannibalization_service.rb', line 23 def initialize( = {}) super @min_search_volume = [:min_search_volume] || 50 end |
Instance Method Details
#analyze_keyword(keyword) ⇒ Hash?
Analyze a specific keyword for cannibalization
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'app/services/seo/cannibalization_service.rb', line 52 def analyze_keyword(keyword) pages = SeoPageKeyword.where(keyword: keyword) .ranking .includes(:site_map) return nil if pages.size < MIN_COMPETING_PAGES { keyword: keyword, search_volume: pages.maximum(:search_volume), pages: pages.map do |pk| { url: pk.site_map.url, category: pk.site_map.category, position: pk.position, traffic_share: pk.traffic_share } end.sort_by { |p| p[:position] || 999 } } end |
#for_page(site_map) ⇒ Array<Hash>
Get cannibalization issues for a specific page
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/services/seo/cannibalization_service.rb', line 76 def for_page(site_map) site_map.seo_page_keywords.at_risk.filter_map do |pk| competitors = pk.competing_pages.at_risk.includes(:site_map) next if competitors.empty? { keyword: pk.keyword, this_position: pk.position, search_volume: pk.search_volume, competitors: competitors.map do |cpk| { url: cpk.site_map.url, category: cpk.site_map.category, position: cpk.position } end } end end |
#process ⇒ Array<Hash>
Find all cannibalization issues site-wide
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/services/seo/cannibalization_service.rb', line 30 def process @logger.info '[CannibalizationService] Analyzing keyword cannibalization...' issues = competing_keywords.filter_map do |entry| next if entry[:pages].size < MIN_COMPETING_PAGES { keyword: entry[:keyword], locale: entry[:locale], search_volume: entry[:pages].first[:search_volume], pages: entry[:pages].sort_by { |p| p[:position] || 999 } } end @logger.info "[CannibalizationService] Found #{issues.size} cannibalization issues" issues.sort_by { |i| -(i[:search_volume] || 0) } end |
#recommendations(issues) ⇒ Array<Hash>
Get recommended actions for cannibalization issues
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'app/services/seo/cannibalization_service.rb', line 99 def recommendations(issues) issues.map do |issue| pages = issue[:pages] best_page = pages.min_by { |p| p[:position] || 999 } other_pages = pages - [best_page] { keyword: issue[:keyword], search_volume: issue[:search_volume], recommendation: build_recommendation(best_page, other_pages), best_page: best_page, consolidate_pages: other_pages } end end |