Sha256: 74704a053c6c2c3c9fde0c908556c4c0b770270f0d540915fa3713e5f605b7e0

Contents?: true

Size: 969 Bytes

Versions: 1

Compression:

Stored size: 969 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 status == 302
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mixpal-0.4.0 lib/mixpal/integration.rb