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'.freeze
DATA_URI =
'https://data.mixpanel.com/api/2.0'.freeze
IMPORT_URI =
'https://api.mixpanel.com'.freeze
VERSION =

Mixpanel::Client library version

'5.1.0'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Configure the client

Examples:

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

Parameters:

  • config (Hash)

    consisting of an ‘api_secret’ and additonal options

Raises:



36
37
38
39
40
41
42
43
44
# File 'lib/mixpanel/client.rb', line 36

def initialize(config)
  @api_secret  = config[:api_secret]
  @timeout     = config[:timeout]    || nil
  @@base_uri   = config[:base_uri]   || nil
  @@data_uri   = config[:data_uri]   || nil
  @@import_uri = config[:import_uri] || nil

  raise ConfigurationError, 'api_secret is required' if @api_secret.nil?
end

Instance Attribute Details

#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

#timeoutObject

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

#uriObject (readonly)

Returns the value of attribute uri.



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

def uri
  @uri
end

Class Method Details

.base_uri_for_resource(resource) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/mixpanel/client.rb', line 19

def self.base_uri_for_resource(resource)
  if resource == 'export'
    @@data_uri ? @@data_uri : DATA_URI
  elsif resource == 'import'
    @@import_uri ? @@import_uri : IMPORT_URI
  else
    @@base_uri ? @@base_uri : BASE_URI
  end
end

Instance Method Details

#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



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

def request(resource, options)
  @uri = request_uri(resource, options)

  response = URI.get(@uri, @timeout, @api_secret)

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

  Utils.to_hash(response, @format)
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



96
97
98
99
# File 'lib/mixpanel/client.rb', line 96

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