Skip to content

Commit

Permalink
Add mixpanel for tracking event
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleconroy committed Nov 18, 2012
1 parent 7b29027 commit 2e37b47
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ src/maps/*.lua
venv
tmx2lua*
release.md
src/conf.lua.bak
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ previous_version = $(shell python scripts/version.py previous)

love: maps
mkdir -p build
cp src/conf.lua src/conf.lua.bak
python scripts/release_conf.py
cd src && zip -r ../build/hawkthorne.love . -x ".*" \
-x ".DS_Store" -x "*/full_soundtrack.ogg"
-x ".DS_Store" -x "*/full_soundtrack.ogg" -x "*/conf.lua.bak"
mv src/conf.lua.bak src/conf.lua

maps: $(patsubst %.tmx,%.lua,$(wildcard src/maps/*.tmx))
positions: $(patsubst %.png,%.lua,$(wildcard src/positions/*.png))
Expand Down
12 changes: 12 additions & 0 deletions scripts/release_conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import os

with open("src/conf.lua.bak") as f:
conf = f.read()

conf = conf.replace("t.release = false",
"t.release = true")
conf = conf.replace("ac1c2db50f1332444fd0cafffd7a5543",
os.environ["MIXPANEL_TOKEN"])

with open("src/conf.lua", "w") as g:
g.write(conf)
2 changes: 2 additions & 0 deletions src/conf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ function love.conf(t)
t.modules.physics = false
t.modules.joystick = false
t.release = false
-- custom variables
t.mixpanel = "ac1c2db50f1332444fd0cafffd7a5543"
end
17 changes: 15 additions & 2 deletions src/main.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local correctVersion = require 'correctversion'

if correctVersion then

require 'utils'
Expand All @@ -12,19 +13,31 @@ if correctVersion then
local controls = require 'controls'
local hud = require 'hud'
local cli = require 'vendor/cliargs'
local mixpanel = require 'vendor/mixpanel'
local character = require 'character'

-- XXX Hack for level loading
Gamestate.Level = Level

-- will hold the currently playing sources
-- Get the current version of the game
local function getVersion()
return split(love.graphics.getCaption(), "v")[2]
end

local function getConfiguration()
local t = {modules = {}, screen = {}}
love.conf(t)
return t
end

function love.load(arg)
table.remove(arg, 1)
local state = 'splash'
local conf = getConfiguration()

-- SCIENCE!
love.thread.newThread('ping', 'ping.lua'):start()
mixpanel.init(conf.mixpanel)
mixpanel.track('game.opened', {version=getVersion()})

-- set settings
local options = require 'options'
Expand Down
2 changes: 0 additions & 2 deletions src/ping.lua

This file was deleted.

22 changes: 22 additions & 0 deletions src/vendor/mixpanel.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "vendor/json"

local mixpanel = {}
local thread = nil
local token = nil

function mixpanel.init(t)
thread = love.thread.newThread("mixpanel", "vendor/mixpanel_thread.lua")
thread:start()
token = t
end

function mixpanel.track(event, data)
assert(thread, "Can't find the mixpanel thread")

local data = data or {}
data.token = token

thread:set("point", json.encode({event=event, properties=data}))
end

return mixpanel
10 changes: 10 additions & 0 deletions src/vendor/mixpanel_thread.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
local http = require("socket.http")
local mime = require("mime")

local thread = love.thread.getThread()

while true do
local point = mime.b64(thread:demand("point"))
local url = "http://api.mixpanel.com/track/?data=" .. point .. "&ip=1"
local b, c, h = http.request(url)
end

0 comments on commit 2e37b47

Please sign in to comment.