Class: Itly::Plugin::Mixpanel

Inherits:
Itly::Plugin show all
Defined in:
lib/itly/plugin/mixpanel/mixpanel.rb,
lib/itly/plugin/mixpanel/options.rb,
lib/itly/plugin/mixpanel/version.rb,
lib/itly/plugin/mixpanel/call_options.rb,
lib/itly/plugin/mixpanel/error_handler.rb

Overview

Mixpanel plugin class for Itly SDK

Defined Under Namespace

Classes: CallOptions, ErrorHandler, Options

Constant Summary collapse

VERSION =
'0.1.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_token:, disabled: false) ⇒ Mixpanel

Instantiate a new Plugin::Mixpanel

Parameters:

  • project_token: (String)

    specify the Mixpanel project token

  • disabled: (TrueClass/FalseClass) (defaults to: false)

    set to true to disable the plugin. Default to false



22
23
24
25
26
# File 'lib/itly/plugin/mixpanel/mixpanel.rb', line 22

def initialize(project_token:, disabled: false)
  super()
  @project_token = project_token
  @disabled = disabled
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



14
15
16
# File 'lib/itly/plugin/mixpanel/mixpanel.rb', line 14

def client
  @client
end

#disabledObject (readonly)

Returns the value of attribute disabled.



14
15
16
# File 'lib/itly/plugin/mixpanel/mixpanel.rb', line 14

def disabled
  @disabled
end

Instance Method Details

#alias(user_id:, previous_id:, options: nil) ⇒ Object

Associate one user ID with another (typically a known user ID with an anonymous one).

Raise an error if the client fails

Parameters:

  • user_id: (String)

    The ID that the user will be identified by going forward. This is typically the user’s database ID (as opposed to an anonymous ID), or their updated ID (for example, if the ID is an email address which the user just updated).

  • previous_id: (String)

    The ID the user has been identified by so far.

  • options: (Itly::Plugin::Mixpanel::AliasOptions) (defaults to: nil)

    the plugin specific options



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/itly/plugin/mixpanel/mixpanel.rb', line 105

def alias(user_id:, previous_id:, options: nil)
  super
  return unless enabled?

  # Log
  log = Itly::Loggers.vars_to_log user_id: user_id, previous_id: previous_id, options: options
  @logger&.info "#{id}: alias(#{log})"

  # Send through the client
  @client.alias user_id, previous_id
end

#idString

Get the plugin ID

Returns:

  • (String)

    plugin id



122
123
124
# File 'lib/itly/plugin/mixpanel/mixpanel.rb', line 122

def id
  'mixpanel'
end

#identify(user_id:, properties: nil, options: nil) ⇒ Object

Identify a user

Raise an error if the client fails

Parameters:

  • user_id: (String)

    the id of the user in your application

  • properties: (Hash) (defaults to: nil)

    the properties containing user’s traits to pass to your application

  • options: (Itly::Plugin::Mixpanel::IdentifyOptions) (defaults to: nil)

    the plugin specific options



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/itly/plugin/mixpanel/mixpanel.rb', line 59

def identify(user_id:, properties: nil, options: nil)
  super
  return unless enabled?

  # Log
  log = Itly::Loggers.vars_to_log user_id: user_id, properties: properties, options: options
  @logger&.info "#{id}: identify(#{log})"

  # Send through the client
  @client.people.set user_id, properties
end

#load(options:) ⇒ Object

Initialize Mixpanel::Tracker client

Parameters:

  • options: (Itly::PluginOptions)

    plugin options



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/itly/plugin/mixpanel/mixpanel.rb', line 33

def load(options:)
  super
  # Get options
  @logger = options.logger

  # Log
  @logger&.info "#{id}: load()"

  if @disabled
    @logger&.info "#{id}: plugin is disabled!"
    return
  end

  # Configure client
  @client = ::Mixpanel::Tracker.new @project_token, ErrorHandler.new
end

#nameObject

Mixpanel specific plugin options class for calls to plugin methods



16
17
18
19
20
21
22
23
# File 'lib/itly/plugin/mixpanel/call_options.rb', line 16

%w[Identify Group Page Track Alias].each do |name|
  class_eval(
    <<-EVAL, __FILE__, __LINE__ + 1
      class #{name}Options < CallOptions         # class IdentifyOptions < CallOptions
      end                                        # end
    EVAL
  )
end

#track(user_id:, event:, options: nil) ⇒ Object

Track an event

Raise an error if the client fails

Parameters:

  • user_id: (String)

    the id of the user in your application

  • event: (Event)

    the Event object to pass to your application

  • options: (Itly::Plugin::Mixpanel::TrackOptions) (defaults to: nil)

    the plugin specific options



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/itly/plugin/mixpanel/mixpanel.rb', line 80

def track(user_id:, event:, options: nil)
  super
  return unless enabled?

  # Log
  log = Itly::Loggers.vars_to_log(
    user_id: user_id, event: event&.name, properties: event&.properties, options: options
  )
  @logger&.info "#{id}: track(#{log})"

  # Send through the client
  @client.track user_id, event.name, event.properties
end