Module: LogStruct::Integrations::Ahoy

Extended by:
T::Sig
Defined in:
lib/log_struct/integrations/ahoy.rb

Overview

Ahoy analytics integration. If Ahoy is present, prepend a small hook to Ahoy::Tracker#track to emit a structured log for analytics events.

Class Method Summary collapse

Class Method Details

.setup(config) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/log_struct/integrations/ahoy.rb', line 12

def self.setup(config)
  return nil unless defined?(::Ahoy)

  if defined?(::Ahoy::Tracker)
    mod = Module.new do
      extend T::Sig

      sig { params(name: T.untyped, properties: T.nilable(T::Hash[T.untyped, T.untyped]), options: T.untyped).returns(T.untyped) }
      def track(name, properties = nil, options = {})
        result = super
        begin
          # Emit a lightweight structured log about the analytics event
          data = {
            ahoy_event: T.let(name, T.untyped)
          }
          data[:properties] = properties if properties
          LogStruct.info(
            LogStruct::Log::Ahoy.new(
              message: "ahoy.track",
              ahoy_event: T.must(T.let(name, T.nilable(String))),
              properties: T.let(
                properties && properties.transform_keys { |k| k.to_sym },
                T.nilable(T::Hash[Symbol, T.untyped])
              )
            )
          )
        rescue => e
          # Never raise from logging; rely on global error handling policies
          LogStruct.handle_exception(e, source: LogStruct::Source::App, context: {integration: :ahoy})
        end
        result
      end
    end

    T.unsafe(::Ahoy::Tracker).prepend(mod)
  end

  true
end