Class: Omnitrack::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/omnitrack/context.rb

Overview

Captures and exposes tracking parameters from a Rack request. Works in both cookie-based (full-stack) and cookie-free (API-only) modes.

Stored per-request in thread-local storage via Omnitrack::Context.current.

Constant Summary collapse

CLICK_ID_PARAMS =
%w[gclid fbclid ttclid sclid msclkid].freeze
UTM_PARAMS =
%w[utm_source utm_medium utm_campaign utm_term utm_content].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip: nil, user_agent: nil, click_ids: {}, utm_params: {}, cookies: {}, headers: {}, custom_data: {}) ⇒ Context

Returns a new instance of Context.



40
41
42
43
44
45
46
47
48
49
# File 'lib/omnitrack/context.rb', line 40

def initialize(ip: nil, user_agent: nil, click_ids: {}, utm_params: {},
               cookies: {}, headers: {}, custom_data: {})
  @ip          = ip
  @user_agent  = user_agent
  @click_ids   = click_ids.transform_keys(&:to_sym)
  @utm_params  = utm_params.transform_keys(&:to_sym)
  @cookies     = cookies
  @headers     = headers
  @custom_data = custom_data || {}
end

Instance Attribute Details

#click_idsObject (readonly)

Returns the value of attribute click_ids.



37
38
39
# File 'lib/omnitrack/context.rb', line 37

def click_ids
  @click_ids
end

#cookiesObject (readonly)

Returns the value of attribute cookies.



37
38
39
# File 'lib/omnitrack/context.rb', line 37

def cookies
  @cookies
end

#custom_dataObject (readonly)

Returns the value of attribute custom_data.



37
38
39
# File 'lib/omnitrack/context.rb', line 37

def custom_data
  @custom_data
end

#headersObject (readonly)

Returns the value of attribute headers.



37
38
39
# File 'lib/omnitrack/context.rb', line 37

def headers
  @headers
end

#ipObject (readonly)

Returns the value of attribute ip.



37
38
39
# File 'lib/omnitrack/context.rb', line 37

def ip
  @ip
end

#user_agentObject (readonly)

Returns the value of attribute user_agent.



37
38
39
# File 'lib/omnitrack/context.rb', line 37

def user_agent
  @user_agent
end

#utm_paramsObject (readonly)

Returns the value of attribute utm_params.



37
38
39
# File 'lib/omnitrack/context.rb', line 37

def utm_params
  @utm_params
end

Class Method Details

.clear!Object



21
22
23
# File 'lib/omnitrack/context.rb', line 21

def self.clear!
  Thread.current[:omnitrack_context] = nil
end

.currentObject

Thread-local current context — set by the middleware



13
14
15
# File 'lib/omnitrack/context.rb', line 13

def self.current
  Thread.current[:omnitrack_context]
end

.current=(ctx) ⇒ Object



17
18
19
# File 'lib/omnitrack/context.rb', line 17

def self.current=(ctx)
  Thread.current[:omnitrack_context] = ctx
end

.from_request(request) ⇒ Object

Build a Context from a Rack::Request object



26
27
28
29
30
31
32
33
34
35
# File 'lib/omnitrack/context.rb', line 26

def self.from_request(request)
  new(
    ip:          extract_ip(request),
    user_agent:  request.env["HTTP_USER_AGENT"],
    click_ids:   extract_click_ids(request),
    utm_params:  extract_utm_params(request),
    cookies:     safe_cookies(request),
    headers:     extract_tracking_headers(request)
  )
end

Instance Method Details

#fbclidObject



61
62
63
# File 'lib/omnitrack/context.rb', line 61

def fbclid
  click_ids[:fbclid]
end

#gclidObject



57
58
59
# File 'lib/omnitrack/context.rb', line 57

def gclid
  click_ids[:gclid]
end

#merge!(data = {}) ⇒ Object

Merge extra data into the context (e.g., from a controller concern)



52
53
54
55
# File 'lib/omnitrack/context.rb', line 52

def merge!(data = {})
  @custom_data.merge!(data.to_h)
  self
end

#to_hObject

Full context as a hash — passed into adapter payloads



70
71
72
73
74
75
76
77
78
# File 'lib/omnitrack/context.rb', line 70

def to_h
  {
    ip:          @ip,
    user_agent:  @user_agent,
    click_ids:   @click_ids,
    utm_params:  @utm_params,
    custom_data: @custom_data
  }.reject { |_, v| v.nil? || (v.respond_to?(:empty?) && v.empty?) }
end

#ttclidObject



65
66
67
# File 'lib/omnitrack/context.rb', line 65

def ttclid
  click_ids[:ttclid]
end