backlink-anchor-mix 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 59c4f50651812037b2108771bc7bdc57d02ae303784c987b27d1fa978b1a0849
4
+ data.tar.gz: 7bb6b68c8c7ef88278e3614b16c2407d4556ff8be8a8225d55855570e39d47e0
5
+ SHA512:
6
+ metadata.gz: 3d217beb677c9f30ae81fb3f29751b96907cfb772fd6294b7ca61e5c4c76536f4fdb67dacce5a79b78eb51b733171385d317d7e40f3e8fe180b89b650b29247d
7
+ data.tar.gz: 890cb89e70759bdbbd7a4bb40dc33e7cb5cd1104fc6f98e3b8a4680925eed8752f3b7ed206c6aa1ac32485823f33848d0238555033ce8903d113fe1fd522f6b7
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 OneGTMLab
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # backlink-anchor-mix
2
+
3
+ Ruby command line tool and library. Give it a CSV of backlinks and it classifies the anchor text of each one as empty or image, naked url, branded, exact match, partial match, generic or other, then shows how many links fall in each type, the share of all links, and how many links are dofollow. If the exact match share is above a limit you set, it says so.
4
+
5
+ Part of the [OneGTMLab](https://www.onegtmlab.dev) free GTM tools. Background on the idea: [backlinks](https://www.onegtmlab.dev/glossary/backlinks).
6
+
7
+ ## Install
8
+
9
+ ```
10
+ gem install backlink-anchor-mix
11
+ ```
12
+
13
+ ## Use
14
+
15
+ CSV columns: source_url, anchor_text, rel. The rel column may be empty, nofollow, ugc or sponsored; a link is dofollow when rel holds none of those three. Pass your brand terms with --brand and your target keywords with --keywords (comma separated, not case sensitive). Each anchor gets the first type that matches, in this order: (1) blank anchor is empty or image; (2) starts with http://, https:// or www., or is a bare domain such as example.org/page, is a naked url; (3) contains a brand term is branded; (4) equals a keyword exactly (trimmed) is an exact match; (5) contains a keyword is a partial match; (6) equals one of click here, here, read more, learn more, website, link, this, visit, source is generic; (7) anything else is other. The exact match limit is --max-exact (default 10, set by you): it is a parameter, not a rule. A missing file, a missing column or a bad option stops the run with an error and a non-zero exit code.
16
+
17
+ ```
18
+ $ ruby bin/backlink-anchor-mix sample.csv --brand "Example Co,example.com" --keywords "crm audit,crm review"
19
+ links total: 10
20
+ dofollow: 7
21
+
22
+ type links share
23
+ branded 2 20.0%
24
+ exact match 2 20.0%
25
+ naked url 2 20.0%
26
+ empty or image 1 10.0%
27
+ generic 1 10.0%
28
+ other 1 10.0%
29
+ partial match 1 10.0%
30
+
31
+ Exact match share 20.0% is above the limit of 10% (--max-exact, set by you).
32
+ ```
33
+
34
+ ## Test
35
+
36
+ ```
37
+ ruby test/run.rb
38
+ ```
39
+
40
+ Offline, no network calls, no dependencies beyond the language runtime. MIT licensed.
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/backlink_anchor_mix'
5
+
6
+ if ARGV.empty? || ARGV.include?('-h') || ARGV.include?('--help')
7
+ warn "usage: backlink-anchor-mix links.csv [--brand \"Example Co,example.com\"] [--keywords \"crm audit,crm review\"] [--max-exact 10]\n" \
8
+ "CSV columns: source_url, anchor_text, rel. More: https://www.onegtmlab.dev/glossary/backlinks"
9
+ exit 2
10
+ end
11
+
12
+ begin
13
+ path, brands, keywords, max_exact = BacklinkAnchorMix.parse_args(ARGV)
14
+ links = BacklinkAnchorMix.from_csv(File.read(path), brands, keywords)
15
+ print BacklinkAnchorMix.render(BacklinkAnchorMix.summarize(links), max_exact)
16
+ rescue ArgumentError, Errno::ENOENT, Errno::EISDIR, CSV::MalformedCSVError => e
17
+ abort "error: #{e.message}"
18
+ end
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'csv'
4
+
5
+ # Classify the anchor text of backlinks into types and show the mix.
6
+ module BacklinkAnchorMix
7
+ TYPES = ['empty or image', 'naked url', 'branded', 'exact match', 'partial match', 'generic', 'other'].freeze
8
+ GENERIC = ['click here', 'here', 'read more', 'learn more', 'website', 'link', 'this', 'visit', 'source'].freeze
9
+ NOFOLLOW_VALUES = %w[nofollow ugc sponsored].freeze
10
+ DEFAULT_MAX_EXACT = 10.0
11
+ BARE_DOMAIN = %r{\A[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)*\.[a-z]{2,}(?:[/?#]\S*)?\z}i.freeze
12
+
13
+ Link = Struct.new(:source_url, :anchor, :rel, :type, :dofollow, keyword_init: true)
14
+
15
+ # Split "a, b ,c" into trimmed, lowercased, non-empty terms.
16
+ def self.terms(text)
17
+ text.to_s.split(',').map { |t| t.strip.downcase }.reject(&:empty?)
18
+ end
19
+
20
+ def self.naked_url?(anchor)
21
+ anchor.match?(%r{\A(?:https?://|www\.)}i) || anchor.match?(BARE_DOMAIN)
22
+ end
23
+
24
+ # Rules, first match wins. brands and keywords are arrays of lowercase terms.
25
+ def self.classify(anchor, brands, keywords)
26
+ a = anchor.to_s.strip
27
+ return 'empty or image' if a.empty?
28
+ return 'naked url' if naked_url?(a)
29
+
30
+ low = a.downcase
31
+ return 'branded' if brands.any? { |b| low.include?(b) }
32
+ return 'exact match' if keywords.include?(low)
33
+ return 'partial match' if keywords.any? { |k| low.include?(k) }
34
+ return 'generic' if GENERIC.include?(low)
35
+
36
+ 'other'
37
+ end
38
+
39
+ def self.dofollow?(rel)
40
+ (rel.to_s.downcase.split(/[\s,]+/) & NOFOLLOW_VALUES).empty?
41
+ end
42
+
43
+ def self.from_csv(text, brands, keywords)
44
+ table = CSV.parse(text, headers: true)
45
+ missing = %w[source_url anchor_text rel] - table.headers.compact.map(&:strip)
46
+ raise ArgumentError, "missing column(s): #{missing.join(', ')}" unless missing.empty?
47
+ raise ArgumentError, 'no rows in the CSV' if table.empty?
48
+
49
+ table.map do |r|
50
+ Link.new(source_url: r['source_url'].to_s.strip, anchor: r['anchor_text'].to_s.strip, rel: r['rel'].to_s.strip,
51
+ type: classify(r['anchor_text'], brands, keywords), dofollow: dofollow?(r['rel']))
52
+ end
53
+ end
54
+
55
+ # Returns { total:, dofollow:, types: [[type, count, share], ...] } sorted by count desc, then name.
56
+ def self.summarize(links)
57
+ total = links.size
58
+ counts = TYPES.to_h { |t| [t, links.count { |l| l.type == t }] }
59
+ rows = counts.map { |t, c| [t, c, c * 100.0 / total] }.sort_by { |t, c, _| [-c, t] }
60
+ { total: total, dofollow: links.count(&:dofollow), types: rows }
61
+ end
62
+
63
+ def self.exact_share(summary)
64
+ summary[:types].find { |t, _, _| t == 'exact match' }[2]
65
+ end
66
+
67
+ def self.render(summary, max_exact = DEFAULT_MAX_EXACT)
68
+ out = +"links total: #{summary[:total]}\n"
69
+ out << "dofollow: #{summary[:dofollow]}\n\n"
70
+ out << format("%-15s %6s %7s\n", 'type', 'links', 'share')
71
+ summary[:types].each { |t, c, s| out << format("%-15s %6d %6.1f%%\n", t, c, s) }
72
+ share = exact_share(summary)
73
+ if share > max_exact
74
+ out << format("\nExact match share %.1f%% is above the limit of %s%% (--max-exact, set by you).\n", share, trim(max_exact))
75
+ end
76
+ out
77
+ end
78
+
79
+ def self.trim(number)
80
+ number == number.to_i ? number.to_i.to_s : number.to_s
81
+ end
82
+
83
+ # Parse the command line. Returns [path, brands, keywords, max_exact]; raises ArgumentError on bad input.
84
+ def self.parse_args(argv)
85
+ args = argv.dup
86
+ path = nil
87
+ brands = []
88
+ keywords = []
89
+ max_exact = DEFAULT_MAX_EXACT
90
+ until args.empty?
91
+ arg = args.shift
92
+ case arg
93
+ when '--brand', '--keywords', '--max-exact'
94
+ raise ArgumentError, "#{arg} needs a value" if args.empty?
95
+
96
+ val = args.shift
97
+ case arg
98
+ when '--brand' then brands = terms(val)
99
+ when '--keywords' then keywords = terms(val)
100
+ else
101
+ raise ArgumentError, "bad --max-exact #{val.inspect}, expected a number from 0 to 100" unless val.match?(/\A\d+(\.\d+)?\z/) && val.to_f <= 100
102
+
103
+ max_exact = val.to_f
104
+ end
105
+ when /\A-/ then raise ArgumentError, "unknown option #{arg}"
106
+ else
107
+ raise ArgumentError, "unexpected extra argument #{arg.inspect}" if path
108
+
109
+ path = arg
110
+ end
111
+ end
112
+ raise ArgumentError, 'no CSV file given' unless path
113
+
114
+ [path, brands, keywords, max_exact]
115
+ end
116
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: backlink-anchor-mix
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - OneGTMLab
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: csv
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ -
17
+ - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: "0"
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ -
25
+ - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: "0"
28
+ description: "Ruby CLI and library: from a CSV of backlinks (source url, anchor text, rel) classify each anchor as branded, exact match, partial match, naked url, generic, empty or other, and show counts and shares. Offline."
29
+ executables:
30
+ - backlink-anchor-mix
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - bin/backlink-anchor-mix
37
+ - lib/backlink_anchor_mix.rb
38
+ homepage: "https://www.onegtmlab.dev/glossary/backlinks"
39
+ licenses:
40
+ - MIT
41
+ metadata:
42
+ source_code_uri: "https://github.com/jaizendra-netizen/backlink-anchor-mix"
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ -
49
+ - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "2.7"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ -
55
+ - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ requirements: []
59
+ rubygems_version: 4.1.0
60
+ specification_version: 4
61
+ summary: "Classify backlink anchor text and show the mix of branded, exact match, naked url and other anchors"
62
+ test_files: []