nps-segment-report 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: f9dbd5cfdec5fa430631d0509df3df9d231495a6434beea6def4e947f95f466e
4
+ data.tar.gz: 017608d69fc37f5f78ec45b84b6f5f8dbdd8384614624d409bf41f0a8a64a00f
5
+ SHA512:
6
+ metadata.gz: 3de6bc281118208c3cc4c8438e3102563b87dea565677b29655ce0bc726236801e513bf7adb1169ed2fcde65f60e8ecf6a6bcc97012f4e22c03c6a3dbe5eff96
7
+ data.tar.gz: 64b8d4889df882276c4a6c927cb4a2f743039b2abf403e41730814d9d388d5efb74ff386a6c5dafaf2be5fa190b6c54f4f7bf3464a861b1749cdb8ff372aff9b
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,43 @@
1
+ # nps-segment-report
2
+
3
+ Ruby command line tool and library. Give it a CSV of survey responses with a segment label and a 0 to 10 score. It counts promoters, passives and detractors overall and per segment, works out the score for each, hides segments with too few responses, and names the best and the worst segment shown.
4
+
5
+ Part of the [OneGTMLab](https://www.onegtmlab.dev) free GTM tools. Background on the idea: [HubSpot](https://www.onegtmlab.dev/tool-directory/crm/hubspot).
6
+
7
+ ## Install
8
+
9
+ ```
10
+ gem install nps-segment-report
11
+ ```
12
+
13
+ ## Use
14
+
15
+ CSV columns: respondent_id, segment, score. The segment is a label from your own data. It is trimmed and compared ignoring upper and lower case, and the first spelling seen is shown. The score is a whole number from 0 to 10 that you collected. The definition is fixed: promoters score 9 or 10, passives 7 or 8, detractors 0 to 6. The score is the promoter share minus the detractor share, in percentage points, from -100.0 to 100.0. It is worked from the exact counts and then rounded half up to 1 decimal, so it can differ by 0.1 from the difference of the two rounded shares printed beside it (46.7 minus 33.3 is 13.4, the score is 13.3). Shares are rounded half up to 1 decimal. Segments are sorted by score, highest first, then by name. Segments with fewer responses than --min-responses (a whole number of 1 or more, default 1, set by you) are hidden and the number hidden is printed; the best and the worst segment are taken from the segments shown, and a tie is printed as a tie. The tool reads no survey platform and does not say what a score means for a segment; small segments swing a lot, which is what --min-responses is for. A respondent_id that appears twice (ignoring case) is an error. A missing file, a missing column, a score that is not a whole number from 0 to 10 or an empty segment (the row is named), a bad --min-responses, an unknown option or a CSV with no rows stops the run with an error and a non-zero exit code.
16
+
17
+ ```
18
+ $ ruby bin/nps-segment-report sample.csv --min-responses 3
19
+ responses: 15
20
+
21
+ overall count share
22
+ promoters 7 46.7%
23
+ passives 3 20.0%
24
+ detractors 5 33.3%
25
+ score: 13.3
26
+
27
+ segment responses promoters passives detractors score
28
+ Segment B 5 3 1 1 40.0
29
+ Segment A 4 2 1 1 25.0
30
+ Segment C 4 1 1 2 -25.0
31
+
32
+ segments hidden (fewer than 3 responses): 1
33
+ best segment: Segment B (40.0)
34
+ worst segment: Segment C (-25.0)
35
+ ```
36
+
37
+ ## Test
38
+
39
+ ```
40
+ ruby test/run.rb
41
+ ```
42
+
43
+ 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/nps_segment_report'
5
+
6
+ if ARGV.empty? || ARGV.include?('-h') || ARGV.include?('--help')
7
+ warn "usage: nps-segment-report responses.csv [--min-responses N]\n" \
8
+ "CSV columns: respondent_id, segment, score. More: https://www.onegtmlab.dev/tool-directory/crm/hubspot"
9
+ exit 2
10
+ end
11
+
12
+ begin
13
+ path, min = NpsSegmentReport.parse_args(ARGV)
14
+ rows = NpsSegmentReport.from_csv(File.read(path))
15
+ print NpsSegmentReport.render(NpsSegmentReport.analyze(rows, min_responses: min))
16
+ rescue ArgumentError, Errno::ENOENT, Errno::EISDIR, CSV::MalformedCSVError => e
17
+ abort "error: #{e.message}"
18
+ end
@@ -0,0 +1,149 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'csv'
4
+
5
+ # Net promoter style score per segment, from the 0 to 10 scores YOU collected.
6
+ # Fixed definition: promoters score 9 or 10, passives 7 or 8, detractors 0 to 6.
7
+ # Score = promoter share minus detractor share, in percentage points, computed from the exact counts (not from rounded shares).
8
+ module NpsSegmentReport
9
+ Row = Struct.new(:id, :segment, :score, keyword_init: true)
10
+
11
+ # Percentage of n in d, rounded half up to 1 decimal. 0.0 when d is 0.
12
+ def self.pct(n, d)
13
+ return 0.0 if d.zero?
14
+
15
+ Rational(n * 100, d).round(1).to_f
16
+ end
17
+
18
+ def self.category(score)
19
+ if score >= 9 then :promoters
20
+ elsif score >= 7 then :passives
21
+ else :detractors
22
+ end
23
+ end
24
+
25
+ # (promoters - detractors) / responses * 100, rounded half up to 1 decimal.
26
+ def self.nps(promoters, detractors, total)
27
+ return 0.0 if total.zero?
28
+
29
+ Rational((promoters - detractors) * 100, total).round(1).to_f
30
+ end
31
+
32
+ def self.parse_score(raw, line)
33
+ s = raw.to_s.strip
34
+ raise ArgumentError, "row #{line}: bad score #{s.inspect}, expected a whole number from 0 to 10" unless s.match?(/\A\d+\z/) && s.to_i <= 10
35
+
36
+ s.to_i
37
+ end
38
+
39
+ def self.from_csv(text)
40
+ table = CSV.parse(text.sub(/\A/, ''), headers: true, header_converters: ->(h) { h.to_s.strip.downcase })
41
+ missing = %w[respondent_id segment score] - table.headers.compact
42
+ raise ArgumentError, "missing column(s): #{missing.join(', ')}" unless missing.empty?
43
+ raise ArgumentError, 'no rows in the CSV' if table.empty?
44
+
45
+ seen = {}
46
+ rows = []
47
+ table.each_with_index do |r, i|
48
+ line = i + 2
49
+ id = r['respondent_id'].to_s.strip
50
+ segment = r['segment'].to_s.strip
51
+ raise ArgumentError, "row #{line}: empty respondent_id" if id.empty?
52
+ raise ArgumentError, "row #{line}: empty segment for respondent #{id.inspect}" if segment.empty?
53
+ raise ArgumentError, "row #{line}: duplicate respondent_id #{id.inspect} (first seen on row #{seen[id.downcase]})" if seen.key?(id.downcase)
54
+
55
+ seen[id.downcase] = line
56
+ rows << Row.new(id: id, segment: segment, score: parse_score(r['score'], line))
57
+ end
58
+ rows
59
+ end
60
+
61
+ def self.counts(rows)
62
+ c = { promoters: 0, passives: 0, detractors: 0 }
63
+ rows.each { |r| c[category(r.score)] += 1 }
64
+ c.merge(responses: rows.size)
65
+ end
66
+
67
+ def self.summarise(rows)
68
+ c = counts(rows)
69
+ c.merge(promoter_share: pct(c[:promoters], c[:responses]), passive_share: pct(c[:passives], c[:responses]),
70
+ detractor_share: pct(c[:detractors], c[:responses]), score: nps(c[:promoters], c[:detractors], c[:responses]))
71
+ end
72
+
73
+ # Segments are grouped ignoring case; the first spelling seen is shown. Segments with fewer than min_responses are hidden.
74
+ def self.analyze(rows, min_responses: 1)
75
+ groups = {}
76
+ rows.each do |r|
77
+ g = (groups[r.segment.downcase] ||= { label: r.segment, rows: [] })
78
+ g[:rows] << r
79
+ end
80
+ all = groups.values.map { |g| { label: g[:label] }.merge(summarise(g[:rows])) }
81
+ shown = all.select { |s| s[:responses] >= min_responses }
82
+ shown.sort_by! { |s| [-s[:score], s[:label].downcase, s[:label]] }
83
+ {
84
+ overall: summarise(rows), min_responses: min_responses, segments: shown, hidden: all.size - shown.size,
85
+ total_segments: all.size,
86
+ best: tied(shown, shown.map { |s| s[:score] }.max), worst: tied(shown, shown.map { |s| s[:score] }.min)
87
+ }
88
+ end
89
+
90
+ def self.tied(shown, value)
91
+ return nil if shown.empty?
92
+
93
+ { labels: shown.select { |s| s[:score] == value }.map { |s| s[:label] }, score: value }
94
+ end
95
+
96
+ def self.tie_text(t)
97
+ t[:labels].size > 1 ? "tie (#{t[:labels].join(', ')}), #{format('%.1f', t[:score])} each" : "#{t[:labels].first} (#{format('%.1f', t[:score])})"
98
+ end
99
+
100
+ def self.render(r)
101
+ o = r[:overall]
102
+ out = +"responses: #{o[:responses]}\n\n"
103
+ out << format("%-11s %6s %7s\n", 'overall', 'count', 'share')
104
+ out << format("%-11s %6d %6.1f%%\n", 'promoters', o[:promoters], o[:promoter_share])
105
+ out << format("%-11s %6d %6.1f%%\n", 'passives', o[:passives], o[:passive_share])
106
+ out << format("%-11s %6d %6.1f%%\n", 'detractors', o[:detractors], o[:detractor_share])
107
+ out << format("score: %.1f\n\n", o[:score])
108
+ if r[:segments].empty?
109
+ out << "no segment has #{r[:min_responses]} or more responses (#{r[:hidden]} hidden)\n"
110
+ return out
111
+ end
112
+ w = [r[:segments].map { |s| s[:label].length }.max, 7].max
113
+ out << format("%-#{w}s %9s %9s %8s %10s %7s\n", 'segment', 'responses', 'promoters', 'passives', 'detractors', 'score')
114
+ r[:segments].each do |s|
115
+ out << format("%-#{w}s %9d %9d %8d %10d %7.1f\n", s[:label], s[:responses], s[:promoters], s[:passives], s[:detractors], s[:score])
116
+ end
117
+ out << "\nsegments hidden (fewer than #{r[:min_responses]} responses): #{r[:hidden]}\n"
118
+ out << "best segment: #{tie_text(r[:best])}\n"
119
+ out << "worst segment: #{tie_text(r[:worst])}\n"
120
+ out
121
+ end
122
+
123
+ # Returns [path, min_responses]; raises ArgumentError on bad input.
124
+ def self.parse_args(argv)
125
+ args = argv.dup
126
+ path = nil
127
+ min = 1
128
+ until args.empty?
129
+ arg = args.shift
130
+ case arg
131
+ when '--min-responses'
132
+ raise ArgumentError, '--min-responses needs a whole number of 1 or more' if args.empty?
133
+
134
+ raw = args.shift.to_s.strip
135
+ raise ArgumentError, "bad --min-responses #{raw.inspect}, expected a whole number of 1 or more" unless raw.match?(/\A\d+\z/) && raw.to_i >= 1
136
+
137
+ min = raw.to_i
138
+ when /\A-/ then raise ArgumentError, "unknown option #{arg}"
139
+ else
140
+ raise ArgumentError, "unexpected extra argument #{arg.inspect}" if path
141
+
142
+ path = arg
143
+ end
144
+ end
145
+ raise ArgumentError, 'no CSV file given' unless path
146
+
147
+ [path, min]
148
+ end
149
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nps-segment-report
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 respondent, segment and a 0 to 10 score, count promoters (9 and 10), passives (7 and 8) and detractors (0 to 6), and show the score (promoter share minus detractor share) overall and per segment, hiding segments with too few responses. Offline."
29
+ executables:
30
+ - nps-segment-report
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - bin/nps-segment-report
37
+ - lib/nps_segment_report.rb
38
+ homepage: "https://www.onegtmlab.dev/tool-directory/crm/hubspot"
39
+ licenses:
40
+ - MIT
41
+ metadata:
42
+ source_code_uri: "https://github.com/jaizendra-netizen/nps-segment-report"
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: "Split 0 to 10 survey scores into promoters, passives and detractors and show the score per segment"
62
+ test_files: []