Sha256: c326b2fec805d514979d2870f7d0a2975fc002936d68d4251e9780ef31b6d131

Contents?: true

Size: 981 Bytes

Versions: 3

Compression:

Stored size: 981 Bytes

Contents

module Mixpal
  module Integration
    extend ActiveSupport::Concern

    included do
      helper_method :mixpanel
      if Rails::VERSION::MAJOR >= 4
        after_action :store_mixpanel_if_redirecting
      else
        after_filter :store_mixpanel_if_redirecting
      end

      class_attribute :mixpanel_identity_data
      def self.mixpanel_identity(object_method, attribute_method)
        self.mixpanel_identity_data = {
          object_method: object_method,
          attribute_method: attribute_method
        }
      end
    end

    def mixpanel
      @mixpanel ||= begin
        identity = if (data = self.class.mixpanel_identity_data)
                     send(data[:object_method]).try(data[:attribute_method])
                   end

        Mixpal::Tracker.new(identity: identity).tap { |t| t.restore!(session) }
      end
    end

    private

    def store_mixpanel_if_redirecting
      mixpanel.store!(session) if (300..399).cover?(status)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mixpal-0.5.0 lib/mixpal/integration.rb
mixpal-0.4.2 lib/mixpal/integration.rb
mixpal-0.4.1 lib/mixpal/integration.rb