Class: Flagsmith::AnalyticsProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/flagsmith/sdk/analytics_processor.rb

Overview

Used to control how often we send data(in seconds)

Constant Summary collapse

ENDPOINT =
'analytics/flags/'
TIMER =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ AnalyticsProcessor

AnalyticsProcessor is used to track how often individual Flags are evaluated within the Flagsmith SDK. Docs: docs.flagsmith.com/advanced-use/flag-analytics.

data environment key obtained from the Flagsmith UI data base api url to override when using self hosted version data used to tell requests to stop waiting for a response after a

given number of seconds


17
18
19
20
21
22
23
# File 'lib/flagsmith/sdk/analytics_processor.rb', line 17

def initialize(data)
  @last_flushed = Time.now
  @analytics_data = {}
  @api_client = data.fetch(:api_client)
  @timeout = data.fetch(:timeout, 3)
  @logger = data.fetch(:logger)
end

Instance Attribute Details

#analytics_dataObject (readonly)

Returns the value of attribute analytics_data.



8
9
10
# File 'lib/flagsmith/sdk/analytics_processor.rb', line 8

def analytics_data
  @analytics_data
end

#last_flushedObject (readonly)

Returns the value of attribute last_flushed.



8
9
10
# File 'lib/flagsmith/sdk/analytics_processor.rb', line 8

def last_flushed
  @last_flushed
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



8
9
10
# File 'lib/flagsmith/sdk/analytics_processor.rb', line 8

def timeout
  @timeout
end

Instance Method Details

#flushObject

Sends all the collected data to the api asynchronously and resets the timer



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/flagsmith/sdk/analytics_processor.rb', line 26

def flush
  return if @analytics_data.empty?

  begin
    @api_client.post(ENDPOINT, @analytics_data.to_json)
    @analytics_data = {}
  rescue StandardError => e
    @logger.warn "Temporarily unable to access flag analytics endpoint for exception: #{e}"
  end

  @last_flushed = Time.now
end

#track_feature(feature_name) ⇒ Object



39
40
41
42
# File 'lib/flagsmith/sdk/analytics_processor.rb', line 39

def track_feature(feature_name)
  @analytics_data[feature_name] = @analytics_data.fetch(feature_name, 0) + 1
  flush if (Time.now - @last_flushed) > TIMER
end