Export All Mixpanel People to a JSON File

| Comments

Purpose

I needed to backup all my people data available in Mixpanel to a JSON file via the Mixpanel Export API.

Script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#==========================================================================================
#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 array
json_file.write('[')

def query_api(page = 0, session_id = nil)
  if(session_id)
    data = $client.request('engage', page: page, session_id: session_id)
  else
    data = $client.request('engage', page: page)
  end
end

# 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_page and this_page['results'].size > 0)
  next_page_number = this_page['page'].to_i + 1
  puts "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 array
json_file.write(']')

puts "Export done"

Gist

Available here. Please feel free to improve it !

More…