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'
OPTIONS =

Availalbe options for a Mixpanel API request

[:resource, :event, :funnel_id, :name, :type, :unit, :interval, :limit, :format, :bucket,
:values, :from_date, :to_date, :on, :where, :buckets, :timezone]
VERSION =

Mixpanel::Client library version

'2.2.0'

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’



37
38
39
40
# File 'lib/mixpanel/client.rb', line 37

def initialize(config)
  @api_key    = config['api_key']
  @api_secret = config['api_secret']
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#api_secretObject

Returns the value of attribute api_secret.



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

def api_secret
  @api_secret
end

#uriObject (readonly)

Returns the value of attribute uri.



14
15
16
# File 'lib/mixpanel/client.rb', line 14

def uri
  @uri
end

Instance Method Details

#request(&options) ⇒ JSON, String

Return mixpanel data as a JSON object or CSV string

Examples:

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

Parameters:

  • options (Block)

    variables used to make a specific request for mixpanel data

Returns:

  • (JSON, String)

    mixpanel response as a JSON object or CSV string



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

def request(&options)
  reset_options
  instance_eval(&options)
  @uri = URI.mixpanel(resource, normalize_params(params))
  response = URI.get(@uri)
  Utils.to_hash(response, @format)
end