Sha256: fa3a46f549b6edccb8d01766b7a127d5a1da338d3f0fb3ab7d2f379653bd502c

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

require_relative 'analytics_ingester_client'
require_relative 'action_launch_context'
require_relative 'analytics_event_builder'

module PantographCore
  class AnalyticsSession
    GA_TRACKING = 'UA-153995158-1'

    private_constant :GA_TRACKING
    attr_accessor :session_id
    attr_accessor :client

    def initialize(analytics_ingester_client: AnalyticsIngesterClient.new(GA_TRACKING))
      require 'securerandom'
      @session_id = SecureRandom.uuid
      @client = analytics_ingester_client
      @threads = []
      @launch_event_sent = false
    end

    def action_launched(launch_context: nil)
      unless did_show_message?
        show_message
      end

      if @launch_event_sent || launch_context.p_hash.nil?
        return
      end

      @launch_event_sent = true
      builder = AnalyticsEventBuilder.new(
        p_hash: launch_context.p_hash,
        session_id: session_id,
        action_name: nil,
        pantograph_client_language: launch_context.pantograph_client_language
      )

      launch_event = builder.new_event(:launch)
      post_thread = client.post_event(launch_event)
      unless post_thread.nil?
        @threads << post_thread
      end
    end

    def action_completed(completion_context: nil)
    end

    def show_message
      UI.message("Sending anonymous analytics information")
      UI.message("Learn more at https://johnknapprs.github.io/pantograph/#metrics")
      UI.message("No personal or sensitive data is sent.")
      UI.message("You can disable this by adding `opt_out_usage` at the top of your Pantfile")
    end

    def did_show_message?
      file_name = ".did_show_opt_info"

      new_path = File.join(PantographCore.pantograph_user_dir, file_name)
      did_show = File.exist?(new_path)

      return did_show if did_show

      File.write(new_path, '1')
      false
    end

    def finalize_session

      @threads.map(&:join)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pantograph-0.1.7 pantograph_core/lib/pantograph_core/analytics/analytics_session.rb