class Mixpanel::Client

Return metrics from Mixpanel Data API

Utility methods for Mixpanel::Client

Return metrics from Mixpanel Data API

Constants

BASE_URI
DATA_URI
IMPORT_URI
VERSION

Mixpanel::Client library version

Attributes

api_secret[RW]
parallel[RW]
timeout[RW]
uri[R]

Public Class Methods

base_uri_for_resource(resource) click to toggle source
# File lib/mixpanel/client.rb, line 19
def self.base_uri_for_resource(resource)
  if resource == 'export'
    DATA_URI
  elsif resource == 'import'
    IMPORT_URI
  else
    BASE_URI
  end
end
new(config) click to toggle source

Configure the client

@example

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

@param [Hash] config consisting of an 'api_secret' and additonal options

# File lib/mixpanel/client.rb, line 36
def initialize(config)
  @api_secret = config[:api_secret]
  @parallel   = config[:parallel] || false
  @timeout    = config[:timeout] || nil

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

Public Instance Methods

hydra() click to toggle source
# File lib/mixpanel/client.rb, line 142
def hydra
  @hydra ||= ::Typhoeus::Hydra.new
end
make_normal_request(resource) click to toggle source
# File lib/mixpanel/client.rb, line 75
def make_normal_request(resource)
  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
make_parallel_request() click to toggle source
# File lib/mixpanel/client.rb, line 68
def make_parallel_request
  require 'typhoeus'
  parallel_request = prepare_parallel_request
  hydra.queue parallel_request
  parallel_request
end
prepare_parallel_request() click to toggle source

TODO: Extract and refactor rubocop:disable MethodLength

# File lib/mixpanel/client.rb, line 111
def prepare_parallel_request
  request = ::Typhoeus::Request.new(@uri, userpwd: "#{@api_secret}:")

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

      raise HTTPError, error_message
    end
  end

  request
end
request(resource, options) click to toggle source

Return mixpanel data as a JSON object or CSV string

@example

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

@resource [String] mixpanel api resource endpoint @options [Hash] options variables used to make a specific request for

mixpanel data

@return [JSON, String] mixpanel response as a JSON object or CSV string

# File lib/mixpanel/client.rb, line 63
def request(resource, options)
  @uri = request_uri(resource, options)
  @parallel ? make_parallel_request : make_normal_request(resource)
end
request_uri(resource, options = {}) click to toggle source

Return mixpanel URI to the data

@example

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

@resource [String] mixpanel api resource endpoint @options [Hash] options variables used to make a specific request for

mixpanel data

@return [JSON, String] mixpanel response as a JSON object or CSV string

# File lib/mixpanel/client.rb, line 104
def request_uri(resource, options = {})
  @format = options[:format] || :json
  URI.mixpanel(resource, normalize_options(options))
end
run_parallel_requests() click to toggle source

rubocop:enable MethodLength

# File lib/mixpanel/client.rb, line 138
def run_parallel_requests
  hydra.run
end

Private Instance Methods

normalize_options(options) click to toggle source

Return a hash of options along with defaults and a generated signature

@return [Hash] collection of options including defaults and generated

signature
# File lib/mixpanel/client.rb, line 152
def normalize_options(options)
  normalized_options = options.dup
  normalized_options.merge!(format: @format)
end