Sha256: 8b5e469369af5405fe05e652e93be3a0fc42e89c043212c8ba2db179acf1b477

Contents?: true

Size: 1.89 KB

Versions: 15

Compression:

Stored size: 1.89 KB

Contents

module MinimumViableProduct
  module AnalyticsConcern
    extend ActiveSupport::Concern

    INVISIBLE_SESSION_COOKIE = 'invisible'

    included do
      before_action :generate_uid_if_not_set
      helper_method :analytics_id
    end

    def track!(event, properties={})
      return if session[INVISIBLE_SESSION_COOKIE].to_b == true
      return unless analytics
      analytics.track({
        user_id: analytics_id,
        event: event,
        properties: properties.reverse_merge({
          iteration: MVP::Iteration.version,
          user: current_user.try(:name),
          url: request.fullpath
        })
      })
    end

    def slack!(message)
      if ENV['SLACK_WEBHOOK_URL']
        begin
          slack_notifier.ping message
        rescue
          slack_notifier.ping "Tried to use slack messaging but failure happened"
        end
      end
    end

    def page!(name, opts={})
      return if sessions[INVISIBLE_SESSION_COOKIE].to_b == true
      return unless analytics
      analytics.page(
        user_id: current_user.try(:id),
        name: name,
        category: opts[:category],
        properties: {
          url: opts[:url],
          iteration: MVP::Iteration.version
        }
      )
    end

    private

    def analytics
      unless defined?(Analytics)
        puts ""
        puts "Please set SEGMENT_WRITE_KEY in .env"
        puts ""
        nil
      else
        Analytics
      end
    end

    def slack_notifier
      Slack::Notifier.new ENV['SLACK_WEBHOOK_URL'], channel: ENV['SLACK_WEBHOOK_URL'],
                                                    username: ENV['SLACK_POST_USERNAME'],
                                                    channel: ENV['SLACK_POST_CHANNEL']
    end

    def analytics_id
      current_user.try(:id) || cookies[:uid]
    end

    def generate_uid_if_not_set
      cookies[:uid] = SecureRandom.uuid unless cookies[:uid].present?
    end
  end
end

Version data entries

15 entries across 14 versions & 2 rubygems

Version Path
minimum_viable_product-0.0.18 app/controllers/concerns/minimum_viable_product/analytics_concern.rb
minimum_viable_product-0.0.17 app/controllers/concerns/minimum_viable_product/analytics_concern.rb
minimum_viable_product-0.0.16 app/controllers/concerns/minimum_viable_product/analytics_concern.rb
minimum_viable_product-0.0.15 app/controllers/concerns/minimum_viable_product/analytics_concern.rb
minimum_viable_product-0.0.14 app/controllers/concerns/minimum_viable_product/analytics_concern.rb
minimum_viable_product-0.0.13 app/controllers/concerns/minimum_viable_product/analytics_concern.rb
minimum_viable_product-0.0.11 app/controllers/concerns/minimum_viable_product/analytics_concern.rb
minimum_viable_product-0.0.11 test/dummy/node_modules/minimum_viable_product/app/controllers/minimum_viable_product/concerns/analytics_concern.rb
minimum_viable_product-0.0.10 app/controllers/minimum_viable_product/concerns/analytics_concern.rb
minimum_viable_product-0.0.9 app/controllers/minimum_viable_product/concerns/analytics_concern.rb
minimum_viable_product-0.0.8 app/controllers/minimum_viable_product/concerns/analytics_concern.rb
minimum-viable-product-0.0.7 app/controllers/mvp/concerns/analytics_concern.rb
minimum-viable-product-0.0.6 app/controllers/mvp/concerns/analytics_concern.rb
minimum-viable-product-0.0.5 app/controllers/mvp/concerns/analytics_concern.rb
minimum-viable-product-0.0.4 app/controllers/mvp/concerns/analytics_concern.rb