This gem manages all the stats related to games: CTR calculations for game thumbs (clicks/impressions) - for homepage ordering and Similar Games stats
Your app should contain a Game model
- Add game stats gem to your Gemfile:
gem 'game_stats', :git => '[email protected]:playedonline/game_stats.git'then run:
$ bundle install- In order to create the game_stats table, run:
$ bundle exec rake game_stats:install:migrationsnote that it creates a migration file in your db/migrate folder - remember to add it to git!
Then run:
$ bundle exec rake db:migrateto run the migration.
-> if you already have existing games, you might need to write a migration to create stats for them, for example:
class InitializeGameStatsForExistingGames < ActiveRecord::Migration
def change
execute 'DELETE FROM game_stats'
execute 'INSERT INTO game_stats(game_id, similar_game_id, impressions, clicks)
SELECT 0, games.id, 0, 0
FROM games'
Game.all.each do |game|
execute "INSERT INTO game_stats(game_id, similar_game_id, impressions, clicks)
SELECT #{game.id}, games.id, 2, 2
FROM games
WHERE games.id <> #{game.id}"
end
end
end- GameStats gem allows you to to set the following configuration values:
auto_populate_similar_games: Determines if we auto populate stats for similar games when adding a new game (default = true)add_homepage_stats_as_game_zero: Determines if we add an entry for homepage with every new game as similar game, for homepage stats (default = false)minutes_to_expire_similar_games_cache: How long should we keep the similar games result in cache, in minutes (default = 5 minutes)
in order to change the configuration values, add an initializer (under config/initializers) - you can name it game_stats.rb and in it add the values you want:
GameStats::auto_populate_similar_games = false
GameStats::add_homepage_stats_as_game_zero = true
GameStats::minutes_to_expire_similar_games_cache = 10- Add to
routes.rb:
mount GameStats::Engine => "/game_stats"- Add to
application.js:
//= require game_stats/game_stats_client_apiFrom any javascript file (usually from homepage or game page), use:
GameStats.record_impressions(game_id, similar_game_ids);similar_game_ids is an array of game ids.
From any javascript file (usually from homepage or game page), use:
GameStats.record_click(game_id, similar_game_id);Then in the relevant controller (usually game#show action) call:
collect_click_dataFrom any controller, you can call the method get_similar_games:
def get_similar_games(game_id, amount, games_cache_key = nil)
...games_cache_key is a key used to expire the cache in case there was a change in games data (i.e game added, became invisible etc)
Remember you have to db:migrate when deploying the new code