Class: Mixpanel::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/mixpanel/utils.rb,
lib/mixpanel/client.rb,
lib/mixpanel/version.rb

Overview

Return metrics from Mixpanel Data API

Defined Under Namespace

Modules: Utils

Constant Summary collapse

BASE_URI =
'https://mixpanel.com/api/2.0'
DATA_URI =
'https://data.mixpanel.com/api/2.0'
IMPORT_URI =
'https://api.mixpanel.com'
VERSION =

Mixpanel::Client library version

'4.1.2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Configure the client

Examples:

config = {api_key: '123', api_secret: '456'}
client = Mixpanel::Client.new(config)

Parameters:

  • config (Hash)

    consisting of an ‘api_key’ and an ‘api_secret’



26
27
28
29
30
31
32
# File 'lib/mixpanel/client.rb', line 26

def initialize(config)
  @api_key    = config[:api_key]
  @api_secret = config[:api_secret]
  @parallel   = config[:parallel]   || false

  fail ConfigurationError if @api_key.nil? || @api_secret.nil?
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



17
18
19
# File 'lib/mixpanel/client.rb', line 17

def api_key
  @api_key
end

#api_secretObject

Returns the value of attribute api_secret.



17
18
19
# File 'lib/mixpanel/client.rb', line 17

def api_secret
  @api_secret
end

#parallelObject

Returns the value of attribute parallel.



17
18
19
# File 'lib/mixpanel/client.rb', line 17

def parallel
  @parallel
end

#uriObject (readonly)

Returns the value of attribute uri.



16
17
18
# File 'lib/mixpanel/client.rb', line 16

def uri
  @uri
end

Instance Method Details

#hydraObject



131
132
133
# File 'lib/mixpanel/client.rb', line 131

def hydra
  @hydra ||= ::Typhoeus::Hydra.new
end

#make_normal_request(resource) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/mixpanel/client.rb', line 65

def make_normal_request(resource)
  response = URI.get(@uri)

  if %w(export import).include?(resource) && @format != 'raw'
    response = %Q([#{response.split("\n").join(',')}])
  end

  Utils.to_hash(response, @format)
end

#make_parallel_requestObject



58
59
60
61
62
63
# File 'lib/mixpanel/client.rb', line 58

def make_parallel_request
  require 'typhoeus'
  parallel_request = prepare_parallel_request
  hydra.queue parallel_request
  parallel_request
end

#prepare_parallel_requestObject

rubocop:disable MethodLength



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/mixpanel/client.rb', line 100

def prepare_parallel_request
  request = ::Typhoeus::Request.new(@uri)

  request.on_complete do |response|
    if response.success?
      Utils.to_hash(response.body, @format)
    elsif response.timed_out?
      fail TimeoutError
    elsif response.code == 0
      # Could not get an http response, something's wrong
      fail HTTPError, response.curl_error_message
    else
      # Received a non-successful http response
      if response.body && response.body != ''
        error_message = JSON.parse(response.body)['error']
      else
        error_message = response.code.to_s
      end

      fail HTTPError, error_message
    end
  end

  request
end

#request(resource, options) ⇒ JSON, String

Return mixpanel data as a JSON object or CSV string

Examples:

data = client.request(
  'events/properties',
  event:    '["test-event"]',
  name:     'hello',
  values:   '["uno", "dos"]',
  type:     'general',
  unit:     'hour',
  interval: 24,
  limit:    5,
  bucket:   'contents'
)

Returns:

  • (JSON, String)

    mixpanel response as a JSON object or CSV string



53
54
55
56
# File 'lib/mixpanel/client.rb', line 53

def request(resource, options)
  @uri = request_uri(resource, options)
  @parallel ? make_parallel_request : make_normal_request(resource)
end

#request_uri(resource, options) ⇒ JSON, String

Return mixpanel URI to the data

Examples:

uri = client.request_uri(
  'events/properties',
  event:    '["test-event"]',
  name:     'hello',
  values:   '["uno", "dos"]',
  type:     'general',
  unit:     'hour',
  interval: 24,
  limit:    5,
  bucket:   'contents'
)

Returns:

  • (JSON, String)

    mixpanel response as a JSON object or CSV string



94
95
96
97
# File 'lib/mixpanel/client.rb', line 94

def request_uri(resource, options)
  @format = options[:format] || :json
  URI.mixpanel(resource, normalize_options(options))
end

#run_parallel_requestsObject

rubocop:enable MethodLength



127
128
129
# File 'lib/mixpanel/client.rb', line 127

def run_parallel_requests
  hydra.run
end