gtm-spend-share-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 +7 -0
- data/LICENSE +22 -0
- data/README.md +38 -0
- data/bin/gtm-spend-share-report +18 -0
- data/lib/gtm_spend_share_report.rb +175 -0
- metadata +62 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 7222d4b58dcaebf550bda141245fec400dfa369d7250f6179df31d65555c3c2a
|
|
4
|
+
data.tar.gz: 350e49fa75e6cb7ba11c7ea4756d0342f0e1b7d9541216d94b8b323cbeea629f
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: ef93b1b8c4166112552c39a482a4a7eb7f7e5d9a6e36d9f529f4b08073a5d8cffc26b0ff38b9e744fd0aaeaf2adf6840bd232407bae1ee8ae4728924b6d98e7a
|
|
7
|
+
data.tar.gz: d8fd65e044af2deb9f493cf0afd2eea4f74d5bdb97f78d1b5a958185799cba40ae7d96f5699e29e7479f98cb7be697dc108ea5f20236bbd9b159c4b5e7b6ae95
|
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,38 @@
|
|
|
1
|
+
# gtm-spend-share-report
|
|
2
|
+
|
|
3
|
+
Ruby command line tool and library. Give it a CSV of your monthly spend items where you have already written down which layer of your GTM stack each one belongs to. It shows the amount and the share of the total per layer, the largest and the smallest layer, and, if you set targets, the gap between your actual share and your target for each layer. The tool knows no layers of its own and does not say how you should spend: it only adds up your own figures.
|
|
4
|
+
|
|
5
|
+
Part of the [OneGTMLab](https://www.onegtmlab.dev) free GTM tools. Background on the idea: [the 5 layers of GTM](https://www.onegtmlab.dev/blog/5-layers-of-gtm-what-to-build-and-where-to-spend).
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
gem install gtm-spend-share-report
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Use
|
|
14
|
+
|
|
15
|
+
CSV columns: item, layer, monthly_amount. The layer is a label you assigned by hand (any names you like); layers are trimmed and compared ignoring upper and lower case, and the first spelling seen is shown. monthly_amount is a number of 0 or more in your own currency; the tool adds no currency and does not convert. Output: items, total amount (2 decimals); per layer the items, the amount (2 decimals) and the share of the total (1 decimal), sorted by amount, highest first, then by name; then the largest and the smallest layer (a tie is shown as a tie; layers that appear only in a --target do not count here). Option --target "LAYER=PERCENT" can be repeated; the percent is a number from 0 to 100 set by you, and the layer is matched ignoring case. It adds the target, the gap in percentage points (actual minus target, signed, 1 decimal) and a word: "over target" or "under target" when the gap is more than 0.5 point either way, otherwise "on target". This 0.5 point band is fixed in the tool, and the gap is worked out from the shares exactly as printed. A targeted layer with no items shows 0.00 spend. All amounts and percentages are rounded half up. A missing file, a missing column, a bad monthly_amount or an empty layer (the row is named), a bad or repeated --target, a percent outside 0 to 100, an unknown option, a total of 0 or a CSV with no rows stops the run with an error and a non-zero exit code.
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
$ ruby bin/gtm-spend-share-report sample.csv --target "Layer A=40" --target "Layer B=30"
|
|
19
|
+
items: 6
|
|
20
|
+
total amount: 7000.00
|
|
21
|
+
|
|
22
|
+
layer items amount share target gap status
|
|
23
|
+
Layer B 2 3000.00 42.9% 30.0% +12.9 over target
|
|
24
|
+
Layer A 2 2000.50 28.6% 40.0% -11.4 under target
|
|
25
|
+
Layer C 1 1000.00 14.3% - - no target
|
|
26
|
+
Layer D 1 999.50 14.3% - - no target
|
|
27
|
+
|
|
28
|
+
largest layer: Layer B, 3000.00
|
|
29
|
+
smallest layer: Layer D, 999.50
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Test
|
|
33
|
+
|
|
34
|
+
```
|
|
35
|
+
ruby test/run.rb
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
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/gtm_spend_share_report'
|
|
5
|
+
|
|
6
|
+
if ARGV.empty? || ARGV.include?('-h') || ARGV.include?('--help')
|
|
7
|
+
warn "usage: gtm-spend-share-report spend.csv [--target \"LAYER=PERCENT\" ...]\n" \
|
|
8
|
+
"CSV columns: item, layer, monthly_amount. More: https://www.onegtmlab.dev/blog/5-layers-of-gtm-what-to-build-and-where-to-spend"
|
|
9
|
+
exit 2
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
begin
|
|
13
|
+
path, targets = GtmSpendShareReport.parse_args(ARGV)
|
|
14
|
+
items = GtmSpendShareReport.from_csv(File.read(path))
|
|
15
|
+
print GtmSpendShareReport.render(GtmSpendShareReport.analyze(items, targets))
|
|
16
|
+
rescue ArgumentError, Errno::ENOENT, Errno::EISDIR, Errno::EACCES, CSV::MalformedCSVError => e
|
|
17
|
+
abort "error: #{e.message}"
|
|
18
|
+
end
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'csv'
|
|
4
|
+
|
|
5
|
+
# Share of your monthly spend per layer, using the layer labels YOU gave each item and, optionally, targets YOU set.
|
|
6
|
+
# The tool knows no layers of its own.
|
|
7
|
+
module GtmSpendShareReport
|
|
8
|
+
Item = Struct.new(:item, :layer, :amount, keyword_init: true)
|
|
9
|
+
|
|
10
|
+
# A target gap is "on target" unless the gap, on the shares as printed (1 decimal), is more than this many points either way.
|
|
11
|
+
BAND_TENTHS = 5
|
|
12
|
+
|
|
13
|
+
# Round a non-negative Rational half up to an Integer.
|
|
14
|
+
def self.round_up(r)
|
|
15
|
+
(r + Rational(1, 2)).floor
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Rational -> string with `places` decimals, rounded half up. Works for negative values too (rounds half away from zero).
|
|
19
|
+
def self.fixed(r, places)
|
|
20
|
+
scale = 10**places
|
|
21
|
+
sign = r.negative? ? '-' : ''
|
|
22
|
+
n = round_up(r.abs * scale)
|
|
23
|
+
return "#{sign}#{n}" if places.zero?
|
|
24
|
+
|
|
25
|
+
"#{sign}#{n / scale}.#{(n % scale).to_s.rjust(places, '0')}"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def self.money(r)
|
|
29
|
+
fixed(r, 2)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Share of total in tenths of a percent (an Integer), rounded half up.
|
|
33
|
+
def self.share_tenths(amount, total)
|
|
34
|
+
round_up(amount * 1000 / total)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.tenths_text(t, signed: false)
|
|
38
|
+
sign = t.negative? ? '-' : (signed ? '+' : '')
|
|
39
|
+
"#{sign}#{t.abs / 10}.#{t.abs % 10}"
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def self.parse_amount(raw, line)
|
|
43
|
+
s = raw.to_s.strip
|
|
44
|
+
raise ArgumentError, "row #{line}: bad monthly_amount #{s.inspect}, expected a number of 0 or more" unless s.match?(/\A\d+(\.\d+)?\z/)
|
|
45
|
+
|
|
46
|
+
Rational(s)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Returns the items; raises ArgumentError on bad input.
|
|
50
|
+
def self.from_csv(text)
|
|
51
|
+
table = CSV.parse(text.sub(/\A/, ''), headers: true, header_converters: ->(h) { h.to_s.strip.downcase })
|
|
52
|
+
missing = %w[item layer monthly_amount] - table.headers.compact
|
|
53
|
+
raise ArgumentError, "missing column(s): #{missing.join(', ')}" unless missing.empty?
|
|
54
|
+
raise ArgumentError, 'no rows in the CSV' if table.empty?
|
|
55
|
+
|
|
56
|
+
table.each_with_index.map do |r, i|
|
|
57
|
+
line = i + 2
|
|
58
|
+
layer = r['layer'].to_s.strip
|
|
59
|
+
raise ArgumentError, "row #{line}: empty layer for item #{r['item'].to_s.strip.inspect}" if layer.empty?
|
|
60
|
+
|
|
61
|
+
Item.new(item: r['item'].to_s.strip, layer: layer, amount: parse_amount(r['monthly_amount'], line))
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# "LAYER=PERCENT" -> [label, Rational]. The split is at the last "=", so a layer label may contain "=".
|
|
66
|
+
def self.parse_target(text)
|
|
67
|
+
idx = text.rindex('=')
|
|
68
|
+
raise ArgumentError, "bad --target #{text.inspect}, expected \"LAYER=PERCENT\"" if idx.nil? || idx.zero?
|
|
69
|
+
|
|
70
|
+
label = text[0...idx].strip
|
|
71
|
+
pct = text[(idx + 1)..].strip
|
|
72
|
+
raise ArgumentError, "bad --target #{text.inspect}, expected \"LAYER=PERCENT\"" if label.empty? || !pct.match?(/\A\d+(\.\d+)?\z/)
|
|
73
|
+
|
|
74
|
+
value = Rational(pct)
|
|
75
|
+
raise ArgumentError, "bad --target #{text.inspect}, percent must be from 0 to 100" if value > 100
|
|
76
|
+
|
|
77
|
+
[label, value]
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# targets: array of [label, Rational percent]. Raises on the same layer given twice.
|
|
81
|
+
def self.analyze(items, targets = [])
|
|
82
|
+
groups = {}
|
|
83
|
+
items.each do |it|
|
|
84
|
+
g = (groups[it.layer.downcase] ||= { label: it.layer, items: 0, amount: Rational(0) })
|
|
85
|
+
g[:items] += 1
|
|
86
|
+
g[:amount] += it.amount
|
|
87
|
+
end
|
|
88
|
+
total = items.sum(&:amount)
|
|
89
|
+
raise ArgumentError, 'total amount is 0, shares cannot be computed' if total.zero?
|
|
90
|
+
|
|
91
|
+
wanted = {}
|
|
92
|
+
targets.each do |label, pct|
|
|
93
|
+
key = label.downcase
|
|
94
|
+
raise ArgumentError, "duplicate --target for layer #{label.inspect}" if wanted.key?(key)
|
|
95
|
+
|
|
96
|
+
wanted[key] = [label, pct]
|
|
97
|
+
end
|
|
98
|
+
wanted.each do |key, (label, _)|
|
|
99
|
+
groups[key] ||= { label: label, items: 0, amount: Rational(0) }
|
|
100
|
+
end
|
|
101
|
+
present = items.map { |it| it.layer.downcase }.uniq
|
|
102
|
+
layers = groups.map do |key, g|
|
|
103
|
+
l = { label: g[:label], items: g[:items], amount: g[:amount], share_tenths: share_tenths(g[:amount], total),
|
|
104
|
+
in_file: present.include?(key) }
|
|
105
|
+
if wanted.key?(key)
|
|
106
|
+
l[:target_tenths] = round_up(wanted[key][1] * 10)
|
|
107
|
+
l[:gap_tenths] = l[:share_tenths] - l[:target_tenths]
|
|
108
|
+
l[:verdict] = if l[:gap_tenths] > BAND_TENTHS then 'over target'
|
|
109
|
+
elsif l[:gap_tenths] < -BAND_TENTHS then 'under target'
|
|
110
|
+
else 'on target'
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
l
|
|
114
|
+
end
|
|
115
|
+
layers.sort_by! { |l| [-l[:amount], l[:label].downcase, l[:label]] }
|
|
116
|
+
in_file = layers.select { |l| l[:in_file] }
|
|
117
|
+
{ items: items.size, total: total, layers: layers, targeted: !wanted.empty?,
|
|
118
|
+
largest: extremes(in_file, in_file.map { |l| l[:amount] }.max),
|
|
119
|
+
smallest: extremes(in_file, in_file.map { |l| l[:amount] }.min) }
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def self.extremes(layers, value)
|
|
123
|
+
{ labels: layers.select { |l| l[:amount] == value }.map { |l| l[:label] }.sort_by { |x| [x.downcase, x] }, amount: value }
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def self.extreme_text(e)
|
|
127
|
+
names = e[:labels].size > 1 ? "tie (#{e[:labels].join(', ')})" : e[:labels].first
|
|
128
|
+
"#{names}, #{money(e[:amount])}#{e[:labels].size > 1 ? ' each' : ''}"
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def self.render(res)
|
|
132
|
+
out = +"items: #{res[:items]}\ntotal amount: #{money(res[:total])}\n\n"
|
|
133
|
+
head = format('%-14s %5s %12s %7s', 'layer', 'items', 'amount', 'share')
|
|
134
|
+
head += format(' %7s %8s %s', 'target', 'gap', 'status') if res[:targeted]
|
|
135
|
+
out << head << "\n"
|
|
136
|
+
res[:layers].each do |l|
|
|
137
|
+
line = format('%-14s %5d %12s %6s%%', l[:label], l[:items], money(l[:amount]), tenths_text(l[:share_tenths]))
|
|
138
|
+
if res[:targeted]
|
|
139
|
+
line += if l[:target_tenths]
|
|
140
|
+
format(' %6s%% %8s %s', tenths_text(l[:target_tenths]), tenths_text(l[:gap_tenths], signed: true), l[:verdict])
|
|
141
|
+
else
|
|
142
|
+
format(' %7s %8s %s', '-', '-', 'no target')
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
out << line << "\n"
|
|
146
|
+
end
|
|
147
|
+
out << "\nlargest layer: #{extreme_text(res[:largest])}\nsmallest layer: #{extreme_text(res[:smallest])}\n"
|
|
148
|
+
out
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# Returns [path, targets]; raises ArgumentError on bad input.
|
|
152
|
+
def self.parse_args(argv)
|
|
153
|
+
args = argv.dup
|
|
154
|
+
path = nil
|
|
155
|
+
targets = []
|
|
156
|
+
until args.empty?
|
|
157
|
+
arg = args.shift
|
|
158
|
+
case arg
|
|
159
|
+
when '--target'
|
|
160
|
+
raise ArgumentError, '--target needs a value like "LAYER=PERCENT"' if args.empty?
|
|
161
|
+
|
|
162
|
+
targets << parse_target(args.shift.to_s)
|
|
163
|
+
when /\A--target=(.*)\z/m then targets << parse_target(Regexp.last_match(1))
|
|
164
|
+
when /\A-/ then raise ArgumentError, "unknown option #{arg}"
|
|
165
|
+
else
|
|
166
|
+
raise ArgumentError, "unexpected extra argument #{arg.inspect}" if path
|
|
167
|
+
|
|
168
|
+
path = arg
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
raise ArgumentError, 'no CSV file given' unless path
|
|
172
|
+
|
|
173
|
+
[path, targets]
|
|
174
|
+
end
|
|
175
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: gtm-spend-share-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 items with a layer label you assigned and a monthly amount, show the amount and share of the total per layer, the largest and smallest layer, and the gap to targets you set. The tool knows no layers of its own. Offline."
|
|
29
|
+
executables:
|
|
30
|
+
- gtm-spend-share-report
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- LICENSE
|
|
35
|
+
- README.md
|
|
36
|
+
- bin/gtm-spend-share-report
|
|
37
|
+
- lib/gtm_spend_share_report.rb
|
|
38
|
+
homepage: "https://www.onegtmlab.dev/blog/5-layers-of-gtm-what-to-build-and-where-to-spend"
|
|
39
|
+
licenses:
|
|
40
|
+
- MIT
|
|
41
|
+
metadata:
|
|
42
|
+
source_code_uri: "https://github.com/jaizendra-netizen/gtm-spend-share-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: "Share of your monthly GTM spend per layer you defined, with optional targets, from a CSV"
|
|
62
|
+
test_files: []
|