game_analytics 0.0.1
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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +56 -0
- data/Rakefile +1 -0
- data/game_analytics.gemspec +26 -0
- data/lib/game_analytics/client.rb +45 -0
- data/lib/game_analytics/common.rb +21 -0
- data/lib/game_analytics/metric.rb +41 -0
- data/lib/game_analytics/version.rb +5 -0
- data/lib/game_analytics/worker.rb +35 -0
- data/lib/game_analytics.rb +25 -0
- metadata +122 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 William Lipa
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# GameAnalytics
|
2
|
+
|
3
|
+
The GameAnalytics gem saves metrics data to gameanalytics.com. It is lightweight and performs the metrics
|
4
|
+
update on a separate worker thread to avoid interfering with normal request processing.
|
5
|
+
|
6
|
+
The gem prioritizes avoiding disruption to the host application over rigorously saving all
|
7
|
+
metrics data whatever the circumstances. That is, if there are problems talking to gameanalytics.com,
|
8
|
+
some metrics data may be dropped, but the host application should continue on as normal.
|
9
|
+
|
10
|
+
Metrics data is held in memory in the worker process until it is either saved on gameanalytics.com or
|
11
|
+
dropped due to falling behind. The latter should be an unusual circumstance when gameanalytics.com is
|
12
|
+
available.
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
gem 'game_analytics'
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install game_analytics
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
Configure the gem with your GameAnalytics keys by placing code like the following in
|
31
|
+
your initializer:
|
32
|
+
|
33
|
+
GameAnalytics.config(
|
34
|
+
:game_key => '123451234512345123451234512345',
|
35
|
+
:secret_key => '123451234512345123451234512345'
|
36
|
+
)
|
37
|
+
|
38
|
+
In your application, create Metric objects of the appropriate GameAnalytics types
|
39
|
+
(Design, Business, Quality, or User), and send them to the service:
|
40
|
+
|
41
|
+
m = GameAnalytics::Metric::Business.new(:user_id => '-100', :session_id => '-100',
|
42
|
+
:build => 'development', :message => 'test')
|
43
|
+
GameAnalytics.client.enqueue m
|
44
|
+
|
45
|
+
You can also send arrays of objects in a single service request:
|
46
|
+
|
47
|
+
GameAnalytics.client.enqueue [m1, m2]
|
48
|
+
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
1. Fork it
|
53
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
54
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
55
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
56
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'game_analytics/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "game_analytics"
|
8
|
+
spec.version = GameAnalytics::VERSION
|
9
|
+
spec.authors = ["wlipa"]
|
10
|
+
spec.email = ["dojo@masterleep.com"]
|
11
|
+
spec.description = %q{Lightweight and non-disruptive interface to save metrics data to gameanalytics.com}
|
12
|
+
spec.summary = %q{saves metrics data to gameanalytics.com}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency('httpclient')
|
22
|
+
spec.add_dependency('rails')
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module GameAnalytics
|
2
|
+
class Client
|
3
|
+
|
4
|
+
include Common
|
5
|
+
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@queue = Queue.new
|
9
|
+
@worker_mutex = Mutex.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def enqueue(metric)
|
13
|
+
return if disabled
|
14
|
+
ensure_worker_running
|
15
|
+
@queue << metric
|
16
|
+
nil
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def ensure_worker_running
|
23
|
+
return if worker_running?
|
24
|
+
@worker_mutex.synchronize do
|
25
|
+
return if worker_running?
|
26
|
+
start_worker
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def worker_running?
|
31
|
+
@worker_thread && @worker_thread.alive?
|
32
|
+
end
|
33
|
+
|
34
|
+
def start_worker
|
35
|
+
@worker_thread = Thread.new do
|
36
|
+
begin
|
37
|
+
Worker.new(@queue).run
|
38
|
+
rescue => ex
|
39
|
+
logger.info "GameAnalytics worker thread exception: #{ex}"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module GameAnalytics
|
2
|
+
module Common
|
3
|
+
|
4
|
+
def logger
|
5
|
+
GameAnalytics.logger
|
6
|
+
end
|
7
|
+
|
8
|
+
def options
|
9
|
+
GameAnalytics.options
|
10
|
+
end
|
11
|
+
|
12
|
+
def client
|
13
|
+
GameAnalytics.client
|
14
|
+
end
|
15
|
+
|
16
|
+
def disabled
|
17
|
+
GameAnalytics.disabled
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module GameAnalytics
|
2
|
+
class Metric
|
3
|
+
|
4
|
+
include Common
|
5
|
+
|
6
|
+
|
7
|
+
def initialize(data={})
|
8
|
+
@data = data
|
9
|
+
needs = required_keys - data.keys
|
10
|
+
raise "missing required fields #{needs}" unless needs.empty?
|
11
|
+
end
|
12
|
+
|
13
|
+
def as_json(options={})
|
14
|
+
@data
|
15
|
+
end
|
16
|
+
|
17
|
+
def required_keys
|
18
|
+
[:user_id, :session_id, :build, :event_id]
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
class Design < Metric
|
23
|
+
end
|
24
|
+
|
25
|
+
class User < Metric
|
26
|
+
end
|
27
|
+
|
28
|
+
class Business < Metric
|
29
|
+
|
30
|
+
def required_keys
|
31
|
+
super + [:currency, :amount]
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
class Quality < Metric
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'digest/md5'
|
2
|
+
require 'httpclient'
|
3
|
+
|
4
|
+
module GameAnalytics
|
5
|
+
class Worker
|
6
|
+
|
7
|
+
include Common
|
8
|
+
|
9
|
+
|
10
|
+
def initialize(q)
|
11
|
+
@queue = q
|
12
|
+
@http = HTTPClient.new
|
13
|
+
@url_base = "http://api.gameanalytics.com/1/#{options[:game_key]}"
|
14
|
+
end
|
15
|
+
|
16
|
+
def process(unit)
|
17
|
+
klass = unit.is_a?(Array) ? unit.first.class : unit.class
|
18
|
+
category = klass.name.demodulize.downcase
|
19
|
+
json_data = unit.to_json
|
20
|
+
hd = Digest::MD5.hexdigest(json_data + options[:secret_key])
|
21
|
+
url = "#{@url_base}/#{category}"
|
22
|
+
logger.info "GameAnalytics <: #{url} #{json_data} #{hd}"
|
23
|
+
resp = @http.post(url, :body => json_data, :header => { 'Authorization' => hd })
|
24
|
+
logger.info "GameAnalytics >: #{resp.content} (#{resp.status})"
|
25
|
+
end
|
26
|
+
|
27
|
+
def run
|
28
|
+
logger.info "GameAnalytics worker running"
|
29
|
+
loop do
|
30
|
+
process @queue.pop
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'game_analytics/common'
|
2
|
+
require 'game_analytics/client'
|
3
|
+
require 'game_analytics/metric'
|
4
|
+
require 'game_analytics/version'
|
5
|
+
require 'game_analytics/worker'
|
6
|
+
|
7
|
+
|
8
|
+
module GameAnalytics
|
9
|
+
|
10
|
+
class << self
|
11
|
+
attr_accessor :options, :logger, :disabled
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
def self.config(opts)
|
16
|
+
@options = opts
|
17
|
+
@logger = opts[:logger] || Rails.logger
|
18
|
+
@disabled = opts[:disabled]
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.client
|
22
|
+
@client ||= Client.new
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: game_analytics
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- wlipa
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-08-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: httpclient
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rails
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: bundler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.3'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.3'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Lightweight and non-disruptive interface to save metrics data to gameanalytics.com
|
79
|
+
email:
|
80
|
+
- dojo@masterleep.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE.txt
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- game_analytics.gemspec
|
91
|
+
- lib/game_analytics.rb
|
92
|
+
- lib/game_analytics/client.rb
|
93
|
+
- lib/game_analytics/common.rb
|
94
|
+
- lib/game_analytics/metric.rb
|
95
|
+
- lib/game_analytics/version.rb
|
96
|
+
- lib/game_analytics/worker.rb
|
97
|
+
homepage: ''
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ! '>='
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
requirements: []
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 1.8.25
|
119
|
+
signing_key:
|
120
|
+
specification_version: 3
|
121
|
+
summary: saves metrics data to gameanalytics.com
|
122
|
+
test_files: []
|