#==========================================================================================#title :mixpanel_people_export.rb#description :This ruby script is exporting mixpanel people json data to a file#author :Quentin Rousseau <[email protected]>#date :2014-07-15#version :1.0#usage :ruby mixpanel_people_export.rb#dependencies :gem install 'mixpanel_client'#moreinfo :https://mixpanel.com/docs/api-documentation/data-export-api#engage-default#===========================================================================================require'rubygems'require'mixpanel_client'API_KEY='YOUR_API_KEY'API_SECRET='YOUR_API_SECRET'NAME_FILE='mixpanel_people_export.json'$client=Mixpanel::Client.new(api_key:API_KEY,api_secret:API_SECRET)json_file=File.open(NAME_FILE,'a')# Open json arrayjson_file.write('[')defquery_api(page=0,session_id=nil)if(session_id)data=$client.request('engage',page:page,session_id:session_id)elsedata=$client.request('engage',page:page)endend# Get the first page of data associated with our selector expression# this_page = query_api(page=NEXT_PAGE)# do_something_with_response(this_page)this_page=query_api(0)json_file.write(this_page.to_json)# If we get fewer records than the page_sized returned with our results,# then there are no more records to get. Otherwise, keep querying for additional pages.# while (length of this_page.results) >= this_page.page_size:# next_page_number = this_page.page + 1# this_page = query_api(page=next_page_number, session_id=this_page.session_id)# do_something_with_response(this_page)while(this_pageandthis_page['results'].size>0)next_page_number=this_page['page'].to_i+1puts"Fetching next_page : #{next_page_number}"this_page=query_api(next_page_number,this_page['session_id'])json_file.write(","+this_page.to_json)end# Close json arrayjson_file.write(']')puts"Export done"