Skip to content

Instantly share code, notes, and snippets.

@goodfeel
Last active August 16, 2017 04:06
Show Gist options
  • Save goodfeel/cf17389938ba28f8084a to your computer and use it in GitHub Desktop.
Save goodfeel/cf17389938ba28f8084a to your computer and use it in GitHub Desktop.
Ruby class for Download Aggregrated Data from GameAnalytics.com
# Communicate with GameAnalytics.com to download aggragrated data.
# Author: Jesdakorn S. (@thet0ny / http://www.ton.in.th / http://zaqqle.com)
require 'rubygems'
require 'active_support/core_ext/date'
require 'active_support/core_ext/numeric/time' # Date time helper (when use outside Rails)
require 'HTTParty' # gem install httparty
require 'JWT' # gem install jwt
class GameAnalytic
attr_accessor :game_id, :api_key
API_BASE_URL = 'https://query.gameanalytics.com'
def initialize(game_id, data_api_key)
self.game_id = game_id
self.api_key = data_api_key
@game_path = "/v1/games/#{game_id}/"
end
def get_dau
metric = 'core/DAU/'
start_time = (Date.today.to_time - 1.day).to_i
end_time = (start_time + 24.hours).to_i
aggr = 'sum'
query = "#{metric}?start=#{start_time}&end=#{end_time}&aggregation=#{aggr}"
api_path = @game_path + query
full_path = API_BASE_URL + api_path
HTTParty.get full_path, :headers => { "Authorization" => generate_signature(api_path) }
end
private
def generate_signature(path)
JWT.encode({"url" => path}, self.api_key)
end
end
ga = GameAnalytic.new("YOUR GAME ID", "YOUR DATA API KEY")
puts ga.get_dau
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment