Class: Omnitrack::Adapters::Base

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

Overview

All platform adapters inherit from this class.

Subclasses MUST implement:

- #track_event(event_name, payload)
- #track_conversion(data)
- #identify_user(user_data)

Subclasses MAY override:

- #enabled?        — to add extra guards
- #validate_config — to check required keys

Direct Known Subclasses

GoogleAds, GoogleAnalytics, Meta, Snapchat, TikTok

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: {}, logger: nil) ⇒ Base

Returns a new instance of Base.



39
40
41
42
43
# File 'lib/omnitrack/adapters/base.rb', line 39

def initialize(config: {}, logger: nil)
  @config = config.transform_keys(&:to_sym)
  @logger = logger || Omnitrack.logger
  validate_config
end

Class Attribute Details

.adapter_nameObject



26
27
28
# File 'lib/omnitrack/adapters/base.rb', line 26

def adapter_name
  @adapter_name || name.to_s.split("::").last.underscore.to_sym
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

Class Method Details

.inherited(subclass) ⇒ Object

Convenience: register a custom adapter with the dispatcher



31
32
33
34
# File 'lib/omnitrack/adapters/base.rb', line 31

def inherited(subclass)
  super
  Omnitrack::Registry.register(subclass) if defined?(Omnitrack::Registry)
end

Instance Method Details

#enabled?Boolean


Helpers available to subclasses


Returns:

  • (Boolean)


75
76
77
# File 'lib/omnitrack/adapters/base.rb', line 75

def enabled?
  config.fetch(:enabled, false)
end

#identify_user(user_data = {}) ⇒ Omnitrack::Result

Identify / associate a user.

Parameters:

  • user_data (Hash) (defaults to: {})

    may include :email, :phone, :external_id

Returns:

Raises:

  • (NotImplementedError)


67
68
69
# File 'lib/omnitrack/adapters/base.rb', line 67

def identify_user(user_data = {})
  raise NotImplementedError, "#{self.class}#identify_user not implemented"
end

#nameObject



79
80
81
# File 'lib/omnitrack/adapters/base.rb', line 79

def name
  self.class.adapter_name
end

#track_conversion(data = {}) ⇒ Omnitrack::Result

Track a conversion (purchase, lead, etc.).

Parameters:

  • data (Hash) (defaults to: {})

Returns:

Raises:

  • (NotImplementedError)


60
61
62
# File 'lib/omnitrack/adapters/base.rb', line 60

def track_conversion(data = {})
  raise NotImplementedError, "#{self.class}#track_conversion not implemented"
end

#track_event(event_name, payload = {}) ⇒ Omnitrack::Result

Track a named event.

Parameters:

  • event_name (String, Symbol)
  • payload (Hash) (defaults to: {})

    event-specific data

Returns:

Raises:

  • (NotImplementedError)


53
54
55
# File 'lib/omnitrack/adapters/base.rb', line 53

def track_event(event_name, payload = {})
  raise NotImplementedError, "#{self.class}#track_event not implemented"
end