Sha256: 0b6af63871f270095721d7e89d07a5ba7b0ab156d4feff027206ecf0975f375e

Contents?: true

Size: 1.85 KB

Versions: 3

Compression:

Stored size: 1.85 KB

Contents

require "open-uri"
require 'base64'
require 'json'
require 'thread'

module Mixpanel
  class Tracker
    require 'mixpanel/async'
    require 'mixpanel/event'
    require 'mixpanel/person'
  
    extend Mixpanel::Async
    include Mixpanel::Event
    include Mixpanel::Person
  
    def initialize(token, options={})
      @token = token
      @async = !!options.fetch(:async, false)
      @persist = !!options.fetch(:persist, false)
      @env = options.fetch :env, {}
      @api_key = options.fetch :api_key, nil
    
      # Make sure queue object is instantiated to an array.  If not persisted, set queue object to empty array.
      if @persist
        @env['rack.session'] ||= {}
        @env['rack.session']['mixpanel_events'] ||= []
      else
        @env['mixpanel_events'] = []
      end
    end
  
    def queue
      @persist ? @env['rack.session']['mixpanel_events'] : @env['mixpanel_events']
    end
  
    def append(type, *args)
      queue << [type, args.collect {|arg| arg.to_json}]
    end
  
    protected
  
    # Walk through each property and see if it is in the special_properties.  If so, change the key to have a $ in front of it.
    def properties_hash(properties, special_properties)
      properties.inject({}) do |props, (key, value)|
        key = "$#{key}" if special_properties.include?(key.to_s)
        props[key.to_sym] = value
        props
      end
    end
  
    def encoded_data(parameters)
      Base64.encode64(JSON.generate(parameters)).gsub(/\n/,'')
    end
  
    def request(url, async)
      async ? send_async(url) : open(url).read
    end
  
    def parse_response(response)
      response.to_i == 1
    end
  
    def send_async(url)
      w = Mixpanel::Tracker.worker
      begin
        url << "\n"
        w.write url
        1
      rescue Errno::EPIPE => e
        Mixpanel::Tracker.dispose_worker w
        0
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mixpanel-3.1.0 lib/mixpanel/tracker.rb
mixpanel-3.0.2 lib/mixpanel/tracker.rb
mixpanel-3.0.1 lib/mixpanel/tracker.rb