Class: Omnitrack::Context
- Inherits:
-
Object
- Object
- Omnitrack::Context
- 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
-
#click_ids ⇒ Object
readonly
Returns the value of attribute click_ids.
-
#cookies ⇒ Object
readonly
Returns the value of attribute cookies.
-
#custom_data ⇒ Object
readonly
Returns the value of attribute custom_data.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#ip ⇒ Object
readonly
Returns the value of attribute ip.
-
#user_agent ⇒ Object
readonly
Returns the value of attribute user_agent.
-
#utm_params ⇒ Object
readonly
Returns the value of attribute utm_params.
Class Method Summary collapse
- .clear! ⇒ Object
-
.current ⇒ Object
Thread-local current context — set by the middleware.
- .current=(ctx) ⇒ Object
-
.from_request(request) ⇒ Object
Build a Context from a Rack::Request object.
Instance Method Summary collapse
- #fbclid ⇒ Object
- #gclid ⇒ Object
-
#initialize(ip: nil, user_agent: nil, click_ids: {}, utm_params: {}, cookies: {}, headers: {}, custom_data: {}) ⇒ Context
constructor
A new instance of Context.
-
#merge!(data = {}) ⇒ Object
Merge extra data into the context (e.g., from a controller concern).
-
#to_h ⇒ Object
Full context as a hash — passed into adapter payloads.
- #ttclid ⇒ Object
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 = @headers = headers @custom_data = custom_data || {} end |
Instance Attribute Details
#click_ids ⇒ Object (readonly)
Returns the value of attribute click_ids.
37 38 39 |
# File 'lib/omnitrack/context.rb', line 37 def click_ids @click_ids end |
#cookies ⇒ Object (readonly)
Returns the value of attribute cookies.
37 38 39 |
# File 'lib/omnitrack/context.rb', line 37 def @cookies end |
#custom_data ⇒ Object (readonly)
Returns the value of attribute custom_data.
37 38 39 |
# File 'lib/omnitrack/context.rb', line 37 def custom_data @custom_data end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
37 38 39 |
# File 'lib/omnitrack/context.rb', line 37 def headers @headers end |
#ip ⇒ Object (readonly)
Returns the value of attribute ip.
37 38 39 |
# File 'lib/omnitrack/context.rb', line 37 def ip @ip end |
#user_agent ⇒ Object (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_params ⇒ Object (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 |
.current ⇒ Object
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: (request), headers: extract_tracking_headers(request) ) end |
Instance Method Details
#fbclid ⇒ Object
61 62 63 |
# File 'lib/omnitrack/context.rb', line 61 def fbclid click_ids[:fbclid] end |
#gclid ⇒ Object
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_h ⇒ Object
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 |
#ttclid ⇒ Object
65 66 67 |
# File 'lib/omnitrack/context.rb', line 65 def ttclid click_ids[:ttclid] end |