Last active
August 29, 2015 13:56
-
-
Save evidanary/9198492 to your computer and use it in GitHub Desktop.
Class file to download mixpanel data and convert to CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mixpanel_client' | |
require 'csv' | |
require 'multi_json' | |
require 'net/http' | |
class MixpanelExtract | |
attr_reader :filename, :request_uri | |
def initialize(config, for_date) | |
@client = Mixpanel::Client.new(config[:mixpanel_auth]) | |
@staging_folder = config[:staging_folder] | |
@event_name = config[:event_name] | |
@for_date = for_date | |
@filename = "#{@staging_folder}/#{@event_name}.events-#{@for_date}.json" | |
@extract_list = config[:extract_list].split(',') | |
@request_uri = @client.request_uri('export', params) | |
@limit = config[:limit] | |
end | |
def params | |
params = { | |
event: '["' + @event_name + '"]', | |
from_date: @for_date, | |
to_date: @for_date, | |
} | |
params[:Limit] = @limit if(@limit != nil) | |
params | |
end | |
def process_date | |
end_point = 'data.mixpanel.com' | |
resource = @request_uri[/#{end_point}\K.*/] | |
puts "End Point: #{end_point}" | |
puts "Resource: #{resource}" | |
Net::HTTP.start(end_point, { :use_ssl => true }) do |http| | |
http.request_get(resource) do |response| | |
open @filename, 'w' do |io| | |
response.read_body do |chunk| | |
io.write chunk | |
end | |
end | |
end | |
end | |
end | |
def flatten_event_properties(jevent) | |
new_event = {} | |
new_event["event"] = jevent["event"] | |
new_event.merge!(jevent["properties"]) | |
new_event | |
end | |
def generate_headers(row_hash) | |
row_hash.keys | |
end | |
def transform(file_in, file_out) | |
CSV.open(file_out,"wb", {:col_sep => "\t"}) do |csv| | |
File.open(file_in).each do |line| | |
flattened_line = flatten_event_properties(MultiJson.load(line)) | |
data = [] | |
@extract_list.each do |k| | |
data << flattened_line[k] | |
end | |
csv << data | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment