module Forwardlytics

Constants

VERSION

Public Class Methods

exception_handler(ex) click to toggle source
# File lib/forwardlytics.rb, line 44
def self.exception_handler(ex)
  if @exception_handler
    @exception_handler.call(ex)
  end
end
exception_handler=(handler) click to toggle source
# File lib/forwardlytics.rb, line 40
def self.exception_handler=(handler)
  @exception_handler = handler
end
identify(user_id:, traits: {}) click to toggle source
# File lib/forwardlytics/identify.rb, line 2
def self.identify(user_id:, traits: {})
  params = {
    userID: user_id,
    userTraits: traits,
    timestamp: Time.now().to_i
  }

  t = Thread.new { post('/identify', params) }
end
post(uri, params) click to toggle source
# File lib/forwardlytics.rb, line 13
def self.post(uri, params)
  begin
    # Forces the user_id to be a string, which is the expected format by Forwardlytics (the server part)
    params[:userID] = params[:userID].to_s unless params[:userID].nil?

    full_url = URI("#{FORWARDLYTICS_URL}#{uri}")
    http = Net::HTTP.new(full_url.host, full_url.port)
    if full_url.port == 443
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_PEER
    end
    req = Net::HTTP::Post.new(full_url, initheader = {'Content-Type' =>'application/json'})
    req['Forwardlytics-Api-Key'] = FORWARDLYTICS_API_KEY
    req.body = params.to_json
    res = http.request(req)

    case res
    when Net::HTTPSuccess, Net::HTTPRedirection
      # OK
    else
      raise NetException, "Potential problem posting to Forwardlytics (#{full_url}) - #{res.inspect} - #{res.code} - #{res.body}"
    end
  rescue Exception => ex
    exception_handler(ex)
  end
end
track(event:, user_id:, properties: {}) click to toggle source
# File lib/forwardlytics/track.rb, line 2
def self.track(event:, user_id:, properties: {})
  params = {
    name: event,
    userID: user_id,
    properties: properties,
    timestamp: Time.now().to_i
  }

  t = Thread.new { post('/track', params) }
end