-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus_board_pages.rb
executable file
·67 lines (57 loc) · 1.63 KB
/
status_board_pages.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env ruby
# status_board_pages.rb
# Hilton Lipschitz
# Twitter/ADN: @hiltmon
# Web: http://www.hiltmon.com
# Use and modify freely, attribution appreciated
#
# Script to generate @panic status board files for Google Analytics web stats
#
# Run this regularly to update status board
#
# For how to set up, see http://www.hiltmon.com/blog/2013/04/10/top-pages-in-status-board/
# Include the gem
require 'rubygems'
require 'gattica'
require 'date'
require 'json'
require 'yaml'
DIR = File.expand_path File.dirname __FILE__
config = YAML.load_file "#{DIR}/config.yaml"
auth = config['auth']
meta = config['pages']
# Configuration
metrics = ['pageviews'] #, 'uniquePageviews', 'newVisits']
colors = ['red', 'green', 'blue']
# Login
ga = Gattica.new({
:email => auth['email'],
:password => auth['password']
})
# Get a list of accounts
accounts = ga.accounts
if meta['profile'] == nil
ga.profile_id = accounts.first.profile_id
else
ga.profile_id = meta['profile']
end
# Get the data
data = ga.get({
:start_date => Date.today.to_s.split('T')[0],
:end_date => Date.today.to_s.split('T')[0],
:dimensions => ['pageTitle'],
:metrics => metrics,
:sort => ['-pageviews']
})
# # Make the CSV file
File.open("#{meta['dir']}/#{meta['file']}.csv", "w") do |f|
f.write "20%, 80%\n"
count = 0
data.to_h['points'].each do |point|
the_page = point.to_h["dimensions"].first[:pageTitle].gsub(',', '').gsub(' - The Hiltmon', '')
the_data = point.to_h["metrics"].map { |e| e.values.first }
f.write the_data.join(',') + "," + the_page + "\n"
count += 1
break if count >= 20
end
end