post-format-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 +7 -0
- data/LICENSE +22 -0
- data/README.md +40 -0
- data/bin/post-format-mix +18 -0
- data/lib/post_format_mix.rb +124 -0
- metadata +62 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 5ec716887e242d1aa95fb2522f6973a4113c71cc08f63dcf3c739a8376f50c3c
|
|
4
|
+
data.tar.gz: a1e90ba72f7ffce55666331d5eec2dc48a1e8ebf8745b5a6b5843a70ea93e32c
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 81ffa8a7fbfca77497f662de2af9af9a41b8ed278c0d5cbaed0f921a203941934458fae36b0e4edfffe7c221fc7848056b18f41ed333947c26a722beca4d6bd9
|
|
7
|
+
data.tar.gz: 04816541c6493bfc1d9a6c7f20116303f2651d7ab37b3685d960b4505f64703946790241842f3b2bdb48617eeb7b5ce550b744fdbdc7f5b2869e4ef34914ab9b
|
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
|
+
# post-format-mix
|
|
2
|
+
|
|
3
|
+
Ruby command line tool and library. Give it a CSV of your posts with the date, the format and the impressions from your own analytics export, and it shows how the posts split across formats: posts and share of posts per format, total, average and median impressions, the date of the best post, the most used format and the format with the highest average.
|
|
4
|
+
|
|
5
|
+
Part of the [OneGTMLab](https://www.onegtmlab.dev) free GTM tools. Background on the idea: [personal branding](https://www.onegtmlab.dev/glossary/personal-branding).
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
gem install post-format-mix
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Use
|
|
14
|
+
|
|
15
|
+
CSV columns: date (yyyy-mm-dd), format (any label, such as text, image, carousel, video or poll), impressions (a whole number of 0 or more). Formats are matched ignoring case and surrounding spaces, and the first spelling seen is the one shown. Shares of posts are percentages with 1 decimal; averages and medians have 1 decimal. Formats are sorted by number of posts, then by name. The best post of a format is the one with the most impressions; if two are equal the earlier date is shown. When two formats are equal on most used or on highest average the tool prints tie and lists them. With --min-posts N (default 1, set by you) a format with fewer posts than N is marked too few posts to compare and is left out of the highest average comparison. The numbers describe your own posts only; a format with a few posts says little. A missing file, a missing column, a bad date or impressions value (the row is named, the first data row is row 1), a bad option or a CSV with no rows stops the run with an error and a non-zero exit code.
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
$ ruby bin/post-format-mix sample.csv --min-posts 2
|
|
19
|
+
posts: 10
|
|
20
|
+
first post: 2026-01-05, last post: 2026-02-05
|
|
21
|
+
total impressions: 16400
|
|
22
|
+
|
|
23
|
+
format posts share impressions average median best post
|
|
24
|
+
text 4 40.0% 4500 1125.0 1100.0 2026-01-12
|
|
25
|
+
carousel 2 20.0% 7000 3500.0 3500.0 2026-01-15
|
|
26
|
+
image 2 20.0% 2000 1000.0 1000.0 2026-01-22
|
|
27
|
+
poll 1 10.0% 400 400.0 400.0 2026-02-05 too few posts to compare
|
|
28
|
+
video 1 10.0% 2500 2500.0 2500.0 2026-01-29 too few posts to compare
|
|
29
|
+
|
|
30
|
+
most used format: text
|
|
31
|
+
highest average impressions: carousel (3500.0)
|
|
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.
|
data/bin/post-format-mix
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require_relative '../lib/post_format_mix'
|
|
5
|
+
|
|
6
|
+
if ARGV.empty? || ARGV.include?('-h') || ARGV.include?('--help')
|
|
7
|
+
warn "usage: post-format-mix posts.csv [--min-posts 1]\n" \
|
|
8
|
+
"CSV columns: date (yyyy-mm-dd), format, impressions. More: https://www.onegtmlab.dev/glossary/personal-branding"
|
|
9
|
+
exit 2
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
begin
|
|
13
|
+
path, min_posts = PostFormatMix.parse_args(ARGV)
|
|
14
|
+
posts = PostFormatMix.from_csv(File.read(path))
|
|
15
|
+
print PostFormatMix.render(PostFormatMix.summarize(posts, min_posts))
|
|
16
|
+
rescue ArgumentError, Errno::ENOENT, Errno::EISDIR, CSV::MalformedCSVError => e
|
|
17
|
+
abort "error: #{e.message}"
|
|
18
|
+
end
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'csv'
|
|
4
|
+
require 'date'
|
|
5
|
+
|
|
6
|
+
# Show how a posting history splits across formats, and how each format performed.
|
|
7
|
+
module PostFormatMix
|
|
8
|
+
Post = Struct.new(:date, :format, :impressions, keyword_init: true)
|
|
9
|
+
|
|
10
|
+
def self.parse_date(text, row)
|
|
11
|
+
s = text.to_s.strip
|
|
12
|
+
m = s.match(/\A(\d{4})-(\d{2})-(\d{2})\z/)
|
|
13
|
+
raise ArgumentError, "row #{row}: bad date #{s.inspect}, expected yyyy-mm-dd" unless m && Date.valid_date?(m[1].to_i, m[2].to_i, m[3].to_i)
|
|
14
|
+
|
|
15
|
+
Date.new(m[1].to_i, m[2].to_i, m[3].to_i)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.parse_impressions(text, row)
|
|
19
|
+
s = text.to_s.strip
|
|
20
|
+
raise ArgumentError, "row #{row}: bad impressions #{s.inspect}, expected a whole number of 0 or more" unless s.match?(/\A\d+\z/)
|
|
21
|
+
|
|
22
|
+
s.to_i
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Row numbers count data rows, the first row under the header is row 1.
|
|
26
|
+
def self.from_csv(text)
|
|
27
|
+
table = CSV.parse(text, headers: true)
|
|
28
|
+
missing = %w[date format impressions] - table.headers.compact.map(&:strip)
|
|
29
|
+
raise ArgumentError, "missing column(s): #{missing.join(', ')}" unless missing.empty?
|
|
30
|
+
raise ArgumentError, 'no rows in the CSV' if table.empty?
|
|
31
|
+
|
|
32
|
+
table.each_with_index.map do |r, i|
|
|
33
|
+
row = i + 1
|
|
34
|
+
fmt = r['format'].to_s.strip
|
|
35
|
+
raise ArgumentError, "row #{row}: empty format" if fmt.empty?
|
|
36
|
+
|
|
37
|
+
Post.new(date: parse_date(r['date'], row), format: fmt, impressions: parse_impressions(r['impressions'], row))
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def self.median(values)
|
|
42
|
+
s = values.sort
|
|
43
|
+
mid = s.size / 2
|
|
44
|
+
s.size.odd? ? s[mid].to_f : (s[mid - 1] + s[mid]) / 2.0
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Formats sorted by posts desc, then name. The display name is the first spelling seen.
|
|
48
|
+
def self.summarize(posts, min_posts = 1)
|
|
49
|
+
groups = {}
|
|
50
|
+
posts.each { |p| (groups[p.format.downcase] ||= { name: p.format, posts: [] })[:posts] << p }
|
|
51
|
+
rows = groups.values.map do |g|
|
|
52
|
+
imps = g[:posts].map(&:impressions)
|
|
53
|
+
best = g[:posts].max_by { |p| [p.impressions, -p.date.jd] }
|
|
54
|
+
{ name: g[:name], posts: imps.size, share: imps.size * 100.0 / posts.size, total: imps.sum,
|
|
55
|
+
average: imps.sum.to_f / imps.size, median: median(imps), best_date: best.date,
|
|
56
|
+
comparable: imps.size >= min_posts }
|
|
57
|
+
end
|
|
58
|
+
rows.sort_by! { |r| [-r[:posts], r[:name].downcase] }
|
|
59
|
+
dates = posts.map(&:date)
|
|
60
|
+
{ posts: posts.size, first: dates.min, last: dates.max, total: posts.sum(&:impressions),
|
|
61
|
+
formats: rows, min_posts: min_posts,
|
|
62
|
+
most_used: leaders(rows, :posts), best_average: leaders(rows.select { |r| r[:comparable] }, :average) }
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Names of the rows holding the highest value of key, or nil when there are no rows.
|
|
66
|
+
def self.leaders(candidates, key)
|
|
67
|
+
return nil if candidates.empty?
|
|
68
|
+
|
|
69
|
+
top = candidates.map { |r| r[key] }.max
|
|
70
|
+
candidates.select { |r| r[key] == top }.map { |r| r[:name] }
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def self.pick(names)
|
|
74
|
+
names.size == 1 ? names.first : "tie (#{names.join(', ')})"
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def self.render(sum)
|
|
78
|
+
out = +"posts: #{sum[:posts]}\n"
|
|
79
|
+
out << "first post: #{sum[:first]}, last post: #{sum[:last]}\n"
|
|
80
|
+
out << "total impressions: #{sum[:total]}\n\n"
|
|
81
|
+
width = [sum[:formats].map { |r| r[:name].length }.max, 6].max
|
|
82
|
+
out << format("%-#{width}s %5s %7s %11s %8s %8s %s\n", 'format', 'posts', 'share', 'impressions', 'average', 'median', 'best post')
|
|
83
|
+
sum[:formats].each do |r|
|
|
84
|
+
note = r[:comparable] ? '' : ' too few posts to compare'
|
|
85
|
+
out << format("%-#{width}s %5d %6.1f%% %11d %8.1f %8.1f %s%s\n", r[:name], r[:posts], r[:share], r[:total],
|
|
86
|
+
r[:average], r[:median], r[:best_date], note)
|
|
87
|
+
end
|
|
88
|
+
out << "\nmost used format: #{pick(sum[:most_used])}\n"
|
|
89
|
+
if sum[:best_average]
|
|
90
|
+
out << "highest average impressions: #{pick(sum[:best_average])}"
|
|
91
|
+
out << " (#{format('%.1f', sum[:formats].find { |r| r[:name] == sum[:best_average].first }[:average])})\n"
|
|
92
|
+
else
|
|
93
|
+
out << "highest average impressions: no format has at least #{sum[:min_posts]} posts (--min-posts, set by you)\n"
|
|
94
|
+
end
|
|
95
|
+
out
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Parse the command line. Returns [path, min_posts]; raises ArgumentError on bad input.
|
|
99
|
+
def self.parse_args(argv)
|
|
100
|
+
args = argv.dup
|
|
101
|
+
path = nil
|
|
102
|
+
min_posts = 1
|
|
103
|
+
until args.empty?
|
|
104
|
+
arg = args.shift
|
|
105
|
+
case arg
|
|
106
|
+
when '--min-posts'
|
|
107
|
+
raise ArgumentError, "#{arg} needs a value" if args.empty?
|
|
108
|
+
|
|
109
|
+
val = args.shift
|
|
110
|
+
raise ArgumentError, "bad --min-posts #{val.inspect}, expected a whole number of 1 or more" unless val.match?(/\A\d+\z/) && val.to_i >= 1
|
|
111
|
+
|
|
112
|
+
min_posts = val.to_i
|
|
113
|
+
when /\A-/ then raise ArgumentError, "unknown option #{arg}"
|
|
114
|
+
else
|
|
115
|
+
raise ArgumentError, "unexpected extra argument #{arg.inspect}" if path
|
|
116
|
+
|
|
117
|
+
path = arg
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
raise ArgumentError, 'no CSV file given' unless path
|
|
121
|
+
|
|
122
|
+
[path, min_posts]
|
|
123
|
+
end
|
|
124
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: post-format-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 posts (date, format, impressions) show the posts per format, share of posts, total, average and median impressions and the best post date of each, the most used format and the format with the highest average. Offline."
|
|
29
|
+
executables:
|
|
30
|
+
- post-format-mix
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- LICENSE
|
|
35
|
+
- README.md
|
|
36
|
+
- bin/post-format-mix
|
|
37
|
+
- lib/post_format_mix.rb
|
|
38
|
+
homepage: "https://www.onegtmlab.dev/glossary/personal-branding"
|
|
39
|
+
licenses:
|
|
40
|
+
- MIT
|
|
41
|
+
metadata:
|
|
42
|
+
source_code_uri: "https://github.com/jaizendra-netizen/post-format-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: "Show the mix of post formats in a posting history, with posts, share and impressions per format"
|
|
62
|
+
test_files: []
|