Class: Satellite::Adapters::GoogleAnalytics::Utme

Inherits:
Object
  • Object
show all
Defined in:
lib/satellite/adapters/google_analytics.rb

Defined Under Namespace

Classes: CustomVariable, CustomVariables, Event

Constant Summary collapse

@@regex_event =
/5\((\w+)\*(\w+)(\*(\w+))?\)(\((\d+)\))?/
@@regex_custom_variables =
/8\(([^\)]*)\)9\(([^\)]*)\)(11\(([^\)]*)\))?/
@@regex_custom_variable_value =
/((\d)!)?([^\(\*]+)/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUtme

Returns a new instance of Utme.



107
108
109
# File 'lib/satellite/adapters/google_analytics.rb', line 107

def initialize
  @custom_variables = CustomVariables.new
end

Class Method Details

.from_string(str) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/satellite/adapters/google_analytics.rb', line 150

def from_string(str)
  utme = self.new

  # parse event
  str.gsub!(@@regex_event) do |match|
    utme.set_event($1, $2, $4, $6)
    ''
  end

  # parse custom variables
  str.gsub!(@@regex_custom_variables) do |match|
    custom_vars = { }
    match_names, match_values, match_scopes = $1, $2, $4

    names = match_names.to_s.split('*')
    values = match_values.to_s.split('*')
    scopes = match_scopes.to_s.split('*')

    raise ArgumentError, "Each custom variable must have a value defined." if names.length != values.length

    names.each_with_index do |raw_name, i|
      match_data = raw_name.match(@@regex_custom_variable_value)
      slot, name = (match_data[2] || i+1).to_i, match_data[3]
      custom_vars[slot] = { :name => name }
    end

    values.each_with_index do |raw_value, i|
      match_data = raw_value.match(@@regex_custom_variable_value)
      slot, value = (match_data[2] || i+1).to_i, match_data[3]
      custom_vars[slot][:value] = value
    end

    scopes.each_with_index do |raw_scope, i|
      match_data = raw_scope.match(@@regex_custom_variable_value)
      slot, scope = (match_data[2] || i+1).to_i, match_data[3]
      # silently ignore scope if there's no corresponding custom variable
      custom_vars[slot][:scope] = scope if custom_vars[slot]
    end

    # finally set all the gathered custom vars
    custom_vars.each do |key, custom_var|
      utme.set_custom_variable(key, custom_var[:name], custom_var[:value], custom_var[:scope])
    end
    ''
  end

  utme
end

.parse(args) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/satellite/adapters/google_analytics.rb', line 138

def parse(args)
  return self.new if args.nil?
  case args
    when String
      return self.from_string(args.dup)
    when self
      return args
    else
      raise ArgumentError, "Could parse argument neither as String nor GATracker::Utme"
  end
end

Instance Method Details

#set_custom_variable(slot, name, value, scope = nil) ⇒ Object



116
117
118
119
# File 'lib/satellite/adapters/google_analytics.rb', line 116

def set_custom_variable(slot, name, value, scope=nil)
  @custom_variables.set_custom_variable(slot, CustomVariable.new(name, value, scope))
  self
end

#set_event(category, action, label = nil, value = nil) ⇒ Object



111
112
113
114
# File 'lib/satellite/adapters/google_analytics.rb', line 111

def set_event(category, action, label=nil, value=nil)
  @event = Event.new(category, action, label, value)
  self
end

#to_sObject Also known as: to_param



126
127
128
# File 'lib/satellite/adapters/google_analytics.rb', line 126

def to_s
  @event.to_s + @custom_variables.to_s
end

#unset_custom_variable(slot) ⇒ Object



121
122
123
124
# File 'lib/satellite/adapters/google_analytics.rb', line 121

def unset_custom_variable(slot)
  @custom_variables.unset_custom_variable(slot)
  self
end