gaffel 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/gaffel.rb +118 -0
  2. metadata +47 -0
data/lib/gaffel.rb ADDED
@@ -0,0 +1,118 @@
1
+ #noinspection RubyStringKeysInHashInspection
2
+ class Gaffel
3
+ # --- login:
4
+ CLIENT_LOGIN_URL = 'https://www.google.com/accounts/ClientLogin'
5
+ ACCOUNT_TYPE = 'GOOGLE'
6
+ SERVICE = 'analytics'
7
+ CONTENT_TYPE = 'application/x-www-form-urlencoded'
8
+ LOGIN_HEADERS = {'Content-Type' => CONTENT_TYPE}
9
+ # --- get request:
10
+ GA_CORE_REPORTING_API_URL_V2_3 = 'https://www.google.com/analytics/feeds/data'
11
+ GA_CORE_REPORTING_API_URL_V2_4 = 'https://www.googleapis.com/analytics/v2.4/data'
12
+ GA_CORE_REPORTING_API_URL_V3_0 = 'https://www.googleapis.com/analytics/v3/data/ga'
13
+ GA_CORE_REPORTING_API_URL = GA_CORE_REPORTING_API_URL_V3_0
14
+
15
+ def initialize(account, data_spec)
16
+ @debug = false
17
+ @account = account
18
+ @data_spec = {:metric => (data_spec[:metric] || 'ga:newVisits'),
19
+ :dimensions => (data_spec[:dimensions] || []).join(','),
20
+ :filters => (data_spec[:filters] || ''),
21
+ :start_date => iso_date(data_spec[:start_date]),
22
+ :end_date => iso_date(data_spec[:end_date]),
23
+ :max_results => (data_spec[:max_results] || 1000),
24
+ :sort => (data_spec[:sort] || 'ga:date')}
25
+ @data_spec[:filters] += ";#{@data_spec[:metric]}>0"
26
+ @login_query = {:Email => @account[:login],
27
+ :Passwd => @account[:password],
28
+ :source => @account[:source],
29
+ :accountType => ACCOUNT_TYPE,
30
+ :service => SERVICE}.to_query
31
+ @headers_for_get_request = {"GData-Version" => '2',
32
+ "Content-Type" => CONTENT_TYPE}
33
+ @rows_processed = 0
34
+ end
35
+
36
+ def each
37
+ get_auth_token
38
+ start_index = 1
39
+ while true
40
+ url = make_url(start_index)
41
+ debug("url", url)
42
+ response = http_get_request(url)
43
+ debug("response", response)
44
+ ga_data = JSON.parse(response.read_body)
45
+ debug("ga_data", ga_data)
46
+ break if done_reading ga_data
47
+ headers = ga_data['columnHeaders']
48
+ ga_data['rows'].each { |values| yield make_hash(headers, values) }
49
+ start_index += @data_spec[:max_results]
50
+ end
51
+ end
52
+
53
+ # ---
54
+ protected
55
+
56
+ def make_hash(headers, values)
57
+ hash = {}
58
+ headers.each_with_index do |header, i|
59
+ hash = hash.merge({header['name'].to_sym => values[i]})
60
+ end
61
+ hash
62
+ end
63
+
64
+ def done_reading(ga_data)
65
+ ga_data["query"]["start-index"] > ga_data["totalResults"]
66
+ end
67
+
68
+ def http_get_request(url)
69
+ http = Net::HTTP.new url.host, 443
70
+ http.use_ssl = url.scheme == 'https'
71
+ http.request_get(url.request_uri, @headers_for_get_request)
72
+ end
73
+
74
+ def get_auth_token
75
+ url = URI CLIENT_LOGIN_URL
76
+ http = Net::HTTP.new url.host, 443
77
+ http.use_ssl = url.scheme == 'https'
78
+ response = http.request_post(url.request_uri, @login_query, LOGIN_HEADERS)
79
+ auth = response.read_body.scan(/Auth=.*/).first
80
+ @headers_for_get_request = @headers_for_get_request.merge("Authorization" => "GoogleLogin #{auth}")
81
+ end
82
+
83
+ def make_url(start_index)
84
+ #see: http://ga-dev-tools.appspot.com/explorer/
85
+ query = {"start-index" => start_index.to_s,
86
+ "metrics" => @data_spec[:metric],
87
+ "dimensions" => @data_spec[:dimensions],
88
+ "filters" => @data_spec[:filters],
89
+ "max-results" => @data_spec[:max_results],
90
+ "start-date" => @data_spec[:start_date],
91
+ "end-date" => @data_spec[:end_date],
92
+ "sort" => @data_spec[:sort],
93
+ "key" => @account[:google_api_key],
94
+ "ids" => @account[:ga_profile_id]}.to_query
95
+ #debug("query", query)
96
+ URI GA_CORE_REPORTING_API_URL + '?' + query
97
+ end
98
+
99
+ def iso_date(date_id)
100
+ year = date_id / 10000
101
+ month = "%02d" % ((date_id / 100) % 100)
102
+ day = "%02d" % (date_id % 100)
103
+ "#{year}-#{month}-#{day}"
104
+ end
105
+
106
+ def debug(msg, data)
107
+ if @debug
108
+ print "\nDEBUG: #{msg}: "
109
+ if data.respond_to?(:size)
110
+ ap data
111
+ puts "(size: #{data.size}): "
112
+ else
113
+ print "#{data}\n"
114
+ end
115
+ end
116
+ end
117
+
118
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gaffel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tilmann Bruckhaus
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-10 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Supports extracting 1 metric for 0 or more dimensions, with start and
15
+ end dates, sorting, and max results requested per Google API call. Provides results
16
+ record-by-record via an iterator.
17
+ email: tilmann@bruckha.us
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/gaffel.rb
23
+ homepage: http://rubygems.org/gems/gaffel
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.15
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: Extract data from Google Analytics (GA) v3.0 API!
47
+ test_files: []